dolibarr  17.0.3
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-2020 Frédéric France <frederic.france@netlogic.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
31 require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
35 
36 
41 {
42  const TRIGGER_PREFIX = 'BILLREC';
46  public $element = 'facturerec';
47 
51  public $table_element = 'facture_rec';
52 
56  public $table_element_line = 'facturedet_rec';
57 
61  public $fk_element = 'fk_facture';
62 
66  public $picto = 'bill';
67 
71  public $entity;
72 
76  protected $table_ref_field = 'titre';
77 
81  public $title;
82 
83  public $socid;
84  public $number;
85  public $date;
86  public $remise;
87  public $remise_absolue;
88  public $remise_percent;
89 
94  public $total;
95 
100  public $tva;
101 
102  public $date_last_gen;
103  public $date_when;
104  public $nb_gen_done;
105  public $nb_gen_max;
106 
107  public $user_author;
108 
112  public $frequency;
113 
117  public $unit_frequency;
118 
119  public $rang;
120  public $special_code;
121 
122  public $usenewprice = 0;
123 
124  public $date_lim_reglement;
125  public $cond_reglement_code; // Code in llx_c_paiement
126  public $mode_reglement_code; // Code in llx_c_paiement
127 
128  public $suspended; // status
129 
130  public $auto_validate; // 0 to create in draft, 1 to create and validate the new invoice
131  public $generate_pdf; // 1 to generate PDF on invoice generation (default)
132 
137  public $brouillon;
138 
139 
164  // BEGIN MODULEBUILDER PROPERTIES
168  public $fields = array(
169  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
170  'titre' =>array('type'=>'varchar(100)', 'label'=>'Titre', 'enabled'=>1, 'showoncombobox' => 1, 'visible'=>-1, 'position'=>15),
171  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>20, 'index'=>1),
172  'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>'$conf->societe->enabled', 'visible'=>-1, 'notnull'=>1, 'position'=>25),
173  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
174  //'amount' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>35),
175  'remise' =>array('type'=>'double', 'label'=>'Remise', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
176  //'remise_percent' =>array('type'=>'double', 'label'=>'Remise percent', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
177  //'remise_absolue' =>array('type'=>'double', 'label'=>'Remise absolue', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
178  'total_tva' =>array('type'=>'double(24,8)', 'label'=>'Tva', 'enabled'=>1, 'visible'=>-1, 'position'=>55, 'isameasure'=>1),
179  'localtax1' =>array('type'=>'double(24,8)', 'label'=>'Localtax1', 'enabled'=>1, 'visible'=>-1, 'position'=>60, 'isameasure'=>1),
180  'localtax2' =>array('type'=>'double(24,8)', 'label'=>'Localtax2', 'enabled'=>1, 'visible'=>-1, 'position'=>65, 'isameasure'=>1),
181  'total_ht' =>array('type'=>'double(24,8)', 'label'=>'Total', 'enabled'=>1, 'visible'=>-1, 'position'=>70, 'isameasure'=>1),
182  'total_ttc' =>array('type'=>'double(24,8)', 'label'=>'Total ttc', 'enabled'=>1, 'visible'=>-1, 'position'=>75, 'isameasure'=>1),
183  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
184  '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),
185  'fk_cond_reglement' =>array('type'=>'integer', 'label'=>'Fk cond reglement', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
186  'fk_mode_reglement' =>array('type'=>'integer', 'label'=>'Fk mode reglement', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
187  'date_lim_reglement' =>array('type'=>'date', 'label'=>'Date lim reglement', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
188  'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>105),
189  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>110),
190  'modelpdf' =>array('type'=>'varchar(255)', 'label'=>'Modelpdf', 'enabled'=>1, 'visible'=>-1, 'position'=>115),
191  'date_when' =>array('type'=>'datetime', 'label'=>'Date when', 'enabled'=>1, 'visible'=>-1, 'position'=>130),
192  'date_last_gen' =>array('type'=>'datetime', 'label'=>'Date last gen', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
193  'nb_gen_done' =>array('type'=>'integer', 'label'=>'Nb gen done', 'enabled'=>1, 'visible'=>-1, 'position'=>140),
194  'nb_gen_max' =>array('type'=>'integer', 'label'=>'Nb gen max', 'enabled'=>1, 'visible'=>-1, 'position'=>145),
195  'frequency' =>array('type'=>'integer', 'label'=>'Frequency', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
196  'unit_frequency' =>array('type'=>'varchar(2)', 'label'=>'UnitFrequency', 'enabled'=>1, 'visible'=>-1, 'position'=>152),
197  'usenewprice' =>array('type'=>'integer', 'label'=>'UseNewPrice', 'enabled'=>1, 'visible'=>0, 'position'=>155),
198  'revenuestamp' =>array('type'=>'double(24,8)', 'label'=>'RevenueStamp', 'enabled'=>1, 'visible'=>-1, 'position'=>160, 'isameasure'=>1),
199  'auto_validate' =>array('type'=>'integer', 'label'=>'Auto validate', 'enabled'=>1, 'visible'=>-1, 'position'=>165),
200  'generate_pdf' =>array('type'=>'integer', 'label'=>'Generate pdf', 'enabled'=>1, 'visible'=>-1, 'position'=>170),
201  'fk_account' =>array('type'=>'integer', 'label'=>'Fk account', 'enabled'=>'$conf->banque->enabled', 'visible'=>-1, 'position'=>175),
202  'fk_multicurrency' =>array('type'=>'integer', 'label'=>'Fk multicurrency', 'enabled'=>1, 'visible'=>-1, 'position'=>180),
203  'multicurrency_code' =>array('type'=>'varchar(255)', 'label'=>'Multicurrency code', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
204  'multicurrency_tx' =>array('type'=>'double(24,8)', 'label'=>'Multicurrency tx', 'enabled'=>1, 'visible'=>-1, 'position'=>190, 'isameasure'=>1),
205  'multicurrency_total_ht' =>array('type'=>'double(24,8)', 'label'=>'Multicurrency total ht', 'enabled'=>1, 'visible'=>-1, 'position'=>195, 'isameasure'=>1),
206  'multicurrency_total_tva' =>array('type'=>'double(24,8)', 'label'=>'Multicurrency total tva', 'enabled'=>1, 'visible'=>-1, 'position'=>200, 'isameasure'=>1),
207  'multicurrency_total_ttc' =>array('type'=>'double(24,8)', 'label'=>'Multicurrency total ttc', 'enabled'=>1, 'visible'=>-1, 'position'=>205, 'isameasure'=>1),
208  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>210),
209  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>215),
210  'suspended' =>array('type'=>'integer', 'label'=>'Suspended', 'enabled'=>1, 'visible'=>-1, 'position'=>225),
211  );
212  // END MODULEBUILDER PROPERTIES
213 
214  const STATUS_NOTSUSPENDED = 0;
215  const STATUS_SUSPENDED = 1;
216 
217 
218 
224  public function __construct(DoliDB $db)
225  {
226  $this->db = $db;
227  }
228 
237  public function create($user, $facid, $notrigger = 0)
238  {
239  global $conf;
240 
241  $error = 0;
242  $now = dol_now();
243 
244  // Clean parameters
245  $this->titre = trim(isset($this->titre) ? $this->titre : $this->title); // deprecated
246  $this->title = trim($this->title);
247  $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
248  if (empty($this->suspended)) {
249  $this->suspended = 0;
250  }
251 
252  // No frequency defined then no next date to execution
253  if (empty($this->frequency)) {
254  $this->frequency = 0;
255  $this->date_when = null;
256  }
257 
258  $this->frequency = abs($this->frequency);
259  $this->nb_gen_done = 0;
260  $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
261  $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
262  $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
263 
264  $this->db->begin();
265 
266  // Charge facture modele
267  $facsrc = new Facture($this->db);
268  $result = $facsrc->fetch($facid);
269  if ($result > 0) {
270  // On positionne en mode brouillon la facture
271  $this->brouillon = 1;
272  $this->fk_soc = $facsrc->socid;
273 
274  $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_rec (";
275  $sql .= "titre";
276  $sql .= ", fk_soc";
277  $sql .= ", entity";
278  $sql .= ", datec";
279  $sql .= ", amount";
280  $sql .= ", remise";
281  $sql .= ", note_private";
282  $sql .= ", note_public";
283  $sql .= ", modelpdf";
284  $sql .= ", fk_user_author";
285  $sql .= ", fk_projet";
286  $sql .= ", fk_account";
287  $sql .= ", fk_cond_reglement";
288  $sql .= ", fk_mode_reglement";
289  $sql .= ", usenewprice";
290  $sql .= ", frequency";
291  $sql .= ", unit_frequency";
292  $sql .= ", date_when";
293  $sql .= ", date_last_gen";
294  $sql .= ", nb_gen_done";
295  $sql .= ", nb_gen_max";
296  $sql .= ", auto_validate";
297  $sql .= ", generate_pdf";
298  $sql .= ", fk_multicurrency";
299  $sql .= ", multicurrency_code";
300  $sql .= ", multicurrency_tx";
301  $sql .= ", suspended";
302  $sql .= ") VALUES (";
303  $sql .= "'".$this->db->escape($this->titre ? $this->titre : $this->title)."'";
304  $sql .= ", ".((int) $this->fk_soc);
305  $sql .= ", ".((int) $conf->entity);
306  $sql .= ", '".$this->db->idate($now)."'";
307  $sql .= ", ".(!empty($facsrc->total_ttc) ? ((float) $facsrc->total_ttc) : '0');
308  $sql .= ", ".(!empty($facsrc->remise_absolue) ? ((float) $this->remise_absolue) : '0');
309  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
310  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
311  $sql .= ", ".(!empty($this->model_pdf) ? ("'".$this->db->escape($this->model_pdf)."'") : "NULL");
312  $sql .= ", ".((int) $user->id);
313  $sql .= ", ".(!empty($this->fk_project) ? ((int) $this->fk_project) : "null");
314  $sql .= ", ".(!empty($facsrc->fk_account) ? ((int) $facsrc->fk_account) : "null");
315  $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : "null");
316  $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "null");
317  $sql .= ", ".((int) $this->usenewprice);
318  $sql .= ", ".((int) $this->frequency);
319  $sql .= ", '".$this->db->escape($this->unit_frequency)."'";
320  $sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'NULL');
321  $sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'NULL');
322  $sql .= ", ".((int) $this->nb_gen_done);
323  $sql .= ", ".((int) $this->nb_gen_max);
324  $sql .= ", ".((int) $this->auto_validate);
325  $sql .= ", ".((int) $this->generate_pdf);
326  $sql .= ", ".((int) $facsrc->fk_multicurrency);
327  $sql .= ", '".$this->db->escape($facsrc->multicurrency_code)."'";
328  $sql .= ", ".((float) $facsrc->multicurrency_tx);
329  $sql .= ", ".((int) $this->suspended);
330  $sql .= ")";
331 
332  if ($this->db->query($sql)) {
333  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec");
334 
335  // Fields used into addline later
336  $this->fk_multicurrency = $facsrc->fk_multicurrency;
337  $this->multicurrency_code = $facsrc->multicurrency_code;
338  $this->multicurrency_tx = $facsrc->multicurrency_tx;
339 
340  // Add lines
341  $num = count($facsrc->lines);
342  for ($i = 0; $i < $num; $i++) {
343  $tva_tx = $facsrc->lines[$i]->tva_tx;
344  if (!empty($facsrc->lines[$i]->vat_src_code) && !preg_match('/\(/', $tva_tx)) {
345  $tva_tx .= ' ('.$facsrc->lines[$i]->vat_src_code.')';
346  }
347 
348  $default_start_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_START');
349  $default_end_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_END');
350 
351  $result_insert = $this->addline(
352  $facsrc->lines[$i]->desc,
353  $facsrc->lines[$i]->subprice,
354  $facsrc->lines[$i]->qty,
355  $tva_tx,
356  $facsrc->lines[$i]->localtax1_tx,
357  $facsrc->lines[$i]->localtax2_tx,
358  $facsrc->lines[$i]->fk_product,
359  $facsrc->lines[$i]->remise_percent,
360  'HT',
361  $facsrc->lines[$i]->info_bits,
362  '',
363  0,
364  $facsrc->lines[$i]->product_type,
365  $facsrc->lines[$i]->rang,
366  $facsrc->lines[$i]->special_code,
367  $facsrc->lines[$i]->label,
368  $facsrc->lines[$i]->fk_unit,
369  $facsrc->lines[$i]->multicurrency_subprice,
370  $default_start_fill,
371  $default_end_fill,
372  null,
373  $facsrc->lines[$i]->pa_ht
374  );
375 
376  if ($result_insert < 0) {
377  $error++;
378  } else {
379  $objectline = new FactureLigneRec($this->db);
380 
381  $result2 = $objectline->fetch($result_insert);
382  if ($result2 > 0) {
383  // Extrafields
384  if (method_exists($facsrc->lines[$i], 'fetch_optionals')) {
385  $facsrc->lines[$i]->fetch_optionals($facsrc->lines[$i]->id);
386  $objectline->array_options = $facsrc->lines[$i]->array_options;
387  }
388 
389  $result = $objectline->insertExtraFields();
390  if ($result < 0) {
391  $error++;
392  }
393  } elseif ($result2 < 0) {
394  $this->errors[] = $objectline->error;
395  $error++;
396  }
397  }
398  }
399 
400  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
401  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
402  }
403 
404  // Add object linked
405  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
406  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
407  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, ...))
408  foreach ($tmp_origin_id as $origin_id) {
409  $ret = $this->add_object_linked($origin, $origin_id);
410  if (!$ret) {
411  $this->error = $this->db->lasterror();
412  $error++;
413  }
414  }
415  } else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
416  {
417  $origin_id = $tmp_origin_id;
418  $ret = $this->add_object_linked($origin, $origin_id);
419  if (!$ret) {
420  $this->error = $this->db->lasterror();
421  $error++;
422  }
423  }
424  }
425  }
426 
427  if (!$error) {
428  $result = $this->insertExtraFields();
429  if ($result < 0) {
430  $error++;
431  }
432  }
433 
434  if (!$error && !$notrigger) {
435  // Call trigger
436  $result = $this->call_trigger('BILLREC_CREATE', $user);
437  if ($result < 0) {
438  $this->db->rollback();
439  return -2;
440  }
441  // End call triggers
442  }
443 
444  if ($error) {
445  $this->db->rollback();
446  return -3;
447  } else {
448  $this->db->commit();
449  return $this->id;
450  }
451  } else {
452  $this->error = $this->db->lasterror();
453  $this->db->rollback();
454  return -2;
455  }
456  } else {
457  $this->db->rollback();
458  return -1;
459  }
460  }
461 
462 
470  public function update(User $user, $notrigger = 0)
471  {
472  $error = 0;
473 
474  $sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec SET";
475  $sql .= " entity = ".((int) $this->entity).",";
476  $sql .= " titre = '".$this->db->escape($this->title)."',";
477  $sql .= " suspended = ".((int) $this->suspended).",";
478  $sql .= " fk_soc = ".((int) $this->socid).",";
479  $sql .= " total_tva = ".((float) $this->total_tva).",";
480  $sql .= " localtax1 = ".((float) $this->total_localtax1).",";
481  $sql .= " localtax2 = ".((float) $this->total_localtax2).",";
482  $sql .= " total_ht = ".((float) $this->total_ht).",";
483  $sql .= " total_ttc = ".((float) $this->total_ttc).",";
484  $sql .= " remise_percent = ".((float) $this->remise_percent);
485  // TODO Add missing fields
486  $sql .= " WHERE rowid = ".((int) $this->id);
487 
488  $this->db->begin();
489 
490  dol_syslog(get_class($this)."::update", LOG_DEBUG);
491 
492  $resql = $this->db->query($sql);
493  if ($resql) {
494  if (!$error) {
495  $result = $this->insertExtraFields();
496  if ($result < 0) {
497  $error++;
498  }
499  }
500 
501  if (!$error && !$notrigger) {
502  // Call trigger
503  $result = $this->call_trigger('BILLREC_MODIFY', $user);
504  if ($result < 0) {
505  $this->db->rollback();
506  return -2;
507  }
508  // End call triggers
509  }
510  $this->db->commit();
511  return 1;
512  } else {
513  $this->error = $this->db->lasterror();
514  $this->db->rollback();
515  return -1;
516  }
517  }
518 
527  public function fetch($rowid, $ref = '', $ref_ext = '')
528  {
529  dol_syslog('FactureRec::fetch', LOG_DEBUG);
530 
531  $sql = 'SELECT f.rowid, f.entity, f.titre as title, f.suspended, f.fk_soc, f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc';
532  $sql .= ', f.remise_percent, f.remise_absolue, f.remise';
533  $sql .= ', f.date_lim_reglement as dlr';
534  $sql .= ', f.note_private, f.note_public, f.fk_user_author';
535  $sql .= ', f.modelpdf as model_pdf';
536  $sql .= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project';
537  $sql .= ', f.fk_account';
538  $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';
539  $sql .= ', f.generate_pdf';
540  $sql .= ", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
541  $sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
542  $sql .= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
543  //$sql.= ', el.fk_source';
544  $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as f';
545  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
546  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id';
547  //$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = f.rowid AND el.targettype = 'facture'";
548  $sql .= ' WHERE f.entity IN ('.getEntity('invoice').')';
549  if ($rowid) {
550  $sql .= ' AND f.rowid = '.((int) $rowid);
551  } elseif ($ref) {
552  $sql .= " AND f.titre = '".$this->db->escape($ref)."'";
553  } else {
554  $sql .= ' AND f.rowid = 0';
555  }
556  /* This field are not used for template invoice
557  if ($ref_ext) $sql.= " AND f.ref_ext='".$this->db->escape($ref_ext)."'";
558  */
559 
560  $result = $this->db->query($sql);
561  if ($result) {
562  if ($this->db->num_rows($result)) {
563  $obj = $this->db->fetch_object($result);
564 
565  $this->id = $obj->rowid;
566  $this->entity = $obj->entity;
567  $this->titre = $obj->title; // deprecated
568  $this->title = $obj->title;
569  $this->ref = $obj->title;
570  $this->suspended = $obj->suspended;
571  $this->remise_percent = $obj->remise_percent;
572  $this->remise_absolue = $obj->remise_absolue;
573  $this->remise = $obj->remise;
574  $this->total_ht = $obj->total_ht;
575  $this->total_tva = $obj->total_tva;
576  $this->total_localtax1 = $obj->localtax1;
577  $this->total_localtax2 = $obj->localtax2;
578  $this->total_ttc = $obj->total_ttc;
579  $this->socid = $obj->fk_soc;
580  $this->date_lim_reglement = $this->db->jdate($obj->dlr);
581  $this->mode_reglement_id = $obj->fk_mode_reglement;
582  $this->mode_reglement_code = $obj->mode_reglement_code;
583  $this->mode_reglement = $obj->mode_reglement_libelle;
584  $this->cond_reglement_id = $obj->fk_cond_reglement;
585  $this->cond_reglement_code = $obj->cond_reglement_code;
586  $this->cond_reglement = $obj->cond_reglement_libelle;
587  $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
588  $this->fk_project = $obj->fk_project;
589  $this->fk_account = $obj->fk_account;
590  $this->note_private = $obj->note_private;
591  $this->note_public = $obj->note_public;
592  $this->user_author = $obj->fk_user_author;
593  $this->modelpdf = $obj->model_pdf; // deprecated
594  $this->model_pdf = $obj->model_pdf;
595  //$this->special_code = $obj->special_code;
596  $this->frequency = $obj->frequency;
597  $this->unit_frequency = $obj->unit_frequency;
598  $this->date_when = $this->db->jdate($obj->date_when);
599  $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
600  $this->nb_gen_done = $obj->nb_gen_done;
601  $this->nb_gen_max = $obj->nb_gen_max;
602  $this->usenewprice = $obj->usenewprice;
603  $this->auto_validate = $obj->auto_validate;
604  $this->generate_pdf = $obj->generate_pdf;
605 
606  // Multicurrency
607  $this->fk_multicurrency = $obj->fk_multicurrency;
608  $this->multicurrency_code = $obj->multicurrency_code;
609  $this->multicurrency_tx = $obj->multicurrency_tx;
610  $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
611  $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
612  $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
613 
614  if ($this->statut == self::STATUS_DRAFT) {
615  $this->brouillon = 1;
616  }
617 
618  // Retrieve all extrafield
619  // fetch optionals attributes and labels
620  $this->fetch_optionals();
621 
622  /*
623  * Lines
624  */
625  $result = $this->fetch_lines();
626  if ($result < 0) {
627  $this->error = $this->db->lasterror();
628  return -3;
629  }
630  return 1;
631  } else {
632  $this->error = 'Bill with id '.$rowid.' or ref '.$ref.' not found';
633  dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
634  return -2;
635  }
636  } else {
637  $this->error = $this->db->error();
638  return -1;
639  }
640  }
641 
642 
648  public function getLinesArray()
649  {
650  return $this->fetch_lines();
651  }
652 
653 
654  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
660  public function fetch_lines()
661  {
662  // phpcs:enable
663 
664  $this->lines = array();
665 
666  // Retrieve all extrafield for line
667  // fetch optionals attributes and labels
668  /*if (!is_object($extrafields)) {
669  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
670  $extrafields = new ExtraFields($this->db);
671  }
672  $extrafields->fetch_name_optionals_label($this->table_element_line, true);
673  */
674 
675  dol_syslog('FactureRec::fetch_lines', LOG_DEBUG);
676 
677  $sql = 'SELECT l.rowid, 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, ';
678  $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
679  $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,';
680  $sql .= ' l.rang, l.special_code,';
681  $sql .= ' l.fk_unit, l.fk_contract_line,';
682  $sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
683  $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
684  $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
685  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
686  $sql .= ' WHERE l.fk_facture = '.((int) $this->id);
687  $sql .= ' ORDER BY l.rang';
688 
689  $result = $this->db->query($sql);
690  if ($result) {
691  $num = $this->db->num_rows($result);
692  $i = 0;
693  while ($i < $num) {
694  $objp = $this->db->fetch_object($result);
695  $line = new FactureLigneRec($this->db);
696 
697  $line->id = $objp->rowid;
698  $line->rowid = $objp->rowid;
699  $line->desc = $objp->description; // Description line
700  $line->description = $objp->description; // Description line
701  $line->ref = $objp->product_ref; // Ref product
702  $line->product_ref = $objp->product_ref; // Ref product
703  $line->libelle = $objp->product_label; // deprecated
704  $line->product_label = $objp->product_label; // Label product
705  $line->product_desc = $objp->product_desc; // Description product
706  $line->product_type = $objp->product_type; // Type of line
707  $line->fk_product_type = $objp->fk_product_type; // Type of product
708  $line->qty = $objp->qty;
709  $line->subprice = $objp->subprice;
710 
711  $line->label = $objp->custom_label; // @deprecated
712 
713  $line->vat_src_code = $objp->vat_src_code;
714  $line->tva_tx = $objp->tva_tx;
715  $line->localtax1_tx = $objp->localtax1_tx;
716  $line->localtax2_tx = $objp->localtax2_tx;
717  $line->localtax1_type = $objp->localtax1_type;
718  $line->localtax2_type = $objp->localtax2_type;
719  $line->remise_percent = $objp->remise_percent;
720  //$line->fk_remise_except = $objp->fk_remise_except;
721  $line->fk_product = $objp->fk_product;
722  $line->date_start_fill = $objp->date_start_fill;
723  $line->date_end_fill = $objp->date_end_fill;
724  $line->info_bits = $objp->info_bits;
725  $line->total_ht = $objp->total_ht;
726  $line->total_tva = $objp->total_tva;
727  $line->total_ttc = $objp->total_ttc;
728 
729  //$line->code_ventilation = $objp->fk_code_ventilation;
730 
731  $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
732  $line->fk_fournprice = $objp->fk_product_fournisseur_price; // For backward compatibility
733 
734  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
735 
736  $line->buyprice = $marginInfos[0];
737  $line->pa_ht = $marginInfos[0]; // For backward compatibility
738  $line->marge_tx = $marginInfos[1];
739  $line->marque_tx = $marginInfos[2];
740  $line->rang = $objp->rang;
741  $line->special_code = $objp->special_code;
742  $line->fk_unit = $objp->fk_unit;
743  $line->fk_contract_line = $objp->fk_contract_line;
744 
745  // Ne plus utiliser
746  $line->price = $objp->price;
747  $line->remise = $objp->remise;
748 
749  $line->fetch_optionals();
750 
751  // Multicurrency
752  $line->fk_multicurrency = $objp->fk_multicurrency;
753  $line->multicurrency_code = $objp->multicurrency_code;
754  $line->multicurrency_subprice = $objp->multicurrency_subprice;
755  $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
756  $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
757  $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
758 
759  $this->lines[$i] = $line;
760 
761  $i++;
762  }
763 
764  $this->db->free($result);
765  return 1;
766  } else {
767  $this->error = $this->db->lasterror();
768  return -3;
769  }
770  }
771 
772 
781  public function delete(User $user, $notrigger = 0, $idwarehouse = -1)
782  {
783  $rowid = $this->id;
784 
785  dol_syslog(get_class($this)."::delete rowid=".((int) $rowid), LOG_DEBUG);
786 
787  $error = 0;
788  $this->db->begin();
789 
790  $main = MAIN_DB_PREFIX.'facturedet_rec';
791  $ef = $main."_extrafields";
792 
793  $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_facture = ".((int) $rowid).")";
794  $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = ".((int) $rowid);
795 
796  if ($this->db->query($sqlef) && $this->db->query($sql)) {
797  $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_rec WHERE rowid = ".((int) $rowid);
798  dol_syslog($sql);
799  if ($this->db->query($sql)) {
800  // Delete linked object
801  $res = $this->deleteObjectLinked();
802  if ($res < 0) {
803  $error = -3;
804  }
805  // Delete extrafields
806  $res = $this->deleteExtraFields();
807  if ($res < 0) {
808  $error = -4;
809  }
810  } else {
811  $this->error = $this->db->lasterror();
812  $error = -1;
813  }
814  } else {
815  $this->error = $this->db->lasterror();
816  $error = -2;
817  }
818  if (!$error && !$notrigger) {
819  // Call trigger
820  $result = $this->call_trigger('BILLREC_DELETE', $user);
821  if ($result < 0) {
822  $error++;
823  }
824  // End call triggers
825  }
826  if (!$error) {
827  $this->db->commit();
828  return 1;
829  } else {
830  $this->db->rollback();
831  return $error;
832  }
833  }
834 
835 
863  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 = '', $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)
864  {
865  global $mysoc;
866 
867  $facid = $this->id;
868 
869  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);
870  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
871 
872  // Check parameters
873  if ($type < 0) {
874  return -1;
875  }
876 
877  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
878 
879  // Clean vat code
880  $reg = array();
881  $vat_src_code = '';
882  if (preg_match('/\((.*)\)/', $txtva, $reg)) {
883  $vat_src_code = $reg[1];
884  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
885  }
886 
887  if ($this->brouillon) {
888  // Clean parameters
889  $remise_percent = price2num($remise_percent);
890  if (empty($remise_percent)) {
891  $remise_percent = 0;
892  }
893  $qty = price2num($qty);
894  $pu_ht = price2num($pu_ht);
895  $pu_ttc = price2num($pu_ttc);
896  if (!preg_match('/\((.*)\)/', $txtva)) {
897  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
898  }
899  $txlocaltax1 = price2num($txlocaltax1);
900  $txlocaltax2 = price2num($txlocaltax2);
901  if (empty($txtva)) {
902  $txtva = 0;
903  }
904  if (empty($txlocaltax1)) {
905  $txlocaltax1 = 0;
906  }
907  if (empty($txlocaltax2)) {
908  $txlocaltax2 = 0;
909  }
910  if (empty($info_bits)) {
911  $info_bits = 0;
912  }
913 
914  if ($price_base_type == 'HT') {
915  $pu = $pu_ht;
916  } else {
917  $pu = $pu_ttc;
918  }
919 
920  // Calcul du total TTC et de la TVA pour la ligne a partir de
921  // qty, pu, remise_percent et txtva
922  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
923  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
924 
925  $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);
926  $total_ht = $tabprice[0];
927  $total_tva = $tabprice[1];
928  $total_ttc = $tabprice[2];
929  $total_localtax1 = $tabprice[9];
930  $total_localtax2 = $tabprice[10];
931  $pu_ht = $tabprice[3];
932 
933  // MultiCurrency
934  $multicurrency_total_ht = $tabprice[16];
935  $multicurrency_total_tva = $tabprice[17];
936  $multicurrency_total_ttc = $tabprice[18];
937  $pu_ht_devise = $tabprice[19];
938 
939  $product_type = $type;
940  if ($fk_product) {
941  $product = new Product($this->db);
942  $result = $product->fetch($fk_product);
943  $product_type = $product->type;
944  }
945 
946  // Rank to use
947  $ranktouse = $rang;
948  if ($ranktouse == -1) {
949  $rangmax = $this->line_max(0);
950  $ranktouse = $rangmax + 1;
951  }
952 
953  $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (";
954  $sql .= "fk_facture";
955  $sql .= ", label";
956  $sql .= ", description";
957  $sql .= ", price";
958  $sql .= ", qty";
959  $sql .= ", tva_tx";
960  $sql .= ", vat_src_code";
961  $sql .= ", localtax1_tx";
962  $sql .= ", localtax1_type";
963  $sql .= ", localtax2_tx";
964  $sql .= ", localtax2_type";
965  $sql .= ", fk_product";
966  $sql .= ", product_type";
967  $sql .= ", remise_percent";
968  $sql .= ", subprice";
969  $sql .= ", remise";
970  $sql .= ", total_ht";
971  $sql .= ", total_tva";
972  $sql .= ", total_localtax1";
973  $sql .= ", total_localtax2";
974  $sql .= ", total_ttc";
975  $sql .= ", date_start_fill";
976  $sql .= ", date_end_fill";
977  $sql .= ", fk_product_fournisseur_price";
978  $sql .= ", buy_price_ht";
979  $sql .= ", info_bits";
980  $sql .= ", rang";
981  $sql .= ", special_code";
982  $sql .= ", fk_unit";
983  $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
984  $sql .= ") VALUES (";
985  $sql .= " ".((int) $facid);
986  $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
987  $sql .= ", '".$this->db->escape($desc)."'";
988  $sql .= ", ".price2num($pu_ht);
989  $sql .= ", ".price2num($qty);
990  $sql .= ", ".price2num($txtva);
991  $sql .= ", '".$this->db->escape($vat_src_code)."'";
992  $sql .= ", ".price2num($txlocaltax1);
993  $sql .= ", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] : '')."'";
994  $sql .= ", ".price2num($txlocaltax2);
995  $sql .= ", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] : '')."'";
996  $sql .= ", ".(!empty($fk_product) ? "'".$this->db->escape($fk_product)."'" : "null");
997  $sql .= ", ".((int) $product_type);
998  $sql .= ", ".price2num($remise_percent);
999  $sql .= ", ".price2num($pu_ht);
1000  $sql .= ", null";
1001  $sql .= ", ".price2num($total_ht);
1002  $sql .= ", ".price2num($total_tva);
1003  $sql .= ", ".price2num($total_localtax1);
1004  $sql .= ", ".price2num($total_localtax2);
1005  $sql .= ", ".price2num($total_ttc);
1006  $sql .= ", ".(int) $date_start_fill;
1007  $sql .= ", ".(int) $date_end_fill;
1008  $sql .= ", ".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1009  $sql .= ", ".($pa_ht ? price2num($pa_ht) : 0);
1010  $sql .= ", ".((int) $info_bits);
1011  $sql .= ", ".((int) $ranktouse);
1012  $sql .= ", ".((int) $special_code);
1013  $sql .= ", ".($fk_unit ? ((int) $fk_unit) : "null");
1014  $sql .= ", ".(int) $this->fk_multicurrency;
1015  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
1016  $sql .= ", ".price2num($pu_ht_devise, 'CU');
1017  $sql .= ", ".price2num($multicurrency_total_ht, 'CT');
1018  $sql .= ", ".price2num($multicurrency_total_tva, 'CT');
1019  $sql .= ", ".price2num($multicurrency_total_ttc, 'CT');
1020  $sql .= ")";
1021 
1022  dol_syslog(get_class($this)."::addline", LOG_DEBUG);
1023  if ($this->db->query($sql)) {
1024  $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX."facturedet_rec");
1025  $this->id = $facid;
1026  $this->update_price(1);
1027  return $lineId;
1028  } else {
1029  $this->error = $this->db->lasterror();
1030  return -1;
1031  }
1032  }
1033  }
1034 
1064  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 = '', $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)
1065  {
1066  global $mysoc;
1067 
1068  $facid = $this->id;
1069 
1070  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);
1071  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1072 
1073  // Clean parameters
1074  if (empty($remise_percent)) {
1075  $remise_percent = 0;
1076  }
1077 
1078  // Check parameters
1079  if ($type < 0) {
1080  return -1;
1081  }
1082 
1083  if ($this->brouillon) {
1084  // Clean parameters
1085  $remise_percent = price2num($remise_percent);
1086  $qty = price2num($qty);
1087  if (empty($info_bits)) {
1088  $info_bits = 0;
1089  }
1090  $pu_ht = price2num($pu_ht);
1091  $pu_ttc = price2num($pu_ttc);
1092  $pu_ht_devise = price2num($pu_ht_devise);
1093  if (!preg_match('/\((.*)\)/', $txtva)) {
1094  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1095  }
1096  $txlocaltax1 = price2num($txlocaltax1);
1097  $txlocaltax2 = price2num($txlocaltax2);
1098  if (empty($txlocaltax1)) {
1099  $txlocaltax1 = 0;
1100  }
1101  if (empty($txlocaltax2)) {
1102  $txlocaltax2 = 0;
1103  }
1104 
1105  if (empty($this->multicurrency_subprice)) {
1106  $this->multicurrency_subprice = 0;
1107  }
1108  if (empty($this->multicurrency_total_ht)) {
1109  $this->multicurrency_total_ht = 0;
1110  }
1111  if (empty($this->multicurrency_total_tva)) {
1112  $this->multicurrency_total_tva = 0;
1113  }
1114  if (empty($this->multicurrency_total_ttc)) {
1115  $this->multicurrency_total_ttc = 0;
1116  }
1117 
1118  if ($price_base_type == 'HT') {
1119  $pu = $pu_ht;
1120  } else {
1121  $pu = $pu_ttc;
1122  }
1123 
1124  // Calculate total with, without tax and tax from qty, pu, remise_percent and txtva
1125  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1126  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1127 
1128  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
1129 
1130  // Clean vat code
1131  $vat_src_code = '';
1132  $reg = array();
1133  if (preg_match('/\((.*)\)/', $txtva, $reg)) {
1134  $vat_src_code = $reg[1];
1135  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
1136  }
1137 
1138  $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);
1139 
1140  $total_ht = $tabprice[0];
1141  $total_tva = $tabprice[1];
1142  $total_ttc = $tabprice[2];
1143  $total_localtax1 = $tabprice[9];
1144  $total_localtax2 = $tabprice[10];
1145  $pu_ht = $tabprice[3];
1146  $pu_tva = $tabprice[4];
1147  $pu_ttc = $tabprice[5];
1148 
1149  // MultiCurrency
1150  $multicurrency_total_ht = $tabprice[16];
1151  $multicurrency_total_tva = $tabprice[17];
1152  $multicurrency_total_ttc = $tabprice[18];
1153  $pu_ht_devise = $tabprice[19];
1154 
1155  $product_type = $type;
1156  if ($fk_product) {
1157  $product = new Product($this->db);
1158  $result = $product->fetch($fk_product);
1159  $product_type = $product->type;
1160  }
1161 
1162  $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET ";
1163  $sql .= "fk_facture = ".((int) $facid);
1164  $sql .= ", label=".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1165  $sql .= ", description='".$this->db->escape($desc)."'";
1166  $sql .= ", price=".price2num($pu_ht);
1167  $sql .= ", qty=".price2num($qty);
1168  $sql .= ", tva_tx=".price2num($txtva);
1169  $sql .= ", vat_src_code='".$this->db->escape($vat_src_code)."'";
1170  $sql .= ", localtax1_tx=".((float) $txlocaltax1);
1171  $sql .= ", localtax1_type='".$this->db->escape($localtaxes_type[0])."'";
1172  $sql .= ", localtax2_tx=".((float) $txlocaltax2);
1173  $sql .= ", localtax2_type='".$this->db->escape($localtaxes_type[2])."'";
1174  $sql .= ", fk_product=".(!empty($fk_product) ? "'".$this->db->escape($fk_product)."'" : "null");
1175  $sql .= ", product_type=".((int) $product_type);
1176  $sql .= ", remise_percent='".price2num($remise_percent)."'";
1177  $sql .= ", subprice='".price2num($pu_ht)."'";
1178  $sql .= ", total_ht='".price2num($total_ht)."'";
1179  $sql .= ", total_tva='".price2num($total_tva)."'";
1180  $sql .= ", total_localtax1='".price2num($total_localtax1)."'";
1181  $sql .= ", total_localtax2='".price2num($total_localtax2)."'";
1182  $sql .= ", total_ttc='".price2num($total_ttc)."'";
1183  $sql .= ", date_start_fill=".((int) $date_start_fill);
1184  $sql .= ", date_end_fill=".((int) $date_end_fill);
1185  $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1186  $sql .= ", buy_price_ht=".($pa_ht ? price2num($pa_ht) : 0);
1187  $sql .= ", info_bits=".((int) $info_bits);
1188  $sql .= ", rang=".((int) $rang);
1189  $sql .= ", special_code=".((int) $special_code);
1190  $sql .= ", fk_unit=".($fk_unit ? "'".$this->db->escape($fk_unit)."'" : "null");
1191  $sql .= ', multicurrency_subprice = '.price2num($pu_ht_devise);
1192  $sql .= ', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1193  $sql .= ', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1194  $sql .= ', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1195  $sql .= " WHERE rowid = ".((int) $rowid);
1196 
1197  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1198  if ($this->db->query($sql)) {
1199  $this->id = $facid;
1200  $this->update_price(1);
1201  return 1;
1202  } else {
1203  $this->error = $this->db->lasterror();
1204  return -1;
1205  }
1206  }
1207  }
1208 
1209 
1215  public function getNextDate()
1216  {
1217  if (empty($this->date_when)) {
1218  return false;
1219  }
1220  return dol_time_plus_duree($this->date_when, $this->frequency, $this->unit_frequency);
1221  }
1222 
1228  public function isMaxNbGenReached()
1229  {
1230  $ret = false;
1231  if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1232  $ret = true;
1233  }
1234  return $ret;
1235  }
1236 
1243  public function strikeIfMaxNbGenReached($ret)
1244  {
1245  // Special case to strike the date
1246  return ($this->isMaxNbGenReached() ? '<strike>' : '').$ret.($this->isMaxNbGenReached() ? '</strike>' : '');
1247  }
1248 
1260  public function createRecurringInvoices($restrictioninvoiceid = 0, $forcevalidation = 0, $notrigger = 0)
1261  {
1262  global $conf, $langs, $db, $user, $hookmanager;
1263 
1264  $error = 0;
1265  $nb_create = 0;
1266 
1267  // Load translation files required by the page
1268  $langs->loadLangs(array("main", "bills"));
1269 
1270  $now = dol_now();
1271  $tmparray = dol_getdate($now);
1272  $today = dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year']); // Today is last second of current day
1273 
1274  $this->output = null;
1275 
1276  dol_syslog("createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid." forcevalidation=".$forcevalidation);
1277 
1278  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_rec';
1279  $sql .= ' WHERE frequency > 0'; // A recurring invoice is an invoice with a frequency
1280  $sql .= " AND (date_when IS NULL OR date_when <= '".$this->db->idate($today)."')";
1281  $sql .= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1282  $sql .= ' AND suspended = 0';
1283  $sql .= ' AND entity = '.$conf->entity; // MUST STAY = $conf->entity here
1284  if ($restrictioninvoiceid > 0) {
1285  $sql .= ' AND rowid = '.((int) $restrictioninvoiceid);
1286  }
1287  $sql .= $this->db->order('entity', 'ASC');
1288  //print $sql;exit;
1289  $parameters = array(
1290  'restrictioninvoiceid' => $restrictioninvoiceid,
1291  'forcevalidation' => $forcevalidation,
1292  );
1293  $reshook = $hookmanager->executeHooks('beforeCreationOfRecurringInvoices', $parameters, $sql); // note that $sql might be modified by hooks
1294 
1295  $resql = $this->db->query($sql);
1296  if ($resql) {
1297  $i = 0;
1298  $num = $this->db->num_rows($resql);
1299 
1300  if ($num) {
1301  $this->output .= $langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n";
1302  } else {
1303  $this->output .= $langs->trans("NoQualifiedRecurringInvoiceTemplateFound");
1304  }
1305 
1306  $saventity = $conf->entity;
1307 
1308  while ($i < $num) { // Loop on each template invoice. If $num = 0, test is false at first pass.
1309  $line = $this->db->fetch_object($resql);
1310 
1311  $this->db->begin();
1312 
1313  $invoiceidgenerated = 0;
1314 
1315  $facture = null;
1316  $facturerec = new FactureRec($this->db);
1317  $facturerec->fetch($line->rowid);
1318 
1319  if ($facturerec->id > 0) {
1320  // Set entity context
1321  $conf->entity = $facturerec->entity;
1322 
1323  dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity);
1324 
1325  $facture = new Facture($this->db);
1326  $facture->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice
1327  $facture->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice
1328 
1329  $facture->type = self::TYPE_STANDARD;
1330  $facture->brouillon = 1;
1331  $facture->statut = self::STATUS_DRAFT;
1332  $facture->status = self::STATUS_DRAFT;
1333  $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.
1334  $facture->socid = $facturerec->socid;
1335  if (!empty($facturerec->fk_multicurrency)) {
1336  $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1337  $facture->multicurrency_code = $facturerec->multicurrency_code;
1338  $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1339  }
1340 
1341  $invoiceidgenerated = $facture->create($user);
1342  if ($invoiceidgenerated <= 0) {
1343  $this->errors = $facture->errors;
1344  $this->error = $facture->error;
1345  $error++;
1346  }
1347 
1348 
1349  if (!$error && ($facturerec->auto_validate || $forcevalidation)) {
1350  $result = $facture->validate($user);
1351  if ($result <= 0) {
1352  $this->errors = $facture->errors;
1353  $this->error = $facture->error;
1354  $error++;
1355  }
1356  }
1357  if (!$error && $facturerec->generate_pdf) {
1358  // We refresh the object in order to have all necessary data (like date_lim_reglement)
1359  $facture->fetch($facture->id);
1360  $result = $facture->generateDocument($facturerec->model_pdf, $langs);
1361  if ($result <= 0) {
1362  $this->errors = $facture->errors;
1363  $this->error = $facture->error;
1364  $error++;
1365  }
1366  }
1367  if (!$error && !$notrigger) {
1368  // Call trigger
1369  $result = $facturerec->call_trigger('BILLREC_CREATEBILL', $user);
1370  if ($result < 0) {
1371  $this->errors = $facturerec->errors;
1372  $this->error = $facturerec->error;
1373  $error++;
1374  }
1375  // End call triggers
1376  }
1377  } else {
1378  $error++;
1379  $this->error = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity."\n";
1380  $this->errors[] = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity;
1381  dol_syslog("createRecurringInvoices Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity);
1382  }
1383 
1384  if (!$error && $invoiceidgenerated >= 0) {
1385  $this->db->commit("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1386  dol_syslog("createRecurringInvoices Process invoice template ".$facturerec->ref." is finished with a success generation");
1387  $nb_create++;
1388  $this->output .= $langs->trans("InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref)."\n";
1389  } else {
1390  $this->db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1391  }
1392 
1393  $parameters = array(
1394  'cpt' => $i,
1395  'total' => $num,
1396  'errorCount' => $error,
1397  'invoiceidgenerated' => $invoiceidgenerated,
1398  'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks.
1399  'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks.
1400  );
1401  $reshook = $hookmanager->executeHooks('afterCreationOfRecurringInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null)
1402 
1403  $i++;
1404  }
1405 
1406  $conf->entity = $saventity; // Restore entity context
1407  } else {
1408  dol_print_error($this->db);
1409  }
1410 
1411  $this->output = trim($this->output);
1412 
1413  return $error ? $error : 0;
1414  }
1415 
1428  public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = '', $save_lastsearch_value = -1)
1429  {
1430  global $langs, $hookmanager;
1431 
1432  $result = '';
1433 
1434  $label = '<u>'.$langs->trans("RepeatableInvoice").'</u>';
1435  if (!empty($this->ref)) {
1436  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1437  }
1438  if ($this->frequency > 0) {
1439  $label .= '<br><b>'.$langs->trans('Frequency').':</b> '.$langs->trans('FrequencyPer_'.$this->unit_frequency, $this->frequency);
1440  }
1441  if (!empty($this->date_last_gen)) {
1442  $label .= '<br><b>'.$langs->trans('DateLastGeneration').':</b> '.dol_print_date($this->date_last_gen, 'dayhour');
1443  }
1444  if ($this->frequency > 0) {
1445  if (!empty($this->date_when)) {
1446  $label .= '<br><b>'.$langs->trans('NextDateToExecution').':</b> ';
1447  $label .= (empty($this->suspended) ? '' : '<strike>').dol_print_date($this->date_when, 'day').(empty($this->suspended) ? '' : '</strike>'); // No hour for this property
1448  if (!empty($this->suspended)) {
1449  $label .= ' ('.$langs->trans("Disabled").')';
1450  }
1451  }
1452  }
1453 
1454  $url = DOL_URL_ROOT.'/compta/facture/card-rec.php?facid='.$this->id;
1455 
1456  if ($short) {
1457  return $url;
1458  }
1459 
1460  if ($option != 'nolink') {
1461  // Add param to save lastsearch_values or not
1462  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1463  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1464  $add_save_lastsearch_values = 1;
1465  }
1466  if ($add_save_lastsearch_values) {
1467  $url .= '&save_lastsearch_values=1';
1468  }
1469  }
1470 
1471  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1472  $linkend = '</a>';
1473 
1474  $result .= $linkstart;
1475  if ($withpicto) {
1476  $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);
1477  }
1478  if ($withpicto != 2) {
1479  $result .= $this->ref;
1480  }
1481  $result .= $linkend;
1482  global $action;
1483  $hookmanager->initHooks(array($this->element . 'dao'));
1484  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1485  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1486  if ($reshook > 0) {
1487  $result = $hookmanager->resPrint;
1488  } else {
1489  $result .= $hookmanager->resPrint;
1490  }
1491  return $result;
1492  }
1493 
1501  public function getLibStatut($mode = 0, $alreadypaid = -1)
1502  {
1503  return $this->LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->type) ? 0 : $this->type);
1504  }
1505 
1506  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1517  public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0)
1518  {
1519  // phpcs:enable
1520  global $langs;
1521  $langs->load('bills');
1522 
1523  $labelStatus = $langs->transnoentitiesnoconv('Active');
1524  $statusType = 'status0';
1525 
1526  //print "$recur,$status,$mode,$alreadypaid,$type";
1527  if ($mode == 0) {
1528  if ($recur) {
1529  if ($status == self::STATUS_SUSPENDED) {
1530  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1531  } else {
1532  $labelStatus = $langs->transnoentitiesnoconv('Active');
1533  }
1534  } else {
1535  if ($status == self::STATUS_SUSPENDED) {
1536  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1537  } else {
1538  $labelStatus = $langs->transnoentitiesnoconv("Draft");
1539  }
1540  }
1541  } elseif ($mode == 1) {
1542  $prefix = 'Short';
1543  if ($recur) {
1544  if ($status == self::STATUS_SUSPENDED) {
1545  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1546  } else {
1547  $labelStatus = $langs->transnoentitiesnoconv('Active');
1548  }
1549  } else {
1550  if ($status == self::STATUS_SUSPENDED) {
1551  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1552  } else {
1553  $labelStatus = $langs->transnoentitiesnoconv("Draft");
1554  }
1555  }
1556  } elseif ($mode == 2) {
1557  if ($recur) {
1558  if ($status == self::STATUS_SUSPENDED) {
1559  $statusType = 'status6';
1560  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1561  } else {
1562  $statusType = 'status4';
1563  $labelStatus = $langs->transnoentitiesnoconv('Active');
1564  }
1565  } else {
1566  if ($status == self::STATUS_SUSPENDED) {
1567  $statusType = 'status6';
1568  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1569  } else {
1570  $statusType = 'status0';
1571  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1572  }
1573  }
1574  } elseif ($mode == 3) {
1575  if ($recur) {
1576  $prefix = 'Short';
1577  if ($status == self::STATUS_SUSPENDED) {
1578  $statusType = 'status6';
1579  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1580  } else {
1581  $statusType = 'status4';
1582  $labelStatus = $langs->transnoentitiesnoconv('Active');
1583  }
1584  } else {
1585  if ($status == self::STATUS_SUSPENDED) {
1586  $statusType = 'status6';
1587  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1588  } else {
1589  $statusType = 'status0';
1590  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1591  }
1592  }
1593  } elseif ($mode == 4) {
1594  $prefix = '';
1595  if ($recur) {
1596  if ($status == self::STATUS_SUSPENDED) {
1597  $statusType = 'status6';
1598  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1599  } else {
1600  $statusType = 'status4';
1601  $labelStatus = $langs->transnoentitiesnoconv('Active');
1602  }
1603  } else {
1604  if ($status == self::STATUS_SUSPENDED) {
1605  $statusType = 'status6';
1606  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1607  } else {
1608  $statusType = 'status0';
1609  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1610  }
1611  }
1612  } elseif ($mode == 5 || $mode == 6) {
1613  $prefix = '';
1614  if ($mode == 5) {
1615  $prefix = 'Short';
1616  }
1617  if ($recur) {
1618  if ($status == self::STATUS_SUSPENDED) {
1619  $statusType = 'status6';
1620  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1621  } else {
1622  $statusType = 'status4';
1623  $labelStatus = $langs->transnoentitiesnoconv('Active');
1624  }
1625  } else {
1626  if ($status == self::STATUS_SUSPENDED) {
1627  $statusType = 'status6';
1628  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1629  } else {
1630  $statusType = 'status0';
1631  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1632  }
1633  }
1634  }
1635 
1636  $labelStatusShort = $labelStatus;
1637 
1638  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1639  }
1640 
1649  public function initAsSpecimen($option = '')
1650  {
1651  global $user, $langs, $conf;
1652 
1653  $now = dol_now();
1654  $arraynow = dol_getdate($now);
1655  $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1656 
1657  // Load array of products prodids
1658  $num_prods = 0;
1659  $prodids = array();
1660 
1661  $sql = "SELECT rowid";
1662  $sql .= " FROM ".MAIN_DB_PREFIX."product";
1663  $sql .= " WHERE entity IN (".getEntity('product').")";
1664  $sql .= $this->db->plimit(100);
1665 
1666  $resql = $this->db->query($sql);
1667  if ($resql) {
1668  $num_prods = $this->db->num_rows($resql);
1669  $i = 0;
1670  while ($i < $num_prods) {
1671  $i++;
1672  $row = $this->db->fetch_row($resql);
1673  $prodids[$i] = $row[0];
1674  }
1675  }
1676 
1677  // Initialize parameters
1678  $this->id = 0;
1679  $this->ref = 'SPECIMEN';
1680  $this->title = 'SPECIMEN';
1681  $this->specimen = 1;
1682  $this->socid = 1;
1683  $this->date = $nownotime;
1684  $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
1685  $this->cond_reglement_id = 1;
1686  $this->cond_reglement_code = 'RECEP';
1687  $this->date_lim_reglement = $this->calculate_date_lim_reglement();
1688  $this->mode_reglement_id = 0; // Not forced to show payment mode CHQ + VIR
1689  $this->mode_reglement_code = ''; // Not forced to show payment mode CHQ + VIR
1690  $this->note_public = 'This is a comment (public)';
1691  $this->note_private = 'This is a comment (private)';
1692  $this->note = 'This is a comment (private)';
1693  $this->fk_incoterms = 0;
1694  $this->location_incoterms = '';
1695 
1696  if (empty($option) || $option != 'nolines') {
1697  // Lines
1698  $nbp = 5;
1699  $xnbp = 0;
1700  while ($xnbp < $nbp) {
1701  $line = new FactureLigne($this->db);
1702  $line->desc = $langs->trans("Description")." ".$xnbp;
1703  $line->qty = 1;
1704  $line->subprice = 100;
1705  $line->tva_tx = 19.6;
1706  $line->localtax1_tx = 0;
1707  $line->localtax2_tx = 0;
1708  $line->remise_percent = 0;
1709  if ($xnbp == 1) { // Qty is negative (product line)
1710  $prodid = mt_rand(1, $num_prods);
1711  $line->fk_product = $prodids[$prodid];
1712  $line->qty = -1;
1713  $line->total_ht = -100;
1714  $line->total_ttc = -119.6;
1715  $line->total_tva = -19.6;
1716  } elseif ($xnbp == 2) { // UP is negative (free line)
1717  $line->subprice = -100;
1718  $line->total_ht = -100;
1719  $line->total_ttc = -119.6;
1720  $line->total_tva = -19.6;
1721  $line->remise_percent = 0;
1722  } elseif ($xnbp == 3) { // Discount is 50% (product line)
1723  $prodid = mt_rand(1, $num_prods);
1724  $line->fk_product = $prodids[$prodid];
1725  $line->total_ht = 50;
1726  $line->total_ttc = 59.8;
1727  $line->total_tva = 9.8;
1728  $line->remise_percent = 50;
1729  } else // (product line)
1730  {
1731  $prodid = mt_rand(1, $num_prods);
1732  $line->fk_product = $prodids[$prodid];
1733  $line->total_ht = 100;
1734  $line->total_ttc = 119.6;
1735  $line->total_tva = 19.6;
1736  $line->remise_percent = 00;
1737  }
1738 
1739  $this->lines[$xnbp] = $line;
1740  $xnbp++;
1741 
1742  $this->total_ht += $line->total_ht;
1743  $this->total_tva += $line->total_tva;
1744  $this->total_ttc += $line->total_ttc;
1745  }
1746  $this->revenuestamp = 0;
1747 
1748  // Add a line "offered"
1749  $line = new FactureLigne($this->db);
1750  $line->desc = $langs->trans("Description")." (offered line)";
1751  $line->qty = 1;
1752  $line->subprice = 100;
1753  $line->tva_tx = 19.6;
1754  $line->localtax1_tx = 0;
1755  $line->localtax2_tx = 0;
1756  $line->remise_percent = 100;
1757  $line->total_ht = 0;
1758  $line->total_ttc = 0; // 90 * 1.196
1759  $line->total_tva = 0;
1760  $prodid = mt_rand(1, $num_prods);
1761  $line->fk_product = $prodids[$prodid];
1762 
1763  $this->lines[$xnbp] = $line;
1764  $xnbp++;
1765  }
1766 
1767  $this->usenewprice = 0;
1768  }
1769 
1778  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1779  {
1780  $tables = array(
1781  'facture_rec'
1782  );
1783 
1784  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1785  }
1786 
1795  public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
1796  {
1797  $tables = array(
1798  'facturedet_rec'
1799  );
1800 
1801  return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
1802  }
1803 
1812  public function setFrequencyAndUnit($frequency, $unit, $notrigger = 0)
1813  {
1814  global $user;
1815 
1816  if (!$this->table_element) {
1817  dol_syslog(get_class($this)."::setFrequencyAndUnit was called on objet with property table_element not defined", LOG_ERR);
1818  return -1;
1819  }
1820 
1821  if (!empty($frequency) && empty($unit)) {
1822  dol_syslog(get_class($this)."::setFrequencyAndUnit was called on objet with params frequency defined but unit not defined", LOG_ERR);
1823  return -2;
1824  }
1825 
1826  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1827  $sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null');
1828  if (!empty($unit)) {
1829  $sql .= ', unit_frequency = \''.$this->db->escape($unit).'\'';
1830  }
1831  $sql .= " WHERE rowid = ".((int) $this->id);
1832 
1833  dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG);
1834  if ($this->db->query($sql)) {
1835  $this->frequency = $frequency;
1836  if (!empty($unit)) {
1837  $this->unit_frequency = $unit;
1838  }
1839 
1840  if (!$notrigger) {
1841  // Call trigger
1842  $result = $this->call_trigger('BILLREC_MODIFY', $user);
1843  if ($result < 0) {
1844  return $result;
1845  }
1846  // End call triggers
1847  }
1848 
1849  return 1;
1850  } else {
1851  dol_print_error($this->db);
1852  return -1;
1853  }
1854  }
1855 
1864  public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
1865  {
1866 
1867  global $user;
1868 
1869  if (!$this->table_element) {
1870  dol_syslog(get_class($this)."::setNextDate was called on objet with property table_element not defined", LOG_ERR);
1871  return -1;
1872  }
1873  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1874  $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
1875  if ($increment_nb_gen_done > 0) {
1876  $sql .= ', nb_gen_done = nb_gen_done + 1';
1877  }
1878  $sql .= " WHERE rowid = ".((int) $this->id);
1879 
1880  dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG);
1881  if ($this->db->query($sql)) {
1882  $this->date_when = $date;
1883  if ($increment_nb_gen_done > 0) {
1884  $this->nb_gen_done++;
1885  }
1886 
1887  if (!$notrigger) {
1888  // Call trigger
1889  $result = $this->call_trigger('BILLREC_MODIFY', $user);
1890  if ($result < 0) {
1891  return $result;
1892  }
1893  // End call triggers
1894  }
1895  return 1;
1896  } else {
1897  dol_print_error($this->db);
1898  return -1;
1899  }
1900  }
1901 
1909  public function setMaxPeriod($nb, $notrigger = 0)
1910  {
1911 
1912  global $user;
1913 
1914  if (!$this->table_element) {
1915  dol_syslog(get_class($this)."::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR);
1916  return -1;
1917  }
1918 
1919  if (empty($nb)) {
1920  $nb = 0;
1921  }
1922 
1923  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1924  $sql .= ' SET nb_gen_max = '.((int) $nb);
1925  $sql .= " WHERE rowid = ".((int) $this->id);
1926 
1927  dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG);
1928  if ($this->db->query($sql)) {
1929  $this->nb_gen_max = $nb;
1930 
1931  if (!$notrigger) {
1932  // Call trigger
1933  $result = $this->call_trigger('BILLREC_MODIFY', $user);
1934  if ($result < 0) {
1935  return $result;
1936  }
1937  // End call triggers
1938  }
1939 
1940  return 1;
1941  } else {
1942  dol_print_error($this->db);
1943  return -1;
1944  }
1945  }
1946 
1954  public function setAutoValidate($validate, $notrigger = 0)
1955  {
1956  global $user;
1957 
1958  if (!$this->table_element) {
1959  dol_syslog(get_class($this)."::setAutoValidate was called on objet with property table_element not defined", LOG_ERR);
1960  return -1;
1961  }
1962 
1963  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1964  $sql .= ' SET auto_validate = '.((int) $validate);
1965  $sql .= " WHERE rowid = ".((int) $this->id);
1966 
1967  dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
1968  if ($this->db->query($sql)) {
1969  $this->auto_validate = $validate;
1970 
1971  if (!$notrigger) {
1972  // Call trigger
1973  $result = $this->call_trigger('BILLREC_MODIFY', $user);
1974  if ($result < 0) {
1975  return $result;
1976  }
1977  // End call triggers
1978  }
1979 
1980  return 1;
1981  } else {
1982  dol_print_error($this->db);
1983  return -1;
1984  }
1985  }
1986 
1994  public function setGeneratePdf($validate, $notrigger = 0)
1995  {
1996  global $user;
1997 
1998  if (!$this->table_element) {
1999  dol_syslog(get_class($this)."::setGeneratePdf was called on objet with property table_element not defined", LOG_ERR);
2000  return -1;
2001  }
2002 
2003  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2004  $sql .= ' SET generate_pdf = '.((int) $validate);
2005  $sql .= " WHERE rowid = ".((int) $this->id);
2006 
2007  dol_syslog(get_class($this)."::setGeneratePdf", LOG_DEBUG);
2008  if ($this->db->query($sql)) {
2009  $this->generate_pdf = $validate;
2010 
2011  if (!$notrigger) {
2012  // Call trigger
2013  $result = $this->call_trigger('BILLREC_MODIFY', $user);
2014  if ($result < 0) {
2015  return $result;
2016  }
2017  // End call triggers
2018  }
2019 
2020  return 1;
2021  } else {
2022  dol_print_error($this->db);
2023  return -1;
2024  }
2025  }
2026 
2034  public function setModelPdf($model, $notrigger = 0)
2035  {
2036  global $user;
2037  if (!$this->table_element) {
2038  dol_syslog(get_class($this)."::setModelPdf was called on objet with property table_element not defined", LOG_ERR);
2039  return -1;
2040  }
2041 
2042  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2043  $sql .= " SET modelpdf = '".$this->db->escape($model)."'";
2044  $sql .= " WHERE rowid = ".((int) $this->id);
2045 
2046  dol_syslog(get_class($this)."::setModelPdf", LOG_DEBUG);
2047  if ($this->db->query($sql)) {
2048  $this->model_pdf = $model;
2049 
2050  if (!$notrigger) {
2051  // Call trigger
2052  $result = $this->call_trigger('BILLREC_MODIFY', $user);
2053  if ($result < 0) {
2054  return $result;
2055  }
2056  // End call triggers
2057  }
2058 
2059  return 1;
2060  } else {
2061  dol_print_error($this->db);
2062  return -1;
2063  }
2064  }
2065 }
2066 
2067 
2068 
2074 {
2078  public $element = 'facturedetrec';
2079 
2083  public $table_element = 'facturedet_rec';
2084 
2085 
2086  public $fk_product_fournisseur_price;
2087  public $fk_fournprice; // For backward compatibility
2088 
2089  public $rang;
2090 
2091  public $desc;
2092  public $description;
2093 
2094  public $fk_product_type; // Use instead product_type
2095 
2096  public $fk_contract_line;
2097 
2098 
2106  public function delete(User $user, $notrigger = false)
2107  {
2108  $error = 0;
2109 
2110  $this->db->begin();
2111 
2112  if (!$error) {
2113  if (!$notrigger) {
2114  // Call triggers
2115  $result = $this->call_trigger('LINEBILLREC_DELETE', $user);
2116  if ($result < 0) {
2117  $error++;
2118  } // Do also here what you must do to rollback action if trigger fail
2119  // End call triggers
2120  }
2121  }
2122 
2123  if (!$error) {
2124  $result = $this->deleteExtraFields();
2125  if ($result < 0) {
2126  $error++;
2127  }
2128  }
2129 
2130  if (!$error) {
2131  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.((int) $this->id);
2132 
2133  $res = $this->db->query($sql);
2134  if (!$res) {
2135  $error++;
2136  $this->errors[] = $this->db->lasterror();
2137  }
2138  }
2139 
2140  // Commit or rollback
2141  if ($error) {
2142  $this->db->rollback();
2143  return -1;
2144  } else {
2145  $this->db->commit();
2146  return 1;
2147  }
2148  }
2149 
2150 
2157  public function fetch($rowid)
2158  {
2159  $sql = 'SELECT l.rowid, l.fk_facture ,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,';
2160  $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2161  $sql .= ' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2162  $sql .= ' l.rang, l.special_code,';
2163  $sql .= ' l.fk_unit, l.fk_contract_line,';
2164  $sql .= ' l.import_key, l.fk_multicurrency,';
2165  $sql .= ' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2166  $sql .= ' l.buy_price_ht, l.fk_product_fournisseur_price,';
2167  $sql .= ' l.fk_user_author, l.fk_user_modif,';
2168  $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2169  $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
2170  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
2171  $sql .= ' WHERE l.rowid = '.((int) $rowid);
2172  $sql .= ' ORDER BY l.rang';
2173 
2174  dol_syslog('FactureRec::fetch', LOG_DEBUG);
2175  $result = $this->db->query($sql);
2176  if ($result) {
2177  $objp = $this->db->fetch_object($result);
2178 
2179  $this->id = $objp->rowid;
2180  $this->label = $objp->custom_label; // Label line
2181  $this->desc = $objp->description; // Description line
2182  $this->description = $objp->description; // Description line
2183  $this->product_type = $objp->product_type; // Type of line
2184  $this->ref = $objp->product_ref; // Ref product
2185  $this->product_ref = $objp->product_ref; // Ref product
2186  $this->libelle = $objp->product_label; // deprecated
2187  $this->product_label = $objp->product_label; // Label product
2188  $this->product_desc = $objp->product_desc; // Description product
2189  $this->fk_product_type = $objp->fk_product_type; // Type of product
2190  $this->qty = $objp->qty;
2191  $this->price = $objp->price;
2192  $this->subprice = $objp->subprice;
2193  $this->fk_facture = $objp->fk_facture;
2194  $this->vat_src_code = $objp->vat_src_code;
2195  $this->tva_tx = $objp->tva_tx;
2196  $this->localtax1_tx = $objp->localtax1_tx;
2197  $this->localtax2_tx = $objp->localtax2_tx;
2198  $this->localtax1_type = $objp->localtax1_type;
2199  $this->localtax2_type = $objp->localtax2_type;
2200  $this->remise_percent = $objp->remise_percent;
2201  //$this->fk_remise_except = $objp->fk_remise_except;
2202  $this->fk_product = $objp->fk_product;
2203  $this->date_start_fill = $objp->date_start_fill;
2204  $this->date_end_fill = $objp->date_end_fill;
2205  $this->info_bits = $objp->info_bits;
2206  $this->total_ht = $objp->total_ht;
2207  $this->total_tva = $objp->total_tva;
2208  $this->total_ttc = $objp->total_ttc;
2209  //$this->code_ventilation = $objp->fk_code_ventilation;
2210  $this->rang = $objp->rang;
2211  $this->special_code = $objp->special_code;
2212  $this->fk_unit = $objp->fk_unit;
2213  $this->fk_contract_line = $objp->fk_contract_line;
2214  $this->import_key = $objp->import_key;
2215  $this->fk_multicurrency = $objp->fk_multicurrency;
2216  $this->multicurrency_code = $objp->multicurrency_code;
2217  $this->multicurrency_subprice = $objp->multicurrency_subprice;
2218  $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2219  $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2220  $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2221 
2222  $this->buy_price_ht = $objp->buy_price_ht;
2223 
2224  $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2225  $this->fk_user_author = $objp->fk_user_author;
2226  $this->fk_user_modif = $objp->fk_user_modif;
2227 
2228  $this->db->free($result);
2229  return 1;
2230  } else {
2231  $this->error = $this->db->lasterror();
2232  return -3;
2233  }
2234  }
2235 
2236 
2244  public function update(User $user, $notrigger = 0)
2245  {
2246  global $conf;
2247 
2248  $error = 0;
2249 
2250  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2251 
2252  $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET";
2253  $sql .= " fk_facture = ".((int) $this->fk_facture);
2254  $sql .= ", label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
2255  $sql .= ", description='".$this->db->escape($this->desc)."'";
2256  $sql .= ", price=".price2num($this->price);
2257  $sql .= ", qty=".price2num($this->qty);
2258  $sql .= ", tva_tx=".price2num($this->tva_tx);
2259  $sql .= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'";
2260  $sql .= ", localtax1_tx=".price2num($this->localtax1_tx);
2261  $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
2262  $sql .= ", localtax2_tx=".price2num($this->localtax2_tx);
2263  $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
2264  $sql .= ", fk_product=".($this->fk_product > 0 ? $this->fk_product : "null");
2265  $sql .= ", product_type=".((int) $this->product_type);
2266  $sql .= ", remise_percent=".price2num($this->remise_percent);
2267  $sql .= ", subprice=".price2num($this->subprice);
2268  $sql .= ", info_bits=".price2num($this->info_bits);
2269  $sql .= ", date_start_fill=".(int) $this->date_start_fill;
2270  $sql .= ", date_end_fill=".(int) $this->date_end_fill;
2271  if (empty($this->skip_update_total)) {
2272  $sql .= ", total_ht=".price2num($this->total_ht);
2273  $sql .= ", total_tva=".price2num($this->total_tva);
2274  $sql .= ", total_localtax1=".price2num($this->total_localtax1);
2275  $sql .= ", total_localtax2=".price2num($this->total_localtax2);
2276  $sql .= ", total_ttc=".price2num($this->total_ttc);
2277  }
2278  $sql .= ", rang=".((int) $this->rang);
2279  $sql .= ", special_code=".((int) $this->special_code);
2280  $sql .= ", fk_unit=".($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : "null");
2281  $sql .= ", fk_contract_line=".($this->fk_contract_line ? $this->fk_contract_line : "null");
2282  $sql .= " WHERE rowid = ".((int) $this->id);
2283 
2284  $this->db->begin();
2285 
2286  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
2287 
2288  $resql = $this->db->query($sql);
2289  if ($resql) {
2290  if (!$error) {
2291  $result = $this->insertExtraFields();
2292  if ($result < 0) {
2293  $error++;
2294  }
2295  }
2296 
2297  if (!$error && !$notrigger) {
2298  // Call trigger
2299  $result = $this->call_trigger('LINEBILLREC_MODIFY', $user);
2300  if ($result < 0) {
2301  $error++;
2302  }
2303  // End call triggers
2304  }
2305 
2306  if ($error) {
2307  $this->db->rollback();
2308  return -2;
2309  } else {
2310  $this->db->commit();
2311  return 1;
2312  }
2313  } else {
2314  $this->error = $this->db->lasterror();
2315  $this->db->rollback();
2316  return -2;
2317  }
2318  }
2319 }
FactureRec\isMaxNbGenReached
isMaxNbGenReached()
Return if maximum number of generation is reached.
Definition: facture-rec.class.php:1228
dol_getdate
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
Definition: functions.lib.php:2820
getLocalTaxesFromRate
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
Definition: functions.lib.php:6264
FactureRec\updateline
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='', $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)
Update a line to invoice.
Definition: facture-rec.class.php:1064
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1493
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
FactureRec\update
update(User $user, $notrigger=0)
Update a line invoice_rec.
Definition: facture-rec.class.php:470
FactureRec\setGeneratePdf
setGeneratePdf($validate, $notrigger=0)
Update the auto generate documents.
Definition: facture-rec.class.php:1994
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
FactureRec\createRecurringInvoices
createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0, $notrigger=0)
Create all recurrents invoices (for all entities if multicompany is used).
Definition: facture-rec.class.php:1260
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4993
FactureRec\setAutoValidate
setAutoValidate($validate, $notrigger=0)
Update the auto validate flag of invoice.
Definition: facture-rec.class.php:1954
CommonInvoice\TYPE_STANDARD
const TYPE_STANDARD
Standard invoice.
Definition: commoninvoice.class.php:39
ref
$object ref
Definition: info.php:78
FactureRec\create
create($user, $facid, $notrigger=0)
Create a predefined invoice.
Definition: facture-rec.class.php:237
FactureRec
Class to manage invoice templates.
Definition: facture-rec.class.php:40
CommonObject\deleteObjectLinked
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
Definition: commonobject.class.php:4244
CommonObject\commonReplaceProduct
static commonReplaceProduct(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
Definition: commonobject.class.php:8528
FactureLigneRec
Class to manage invoice lines of templates.
Definition: facture-rec.class.php:2073
Facture
Class to manage invoices.
Definition: facture.class.php:60
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5823
FactureRec\$total
$total
Definition: facture-rec.class.php:94
FactureRec\fetch
fetch($rowid, $ref='', $ref_ext='')
Load object and lines.
Definition: facture-rec.class.php:527
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2550
FactureRec\replaceProduct
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
Definition: facture-rec.class.php:1795
calcul_price_total
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:86
FactureLigne
Class to manage invoice lines.
Definition: facture.class.php:5759
CommonObject\update_price
update_price($exclspec=0, $roundingadjust='none', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
Definition: commonobject.class.php:3550
FactureRec\getNomUrl
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $moretitle='', $notooltip='', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: facture-rec.class.php:1428
FactureRec\getLinesArray
getLinesArray()
Create an array of invoice lines.
Definition: facture-rec.class.php:648
FactureRec\getNextDate
getNextDate()
Return the next date of.
Definition: facture-rec.class.php:1215
FactureLigneRec\update
update(User $user, $notrigger=0)
Update a line to invoice_rec.
Definition: facture-rec.class.php:2244
CommonInvoice\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: commoninvoice.class.php:70
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6185
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
FactureRec\initAsSpecimen
initAsSpecimen($option='')
Initialise an instance with random values.
Definition: facture-rec.class.php:1649
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8499
FactureRec\replaceThirdparty
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: facture-rec.class.php:1778
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
CommonObject\line_max
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
Definition: commonobject.class.php:3398
FactureRec\fetch_lines
fetch_lines()
Get lines of template invoices into this->lines.
Definition: facture-rec.class.php:660
CommonObject\fetch_optionals
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...
Definition: commonobject.class.php:6034
FactureRec\__construct
__construct(DoliDB $db)
Constructor.
Definition: facture-rec.class.php:224
FactureRec\setFrequencyAndUnit
setFrequencyAndUnit($frequency, $unit, $notrigger=0)
Update frequency and unit.
Definition: facture-rec.class.php:1812
dol_time_plus_duree
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:121
User
Class to manage Dolibarr users.
Definition: user.class.php:46
CommonInvoice
Superclass for invoices classes.
Definition: commoninvoice.class.php:32
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6145
FactureRec\$table_ref_field
$table_ref_field
{}
Definition: facture-rec.class.php:76
Product
Class to manage products or services.
Definition: product.class.php:46
getMarginInfos
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
Definition: margins.lib.php:118
CommonObject\add_object_linked
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
Definition: commonobject.class.php:3834
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10745
FactureRec\setModelPdf
setModelPdf($model, $notrigger=0)
Update the model for documents.
Definition: facture-rec.class.php:2034
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4360
CommonInvoiceLine
Parent class of all other business classes for details of elements (invoices, contracts,...
Definition: commoninvoice.class.php:1854
FactureRec\getLibStatut
getLibStatut($mode=0, $alreadypaid=-1)
Return label of object status.
Definition: facture-rec.class.php:1501
FactureLigneRec\fetch
fetch($rowid)
Get line of template invoice.
Definition: facture-rec.class.php:2157
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
FactureRec\strikeIfMaxNbGenReached
strikeIfMaxNbGenReached($ret)
Format string to output with by striking the string if max number of generation was reached.
Definition: facture-rec.class.php:1243
price
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.
Definition: functions.lib.php:5697
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5818
FactureRec\$tva
$tva
Definition: facture-rec.class.php:100
FactureRec\setMaxPeriod
setMaxPeriod($nb, $notrigger=0)
Update the maximum period.
Definition: facture-rec.class.php:1909
CommonInvoice\calculate_date_lim_reglement
calculate_date_lim_reglement($cond_reglement=0)
Returns an invoice payment deadline based on the invoice settlement conditions and billing date.
Definition: commoninvoice.class.php:668
FactureRec\addline
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='', $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)
Add a line to invoice.
Definition: facture-rec.class.php:863
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:2863
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:96
FactureRec\setNextDate
setNextDate($date, $increment_nb_gen_done=0, $notrigger=0)
Update the next date of execution.
Definition: facture-rec.class.php:1864
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
FactureRec\LibStatut
LibStatut($recur, $status, $mode=0, $alreadypaid=-1, $type=0)
Return label of a status.
Definition: facture-rec.class.php:1517
float
div float
Buy price without taxes.
Definition: style.css.php:913