dolibarr  20.0.0-alpha
facture-rec.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
7  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8  * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
9  * Copyright (C) 2017-2024 Frédéric France <frederic.france@free.fr>
10  * Copyright (C) 2023 Nick Fragoulis
11  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <https://www.gnu.org/licenses/>.
25  */
26 
33 require_once DOL_DOCUMENT_ROOT.'/core/class/commoninvoice.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
38 
39 
44 {
45  const TRIGGER_PREFIX = 'BILLREC';
49  public $element = 'facturerec';
50 
54  public $table_element = 'facture_rec';
55 
59  public $table_element_line = 'facturedet_rec';
60 
64  public $fk_element = 'fk_facture';
65 
69  public $picto = 'bill';
70 
74  public $entity;
75 
79  protected $table_ref_field = 'titre';
80 
84  public $title;
85 
90  public $titre;
91 
95  public $multicurrency_subprice;
96  public $socid;
97  public $number;
98  public $date;
99  //public $remise;
100  //public $remise_absolue;
101  //public $remise_percent;
102 
107  public $total;
108 
113  public $tva;
114 
115  public $date_last_gen;
116  public $date_when;
117  public $nb_gen_done;
118  public $nb_gen_max;
119 
120  public $user_author;
121 
125  public $frequency;
126 
130  public $unit_frequency;
131 
132  public $rang;
133 
137  public $special_code;
138 
139  public $usenewprice = 0;
140 
144  public $date_lim_reglement;
145  public $cond_reglement_code; // Code in llx_c_paiement
146  public $mode_reglement_code; // Code in llx_c_paiement
147 
148  public $suspended; // status
149 
150  public $auto_validate; // 0 to create in draft, 1 to create and validate the new invoice
151  public $generate_pdf; // 1 to generate PDF on invoice generation (default)
152 
153 
178  // BEGIN MODULEBUILDER PROPERTIES
182  public $fields = array(
183  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
184  'titre' => array('type' => 'varchar(100)', 'label' => 'Titre', 'enabled' => 1, 'showoncombobox' => 1, 'visible' => -1, 'position' => 15),
185  'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 20, 'index' => 1),
186  'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 25),
187  'subtype' => array('type' => 'smallint(6)', 'label' => 'InvoiceSubtype', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
188  'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 35),
189  'total_tva' => array('type' => 'double(24,8)', 'label' => 'Tva', 'enabled' => 1, 'visible' => -1, 'position' => 55, 'isameasure' => 1),
190  'localtax1' => array('type' => 'double(24,8)', 'label' => 'Localtax1', 'enabled' => 1, 'visible' => -1, 'position' => 60, 'isameasure' => 1),
191  'localtax2' => array('type' => 'double(24,8)', 'label' => 'Localtax2', 'enabled' => 1, 'visible' => -1, 'position' => 65, 'isameasure' => 1),
192  'total_ht' => array('type' => 'double(24,8)', 'label' => 'Total', 'enabled' => 1, 'visible' => -1, 'position' => 70, 'isameasure' => 1),
193  'total_ttc' => array('type' => 'double(24,8)', 'label' => 'Total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 75, 'isameasure' => 1),
194  'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 80),
195  'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Fk projet', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 85),
196  'fk_cond_reglement' => array('type' => 'integer', 'label' => 'Fk cond reglement', 'enabled' => 1, 'visible' => -1, 'position' => 90),
197  'fk_mode_reglement' => array('type' => 'integer', 'label' => 'Fk mode reglement', 'enabled' => 1, 'visible' => -1, 'position' => 95),
198  'date_lim_reglement' => array('type' => 'date', 'label' => 'Date lim reglement', 'enabled' => 1, 'visible' => -1, 'position' => 100),
199  'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 105),
200  'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 110),
201  'modelpdf' => array('type' => 'varchar(255)', 'label' => 'Modelpdf', 'enabled' => 1, 'visible' => -1, 'position' => 115),
202  'date_when' => array('type' => 'datetime', 'label' => 'Date when', 'enabled' => 1, 'visible' => -1, 'position' => 130),
203  'date_last_gen' => array('type' => 'datetime', 'label' => 'Date last gen', 'enabled' => 1, 'visible' => -1, 'position' => 135),
204  'nb_gen_done' => array('type' => 'integer', 'label' => 'Nb gen done', 'enabled' => 1, 'visible' => -1, 'position' => 140),
205  'nb_gen_max' => array('type' => 'integer', 'label' => 'Nb gen max', 'enabled' => 1, 'visible' => -1, 'position' => 145),
206  'frequency' => array('type' => 'integer', 'label' => 'Frequency', 'enabled' => 1, 'visible' => -1, 'position' => 150),
207  'unit_frequency' => array('type' => 'varchar(2)', 'label' => 'UnitFrequency', 'enabled' => 1, 'visible' => -1, 'position' => 152),
208  'usenewprice' => array('type' => 'integer', 'label' => 'UseNewPrice', 'enabled' => 1, 'visible' => 0, 'position' => 155),
209  'revenuestamp' => array('type' => 'double(24,8)', 'label' => 'RevenueStamp', 'enabled' => 1, 'visible' => -1, 'position' => 160, 'isameasure' => 1),
210  'auto_validate' => array('type' => 'integer', 'label' => 'Auto validate', 'enabled' => 1, 'visible' => -1, 'position' => 165),
211  'generate_pdf' => array('type' => 'integer', 'label' => 'Generate pdf', 'enabled' => 1, 'visible' => -1, 'position' => 170),
212  'fk_account' => array('type' => 'integer', 'label' => 'Fk account', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 175),
213  'fk_multicurrency' => array('type' => 'integer', 'label' => 'Fk multicurrency', 'enabled' => 1, 'visible' => -1, 'position' => 180),
214  'multicurrency_code' => array('type' => 'varchar(255)', 'label' => 'Multicurrency code', 'enabled' => 1, 'visible' => -1, 'position' => 185),
215  'multicurrency_tx' => array('type' => 'double(24,8)', 'label' => 'Multicurrency tx', 'enabled' => 1, 'visible' => -1, 'position' => 190, 'isameasure' => 1),
216  'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ht', 'enabled' => 1, 'visible' => -1, 'position' => 195, 'isameasure' => 1),
217  'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total tva', 'enabled' => 1, 'visible' => -1, 'position' => 200, 'isameasure' => 1),
218  'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 205, 'isameasure' => 1),
219  'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 210),
220  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 215),
221  'suspended' => array('type' => 'integer', 'label' => 'Suspended', 'enabled' => 1, 'visible' => -1, 'position' => 225),
222  );
223  // END MODULEBUILDER PROPERTIES
224 
225  const STATUS_NOTSUSPENDED = 0;
226  const STATUS_SUSPENDED = 1;
227 
228 
229 
235  public function __construct(DoliDB $db)
236  {
237  $this->db = $db;
238  }
239 
249  public function create($user, $facid, $notrigger = 0, $onlylines = array())
250  {
251  global $conf;
252 
253  $error = 0;
254  $now = dol_now();
255 
256  // Clean parameters
257  $this->titre = trim(isset($this->titre) ? $this->titre : $this->title); // deprecated
258  $this->title = trim($this->title);
259  $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
260  if (empty($this->suspended)) {
261  $this->suspended = 0;
262  }
263 
264  // No frequency defined then no next date to execution
265  if (empty($this->frequency)) {
266  $this->frequency = 0;
267  $this->date_when = null;
268  }
269 
270  $this->frequency = abs($this->frequency);
271  $this->nb_gen_done = 0;
272  $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
273  $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
274  $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
275 
276  $this->db->begin();
277 
278  // Load invoice template
279  $facsrc = new Facture($this->db);
280  $result = $facsrc->fetch($facid);
281  if ($result > 0) {
282  $this->socid = $facsrc->socid;
283 
284  $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_rec (";
285  $sql .= "titre";
286  $sql .= ", fk_soc";
287  $sql .= ", subtype";
288  $sql .= ", entity";
289  $sql .= ", datec";
290  $sql .= ", amount";
291  //$sql .= ", remise";
292  $sql .= ", note_private";
293  $sql .= ", note_public";
294  $sql .= ", modelpdf";
295  $sql .= ", fk_user_author";
296  $sql .= ", fk_projet";
297  $sql .= ", fk_account";
298  $sql .= ", fk_cond_reglement";
299  $sql .= ", fk_mode_reglement";
300  $sql .= ", usenewprice";
301  $sql .= ", frequency";
302  $sql .= ", unit_frequency";
303  $sql .= ", date_when";
304  $sql .= ", date_last_gen";
305  $sql .= ", nb_gen_done";
306  $sql .= ", nb_gen_max";
307  $sql .= ", auto_validate";
308  $sql .= ", generate_pdf";
309  $sql .= ", fk_multicurrency";
310  $sql .= ", multicurrency_code";
311  $sql .= ", multicurrency_tx";
312  $sql .= ", suspended";
313  $sql .= ") VALUES (";
314  $sql .= "'".$this->db->escape($this->titre ? $this->titre : $this->title)."'";
315  $sql .= ", ".((int) $this->socid);
316  $sql .= ", ".($this->subtype ? "'".$this->db->escape($this->subtype)."'" : "null");
317  $sql .= ", ".((int) $conf->entity);
318  $sql .= ", '".$this->db->idate($now)."'";
319  $sql .= ", ".(!empty($facsrc->total_ttc) ? ((float) $facsrc->total_ttc) : '0');
320  //$sql .= ", ".(!empty($facsrc->remise_absolue) ? ((float) $this->remise_absolue) : '0');
321  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
322  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
323  $sql .= ", ".(!empty($this->model_pdf) ? ("'".$this->db->escape($this->model_pdf)."'") : "NULL");
324  $sql .= ", ".((int) $user->id);
325  $sql .= ", ".(!empty($this->fk_project) ? ((int) $this->fk_project) : "null");
326  $sql .= ", ".(!empty($facsrc->fk_account) ? ((int) $facsrc->fk_account) : "null");
327  $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : "null");
328  $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "null");
329  $sql .= ", ".((int) $this->usenewprice);
330  $sql .= ", ".((int) $this->frequency);
331  $sql .= ", '".$this->db->escape($this->unit_frequency)."'";
332  $sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'NULL');
333  $sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'NULL');
334  $sql .= ", ".((int) $this->nb_gen_done);
335  $sql .= ", ".((int) $this->nb_gen_max);
336  $sql .= ", ".((int) $this->auto_validate);
337  $sql .= ", ".((int) $this->generate_pdf);
338  $sql .= ", ".((int) $facsrc->fk_multicurrency);
339  $sql .= ", '".$this->db->escape($facsrc->multicurrency_code)."'";
340  $sql .= ", ".((float) $facsrc->multicurrency_tx);
341  $sql .= ", ".((int) $this->suspended);
342  $sql .= ")";
343 
344  if ($this->db->query($sql)) {
345  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec");
346 
347  // Fields used into addline later
348  $this->fk_multicurrency = $facsrc->fk_multicurrency;
349  $this->multicurrency_code = $facsrc->multicurrency_code;
350  $this->multicurrency_tx = $facsrc->multicurrency_tx;
351 
352  // Add lines
353  $fk_parent_line = 0;
354 
355  $num = count($facsrc->lines);
356  for ($i = 0; $i < $num; $i++) {
357  if (!empty($onlylines) && !in_array($facsrc->lines[$i]->id, $onlylines)) {
358  continue; // Skip unselected lines
359  }
360 
361  // Reset fk_parent_line for no child products and special product
362  if (($facsrc->lines[$i]->product_type != 9 && empty($facsrc->lines[$i]->fk_parent_line)) || $facsrc->lines[$i]->product_type == 9) {
363  $fk_parent_line = 0;
364  }
365 
366  $tva_tx = $facsrc->lines[$i]->tva_tx;
367  if (!empty($facsrc->lines[$i]->vat_src_code) && !preg_match('/\‍(/', (string) $tva_tx)) {
368  $tva_tx .= ' ('.$facsrc->lines[$i]->vat_src_code.')';
369  }
370 
371  $default_start_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_START');
372  $default_end_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_END');
373 
374  $result_insert = $this->addline(
375  $facsrc->lines[$i]->desc,
376  $facsrc->lines[$i]->subprice,
377  $facsrc->lines[$i]->qty,
378  $tva_tx,
379  $facsrc->lines[$i]->localtax1_tx,
380  $facsrc->lines[$i]->localtax2_tx,
381  $facsrc->lines[$i]->fk_product,
382  $facsrc->lines[$i]->remise_percent,
383  'HT',
384  $facsrc->lines[$i]->info_bits,
385  '',
386  0,
387  $facsrc->lines[$i]->product_type,
388  $facsrc->lines[$i]->rang,
389  $facsrc->lines[$i]->special_code,
390  $facsrc->lines[$i]->label,
391  $facsrc->lines[$i]->fk_unit,
392  $facsrc->lines[$i]->multicurrency_subprice,
393  $default_start_fill,
394  $default_end_fill,
395  null,
396  $facsrc->lines[$i]->pa_ht,
397  $fk_parent_line
398  );
399 
400  // Defined the new fk_parent_line
401  if ($result_insert > 0 && $facsrc->lines[$i]->product_type == 9) {
402  $fk_parent_line = $result_insert;
403  }
404 
405  if ($result_insert < 0) {
406  $error++;
407  } else {
408  $objectline = new FactureLigneRec($this->db);
409 
410  $result2 = $objectline->fetch($result_insert);
411  if ($result2 > 0) {
412  // Extrafields
413  if (method_exists($facsrc->lines[$i], 'fetch_optionals')) {
414  $facsrc->lines[$i]->fetch_optionals($facsrc->lines[$i]->id);
415  $objectline->array_options = $facsrc->lines[$i]->array_options;
416  }
417 
418  $result = $objectline->insertExtraFields();
419  if ($result < 0) {
420  $error++;
421  }
422  } elseif ($result2 < 0) {
423  $this->errors[] = $objectline->error;
424  $error++;
425  }
426  }
427  }
428 
429  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
430  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
431  }
432 
433  // Add object linked
434  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
435  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
436  if (is_array($tmp_origin_id)) { // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...))
437  foreach ($tmp_origin_id as $origin_id) {
438  $ret = $this->add_object_linked($origin, $origin_id);
439  if (!$ret) {
440  $this->error = $this->db->lasterror();
441  $error++;
442  }
443  }
444  } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
445  $origin_id = $tmp_origin_id;
446  $ret = $this->add_object_linked($origin, $origin_id);
447  if (!$ret) {
448  $this->error = $this->db->lasterror();
449  $error++;
450  }
451  }
452  }
453  }
454 
455  if (!$error) {
456  $result = $this->insertExtraFields();
457  if ($result < 0) {
458  $error++;
459  }
460  }
461 
462  if (!$error && !$notrigger) {
463  // Call trigger
464  $result = $this->call_trigger('BILLREC_CREATE', $user);
465  if ($result < 0) {
466  $this->db->rollback();
467  return -2;
468  }
469  // End call triggers
470  }
471 
472  if ($error) {
473  $this->db->rollback();
474  return -3;
475  } else {
476  $this->db->commit();
477  return $this->id;
478  }
479  } else {
480  $this->error = $this->db->lasterror();
481  $this->db->rollback();
482  return -2;
483  }
484  } else {
485  $this->db->rollback();
486  return -1;
487  }
488  }
489 
490 
498  public function update(User $user, $notrigger = 0)
499  {
500  $error = 0;
501 
502  $sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec SET";
503  $sql .= " entity = ".((int) $this->entity).",";
504  $sql .= " titre = '".$this->db->escape($this->title)."',";
505  $sql .= " suspended = ".((int) $this->suspended).",";
506  $sql .= " fk_soc = ".((int) $this->socid).",";
507  $sql .= " total_tva = ".((float) $this->total_tva).",";
508  $sql .= " localtax1 = ".((float) $this->total_localtax1).",";
509  $sql .= " localtax2 = ".((float) $this->total_localtax2).",";
510  $sql .= " total_ht = ".((float) $this->total_ht).",";
511  $sql .= " total_ttc = ".((float) $this->total_ttc);
512  // TODO Add missing fields
513  $sql .= " WHERE rowid = ".((int) $this->id);
514 
515  $this->db->begin();
516 
517  dol_syslog(get_class($this)."::update", LOG_DEBUG);
518 
519  $resql = $this->db->query($sql);
520  if ($resql) {
521  if (!$error) {
522  $result = $this->insertExtraFields();
523  if ($result < 0) {
524  $error++;
525  }
526  }
527 
528  if (!$error && !$notrigger) {
529  // Call trigger
530  $result = $this->call_trigger('BILLREC_MODIFY', $user);
531  if ($result < 0) {
532  $this->db->rollback();
533  return -2;
534  }
535  // End call triggers
536  }
537  $this->db->commit();
538  return 1;
539  } else {
540  $this->error = $this->db->lasterror();
541  $this->db->rollback();
542  return -1;
543  }
544  }
545 
556  public function fetch($rowid, $ref = '', $ref_ext = '', $noextrafields = 0, $nolines = 0)
557  {
558  dol_syslog('FactureRec::fetch', LOG_DEBUG);
559 
560  $sql = 'SELECT f.rowid, f.entity, f.titre as title, f.suspended, f.fk_soc, f.subtype, f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc';
561  $sql .= ', f.date_lim_reglement as dlr';
562  $sql .= ', f.note_private, f.note_public, f.fk_user_author';
563  $sql .= ', f.modelpdf as model_pdf';
564  $sql .= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project';
565  $sql .= ', f.fk_account';
566  $sql .= ', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.usenewprice, f.auto_validate';
567  $sql .= ', f.generate_pdf';
568  $sql .= ", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
569  $sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
570  $sql .= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
571  //$sql.= ', el.fk_source';
572  $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as f';
573  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
574  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id';
575  //$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = f.rowid AND el.targettype = 'facture'";
576  $sql .= ' WHERE f.entity IN ('.getEntity('invoice').')';
577  if ($rowid) {
578  $sql .= ' AND f.rowid = '.((int) $rowid);
579  } elseif ($ref) {
580  $sql .= " AND f.titre = '".$this->db->escape($ref)."'";
581  } else {
582  $sql .= ' AND f.rowid = 0';
583  }
584  /* This field are not used for template invoice
585  if ($ref_ext) $sql.= " AND f.ref_ext='".$this->db->escape($ref_ext)."'";
586  */
587 
588  $result = $this->db->query($sql);
589  if ($result) {
590  if ($this->db->num_rows($result)) {
591  $obj = $this->db->fetch_object($result);
592 
593  $this->id = $obj->rowid;
594  $this->entity = $obj->entity;
595  $this->titre = $obj->title; // deprecated
596  $this->title = $obj->title;
597  $this->ref = $obj->title;
598  $this->subtype = $obj->subtype;
599  $this->suspended = $obj->suspended;
600  $this->total_ht = $obj->total_ht;
601  $this->total_tva = $obj->total_tva;
602  $this->total_localtax1 = $obj->localtax1;
603  $this->total_localtax2 = $obj->localtax2;
604  $this->total_ttc = $obj->total_ttc;
605  $this->socid = $obj->fk_soc;
606  $this->date_lim_reglement = $this->db->jdate($obj->dlr);
607  $this->mode_reglement_id = $obj->fk_mode_reglement;
608  $this->mode_reglement_code = $obj->mode_reglement_code;
609  $this->mode_reglement = $obj->mode_reglement_libelle;
610  $this->cond_reglement_id = $obj->fk_cond_reglement;
611  $this->cond_reglement_code = $obj->cond_reglement_code;
612  $this->cond_reglement = $obj->cond_reglement_libelle;
613  $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
614  $this->fk_project = $obj->fk_project;
615  $this->fk_account = $obj->fk_account;
616  $this->note_private = $obj->note_private;
617  $this->note_public = $obj->note_public;
618  $this->user_author = $obj->fk_user_author;
619  $this->model_pdf = $obj->model_pdf;
620  //$this->special_code = $obj->special_code;
621  $this->frequency = $obj->frequency;
622  $this->unit_frequency = $obj->unit_frequency;
623  $this->date_when = $this->db->jdate($obj->date_when);
624  $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
625  $this->nb_gen_done = $obj->nb_gen_done;
626  $this->nb_gen_max = $obj->nb_gen_max;
627  $this->usenewprice = $obj->usenewprice;
628  $this->auto_validate = $obj->auto_validate;
629  $this->generate_pdf = $obj->generate_pdf;
630 
631  // Multicurrency
632  $this->fk_multicurrency = $obj->fk_multicurrency;
633  $this->multicurrency_code = $obj->multicurrency_code;
634  $this->multicurrency_tx = $obj->multicurrency_tx;
635  $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
636  $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
637  $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
638 
639  // Retrieve all extrafield
640  // fetch optionals attributes and labels
641  if (empty($noextrafields)) {
642  $result = $this->fetch_optionals();
643  if ($result < 0) {
644  $this->error = $this->db->lasterror();
645  return -4;
646  }
647  }
648 
649  // Retrieve lines
650  if (empty($nolines)) {
651  $result = $this->fetch_lines();
652  if ($result < 0) {
653  $this->error = $this->db->lasterror();
654  return -3;
655  }
656  }
657 
658  return 1;
659  } else {
660  $this->error = 'Bill with id '.$rowid.' or ref '.$ref.' not found';
661  dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
662  return -2;
663  }
664  } else {
665  $this->error = $this->db->error();
666  return -1;
667  }
668  }
669 
670 
676  public function getLinesArray()
677  {
678  return $this->fetch_lines();
679  }
680 
681 
682  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
688  public function fetch_lines()
689  {
690  // phpcs:enable
691 
692  $this->lines = array();
693 
694  dol_syslog('FactureRec::fetch_lines', LOG_DEBUG);
695 
696  $sql = 'SELECT l.rowid, l.fk_facture, l.fk_product, l.fk_parent_line, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx, ';
697  $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
698  $sql .= ' l.info_bits, l.date_start_fill, l.date_end_fill, l.total_ht, l.total_tva, l.total_ttc, l.fk_product_fournisseur_price, l.buy_price_ht as pa_ht,';
699  $sql .= ' l.rang, l.special_code,';
700  $sql .= ' l.fk_unit, l.fk_contract_line,';
701  $sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
702  $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
703  $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
704  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
705  $sql .= ' WHERE l.fk_facture = '.((int) $this->id);
706  $sql .= ' ORDER BY l.rang';
707 
708  $result = $this->db->query($sql);
709  if ($result) {
710  $num = $this->db->num_rows($result);
711  $i = 0;
712  while ($i < $num) {
713  $objp = $this->db->fetch_object($result);
714  $line = new FactureLigneRec($this->db);
715 
716  $line->id = $objp->rowid;
717  $line->rowid = $objp->rowid;
718  $line->fk_facture = $objp->fk_facture;
719  $line->fk_parent_line = $objp->fk_parent_line;
720  $line->desc = $objp->description; // Description line
721  $line->description = $objp->description; // Description line
722  $line->ref = $objp->product_ref; // Ref product
723  $line->product_ref = $objp->product_ref; // Ref product
724  $line->libelle = $objp->product_label; // deprecated
725  $line->product_label = $objp->product_label; // Label product
726  $line->product_desc = $objp->product_desc; // Description product
727  $line->product_type = $objp->product_type; // Type of line
728  $line->fk_product_type = $objp->fk_product_type; // Type of product
729  $line->qty = $objp->qty;
730  $line->subprice = $objp->subprice;
731 
732  $line->label = $objp->custom_label; // @deprecated
733 
734  $line->vat_src_code = $objp->vat_src_code;
735  $line->tva_tx = $objp->tva_tx;
736  $line->localtax1_tx = $objp->localtax1_tx;
737  $line->localtax2_tx = $objp->localtax2_tx;
738  $line->localtax1_type = $objp->localtax1_type;
739  $line->localtax2_type = $objp->localtax2_type;
740  $line->remise_percent = $objp->remise_percent;
741  //$line->fk_remise_except = $objp->fk_remise_except;
742  $line->fk_product = $objp->fk_product;
743  $line->date_start_fill = $objp->date_start_fill;
744  $line->date_end_fill = $objp->date_end_fill;
745  $line->info_bits = $objp->info_bits;
746  $line->total_ht = $objp->total_ht;
747  $line->total_tva = $objp->total_tva;
748  $line->total_ttc = $objp->total_ttc;
749 
750  $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
751  $line->fk_fournprice = $objp->fk_product_fournisseur_price; // For backward compatibility
752 
753  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
754 
755  $line->buyprice = $marginInfos[0];
756  $line->pa_ht = $marginInfos[0]; // For backward compatibility
757  $line->marge_tx = $marginInfos[1];
758  $line->marque_tx = $marginInfos[2];
759  $line->rang = $objp->rang;
760  $line->special_code = $objp->special_code;
761  $line->fk_unit = $objp->fk_unit;
762  $line->fk_contract_line = $objp->fk_contract_line;
763 
764  // Ne plus utiliser
765  $line->price = $objp->price;
766  $line->remise = $objp->remise;
767 
768  $line->fetch_optionals();
769 
770  // Multicurrency
771  $line->fk_multicurrency = $objp->fk_multicurrency;
772  $line->multicurrency_code = $objp->multicurrency_code;
773  $line->multicurrency_subprice = $objp->multicurrency_subprice;
774  $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
775  $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
776  $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
777 
778  $this->lines[$i] = $line;
779 
780  $i++;
781  }
782 
783  $this->db->free($result);
784  return 1;
785  } else {
786  $this->error = $this->db->lasterror();
787  return -3;
788  }
789  }
790 
791 
800  public function delete(User $user, $notrigger = 0, $idwarehouse = -1)
801  {
802  $rowid = $this->id;
803 
804  dol_syslog(get_class($this)."::delete rowid=".((int) $rowid), LOG_DEBUG);
805 
806  $error = 0;
807  $this->db->begin();
808 
809  $main = MAIN_DB_PREFIX.'facturedet_rec';
810  $ef = $main."_extrafields";
811 
812  $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_facture = ".((int) $rowid).")";
813  $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = ".((int) $rowid);
814 
815  if ($this->db->query($sqlef) && $this->db->query($sql)) {
816  $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_rec WHERE rowid = ".((int) $rowid);
817  dol_syslog($sql);
818  if ($this->db->query($sql)) {
819  // Delete linked object
820  $res = $this->deleteObjectLinked();
821  if ($res < 0) {
822  $error = -3;
823  }
824  // Delete extrafields
825  $res = $this->deleteExtraFields();
826  if ($res < 0) {
827  $error = -4;
828  }
829  } else {
830  $this->error = $this->db->lasterror();
831  $error = -1;
832  }
833  } else {
834  $this->error = $this->db->lasterror();
835  $error = -2;
836  }
837  if (!$error && !$notrigger) {
838  // Call trigger
839  $result = $this->call_trigger('BILLREC_DELETE', $user);
840  if ($result < 0) {
841  $error++;
842  }
843  // End call triggers
844  }
845  if (!$error) {
846  $this->db->commit();
847  return 1;
848  } else {
849  $this->db->rollback();
850  return $error;
851  }
852  }
853 
854 
883  public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $info_bits = 0, $fk_remise_except = 0, $pu_ttc = 0, $type = 0, $rang = -1, $special_code = 0, $label = '', $fk_unit = null, $pu_ht_devise = 0, $date_start_fill = 0, $date_end_fill = 0, $fk_fournprice = null, $pa_ht = 0, $fk_parent_line = 0)
884  {
885  global $mysoc;
886 
887  $facid = $this->id;
888 
889  dol_syslog(get_class($this)."::addline facid=$facid,desc=$desc,pu_ht=$pu_ht,qty=$qty,txtva=$txtva,txlocaltax1=$txlocaltax1,txlocaltax2=$txlocaltax2,fk_product=$fk_product,remise_percent=$remise_percent,info_bits=$info_bits,fk_remise_except=$fk_remise_except,price_base_type=$price_base_type,pu_ttc=$pu_ttc,type=$type,fk_unit=$fk_unit,pu_ht_devise=$pu_ht_devise,date_start_fill=$date_start_fill,date_end_fill=$date_end_fill,pa_ht=$pa_ht", LOG_DEBUG);
890  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
891 
892  // Check parameters
893  if ($type < 0) {
894  return -1;
895  }
896 
897  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
898 
899  // Clean vat code
900  $reg = array();
901  $vat_src_code = '';
902  if (preg_match('/\‍((.*)\‍)/', (string) $txtva, $reg)) {
903  $vat_src_code = $reg[1];
904  $txtva = preg_replace('/\s*\‍(.*\‍)/', '', (string) $txtva); // Remove code from vatrate.
905  }
906 
907 
908  // Clean parameters
909  $remise_percent = price2num($remise_percent);
910  if (empty($remise_percent)) {
911  $remise_percent = 0;
912  }
913  $qty = price2num($qty);
914  $pu_ht = price2num($pu_ht);
915  $pu_ttc = price2num($pu_ttc);
916  if (!preg_match('/\‍((.*)\‍)/', $txtva)) {
917  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
918  }
919  $txlocaltax1 = price2num($txlocaltax1);
920  $txlocaltax2 = price2num($txlocaltax2);
921  if (empty($txtva)) {
922  $txtva = 0;
923  }
924  if (empty($txlocaltax1)) {
925  $txlocaltax1 = 0;
926  }
927  if (empty($txlocaltax2)) {
928  $txlocaltax2 = 0;
929  }
930  if (empty($info_bits)) {
931  $info_bits = 0;
932  }
933 
934  if ($price_base_type == 'HT') {
935  $pu = $pu_ht;
936  } else {
937  $pu = $pu_ttc;
938  }
939 
940  // Calcul du total TTC et de la TVA pour la ligne a partir de
941  // qty, pu, remise_percent et txtva
942  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
943  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
944 
945  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
946  $total_ht = $tabprice[0];
947  $total_tva = $tabprice[1];
948  $total_ttc = $tabprice[2];
949  $total_localtax1 = $tabprice[9];
950  $total_localtax2 = $tabprice[10];
951  $pu_ht = $tabprice[3];
952 
953  // MultiCurrency
954  $multicurrency_total_ht = $tabprice[16];
955  $multicurrency_total_tva = $tabprice[17];
956  $multicurrency_total_ttc = $tabprice[18];
957  $pu_ht_devise = $tabprice[19];
958 
959  $product_type = $type;
960  if ($fk_product) {
961  $product = new Product($this->db);
962  $result = $product->fetch($fk_product);
963  $product_type = $product->type;
964  }
965 
966  if (empty($fk_parent_line) || $fk_parent_line < 0) {
967  $fk_parent_line = 0;
968  }
969 
970  // Rank to use
971  $ranktouse = $rang;
972  if ($ranktouse == -1) {
973  $rangmax = $this->line_max(0);
974  $ranktouse = $rangmax + 1;
975  }
976 
977  $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (";
978  $sql .= "fk_facture";
979  $sql .= ", fk_parent_line";
980  $sql .= ", label";
981  $sql .= ", description";
982  $sql .= ", price";
983  $sql .= ", qty";
984  $sql .= ", tva_tx";
985  $sql .= ", vat_src_code";
986  $sql .= ", localtax1_tx";
987  $sql .= ", localtax1_type";
988  $sql .= ", localtax2_tx";
989  $sql .= ", localtax2_type";
990  $sql .= ", fk_product";
991  $sql .= ", product_type";
992  $sql .= ", remise_percent";
993  $sql .= ", subprice";
994  $sql .= ", remise";
995  $sql .= ", total_ht";
996  $sql .= ", total_tva";
997  $sql .= ", total_localtax1";
998  $sql .= ", total_localtax2";
999  $sql .= ", total_ttc";
1000  $sql .= ", date_start_fill";
1001  $sql .= ", date_end_fill";
1002  $sql .= ", fk_product_fournisseur_price";
1003  $sql .= ", buy_price_ht";
1004  $sql .= ", info_bits";
1005  $sql .= ", rang";
1006  $sql .= ", special_code";
1007  $sql .= ", fk_unit";
1008  $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
1009  $sql .= ") VALUES (";
1010  $sql .= " ".((int) $facid);
1011  $sql .= ", ".($fk_parent_line > 0 ? ((int) $fk_parent_line) : "null");
1012  $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1013  $sql .= ", '".$this->db->escape($desc)."'";
1014  $sql .= ", ".price2num($pu_ht);
1015  $sql .= ", ".price2num($qty);
1016  $sql .= ", ".price2num($txtva);
1017  $sql .= ", '".$this->db->escape($vat_src_code)."'";
1018  $sql .= ", ".price2num($txlocaltax1);
1019  $sql .= ", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] : '')."'";
1020  $sql .= ", ".price2num($txlocaltax2);
1021  $sql .= ", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] : '')."'";
1022  $sql .= ", ".(!empty($fk_product) ? "'".$this->db->escape($fk_product)."'" : "null");
1023  $sql .= ", ".((int) $product_type);
1024  $sql .= ", ".price2num($remise_percent);
1025  $sql .= ", ".price2num($pu_ht);
1026  $sql .= ", null";
1027  $sql .= ", ".price2num($total_ht);
1028  $sql .= ", ".price2num($total_tva);
1029  $sql .= ", ".price2num($total_localtax1);
1030  $sql .= ", ".price2num($total_localtax2);
1031  $sql .= ", ".price2num($total_ttc);
1032  $sql .= ", ".(int) $date_start_fill;
1033  $sql .= ", ".(int) $date_end_fill;
1034  $sql .= ", ".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1035  $sql .= ", ".($pa_ht ? price2num($pa_ht) : 0);
1036  $sql .= ", ".((int) $info_bits);
1037  $sql .= ", ".((int) $ranktouse);
1038  $sql .= ", ".((int) $special_code);
1039  $sql .= ", ".($fk_unit ? ((int) $fk_unit) : "null");
1040  $sql .= ", ".(int) $this->fk_multicurrency;
1041  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
1042  $sql .= ", ".price2num($pu_ht_devise, 'CU');
1043  $sql .= ", ".price2num($multicurrency_total_ht, 'CT');
1044  $sql .= ", ".price2num($multicurrency_total_tva, 'CT');
1045  $sql .= ", ".price2num($multicurrency_total_ttc, 'CT');
1046  $sql .= ")";
1047 
1048  dol_syslog(get_class($this)."::addline", LOG_DEBUG);
1049  if ($this->db->query($sql)) {
1050  $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX."facturedet_rec");
1051  $this->id = $facid;
1052  $this->update_price(1);
1053  return $lineId;
1054  } else {
1055  $this->error = $this->db->lasterror();
1056  return -1;
1057  }
1058  }
1059 
1090  public function updateline($rowid, $desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $info_bits = 0, $fk_remise_except = 0, $pu_ttc = 0, $type = 0, $rang = -1, $special_code = 0, $label = '', $fk_unit = null, $pu_ht_devise = 0, $notrigger = 0, $date_start_fill = 0, $date_end_fill = 0, $fk_fournprice = null, $pa_ht = 0, $fk_parent_line = 0)
1091  {
1092  global $mysoc;
1093 
1094  $facid = $this->id;
1095 
1096  dol_syslog(get_class($this)."::updateline facid=".$facid." rowid=$rowid, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, type=$type, fk_unit=$fk_unit, pu_ht_devise=$pu_ht_devise", LOG_DEBUG);
1097  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1098 
1099  // Clean parameters
1100  if (empty($remise_percent)) {
1101  $remise_percent = 0;
1102  }
1103 
1104  // Check parameters
1105  if ($type < 0) {
1106  return -1;
1107  }
1108 
1109  // Clean parameters
1110  $remise_percent = price2num($remise_percent);
1111  $qty = price2num($qty);
1112  if (empty($info_bits)) {
1113  $info_bits = 0;
1114  }
1115  $pu_ht = price2num($pu_ht);
1116  $pu_ttc = price2num($pu_ttc);
1117  $pu_ht_devise = price2num($pu_ht_devise);
1118  if (!preg_match('/\‍((.*)\‍)/', (string) $txtva)) {
1119  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1120  }
1121  $txlocaltax1 = price2num($txlocaltax1);
1122  $txlocaltax2 = price2num($txlocaltax2);
1123  if (empty($txlocaltax1)) {
1124  $txlocaltax1 = 0;
1125  }
1126  if (empty($txlocaltax2)) {
1127  $txlocaltax2 = 0;
1128  }
1129 
1130  if (empty($this->multicurrency_subprice)) {
1131  $this->multicurrency_subprice = 0;
1132  }
1133  if (empty($this->multicurrency_total_ht)) {
1134  $this->multicurrency_total_ht = 0;
1135  }
1136  if (empty($this->multicurrency_total_tva)) {
1137  $this->multicurrency_total_tva = 0;
1138  }
1139  if (empty($this->multicurrency_total_ttc)) {
1140  $this->multicurrency_total_ttc = 0;
1141  }
1142 
1143  if ($price_base_type == 'HT') {
1144  $pu = $pu_ht;
1145  } else {
1146  $pu = $pu_ttc;
1147  }
1148 
1149  // Calculate total with, without tax and tax from qty, pu, remise_percent and txtva
1150  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1151  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1152 
1153  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
1154 
1155  // Clean vat code
1156  $vat_src_code = '';
1157  $reg = array();
1158  if (preg_match('/\‍((.*)\‍)/', $txtva, $reg)) {
1159  $vat_src_code = $reg[1];
1160  $txtva = preg_replace('/\s*\‍(.*\‍)/', '', $txtva); // Remove code into vatrate.
1161  }
1162 
1163  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
1164 
1165  $total_ht = $tabprice[0];
1166  $total_tva = $tabprice[1];
1167  $total_ttc = $tabprice[2];
1168  $total_localtax1 = $tabprice[9];
1169  $total_localtax2 = $tabprice[10];
1170  $pu_ht = $tabprice[3];
1171  $pu_tva = $tabprice[4];
1172  $pu_ttc = $tabprice[5];
1173 
1174  // MultiCurrency
1175  $multicurrency_total_ht = $tabprice[16];
1176  $multicurrency_total_tva = $tabprice[17];
1177  $multicurrency_total_ttc = $tabprice[18];
1178  $pu_ht_devise = $tabprice[19];
1179 
1180  $product_type = $type;
1181  if ($fk_product) {
1182  $product = new Product($this->db);
1183  $result = $product->fetch($fk_product);
1184  $product_type = $product->type;
1185  }
1186 
1187  if (empty($fk_parent_line) || $fk_parent_line < 0) {
1188  $fk_parent_line = 0;
1189  }
1190 
1191  $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET ";
1192  $sql .= "fk_facture = ".((int) $facid);
1193  $sql .= ", fk_parent_line = ".($fk_parent_line > 0 ? ((int) $fk_parent_line) : "null");
1194  $sql .= ", label=".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1195  $sql .= ", description='".$this->db->escape($desc)."'";
1196  $sql .= ", price=".price2num($pu_ht);
1197  $sql .= ", qty=".price2num($qty);
1198  $sql .= ", tva_tx=".price2num($txtva);
1199  $sql .= ", vat_src_code='".$this->db->escape($vat_src_code)."'";
1200  $sql .= ", localtax1_tx=".((float) $txlocaltax1);
1201  $sql .= ", localtax1_type='".$this->db->escape($localtaxes_type[0])."'";
1202  $sql .= ", localtax2_tx=".((float) $txlocaltax2);
1203  $sql .= ", localtax2_type='".$this->db->escape($localtaxes_type[2])."'";
1204  $sql .= ", fk_product=".(!empty($fk_product) ? "'".$this->db->escape($fk_product)."'" : "null");
1205  $sql .= ", product_type=".((int) $product_type);
1206  $sql .= ", remise_percent='".price2num($remise_percent)."'";
1207  $sql .= ", subprice='".price2num($pu_ht)."'";
1208  $sql .= ", total_ht='".price2num($total_ht)."'";
1209  $sql .= ", total_tva='".price2num($total_tva)."'";
1210  $sql .= ", total_localtax1='".price2num($total_localtax1)."'";
1211  $sql .= ", total_localtax2='".price2num($total_localtax2)."'";
1212  $sql .= ", total_ttc='".price2num($total_ttc)."'";
1213  $sql .= ", date_start_fill=".((int) $date_start_fill);
1214  $sql .= ", date_end_fill=".((int) $date_end_fill);
1215  $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1216  $sql .= ", buy_price_ht=".($pa_ht ? price2num($pa_ht) : 0);
1217  $sql .= ", info_bits=".((int) $info_bits);
1218  $sql .= ", rang=".((int) $rang);
1219  $sql .= ", special_code=".((int) $special_code);
1220  $sql .= ", fk_unit=".($fk_unit ? "'".$this->db->escape($fk_unit)."'" : "null");
1221  $sql .= ', multicurrency_subprice = '.price2num($pu_ht_devise);
1222  $sql .= ', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1223  $sql .= ', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1224  $sql .= ', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1225  $sql .= " WHERE rowid = ".((int) $rowid);
1226 
1227  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1228  if ($this->db->query($sql)) {
1229  $this->id = $facid;
1230  $this->update_price(1);
1231  return 1;
1232  } else {
1233  $this->error = $this->db->lasterror();
1234  return -1;
1235  }
1236  }
1237 
1238 
1244  public function getNextDate()
1245  {
1246  if (empty($this->date_when)) {
1247  return false;
1248  }
1249  return dol_time_plus_duree($this->date_when, $this->frequency, $this->unit_frequency);
1250  }
1251 
1257  public function isMaxNbGenReached()
1258  {
1259  $ret = false;
1260  if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1261  $ret = true;
1262  }
1263  return $ret;
1264  }
1265 
1272  public function strikeIfMaxNbGenReached($ret)
1273  {
1274  return $this->isMaxNbGenReached() ? '<strike>'.$ret.'</strike>' : $ret;
1275  }
1276 
1288  public function createRecurringInvoices($restrictioninvoiceid = 0, $forcevalidation = 0, $notrigger = 0)
1289  {
1290  global $conf, $langs, $db, $user, $hookmanager;
1291 
1292  $error = 0;
1293  $nb_create = 0;
1294 
1295  // Load translation files required by the page
1296  $langs->loadLangs(array("main", "bills"));
1297 
1298  $now = dol_now();
1299  $tmparray = dol_getdate($now);
1300  $today = dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year']); // Today is last second of current day
1301 
1302  $this->output = '';
1303 
1304  dol_syslog("createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid." forcevalidation=".$forcevalidation);
1305 
1306  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_rec';
1307  $sql .= ' WHERE frequency > 0'; // A recurring invoice is an invoice with a frequency
1308  $sql .= " AND (date_when IS NULL OR date_when <= '".$this->db->idate($today)."')";
1309  $sql .= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1310  $sql .= ' AND suspended = 0';
1311  $sql .= ' AND entity = '.$conf->entity; // MUST STAY = $conf->entity here
1312  if ($restrictioninvoiceid > 0) {
1313  $sql .= ' AND rowid = '.((int) $restrictioninvoiceid);
1314  }
1315  $sql .= $this->db->order('entity', 'ASC');
1316  //print $sql;exit;
1317  $parameters = array(
1318  'restrictioninvoiceid' => $restrictioninvoiceid,
1319  'forcevalidation' => $forcevalidation,
1320  );
1321  $reshook = $hookmanager->executeHooks('beforeCreationOfRecurringInvoices', $parameters, $sql); // note that $sql might be modified by hooks
1322 
1323  $resql = $this->db->query($sql);
1324  if ($resql) {
1325  $i = 0;
1326  $num = $this->db->num_rows($resql);
1327 
1328  if ($num) {
1329  $this->output .= $langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n";
1330  } else {
1331  $this->output .= $langs->trans("NoQualifiedRecurringInvoiceTemplateFound");
1332  }
1333 
1334  $saventity = $conf->entity;
1335 
1336  while ($i < $num) { // Loop on each template invoice. If $num = 0, test is false at first pass.
1337  $line = $this->db->fetch_object($resql);
1338 
1339  $this->db->begin();
1340 
1341  $invoiceidgenerated = 0;
1342 
1343  $facture = null;
1344  $facturerec = new FactureRec($this->db);
1345  $facturerec->fetch($line->rowid);
1346 
1347  if ($facturerec->id > 0) {
1348  // Set entity context
1349  $conf->entity = $facturerec->entity;
1350 
1351  dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity);
1352 
1353  $facture = new Facture($this->db);
1354  $facture->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice
1355  $facture->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice
1356 
1357  $facture->type = self::TYPE_STANDARD;
1358  $facture->subtype = $facturerec->subtype;
1359  $facture->statut = self::STATUS_DRAFT; // deprecated
1360  $facture->status = self::STATUS_DRAFT;
1361  $facture->date = (empty($facturerec->date_when) ? $now : $facturerec->date_when); // We could also use dol_now here but we prefer date_when so invoice has real date when we would like even if we generate later.
1362  $facture->socid = $facturerec->socid;
1363  if (!empty($facturerec->fk_multicurrency)) {
1364  $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1365  $facture->multicurrency_code = $facturerec->multicurrency_code;
1366  $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1367  }
1368 
1369  $invoiceidgenerated = $facture->create($user);
1370  if ($invoiceidgenerated <= 0) {
1371  $this->errors = $facture->errors;
1372  $this->error = $facture->error;
1373  $error++;
1374  }
1375 
1376 
1377  if (!$error && ($facturerec->auto_validate || $forcevalidation)) {
1378  $result = $facture->validate($user);
1379  if ($result <= 0) {
1380  $this->errors = $facture->errors;
1381  $this->error = $facture->error;
1382  $error++;
1383  }
1384  }
1385  if (!$error && $facturerec->generate_pdf) {
1386  // We refresh the object in order to have all necessary data (like date_lim_reglement)
1387  $facture->fetch($facture->id);
1388  $result = $facture->generateDocument($facturerec->model_pdf, $langs);
1389  if ($result <= 0) {
1390  $this->errors = $facture->errors;
1391  $this->error = $facture->error;
1392  $error++;
1393  }
1394  }
1395  } else {
1396  $error++;
1397  $this->error = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity."\n";
1398  $this->errors[] = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity;
1399  dol_syslog("createRecurringInvoices Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity);
1400  }
1401 
1402  if (!$error && $invoiceidgenerated >= 0) {
1403  $this->db->commit("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1404  dol_syslog("createRecurringInvoices Process invoice template ".$facturerec->ref." is finished with a success generation");
1405  $nb_create++;
1406  $this->output .= $langs->trans("InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref)."\n";
1407  } else {
1408  $this->db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1409  }
1410 
1411  $parameters = array(
1412  'cpt' => $i,
1413  'total' => $num,
1414  'errorCount' => $error,
1415  'invoiceidgenerated' => $invoiceidgenerated,
1416  'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks.
1417  'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks.
1418  );
1419  $reshook = $hookmanager->executeHooks('afterCreationOfRecurringInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null)
1420 
1421  $i++;
1422  }
1423 
1424  $conf->entity = $saventity; // Restore entity context
1425  } else {
1426  dol_print_error($this->db);
1427  }
1428 
1429  $this->output = trim($this->output);
1430 
1431  return $error ? $error : 0;
1432  }
1433 
1446  public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1)
1447  {
1448  global $langs, $hookmanager;
1449 
1450  $result = '';
1451 
1452  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("RepeatableInvoice").'</u>';
1453  if (!empty($this->ref)) {
1454  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1455  }
1456  if ($this->frequency > 0) {
1457  $label .= '<br><b>'.$langs->trans('Frequency').':</b> '.$langs->trans('FrequencyPer_'.$this->unit_frequency, $this->frequency);
1458  }
1459  if (!empty($this->date_last_gen)) {
1460  $label .= '<br><b>'.$langs->trans('DateLastGeneration').':</b> '.dol_print_date($this->date_last_gen, 'dayhour');
1461  }
1462  if ($this->frequency > 0) {
1463  if (!empty($this->date_when)) {
1464  $label .= '<br><b>'.$langs->trans('NextDateToExecution').':</b> ';
1465  $label .= (empty($this->suspended) ? '' : '<strike>').dol_print_date($this->date_when, 'day').(empty($this->suspended) ? '' : '</strike>'); // No hour for this property
1466  if (!empty($this->suspended)) {
1467  $label .= ' ('.$langs->trans("Disabled").')';
1468  }
1469  }
1470  }
1471 
1472  $url = DOL_URL_ROOT.'/compta/facture/card-rec.php?facid='.$this->id;
1473 
1474  if ($short) {
1475  return $url;
1476  }
1477 
1478  if ($option != 'nolink') {
1479  // Add param to save lastsearch_values or not
1480  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1481  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1482  $add_save_lastsearch_values = 1;
1483  }
1484  if ($add_save_lastsearch_values) {
1485  $url .= '&save_lastsearch_values=1';
1486  }
1487  }
1488 
1489  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1490  $linkend = '</a>';
1491 
1492  $result .= $linkstart;
1493  if ($withpicto) {
1494  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1495  }
1496  if ($withpicto != 2) {
1497  $result .= $this->ref;
1498  }
1499  $result .= $linkend;
1500  global $action;
1501  $hookmanager->initHooks(array($this->element . 'dao'));
1502  $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1503  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1504  if ($reshook > 0) {
1505  $result = $hookmanager->resPrint;
1506  } else {
1507  $result .= $hookmanager->resPrint;
1508  }
1509  return $result;
1510  }
1511 
1519  public function getLibStatut($mode = 0, $alreadypaid = -1)
1520  {
1521  return $this->LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->type) ? 0 : $this->type);
1522  }
1523 
1524  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1535  public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0)
1536  {
1537  // phpcs:enable
1538  global $langs;
1539  $langs->load('bills');
1540 
1541  $labelStatus = $langs->transnoentitiesnoconv('Active');
1542  $statusType = 'status0';
1543 
1544  //print "$recur,$status,$mode,$alreadypaid,$type";
1545  if ($mode == 0) {
1546  if ($recur) {
1547  if ($status == self::STATUS_SUSPENDED) {
1548  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1549  } else {
1550  $labelStatus = $langs->transnoentitiesnoconv('Active');
1551  }
1552  } else {
1553  if ($status == self::STATUS_SUSPENDED) {
1554  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1555  } else {
1556  $labelStatus = $langs->transnoentitiesnoconv("Draft");
1557  }
1558  }
1559  } elseif ($mode == 1) {
1560  $prefix = 'Short';
1561  if ($recur) {
1562  if ($status == self::STATUS_SUSPENDED) {
1563  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1564  } else {
1565  $labelStatus = $langs->transnoentitiesnoconv('Active');
1566  }
1567  } else {
1568  if ($status == self::STATUS_SUSPENDED) {
1569  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1570  } else {
1571  $labelStatus = $langs->transnoentitiesnoconv("Draft");
1572  }
1573  }
1574  } elseif ($mode == 2) {
1575  if ($recur) {
1576  if ($status == self::STATUS_SUSPENDED) {
1577  $statusType = 'status6';
1578  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1579  } else {
1580  $statusType = 'status4';
1581  $labelStatus = $langs->transnoentitiesnoconv('Active');
1582  }
1583  } else {
1584  if ($status == self::STATUS_SUSPENDED) {
1585  $statusType = 'status6';
1586  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1587  } else {
1588  $statusType = 'status0'; // @phan-suppress-current-line PhanPluginRedundantAssignment
1589  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1590  }
1591  }
1592  } elseif ($mode == 3) {
1593  if ($recur) {
1594  $prefix = 'Short';
1595  if ($status == self::STATUS_SUSPENDED) {
1596  $statusType = 'status6';
1597  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1598  } else {
1599  $statusType = 'status4';
1600  $labelStatus = $langs->transnoentitiesnoconv('Active');
1601  }
1602  } else {
1603  if ($status == self::STATUS_SUSPENDED) {
1604  $statusType = 'status6';
1605  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1606  } else {
1607  $statusType = 'status0'; // @phan-suppress-current-line PhanPluginRedundantAssignment
1608  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1609  }
1610  }
1611  } elseif ($mode == 4) {
1612  $prefix = '';
1613  if ($recur) {
1614  if ($status == self::STATUS_SUSPENDED) {
1615  $statusType = 'status6';
1616  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1617  } else {
1618  $statusType = 'status4';
1619  $labelStatus = $langs->transnoentitiesnoconv('Active');
1620  }
1621  } else {
1622  if ($status == self::STATUS_SUSPENDED) {
1623  $statusType = 'status6';
1624  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1625  } else {
1626  $statusType = 'status0'; // @phan-suppress-current-line PhanPluginRedundantAssignment
1627  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1628  }
1629  }
1630  } elseif ($mode == 5 || $mode == 6) {
1631  $prefix = '';
1632  if ($mode == 5) {
1633  $prefix = 'Short';
1634  }
1635  if ($recur) {
1636  if ($status == self::STATUS_SUSPENDED) {
1637  $statusType = 'status6';
1638  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1639  } else {
1640  $statusType = 'status4';
1641  $labelStatus = $langs->transnoentitiesnoconv('Active');
1642  }
1643  } else {
1644  if ($status == self::STATUS_SUSPENDED) {
1645  $statusType = 'status6';
1646  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1647  } else {
1648  $statusType = 'status0'; // @phan-suppress-current-line PhanPluginRedundantAssignment
1649  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1650  }
1651  }
1652  }
1653 
1654  $labelStatusShort = $labelStatus;
1655 
1656  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1657  }
1658 
1666  public function getNextNumRef($soc, $mode = 'next')
1667  {
1668  // Not used for recurring invoices
1669  return '';
1670  }
1671 
1678  public function info($id)
1679  {
1680  $sql = 'SELECT c.rowid, datec, tms as datem,';
1681  $sql .= ' fk_user_author, fk_user_modif';
1682  $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as c';
1683  $sql .= ' WHERE c.rowid = '.((int) $id);
1684 
1685  $result = $this->db->query($sql);
1686  if ($result) {
1687  if ($this->db->num_rows($result)) {
1688  $obj = $this->db->fetch_object($result);
1689 
1690  $this->id = $obj->rowid;
1691 
1692  $this->user_creation_id = $obj->fk_user_author;
1693  $this->user_modification_id = $obj->fk_user_modif;
1694  $this->date_creation = $this->db->jdate($obj->datec);
1695  $this->date_modification = $this->db->jdate($obj->datem);
1696  }
1697  $this->db->free($result);
1698  } else {
1699  dol_print_error($this->db);
1700  }
1701  }
1702 
1711  public function initAsSpecimen($option = '')
1712  {
1713  global $langs;
1714 
1715  $now = dol_now();
1716  $arraynow = dol_getdate($now);
1717  $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1718 
1719  // Load array of products prodids
1720  $num_prods = 0;
1721  $prodids = array();
1722 
1723  $sql = "SELECT rowid";
1724  $sql .= " FROM ".MAIN_DB_PREFIX."product";
1725  $sql .= " WHERE entity IN (".getEntity('product').")";
1726  $sql .= $this->db->plimit(100);
1727 
1728  $resql = $this->db->query($sql);
1729  if ($resql) {
1730  $num_prods = $this->db->num_rows($resql);
1731  $i = 0;
1732  while ($i < $num_prods) {
1733  $i++;
1734  $row = $this->db->fetch_row($resql);
1735  $prodids[$i] = $row[0];
1736  }
1737  }
1738 
1739  // Initialize parameters
1740  $this->id = 0;
1741  $this->ref = 'SPECIMEN';
1742  $this->title = 'SPECIMEN';
1743  $this->specimen = 1;
1744  $this->socid = 1;
1745  $this->date = $nownotime;
1746  $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
1747  $this->cond_reglement_id = 1;
1748  $this->cond_reglement_code = 'RECEP';
1749  $this->date_lim_reglement = $this->calculate_date_lim_reglement();
1750  $this->mode_reglement_id = 0; // Not forced to show payment mode CHQ + VIR
1751  $this->mode_reglement_code = ''; // Not forced to show payment mode CHQ + VIR
1752  $this->note_public = 'This is a comment (public)';
1753  $this->note_private = 'This is a comment (private)';
1754  $this->note = 'This is a comment (private)';
1755  $this->fk_incoterms = 0;
1756  $this->location_incoterms = '';
1757 
1758  if (empty($option) || $option != 'nolines') {
1759  // Lines
1760  $nbp = 5;
1761  $xnbp = 0;
1762  while ($xnbp < $nbp) {
1763  $line = new FactureLigne($this->db);
1764  $line->desc = $langs->trans("Description")." ".$xnbp;
1765  $line->qty = 1;
1766  $line->subprice = 100;
1767  $line->tva_tx = 19.6;
1768  $line->localtax1_tx = 0;
1769  $line->localtax2_tx = 0;
1770  $line->remise_percent = 0;
1771  if ($xnbp == 1) { // Qty is negative (product line)
1772  $prodid = mt_rand(1, $num_prods);
1773  $line->fk_product = $prodids[$prodid];
1774  $line->qty = -1;
1775  $line->total_ht = -100;
1776  $line->total_ttc = -119.6;
1777  $line->total_tva = -19.6;
1778  } elseif ($xnbp == 2) { // UP is negative (free line)
1779  $line->subprice = -100;
1780  $line->total_ht = -100;
1781  $line->total_ttc = -119.6;
1782  $line->total_tva = -19.6;
1783  $line->remise_percent = 0;
1784  } elseif ($xnbp == 3) { // Discount is 50% (product line)
1785  $prodid = mt_rand(1, $num_prods);
1786  $line->fk_product = $prodids[$prodid];
1787  $line->total_ht = 50;
1788  $line->total_ttc = 59.8;
1789  $line->total_tva = 9.8;
1790  $line->remise_percent = 50;
1791  } else { // (product line)
1792  $prodid = mt_rand(1, $num_prods);
1793  $line->fk_product = $prodids[$prodid];
1794  $line->total_ht = 100;
1795  $line->total_ttc = 119.6;
1796  $line->total_tva = 19.6;
1797  $line->remise_percent = 00;
1798  }
1799 
1800  $this->lines[$xnbp] = $line;
1801  $xnbp++;
1802 
1803  $this->total_ht += $line->total_ht;
1804  $this->total_tva += $line->total_tva;
1805  $this->total_ttc += $line->total_ttc;
1806  }
1807  $this->revenuestamp = 0;
1808 
1809  // Add a line "offered"
1810  $line = new FactureLigne($this->db);
1811  $line->desc = $langs->trans("Description")." (offered line)";
1812  $line->qty = 1;
1813  $line->subprice = 100;
1814  $line->tva_tx = 19.6;
1815  $line->localtax1_tx = 0;
1816  $line->localtax2_tx = 0;
1817  $line->remise_percent = 100;
1818  $line->total_ht = 0;
1819  $line->total_ttc = 0; // 90 * 1.196
1820  $line->total_tva = 0;
1821  $prodid = mt_rand(1, $num_prods);
1822  $line->fk_product = $prodids[$prodid];
1823 
1824  $this->lines[$xnbp] = $line;
1825  $xnbp++;
1826  }
1827 
1828  $this->usenewprice = 0;
1829 
1830  return 1;
1831  }
1832 
1841  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1842  {
1843  $tables = array(
1844  'facture_rec'
1845  );
1846 
1847  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1848  }
1849 
1858  public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1859  {
1860  $tables = array(
1861  'facturedet_rec'
1862  );
1863 
1864  return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1865  }
1866 
1875  public function setFrequencyAndUnit($frequency, $unit, $notrigger = 0)
1876  {
1877  global $user;
1878 
1879  if (!$this->table_element) {
1880  dol_syslog(get_class($this)."::setFrequencyAndUnit was called on object with property table_element not defined", LOG_ERR);
1881  return -1;
1882  }
1883 
1884  if (!empty($frequency) && empty($unit)) {
1885  dol_syslog(get_class($this)."::setFrequencyAndUnit was called on object with params frequency defined but unit not defined", LOG_ERR);
1886  return -2;
1887  }
1888 
1889  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1890  $sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null');
1891  if (!empty($unit)) {
1892  $sql .= ', unit_frequency = \''.$this->db->escape($unit).'\'';
1893  }
1894  $sql .= " WHERE rowid = ".((int) $this->id);
1895 
1896  dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG);
1897  if ($this->db->query($sql)) {
1898  $this->frequency = $frequency;
1899  if (!empty($unit)) {
1900  $this->unit_frequency = $unit;
1901  }
1902 
1903  if (!$notrigger) {
1904  // Call trigger
1905  $result = $this->call_trigger('BILLREC_MODIFY', $user);
1906  if ($result < 0) {
1907  return $result;
1908  }
1909  // End call triggers
1910  }
1911 
1912  return 1;
1913  } else {
1914  dol_print_error($this->db);
1915  return -1;
1916  }
1917  }
1918 
1927  public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
1928  {
1929  global $user;
1930 
1931  if (!$this->table_element) {
1932  dol_syslog(get_class($this)."::setNextDate was called on object with property table_element not defined", LOG_ERR);
1933  return -1;
1934  }
1935  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1936  $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
1937  if ($increment_nb_gen_done > 0) {
1938  $sql .= ', nb_gen_done = nb_gen_done + 1';
1939  }
1940  $sql .= " WHERE rowid = ".((int) $this->id);
1941 
1942  dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG);
1943  if ($this->db->query($sql)) {
1944  $this->date_when = $date;
1945  if ($increment_nb_gen_done > 0) {
1946  $this->nb_gen_done++;
1947  }
1948 
1949  if (!$notrigger) {
1950  // Call trigger
1951  $result = $this->call_trigger('BILLREC_MODIFY', $user);
1952  if ($result < 0) {
1953  return $result;
1954  }
1955  // End call triggers
1956  }
1957  return 1;
1958  } else {
1959  dol_print_error($this->db);
1960  return -1;
1961  }
1962  }
1963 
1971  public function setMaxPeriod($nb, $notrigger = 0)
1972  {
1973  global $user;
1974 
1975  if (!$this->table_element) {
1976  dol_syslog(get_class($this)."::setMaxPeriod was called on object with property table_element not defined", LOG_ERR);
1977  return -1;
1978  }
1979 
1980  if (empty($nb)) {
1981  $nb = 0;
1982  }
1983 
1984  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1985  $sql .= ' SET nb_gen_max = '.((int) $nb);
1986  $sql .= " WHERE rowid = ".((int) $this->id);
1987 
1988  dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG);
1989  if ($this->db->query($sql)) {
1990  $this->nb_gen_max = $nb;
1991 
1992  if (!$notrigger) {
1993  // Call trigger
1994  $result = $this->call_trigger('BILLREC_MODIFY', $user);
1995  if ($result < 0) {
1996  return $result;
1997  }
1998  // End call triggers
1999  }
2000 
2001  return 1;
2002  } else {
2003  dol_print_error($this->db);
2004  return -1;
2005  }
2006  }
2007 
2015  public function setAutoValidate($validate, $notrigger = 0)
2016  {
2017  global $user;
2018 
2019  if (!$this->table_element) {
2020  dol_syslog(get_class($this)."::setAutoValidate was called on object with property table_element not defined", LOG_ERR);
2021  return -1;
2022  }
2023 
2024  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2025  $sql .= ' SET auto_validate = '.((int) $validate);
2026  $sql .= " WHERE rowid = ".((int) $this->id);
2027 
2028  dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
2029  if ($this->db->query($sql)) {
2030  $this->auto_validate = $validate;
2031 
2032  if (!$notrigger) {
2033  // Call trigger
2034  $result = $this->call_trigger('BILLREC_MODIFY', $user);
2035  if ($result < 0) {
2036  return $result;
2037  }
2038  // End call triggers
2039  }
2040 
2041  return 1;
2042  } else {
2043  dol_print_error($this->db);
2044  return -1;
2045  }
2046  }
2047 
2055  public function setGeneratePdf($validate, $notrigger = 0)
2056  {
2057  global $user;
2058 
2059  if (!$this->table_element) {
2060  dol_syslog(get_class($this)."::setGeneratePdf was called on object with property table_element not defined", LOG_ERR);
2061  return -1;
2062  }
2063 
2064  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2065  $sql .= ' SET generate_pdf = '.((int) $validate);
2066  $sql .= " WHERE rowid = ".((int) $this->id);
2067 
2068  dol_syslog(get_class($this)."::setGeneratePdf", LOG_DEBUG);
2069  if ($this->db->query($sql)) {
2070  $this->generate_pdf = $validate;
2071 
2072  if (!$notrigger) {
2073  // Call trigger
2074  $result = $this->call_trigger('BILLREC_MODIFY', $user);
2075  if ($result < 0) {
2076  return $result;
2077  }
2078  // End call triggers
2079  }
2080 
2081  return 1;
2082  } else {
2083  dol_print_error($this->db);
2084  return -1;
2085  }
2086  }
2087 
2095  public function setModelPdf($model, $notrigger = 0)
2096  {
2097  global $user;
2098  if (!$this->table_element) {
2099  dol_syslog(get_class($this)."::setModelPdf was called on object with property table_element not defined", LOG_ERR);
2100  return -1;
2101  }
2102 
2103  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2104  $sql .= " SET modelpdf = '".$this->db->escape($model)."'";
2105  $sql .= " WHERE rowid = ".((int) $this->id);
2106 
2107  dol_syslog(get_class($this)."::setModelPdf", LOG_DEBUG);
2108  if ($this->db->query($sql)) {
2109  $this->model_pdf = $model;
2110 
2111  if (!$notrigger) {
2112  // Call trigger
2113  $result = $this->call_trigger('BILLREC_MODIFY', $user);
2114  if ($result < 0) {
2115  return $result;
2116  }
2117  // End call triggers
2118  }
2119 
2120  return 1;
2121  } else {
2122  dol_print_error($this->db);
2123  return -1;
2124  }
2125  }
2126 }
2127 
2128 
2129 
2135 {
2139  public $element = 'facturedetrec';
2140 
2144  public $table_element = 'facturedet_rec';
2145 
2148  public $fk_facture;
2151 
2152  public $fk_product_fournisseur_price;
2153  public $fk_fournprice; // For backward compatibility
2154 
2155  public $rang;
2156  //public $situation_percent; // Not supported on recurring invoice line
2157 
2158  public $desc;
2159  public $description;
2160 
2161  public $fk_product_type; // Use instead product_type
2162 
2163  public $fk_contract_line;
2164 
2165 
2173  public function delete(User $user, $notrigger = 0)
2174  {
2175  $error = 0;
2176 
2177  $this->db->begin();
2178 
2179  if (!$error) {
2180  if (!$notrigger) {
2181  // Call triggers
2182  $result = $this->call_trigger('LINEBILLREC_DELETE', $user);
2183  if ($result < 0) {
2184  $error++;
2185  } // Do also here what you must do to rollback action if trigger fail
2186  // End call triggers
2187  }
2188  }
2189 
2190  if (!$error) {
2191  $result = $this->deleteExtraFields();
2192  if ($result < 0) {
2193  $error++;
2194  }
2195  }
2196 
2197  if (!$error) {
2198  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.((int) $this->id);
2199 
2200  $res = $this->db->query($sql);
2201  if (!$res) {
2202  $error++;
2203  $this->errors[] = $this->db->lasterror();
2204  }
2205  }
2206 
2207  // Commit or rollback
2208  if ($error) {
2209  $this->db->rollback();
2210  return -1;
2211  } else {
2212  $this->db->commit();
2213  return 1;
2214  }
2215  }
2216 
2217 
2224  public function fetch($rowid)
2225  {
2226  $sql = 'SELECT l.rowid, l.fk_facture, l.fk_parent_line, l.fk_product, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx,';
2227  $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2228  $sql .= ' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2229  $sql .= ' l.rang, l.special_code,';
2230  $sql .= ' l.fk_unit, l.fk_contract_line,';
2231  $sql .= ' l.import_key, l.fk_multicurrency,';
2232  $sql .= ' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2233  $sql .= ' l.buy_price_ht, l.fk_product_fournisseur_price,';
2234  $sql .= ' l.fk_user_author, l.fk_user_modif,';
2235  $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2236  $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
2237  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
2238  $sql .= ' WHERE l.rowid = '.((int) $rowid);
2239  $sql .= ' ORDER BY l.rang';
2240 
2241  dol_syslog('FactureRec::fetch', LOG_DEBUG);
2242  $result = $this->db->query($sql);
2243  if ($result) {
2244  $objp = $this->db->fetch_object($result);
2245 
2246  $this->id = $objp->rowid;
2247  $this->fk_facture = $objp->fk_facture;
2248  $this->fk_parent_line = $objp->fk_parent_line;
2249  $this->label = $objp->custom_label; // Label line
2250  $this->desc = $objp->description; // Description line
2251  $this->description = $objp->description; // Description line
2252  $this->product_type = $objp->product_type; // Type of line
2253  $this->ref = $objp->product_ref; // Ref product
2254  $this->product_ref = $objp->product_ref; // Ref product
2255  $this->libelle = $objp->product_label; // deprecated
2256  $this->product_label = $objp->product_label; // Label product
2257  $this->product_desc = $objp->product_desc; // Description product
2258  $this->fk_product_type = $objp->fk_product_type; // Type of product
2259  $this->qty = $objp->qty;
2260  $this->price = $objp->price;
2261  $this->subprice = $objp->subprice;
2262  $this->vat_src_code = $objp->vat_src_code;
2263  $this->tva_tx = $objp->tva_tx;
2264  $this->localtax1_tx = $objp->localtax1_tx;
2265  $this->localtax2_tx = $objp->localtax2_tx;
2266  $this->localtax1_type = $objp->localtax1_type;
2267  $this->localtax2_type = $objp->localtax2_type;
2268  $this->remise_percent = $objp->remise_percent;
2269  //$this->fk_remise_except = $objp->fk_remise_except;
2270  $this->fk_product = $objp->fk_product;
2271  $this->date_start_fill = $objp->date_start_fill;
2272  $this->date_end_fill = $objp->date_end_fill;
2273  $this->info_bits = $objp->info_bits;
2274  $this->total_ht = $objp->total_ht;
2275  $this->total_tva = $objp->total_tva;
2276  $this->total_ttc = $objp->total_ttc;
2277 
2278  $this->rang = $objp->rang;
2279  $this->special_code = $objp->special_code;
2280  $this->fk_unit = $objp->fk_unit;
2281  $this->fk_contract_line = $objp->fk_contract_line;
2282  $this->import_key = $objp->import_key;
2283  $this->fk_multicurrency = $objp->fk_multicurrency;
2284  $this->multicurrency_code = $objp->multicurrency_code;
2285  $this->multicurrency_subprice = $objp->multicurrency_subprice;
2286  $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2287  $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2288  $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2289 
2290  $this->buy_price_ht = $objp->buy_price_ht;
2291 
2292  $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2293  $this->fk_user_author = $objp->fk_user_author;
2294  $this->fk_user_modif = $objp->fk_user_modif;
2295 
2296  $this->db->free($result);
2297  return 1;
2298  } else {
2299  $this->error = $this->db->lasterror();
2300  return -3;
2301  }
2302  }
2303 
2304 
2312  public function update(User $user, $notrigger = 0)
2313  {
2314  global $conf;
2315 
2316  $error = 0;
2317 
2318  // Clean parameters
2319  if (empty($this->fk_parent_line)) {
2320  $this->fk_parent_line = 0;
2321  }
2322 
2323  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2324 
2325  $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET";
2326  $sql .= " fk_facture = ".((int) $this->fk_facture);
2327  $sql .= ", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
2328  $sql .= ", label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
2329  $sql .= ", description='".$this->db->escape($this->desc)."'";
2330  $sql .= ", price=".price2num($this->price);
2331  $sql .= ", qty=".price2num($this->qty);
2332  $sql .= ", tva_tx=".price2num($this->tva_tx);
2333  $sql .= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'";
2334  $sql .= ", localtax1_tx=".price2num($this->localtax1_tx);
2335  $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
2336  $sql .= ", localtax2_tx=".price2num($this->localtax2_tx);
2337  $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
2338  $sql .= ", fk_product=".($this->fk_product > 0 ? $this->fk_product : "null");
2339  $sql .= ", product_type=".((int) $this->product_type);
2340  $sql .= ", remise_percent=".price2num($this->remise_percent);
2341  $sql .= ", subprice=".price2num($this->subprice);
2342  $sql .= ", info_bits=".price2num($this->info_bits);
2343  $sql .= ", date_start_fill=".(int) $this->date_start_fill;
2344  $sql .= ", date_end_fill=".(int) $this->date_end_fill;
2345  if (empty($this->skip_update_total)) {
2346  $sql .= ", total_ht=".price2num($this->total_ht);
2347  $sql .= ", total_tva=".price2num($this->total_tva);
2348  $sql .= ", total_localtax1=".price2num($this->total_localtax1);
2349  $sql .= ", total_localtax2=".price2num($this->total_localtax2);
2350  $sql .= ", total_ttc=".price2num($this->total_ttc);
2351  }
2352  $sql .= ", rang=".((int) $this->rang);
2353  $sql .= ", special_code=".((int) $this->special_code);
2354  $sql .= ", fk_unit=".($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : "null");
2355  $sql .= ", fk_contract_line=".($this->fk_contract_line ? $this->fk_contract_line : "null");
2356  $sql .= " WHERE rowid = ".((int) $this->id);
2357 
2358  $this->db->begin();
2359 
2360  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
2361 
2362  $resql = $this->db->query($sql);
2363  if ($resql) {
2364  if (!$error) {
2365  $result = $this->insertExtraFields();
2366  if ($result < 0) {
2367  $error++;
2368  }
2369  }
2370 
2371  if (!$error && !$notrigger) {
2372  // Call trigger
2373  $result = $this->call_trigger('LINEBILLREC_MODIFY', $user);
2374  if ($result < 0) {
2375  $error++;
2376  }
2377  // End call triggers
2378  }
2379 
2380  if ($error) {
2381  $this->db->rollback();
2382  return -2;
2383  } else {
2384  $this->db->commit();
2385  return 1;
2386  }
2387  } else {
2388  $this->error = $this->db->lasterror();
2389  $this->db->rollback();
2390  return -2;
2391  }
2392  }
2393 }
$object ref
Definition: info.php:79
Superclass for invoices classes.
const TYPE_STANDARD
Standard invoice.
calculate_date_lim_reglement($cond_reglement=0)
Returns an invoice payment deadline based on the invoice settlement conditions and billing date.
const STATUS_DRAFT
Draft status.
Parent class of all other business classes for details of elements (invoices, contracts,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
update_price($exclspec=0, $roundingadjust='auto', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage invoices.
Class to manage invoice lines.
Class to manage invoice lines of templates.
$fk_parent_line
Id parent line.
fetch($rowid)
Get line of template invoice.
$fk_facture
From llx_facturedet_rec Id facture.
update(User $user, $notrigger=0)
Update a line to invoice_rec.
Class to manage invoice templates.
setFrequencyAndUnit($frequency, $unit, $notrigger=0)
Update frequency and unit.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of object status.
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type='HT', $info_bits=0, $fk_remise_except=0, $pu_ttc=0, $type=0, $rang=-1, $special_code=0, $label='', $fk_unit=null, $pu_ht_devise=0, $date_start_fill=0, $date_end_fill=0, $fk_fournprice=null, $pa_ht=0, $fk_parent_line=0)
Add a line to invoice.
LibStatut($recur, $status, $mode=0, $alreadypaid=-1, $type=0)
Return label of a status.
updateline($rowid, $desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type='HT', $info_bits=0, $fk_remise_except=0, $pu_ttc=0, $type=0, $rang=-1, $special_code=0, $label='', $fk_unit=null, $pu_ht_devise=0, $notrigger=0, $date_start_fill=0, $date_end_fill=0, $fk_fournprice=null, $pa_ht=0, $fk_parent_line=0)
Update a line to invoice.
fetch_lines()
Get lines of template invoices into this->lines.
info($id)
Load miscellaneous information for tab "Info".
getLinesArray()
Create an array of invoice lines.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
isMaxNbGenReached()
Return if maximum number of generation is reached.
setModelPdf($model, $notrigger=0)
Update the model for documents.
getNextNumRef($soc, $mode='next')
Return next reference of invoice not already used (or last reference)
__construct(DoliDB $db)
Constructor.
setNextDate($date, $increment_nb_gen_done=0, $notrigger=0)
Update the next date of execution.
setAutoValidate($validate, $notrigger=0)
Update the auto validate flag of invoice.
initAsSpecimen($option='')
Initialise an instance with random values.
setMaxPeriod($nb, $notrigger=0)
Update the maximum period.
update(User $user, $notrigger=0)
Update a line invoice_rec.
create($user, $facid, $notrigger=0, $onlylines=array())
Create a predefined invoice.
strikeIfMaxNbGenReached($ret)
Format string to output with by striking the string if max number of generation was reached.
fetch($rowid, $ref='', $ref_ext='', $noextrafields=0, $nolines=0)
Load object and lines.
setGeneratePdf($validate, $notrigger=0)
Update the auto generate documents.
static replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a product id with another one.
getNextDate()
Return the next date of.
createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0, $notrigger=0)
Create all recurrents invoices (for all entities if multicompany is used).
Class to manage products or services.
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:123
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
div float
Buy price without taxes.
Definition: style.css.php:959
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:88
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:122