modFacture.class.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 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 parameter 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 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_int' => '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.remise_percent' => 'RemisePercent',
  219. 'f.remise_absolue' => 'RemiseAbsolue',
  220. 'f.remise' => 'Remise',
  221. 'f.total_tva' => 'TotalVAT',
  222. 'f.total_ht' => 'TotalHT',
  223. 'f.total_ttc' => 'TotalTTC',
  224. 'f.fk_statut' => 'InvoiceStatus',
  225. 'f.fk_user_modif' => 'Modifier Id',
  226. 'f.fk_user_valid' => 'Validator Id',
  227. 'f.fk_user_closing' => 'Closer Id',
  228. 'f.fk_facture_source' => 'Invoice Source Id',
  229. 'f.fk_projet' => 'Project Id',
  230. 'f.fk_account' => 'Bank Account',
  231. 'f.fk_currency' => 'Currency*',
  232. 'f.fk_cond_reglement' => 'Payment Condition',
  233. 'f.fk_mode_reglement' => 'Payment Mode',
  234. 'f.date_lim_reglement' => 'DateMaxPayment',
  235. 'f.note_public' => 'InvoiceNote',
  236. 'f.note_private' => 'NotePrivate',
  237. 'f.model_pdf' => 'Model'
  238. );
  239. if (!empty($conf->multicurrency->enabled)) {
  240. $this->import_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  241. $this->import_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  242. $this->import_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  243. $this->import_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  244. $this->import_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  245. }
  246. // Add extra fields
  247. $import_extrafield_sample = array();
  248. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture' AND entity IN (0, ".$conf->entity.")";
  249. $resql = $this->db->query($sql);
  250. if ($resql) {
  251. while ($obj = $this->db->fetch_object($resql)) {
  252. $fieldname = 'extra.'.$obj->name;
  253. $fieldlabel = ucfirst($obj->label);
  254. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  255. $import_extrafield_sample[$fieldname] = $fieldlabel;
  256. }
  257. }
  258. // End add extra fields
  259. $this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'facture');
  260. $this->import_regex_array[$r] = array('f.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency');
  261. $import_sample = array(
  262. 'f.ref' => '(PROV0001)',
  263. 'f.ref_ext' => '',
  264. 'f.ref_int' => '',
  265. 'f.ref_client' => '',
  266. 'f.type' => '0',
  267. 'f.fk_soc' => '80LIMIT',
  268. 'f.datec' => '2021-11-24',
  269. 'f.datef' => '2021-11-24',
  270. 'f.date_valid' => '2021-11-24',
  271. 'f.paye' => '1',
  272. 'f.remise_percent' => '0',
  273. 'f.remise_absolue' => '0',
  274. 'f.remise' => '0',
  275. 'f.total_tva' => '21',
  276. 'f.total_ht' => '100',
  277. 'f.total_ttc' => '121',
  278. 'f.fk_statut' => '1',
  279. 'f.fk_user_modif' => '',
  280. 'f.fk_user_valid' => '',
  281. 'f.fk_user_closing' => '',
  282. 'f.fk_facture_source' => '',
  283. 'f.fk_projet' => '',
  284. 'f.fk_account' => '',
  285. 'f.fk_currency' => 'EUR',
  286. 'f.fk_cond_reglement' => '30D',
  287. 'f.fk_mode_reglement' => 'VIR',
  288. 'f.date_lim_reglement' => '2021-12-24',
  289. 'f.note_public' => '',
  290. 'f.note_private' => '',
  291. 'f.model_pdf' => 'crabe',
  292. 'f.multicurrency_code' => 'EUR',
  293. 'f.multicurrency_tx' => '1',
  294. 'f.multicurrency_total_ht' => '100',
  295. 'f.multicurrency_total_tva' => '21',
  296. 'f.multicurrency_total_ttc' => '121'
  297. );
  298. $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
  299. $this->import_updatekeys_array[$r] = array('f.ref' => 'Ref');
  300. $this->import_convertvalue_array[$r] = array(
  301. 'f.fk_soc' => array(
  302. 'rule' => 'fetchidfromref',
  303. 'file' => '/societe/class/societe.class.php',
  304. 'class' => 'Societe',
  305. 'method' => 'fetch',
  306. 'element' => 'ThirdParty'
  307. ),
  308. 'f.fk_projet' => array(
  309. 'rule' => 'fetchidfromref',
  310. 'file' => '/projet/class/project.class.php',
  311. 'class' => 'Project',
  312. 'method' => 'fetch',
  313. 'element' => 'facture'
  314. ),
  315. 'f.fk_cond_reglement' => array(
  316. 'rule' => 'fetchidfromcodeorlabel',
  317. 'file' => '/compta/facture/class/paymentterm.class.php',
  318. 'class' => 'PaymentTerm',
  319. 'method' => 'fetch',
  320. 'element' => 'c_payment_term'
  321. )
  322. );
  323. // Import Invoice Lines
  324. $r++;
  325. $this->import_code[$r] = $this->rights_class.'_'.$r;
  326. $this->import_label[$r] = "InvoiceLine"; // Translation key
  327. $this->import_icon[$r] = $this->picto;
  328. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  329. $this->import_tables_array[$r] = array('fd' => MAIN_DB_PREFIX.'facturedet', 'extra' => MAIN_DB_PREFIX.'facturedet_extrafields');
  330. $this->import_fields_array[$r] = array(
  331. 'fd.fk_facture' => 'InvoiceRef*',
  332. 'fd.fk_parent_line' => 'FacParentLine',
  333. 'fd.fk_product' => 'IdProduct',
  334. 'fd.label' => 'Label',
  335. 'fd.description' => 'LineDescription*',
  336. 'fd.vat_src_code' => 'Vat Source Code',
  337. 'fd.tva_tx' => 'LineVATRate*',
  338. // localtax1_tx
  339. // localtax1_type
  340. // localtax2_tx
  341. // localtax2_type
  342. 'fd.qty' => 'LineQty',
  343. 'fd.remise_percent' => 'Reduc. (%)',
  344. // remise
  345. // fk_remise_except
  346. // subprice
  347. // price
  348. 'fd.total_ht' => 'LineTotalHT',
  349. 'fd.total_tva' => 'LineTotalVAT',
  350. // total_localtax1
  351. // total_localtax2
  352. 'fd.total_ttc' => 'LineTotalTTC',
  353. 'fd.product_type' => 'TypeOfLineServiceOrProduct',
  354. 'fd.date_start' => 'Start Date',
  355. 'fd.date_end' => 'End Date',
  356. // info_bits
  357. // buy_price_ht
  358. // fk_product_fournisseur_price
  359. // specia_code
  360. // rang
  361. // fk_contract_line
  362. 'fd.fk_unit' => 'Unit',
  363. // fk_code_ventilation
  364. // situation_percent
  365. // fk_prev_id
  366. // fk_user_author
  367. // fk_user_modif
  368. // ref_ext
  369. );
  370. if (!empty($conf->multicurrency->enabled)) {
  371. $this->import_fields_array[$r]['fd.multicurrency_code'] = 'Currency';
  372. $this->import_fields_array[$r]['fd.multicurrency_subprice'] = 'CurrencyRate';
  373. $this->import_fields_array[$r]['fd.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  374. $this->import_fields_array[$r]['fd.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  375. $this->import_fields_array[$r]['fd.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  376. }
  377. // Add extra fields
  378. $import_extrafield_sample = array();
  379. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_det' AND entity IN (0, ".$conf->entity.")";
  380. $resql = $this->db->query($sql);
  381. if ($resql) {
  382. while ($obj = $this->db->fetch_object($resql)) {
  383. $fieldname = 'extra.'.$obj->name;
  384. $fieldlabel = ucfirst($obj->label);
  385. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  386. $import_extrafield_sample[$fieldname] = $fieldlabel;
  387. }
  388. }
  389. // End add extra fields
  390. $this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'facturedet');
  391. $this->import_regex_array[$r] = array(
  392. 'fd.fk_product' => 'rowid@'.MAIN_DB_PREFIX.'product',
  393. 'fd.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'
  394. );
  395. $import_sample = array(
  396. 'fd.fk_facture' => '(PROV00001)',
  397. 'fd.fk_parent_line' => '',
  398. 'fd.fk_product' => '',
  399. 'fd.label' => '',
  400. 'fd.description' => 'Test product',
  401. 'fd.vat_src_code' => '',
  402. 'fd.tva_tx' => '21',
  403. // localtax1_tx
  404. // localtax1_type
  405. // localtax2_tx
  406. // localtax2_type
  407. 'fd.qty' => '1',
  408. 'fd.remise_percent' => '0',
  409. // remise
  410. // fk_remise_except
  411. // subprice
  412. // price
  413. 'fd.total_ht' => '100',
  414. 'fd.total_tva' => '21',
  415. // total_localtax1
  416. // total_localtax2
  417. 'fd.total_ttc' => '121',
  418. 'fd.product_type' => '0',
  419. 'fd.date_start' => '',
  420. 'fd.date_end' => '',
  421. // info_bits
  422. // buy_price_ht
  423. // fk_product_fournisseur_price
  424. // specia_code
  425. // rang
  426. // fk_contract_line
  427. 'fd.fk_unit' => '',
  428. // fk_code_ventilation
  429. // situation_percent
  430. // fk_prev_id
  431. // fk_user_author
  432. // fk_user_modif
  433. // ref_ext
  434. 'fd.multicurrency_code' => 'EUR',
  435. 'fd.multicurrency_tx' => '21',
  436. 'fd.multicurrency_total_ht' => '100',
  437. 'fd.multicurrency_total_tva' => '21',
  438. 'fd.multicurrency_total_ttc' => '121'
  439. );
  440. $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
  441. $this->import_updatekeys_array[$r] = array(
  442. 'fd.rowid' => 'Row Id',
  443. 'fd.fk_facture' => 'Invoice Id'
  444. );
  445. $this->import_convertvalue_array[$r] = array(
  446. 'fd.fk_facture' => array(
  447. 'rule' => 'fetchidfromref',
  448. 'file' => '/compta/facture/class/facture.class.php',
  449. 'class' => 'Facture',
  450. 'method' => 'fetch',
  451. 'element' => 'facture'
  452. ),
  453. 'fd.fk_projet' => array(
  454. 'rule' => 'fetchidfromref',
  455. 'file' => '/projet/class/project.class.php',
  456. 'class' => 'Project',
  457. 'method' => 'fetch',
  458. 'element' => 'facture'
  459. ),
  460. );
  461. // Exports
  462. //--------
  463. $r = 1;
  464. $alias_product_perentity = empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "p" : "ppe";
  465. $this->export_code[$r] = $this->rights_class.'_'.$r;
  466. $this->export_label[$r] = 'CustomersInvoicesAndInvoiceLines'; // Translation key (used only if key ExportDataset_xxx_z not found)
  467. $this->export_icon[$r] = 'invoice';
  468. $this->export_permission[$r] = array(array("facture", "facture", "export", "other"));
  469. $this->export_fields_array[$r] = array(
  470. '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',
  471. 's.phone'=>'Phone',
  472. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4',
  473. 's.code_compta'=>'CustomerAccountancyCode',
  474. 's.code_compta_fournisseur'=>'SupplierAccountancyCode',
  475. 's.tva_intra'=>'VATIntra',
  476. 't.libelle'=>"ThirdPartyType", // 'ce.code'=>"Staff", "cfj.libelle"=>"JuridicalStatus",
  477. 'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_client'=>'RefCustomer',
  478. 'f.type'=>"Type", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>"DateDue", 'f.total_ht'=>"TotalHT",
  479. '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',
  480. 'none.rest'=>'Rest',
  481. 'f.note_private'=>"NotePrivate", 'f.note_public'=>"NotePublic"
  482. );
  483. // Add multicurrency fields
  484. if (!empty($conf->multicurrency->enabled)) {
  485. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  486. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  487. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  488. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  489. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  490. }
  491. // Add POS fields
  492. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->INVOICE_SHOW_POS)) {
  493. $this->export_fields_array[$r]['f.module_source'] = 'Module';
  494. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  495. }
  496. $this->export_fields_array[$r] = $this->export_fields_array[$r] + array(
  497. 'f.fk_user_author'=>'CreatedById', 'uc.login'=>'CreatedByLogin',
  498. 'f.fk_user_valid'=>'ValidatedById', 'uv.login'=>'ValidatedByLogin',
  499. 'pj.ref'=>'ProjectRef', 'pj.title'=>'ProjectLabel'
  500. );
  501. // Add multicompany field
  502. if (!empty($conf->global->MULTICOMPANY_ENTITY_IN_EXPORT_IF_SHARED)) {
  503. $nbofallowedentities = count(explode(',', getEntity('invoice')));
  504. if (!empty($conf->multicompany->enabled) && $nbofallowedentities > 1) {
  505. $this->export_fields_array[$r]['f.entity'] = 'Entity';
  506. }
  507. }
  508. $this->export_fields_array[$r] = $this->export_fields_array[$r] + array(
  509. 'fd.rowid'=>'LineId', 'fd.description'=>"LineDescription",
  510. 'fd.subprice'=>"LineUnitPrice", 'fd.tva_tx'=>"LineVATRate", 'fd.qty'=>"LineQty", 'fd.total_ht'=>"LineTotalHT", 'fd.total_tva'=>"LineTotalVAT",
  511. 'fd.total_ttc'=>"LineTotalTTC", 'fd.date_start'=>"DateStart", 'fd.date_end'=>"DateEnd", 'fd.special_code'=>'SpecialCode',
  512. 'fd.product_type'=>"TypeOfLineServiceOrProduct", 'fd.fk_product'=>'ProductId', 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel',
  513. $alias_product_perentity . '.accountancy_code_sell'=>'ProductAccountancySellCode'
  514. );
  515. $this->export_TypeFields_array[$r] = array(
  516. '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',
  517. 's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text',
  518. 't.libelle'=>"Text", // 'ce.code'=>"List:c_effectif:libelle:code", "cfj.libelle"=>"Text",
  519. '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',
  520. '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',
  521. 'none.rest'=>"NumericCompute",
  522. 'f.note_private'=>"Text", 'f.note_public'=>"Text", '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.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. 'f.entity'=>'List:entity:label:rowid',
  528. 'f.module_source' => 'Text',
  529. 'f.pos_source' => 'Text'
  530. );
  531. $this->export_entities_array[$r] = array(
  532. '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',
  533. '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',
  534. 't.libelle'=>'company', // 'ce.code'=>'company', 'cfj.libelle'=>'company'
  535. 'pj.ref'=>'project', 'pj.title'=>'project', 'fd.rowid'=>'invoice_line', 'fd.description'=>"invoice_line",
  536. 'fd.subprice'=>"invoice_line", 'fd.total_ht'=>"invoice_line", 'fd.total_tva'=>"invoice_line", 'fd.total_ttc'=>"invoice_line", 'fd.tva_tx'=>"invoice_line",
  537. 'fd.qty'=>"invoice_line", 'fd.date_start'=>"invoice_line", 'fd.date_end'=>"invoice_line", 'fd.special_code'=>'invoice_line',
  538. 'fd.product_type'=>'invoice_line', 'fd.fk_product'=>'product', 'p.ref'=>'product', 'p.label'=>'product', $alias_product_perentity . '.accountancy_code_sell'=>'product',
  539. 'f.fk_user_author'=>'user', 'uc.login'=>'user', 'f.fk_user_valid'=>'user', 'uv.login'=>'user'
  540. );
  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->rights->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 (!empty($conf->global->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] .= ' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture';
  582. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('invoice').')';
  583. if (empty($user->rights->societe->client->voir)) {
  584. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.(empty($user) ? 0 : $user->id);
  585. }
  586. $r++;
  587. $this->export_code[$r] = $this->rights_class.'_'.$r;
  588. $this->export_label[$r] = 'CustomersInvoicesAndPayments'; // Translation key (used only if key ExportDataset_xxx_z not found)
  589. $this->export_icon[$r] = 'invoice';
  590. $this->export_permission[$r] = array(array("facture", "facture", "export"));
  591. $this->export_fields_array[$r] = array(
  592. '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',
  593. 's.phone'=>'Phone',
  594. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.code_compta'=>'CustomerAccountancyCode',
  595. 's.code_compta_fournisseur'=>'SupplierAccountancyCode', 's.tva_intra'=>'VATIntra',
  596. 'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_client'=>'RefCustomer',
  597. 'f.type'=>"Type", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>"DateDue", 'f.total_ht'=>"TotalHT",
  598. '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',
  599. 'none.rest'=>'Rest',
  600. 'f.note_private'=>"NotePrivate", 'f.note_public'=>"NotePublic", 'f.fk_user_author'=>'CreatedById', 'uc.login'=>'CreatedByLogin',
  601. 'f.fk_user_valid'=>'ValidatedById', 'uv.login'=>'ValidatedByLogin', 'pj.ref'=>'ProjectRef', 'pj.title'=>'ProjectLabel', 'p.rowid'=>'PaymentId', 'p.ref'=>'PaymentRef',
  602. 'p.amount'=>'AmountPayment', 'pf.amount'=>'AmountPaymentDistributedOnInvoice', 'p.datep'=>'DatePayment', 'p.num_paiement'=>'PaymentNumber',
  603. 'pt.code'=>'CodePaymentMode', 'pt.libelle'=>'LabelPaymentMode', 'p.note'=>'PaymentNote', 'p.fk_bank'=>'IdTransaction', 'ba.ref'=>'AccountRef'
  604. );
  605. $this->export_help_array[$r] = array('f.paye'=>'InvoicePaidCompletelyHelp');
  606. if (!empty($conf->multicurrency->enabled)) {
  607. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  608. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  609. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  610. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  611. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  612. $this->export_examplevalues_array[$r]['f.multicurrency_code'] = 'EUR';
  613. }
  614. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->INVOICE_SHOW_POS)) {
  615. $this->export_fields_array[$r]['f.module_source'] = 'POSModule';
  616. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  617. }
  618. $this->export_TypeFields_array[$r] = array(
  619. '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',
  620. 's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text',
  621. 'f.rowid'=>"Numeric", 'f.ref'=>"Text", 'f.ref_client'=>'Text', 'f.type'=>"Numeric", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.date_lim_reglement'=>"Date",
  622. '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',
  623. 'none.rest'=>'NumericCompute',
  624. 'f.note_private'=>"Text", 'f.note_public'=>"Text", 'f.fk_user_author'=>'Numeric', 'uc.login'=>'Text', 'f.fk_user_valid'=>'Numeric', 'uv.login'=>'Text',
  625. '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',
  626. 'p.fk_bank'=>'Numeric', 'p.note'=>'Text', 'pt.code'=>'Text', 'pt.libelle'=>'text', 'ba.ref'=>'Text'
  627. );
  628. if (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->INVOICE_SHOW_POS)) {
  629. $this->export_fields_array[$r]['f.module_source'] = 'POSModule';
  630. $this->export_fields_array[$r]['f.pos_source'] = 'POSTerminal';
  631. }
  632. $this->export_entities_array[$r] = array(
  633. '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',
  634. 's.siren'=>'company', 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company',
  635. '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',
  636. 'p.num_paiement'=>'payment', 'pt.code'=>'payment', 'pt.libelle'=>'payment', 'p.note'=>'payment', 'f.fk_user_author'=>'user', 'uc.login'=>'user',
  637. 'f.fk_user_valid'=>'user', 'uv.login'=>'user', 'p.fk_bank'=>'account', 'ba.ref'=>'account'
  638. );
  639. $this->export_special_array[$r] = array('none.rest'=>'getRemainToPay');
  640. $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
  641. $keyforselect = 'facture';
  642. $keyforelement = 'invoice';
  643. $keyforaliasextra = 'extra';
  644. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  645. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  646. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  647. if (empty($user->rights->societe->client->voir)) {
  648. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  649. }
  650. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c on s.fk_pays = c.rowid';
  651. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as cd on s.fk_departement = cd.rowid,';
  652. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'facture as f';
  653. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pj ON f.fk_projet = pj.rowid';
  654. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uc ON f.fk_user_author = uc.rowid';
  655. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uv ON f.fk_user_valid = uv.rowid';
  656. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_extrafields as extra ON f.rowid = extra.fk_object';
  657. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON pf.fk_facture = f.rowid';
  658. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement as p ON pf.fk_paiement = p.rowid';
  659. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as pt ON pt.id = p.fk_paiement';
  660. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON b.rowid = p.fk_bank';
  661. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON ba.rowid = b.fk_account';
  662. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid';
  663. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('invoice').')';
  664. if (empty($user->rights->societe->client->voir)) {
  665. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.(empty($user) ? 0 : $user->id);
  666. }
  667. $r++;
  668. }
  669. /**
  670. * Function called when module is enabled.
  671. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  672. * It also creates data directories
  673. *
  674. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes')
  675. * @return int 1 if OK, 0 if KO
  676. */
  677. public function init($options = '')
  678. {
  679. global $conf, $langs;
  680. // Remove permissions and default values
  681. $this->remove($options);
  682. //ODT template
  683. $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/invoices/template_invoice.odt';
  684. $dirodt = DOL_DATA_ROOT.'/doctemplates/invoices';
  685. $dest = $dirodt.'/template_invoice.odt';
  686. if (file_exists($src) && !file_exists($dest)) {
  687. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  688. dol_mkdir($dirodt);
  689. $result = dol_copy($src, $dest, 0, 0);
  690. if ($result < 0) {
  691. $langs->load("errors");
  692. $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
  693. return 0;
  694. }
  695. }
  696. $sql = array(
  697. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'invoice' AND entity = ".((int) $conf->entity),
  698. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','invoice',".((int) $conf->entity).")"
  699. );
  700. return $this->_init($sql, $options);
  701. }
  702. }