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