modFacture.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /* Copyright (C) 2003-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  5. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  6. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  7. * Copyright (C) 2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \defgroup facture Module customer invoices
  24. * \brief Module to manage customer invoices
  25. * \file htdocs/core/modules/modFacture.class.php
  26. * \ingroup facture
  27. * \brief Description and activation file for the module customer invoices
  28. */
  29. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  30. /**
  31. * Class to describe module customer invoices
  32. */
  33. class modFacture extends DolibarrModules
  34. {
  35. /**
  36. * Constructor. Define names, constants, directories, boxes, permissions
  37. *
  38. * @param DoliDB $db Database handler
  39. */
  40. public function __construct($db)
  41. {
  42. global $conf, $user;
  43. $this->db = $db;
  44. $this->numero = 30;
  45. $this->family = "financial";
  46. $this->module_position = '11';
  47. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  48. $this->name = preg_replace('/^mod/i', '', get_class($this));
  49. $this->description = "Gestion des factures";
  50. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  51. $this->version = 'dolibarr';
  52. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  53. $this->picto = 'bill';
  54. // Data directories to create when module is enabled
  55. $this->dirs = array("/facture/temp");
  56. // Dependencies
  57. $this->depends = array('always'=>"modSociete");
  58. $this->requiredby = array("modComptabilite", "modAccounting");
  59. $this->conflictwith = array();
  60. $this->langfiles = array("bills", "companies", "compta", "products");
  61. $this->warnings_activation = array(); // Warning to show when we activate module. array('always'='text') or array('FR'='text')
  62. $this->warnings_activation_ext = array(); // Warning to show when we activate an external module. array('always'='text') or array('FR'='text')
  63. // Config pages
  64. $this->config_page_url = array("facture.php");
  65. // Constants
  66. $this->const = array();
  67. $r = 0;
  68. $this->const[$r][0] = "FACTURE_ADDON";
  69. $this->const[$r][1] = "chaine";
  70. $this->const[$r][2] = "mod_facture_terre";
  71. $this->const[$r][3] = 'Name of numbering numerotation rules of invoice';
  72. $this->const[$r][4] = 0;
  73. $r++;
  74. $this->const[$r][0] = "FACTURE_ADDON_PDF";
  75. $this->const[$r][1] = "chaine";
  76. $this->const[$r][2] = "crabe";
  77. $this->const[$r][3] = 'Name of PDF model of invoice';
  78. $this->const[$r][4] = 0;
  79. $r++;
  80. $this->const[$r][0] = "FACTURE_ADDON_PDF_ODT_PATH";
  81. $this->const[$r][1] = "chaine";
  82. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/invoices";
  83. $this->const[$r][3] = "";
  84. $this->const[$r][4] = 0;
  85. $r++;
  86. /*$this->const[$r][0] = "FACTURE_DRAFT_WATERMARK";
  87. $this->const[$r][1] = "chaine";
  88. $this->const[$r][2] = "__(Draft)__";
  89. $this->const[$r][3] = 'Watermark to show on draft invoices';
  90. $this->const[$r][4] = 0;
  91. $r++;*/
  92. // Boxes
  93. //$this->boxes = array(0=>array(1=>'box_factures_imp.php'),1=>array(1=>'box_factures.php'));
  94. $this->boxes = array(
  95. 0=>array('file'=>'box_factures_imp.php', 'enabledbydefaulton'=>'Home'),
  96. 1=>array('file'=>'box_factures.php', 'enabledbydefaulton'=>'Home'),
  97. 2=>array('file'=>'box_graph_invoices_permonth.php', 'enabledbydefaulton'=>'Home'),
  98. 3=>array('file'=>'box_customers_outstanding_bill_reached.php', 'enabledbydefaulton'=>'Home')
  99. );
  100. // Cronjobs
  101. $arraydate = dol_getdate(dol_now());
  102. $datestart = dol_mktime(23, 0, 0, $arraydate['mon'], $arraydate['mday'], $arraydate['year']);
  103. $this->cronjobs = array(
  104. 0 => array(
  105. 'label'=>'RecurringInvoices',
  106. 'jobtype'=>'method',
  107. 'class'=>'compta/facture/class/facture-rec.class.php',
  108. 'objectname'=>'FactureRec',
  109. 'method'=>'createRecurringInvoices',
  110. 'parameters'=>'',
  111. 'comment'=>'Generate recurring invoices',
  112. 'frequency'=>1,
  113. 'unitfrequency'=>3600 * 24,
  114. 'priority'=>50,
  115. 'status'=>1,
  116. 'test'=>'$conf->facture->enabled',
  117. 'datestart'=>$datestart
  118. ),
  119. 1 => array(
  120. 'label'=>'SendEmailsRemindersOnInvoiceDueDate',
  121. 'jobtype'=>'method',
  122. 'class'=>'compta/facture/class/facture.class.php',
  123. 'objectname'=>'Facture',
  124. 'method'=>'sendEmailsRemindersOnInvoiceDueDate',
  125. 'parameters'=>"10,all,EmailTemplateCode",
  126. 'comment'=>'Send an emails when the unpaid invoices reach a due date + n days = today. First param is the offset n of days, second parameter is "all" or a payment mode code, last paramater is the code of email template to use (an email template with EmailTemplateCode must exists. the version in the language of the thirdparty will be used in priority).',
  127. 'frequency'=>1,
  128. 'unitfrequency'=>3600 * 24,
  129. 'priority'=>50,
  130. 'status'=>0,
  131. 'test'=>'$conf->facture->enabled',
  132. 'datestart'=>$datestart
  133. ),
  134. );
  135. // Permissions
  136. $this->rights = array();
  137. $this->rights_class = 'facture';
  138. $r = 0;
  139. $r++;
  140. $this->rights[$r][0] = 11;
  141. $this->rights[$r][1] = 'Read invoices';
  142. $this->rights[$r][2] = 'a';
  143. $this->rights[$r][3] = 0;
  144. $this->rights[$r][4] = 'lire';
  145. $r++;
  146. $this->rights[$r][0] = 12;
  147. $this->rights[$r][1] = 'Create and update invoices';
  148. $this->rights[$r][2] = 'a';
  149. $this->rights[$r][3] = 0;
  150. $this->rights[$r][4] = 'creer';
  151. // There is a particular permission for unvalidate because this may be not forbidden by some laws
  152. $r++;
  153. $this->rights[$r][0] = 13;
  154. $this->rights[$r][1] = 'Devalidate invoices';
  155. $this->rights[$r][2] = 'a';
  156. $this->rights[$r][3] = 0;
  157. $this->rights[$r][4] = 'invoice_advance';
  158. $this->rights[$r][5] = 'unvalidate';
  159. $r++;
  160. $this->rights[$r][0] = 14;
  161. $this->rights[$r][1] = 'Validate invoices';
  162. $this->rights[$r][2] = 'a';
  163. $this->rights[$r][3] = 0;
  164. $this->rights[$r][4] = 'invoice_advance';
  165. $this->rights[$r][5] = 'validate';
  166. $r++;
  167. $this->rights[$r][0] = 15;
  168. $this->rights[$r][1] = 'Send invoices by email';
  169. $this->rights[$r][2] = 'a';
  170. $this->rights[$r][3] = 0;
  171. $this->rights[$r][4] = 'invoice_advance';
  172. $this->rights[$r][5] = 'send';
  173. $r++;
  174. $this->rights[$r][0] = 16;
  175. $this->rights[$r][1] = 'Issue payments on invoices';
  176. $this->rights[$r][2] = 'a';
  177. $this->rights[$r][3] = 0;
  178. $this->rights[$r][4] = 'paiement';
  179. $r++;
  180. $this->rights[$r][0] = 19;
  181. $this->rights[$r][1] = 'Delete invoices';
  182. $this->rights[$r][2] = 'a';
  183. $this->rights[$r][3] = 0;
  184. $this->rights[$r][4] = 'supprimer';
  185. $r++;
  186. $this->rights[$r][0] = 1321;
  187. $this->rights[$r][1] = 'Export customer invoices, attributes and payments';
  188. $this->rights[$r][2] = 'r';
  189. $this->rights[$r][3] = 0;
  190. $this->rights[$r][4] = 'facture';
  191. $this->rights[$r][5] = 'export';
  192. $r++;
  193. $this->rights[$r][0] = 1322;
  194. $this->rights[$r][1] = 'Re-open a fully paid invoice';
  195. $this->rights[$r][2] = 'r';
  196. $this->rights[$r][3] = 0;
  197. $this->rights[$r][4] = 'invoice_advance';
  198. $this->rights[$r][5] = 'reopen';
  199. // Menus
  200. //-------
  201. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  202. // Exports
  203. //--------
  204. $r = 1;
  205. $alias_product_perentity = empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "p" : "ppe";
  206. $this->export_code[$r] = $this->rights_class.'_'.$r;
  207. $this->export_label[$r] = 'CustomersInvoicesAndInvoiceLines'; // Translation key (used only if key ExportDataset_xxx_z not found)
  208. $this->export_icon[$r] = 'invoice';
  209. $this->export_permission[$r] = array(array("facture", "facture", "export", "other"));
  210. $this->export_fields_array[$r] = array(
  211. 's.rowid'=>"IdCompany", 's.nom'=>'CompanyName', 'ps.nom' => 'ParentCompany', 's.code_client'=>'CustomerCode', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'c.code'=>'CountryCode', 'cd.nom'=>'State',
  212. 's.phone'=>'Phone',
  213. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4',
  214. 's.code_compta'=>'CustomerAccountancyCode',
  215. 's.code_compta_fournisseur'=>'SupplierAccountancyCode',
  216. 's.tva_intra'=>'VATIntra',
  217. 'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_client'=>'RefCustomer',
  218. 'f.type'=>"Type", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>"DateDue", 'f.total_ht'=>"TotalHT",
  219. 'f.total_ttc'=>"TotalTTC", 'f.total_tva'=>"TotalVAT", 'f.localtax1'=>'LT1', 'f.localtax2'=>'LT2', 'f.paye'=>"InvoicePaidCompletely", 'f.fk_statut'=>'InvoiceStatus', 'f.close_code'=>'EarlyClosingReason', 'f.close_note'=>'EarlyClosingComment',
  220. 'none.rest'=>'Rest',
  221. 'f.note_private'=>"NotePrivate", 'f.note_public'=>"NotePublic", 'f.fk_user_author'=>'CreatedById', 'uc.login'=>'CreatedByLogin',
  222. 'f.fk_user_valid'=>'ValidatedById', 'uv.login'=>'ValidatedByLogin', 'pj.ref'=>'ProjectRef', 'pj.title'=>'ProjectLabel', 'fd.rowid'=>'LineId', 'fd.description'=>"LineDescription",
  223. 'fd.subprice'=>"LineUnitPrice", 'fd.tva_tx'=>"LineVATRate", 'fd.qty'=>"LineQty", 'fd.total_ht'=>"LineTotalHT", 'fd.total_tva'=>"LineTotalVAT",
  224. 'fd.total_ttc'=>"LineTotalTTC", 'fd.date_start'=>"DateStart", 'fd.date_end'=>"DateEnd", 'fd.special_code'=>'SpecialCode',
  225. 'fd.product_type'=>"TypeOfLineServiceOrProduct", 'fd.fk_product'=>'ProductId', 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel',
  226. $alias_product_perentity . '.accountancy_code_sell'=>'ProductAccountancySellCode'
  227. );
  228. if (!empty($conf->multicurrency->enabled)) {
  229. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  230. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  231. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  232. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  233. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  234. }
  235. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->INVOICE_SHOW_POS)) {
  236. $this->export_fields_array[$r]['f.module_source'] = 'Module';
  237. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  238. }
  239. $this->export_TypeFields_array[$r] = array(
  240. 's.rowid'=>'Numeric', 's.nom'=>'Text', 'ps.nom'=>'Text', 's.code_client'=>'Text', 's.address'=>'Text', 's.zip'=>'Text', 's.town'=>'Text', 'c.code'=>'Text', 'cd.nom'=>'Text', 's.phone'=>'Text', 's.siren'=>'Text',
  241. 's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text',
  242. 'f.rowid'=>'Numeric', 'f.ref'=>"Text", 'f.ref_client'=>'Text', 'f.type'=>"Numeric", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.date_lim_reglement'=>"Date",
  243. 'f.total_ht'=>"Numeric", 'f.total_ttc'=>"Numeric", 'f.total_tva'=>"Numeric", 'f.localtax1'=>'Numeric', 'f.localtax2'=>'Numeric', 'f.paye'=>"Boolean", 'f.fk_statut'=>'Numeric', 'f.close_code'=>'Text', 'f.close_note'=>'Text',
  244. 'none.rest'=>"NumericCompute",
  245. 'f.note_private'=>"Text", 'f.note_public'=>"Text", 'f.fk_user_author'=>'Numeric', 'uc.login'=>'Text', 'f.fk_user_valid'=>'Numeric', 'uv.login'=>'Text',
  246. 'pj.ref'=>'Text', 'pj.title'=>'Text', 'fd.rowid'=>'Numeric', 'fd.label'=>'Text', 'fd.description'=>"Text", 'fd.subprice'=>"Numeric", 'fd.tva_tx'=>"Numeric",
  247. 'fd.qty'=>"Numeric", 'fd.total_ht'=>"Numeric", 'fd.total_tva'=>"Numeric", 'fd.total_ttc'=>"Numeric", 'fd.date_start'=>"Date", 'fd.date_end'=>"Date",
  248. 'fd.special_code'=>'Numeric', 'fd.product_type'=>"Numeric", 'fd.fk_product'=>'List:product:label', 'p.ref'=>'Text', 'p.label'=>'Text',
  249. $alias_product_perentity . '.accountancy_code_sell'=>'Text'
  250. );
  251. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->INVOICE_SHOW_POS)) {
  252. $this->export_TypeFields_array[$r]['f.module_source'] = 'Text';
  253. $this->export_TypeFields_array[$r]['f.pos_source'] = 'Text';
  254. }
  255. $this->export_entities_array[$r] = array(
  256. 's.rowid'=>"company", 's.nom'=>'company', 'ps.nom'=>'company', 's.code_client'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'c.code'=>'company', 'cd.nom'=>'company', 's.phone'=>'company',
  257. 's.siren'=>'company', 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company',
  258. 's.tva_intra'=>'company', 'pj.ref'=>'project', 'pj.title'=>'project', 'fd.rowid'=>'invoice_line', 'fd.label'=>"invoice_line", 'fd.description'=>"invoice_line",
  259. 'fd.subprice'=>"invoice_line", 'fd.total_ht'=>"invoice_line", 'fd.total_tva'=>"invoice_line", 'fd.total_ttc'=>"invoice_line", 'fd.tva_tx'=>"invoice_line",
  260. 'fd.qty'=>"invoice_line", 'fd.date_start'=>"invoice_line", 'fd.date_end'=>"invoice_line", 'fd.special_code'=>'invoice_line',
  261. 'fd.product_type'=>'invoice_line', 'fd.fk_product'=>'product', 'p.ref'=>'product', 'p.label'=>'product', $alias_product_perentity . '.accountancy_code_sell'=>'product',
  262. 'f.fk_user_author'=>'user', 'uc.login'=>'user', 'f.fk_user_valid'=>'user', 'uv.login'=>'user'
  263. );
  264. $this->export_special_array[$r] = array('none.rest'=>'getRemainToPay');
  265. $this->export_dependencies_array[$r] = array('invoice_line'=>'fd.rowid', 'product'=>'fd.rowid', 'none.rest'=>array('f.rowid', 'f.total_ttc', 'f.close_code')); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
  266. $keyforselect = 'facture';
  267. $keyforelement = 'invoice';
  268. $keyforaliasextra = 'extra';
  269. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  270. $keyforselect = 'facturedet';
  271. $keyforelement = 'invoice_line';
  272. $keyforaliasextra = 'extra2';
  273. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  274. $keyforselect = 'product';
  275. $keyforelement = 'product';
  276. $keyforaliasextra = 'extra3';
  277. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  278. $keyforselect = 'societe';
  279. $keyforelement = 'company';
  280. $keyforaliasextra = 'extra4';
  281. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  282. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  283. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  284. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_extrafields as extra4 ON s.rowid = extra4.fk_object';
  285. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as ps ON ps.rowid = s.parent';
  286. if (empty($user->rights->societe->client->voir)) {
  287. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  288. }
  289. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c on s.fk_pays = c.rowid';
  290. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as cd on s.fk_departement = cd.rowid,';
  291. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'facture as f';
  292. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pj ON f.fk_projet = pj.rowid';
  293. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uc ON f.fk_user_author = uc.rowid';
  294. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uv ON f.fk_user_valid = uv.rowid';
  295. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_extrafields as extra ON f.rowid = extra.fk_object';
  296. $this->export_sql_end[$r] .= ' , '.MAIN_DB_PREFIX.'facturedet as fd';
  297. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facturedet_extrafields as extra2 on fd.rowid = extra2.fk_object';
  298. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  299. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  300. $this->export_sql_end[$r] .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
  301. }
  302. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra3 on p.rowid = extra3.fk_object';
  303. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture';
  304. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('invoice').')';
  305. if (empty($user->rights->societe->client->voir)) {
  306. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.(empty($user) ? 0 : $user->id);
  307. }
  308. $r++;
  309. $this->export_code[$r] = $this->rights_class.'_'.$r;
  310. $this->export_label[$r] = 'CustomersInvoicesAndPayments'; // Translation key (used only if key ExportDataset_xxx_z not found)
  311. $this->export_icon[$r] = 'invoice';
  312. $this->export_permission[$r] = array(array("facture", "facture", "export"));
  313. $this->export_fields_array[$r] = array(
  314. 's.rowid'=>"IdCompany", 's.nom'=>'CompanyName', 's.code_client'=>'CustomerCode', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'c.code'=>'CountryCode', 'cd.nom'=>'State',
  315. 's.phone'=>'Phone',
  316. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.code_compta'=>'CustomerAccountancyCode',
  317. 's.code_compta_fournisseur'=>'SupplierAccountancyCode', 's.tva_intra'=>'VATIntra',
  318. 'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_client'=>'RefCustomer',
  319. 'f.type'=>"Type", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>"DateDue", 'f.total_ht'=>"TotalHT",
  320. 'f.total_ttc'=>"TotalTTC", 'f.total_tva'=>"TotalVAT", 'f.localtax1'=>'LT1', 'f.localtax2'=>'LT2', 'f.paye'=>"InvoicePaidCompletely", 'f.fk_statut'=>'InvoiceStatus', 'f.close_code'=>'EarlyClosingReason', 'f.close_note'=>'EarlyClosingComment',
  321. 'none.rest'=>'Rest',
  322. 'f.note_private'=>"NotePrivate", 'f.note_public'=>"NotePublic", 'f.fk_user_author'=>'CreatedById', 'uc.login'=>'CreatedByLogin',
  323. 'f.fk_user_valid'=>'ValidatedById', 'uv.login'=>'ValidatedByLogin', 'pj.ref'=>'ProjectRef', 'pj.title'=>'ProjectLabel', 'p.rowid'=>'PaymentId', 'p.ref'=>'PaymentRef',
  324. 'p.amount'=>'AmountPayment', 'pf.amount'=>'AmountPaymentDistributedOnInvoice', 'p.datep'=>'DatePayment', 'p.num_paiement'=>'PaymentNumber',
  325. 'pt.code'=>'CodePaymentMode', 'pt.libelle'=>'LabelPaymentMode', 'p.note'=>'PaymentNote', 'p.fk_bank'=>'IdTransaction', 'ba.ref'=>'AccountRef'
  326. );
  327. $this->export_help_array[$r] = array('f.paye'=>'InvoicePaidCompletelyHelp');
  328. if (!empty($conf->multicurrency->enabled)) {
  329. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  330. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  331. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  332. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  333. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  334. $this->export_examplevalues_array[$r]['f.multicurrency_code'] = 'EUR';
  335. }
  336. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->INVOICE_SHOW_POS)) {
  337. $this->export_fields_array[$r]['f.module_source'] = 'POSModule';
  338. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  339. }
  340. $this->export_TypeFields_array[$r] = array(
  341. 's.rowid'=>'Numeric', 's.nom'=>'Text', 's.code_client'=>'Text', 's.address'=>'Text', 's.zip'=>'Text', 's.town'=>'Text', 'c.code'=>'Text', 'cd.nom'=>'Text', 's.phone'=>'Text', 's.siren'=>'Text',
  342. 's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text',
  343. 'f.rowid'=>"Numeric", 'f.ref'=>"Text", 'f.ref_client'=>'Text', 'f.type'=>"Numeric", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.date_lim_reglement'=>"Date",
  344. 'f.total_ht'=>"Numeric", 'f.total_ttc'=>"Numeric", 'f.total_tva'=>"Numeric", 'f.localtax1'=>'Numeric', 'f.localtax2'=>'Numeric', 'f.paye'=>"Boolean", 'f.fk_statut'=>'Status', 'f.close_code'=>'Text', 'f.close_note'=>'Text',
  345. 'none.rest'=>'NumericCompute',
  346. 'f.note_private'=>"Text", 'f.note_public'=>"Text", 'f.fk_user_author'=>'Numeric', 'uc.login'=>'Text', 'f.fk_user_valid'=>'Numeric', 'uv.login'=>'Text',
  347. 'pj.ref'=>'Text', 'pj.title'=>'Text', 'p.amount'=>'Numeric', 'pf.amount'=>'Numeric', 'p.rowid'=>'Numeric', 'p.ref'=>'Text', 'p.title'=>'Text', 'p.datep'=>'Date', 'p.num_paiement'=>'Numeric',
  348. 'p.fk_bank'=>'Numeric', 'p.note'=>'Text', 'pt.code'=>'Text', 'pt.libelle'=>'text', 'ba.ref'=>'Text'
  349. );
  350. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->INVOICE_SHOW_POS)) {
  351. $this->export_fields_array[$r]['f.module_source'] = 'POSModule';
  352. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  353. }
  354. $this->export_entities_array[$r] = array(
  355. 's.rowid'=>"company", 's.nom'=>'company', 's.code_client'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'c.code'=>'company', 'cd.nom'=>'company', 's.phone'=>'company',
  356. 's.siren'=>'company', 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company',
  357. 's.tva_intra'=>'company', 'pj.ref'=>'project', 'pj.title'=>'project', 'p.rowid'=>'payment', 'p.ref'=>'payment', 'p.amount'=>'payment', 'pf.amount'=>'payment', 'p.datep'=>'payment',
  358. 'p.num_paiement'=>'payment', 'pt.code'=>'payment', 'pt.libelle'=>'payment', 'p.note'=>'payment', 'f.fk_user_author'=>'user', 'uc.login'=>'user',
  359. 'f.fk_user_valid'=>'user', 'uv.login'=>'user', 'p.fk_bank'=>'account', 'ba.ref'=>'account'
  360. );
  361. $this->export_special_array[$r] = array('none.rest'=>'getRemainToPay');
  362. $this->export_dependencies_array[$r] = array('payment'=>'p.rowid', 'none.rest'=>array('f.rowid', 'f.total_ttc', 'f.close_code')); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them, or just to have field we need
  363. $keyforselect = 'facture';
  364. $keyforelement = 'invoice';
  365. $keyforaliasextra = 'extra';
  366. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  367. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  368. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  369. if (empty($user->rights->societe->client->voir)) {
  370. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  371. }
  372. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c on s.fk_pays = c.rowid';
  373. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as cd on s.fk_departement = cd.rowid,';
  374. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'facture as f';
  375. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pj ON f.fk_projet = pj.rowid';
  376. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uc ON f.fk_user_author = uc.rowid';
  377. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uv ON f.fk_user_valid = uv.rowid';
  378. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_extrafields as extra ON f.rowid = extra.fk_object';
  379. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON pf.fk_facture = f.rowid';
  380. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement as p ON pf.fk_paiement = p.rowid';
  381. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as pt ON pt.id = p.fk_paiement';
  382. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON b.rowid = p.fk_bank';
  383. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON ba.rowid = b.fk_account';
  384. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid';
  385. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('invoice').')';
  386. if (empty($user->rights->societe->client->voir)) {
  387. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.(empty($user) ? 0 : $user->id);
  388. }
  389. $r++;
  390. }
  391. /**
  392. * Function called when module is enabled.
  393. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  394. * It also creates data directories
  395. *
  396. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes')
  397. * @return int 1 if OK, 0 if KO
  398. */
  399. public function init($options = '')
  400. {
  401. global $conf, $langs;
  402. // Remove permissions and default values
  403. $this->remove($options);
  404. //ODT template
  405. $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/invoices/template_invoice.odt';
  406. $dirodt = DOL_DATA_ROOT.'/doctemplates/invoices';
  407. $dest = $dirodt.'/template_invoice.odt';
  408. if (file_exists($src) && !file_exists($dest)) {
  409. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  410. dol_mkdir($dirodt);
  411. $result = dol_copy($src, $dest, 0, 0);
  412. if ($result < 0) {
  413. $langs->load("errors");
  414. $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
  415. return 0;
  416. }
  417. }
  418. $sql = array(
  419. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'invoice' AND entity = ".$conf->entity,
  420. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','invoice',".$conf->entity.")"
  421. );
  422. return $this->_init($sql, $options);
  423. }
  424. }