dolibarr  20.0.0-alpha
fournisseur.facture-rec.class.php
1 <?php
2 /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
7  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8  * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
9  * Copyright (C) 2017-2024 Frédéric France <frederic.france@free.fr>
10  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
11  * Copyright (C) 2023-2024 Nick Fragoulis
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <https://www.gnu.org/licenses/>.
25  */
26 
33 require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
37 
38 
43 {
44  const TRIGGER_PREFIX = 'SUPPLIERBILLREC';
48  public $element = 'invoice_supplier_rec';
49 
53  public $table_element = 'facture_fourn_rec';
54 
58  public $table_element_line = 'facture_fourn_det_rec';
59 
63  public $fk_element = 'fk_facture_fourn';
64 
68  public $picto = 'bill';
69 
73  protected $table_ref_field = 'titre';
74 
79  public $titre;
83  public $title;
84 
85  public $ref_supplier;
86  public $socid;
87 
92  public $fk_soc;
93 
94  public $suspended; // status
95 
100  public $libelle;
104  public $label;
105 
110  public $amount;
115  public $remise;
116 
117  public $vat_src_code;
118  public $localtax1;
119  public $localtax2;
120 
121  public $user_author;
122  public $user_modif;
123  public $fk_project;
124 
125  public $mode_reglement_id;
126  public $mode_reglement_code;
127  public $cond_reglement_code;
128  public $cond_reglement_doc;
129  public $cond_reglement_id;
130 
134  public $date_lim_reglement;
135 
136  public $usenewprice = 0;
137  public $frequency;
138  public $unit_frequency;
139  public $date_when;
140  public $date_last_gen;
141 
145  public $nb_gen_done;
146 
150  public $nb_gen_max;
151 
155  public $auto_validate; //
156  public $generate_pdf; // 1 to generate PDF on invoice generation (default)
157 
162  public $lines = array();
163 
164 
165  /* Override fields in CommonObject
166  public $entity;
167  public $date_creation;
168  public $date_modification;
169  public $total_ht;
170  public $total_tva;
171  public $total_ttc;
172  public $fk_account;
173  public $mode_reglement;
174  public $cond_reglement;
175  public $note_public;
176  public $note_private;
177  */
178 
203  // BEGIN MODULEBUILDER PROPERTIES
207  public $fields = array(
208  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
209  'titre' => array('type' => 'varchar(100)', 'label' => 'Titre', 'enabled' => 1, 'showoncombobox' => 1, 'visible' => -1, 'position' => 15),
210  'ref_supplier' => array('type' => 'varchar(180)', 'label' => 'RefSupplier', 'enabled' => 1, 'showoncombobox' => 1, 'visible' => -1, 'position' => 20),
211  'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 25, 'index' => 1),
212  'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 30),
213  'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 35),
214  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 40),
215  'suspended' => array('type' => 'integer', 'label' => 'Suspended', 'enabled' => 1, 'visible' => -1, 'position' => 225),
216  'libelle' => array('type' => 'varchar(100)', 'label' => 'Libelle', 'enabled' => 1, 'showoncombobox' => 0, 'visible' => -1, 'position' => 15),
217 
218  'localtax1' => array('type' => 'double(24,8)', 'label' => 'Localtax1', 'enabled' => 1, 'visible' => -1, 'position' => 60, 'isameasure' => 1),
219  'localtax2' => array('type' => 'double(24,8)', 'label' => 'Localtax2', 'enabled' => 1, 'visible' => -1, 'position' => 65, 'isameasure' => 1),
220  'total_ht' => array('type' => 'double(24,8)', 'label' => 'Total', 'enabled' => 1, 'visible' => -1, 'position' => 70, 'isameasure' => 1),
221  'total_tva' => array('type' => 'double(24,8)', 'label' => 'Tva', 'enabled' => 1, 'visible' => -1, 'position' => 55, 'isameasure' => 1),
222  'total_ttc' => array('type' => 'double(24,8)', 'label' => 'Total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 75, 'isameasure' => 1),
223 
224  'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 80),
225  'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 210),
226  '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),
227  'fk_account' => array('type' => 'integer', 'label' => 'Fk account', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 175),
228  'fk_cond_reglement' => array('type' => 'integer', 'label' => 'Fk cond reglement', 'enabled' => 1, 'visible' => -1, 'position' => 90),
229  'fk_mode_reglement' => array('type' => 'integer', 'label' => 'Fk mode reglement', 'enabled' => 1, 'visible' => -1, 'position' => 95),
230  'date_lim_reglement' => array('type' => 'date', 'label' => 'Date lim reglement', 'enabled' => 1, 'visible' => -1, 'position' => 100),
231 
232  'note_private' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105),
233  'note_public' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110),
234  'modelpdf' => array('type' => 'varchar(255)', 'label' => 'Modelpdf', 'enabled' => 1, 'visible' => -1, 'position' => 115),
235 
236  'fk_multicurrency' => array('type' => 'integer', 'label' => 'Fk multicurrency', 'enabled' => 1, 'visible' => -1, 'position' => 180),
237  'multicurrency_code' => array('type' => 'varchar(255)', 'label' => 'Multicurrency code', 'enabled' => 1, 'visible' => -1, 'position' => 185),
238  'multicurrency_tx' => array('type' => 'double(24,8)', 'label' => 'Multicurrency tx', 'enabled' => 1, 'visible' => -1, 'position' => 190, 'isameasure' => 1),
239  'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ht', 'enabled' => 1, 'visible' => -1, 'position' => 195, 'isameasure' => 1),
240  'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total tva', 'enabled' => 1, 'visible' => -1, 'position' => 200, 'isameasure' => 1),
241  'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 205, 'isameasure' => 1),
242 
243  'usenewprice' => array('type' => 'integer', 'label' => 'UseNewPrice', 'enabled' => 1, 'visible' => 0, 'position' => 155),
244  'frequency' => array('type' => 'integer', 'label' => 'Frequency', 'enabled' => 1, 'visible' => -1, 'position' => 150),
245  'unit_frequency' => array('type' => 'varchar(2)', 'label' => 'Unit frequency', 'enabled' => 1, 'visible' => -1, 'position' => 125),
246 
247  'date_when' => array('type' => 'datetime', 'label' => 'Date when', 'enabled' => 1, 'visible' => -1, 'position' => 130),
248  'date_last_gen' => array('type' => 'datetime', 'label' => 'Date last gen', 'enabled' => 1, 'visible' => -1, 'position' => 135),
249  'nb_gen_done' => array('type' => 'integer', 'label' => 'Nb gen done', 'enabled' => 1, 'visible' => -1, 'position' => 140),
250  'nb_gen_max' => array('type' => 'integer', 'label' => 'Nb gen max', 'enabled' => 1, 'visible' => -1, 'position' => 145),
251  'revenuestamp' => array('type' => 'double(24,8)', 'label' => 'RevenueStamp', 'enabled' => 1, 'visible' => -1, 'position' => 160, 'isameasure' => 1),
252  'auto_validate' => array('type' => 'integer', 'label' => 'Auto validate', 'enabled' => 1, 'visible' => -1, 'position' => 165),
253  'generate_pdf' => array('type' => 'integer', 'label' => 'Generate pdf', 'enabled' => 1, 'visible' => -1, 'position' => 170),
254  );
255  // END MODULEBUILDER PROPERTIES
256 
257  const STATUS_NOTSUSPENDED = 0;
258  const STATUS_SUSPENDED = 1;
259 
260 
261 
267  public function __construct($db)
268  {
269  $this->db = $db;
270  }
271 
280  public function create($user, $facFournId, $notrigger = 0)
281  {
282  global $conf;
283 
284  $error = 0;
285  $now = dol_now();
286 
287  // Clean parameters
288  $this->titre = empty($this->titre) ? '' : $this->titre; // deprecated
289  $this->title = empty($this->title) ? '' : $this->title;
290  $keyforref = $this->table_ref_field;
291  $this->ref = $this->$keyforref;
292  $this->ref_supplier = empty($this->ref_supplier) ? '' : $this->ref_supplier;
293  $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
294  $this->suspended = empty($this->suspended) ? 0 : $this->suspended;
295  // No frequency defined then no next date to execution
296  if (empty($this->frequency)) {
297  $this->frequency = 0;
298  $this->date_when = null;
299  }
300  $this->frequency = abs($this->frequency);
301  $this->nb_gen_done = 0;
302  $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
303  $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
304  $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
305 
306  $this->db->begin();
307 
308  // On charge la facture fournisseur depuis laquelle on crée la facture fournisseur modèle
309  $facfourn_src = new FactureFournisseur($this->db);
310  $result = $facfourn_src->fetch($facFournId);
311  if ($result > 0) {
312  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_rec (';
313  $sql .= 'titre';
314  $sql .= ", subtype";
315  $sql .= ', ref_supplier';
316  $sql .= ', entity';
317  $sql .= ', fk_soc';
318  $sql .= ', datec';
319  $sql .= ', suspended';
320  $sql .= ', libelle';
321  $sql .= ', total_ttc';
322  $sql .= ', fk_user_author';
323  $sql .= ', fk_projet';
324  $sql .= ', fk_account';
325  $sql .= ', fk_cond_reglement';
326  $sql .= ', fk_mode_reglement';
327  $sql .= ', date_lim_reglement';
328  $sql .= ', note_private';
329  $sql .= ', note_public';
330  $sql .= ', modelpdf';
331  $sql .= ', fk_multicurrency';
332  $sql .= ', multicurrency_code';
333  $sql .= ', multicurrency_tx';
334  $sql .= ', usenewprice';
335  $sql .= ', frequency';
336  $sql .= ', unit_frequency';
337  $sql .= ', date_when';
338  $sql .= ', date_last_gen';
339  $sql .= ', nb_gen_done';
340  $sql .= ', nb_gen_max';
341  $sql .= ', auto_validate';
342  $sql .= ', generate_pdf';
343  $sql .= ') VALUES (';
344  $sql .= "'".$this->db->escape($this->title)."'";
345  $sql .= ", ".($this->subtype ? "'".$this->db->escape($this->subtype)."'" : "null");
346  $sql .= ", '".$this->db->escape($this->ref_supplier)."'";
347  $sql .= ", ".((int) $conf->entity);
348  $sql .= ", ".((int) $facfourn_src->socid);
349  $sql .= ", '".$this->db->idate($now)."'";
350  $sql .= ", ".((int) $this->suspended);
351  $sql .= ", '".$this->db->escape($this->libelle)."'";
352  $sql .= ", " .(!empty($facfourn_src->total_ttc) ? (float) $facfourn_src->total_ttc : '0'); // amount
353  $sql .= ", " .((int) $user->id);
354  $sql .= ", " .(!empty($this->fk_project) ? ((int) $this->fk_project) : 'NULL');
355  $sql .= ", " .(!empty($facfourn_src->fk_account) ? ((int) $facfourn_src->fk_account) : 'NULL');
356  $sql .= ", " .($this->cond_reglement_id > 0 ? (int) $this->cond_reglement_id : 'NULL');
357  $sql .= ", " .($this->mode_reglement_id > 0 ? (int) $this->mode_reglement_id : 'NULL');
358  $sql .= ", ".($facfourn_src->date_echeance > 0 ? "'".$this->db->idate($facfourn_src->date_echeance)."'" : 'NULL'); // date_lim_reglement
359  $sql .= ", " .(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : 'NULL');
360  $sql .= ", " .(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : 'NULL');
361  $sql .= ", " .(!empty($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : 'NULL');
362  $sql .= ", " . (int) $facfourn_src->fk_multicurrency;
363  $sql .= ", '".$this->db->escape($facfourn_src->multicurrency_code)."'";
364  $sql .= ", " . (float) $facfourn_src->multicurrency_tx;
365  $sql .= ", " . (int) $this->usenewprice;
366  $sql .= ", " . (int) $this->frequency;
367  $sql .= ", '".$this->db->escape($this->unit_frequency)."'";
368  $sql .= ", " .(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'NULL');
369  $sql .= ", " .(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'NULL');
370  $sql .= ", " . (int) $this->nb_gen_done;
371  $sql .= ", " . (int) $this->nb_gen_max;
372  $sql .= ", " . (int) $this->auto_validate;
373  $sql .= ", " . (int) $this->generate_pdf;
374  $sql .= ')';
375 
376  if ($this->db->query($sql)) {
377  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX. 'facture_fourn_rec');
378 
379  // Fields used into addline later
380  $this->fk_multicurrency = $facfourn_src->fk_multicurrency;
381 
382  $this->multicurrency_code = $facfourn_src->multicurrency_code;
383  $this->multicurrency_tx = $facfourn_src->multicurrency_tx;
384 
385  // Add lines
386  $num = count($facfourn_src->lines);
387  for ($i = 0; $i < $num; $i++) {
388  $tva_tx = $facfourn_src->lines[$i]->tva_tx;
389  if (!empty($facfourn_src->lines[$i]->vat_src_code) && !preg_match('/\‍(/', (string) $tva_tx)) {
390  $tva_tx .= ' ('.$facfourn_src->lines[$i]->vat_src_code.')';
391  }
392 
393  $result_insert = $this->addline(
394  $facfourn_src->lines[$i]->fk_product,
395  $facfourn_src->lines[$i]->ref_supplier,
396  $facfourn_src->lines[$i]->product_label,
397  $facfourn_src->lines[$i]->desc ? $facfourn_src->lines[$i]->desc : $facfourn_src->lines[$i]->description,
398  $facfourn_src->lines[$i]->pu_ht,
399  $facfourn_src->lines[$i]->pu_ttc,
400  $facfourn_src->lines[$i]->qty,
401  $facfourn_src->lines[$i]->remise_percent,
402  $tva_tx,
403  $facfourn_src->lines[$i]->localtax1_tx,
404  $facfourn_src->lines[$i]->localtax2_tx,
405  'HT',
406  $facfourn_src->lines[$i]->product_type,
407  $facfourn_src->lines[$i]->date_start,
408  $facfourn_src->lines[$i]->date_end,
409  $facfourn_src->lines[$i]->info_bits,
410  $facfourn_src->lines[$i]->special_code,
411  $facfourn_src->lines[$i]->rang,
412  $facfourn_src->lines[$i]->fk_unit
413  );
414 
415  if ($result_insert < 0) {
416  $error++;
417  } else {
418  $objectline = new FactureFournisseurLigneRec($this->db);
419 
420  $result2 = $objectline->fetch($result_insert);
421  if ($result2 > 0) {
422  // Extrafields
423  if (method_exists($facfourn_src->lines[$i], 'fetch_optionals')) {
424  $facfourn_src->lines[$i]->fetch_optionals($facfourn_src->lines[$i]->id);
425  $objectline->array_options = $facfourn_src->lines[$i]->array_options;
426  }
427 
428  $result = $objectline->insertExtraFields();
429  if ($result < 0) {
430  $error++;
431  }
432  } elseif ($result2 < 0) {
433  $this->errors[] = $objectline->error;
434  $error++;
435  }
436  }
437  }
438 
439  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
440  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
441  }
442 
443  // Add object linked
444  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
445  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
446  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, ...))
447  foreach ($tmp_origin_id as $origin_id) {
448  $ret = $this->add_object_linked($origin, $origin_id);
449  if (!$ret) {
450  $this->error = $this->db->lasterror();
451  $error++;
452  }
453  }
454  } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
455  $origin_id = $tmp_origin_id;
456  $ret = $this->add_object_linked($origin, $origin_id);
457  if (!$ret) {
458  $this->error = $this->db->lasterror();
459  $error++;
460  }
461  }
462  }
463  }
464 
465  if (!$error) {
466  $result = $this->insertExtraFields();
467  if ($result < 0) {
468  $error++;
469  }
470  }
471 
472  if (!$error && !$notrigger) {
473  // Call trigger
474  $result = $this->call_trigger('SUPPLIERBILLREC_CREATE', $user);
475  if ($result < 0) {
476  $this->db->rollback();
477  return -2;
478  }
479  // End call triggers
480  }
481 
482  if ($error) {
483  $this->db->rollback();
484  return -3;
485  } else {
486  $this->db->commit();
487  return $this->id;
488  }
489  } else {
490  $this->error = $this->db->lasterror();
491  $this->db->rollback();
492  return -2;
493  }
494  } else {
495  $this->db->rollback();
496  return -1;
497  }
498  }
499 
500 
508  public function update(User $user, $notrigger = 0)
509  {
510  $error = 0;
511 
512  $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_rec SET";
513  $sql .= " titre = '" . (!empty($this->title) ? $this->db->escape($this->title) : "")."'," ;
514  $sql .= " subtype=".(isset($this->subtype) ? $this->db->escape($this->subtype) : "null").",";
515  $sql .= " ref_supplier = '". (!empty($this->ref_supplier) ? $this->db->escape($this->ref_supplier) : "")."',";
516  $sql .= " entity = ". (!empty($this->entity) ? ((int) $this->entity) : 1) . ',';
517  if (!empty($this->socid) && $this->socid > 0) {
518  $sql .= " fk_soc = ". ((int) $this->socid). ',';
519  } elseif (!empty($this->fk_soc) && $this->fk_soc > 0) { // For backward compatibility
520  $sql .= " fk_soc = ". ((int) $this->fk_soc). ',';
521  }
522  $sql .= " suspended = ". (!empty($this->suspended) ? ((int) $this->suspended) : 0) . ',';
523  $sql .= " libelle = ". (!empty($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : 'NULL') . ",";
524  $sql .= " vat_src_code = ". (!empty($this->vat_src_code) ? "'".$this->db->escape($this->vat_src_code)."'" : 'NULL') . ',';
525  $sql .= " localtax1 = ". (!empty($this->localtax1) ? ((float) $this->localtax1) : 0.00) . ',';
526  $sql .= " localtax2 = ". (!empty($this->localtax2) ? ((float) $this->localtax2) : 0.00) . ',';
527  $sql .= " total_ht = ". (!empty($this->total_ht) ? ((float) $this->total_ht) : 0.00) . ',';
528  $sql .= " total_tva = ". (!empty($this->total_tva) ? ((float) $this->total_tva) : 0.00) . ',';
529  $sql .= " total_ttc = ". (!empty($this->total_ttc) ? ((float) $this->total_ttc) : 0.00) . ',';
530  $sql .= " fk_user_modif = ". ((int) $user->id) . ',';
531  $sql .= " fk_projet = ". (!empty($this->fk_project) ? ((int) $this->fk_project) : 'NULL') . ',';
532  $sql .= " fk_account = ". (!empty($this->fk_account) ? ((int) $this->fk_account) : 'NULL') . ',';
533  $sql .= " fk_mode_reglement = ". (!empty($this->mode_reglement_id) ? ((int) $this->mode_reglement_id) : 'NULL') . ',';
534  $sql .= " fk_cond_reglement = ". (!empty($this->cond_reglement_id) ? ((int) $this->cond_reglement_id) : 'NULL') . ',';
535  $sql .= " date_lim_reglement = ". (!empty($this->date_lim_reglement) ? "'".$this->db->idate($this->date_lim_reglement)."'" : 'NULL') . ',';
536  $sql .= " note_private = '". (!empty($this->note_private) ? $this->db->escape($this->note_private) : '') . "',";
537  $sql .= " note_public = '". (!empty($this->note_public) ? $this->db->escape($this->note_public) : '') . "',";
538  $sql .= " modelpdf = ". (!empty($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : 'NULL') . ",";
539  $sql .= " fk_multicurrency = ". (!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : 'NULL') . ',';
540  $sql .= " multicurrency_code = ". (!empty($this->multicurrency_code) ? "'".$this->db->escape($this->multicurrency_code)."'" : 'NULL') . ",";
541  $sql .= " multicurrency_tx = ". (!empty($this->multicurrency_tx) ? ((float) $this->multicurrency_tx) : 1) . ',';
542  $sql .= " multicurrency_total_ht = ". (!empty($this->multicurrency_total_ht) ? ((float) $this->multicurrency_total_ht) : 0.00) . ',';
543  $sql .= " multicurrency_total_tva = ". (!empty($this->multicurrency_total_tva) ? ((float) $this->multicurrency_total_tva) : 0.00) . ',';
544  $sql .= " multicurrency_total_ttc = ". (!empty($this->multicurrency_total_ttc) ? ((float) $this->multicurrency_total_ttc) : 0.00) . ',';
545  $sql .= " usenewprice = ". (!empty($this->usenewprice) ? ((int) $this->usenewprice) : 0) . ',';
546  $sql .= " frequency = ". (!empty($this->frequency) ? ((int) $this->frequency) : 0). ',';
547  $sql .= " unit_frequency = '". (!empty($this->unit_frequency) ? $this->db->escape($this->unit_frequency) : ''). "',";
548  $sql .= " date_when = ". (!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'NULL') . ',';
549  $sql .= " date_last_gen = ". (!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'NULL') . ',';
550  $sql .= " nb_gen_done = ". (!empty($this->nb_gen_done) ? ((int) $this->nb_gen_done) : 0) . ',';
551  $sql .= " nb_gen_max = ". (!empty($this->nb_gen_max) ? ((int) $this->nb_gen_max) : 0) . ',';
552  $sql .= " auto_validate = ". (!empty($this->auto_validate) ? ((int) $this->auto_validate) : 0);
553  $sql .= " WHERE rowid = ". (int) $this->id;
554 
555  $this->db->begin();
556 
557  dol_syslog(get_class($this)."::update", LOG_DEBUG);
558  $resql = $this->db->query($sql);
559  if ($resql) {
560  if (!$error) {
561  $result = $this->insertExtraFields();
562  if ($result < 0) {
563  $error++;
564  }
565  }
566 
567  if (!$error && !$notrigger) {
568  // Call trigger
569  $result = $this->call_trigger('SUPPLIERBILLREC_MODIFY', $user);
570  if ($result < 0) {
571  $this->db->rollback();
572  return -2;
573  }
574  // End call triggers
575  }
576  $this->db->commit();
577  return 1;
578  } else {
579  $this->error = $this->db->lasterror();
580  $this->db->rollback();
581  return -2;
582  }
583  }
584 
593  public function fetch($rowid, $ref = '', $ref_ext = '')
594  {
595  $sql = 'SELECT f.rowid, f.titre as title, f.subtype, f.ref_supplier, f.entity, f.fk_soc';
596  $sql .= ', f.datec, f.tms, f.suspended';
597  $sql .= ', f.libelle as label';
598  $sql .= ', f.vat_src_code, f.localtax1, f.localtax2';
599  $sql .= ', f.total_tva, f.total_ht, f.total_ttc';
600  $sql .= ', f.fk_user_author, f.fk_user_modif';
601  $sql .= ', f.fk_projet as fk_project, f.fk_account';
602  $sql .= ', f.fk_mode_reglement, p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
603  $sql .= ', f.fk_cond_reglement, c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
604  $sql .= ', f.date_lim_reglement';
605  $sql .= ', f.note_private, f.note_public, f.modelpdf as model_pdf';
606  $sql .= ', f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc';
607  $sql .= ', f.usenewprice, f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.auto_validate';
608  $sql .= ', f.generate_pdf';
609  $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_rec as f';
610  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
611  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id';
612  $sql .= ' WHERE f.entity IN ('.getEntity('invoice').')';
613  if ($rowid) {
614  $sql .= ' AND f.rowid='. (int) $rowid;
615  } elseif ($ref) {
616  $sql .= " AND f.titre='".$this->db->escape($ref)."'";
617  } else {
618  $sql .= ' AND f.rowid = 0';
619  }
620 
621  $result = $this->db->query($sql);
622  if ($result) {
623  if ($this->db->num_rows($result)) {
624  $obj = $this->db->fetch_object($result);
625 
626  $keyforref = $this->table_ref_field;
627 
628  $this->id = $obj->rowid;
629  $this->titre = $obj->title;
630  $this->title = $obj->title;
631  $this->subtype = $obj->subtype;
632  $this->ref = $obj->title;
633  $this->ref_supplier = $obj->ref_supplier;
634  $this->entity = $obj->entity;
635  $this->socid = $obj->fk_soc;
636  $this->date_creation = $obj->datec;
637  $this->date_modification = $obj->tms;
638  $this->suspended = $obj->suspended;
639  $this->libelle = $obj->label;
640  $this->label = $obj->label;
641  $this->vat_src_code = $obj->vat_src_code;
642  $this->total_localtax1 = $obj->localtax1;
643  $this->total_localtax2 = $obj->localtax2;
644  $this->total_ht = $obj->total_ht;
645  $this->total_tva = $obj->total_tva;
646  $this->total_ttc = $obj->total_ttc;
647  $this->user_author = $obj->fk_user_author;
648  $this->user_modif = $obj->fk_user_modif;
649  $this->fk_project = $obj->fk_project;
650  $this->fk_account = $obj->fk_account;
651  $this->mode_reglement_id = $obj->fk_mode_reglement;
652  $this->mode_reglement_code = $obj->mode_reglement_code;
653  $this->mode_reglement = $obj->mode_reglement_libelle;
654  $this->cond_reglement_id = $obj->fk_cond_reglement;
655  $this->cond_reglement_code = $obj->cond_reglement_code;
656  $this->cond_reglement = $obj->cond_reglement_libelle;
657  $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
658  $this->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
659  $this->note_private = $obj->note_private;
660  $this->note_public = $obj->note_public;
661  $this->model_pdf = $obj->model_pdf;
662 
663  // Multicurrency
664  $this->fk_multicurrency = $obj->fk_multicurrency;
665  $this->multicurrency_code = $obj->multicurrency_code;
666  $this->multicurrency_tx = $obj->multicurrency_tx;
667  $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
668  $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
669  $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
670 
671  $this->usenewprice = $obj->usenewprice;
672  $this->frequency = $obj->frequency;
673  $this->unit_frequency = $obj->unit_frequency;
674  $this->date_when = $this->db->jdate($obj->date_when);
675  $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
676  $this->nb_gen_done = $obj->nb_gen_done;
677  $this->nb_gen_max = $obj->nb_gen_max;
678  $this->auto_validate = $obj->auto_validate;
679  $this->generate_pdf = $obj->generate_pdf;
680 
681  // Retrieve all extrafield
682  // fetch optionals attributes and labels
683  $this->fetch_optionals();
684 
685  /*
686  * Lines
687  */
688  $result = $this->fetch_lines();
689  if ($result < 0) {
690  $this->error = $this->db->lasterror();
691  return -3;
692  }
693  return 1;
694  } else {
695  $this->error = 'Bill with id '.$rowid.' or ref '.$ref.' not found';
696  dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
697  return -2;
698  }
699  } else {
700  $this->error = $this->db->error();
701  return -1;
702  }
703  }
704 
705 
711  public function getLinesArray()
712  {
713  return $this->fetch_lines();
714  }
715 
716  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
722  public function fetch_lines()
723  {
724  // phpcs:enable
725  $this->lines = array();
726 
727  // Retrieve all extrafield for line
728  // fetch optionals attributes and labels
729  /*if (!is_object($extrafields)) {
730  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
731  $extrafields = new ExtraFields($this->db);
732  }
733  $extrafields->fetch_name_optionals_label($this->table_element_line, true);
734  */
735 
736  $sql = 'SELECT l.rowid,';
737  $sql .= ' l.fk_facture_fourn, l.fk_parent_line, l.fk_product, l.ref, l.label, l.description as line_desc,';
738  $sql .= ' l.pu_ht, l.pu_ttc, l.qty, l.remise_percent, l.fk_remise_except, l.vat_src_code, l.tva_tx,';
739  $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type,';
740  $sql .= ' l.total_ht, l.total_tva, l.total_ttc, total_localtax1, total_localtax2,';
741  $sql .= ' l.product_type, l.date_start, l.date_end,';
742  $sql .= ' l.info_bits, l.special_code, l.rang,';
743  $sql .= ' l.fk_unit, l.import_key, l.fk_user_author, l.fk_user_modif,';
744  $sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
745  $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
746  $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_det_rec as l';
747  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
748  $sql .= ' WHERE l.fk_facture_fourn = '. (int) $this->id;
749  $sql .= ' ORDER BY l.rang';
750 
751  dol_syslog('FactureFournisseurRec::fetch_lines', LOG_DEBUG);
752 
753  $result = $this->db->query($sql);
754  if ($result) {
755  $num = $this->db->num_rows($result);
756  $i = 0;
757  while ($i < $num) {
758  $objp = $this->db->fetch_object($result);
759 
760  $line = new FactureFournisseurLigneRec($this->db);
761 
762  $line->id = $objp->rowid;
763  $line->fk_facture_fourn = $objp->fk_facture_fourn;
764  $line->fk_parent = $objp->fk_parent_line;
765  $line->fk_product = $objp->fk_product;
766  $line->ref_supplier = $objp->ref;
767  $line->label = $objp->label;
768  $line->description = $objp->line_desc;
769  $line->desc = $objp->line_desc;
770  $line->pu_ht = $objp->pu_ht;
771  $line->pu_ttc = $objp->pu_ttc;
772  $line->qty = $objp->qty;
773  $line->remise_percent = $objp->remise_percent;
774  $line->fk_remise_except = $objp->fk_remise_except;
775  $line->vat_src_code = $objp->vat_src_code;
776  $line->tva_tx = $objp->tva_tx;
777  $line->localtax1_tx = $objp->localtax1_tx;
778  $line->localtax1_type = $objp->localtax1_type;
779  $line->localtax2_tx = $objp->localtax2_tx;
780  $line->localtax2_type = $objp->localtax2_type;
781  $line->total_ht = $objp->total_ht;
782  $line->total_tva = $objp->total_tva;
783  $line->total_localtax1 = $objp->total_localtax1;
784  $line->total_localtax2 = $objp->total_localtax2;
785  $line->total_ttc = $objp->total_ttc;
786  $line->product_type = $objp->product_type;
787  $line->date_start = $objp->date_start;
788  $line->date_end = $objp->date_end;
789  $line->info_bits = $objp->info_bits ;
790  $line->special_code = $objp->special_code;
791  $line->rang = $objp->rang;
792  $line->fk_unit = $objp->fk_unit;
793  $line->import_key = $objp->import_key;
794  $line->fk_user_author = $objp->fk_user_author;
795  $line->fk_user_modif = $objp->fk_user_modif;
796  $line->fk_multicurrency = $objp->fk_multicurrency;
797  $line->multicurrency_code = $objp->multicurrency_code;
798  $line->multicurrency_subprice = $objp->multicurrency_subprice;
799  $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
800  $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
801  $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
802 
803  $line->fetch_optionals();
804 
805  $this->lines[$i] = $line;
806 
807  $i++;
808  }
809 
810  $this->db->free($result);
811  return 1;
812  } else {
813  $this->error = $this->db->lasterror();
814  return -3;
815  }
816  }
817 
818 
827  public function delete(User $user, $notrigger = 0, $idwarehouse = -1)
828  {
829  $rowid = $this->id;
830 
831  dol_syslog(get_class($this)."::delete rowid=".((int) $rowid), LOG_DEBUG);
832 
833  $error = 0;
834  $this->db->begin();
835 
836  $main = MAIN_DB_PREFIX.'facture_fourn_det_rec';
837  $ef = $main."_extrafields";
838 
839  $sqlef = "DELETE FROM ".$ef." WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_facture_fourn = ". (int) $rowid .")";
840  $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_fourn_det_rec WHERE fk_facture_fourn = ". (int) $rowid;
841 
842  if ($this->db->query($sqlef) && $this->db->query($sql)) {
843  $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_fourn_rec WHERE rowid = ". (int) $rowid;
844  dol_syslog($sql);
845  if ($this->db->query($sql)) {
846  // Delete linked object
847  $res = $this->deleteObjectLinked();
848  if ($res < 0) {
849  $error = -3;
850  }
851  // Delete extrafields
852  $res = $this->deleteExtraFields();
853  if ($res < 0) {
854  $error = -4;
855  }
856  } else {
857  $this->error = $this->db->lasterror();
858  $error = -1;
859  }
860  } else {
861  $this->error = $this->db->lasterror();
862  $error = -2;
863  }
864  if (!$error && !$notrigger) {
865  // Call trigger
866  $result = $this->call_trigger('SUPPLIERBILLREC_DELETE', $user);
867  if ($result < 0) {
868  $error++;
869  }
870  // End call triggers
871  }
872  if (! $error) {
873  $this->db->commit();
874  return 1;
875  } else {
876  $this->db->rollback();
877  return $error;
878  }
879  }
880 
907  public function addline($fk_product, $ref, $label, $desc, $pu_ht, $pu_ttc, $qty, $remise_percent, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $price_base_type = 'HT', $type = 0, $date_start = 0, $date_end = 0, $info_bits = 0, $special_code = 0, $rang = -1, $fk_unit = null, $pu_ht_devise = 0)
908  {
909  global $mysoc, $user;
910 
911  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
912 
913  $facid = $this->id; //Supplier invoice template ID linked to
914 
915  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,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,date_end_fill=$date_end", LOG_DEBUG);
916 
917  // Check if object of the line is product or service
918  if ($type < 0) {
919  return -1;
920  }
921 
922  if ($this->suspended == self::STATUS_NOTSUSPENDED) {
923  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
924 
925  // Clean vat code
926  $reg = array();
927  $vat_src_code = '';
928  if (preg_match('/\‍((.*)\‍)/', (string) $txtva, $reg)) {
929  $vat_src_code = $reg[1];
930  $txtva = preg_replace('/\s*\‍(.*\‍)/', '', (string) $txtva); // Remove code into vatrate.
931  }
932 
933  // Clean parameters
934  $fk_product = empty($fk_product) ? 0 : $fk_product;
935  $label = empty($label) ? '' : $label;
936  $remise_percent = empty($remise_percent) ? 0 : price2num($remise_percent);
937  $qty = price2num($qty);
938  $pu_ht = price2num($pu_ht);
939  $pu_ttc = price2num($pu_ttc);
940  if (!preg_match('/\‍((.*)\‍)/', $txtva)) {
941  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
942  }
943  $txlocaltax1 = price2num($txlocaltax1);
944  $txlocaltax2 = price2num($txlocaltax2);
945  $txtva = !empty($txtva) ? $txtva : 0;
946  $txlocaltax1 = !empty($txlocaltax1) ? $txlocaltax1 : 0;
947  $txlocaltax2 = !empty($txlocaltax2) ? $txlocaltax2 : 0;
948  $info_bits = !empty($info_bits) ? $info_bits : 0;
949  $info_bits = !empty($info_bits) ? $info_bits : 0;
950  $pu = $price_base_type == 'HT' ? $pu_ht : $pu_ttc;
951 
952  // Calcul du total TTC et de la TVA pour la ligne a partir de qty, pu, remise_percent et txtva
953  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
954  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
955 
956  $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);
957  $total_ht = $tabprice[0];
958  $total_tva = $tabprice[1];
959  $total_ttc = $tabprice[2];
960  $total_localtax1 = $tabprice[9];
961  $total_localtax2 = $tabprice[10];
962  $pu_ht = $tabprice[3];
963 
964  // MultiCurrency
965  $multicurrency_total_ht = $tabprice[16];
966  $multicurrency_total_tva = $tabprice[17];
967  $multicurrency_total_ttc = $tabprice[18];
968  $pu_ht_devise = $tabprice[19];
969 
970  $this->db->begin();
971  $product_type = $type;
972  if ($fk_product) {
973  $product = new Product($this->db);
974  $result = $product->fetch($fk_product);
975  if ($result < 0) {
976  return -1;
977  }
978  $product_type = $product->type;
979  if (empty($label)) {
980  $label = $product->label;
981  }
982  }
983 
984  $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'facture_fourn_det_rec (';
985  $sql .= 'fk_facture_fourn';
986  $sql .= ', fk_product';
987  $sql .= ', ref';
988  $sql .= ', label';
989  $sql .= ', description';
990  $sql .= ', pu_ht';
991  $sql .= ', pu_ttc';
992  $sql .= ', qty';
993  $sql .= ', remise_percent';
994  $sql .= ', fk_remise_except';
995  $sql .= ', vat_src_code';
996  $sql .= ', tva_tx';
997  $sql .= ', localtax1_tx';
998  $sql .= ', localtax1_type';
999  $sql .= ', localtax2_tx';
1000  $sql .= ', localtax2_type';
1001  $sql .= ', total_ht';
1002  $sql .= ', total_tva';
1003  $sql .= ', total_localtax1';
1004  $sql .= ', total_localtax2';
1005  $sql .= ', total_ttc';
1006  $sql .= ', product_type';
1007  $sql .= ', date_start';
1008  $sql .= ', date_end';
1009  $sql .= ', info_bits';
1010  $sql .= ', special_code';
1011  $sql .= ', rang';
1012  $sql .= ', fk_unit';
1013  $sql .= ', fk_user_author';
1014  $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
1015  $sql .= ') VALUES (';
1016  $sql .= ' ' . (int) $facid; // source supplier invoice id
1017  $sql .= ', ' . (!empty($fk_product) ? "'" . $this->db->escape($fk_product) . "'" : 'null');
1018  $sql .= ', ' . (!empty($ref) ? "'" . $this->db->escape($ref) . "'" : 'null');
1019  $sql .= ', ' . (!empty($label) ? "'" . $this->db->escape($label) . "'" : 'null');
1020  $sql .= ", '" . $this->db->escape($desc) . "'";
1021  $sql .= ', ' . price2num($pu_ht);
1022  $sql .= ', ' . price2num($pu_ttc);
1023  $sql .= ', ' . price2num($qty);
1024  $sql .= ', ' . price2num($remise_percent);
1025  $sql .= ', null';
1026  $sql .= ", '" . $this->db->escape($vat_src_code) . "'";
1027  $sql .= ', ' . price2num($txtva);
1028  $sql .= ', ' . price2num($txlocaltax1);
1029  $sql .= ", '" . $this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] : '') . "'";
1030  $sql .= ', ' . price2num($txlocaltax2);
1031  $sql .= ", '" . $this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] : '') . "'";
1032  $sql .= ', ' . price2num($total_ht);
1033  $sql .= ', ' . price2num($total_tva);
1034  $sql .= ', ' . price2num($total_localtax1);
1035  $sql .= ', ' . price2num($total_localtax2);
1036  $sql .= ', ' . price2num($total_ttc);
1037  $sql .= ', ' . (int) $product_type;
1038  $sql .= ', ' . ($date_start > 0 ? (int) $date_start : 'NULL');
1039  $sql .= ', ' . ($date_end > 0 ? (int) $date_end : 'NULL');
1040  $sql .= ', ' . (int) $info_bits;
1041  $sql .= ', ' . (int) $special_code;
1042  $sql .= ', ' . (int) $rang;
1043  $sql .= ', ' . ($fk_unit ? (int) $fk_unit : 'NULL');
1044  $sql .= ', ' . (int) $user->id;
1045  $sql .= ', ' . (int) $this->fk_multicurrency;
1046  $sql .= ", '" . $this->db->escape($this->multicurrency_code) . "'";
1047  $sql .= ', ' . price2num($pu_ht_devise, 'CU');
1048  $sql .= ', ' . price2num($multicurrency_total_ht, 'CT');
1049  $sql .= ', ' . price2num($multicurrency_total_tva, 'CT');
1050  $sql .= ', ' . price2num($multicurrency_total_ttc, 'CT');
1051  $sql .= ')';
1052 
1053  dol_syslog(get_class($this). '::addline', LOG_DEBUG);
1054  if ($this->db->query($sql)) {
1055  $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX. 'facture_fourn_det_rec');
1056  $this->update_price();
1057  $this->id = $facid;
1058  $this->db->commit();
1059  return $lineId;
1060  } else {
1061  $this->db->rollback();
1062  $this->error = $this->db->lasterror();
1063 
1064  return -1;
1065  }
1066  } else {
1067  $this->error = 'Recurring Invoice is suspended. adding lines not allowed.';
1068 
1069  return -1;
1070  }
1071  }
1072 
1100  public function updateline($rowid, $fk_product, $ref, $label, $desc, $pu_ht, $qty, $remise_percent, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $price_base_type = 'HT', $type = 0, $date_start = 0, $date_end = 0, $info_bits = 0, $special_code = 0, $rang = -1, $fk_unit = null, $pu_ht_devise = 0, $pu_ttc = 0)
1101  {
1102  global $mysoc, $user;
1103 
1104  $facid = $this->id;
1105 
1106  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, price_base_type=$price_base_type, pu_ttc=$pu_ttc, type=$type, fk_unit=$fk_unit, pu_ht_devise=$pu_ht_devise", LOG_DEBUG);
1107  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1108 
1109  // Check parameters
1110  if ($type < 0) {
1111  return -1;
1112  }
1113 
1114  if ($this->status == self::STATUS_SUSPENDED) {
1115  // Clean parameters
1116  $fk_product = empty($fk_product) ? 0 : $fk_product;
1117  $label = empty($label) ? '' : $label;
1118  $remise_percent = empty($remise_percent) ? 0 : price2num($remise_percent);
1119  $qty = price2num($qty);
1120  $info_bits = empty($info_bits) ? 0 : $info_bits;
1121  $pu_ht = price2num($pu_ht);
1122  $pu_ttc = price2num($pu_ttc);
1123  $pu_ht_devise = price2num($pu_ht_devise);
1124 
1125  if (!preg_match('/\‍((.*)\‍)/', (string) $txtva)) {
1126  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1127  }
1128 
1129  $txlocaltax1 = empty($txlocaltax1) ? 0 : price2num($txlocaltax1);
1130  $txlocaltax2 = empty($txlocaltax2) ? 0 : price2num($txlocaltax2);
1131  $this->multicurrency_total_ht = empty($this->multicurrency_total_ht) ? 0 : $this->multicurrency_total_ht;
1132  $this->multicurrency_total_tva = empty($this->multicurrency_total_tva) ? 0 : $this->multicurrency_total_tva;
1133  $this->multicurrency_total_ttc = empty($this->multicurrency_total_ttc) ? 0 : $this->multicurrency_total_ttc;
1134 
1135  $pu = $price_base_type == 'HT' ? $pu_ht : $pu_ttc;
1136 
1137 
1138  // Calculate total with, without tax and tax from qty, pu, remise_percent and txtva
1139  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1140  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1141 
1142  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
1143 
1144  // Clean vat code
1145  $vat_src_code = '';
1146  $reg = array();
1147  if (preg_match('/\‍((.*)\‍)/', $txtva, $reg)) {
1148  $vat_src_code = $reg[1];
1149  $txtva = preg_replace('/\s*\‍(.*\‍)/', '', $txtva); // Remove code into vatrate.
1150  }
1151 
1152  $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);
1153 
1154  $total_ht = $tabprice[0];
1155  $total_tva = $tabprice[1];
1156  $total_ttc = $tabprice[2];
1157  $total_localtax1 = $tabprice[9];
1158  $total_localtax2 = $tabprice[10];
1159  $pu_ht = $tabprice[3];
1160  $pu_tva = $tabprice[4];
1161  $pu_ttc = $tabprice[5];
1162 
1163  // MultiCurrency
1164  $multicurrency_total_ht = $tabprice[16];
1165  $multicurrency_total_tva = $tabprice[17];
1166  $multicurrency_total_ttc = $tabprice[18];
1167  $pu_ht_devise = $tabprice[19];
1168 
1169  $product_type = $type;
1170  if ($fk_product) {
1171  $product = new Product($this->db);
1172  $result = $product->fetch($fk_product);
1173  $product_type = $product->type;
1174  }
1175 
1176  $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture_fourn_det_rec SET';
1177  $sql .= ' fk_facture_fourn = ' . ((int) $facid);
1178  $sql .= ', fk_product = ' . ($fk_product > 0 ? ((int) $fk_product) : 'null');
1179  $sql .= ", ref = '" . $this->db->escape($ref) . "'";
1180  $sql .= ", label = '" . $this->db->escape($label) . "'";
1181  $sql .= ", description = '" . $this->db->escape($desc) . "'";
1182  $sql .= ', pu_ht = ' . price2num($pu_ht);
1183  $sql .= ', qty = ' . price2num($qty);
1184  $sql .= ", remise_percent = '" . price2num($remise_percent) . "'";
1185  $sql .= ", vat_src_code = '" . $this->db->escape($vat_src_code) . "'";
1186  $sql .= ', tva_tx = ' . price2num($txtva);
1187  $sql .= ', localtax1_tx = ' . (float) $txlocaltax1;
1188  $sql .= ", localtax1_type = '" . $this->db->escape($localtaxes_type[0]) . "'";
1189  $sql .= ', localtax2_tx = ' . (float) $txlocaltax2;
1190  $sql .= ", localtax2_type = '" . $this->db->escape($localtaxes_type[2]) . "'";
1191  $sql .= ", total_ht = '" . price2num($total_ht) . "'";
1192  $sql .= ", total_tva = '" . price2num($total_tva) . "'";
1193  $sql .= ", total_localtax1 = '" . price2num($total_localtax1) . "'";
1194  $sql .= ", total_localtax2 = '" . price2num($total_localtax2) . "'";
1195  $sql .= ", total_ttc = '" . price2num($total_ttc) . "'";
1196  $sql .= ', product_type = ' . (int) $product_type;
1197  $sql .= ', date_start = ' . (empty($date_start) ? 'NULL' : (int) $date_start);
1198  $sql .= ', date_end = ' . (empty($date_end) ? 'NULL' : (int) $date_end);
1199  $sql .= ', info_bits = ' . (int) $info_bits;
1200  $sql .= ', special_code = ' . (int) $special_code;
1201  $sql .= ', rang = ' . (int) $rang;
1202  $sql .= ', fk_unit = ' . ($fk_unit ? "'" . $this->db->escape($fk_unit) . "'" : 'null');
1203  $sql .= ', fk_user_modif = ' . (int) $user;
1204  $sql .= ', multicurrency_subprice = '.price2num($pu_ht_devise);
1205  $sql .= ', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1206  $sql .= ', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1207  $sql .= ', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1208  $sql .= ' WHERE rowid = ' . (int) $rowid;
1209 
1210  dol_syslog(get_class($this). '::updateline', LOG_DEBUG);
1211  if ($this->db->query($sql)) {
1212  $this->id = $facid;
1213  $this->update_price();
1214  return 1;
1215  } else {
1216  $this->error = $this->db->lasterror();
1217  return -1;
1218  }
1219  }
1220  return 0;
1221  }
1222 
1223 
1231  public function getNextNumRef($soc, $mode = 'next')
1232  {
1233  // Not used for recurring invoices
1234  return '';
1235  }
1236 
1242  public function getNextDate()
1243  {
1244  if (empty($this->date_when)) {
1245  return false;
1246  }
1247  return dol_time_plus_duree($this->date_when, $this->frequency, $this->unit_frequency);
1248  }
1249 
1255  public function isMaxNbGenReached()
1256  {
1257  $ret = false;
1258  if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1259  $ret = true;
1260  }
1261  return $ret;
1262  }
1263 
1270  public function strikeIfMaxNbGenReached($ret)
1271  {
1272  // Special case to strike the date
1273  return ($this->isMaxNbGenReached() ? '<strike>' : '').$ret.($this->isMaxNbGenReached() ? '</strike>' : '');
1274  }
1275 
1286  public function createRecurringInvoices($restrictioninvoiceid = 0, $forcevalidation = 0)
1287  {
1288  global $conf, $langs, $db, $user, $hookmanager;
1289 
1290  $error = 0;
1291  $nb_create = 0;
1292 
1293  // Load translation files required by the page
1294  $langs->loadLangs(array('main', 'bills'));
1295 
1296  $now = dol_now();
1297  $tmparray = dol_getdate($now);
1298  $today = dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year']); // Today is last second of current day
1299 
1300  dol_syslog('createRecurringInvoices restrictioninvoiceid=' .$restrictioninvoiceid. ' forcevalidation=' .$forcevalidation);
1301 
1302  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_fourn_rec';
1303  $sql .= ' WHERE frequency > 0'; // A recurring supplier invoice is an invoice with a frequency
1304  $sql .= " AND (date_when IS NULL OR date_when <= '".$this->db->idate($today)."')";
1305  $sql .= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1306  $sql .= ' AND suspended = 0';
1307  $sql .= ' AND entity = '. (int) $conf->entity; // MUST STAY = $conf->entity here
1308  if ($restrictioninvoiceid > 0) {
1309  $sql .= ' AND rowid = '. (int) $restrictioninvoiceid;
1310  }
1311  $sql .= $this->db->order('entity', 'ASC');
1312  //print $sql;exit;
1313  $parameters = array(
1314  'restrictioninvoiceid' => $restrictioninvoiceid,
1315  'forcevalidation' => $forcevalidation,
1316  );
1317  $reshook = $hookmanager->executeHooks('beforeCreationOfRecurringInvoices', $parameters, $sql); // note that $sql might be modified by hooks
1318 
1319  $resql = $this->db->query($sql);
1320  if ($resql) {
1321  $i = 0;
1322  $num = $this->db->num_rows($resql);
1323 
1324  if ($num) {
1325  $this->output .= $langs->trans('FoundXQualifiedRecurringInvoiceTemplate', $num)."\n";
1326  } else {
1327  $this->output .= $langs->trans('NoQualifiedRecurringInvoiceTemplateFound');
1328  }
1329 
1330  $saventity = $conf->entity;
1331  $laststep = "None";
1332 
1333  while ($i < $num) { // Loop on each template invoice. If $num = 0, test is false at first pass.
1334  $line = $this->db->fetch_object($resql);
1335 
1336  $this->db->begin();
1337 
1338  $invoiceidgenerated = 0;
1339 
1340  $new_fac_fourn = null;
1341  $facturerec = new FactureFournisseurRec($this->db);
1342  $laststep = "Fetch {$line->rowid}";
1343  $facturerec->fetch($line->rowid);
1344 
1345  if ($facturerec->id > 0) {
1346  // Set entity context
1347  $conf->entity = $facturerec->entity;
1348 
1349  dol_syslog('createRecurringInvoices Process invoice template id=' .$facturerec->id. ', ref=' .$facturerec->ref. ', entity=' .$facturerec->entity);
1350 
1351  $new_fac_fourn = new FactureFournisseur($this->db);
1352  $new_fac_fourn->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice
1353  $new_fac_fourn->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice
1354 
1355  $new_fac_fourn->type = self::TYPE_STANDARD;
1356  $new_fac_fourn->subtype = $facturerec->subtype;
1357  $new_fac_fourn->statut = self::STATUS_DRAFT; // deprecated
1358  $new_fac_fourn->status = self::STATUS_DRAFT;
1359  $new_fac_fourn->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.
1360  $new_fac_fourn->socid = $facturerec->socid;
1361  $new_fac_fourn->lines = $facturerec->lines;
1362  $new_fac_fourn->ref_supplier = $facturerec->ref_supplier;
1363  $new_fac_fourn->model_pdf = $facturerec->model_pdf;
1364  $new_fac_fourn->fk_project = $facturerec->fk_project;
1365  $new_fac_fourn->label = $facturerec->label;
1366  $new_fac_fourn->libelle = $facturerec->label; // deprecated
1367 
1368  $invoiceidgenerated = $new_fac_fourn->create($user);
1369  $laststep = "Create invoiceidgenerated $invoiceidgenerated";
1370  if ($invoiceidgenerated <= 0) {
1371  $this->errors = $new_fac_fourn->errors;
1372  $this->error = $new_fac_fourn->error;
1373  $error++;
1374  }
1375  if (!$error && ($facturerec->auto_validate || $forcevalidation)) {
1376  $result = $new_fac_fourn->validate($user);
1377  $laststep = "Validate by user {$user->login}";
1378  if ($result <= 0) {
1379  $this->errors = $new_fac_fourn->errors;
1380  $this->error = $new_fac_fourn->error;
1381  $error++;
1382  }
1383  }
1384 
1385  if (!$error && $facturerec->generate_pdf) {
1386  // We refresh the object in order to have all necessary data (like date_lim_reglement)
1387  $laststep = "Refresh ".$new_fac_fourn->id;
1388  $new_fac_fourn->fetch($new_fac_fourn->id);
1389  $laststep = "GenerateDocument ".$new_fac_fourn->id;
1390  $result = $new_fac_fourn->generateDocument($facturerec->model_pdf, $langs);
1391  if ($result < 0) {
1392  $this->errors = $new_fac_fourn->errors;
1393  $this->error = $new_fac_fourn->error;
1394  $error++;
1395  }
1396  }
1397  } else {
1398  $error++;
1399  $this->error = 'Failed to load invoice template with id=' .$line->rowid. ', entity=' .$conf->entity."\n";
1400  $this->errors[] = 'Failed to load invoice template with id=' .$line->rowid. ', entity=' .$conf->entity;
1401  dol_syslog('createRecurringInvoices Failed to load invoice template with id=' .$line->rowid. ', entity=' .$conf->entity);
1402  }
1403 
1404  if (!$error && $invoiceidgenerated >= 0) {
1405  $facturerec->nb_gen_done++;
1406  $facturerec->date_last_gen = dol_now();
1407  $facturerec->date_when = $facturerec->getNextDate();
1408  $facturerec->update($user);
1409  $this->db->commit('createRecurringInvoices Process invoice template id=' .$facturerec->id. ', title=' .$facturerec->title);
1410  dol_syslog('createRecurringInvoices Process invoice template ' .$facturerec->title. ' is finished with a success generation');
1411  $nb_create++;
1412  $this->output .= $langs->trans('InvoiceGeneratedFromTemplate', $new_fac_fourn->ref, $facturerec->title)."\n";
1413  } else {
1414  $this->db->rollback('createRecurringInvoices Process invoice template error='.$error.' invoiceidgenerated='.$invoiceidgenerated.' LastStep='.$laststep.' id=' .$facturerec->id. ', title=' .$facturerec->title);
1415  }
1416 
1417  $parameters = array(
1418  'cpt' => $i,
1419  'total' => $num,
1420  'errorCount' => $error,
1421  'invoiceidgenerated' => $invoiceidgenerated,
1422  'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks.
1423  'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks.
1424  );
1425  $reshook = $hookmanager->executeHooks('afterCreationOfRecurringInvoice', $parameters, $new_fac_fourn); // note: $facture can be modified by hooks (warning: $facture can be null)
1426 
1427  $i++;
1428  }
1429 
1430  $conf->entity = $saventity; // Restore entity context
1431  } else {
1432  dol_print_error($this->db);
1433  }
1434 
1435  $this->output = trim($this->output);
1436 
1437  return $error ? $error : 0;
1438  }
1439 
1452  public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1)
1453  {
1454  global $langs, $hookmanager;
1455 
1456  $result = '';
1457 
1458  $label = '<u>'.$langs->trans('RepeatableInvoice').'</u>';
1459  if (!empty($this->ref)) {
1460  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1461  }
1462  if ($this->frequency > 0) {
1463  $label .= '<br><b>'.$langs->trans('Frequency').':</b> '.$langs->trans('FrequencyPer_'.$this->unit_frequency, $this->frequency);
1464  }
1465  if (!empty($this->date_last_gen)) {
1466  $label .= '<br><b>'.$langs->trans('DateLastGeneration').':</b> '.dol_print_date($this->date_last_gen, 'dayhour');
1467  }
1468  if ($this->frequency > 0) {
1469  if (!empty($this->date_when)) {
1470  $label .= '<br><b>'.$langs->trans('NextDateToExecution').':</b> ';
1471  $label .= (empty($this->suspended) ? '' : '<strike>').dol_print_date($this->date_when, 'day').(empty($this->suspended) ? '' : '</strike>'); // No hour for this property
1472  if (!empty($this->suspended)) {
1473  $label .= ' ('.$langs->trans('Disabled').')';
1474  }
1475  }
1476  }
1477 
1478  $url = DOL_URL_ROOT.'/fourn/facture/card-rec.php?facid='.$this->id;
1479 
1480  if ($short) {
1481  return $url;
1482  }
1483 
1484  if ($option != 'nolink') {
1485  // Add param to save lastsearch_values or not
1486  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1487  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER['PHP_SELF'])) {
1488  $add_save_lastsearch_values = 1;
1489  }
1490  if ($add_save_lastsearch_values) {
1491  $url .= '&save_lastsearch_values=1';
1492  }
1493  }
1494 
1495  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1496  $linkend = '</a>';
1497 
1498  $result .= $linkstart;
1499  if ($withpicto) {
1500  $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);
1501  }
1502  if ($withpicto != 2) {
1503  $result .= $this->ref;
1504  }
1505  $result .= $linkend;
1506  global $action;
1507  $hookmanager->initHooks(array($this->element . 'dao'));
1508  $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1509  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1510  if ($reshook > 0) {
1511  $result = $hookmanager->resPrint;
1512  } else {
1513  $result .= $hookmanager->resPrint;
1514  }
1515  return $result;
1516  }
1517 
1525  public function getLibStatut($mode = 0, $alreadypaid = -1)
1526  {
1527  return $this->LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->type) ? 0 : $this->type);
1528  }
1529 
1530  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1541  public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0)
1542  {
1543  // phpcs:enable
1544  global $langs;
1545  $langs->load('bills');
1546 
1547  $labelStatus = $langs->transnoentitiesnoconv('Active');
1548  $statusType = 'status0';
1549 
1550  //print "$recur,$status,$mode,$alreadypaid,$type";
1551  if ($mode == 0) {
1552  if ($recur) {
1553  if ($status == self::STATUS_SUSPENDED) {
1554  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1555  } else {
1556  $labelStatus = $langs->transnoentitiesnoconv('Active');
1557  }
1558  } else {
1559  if ($status == self::STATUS_SUSPENDED) {
1560  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1561  } else {
1562  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1563  }
1564  }
1565  } elseif ($mode == 1) {
1566  $prefix = 'Short';
1567  if ($recur) {
1568  if ($status == self::STATUS_SUSPENDED) {
1569  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1570  } else {
1571  $labelStatus = $langs->transnoentitiesnoconv('Active');
1572  }
1573  } else {
1574  if ($status == self::STATUS_SUSPENDED) {
1575  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1576  } else {
1577  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1578  }
1579  }
1580  } elseif ($mode == 2) {
1581  if ($recur) {
1582  if ($status == self::STATUS_SUSPENDED) {
1583  $statusType = 'status6';
1584  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1585  } else {
1586  $statusType = 'status4';
1587  $labelStatus = $langs->transnoentitiesnoconv('Active');
1588  }
1589  } else {
1590  if ($status == self::STATUS_SUSPENDED) {
1591  $statusType = 'status6';
1592  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1593  } else {
1594  $statusType = 'status0'; // @phan-suppress-current-line PhanPluginRedundantAssignment
1595  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1596  }
1597  }
1598  } elseif ($mode == 3) {
1599  if ($recur) {
1600  $prefix = 'Short';
1601  if ($status == self::STATUS_SUSPENDED) {
1602  $statusType = 'status6';
1603  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1604  } else {
1605  $statusType = 'status4';
1606  $labelStatus = $langs->transnoentitiesnoconv('Active');
1607  }
1608  } else {
1609  if ($status == self::STATUS_SUSPENDED) {
1610  $statusType = 'status6';
1611  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1612  } else {
1613  $statusType = 'status0'; // @phan-suppress-current-line PhanPluginRedundantAssignment
1614  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1615  }
1616  }
1617  } elseif ($mode == 4) {
1618  $prefix = '';
1619  if ($recur) {
1620  if ($status == self::STATUS_SUSPENDED) {
1621  $statusType = 'status6';
1622  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1623  } else {
1624  $statusType = 'status4';
1625  $labelStatus = $langs->transnoentitiesnoconv('Active');
1626  }
1627  } else {
1628  if ($status == self::STATUS_SUSPENDED) {
1629  $statusType = 'status6';
1630  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1631  } else {
1632  $statusType = 'status0'; // @phan-suppress-current-line PhanPluginRedundantAssignment
1633  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1634  }
1635  }
1636  } elseif ($mode == 5 || $mode == 6) {
1637  $prefix = '';
1638  if ($mode == 5) {
1639  $prefix = 'Short';
1640  }
1641  if ($recur) {
1642  if ($status == self::STATUS_SUSPENDED) {
1643  $statusType = 'status6';
1644  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1645  } else {
1646  $statusType = 'status4';
1647  $labelStatus = $langs->transnoentitiesnoconv('Active');
1648  }
1649  } else {
1650  if ($status == self::STATUS_SUSPENDED) {
1651  $statusType = 'status6';
1652  $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1653  } else {
1654  $statusType = 'status0'; // @phan-suppress-current-line PhanPluginRedundantAssignment
1655  $labelStatus = $langs->transnoentitiesnoconv('Draft');
1656  }
1657  }
1658  }
1659 
1660  $labelStatusShort = $labelStatus;
1661 
1662  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1663  }
1664 
1673  public function initAsSpecimen($option = '')
1674  {
1675  global $user, $langs, $conf;
1676 
1677  $now = dol_now();
1678  $arraynow = dol_getdate($now);
1679  $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1680 
1681  // Load array of products prodids
1682  $num_prods = 0;
1683  $prodids = array();
1684 
1685  $sql = 'SELECT rowid';
1686  $sql .= ' FROM ' .MAIN_DB_PREFIX. 'product';
1687  $sql .= ' WHERE entity IN (' .getEntity('product'). ')';
1688  $sql .= $this->db->plimit(100);
1689 
1690  $resql = $this->db->query($sql);
1691  if ($resql) {
1692  $num_prods = $this->db->num_rows($resql);
1693  $i = 0;
1694  while ($i < $num_prods) {
1695  $i++;
1696  $row = $this->db->fetch_row($resql);
1697  $prodids[$i] = $row[0];
1698  }
1699  }
1700 
1701  // Initialize parameters
1702  $this->id = 0;
1703  $this->ref = 'SPECIMEN';
1704  $this->title = 'SPECIMEN';
1705  $this->specimen = 1;
1706  $this->socid = 1;
1707  $this->date = $nownotime;
1708  $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
1709  $this->cond_reglement_id = 1;
1710  $this->cond_reglement_code = 'RECEP';
1711  $this->date_lim_reglement = $this->calculate_date_lim_reglement();
1712  $this->mode_reglement_id = 0; // Not forced to show payment mode CHQ + VIR
1713  $this->mode_reglement_code = ''; // Not forced to show payment mode CHQ + VIR
1714  $this->note_public = 'This is a comment (public)';
1715  $this->note_private = 'This is a comment (private)';
1716  $this->note = 'This is a comment (private)';
1717  $this->fk_incoterms = 0;
1718  $this->location_incoterms = '';
1719 
1720  if (empty($option) || $option != 'nolines') {
1721  // Lines
1722  $nbp = 5;
1723  $xnbp = 0;
1724  while ($xnbp < $nbp) {
1725  $line = new FactureLigne($this->db);
1726  $line->desc = $langs->trans('Description'). ' ' .$xnbp;
1727  $line->qty = 1;
1728  $line->subprice = 100;
1729  $line->tva_tx = 19.6;
1730  $line->localtax1_tx = 0;
1731  $line->localtax2_tx = 0;
1732  $line->remise_percent = 0;
1733  if ($xnbp == 1) { // Qty is negative (product line)
1734  $prodid = mt_rand(1, $num_prods);
1735  $line->fk_product = $prodids[$prodid];
1736  $line->qty = -1;
1737  $line->total_ht = -100;
1738  $line->total_ttc = -119.6;
1739  $line->total_tva = -19.6;
1740  } elseif ($xnbp == 2) { // UP is negative (free line)
1741  $line->subprice = -100;
1742  $line->total_ht = -100;
1743  $line->total_ttc = -119.6;
1744  $line->total_tva = -19.6;
1745  $line->remise_percent = 0;
1746  } elseif ($xnbp == 3) { // Discount is 50% (product line)
1747  $prodid = mt_rand(1, $num_prods);
1748  $line->fk_product = $prodids[$prodid];
1749  $line->total_ht = 50;
1750  $line->total_ttc = 59.8;
1751  $line->total_tva = 9.8;
1752  $line->remise_percent = 50;
1753  } else { // (product line)
1754  $prodid = mt_rand(1, $num_prods);
1755  $line->fk_product = $prodids[$prodid];
1756  $line->total_ht = 100;
1757  $line->total_ttc = 119.6;
1758  $line->total_tva = 19.6;
1759  $line->remise_percent = 00;
1760  }
1761 
1762  $this->lines[$xnbp] = $line;
1763  $xnbp++;
1764 
1765  $this->total_ht += $line->total_ht;
1766  $this->total_tva += $line->total_tva;
1767  $this->total_ttc += $line->total_ttc;
1768  }
1769  $this->revenuestamp = 0;
1770 
1771  // Add a line "offered"
1772  $line = new FactureLigne($this->db);
1773  $line->desc = $langs->trans('Description'). ' (offered line)';
1774  $line->qty = 1;
1775  $line->subprice = 100;
1776  $line->tva_tx = 19.6;
1777  $line->localtax1_tx = 0;
1778  $line->localtax2_tx = 0;
1779  $line->remise_percent = 100;
1780  $line->total_ht = 0;
1781  $line->total_ttc = 0; // 90 * 1.196
1782  $line->total_tva = 0;
1783  $prodid = mt_rand(1, $num_prods);
1784  $line->fk_product = $prodids[$prodid];
1785 
1786  $this->lines[$xnbp] = $line;
1787  $xnbp++;
1788  }
1789 
1790  $this->usenewprice = 0;
1791 
1792  return 1;
1793  }
1794 
1803  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1804  {
1805  $tables = array(
1806  'facture_fourn_rec'
1807  );
1808 
1809  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1810  }
1811 
1819  public function setFrequencyAndUnit($frequency, $unit)
1820  {
1821  if (!$this->table_element) {
1822  dol_syslog(get_class($this). '::setFrequencyAndUnit was called on object with property table_element not defined', LOG_ERR);
1823  return -1;
1824  }
1825 
1826  if (!empty($frequency) && empty($unit)) {
1827  dol_syslog(get_class($this). '::setFrequencyAndUnit was called on object with params frequency defined but unit not defined', LOG_ERR);
1828  return -2;
1829  }
1830 
1831  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1832  $sql .= " SET frequency = ".($frequency ? ((int) $frequency) : "NULL");
1833  if (!empty($unit)) {
1834  $sql .= ", unit_frequency = '".$this->db->escape($unit)."'";
1835  }
1836  $sql .= " WHERE rowid = ".((int) $this->id);
1837 
1838  dol_syslog(get_class($this).'::setFrequencyAndUnit', LOG_DEBUG);
1839 
1840  if ($this->db->query($sql)) {
1841  $this->frequency = $frequency;
1842  if (!empty($unit)) {
1843  $this->unit_frequency = $unit;
1844  }
1845  return 1;
1846  } else {
1847  $this->error = $this->db->lasterror();
1848  return -1;
1849  }
1850  }
1851 
1859  public function setNextDate($date, $increment_nb_gen_done = 0)
1860  {
1861  if (!$this->table_element) {
1862  dol_syslog(get_class($this).'::setNextDate was called on object with property table_element not defined', LOG_ERR);
1863  return -1;
1864  }
1865  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1866  $sql .= " SET date_when = " .($date ? "'".$this->db->idate($date)."'" : "NULL");
1867  if ($increment_nb_gen_done > 0) {
1868  $sql .= ", nb_gen_done = nb_gen_done + 1";
1869  }
1870  $sql .= " WHERE rowid = " . (int) $this->id;
1871 
1872  dol_syslog(get_class($this).'::setNextDate', LOG_DEBUG);
1873 
1874  if ($this->db->query($sql)) {
1875  $this->date_when = $date;
1876  if ($increment_nb_gen_done > 0) {
1877  $this->nb_gen_done++;
1878  }
1879  return 1;
1880  } else {
1881  $this->error = $this->db->lasterror();
1882  return -1;
1883  }
1884  }
1885 
1892  public function setMaxPeriod($nb)
1893  {
1894  if (!$this->table_element) {
1895  dol_syslog(get_class($this).'::setMaxPeriod was called on object with property table_element not defined', LOG_ERR);
1896  return -1;
1897  }
1898 
1899  if (empty($nb)) {
1900  $nb = 0;
1901  }
1902 
1903  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1904  $sql .= " SET nb_gen_max = ". (int) $nb;
1905  $sql .= " WHERE rowid = " . (int) $this->id;
1906 
1907  dol_syslog(get_class($this).'::setMaxPeriod', LOG_DEBUG);
1908 
1909  if ($this->db->query($sql)) {
1910  $this->nb_gen_max = $nb;
1911  return 1;
1912  } else {
1913  dol_print_error($this->db);
1914  return -1;
1915  }
1916  }
1917 
1924  public function setAutoValidate($validate)
1925  {
1926  if (!$this->table_element) {
1927  dol_syslog(get_class($this).'::setAutoValidate was called on object with property table_element not defined', LOG_ERR);
1928  return -1;
1929  }
1930 
1931  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1932  $sql .= " SET auto_validate = ".((int) $validate);
1933  $sql .= " WHERE rowid = " . (int) $this->id;
1934 
1935  dol_syslog(get_class($this).'::setAutoValidate', LOG_DEBUG);
1936 
1937  if ($this->db->query($sql)) {
1938  $this->auto_validate = $validate;
1939  return 1;
1940  } else {
1941  dol_print_error($this->db);
1942  return -1;
1943  }
1944  }
1945 
1952  public function setGeneratePdf($validate)
1953  {
1954  if (!$this->table_element) {
1955  dol_syslog(get_class($this).'::setGeneratePdf was called on object with property table_element not defined', LOG_ERR);
1956  return -1;
1957  }
1958 
1959  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1960  $sql .= " SET generate_pdf = ". (int) $validate;
1961  $sql .= " WHERE rowid = " . (int) $this->id;
1962 
1963  dol_syslog(get_class($this).'::setGeneratePdf', LOG_DEBUG);
1964 
1965  if ($this->db->query($sql)) {
1966  $this->generate_pdf = $validate;
1967  return 1;
1968  } else {
1969  dol_print_error($this->db);
1970  return -1;
1971  }
1972  }
1973 
1980  public function setModelPdf($model)
1981  {
1982  if (!$this->table_element) {
1983  dol_syslog(get_class($this).'::setModelPdf was called on object with property table_element not defined', LOG_ERR);
1984  return -1;
1985  }
1986 
1987  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1988  $sql .= " SET modelpdf = '".$this->db->escape($model)."'";
1989  $sql .= " WHERE rowid = " . (int) $this->id;
1990 
1991  dol_syslog(get_class($this).'::setModelPdf', LOG_DEBUG);
1992 
1993  if ($this->db->query($sql)) {
1994  $this->model_pdf = $model;
1995  return 1;
1996  } else {
1997  dol_print_error($this->db);
1998  return -1;
1999  }
2000  }
2001 }
2002 
2003 
2004 
2010 {
2014  public $element = 'invoice_supplier_det_rec';
2015 
2019  public $table_element = 'facture_fourn_det_rec';
2020 
2021  public $fk_facture_fourn;
2022  public $fk_parent;
2023  public $fk_product;
2024  public $ref_supplier;
2025  public $label;
2030  public $pu_ht;
2031  public $pu_ttc;
2032  public $qty;
2033  public $remise_percent;
2034  public $fk_remise_except;
2035  public $vat_src_code;
2036  public $tva_tx;
2037  public $localtax1_tx;
2038  public $localtax1_type;
2039  public $localtax2_tx;
2040  public $localtax2_type;
2041 
2042  public $product_type;
2043  public $date_start;
2044  public $date_end;
2045  public $info_bits;
2046 
2050  public $special_code;
2051  public $rang;
2052 
2053  public $fk_user_author;
2054  public $fk_user_modif;
2055 
2056 
2064  public function delete(User $user, $notrigger = 0)
2065  {
2066  $error = 0;
2067  $this->db->begin();
2068 
2069  if (! $error) {
2070  if (! $notrigger) {
2071  // Call triggers
2072  $result = $this->call_trigger('LINESUPPLIERBILLREC_DELETE', $user);
2073  if ($result < 0) {
2074  $error++;
2075  } // Do also here what you must do to rollback action if trigger fail
2076  // End call triggers
2077  }
2078  }
2079 
2080  if (! $error) {
2081  $result = $this->deleteExtraFields();
2082  if ($result < 0) {
2083  $error++;
2084  }
2085  }
2086 
2087  if (! $error) {
2088  $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element . ' WHERE rowid=' . (int) $this->id;
2089 
2090  $res = $this->db->query($sql);
2091  if ($res === false) {
2092  $error++;
2093  $this->errors[] = $this->db->lasterror();
2094  }
2095  }
2096 
2097  // Commit or rollback
2098  if ($error) {
2099  $this->db->rollback();
2100  return -1;
2101  } else {
2102  $this->db->commit();
2103  return 1;
2104  }
2105  }
2106 
2107 
2114  public function fetch($rowid)
2115  {
2116  $sql = 'SELECT l.rowid,';
2117  $sql .= ' l.fk_facture_fourn, l.fk_parent_line, l.fk_product,';
2118  $sql .= ' l.ref as ref_supplier, l.label, l.description as line_desc, l.pu_ht, l.pu_ttc, l.qty, l.remise_percent, l.fk_remise_except,';
2119  $sql .= ' l.vat_src_code, l.tva_tx, l.localtax1_tx, l.localtax1_type, l.localtax2_tx, l.localtax2_type,';
2120  $sql .= ' l.total_ht, l.total_tva, l.total_localtax1, l.total_localtax2, l.total_ttc,';
2121  $sql .= ' l.product_type, l.date_start, l.date_end,';
2122  $sql .= ' l.info_bits, l.special_code, l.rang, l.fk_unit, l.import_key,';
2123  $sql .= ' l.fk_user_author, l.fk_user_modif, l.fk_multicurrency,';
2124  $sql .= ' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2125  $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2126  $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_det_rec as l';
2127  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
2128  $sql .= ' WHERE l.rowid = '. (int) $rowid;
2129  $sql .= ' ORDER BY l.rang';
2130 
2131  dol_syslog('FactureRec::fetch', LOG_DEBUG);
2132  $result = $this->db->query($sql);
2133  if ($result) {
2134  $objp = $this->db->fetch_object($result);
2135 
2136  $this->id = $objp->rowid;
2137  $this->fk_facture_fourn = $objp->fk_facture_fourn;
2138  $this->fk_parent = $objp->fk_parent_line;
2139  $this->fk_product = $objp->fk_product;
2140  $this->ref_supplier = $objp->ref_supplier;
2141  $this->label = $objp->label;
2142  $this->description = $objp->line_desc;
2143  $this->desc = $objp->line_desc;
2144  $this->pu_ht = $objp->pu_ht;
2145  $this->pu_ttc = $objp->pu_ttc;
2146  $this->qty = $objp->qty;
2147  $this->remise_percent = $objp->remise_percent;
2148  $this->fk_remise_except = $objp->fk_remise_except;
2149  $this->vat_src_code = $objp->vat_src_code;
2150  $this->tva_tx = $objp->tva_tx;
2151  $this->localtax1_tx = $objp->localtax1_tx;
2152  $this->localtax1_type = $objp->localtax1_type;
2153  $this->localtax2_tx = $objp->localtax2_tx;
2154  $this->localtax2_type = $objp->localtax2_type;
2155  $this->total_ht = $objp->total_ht;
2156  $this->total_tva = $objp->total_tva;
2157  $this->total_localtax1 = $objp->total_localtax1;
2158  $this->total_localtax2 = $objp->total_localtax2;
2159  $this->total_ttc = $objp->total_ttc;
2160  $this->product_type = $objp->product_type;
2161  $this->date_start = $objp->date_start;
2162  $this->date_end = $objp->date_end;
2163  $this->info_bits = $objp->info_bits;
2164  $this->special_code = $objp->special_code;
2165  $this->rang = $objp->rang;
2166  $this->fk_unit = $objp->fk_unit;
2167  $this->import_key = $objp->import_key;
2168  $this->fk_user_author = $objp->fk_user_author;
2169  $this->fk_user_modif = $objp->fk_user_modif;
2170  $this->fk_multicurrency = $objp->fk_multicurrency;
2171  $this->multicurrency_code = $objp->multicurrency_code;
2172  $this->multicurrency_subprice = $objp->multicurrency_subprice;
2173  $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2174  $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2175  $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2176 
2177  $this->db->free($result);
2178  return 1;
2179  } else {
2180  $this->error = $this->db->lasterror();
2181  return -3;
2182  }
2183  }
2184 
2185 
2193  public function update(User $user, $notrigger = 0)
2194  {
2195  global $conf;
2196 
2197  $error = 0;
2198 
2199  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2200 
2201  $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture_fourn_det_rec SET';
2202  $sql .= ' fk_facture_fourn = ' . (int) $this->fk_facture_fourn;
2203  $sql .= ', fk_parent_line = ' . (int) $this->fk_parent;
2204  $sql .= ', fk_product = ' . (int) $this->fk_product;
2205  $sql .= ', ref = ' . (!empty($this->ref) ? "'" . $this->db->escape($this->ref) . "'" : 'NULL');
2206  $sql .= ", label = " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : 'NULL');
2207  $sql .= ", description = '" . $this->db->escape($this->desc ? $this->desc : $this->description) . "'";
2208  $sql .= ', pu_ht = ' . price2num($this->pu_ht);
2209  $sql .= ', pu_ttc = ' . price2num($this->pu_ttc);
2210  $sql .= ', qty = ' . price2num($this->qty);
2211  $sql .= ", remise_percent = '" . price2num($this->remise_percent) . "'";
2212  $sql .= ', fk_remise_except = ' . (int) $this->fk_remise_except;
2213  $sql .= ", vat_src_code = '" . $this->db->escape($this->vat_src_code) . "'";
2214  $sql .= ', tva_tx = ' . price2num($this->tva_tx);
2215  $sql .= ', localtax1_tx = ' . price2num($this->localtax1_tx);
2216  $sql .= ", localtax1_type = '" . $this->db->escape($this->localtax1_type) . "'";
2217  $sql .= ', localtax2_tx = ' . price2num($this->localtax2_tx);
2218  $sql .= ", localtax2_type = '" . $this->db->escape($this->localtax2_type) . "'";
2219  if (empty($this->skip_update_total)) {
2220  $sql .= ', total_ht = ' . price2num($this->total_ht);
2221  $sql .= ', total_tva = ' . price2num($this->total_tva);
2222  $sql .= ', total_localtax1 = ' . price2num($this->total_localtax1);
2223  $sql .= ', total_localtax2 = ' . price2num($this->total_localtax2);
2224  $sql .= ', total_ttc = ' . price2num($this->total_ttc);
2225  }
2226  $sql .= ', product_type = ' . (int) $this->product_type;
2227  $sql .= ', date_start = ' . (int) $this->date_start;
2228  $sql .= ', date_end = ' . (int) $this->date_end;
2229  $sql .= ", info_bits = " . ((int) $this->info_bits);
2230  $sql .= ', special_code =' . (int) $this->special_code;
2231  $sql .= ', rang = ' . (int) $this->rang;
2232  $sql .= ', fk_unit = ' .($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : 'null');
2233  $sql .= ', fk_user_modif = ' . (int) $user->id;
2234  $sql .= ' WHERE rowid = ' . (int) $this->id;
2235 
2236  $this->db->begin();
2237 
2238  dol_syslog(get_class($this). '::updateline', LOG_DEBUG);
2239  $resql = $this->db->query($sql);
2240  if ($resql) {
2241  if (!$error) {
2242  $result = $this->insertExtraFields();
2243  if ($result < 0) {
2244  $error++;
2245  }
2246  }
2247 
2248  if (!$error && !$notrigger) {
2249  // Call trigger
2250  $result = $this->call_trigger('LINESUPPLIERBILLREC_MODIFY', $user);
2251  if ($result < 0) {
2252  $error++;
2253  }
2254  // End call triggers
2255  }
2256 
2257  if ($error) {
2258  $this->db->rollback();
2259  return -2;
2260  } else {
2261  $this->db->commit();
2262  return 1;
2263  }
2264  } else {
2265  $this->error = $this->db->lasterror();
2266  $this->db->rollback();
2267  return -2;
2268  }
2269  }
2270 }
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition: security.php:604
$object ref
Definition: info.php:79
Superclass for invoices classes.
const TYPE_STANDARD
Standard invoice.
calculate_date_lim_reglement($cond_reglement=0)
Returns an invoice payment deadline based on the invoice settlement conditions and billing date.
const STATUS_DRAFT
Draft status.
Parent class of all other business classes for details of elements (invoices, contracts,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
update_price($exclspec=0, $roundingadjust='auto', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage suppliers invoices.
Class to manage supplier invoice lines of templates.
fetch($rowid)
Get line of template invoice.
update(User $user, $notrigger=0)
Update a line to supplier invoice template .
Class to manage invoice templates.
setModelPdf($model)
Update the model for documents.
fetch($rowid, $ref='', $ref_ext='')
Load object and lines.
strikeIfMaxNbGenReached($ret)
Format string to output with by striking the string if max number of generation was reached.
setFrequencyAndUnit($frequency, $unit)
Update frequency and unit.
getNextNumRef($soc, $mode='next')
Return next reference of invoice not already used (or last reference)
create($user, $facFournId, $notrigger=0)
Create a predefined supplier invoice.
setGeneratePdf($validate)
Update the auto generate documents.
getLinesArray()
Create an array of invoice lines.
initAsSpecimen($option='')
Initialise an instance with random values.
LibStatut($recur, $status, $mode=0, $alreadypaid=-1, $type=0)
Return label of a status.
updateline($rowid, $fk_product, $ref, $label, $desc, $pu_ht, $qty, $remise_percent, $txtva, $txlocaltax1=0, $txlocaltax2=0, $price_base_type='HT', $type=0, $date_start=0, $date_end=0, $info_bits=0, $special_code=0, $rang=-1, $fk_unit=null, $pu_ht_devise=0, $pu_ttc=0)
Update a line to supplier invoice template.
isMaxNbGenReached()
Return if maximum number of generation is reached.
setAutoValidate($validate)
Update the auto validate flag of invoice.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getNextDate()
Return the next date of.
fetch_lines()
Get lines of template invoices into this->lines.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of object status.
setNextDate($date, $increment_nb_gen_done=0)
Update the next date of execution.
setMaxPeriod($nb)
Update the maximum period.
createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0)
Create all recurrents supplier invoices (for all entities if multicompany is used).
update(User $user, $notrigger=0)
Update fourn_invoice_rec.
addline($fk_product, $ref, $label, $desc, $pu_ht, $pu_ttc, $qty, $remise_percent, $txtva, $txlocaltax1=0, $txlocaltax2=0, $price_base_type='HT', $type=0, $date_start=0, $date_end=0, $info_bits=0, $special_code=0, $rang=-1, $fk_unit=null, $pu_ht_devise=0)
Add a line to recursive supplier invoice.
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Class to manage invoice lines.
Class to manage products or services.
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:123
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
div float
Buy price without taxes.
Definition: style.css.php:959
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:88
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:122