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