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