modFacture.class.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. * Copyright (C) 2022 Frédéric France <frederic.france@netlogic.fr>
  9. * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \defgroup facture Module customer invoices
  26. * \brief Module to manage customer invoices
  27. * \file htdocs/core/modules/modFacture.class.php
  28. * \ingroup facture
  29. * \brief Description and activation file for the module customer invoices
  30. */
  31. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  32. /**
  33. * Class to describe module customer invoices
  34. */
  35. class modFacture extends DolibarrModules
  36. {
  37. /**
  38. * Constructor. Define names, constants, directories, boxes, permissions
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. public function __construct($db)
  43. {
  44. global $conf, $user;
  45. $this->db = $db;
  46. $this->numero = 30;
  47. $this->family = "financial";
  48. $this->module_position = '11';
  49. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  50. $this->name = preg_replace('/^mod/i', '', get_class($this));
  51. $this->description = "Gestion des factures";
  52. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  53. $this->version = 'dolibarr';
  54. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  55. $this->picto = 'bill';
  56. // Data directories to create when module is enabled
  57. $this->dirs = array("/facture/temp");
  58. // Dependencies
  59. $this->depends = array('always'=>"modSociete");
  60. $this->requiredby = array("modComptabilite", "modAccounting");
  61. $this->conflictwith = array();
  62. $this->langfiles = array("bills", "companies", "compta", "products");
  63. $this->warnings_activation = array(); // Warning to show when we activate module. array('always'='text') or array('FR'='text')
  64. $this->warnings_activation_ext = array(); // Warning to show when we activate an external module. array('always'='text') or array('FR'='text')
  65. // Config pages
  66. $this->config_page_url = array("facture.php");
  67. // Constants
  68. $this->const = array();
  69. $r = 0;
  70. $this->const[$r][0] = "FACTURE_ADDON";
  71. $this->const[$r][1] = "chaine";
  72. $this->const[$r][2] = "mod_facture_terre";
  73. $this->const[$r][3] = 'Name of numbering numerotation rules of invoice';
  74. $this->const[$r][4] = 0;
  75. $r++;
  76. $this->const[$r][0] = "FACTURE_ADDON_PDF";
  77. $this->const[$r][1] = "chaine";
  78. $this->const[$r][2] = "sponge";
  79. $this->const[$r][3] = 'Name of PDF model of invoice';
  80. $this->const[$r][4] = 0;
  81. $r++;
  82. $this->const[$r][0] = "FACTURE_ADDON_PDF_ODT_PATH";
  83. $this->const[$r][1] = "chaine";
  84. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/invoices";
  85. $this->const[$r][3] = "";
  86. $this->const[$r][4] = 0;
  87. $r++;
  88. // Boxes
  89. //$this->boxes = array(0=>array(1=>'box_factures_imp.php'),1=>array(1=>'box_factures.php'));
  90. $this->boxes = array(
  91. 0=>array('file'=>'box_factures_imp.php', 'enabledbydefaulton'=>'Home'),
  92. 1=>array('file'=>'box_factures.php', 'enabledbydefaulton'=>'Home'),
  93. 2=>array('file'=>'box_graph_invoices_permonth.php', 'enabledbydefaulton'=>'Home'),
  94. 3=>array('file'=>'box_customers_outstanding_bill_reached.php', 'enabledbydefaulton'=>'Home')
  95. );
  96. // Cronjobs
  97. $arraydate = dol_getdate(dol_now());
  98. $datestart = dol_mktime(23, 0, 0, $arraydate['mon'], $arraydate['mday'], $arraydate['year']);
  99. $this->cronjobs = array(
  100. 0 => array(
  101. 'label'=>'RecurringInvoicesJob',
  102. 'jobtype'=>'method',
  103. 'class'=>'compta/facture/class/facture-rec.class.php',
  104. 'objectname'=>'FactureRec',
  105. 'method'=>'createRecurringInvoices',
  106. 'parameters'=>'',
  107. 'comment'=>'Generate recurring invoices',
  108. 'frequency'=>1,
  109. 'unitfrequency'=>3600 * 24,
  110. 'priority'=>51,
  111. 'status'=>1,
  112. 'test'=>'$conf->facture->enabled',
  113. 'datestart'=>$datestart
  114. ),
  115. 1 => array(
  116. 'label'=>'SendEmailsRemindersOnInvoiceDueDate',
  117. 'jobtype'=>'method',
  118. 'class'=>'compta/facture/class/facture.class.php',
  119. 'objectname'=>'Facture',
  120. 'method'=>'sendEmailsRemindersOnInvoiceDueDate',
  121. 'parameters'=>"10,all,EmailTemplateCode,duedate",
  122. 'comment'=>'Send an email when we reach the invoice due date (or invoice date) - n days. First param is n, the number of days before due date (or invoice date) to send the remind (or after if value is negative), second parameter is "all" or a payment mode code, third parameter is the code of the email template to use (an email template with the EmailTemplateCode must exists. The version of the email template in the language of the thirdparty will be used in priority. Language of the thirdparty will be also used to update the PDF of the sent invoice). The fourth parameter is the string "duedate" (default) or "invoicedate" to define which date of the invoice to use.',
  123. 'frequency'=>1,
  124. 'unitfrequency'=>3600 * 24,
  125. 'priority'=>50,
  126. 'status'=>0,
  127. 'test'=>'$conf->facture->enabled',
  128. 'datestart'=>$datestart
  129. ),
  130. );
  131. // Permissions
  132. $this->rights = array();
  133. $this->rights_class = 'facture';
  134. $r = 0;
  135. $r++;
  136. $this->rights[$r][0] = 11;
  137. $this->rights[$r][1] = 'Read invoices';
  138. $this->rights[$r][2] = 'a';
  139. $this->rights[$r][3] = 0;
  140. $this->rights[$r][4] = 'lire';
  141. $r++;
  142. $this->rights[$r][0] = 12;
  143. $this->rights[$r][1] = 'Create and update invoices';
  144. $this->rights[$r][2] = 'a';
  145. $this->rights[$r][3] = 0;
  146. $this->rights[$r][4] = 'creer';
  147. // There is a particular permission for unvalidate because this may be not forbidden by some laws
  148. $r++;
  149. $this->rights[$r][0] = 13;
  150. $this->rights[$r][1] = 'Devalidate invoices';
  151. $this->rights[$r][2] = 'a';
  152. $this->rights[$r][3] = 0;
  153. $this->rights[$r][4] = 'invoice_advance';
  154. $this->rights[$r][5] = 'unvalidate';
  155. $r++;
  156. $this->rights[$r][0] = 14;
  157. $this->rights[$r][1] = 'Validate invoices';
  158. $this->rights[$r][2] = 'a';
  159. $this->rights[$r][3] = 0;
  160. $this->rights[$r][4] = 'invoice_advance';
  161. $this->rights[$r][5] = 'validate';
  162. $r++;
  163. $this->rights[$r][0] = 15;
  164. $this->rights[$r][1] = 'Send invoices by email';
  165. $this->rights[$r][2] = 'a';
  166. $this->rights[$r][3] = 0;
  167. $this->rights[$r][4] = 'invoice_advance';
  168. $this->rights[$r][5] = 'send';
  169. $r++;
  170. $this->rights[$r][0] = 16;
  171. $this->rights[$r][1] = 'Issue payments on invoices';
  172. $this->rights[$r][2] = 'a';
  173. $this->rights[$r][3] = 0;
  174. $this->rights[$r][4] = 'paiement';
  175. $r++;
  176. $this->rights[$r][0] = 19;
  177. $this->rights[$r][1] = 'Delete invoices';
  178. $this->rights[$r][2] = 'a';
  179. $this->rights[$r][3] = 0;
  180. $this->rights[$r][4] = 'supprimer';
  181. $r++;
  182. $this->rights[$r][0] = 1321;
  183. $this->rights[$r][1] = 'Export customer invoices, attributes and payments';
  184. $this->rights[$r][2] = 'r';
  185. $this->rights[$r][3] = 0;
  186. $this->rights[$r][4] = 'facture';
  187. $this->rights[$r][5] = 'export';
  188. $r++;
  189. $this->rights[$r][0] = 1322;
  190. $this->rights[$r][1] = 'Re-open a fully paid invoice';
  191. $this->rights[$r][2] = 'r';
  192. $this->rights[$r][3] = 0;
  193. $this->rights[$r][4] = 'invoice_advance';
  194. $this->rights[$r][5] = 'reopen';
  195. // Menus
  196. //-------
  197. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  198. // Imports
  199. //--------
  200. $r = 1;
  201. $r++;
  202. $this->import_code[$r] = $this->rights_class.'_'.$r;
  203. $this->import_label[$r] = "Invoices"; // Translation key
  204. $this->import_icon[$r] = $this->picto;
  205. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  206. $this->import_tables_array[$r] = array('f' => MAIN_DB_PREFIX.'facture', 'extra' => MAIN_DB_PREFIX.'facture_extrafields');
  207. $this->import_tables_creator_array[$r] = array('f' => 'fk_user_author'); // Fields to store import user id
  208. $this->import_fields_array[$r] = array(
  209. 'f.ref' => 'InvoiceRef*',
  210. 'f.ref_ext' => 'ExternalRef',
  211. 'f.ref_client' => 'RefCustomer',
  212. 'f.type' => 'Type*',
  213. 'f.fk_soc' => 'Customer*',
  214. 'f.datec' => 'InvoiceDateCreation',
  215. 'f.datef' => 'DateInvoice',
  216. 'f.date_valid' => 'Validation Date',
  217. 'f.paye' => 'InvoicePaid',
  218. 'f.total_tva' => 'TotalVAT',
  219. 'f.total_ht' => 'TotalHT',
  220. 'f.total_ttc' => 'TotalTTC',
  221. 'f.fk_statut' => 'InvoiceStatus',
  222. 'f.fk_user_modif' => 'Modifier Id',
  223. 'f.fk_user_valid' => 'Validator Id',
  224. 'f.fk_user_closing' => 'Closer Id',
  225. 'f.fk_facture_source' => 'Invoice Source Id',
  226. 'f.fk_projet' => 'Project Id',
  227. 'f.fk_account' => 'Bank Account',
  228. 'f.fk_currency' => 'Currency*',
  229. 'f.fk_cond_reglement' => 'PaymentTerm',
  230. 'f.fk_mode_reglement' => 'PaymentMode',
  231. 'f.date_lim_reglement' => 'DateMaxPayment',
  232. 'f.note_public' => 'InvoiceNote',
  233. 'f.note_private' => 'NotePrivate',
  234. 'f.model_pdf' => 'Model'
  235. );
  236. if (isModEnabled("multicurrency")) {
  237. $this->import_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  238. $this->import_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  239. $this->import_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  240. $this->import_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  241. $this->import_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  242. }
  243. // Add extra fields
  244. $import_extrafield_sample = array();
  245. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture' AND entity IN (0, ".$conf->entity.")";
  246. $resql = $this->db->query($sql);
  247. if ($resql) {
  248. while ($obj = $this->db->fetch_object($resql)) {
  249. $fieldname = 'extra.'.$obj->name;
  250. $fieldlabel = ucfirst($obj->label);
  251. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  252. $import_extrafield_sample[$fieldname] = $fieldlabel;
  253. }
  254. }
  255. // End add extra fields
  256. $this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'facture');
  257. $this->import_regex_array[$r] = array('f.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency');
  258. $import_sample = array(
  259. 'f.ref' => '(PROV0001)',
  260. 'f.ref_ext' => '',
  261. 'f.ref_client' => '',
  262. 'f.type' => '0',
  263. 'f.fk_soc' => '80LIMIT',
  264. 'f.datec' => '2021-11-24',
  265. 'f.datef' => '2021-11-24',
  266. 'f.date_valid' => '2021-11-24',
  267. 'f.paye' => '1',
  268. 'f.total_tva' => '21',
  269. 'f.total_ht' => '100',
  270. 'f.total_ttc' => '121',
  271. 'f.fk_statut' => '1',
  272. 'f.fk_user_modif' => '',
  273. 'f.fk_user_valid' => '',
  274. 'f.fk_user_closing' => '',
  275. 'f.fk_facture_source' => '',
  276. 'f.fk_projet' => '',
  277. 'f.fk_account' => '',
  278. 'f.fk_currency' => 'EUR',
  279. 'f.fk_cond_reglement' => '30D',
  280. 'f.fk_mode_reglement' => 'VIR',
  281. 'f.date_lim_reglement' => '2021-12-24',
  282. 'f.note_public' => '',
  283. 'f.note_private' => '',
  284. 'f.model_pdf' => 'sponge',
  285. 'f.multicurrency_code' => 'EUR',
  286. 'f.multicurrency_tx' => '1',
  287. 'f.multicurrency_total_ht' => '100',
  288. 'f.multicurrency_total_tva' => '21',
  289. 'f.multicurrency_total_ttc' => '121'
  290. );
  291. $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
  292. $this->import_updatekeys_array[$r] = array('f.ref' => 'Ref');
  293. $this->import_convertvalue_array[$r] = array(
  294. 'f.fk_soc' => array(
  295. 'rule' => 'fetchidfromref',
  296. 'file' => '/societe/class/societe.class.php',
  297. 'class' => 'Societe',
  298. 'method' => 'fetch',
  299. 'element' => 'ThirdParty'
  300. ),
  301. 'f.fk_projet' => array(
  302. 'rule' => 'fetchidfromref',
  303. 'file' => '/projet/class/project.class.php',
  304. 'class' => 'Project',
  305. 'method' => 'fetch',
  306. 'element' => 'facture'
  307. ),
  308. 'f.fk_cond_reglement' => array(
  309. 'rule' => 'fetchidfromcodeorlabel',
  310. 'file' => '/compta/facture/class/paymentterm.class.php',
  311. 'class' => 'PaymentTerm',
  312. 'method' => 'fetch',
  313. 'element' => 'c_payment_term'
  314. )
  315. );
  316. // Import Invoice Lines
  317. $r++;
  318. $this->import_code[$r] = $this->rights_class.'_'.$r;
  319. $this->import_label[$r] = "InvoiceLine"; // Translation key
  320. $this->import_icon[$r] = $this->picto;
  321. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  322. $this->import_tables_array[$r] = array('fd' => MAIN_DB_PREFIX.'facturedet', 'extra' => MAIN_DB_PREFIX.'facturedet_extrafields');
  323. $this->import_fields_array[$r] = array(
  324. 'fd.fk_facture' => 'InvoiceRef*',
  325. 'fd.fk_parent_line' => 'FacParentLine',
  326. 'fd.fk_product' => 'IdProduct',
  327. 'fd.label' => 'Label',
  328. 'fd.description' => 'LineDescription*',
  329. 'fd.vat_src_code' => 'Vat Source Code',
  330. 'fd.tva_tx' => 'LineVATRate*',
  331. // localtax1_tx
  332. // localtax1_type
  333. // localtax2_tx
  334. // localtax2_type
  335. 'fd.qty' => 'LineQty',
  336. 'fd.remise_percent' => 'Reduc. (%)',
  337. // remise
  338. // fk_remise_except
  339. 'fd.subprice' => 'UnitPriceHT',
  340. // price
  341. 'fd.total_ht' => 'LineTotalHT',
  342. 'fd.total_tva' => 'LineTotalVAT',
  343. // total_localtax1
  344. // total_localtax2
  345. 'fd.total_ttc' => 'LineTotalTTC',
  346. 'fd.product_type' => 'TypeOfLineServiceOrProduct',
  347. 'fd.date_start' => 'Start Date',
  348. 'fd.date_end' => 'End Date',
  349. // info_bits
  350. // buy_price_ht
  351. // fk_product_fournisseur_price
  352. // specia_code
  353. // rang
  354. // fk_contract_line
  355. 'fd.fk_unit' => 'Unit',
  356. // fk_code_ventilation
  357. // situation_percent
  358. // fk_prev_id
  359. // fk_user_author
  360. // fk_user_modif
  361. // ref_ext
  362. );
  363. if (isModEnabled("multicurrency")) {
  364. $this->import_fields_array[$r]['fd.multicurrency_code'] = 'Currency';
  365. $this->import_fields_array[$r]['fd.multicurrency_subprice'] = 'CurrencyRate';
  366. $this->import_fields_array[$r]['fd.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  367. $this->import_fields_array[$r]['fd.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  368. $this->import_fields_array[$r]['fd.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  369. }
  370. // Add extra fields
  371. $import_extrafield_sample = array();
  372. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_det' AND entity IN (0, ".$conf->entity.")";
  373. $resql = $this->db->query($sql);
  374. if ($resql) {
  375. while ($obj = $this->db->fetch_object($resql)) {
  376. $fieldname = 'extra.'.$obj->name;
  377. $fieldlabel = ucfirst($obj->label);
  378. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  379. $import_extrafield_sample[$fieldname] = $fieldlabel;
  380. }
  381. }
  382. // End add extra fields
  383. $this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'facturedet');
  384. $this->import_regex_array[$r] = array(
  385. 'fd.fk_product' => 'rowid@'.MAIN_DB_PREFIX.'product',
  386. 'fd.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'
  387. );
  388. $import_sample = array(
  389. 'fd.fk_facture' => '(PROV00001)',
  390. 'fd.fk_parent_line' => '',
  391. 'fd.fk_product' => '',
  392. 'fd.label' => '',
  393. 'fd.description' => 'Test product',
  394. 'fd.vat_src_code' => '',
  395. 'fd.tva_tx' => '21',
  396. // localtax1_tx
  397. // localtax1_type
  398. // localtax2_tx
  399. // localtax2_type
  400. 'fd.qty' => '1',
  401. 'fd.remise_percent' => '0',
  402. // remise
  403. // fk_remise_except
  404. 'fd.subprice' => '100',
  405. // price
  406. 'fd.total_ht' => '100',
  407. 'fd.total_tva' => '21',
  408. // total_localtax1
  409. // total_localtax2
  410. 'fd.total_ttc' => '121',
  411. 'fd.product_type' => '0',
  412. 'fd.date_start' => '',
  413. 'fd.date_end' => '',
  414. // info_bits
  415. // buy_price_ht
  416. // fk_product_fournisseur_price
  417. // specia_code
  418. // rang
  419. // fk_contract_line
  420. 'fd.fk_unit' => '',
  421. // fk_code_ventilation
  422. // situation_percent
  423. // fk_prev_id
  424. // fk_user_author
  425. // fk_user_modif
  426. // ref_ext
  427. 'fd.multicurrency_code' => 'EUR',
  428. 'fd.multicurrency_tx' => '21',
  429. 'fd.multicurrency_total_ht' => '100',
  430. 'fd.multicurrency_total_tva' => '21',
  431. 'fd.multicurrency_total_ttc' => '121'
  432. );
  433. $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
  434. $this->import_updatekeys_array[$r] = array(
  435. 'fd.rowid' => 'Row Id',
  436. 'fd.fk_facture' => 'Invoice Id'
  437. );
  438. $this->import_convertvalue_array[$r] = array(
  439. 'fd.fk_facture' => array(
  440. 'rule' => 'fetchidfromref',
  441. 'file' => '/compta/facture/class/facture.class.php',
  442. 'class' => 'Facture',
  443. 'method' => 'fetch',
  444. 'element' => 'facture'
  445. ),
  446. 'fd.fk_projet' => array(
  447. 'rule' => 'fetchidfromref',
  448. 'file' => '/projet/class/project.class.php',
  449. 'class' => 'Project',
  450. 'method' => 'fetch',
  451. 'element' => 'facture'
  452. ),
  453. );
  454. // Exports
  455. //--------
  456. $r = 1;
  457. $alias_product_perentity = !getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED') ? "p" : "ppe";
  458. $this->export_code[$r] = $this->rights_class.'_'.$r;
  459. $this->export_label[$r] = 'CustomersInvoicesAndInvoiceLines'; // Translation key (used only if key ExportDataset_xxx_z not found)
  460. $this->export_icon[$r] = 'invoice';
  461. $this->export_permission[$r] = array(array("facture", "facture", "export", "other"));
  462. $this->export_fields_array[$r] = array(
  463. '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',
  464. 's.phone'=>'Phone',
  465. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4',
  466. 's.code_compta'=>'CustomerAccountancyCode',
  467. 's.code_compta_fournisseur'=>'SupplierAccountancyCode',
  468. 's.tva_intra'=>'VATIntra',
  469. 't.libelle'=>"ThirdPartyType", // 'ce.code'=>"Staff", "cfj.libelle"=>"JuridicalStatus",
  470. 'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_client'=>'RefCustomer', 'f.fk_facture_source'=>'SourceInvoiceId',
  471. 'f.type'=>"Type", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>"DateDue",
  472. 'f.fk_cond_reglement'=>'IdPaymentTerm', 'f.fk_mode_reglement'=>'IdPaymentMode',
  473. 'f.total_ht'=>"TotalHT", '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',
  474. 'none.rest'=>'Rest',
  475. 'f.note_private'=>"NotePrivate", 'f.note_public'=>"NotePublic"
  476. );
  477. // Add multicurrency fields
  478. if (isModEnabled("multicurrency")) {
  479. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  480. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  481. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  482. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  483. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  484. }
  485. // Add POS fields
  486. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || getDolGlobalString('INVOICE_SHOW_POS')) {
  487. $this->export_fields_array[$r]['f.module_source'] = 'Module';
  488. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  489. }
  490. $this->export_fields_array[$r] = $this->export_fields_array[$r] + array(
  491. 'f.fk_user_author'=>'CreatedById', 'uc.login'=>'CreatedByLogin',
  492. 'f.fk_user_valid'=>'ValidatedById', 'uv.login'=>'ValidatedByLogin',
  493. 'pj.ref'=>'ProjectRef', 'pj.title'=>'ProjectLabel'
  494. );
  495. // Add multicompany field
  496. if (getDolGlobalString('MULTICOMPANY_ENTITY_IN_EXPORT_IF_SHARED')) {
  497. $nbofallowedentities = count(explode(',', getEntity('invoice')));
  498. if (isModEnabled('multicompany') && $nbofallowedentities > 1) {
  499. $this->export_fields_array[$r]['f.entity'] = 'Entity';
  500. }
  501. }
  502. $this->export_fields_array[$r] = $this->export_fields_array[$r] + array(
  503. 'fd.rowid'=>'LineId', 'fd.description'=>"LineDescription",
  504. 'fd.subprice'=>"LineUnitPrice", 'fd.tva_tx'=>"LineVATRate", 'fd.qty'=>"LineQty", 'fd.total_ht'=>"LineTotalHT", 'fd.total_tva'=>"LineTotalVAT",
  505. 'fd.total_ttc'=>"LineTotalTTC", 'fd.buy_price_ht'=>'BuyingPrice', 'fd.date_start'=>"DateStart", 'fd.date_end'=>"DateEnd", 'fd.special_code'=>'SpecialCode',
  506. 'fd.product_type'=>"TypeOfLineServiceOrProduct", 'fd.fk_product'=>'ProductId', 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel',
  507. $alias_product_perentity . '.accountancy_code_sell'=>'ProductAccountancySellCode',
  508. 'aa.account_number' => 'AccountingAffectation'
  509. );
  510. $this->export_TypeFields_array[$r] = array(
  511. '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',
  512. 's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text',
  513. 't.libelle'=>"Text", // 'ce.code'=>"List:c_effectif:libelle:code", "cfj.libelle"=>"Text",
  514. 'f.rowid'=>'Numeric', 'f.ref'=>"Text", 'f.ref_client'=>'Text', 'f.fk_facture_source'=>'Numeric', 'f.type'=>"Numeric", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.date_lim_reglement'=>"Date",
  515. 'f.fk_cond_reglement'=>'Numeric', 'f.fk_mode_reglement'=>'Numeric',
  516. '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',
  517. 'none.rest'=>"NumericCompute",
  518. 'f.note_private'=>"Text", 'f.note_public'=>"Text",
  519. 'f.module_source' => 'Text',
  520. 'f.pos_source' => 'Text',
  521. 'f.entity'=>'List:entity:label:rowid',
  522. 'f.fk_user_author'=>'Numeric', 'uc.login'=>'Text', 'f.fk_user_valid'=>'Numeric', 'uv.login'=>'Text',
  523. 'pj.ref'=>'Text', 'pj.title'=>'Text', 'fd.rowid'=>'Numeric', 'fd.description'=>"Text", 'fd.subprice'=>"Numeric", 'fd.tva_tx'=>"Numeric",
  524. 'fd.qty'=>"Numeric", 'fd.total_ht'=>"Numeric", 'fd.total_tva'=>"Numeric", 'fd.total_ttc'=>"Numeric", 'fd.buy_price_ht'=>"Numeric", 'fd.date_start'=>"Date", 'fd.date_end'=>"Date",
  525. 'fd.special_code'=>'Numeric', 'fd.product_type'=>"Numeric", 'fd.fk_product'=>'List:product:label', 'p.ref'=>'Text', 'p.label'=>'Text',
  526. $alias_product_perentity . '.accountancy_code_sell'=>'Text',
  527. 'aa.account_number' => 'Text'
  528. );
  529. $this->export_entities_array[$r] = array(
  530. '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',
  531. 's.siren'=>'company', 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company', 's.tva_intra'=>'company',
  532. 't.libelle'=>'company', // 'ce.code'=>'company', 'cfj.libelle'=>'company'
  533. 'pj.ref'=>'project', 'pj.title'=>'project', 'fd.rowid'=>'invoice_line', 'fd.description'=>"invoice_line",
  534. 'fd.subprice'=>"invoice_line", 'fd.total_ht'=>"invoice_line", 'fd.total_tva'=>"invoice_line", 'fd.total_ttc'=>"invoice_line", 'fd.buy_price_ht'=>'invoice_line', 'fd.tva_tx'=>"invoice_line",
  535. 'fd.qty'=>"invoice_line", 'fd.date_start'=>"invoice_line", 'fd.date_end'=>"invoice_line", 'fd.special_code'=>'invoice_line',
  536. 'fd.product_type'=>'invoice_line', 'fd.fk_product'=>'product', 'p.ref'=>'product', 'p.label'=>'product', $alias_product_perentity . '.accountancy_code_sell'=>'product',
  537. 'f.fk_user_author'=>'user', 'uc.login'=>'user', 'f.fk_user_valid'=>'user', 'uv.login'=>'user',
  538. 'aa.account_number' => "invoice_line",
  539. );
  540. $this->export_help_array[$r] = array('fd.buy_price_ht'=>'CostPriceUsage');
  541. $this->export_special_array[$r] = array('none.rest'=>'getRemainToPay');
  542. $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
  543. $keyforselect = 'facture';
  544. $keyforelement = 'invoice';
  545. $keyforaliasextra = 'extra';
  546. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  547. $keyforselect = 'facturedet';
  548. $keyforelement = 'invoice_line';
  549. $keyforaliasextra = 'extra2';
  550. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  551. $keyforselect = 'product';
  552. $keyforelement = 'product';
  553. $keyforaliasextra = 'extra3';
  554. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  555. $keyforselect = 'societe';
  556. $keyforelement = 'company';
  557. $keyforaliasextra = 'extra4';
  558. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  559. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  560. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  561. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_extrafields as extra4 ON s.rowid = extra4.fk_object';
  562. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as ps ON ps.rowid = s.parent';
  563. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_typent as t ON s.fk_typent = t.id';
  564. if (!empty($user) && !$user->hasRight('societe', 'client', 'voir')) {
  565. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  566. }
  567. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c on s.fk_pays = c.rowid';
  568. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as cd on s.fk_departement = cd.rowid,';
  569. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'facture as f';
  570. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pj ON f.fk_projet = pj.rowid';
  571. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uc ON f.fk_user_author = uc.rowid';
  572. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uv ON f.fk_user_valid = uv.rowid';
  573. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_extrafields as extra ON f.rowid = extra.fk_object';
  574. $this->export_sql_end[$r] .= ' , '.MAIN_DB_PREFIX.'facturedet as fd';
  575. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facturedet_extrafields as extra2 on fd.rowid = extra2.fk_object';
  576. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  577. if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) {
  578. $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);
  579. }
  580. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra3 on p.rowid = extra3.fk_object';
  581. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_account as aa on fd.fk_code_ventilation = aa.rowid';
  582. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture';
  583. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('invoice').')';
  584. if (!empty($user) && !$user->hasRight('societe', 'client', 'voir')) {
  585. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.(empty($user) ? 0 : $user->id);
  586. }
  587. $r++;
  588. $this->export_code[$r] = $this->rights_class.'_'.$r;
  589. $this->export_label[$r] = 'CustomersInvoicesAndPayments'; // Translation key (used only if key ExportDataset_xxx_z not found)
  590. $this->export_icon[$r] = 'invoice';
  591. $this->export_permission[$r] = array(array("facture", "facture", "export"));
  592. $this->export_fields_array[$r] = array(
  593. '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',
  594. 's.phone'=>'Phone',
  595. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.code_compta'=>'CustomerAccountancyCode',
  596. 's.code_compta_fournisseur'=>'SupplierAccountancyCode', 's.tva_intra'=>'VATIntra',
  597. 'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_client'=>'RefCustomer', 'f.fk_facture_source'=>'SourceInvoiceId',
  598. 'f.type'=>"Type", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>"DateDue",
  599. 'f.fk_cond_reglement'=>'IdPaymentTerm', 'f.fk_mode_reglement'=>'IdPaymentMode',
  600. 'f.total_ht'=>"TotalHT", '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',
  601. 'none.rest'=>'Rest',
  602. 'f.note_private'=>"NotePrivate", 'f.note_public'=>"NotePublic", 'f.fk_user_author'=>'CreatedById', 'uc.login'=>'CreatedByLogin',
  603. 'f.fk_user_valid'=>'ValidatedById', 'uv.login'=>'ValidatedByLogin', 'pj.ref'=>'ProjectRef', 'pj.title'=>'ProjectLabel', 'p.rowid'=>'PaymentId', 'p.ref'=>'PaymentRef',
  604. 'p.amount'=>'AmountPayment', 'pf.amount'=>'AmountPaymentDistributedOnInvoice', 'p.datep'=>'DatePayment', 'p.num_paiement'=>'PaymentNumber',
  605. 'pt.code'=>'CodePaymentMode', 'pt.libelle'=>'LabelPaymentMode', 'p.note'=>'PaymentNote', 'p.fk_bank'=>'IdTransaction', 'ba.ref'=>'AccountRef'
  606. );
  607. $this->export_help_array[$r] = array('f.paye'=>'InvoicePaidCompletelyHelp');
  608. if (isModEnabled("multicurrency")) {
  609. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  610. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  611. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  612. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  613. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  614. $this->export_examplevalues_array[$r]['f.multicurrency_code'] = 'EUR';
  615. }
  616. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || getDolGlobalString('INVOICE_SHOW_POS')) {
  617. $this->export_fields_array[$r]['f.module_source'] = 'POSModule';
  618. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  619. }
  620. $this->export_TypeFields_array[$r] = array(
  621. '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',
  622. 's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text',
  623. 'f.rowid'=>"Numeric", 'f.ref'=>"Text", 'f.ref_client'=>'Text', 'f.fk_facture_source'=>'Numeric', 'f.type'=>"Numeric", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.date_lim_reglement'=>"Date",
  624. 'f.fk_cond_reglement'=>'Numeric', 'f.fk_mode_reglement'=>'Numeric',
  625. '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',
  626. 'none.rest'=>'NumericCompute',
  627. 'f.note_private'=>"Text", 'f.note_public'=>"Text", 'f.fk_user_author'=>'Numeric', 'uc.login'=>'Text', 'f.fk_user_valid'=>'Numeric', 'uv.login'=>'Text',
  628. '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',
  629. 'p.fk_bank'=>'Numeric', 'p.note'=>'Text', 'pt.code'=>'Text', 'pt.libelle'=>'text', 'ba.ref'=>'Text'
  630. );
  631. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || getDolGlobalString('INVOICE_SHOW_POS')) {
  632. $this->export_fields_array[$r]['f.module_source'] = 'POSModule';
  633. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  634. }
  635. $this->export_entities_array[$r] = array(
  636. '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',
  637. 's.siren'=>'company', 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company',
  638. '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',
  639. 'p.num_paiement'=>'payment', 'pt.code'=>'payment', 'pt.libelle'=>'payment', 'p.note'=>'payment', 'f.fk_user_author'=>'user', 'uc.login'=>'user',
  640. 'f.fk_user_valid'=>'user', 'uv.login'=>'user', 'p.fk_bank'=>'account', 'ba.ref'=>'account'
  641. );
  642. $this->export_special_array[$r] = array('none.rest'=>'getRemainToPay');
  643. $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
  644. $keyforselect = 'facture';
  645. $keyforelement = 'invoice';
  646. $keyforaliasextra = 'extra';
  647. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  648. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  649. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  650. if (!empty($user) && !$user->hasRight('societe', 'client', 'voir')) {
  651. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  652. }
  653. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c on s.fk_pays = c.rowid';
  654. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as cd on s.fk_departement = cd.rowid,';
  655. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'facture as f';
  656. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pj ON f.fk_projet = pj.rowid';
  657. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uc ON f.fk_user_author = uc.rowid';
  658. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uv ON f.fk_user_valid = uv.rowid';
  659. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_extrafields as extra ON f.rowid = extra.fk_object';
  660. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON pf.fk_facture = f.rowid';
  661. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement as p ON pf.fk_paiement = p.rowid';
  662. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as pt ON pt.id = p.fk_paiement';
  663. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON b.rowid = p.fk_bank';
  664. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON ba.rowid = b.fk_account';
  665. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid';
  666. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('invoice').')';
  667. if (!empty($user) && !$user->hasRight('societe', 'client', 'voir')) {
  668. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.(empty($user) ? 0 : $user->id);
  669. }
  670. $r++;
  671. }
  672. /**
  673. * Function called when module is enabled.
  674. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  675. * It also creates data directories
  676. *
  677. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes')
  678. * @return int 1 if OK, 0 if KO
  679. */
  680. public function init($options = '')
  681. {
  682. global $conf, $langs;
  683. // Remove permissions and default values
  684. $this->remove($options);
  685. //ODT template
  686. $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/invoices/template_invoice.odt';
  687. $dirodt = DOL_DATA_ROOT.'/doctemplates/invoices';
  688. $dest = $dirodt.'/template_invoice.odt';
  689. if (file_exists($src) && !file_exists($dest)) {
  690. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  691. dol_mkdir($dirodt);
  692. $result = dol_copy($src, $dest, 0, 0);
  693. if ($result < 0) {
  694. $langs->load("errors");
  695. $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
  696. return 0;
  697. }
  698. }
  699. $sql = array(
  700. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'invoice' AND entity = ".((int) $conf->entity),
  701. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','invoice',".((int) $conf->entity).")"
  702. );
  703. return $this->_init($sql, $options);
  704. }
  705. }