modFacture.class.php 35 KB

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