dolibarr 21.0.3
facture-rec.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
7 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2017-2024 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2023 Nick Fragoulis
11 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
33require_once DOL_DOCUMENT_ROOT.'/core/class/commoninvoice.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
35require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
36require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
37require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/factureligne.class.php';
38require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
39
40
45{
46 const TRIGGER_PREFIX = 'BILLREC';
50 public $element = 'facturerec';
51
55 public $table_element = 'facture_rec';
56
60 public $table_element_line = 'facturedet_rec';
61
65 public $fk_element = 'fk_facture';
66
70 public $picto = 'bill';
71
75 public $entity;
76
80 protected $table_ref_field = 'titre';
81
85 public $title;
86
91 public $titre;
92
96 public $multicurrency_subprice;
100 public $socid;
104 public $number;
108 public $date;
109 //public $remise;
110 //public $remise_absolue;
111 //public $remise_percent;
112
118 public $total;
119
125 public $tva;
126
130 public $date_last_gen;
134 public $date_when;
138 public $nb_gen_done;
142 public $nb_gen_max;
143
147 public $user_author;
148
152 public $frequency;
153
157 public $unit_frequency;
158
162 public $rang;
163
167 public $special_code;
168
172 public $usenewprice = 0;
173
177 public $date_lim_reglement;
181 public $cond_reglement_code; // Code in llx_c_paiement
185 public $mode_reglement_code; // Code in llx_c_paiement
186
190 public $fk_societe_rib;
191
195 public $suspended; // status
196
200 public $auto_validate; // 0 to create in draft, 1 to create and validate the new invoice
204 public $generate_pdf; // 1 to generate PDF on invoice generation (default)
205
206
207
208 const PAYMENTCODETOEDITSOCIETERIB = "PRE";
209
210
235 // BEGIN MODULEBUILDER PROPERTIES
239 public $fields = array(
240 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
241 'titre' => array('type' => 'varchar(100)', 'label' => 'Titre', 'enabled' => 1, 'showoncombobox' => 1, 'visible' => -1, 'position' => 15),
242 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 20, 'index' => 1),
243 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 25),
244 'subtype' => array('type' => 'smallint(6)', 'label' => 'InvoiceSubtype', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
245 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 35),
246 'total_tva' => array('type' => 'double(24,8)', 'label' => 'Tva', 'enabled' => 1, 'visible' => -1, 'position' => 55, 'isameasure' => 1),
247 'localtax1' => array('type' => 'double(24,8)', 'label' => 'Localtax1', 'enabled' => 1, 'visible' => -1, 'position' => 60, 'isameasure' => 1),
248 'localtax2' => array('type' => 'double(24,8)', 'label' => 'Localtax2', 'enabled' => 1, 'visible' => -1, 'position' => 65, 'isameasure' => 1),
249 'total_ht' => array('type' => 'double(24,8)', 'label' => 'Total', 'enabled' => 1, 'visible' => -1, 'position' => 70, 'isameasure' => 1),
250 'total_ttc' => array('type' => 'double(24,8)', 'label' => 'Total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 75, 'isameasure' => 1),
251 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 80),
252 '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),
253 'fk_cond_reglement' => array('type' => 'integer', 'label' => 'Fk cond reglement', 'enabled' => 1, 'visible' => -1, 'position' => 90),
254 'fk_mode_reglement' => array('type' => 'integer', 'label' => 'Fk mode reglement', 'enabled' => 1, 'visible' => -1, 'position' => 95),
255 'date_lim_reglement' => array('type' => 'date', 'label' => 'Date lim reglement', 'enabled' => 1, 'visible' => -1, 'position' => 100),
256 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 105),
257 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 110),
258 'modelpdf' => array('type' => 'varchar(255)', 'label' => 'Modelpdf', 'enabled' => 1, 'visible' => -1, 'position' => 115),
259 'date_when' => array('type' => 'datetime', 'label' => 'Date when', 'enabled' => 1, 'visible' => -1, 'position' => 130),
260 'date_last_gen' => array('type' => 'datetime', 'label' => 'Date last gen', 'enabled' => 1, 'visible' => -1, 'position' => 135),
261 'nb_gen_done' => array('type' => 'integer', 'label' => 'Nb gen done', 'enabled' => 1, 'visible' => -1, 'position' => 140),
262 'nb_gen_max' => array('type' => 'integer', 'label' => 'Nb gen max', 'enabled' => 1, 'visible' => -1, 'position' => 145),
263 'frequency' => array('type' => 'integer', 'label' => 'Frequency', 'enabled' => 1, 'visible' => -1, 'position' => 150),
264 'unit_frequency' => array('type' => 'varchar(2)', 'label' => 'UnitFrequency', 'enabled' => 1, 'visible' => -1, 'position' => 152),
265 'usenewprice' => array('type' => 'integer', 'label' => 'UseNewPrice', 'enabled' => 1, 'visible' => 0, 'position' => 155),
266 'revenuestamp' => array('type' => 'double(24,8)', 'label' => 'RevenueStamp', 'enabled' => 1, 'visible' => -1, 'position' => 160, 'isameasure' => 1),
267 'auto_validate' => array('type' => 'integer', 'label' => 'Auto validate', 'enabled' => 1, 'visible' => -1, 'position' => 165),
268 'generate_pdf' => array('type' => 'integer', 'label' => 'Generate pdf', 'enabled' => 1, 'visible' => -1, 'position' => 170),
269 'fk_account' => array('type' => 'integer', 'label' => 'Fk account', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 175),
270 'fk_multicurrency' => array('type' => 'integer', 'label' => 'Fk multicurrency', 'enabled' => 1, 'visible' => -1, 'position' => 180),
271 'multicurrency_code' => array('type' => 'varchar(255)', 'label' => 'Multicurrency code', 'enabled' => 1, 'visible' => -1, 'position' => 185),
272 'multicurrency_tx' => array('type' => 'double(24,8)', 'label' => 'Multicurrency tx', 'enabled' => 1, 'visible' => -1, 'position' => 190, 'isameasure' => 1),
273 'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ht', 'enabled' => 1, 'visible' => -1, 'position' => 195, 'isameasure' => 1),
274 'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total tva', 'enabled' => 1, 'visible' => -1, 'position' => 200, 'isameasure' => 1),
275 'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 205, 'isameasure' => 1),
276 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 210),
277 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 215),
278 'suspended' => array('type' => 'integer', 'label' => 'Suspended', 'enabled' => 1, 'visible' => -1, 'position' => 225),
279 'fk_societe_rib' => array('type' => 'integer', 'label' => 'Fk Societe RIB', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 175),
280 );
281 // END MODULEBUILDER PROPERTIES
282
283 const STATUS_NOTSUSPENDED = 0;
284 const STATUS_SUSPENDED = 1;
285
286
287
293 public function __construct(DoliDB $db)
294 {
295 $this->db = $db;
296 }
297
307 public function create($user, $facid, $notrigger = 0, $onlylines = array())
308 {
309 global $conf;
310
311 $error = 0;
312 $now = dol_now();
313
314 // Clean parameters
315 $this->titre = trim(isset($this->titre) ? $this->titre : $this->title); // deprecated
316 $this->title = trim($this->title);
317 $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
318 if (empty($this->suspended)) {
319 $this->suspended = 0;
320 }
321
322 // No frequency defined then no next date to execution
323 if (empty($this->frequency)) {
324 $this->frequency = 0;
325 $this->date_when = null;
326 }
327
328 $this->frequency = abs($this->frequency);
329 $this->nb_gen_done = 0;
330 $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
331 $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
332 $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
333
334 $this->db->begin();
335
336 // Load invoice template
337 $facsrc = new Facture($this->db);
338 $result = $facsrc->fetch($facid);
339 if ($result > 0) {
340 $this->socid = $facsrc->socid;
341
342 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_rec (";
343 $sql .= "titre";
344 $sql .= ", fk_soc";
345 $sql .= ", subtype";
346 $sql .= ", entity";
347 $sql .= ", datec";
348 $sql .= ", amount";
349 //$sql .= ", remise";
350 $sql .= ", note_private";
351 $sql .= ", note_public";
352 $sql .= ", modelpdf";
353 $sql .= ", fk_user_author";
354 $sql .= ", fk_projet";
355 $sql .= ", fk_account";
356 $sql .= ", fk_cond_reglement";
357 $sql .= ", fk_mode_reglement";
358 $sql .= ", usenewprice";
359 $sql .= ", frequency";
360 $sql .= ", unit_frequency";
361 $sql .= ", date_when";
362 $sql .= ", date_last_gen";
363 $sql .= ", nb_gen_done";
364 $sql .= ", nb_gen_max";
365 $sql .= ", auto_validate";
366 $sql .= ", generate_pdf";
367 $sql .= ", fk_multicurrency";
368 $sql .= ", multicurrency_code";
369 $sql .= ", multicurrency_tx";
370 $sql .= ", suspended";
371 $sql .= ", fk_societe_rib";
372 $sql .= ") VALUES (";
373 $sql .= "'".$this->db->escape($this->titre ? $this->titre : $this->title)."'";
374 $sql .= ", ".((int) $this->socid);
375 $sql .= ", ".($this->subtype ? "'".$this->db->escape($this->subtype)."'" : "null");
376 $sql .= ", ".((int) $conf->entity);
377 $sql .= ", '".$this->db->idate($now)."'";
378 $sql .= ", ".(!empty($facsrc->total_ttc) ? ((float) $facsrc->total_ttc) : '0');
379 //$sql .= ", ".(!empty($facsrc->remise_absolue) ? ((float) $this->remise_absolue) : '0');
380 $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
381 $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
382 $sql .= ", ".(!empty($this->model_pdf) ? ("'".$this->db->escape($this->model_pdf)."'") : "NULL");
383 $sql .= ", ".((int) $user->id);
384 $sql .= ", ".(!empty($this->fk_project) ? ((int) $this->fk_project) : "null");
385 $sql .= ", ".(!empty($facsrc->fk_account) ? ((int) $facsrc->fk_account) : "null");
386 $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : "null");
387 $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "null");
388 $sql .= ", ".((int) $this->usenewprice);
389 $sql .= ", ".((int) $this->frequency);
390 $sql .= ", '".$this->db->escape($this->unit_frequency)."'";
391 $sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'NULL');
392 $sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'NULL');
393 $sql .= ", ".((int) $this->nb_gen_done);
394 $sql .= ", ".((int) $this->nb_gen_max);
395 $sql .= ", ".((int) $this->auto_validate);
396 $sql .= ", ".((int) $this->generate_pdf);
397 $sql .= ", ".((int) $facsrc->fk_multicurrency);
398 $sql .= ", '".$this->db->escape($facsrc->multicurrency_code)."'";
399 $sql .= ", ".((float) $facsrc->multicurrency_tx);
400 $sql .= ", ".((int) $this->suspended);
401 $sql .= ", ".(!empty($this->fk_societe_rib) ? ((int) $this->fk_societe_rib) : 'NULL');
402 $sql .= ")";
403
404 if ($this->db->query($sql)) {
405 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec");
406
407 // Fields used into addline later
408 $this->fk_multicurrency = $facsrc->fk_multicurrency;
409 $this->multicurrency_code = $facsrc->multicurrency_code;
410 $this->multicurrency_tx = $facsrc->multicurrency_tx;
411
412 // Add lines
413 $fk_parent_line = 0;
414
415 $num = count($facsrc->lines);
416 for ($i = 0; $i < $num; $i++) {
417 $facline = $facsrc->lines[$i];
418 '@phan-var-force FactureLigneRec $facline';
419 if (!empty($onlylines) && !in_array($facline->id, $onlylines)) {
420 continue; // Skip unselected lines
421 }
422
423 // Reset fk_parent_line for no child products and special product
424 if (($facline->product_type != 9 && empty($facline->fk_parent_line)) || $facline->product_type == 9) {
425 $fk_parent_line = 0;
426 }
427
428 $tva_tx = $facline->tva_tx;
429 if (!empty($facline->vat_src_code) && !preg_match('/\‍(/', (string) $tva_tx)) {
430 $tva_tx .= ' ('.$facline->vat_src_code.')';
431 }
432
433 $default_start_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_START');
434 $default_end_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_END');
435
436 $result_insert = $this->addline(
437 $facline->desc,
438 $facline->subprice,
439 $facline->qty,
440 $tva_tx,
441 $facline->localtax1_tx,
442 $facline->localtax2_tx,
443 $facline->fk_product,
444 $facline->remise_percent,
445 'HT',
446 $facline->info_bits,
447 0,
448 0,
449 $facline->product_type,
450 $facline->rang,
451 $facline->special_code,
452 $facline->label,
453 $facline->fk_unit,
454 $facline->multicurrency_subprice,
455 $default_start_fill,
456 $default_end_fill,
457 0,
458 $facline->pa_ht,
459 $fk_parent_line
460 );
461
462 // Defined the new fk_parent_line
463 if ($result_insert > 0 && $facline->product_type == 9) {
464 $fk_parent_line = $result_insert;
465 }
466
467 if ($result_insert < 0) {
468 $error++;
469 } else {
470 $objectline = new FactureLigneRec($this->db);
471
472 $result2 = $objectline->fetch($result_insert);
473 if ($result2 > 0) {
474 // Extrafields
475 if (method_exists($facline, 'fetch_optionals')) {
476 $facline->fetch_optionals($facline->id);
477 $objectline->array_options = $facline->array_options;
478 }
479
480 $result = $objectline->insertExtraFields();
481 if ($result < 0) {
482 $error++;
483 }
484 } elseif ($result2 < 0) {
485 $this->errors[] = $objectline->error;
486 $error++;
487 }
488 }
489 }
490
491 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
492 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
493 }
494
495 // Add object linked
496 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
497 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
498 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, ...))
499 foreach ($tmp_origin_id as $origin_id) {
500 $ret = $this->add_object_linked($origin, $origin_id);
501 if (!$ret) {
502 $this->error = $this->db->lasterror();
503 $error++;
504 }
505 }
506 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
507 $origin_id = $tmp_origin_id;
508 $ret = $this->add_object_linked($origin, $origin_id);
509 if (!$ret) {
510 $this->error = $this->db->lasterror();
511 $error++;
512 }
513 }
514 }
515 }
516
517 if (!$error) {
518 $result = $this->insertExtraFields();
519 if ($result < 0) {
520 $error++;
521 }
522 }
523
524 if (!$error && !$notrigger) {
525 // Call trigger
526 $result = $this->call_trigger('BILLREC_CREATE', $user);
527 if ($result < 0) {
528 $this->db->rollback();
529 return -2;
530 }
531 // End call triggers
532 }
533
534 if ($error) {
535 $this->db->rollback();
536 return -3;
537 } else {
538 $this->db->commit();
539 return $this->id;
540 }
541 } else {
542 $this->error = $this->db->lasterror();
543 $this->db->rollback();
544 return -2;
545 }
546 } else {
547 $this->db->rollback();
548 return -1;
549 }
550 }
551
552
560 public function update(User $user, $notrigger = 0)
561 {
562 $error = 0;
563
564 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec SET";
565 $sql .= " entity = ".((int) $this->entity).",";
566 $sql .= " titre = '".$this->db->escape($this->title)."',";
567 $sql .= " suspended = ".((int) $this->suspended).",";
568 $sql .= " fk_soc = ".((int) $this->socid).",";
569 $sql .= " total_tva = ".((float) $this->total_tva).",";
570 $sql .= " localtax1 = ".((float) $this->total_localtax1).",";
571 $sql .= " localtax2 = ".((float) $this->total_localtax2).",";
572 $sql .= " total_ht = ".((float) $this->total_ht).",";
573 $sql .= " total_ttc = ".((float) $this->total_ttc).",";
574 $sql .= " fk_societe_rib = ".(!empty($this->fk_societe_rib) ? ((int) $this->fk_societe_rib) : 'NULL');
575
576 // TODO Add missing fields
577 $sql .= " WHERE rowid = ".((int) $this->id);
578
579 $this->db->begin();
580
581 dol_syslog(get_class($this)."::update", LOG_DEBUG);
582
583 $resql = $this->db->query($sql);
584 if ($resql) {
585 if (!$error) {
586 $result = $this->insertExtraFields();
587 if ($result < 0) {
588 $error++;
589 }
590 }
591
592 if (!$error && !$notrigger) {
593 // Call trigger
594 $result = $this->call_trigger('BILLREC_MODIFY', $user);
595 if ($result < 0) {
596 $this->db->rollback();
597 return -2;
598 }
599 // End call triggers
600 }
601 $this->db->commit();
602 return 1;
603 } else {
604 $this->error = $this->db->lasterror();
605 $this->db->rollback();
606 return -1;
607 }
608 }
609
620 public function fetch($rowid, $ref = '', $ref_ext = '', $noextrafields = 0, $nolines = 0)
621 {
622 dol_syslog('FactureRec::fetch', LOG_DEBUG);
623
624 $sql = 'SELECT f.rowid, f.entity, f.titre as title, f.suspended, f.fk_soc, f.subtype, f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc';
625 $sql .= ', f.date_lim_reglement as dlr';
626 $sql .= ', f.note_private, f.note_public, f.fk_user_author';
627 $sql .= ', f.modelpdf as model_pdf';
628 $sql .= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project';
629 $sql .= ', f.fk_account, f.fk_societe_rib';
630 $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';
631 $sql .= ', f.generate_pdf';
632 $sql .= ", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
633 $sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
634 $sql .= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
635 //$sql.= ', el.fk_source';
636 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as f';
637 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
638 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id';
639 //$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = f.rowid AND el.targettype = 'facture'";
640 $sql .= ' WHERE f.entity IN ('.getEntity('invoice').')';
641 if ($rowid) {
642 $sql .= ' AND f.rowid = '.((int) $rowid);
643 } elseif ($ref) {
644 $sql .= " AND f.titre = '".$this->db->escape($ref)."'";
645 } else {
646 $sql .= ' AND f.rowid = 0';
647 }
648 /* This field are not used for template invoice
649 if ($ref_ext) $sql.= " AND f.ref_ext='".$this->db->escape($ref_ext)."'";
650 */
651
652 $result = $this->db->query($sql);
653 if ($result) {
654 if ($this->db->num_rows($result)) {
655 $obj = $this->db->fetch_object($result);
656
657 $this->id = $obj->rowid;
658 $this->entity = $obj->entity;
659 $this->titre = $obj->title; // deprecated
660 $this->title = $obj->title;
661 $this->ref = $obj->title;
662 $this->subtype = $obj->subtype;
663 $this->suspended = $obj->suspended;
664 $this->total_ht = $obj->total_ht;
665 $this->total_tva = $obj->total_tva;
666 $this->total_localtax1 = $obj->localtax1;
667 $this->total_localtax2 = $obj->localtax2;
668 $this->total_ttc = $obj->total_ttc;
669 $this->socid = $obj->fk_soc;
670 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
671 $this->mode_reglement_id = $obj->fk_mode_reglement;
672 $this->mode_reglement_code = $obj->mode_reglement_code;
673 $this->mode_reglement = $obj->mode_reglement_libelle;
674 $this->cond_reglement_id = $obj->fk_cond_reglement;
675 $this->cond_reglement_code = $obj->cond_reglement_code;
676 $this->cond_reglement = $obj->cond_reglement_libelle;
677 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
678 $this->fk_project = $obj->fk_project;
679 $this->fk_account = $obj->fk_account;
680 $this->fk_societe_rib = $obj->fk_societe_rib;
681 $this->note_private = $obj->note_private;
682 $this->note_public = $obj->note_public;
683 $this->user_author = $obj->fk_user_author; // deprecated
684 $this->user_creation_id = $obj->fk_user_author;
685 $this->model_pdf = $obj->model_pdf;
686 //$this->special_code = $obj->special_code;
687 $this->frequency = $obj->frequency;
688 $this->unit_frequency = $obj->unit_frequency;
689 $this->date_when = $this->db->jdate($obj->date_when);
690 $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
691 $this->nb_gen_done = $obj->nb_gen_done;
692 $this->nb_gen_max = $obj->nb_gen_max;
693 $this->usenewprice = $obj->usenewprice;
694 $this->auto_validate = $obj->auto_validate;
695 $this->generate_pdf = $obj->generate_pdf;
696
697 // Multicurrency
698 $this->fk_multicurrency = $obj->fk_multicurrency;
699 $this->multicurrency_code = $obj->multicurrency_code;
700 $this->multicurrency_tx = $obj->multicurrency_tx;
701 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
702 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
703 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
704
705 // Retrieve all extrafield
706 // fetch optionals attributes and labels
707 if (empty($noextrafields)) {
708 $result = $this->fetch_optionals();
709 if ($result < 0) {
710 $this->error = $this->db->lasterror();
711 return -4;
712 }
713 }
714
715 // Retrieve lines
716 if (empty($nolines)) {
717 $result = $this->fetch_lines();
718 if ($result < 0) {
719 $this->error = $this->db->lasterror();
720 return -3;
721 }
722 }
723
724 return 1;
725 } else {
726 $this->error = 'Bill with id '.$rowid.' or ref '.$ref.' not found';
727 dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
728 return -2;
729 }
730 } else {
731 $this->error = $this->db->error();
732 return -1;
733 }
734 }
735
736
742 public function getLinesArray()
743 {
744 return $this->fetch_lines();
745 }
746
747
748 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
754 public function fetch_lines()
755 {
756 // phpcs:enable
757
758 $this->lines = array();
759
760 dol_syslog('FactureRec::fetch_lines', LOG_DEBUG);
761
762 $sql = 'SELECT l.rowid, l.fk_facture, l.fk_product, l.fk_parent_line, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx, ';
763 $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
764 $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,';
765 $sql .= ' l.rang, l.special_code,';
766 $sql .= ' l.fk_unit, l.fk_contract_line,';
767 $sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
768 $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
769 $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
770 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
771 $sql .= ' WHERE l.fk_facture = '.((int) $this->id);
772 $sql .= ' ORDER BY l.rang';
773
774 $result = $this->db->query($sql);
775 if ($result) {
776 $num = $this->db->num_rows($result);
777 $i = 0;
778 while ($i < $num) {
779 $objp = $this->db->fetch_object($result);
780 $line = new FactureLigneRec($this->db);
781
782 $line->id = $objp->rowid;
783 $line->rowid = $objp->rowid;
784 $line->fk_facture = $objp->fk_facture;
785 $line->fk_parent_line = $objp->fk_parent_line;
786 $line->desc = $objp->description; // Description line
787 $line->description = $objp->description; // Description line
788 $line->ref = $objp->product_ref; // Ref product
789 $line->product_ref = $objp->product_ref; // Ref product
790 $line->libelle = $objp->product_label; // deprecated
791 $line->product_label = $objp->product_label; // Label product
792 $line->product_desc = $objp->product_desc; // Description product
793 $line->product_type = $objp->product_type; // Type of line
794 $line->fk_product_type = $objp->fk_product_type; // Type of product
795 $line->qty = $objp->qty;
796 $line->subprice = $objp->subprice;
797
798 $line->label = $objp->custom_label; // @deprecated
799
800 $line->vat_src_code = $objp->vat_src_code;
801 $line->tva_tx = $objp->tva_tx;
802 $line->localtax1_tx = $objp->localtax1_tx;
803 $line->localtax2_tx = $objp->localtax2_tx;
804 $line->localtax1_type = $objp->localtax1_type;
805 $line->localtax2_type = $objp->localtax2_type;
806 $line->remise_percent = $objp->remise_percent;
807 //$line->fk_remise_except = $objp->fk_remise_except;
808 $line->fk_product = $objp->fk_product;
809 $line->date_start_fill = $objp->date_start_fill;
810 $line->date_end_fill = $objp->date_end_fill;
811 $line->info_bits = $objp->info_bits;
812 $line->total_ht = $objp->total_ht;
813 $line->total_tva = $objp->total_tva;
814 $line->total_ttc = $objp->total_ttc;
815
816 $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
817 $line->fk_fournprice = $objp->fk_product_fournisseur_price; // For backward compatibility
818
819 $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
820
821 $line->buyprice = $marginInfos[0];
822 $line->pa_ht = $marginInfos[0]; // For backward compatibility
823 $line->marge_tx = (string) $marginInfos[1];
824 $line->marque_tx = (string) $marginInfos[2];
825 $line->rang = $objp->rang;
826 $line->special_code = $objp->special_code;
827 $line->fk_unit = $objp->fk_unit;
828 $line->fk_contract_line = $objp->fk_contract_line;
829
830 // Ne plus utiliser
831 $line->price = $objp->price;
832 $line->remise = $objp->remise;
833
834 $line->fetch_optionals();
835
836 // Multicurrency
837 $line->fk_multicurrency = $objp->fk_multicurrency;
838 $line->multicurrency_code = $objp->multicurrency_code;
839 $line->multicurrency_subprice = $objp->multicurrency_subprice;
840 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
841 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
842 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
843
844 $this->lines[$i] = $line;
845
846 $i++;
847 }
848
849 $this->db->free($result);
850 return 1;
851 } else {
852 $this->error = $this->db->lasterror();
853 return -3;
854 }
855 }
856
857
866 public function delete(User $user, $notrigger = 0, $idwarehouse = -1)
867 {
868 $rowid = $this->id;
869
870 dol_syslog(get_class($this)."::delete rowid=".((int) $rowid), LOG_DEBUG);
871
872 $error = 0;
873 $this->db->begin();
874
875 $main = MAIN_DB_PREFIX.'facturedet_rec';
876 $ef = $main."_extrafields";
877
878 $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_facture = ".((int) $rowid).")";
879 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = ".((int) $rowid);
880
881 if ($this->db->query($sqlef) && $this->db->query($sql)) {
882 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_rec WHERE rowid = ".((int) $rowid);
883 dol_syslog($sql);
884 if ($this->db->query($sql)) {
885 // Delete linked object
886 $res = $this->deleteObjectLinked();
887 if ($res < 0) {
888 $error = -3;
889 }
890 // Delete extrafields
891 $res = $this->deleteExtraFields();
892 if ($res < 0) {
893 $error = -4;
894 }
895 } else {
896 $this->error = $this->db->lasterror();
897 $error = -1;
898 }
899 } else {
900 $this->error = $this->db->lasterror();
901 $error = -2;
902 }
903 if (!$error && !$notrigger) {
904 // Call trigger
905 $result = $this->call_trigger('BILLREC_DELETE', $user);
906 if ($result < 0) {
907 $error++;
908 }
909 // End call triggers
910 }
911 if (!$error) {
912 $this->db->commit();
913 return 1;
914 } else {
915 $this->db->rollback();
916 return $error;
917 }
918 }
919
920
949 public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $info_bits = 0, $fk_remise_except = 0, $pu_ttc = 0, $type = 0, $rang = -1, $special_code = 0, $label = '', $fk_unit = null, $pu_ht_devise = 0, $date_start_fill = 0, $date_end_fill = 0, $fk_fournprice = null, $pa_ht = 0, $fk_parent_line = 0)
950 {
951 global $mysoc;
952
953 $facid = $this->id;
954
955 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);
956 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
957
958 // Check parameters
959 if ($type < 0) {
960 return -1;
961 }
962
963 $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
964
965 // Clean vat code
966 $reg = array();
967 $vat_src_code = '';
968 if (preg_match('/\‍((.*)\‍)/', (string) $txtva, $reg)) {
969 $vat_src_code = $reg[1];
970 $txtva = preg_replace('/\s*\‍(.*\‍)/', '', (string) $txtva); // Remove code from vatrate.
971 }
972
973
974 // Clean parameters
975 $remise_percent = price2num($remise_percent);
976 if (empty($remise_percent)) {
977 $remise_percent = 0;
978 }
979 $qty = (float) price2num($qty);
980 $pu_ht = (float) price2num($pu_ht);
981 $pu_ttc = (float) price2num($pu_ttc);
982 if (!preg_match('/\‍((.*)\‍)/', $txtva)) {
983 $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
984 }
985 $txlocaltax1 = (float) price2num($txlocaltax1);
986 $txlocaltax2 = (float) price2num($txlocaltax2);
987 if (empty($txtva)) {
988 $txtva = 0;
989 }
990 if (empty($txlocaltax1)) {
991 $txlocaltax1 = 0;
992 }
993 if (empty($txlocaltax2)) {
994 $txlocaltax2 = 0;
995 }
996 if (empty($info_bits)) {
997 $info_bits = 0;
998 }
999
1000 if ($price_base_type == 'HT') {
1001 $pu = $pu_ht;
1002 } else {
1003 $pu = $pu_ttc;
1004 }
1005
1006 // Calcul du total TTC et de la TVA pour la ligne a partir de
1007 // qty, pu, remise_percent et txtva
1008 // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1009 // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1010
1011 $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);
1012 $total_ht = $tabprice[0];
1013 $total_tva = $tabprice[1];
1014 $total_ttc = $tabprice[2];
1015 $total_localtax1 = $tabprice[9];
1016 $total_localtax2 = $tabprice[10];
1017 $pu_ht = $tabprice[3];
1018
1019 // MultiCurrency
1020 $multicurrency_total_ht = $tabprice[16];
1021 $multicurrency_total_tva = $tabprice[17];
1022 $multicurrency_total_ttc = $tabprice[18];
1023 $pu_ht_devise = $tabprice[19];
1024
1025 $product_type = $type;
1026 if ($fk_product) {
1027 $product = new Product($this->db);
1028 $result = $product->fetch($fk_product);
1029 $product_type = $product->type;
1030 }
1031
1032 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1033 $fk_parent_line = 0;
1034 }
1035
1036 // Rank to use
1037 $ranktouse = $rang;
1038 if ($ranktouse == -1) {
1039 $rangmax = $this->line_max(0);
1040 $ranktouse = $rangmax + 1;
1041 }
1042
1043 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (";
1044 $sql .= "fk_facture";
1045 $sql .= ", fk_parent_line";
1046 $sql .= ", label";
1047 $sql .= ", description";
1048 $sql .= ", price";
1049 $sql .= ", qty";
1050 $sql .= ", tva_tx";
1051 $sql .= ", vat_src_code";
1052 $sql .= ", localtax1_tx";
1053 $sql .= ", localtax1_type";
1054 $sql .= ", localtax2_tx";
1055 $sql .= ", localtax2_type";
1056 $sql .= ", fk_product";
1057 $sql .= ", product_type";
1058 $sql .= ", remise_percent";
1059 $sql .= ", subprice";
1060 $sql .= ", remise";
1061 $sql .= ", total_ht";
1062 $sql .= ", total_tva";
1063 $sql .= ", total_localtax1";
1064 $sql .= ", total_localtax2";
1065 $sql .= ", total_ttc";
1066 $sql .= ", date_start_fill";
1067 $sql .= ", date_end_fill";
1068 $sql .= ", fk_product_fournisseur_price";
1069 $sql .= ", buy_price_ht";
1070 $sql .= ", info_bits";
1071 $sql .= ", rang";
1072 $sql .= ", special_code";
1073 $sql .= ", fk_unit";
1074 $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
1075 $sql .= ") VALUES (";
1076 $sql .= " ".((int) $facid);
1077 $sql .= ", ".($fk_parent_line > 0 ? ((int) $fk_parent_line) : "null");
1078 $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1079 $sql .= ", '".$this->db->escape($desc)."'";
1080 $sql .= ", ".price2num($pu_ht);
1081 $sql .= ", ".price2num($qty);
1082 $sql .= ", ".price2num($txtva);
1083 $sql .= ", '".$this->db->escape($vat_src_code)."'";
1084 $sql .= ", ".price2num($txlocaltax1);
1085 $sql .= ", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] : '')."'";
1086 $sql .= ", ".price2num($txlocaltax2);
1087 $sql .= ", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] : '')."'";
1088 $sql .= ", ".(!empty($fk_product) ? "'".$this->db->escape($fk_product)."'" : "null");
1089 $sql .= ", ".((int) $product_type);
1090 $sql .= ", ".price2num($remise_percent);
1091 $sql .= ", ".price2num($pu_ht);
1092 $sql .= ", null";
1093 $sql .= ", ".price2num($total_ht);
1094 $sql .= ", ".price2num($total_tva);
1095 $sql .= ", ".price2num($total_localtax1);
1096 $sql .= ", ".price2num($total_localtax2);
1097 $sql .= ", ".price2num($total_ttc);
1098 $sql .= ", ".(int) $date_start_fill;
1099 $sql .= ", ".(int) $date_end_fill;
1100 $sql .= ", ".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1101 $sql .= ", ".($pa_ht ? price2num($pa_ht) : 0);
1102 $sql .= ", ".((int) $info_bits);
1103 $sql .= ", ".((int) $ranktouse);
1104 $sql .= ", ".((int) $special_code);
1105 $sql .= ", ".($fk_unit ? ((int) $fk_unit) : "null");
1106 $sql .= ", ".(int) $this->fk_multicurrency;
1107 $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
1108 $sql .= ", ".price2num($pu_ht_devise, 'CU');
1109 $sql .= ", ".price2num($multicurrency_total_ht, 'CT');
1110 $sql .= ", ".price2num($multicurrency_total_tva, 'CT');
1111 $sql .= ", ".price2num($multicurrency_total_ttc, 'CT');
1112 $sql .= ")";
1113
1114 dol_syslog(get_class($this)."::addline", LOG_DEBUG);
1115 if ($this->db->query($sql)) {
1116 $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX."facturedet_rec");
1117 $this->id = $facid;
1118 $this->update_price(1);
1119 return $lineId;
1120 } else {
1121 $this->error = $this->db->lasterror();
1122 return -1;
1123 }
1124 }
1125
1156 public function updateline($rowid, $desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $info_bits = 0, $fk_remise_except = 0, $pu_ttc = 0, $type = 0, $rang = -1, $special_code = 0, $label = '', $fk_unit = null, $pu_ht_devise = 0, $notrigger = 0, $date_start_fill = 0, $date_end_fill = 0, $fk_fournprice = null, $pa_ht = 0, $fk_parent_line = 0)
1157 {
1158 global $mysoc;
1159
1160 $facid = $this->id;
1161
1162 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);
1163 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1164
1165 // Clean parameters
1166 if (empty($remise_percent)) {
1167 $remise_percent = 0;
1168 }
1169
1170 // Check parameters
1171 if ($type < 0) {
1172 return -1;
1173 }
1174
1175 // Clean parameters
1176 $remise_percent = (float) price2num($remise_percent);
1177 $qty = (float) price2num($qty);
1178 if (empty($info_bits)) {
1179 $info_bits = 0;
1180 }
1181 $pu_ht = (float) price2num($pu_ht);
1182 $pu_ttc = (float) price2num($pu_ttc);
1183 $pu_ht_devise = (float) price2num($pu_ht_devise);
1184 if (!preg_match('/\‍((.*)\‍)/', (string) $txtva)) {
1185 $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1186 }
1187 $txlocaltax1 = (float) price2num($txlocaltax1);
1188 $txlocaltax2 = (float) price2num($txlocaltax2);
1189 if (empty($txlocaltax1)) {
1190 $txlocaltax1 = 0;
1191 }
1192 if (empty($txlocaltax2)) {
1193 $txlocaltax2 = 0;
1194 }
1195
1196 if (empty($this->multicurrency_subprice)) {
1197 $this->multicurrency_subprice = 0;
1198 }
1199 if (empty($this->multicurrency_total_ht)) {
1200 $this->multicurrency_total_ht = 0;
1201 }
1202 if (empty($this->multicurrency_total_tva)) {
1203 $this->multicurrency_total_tva = 0;
1204 }
1205 if (empty($this->multicurrency_total_ttc)) {
1206 $this->multicurrency_total_ttc = 0;
1207 }
1208
1209 if ($price_base_type == 'HT') {
1210 $pu = $pu_ht;
1211 } else {
1212 $pu = $pu_ttc;
1213 }
1214
1215 // Calculate total with, without tax and tax from qty, pu, remise_percent and txtva
1216 // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1217 // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1218
1219 $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
1220
1221 // Clean vat code
1222 $vat_src_code = '';
1223 $reg = array();
1224 if (preg_match('/\‍((.*)\‍)/', $txtva, $reg)) {
1225 $vat_src_code = $reg[1];
1226 $txtva = preg_replace('/\s*\‍(.*\‍)/', '', $txtva); // Remove code into vatrate.
1227 }
1228
1229 $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);
1230
1231 $total_ht = $tabprice[0];
1232 $total_tva = $tabprice[1];
1233 $total_ttc = $tabprice[2];
1234 $total_localtax1 = $tabprice[9];
1235 $total_localtax2 = $tabprice[10];
1236 $pu_ht = $tabprice[3];
1237 $pu_tva = $tabprice[4];
1238 $pu_ttc = $tabprice[5];
1239
1240 // MultiCurrency
1241 $multicurrency_total_ht = $tabprice[16];
1242 $multicurrency_total_tva = $tabprice[17];
1243 $multicurrency_total_ttc = $tabprice[18];
1244 $pu_ht_devise = $tabprice[19];
1245
1246 $product_type = $type;
1247 if ($fk_product) {
1248 $product = new Product($this->db);
1249 $result = $product->fetch($fk_product);
1250 $product_type = $product->type;
1251 }
1252
1253 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1254 $fk_parent_line = 0;
1255 }
1256
1257 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET ";
1258 $sql .= "fk_facture = ".((int) $facid);
1259 $sql .= ", fk_parent_line = ".($fk_parent_line > 0 ? ((int) $fk_parent_line) : "null");
1260 $sql .= ", label=".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1261 $sql .= ", description='".$this->db->escape($desc)."'";
1262 $sql .= ", price=".price2num($pu_ht);
1263 $sql .= ", qty=".price2num($qty);
1264 $sql .= ", tva_tx=".price2num($txtva);
1265 $sql .= ", vat_src_code='".$this->db->escape($vat_src_code)."'";
1266 $sql .= ", localtax1_tx=".((float) $txlocaltax1);
1267 $sql .= ", localtax1_type='".$this->db->escape($localtaxes_type[0])."'";
1268 $sql .= ", localtax2_tx=".((float) $txlocaltax2);
1269 $sql .= ", localtax2_type='".$this->db->escape($localtaxes_type[2])."'";
1270 $sql .= ", fk_product=".(!empty($fk_product) ? "'".$this->db->escape($fk_product)."'" : "null");
1271 $sql .= ", product_type=".((int) $product_type);
1272 $sql .= ", remise_percent='".price2num($remise_percent)."'";
1273 $sql .= ", subprice='".price2num($pu_ht)."'";
1274 $sql .= ", total_ht='".price2num($total_ht)."'";
1275 $sql .= ", total_tva='".price2num($total_tva)."'";
1276 $sql .= ", total_localtax1='".price2num($total_localtax1)."'";
1277 $sql .= ", total_localtax2='".price2num($total_localtax2)."'";
1278 $sql .= ", total_ttc='".price2num($total_ttc)."'";
1279 $sql .= ", date_start_fill=".((int) $date_start_fill);
1280 $sql .= ", date_end_fill=".((int) $date_end_fill);
1281 $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1282 $sql .= ", buy_price_ht=".($pa_ht ? price2num($pa_ht) : 0);
1283 $sql .= ", info_bits=".((int) $info_bits);
1284 $sql .= ", rang=".((int) $rang);
1285 $sql .= ", special_code=".((int) $special_code);
1286 $sql .= ", fk_unit=".($fk_unit ? "'".$this->db->escape($fk_unit)."'" : "null");
1287 $sql .= ', multicurrency_subprice = '.price2num($pu_ht_devise);
1288 $sql .= ', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1289 $sql .= ', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1290 $sql .= ', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1291 $sql .= " WHERE rowid = ".((int) $rowid);
1292
1293 dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1294 if ($this->db->query($sql)) {
1295 $this->id = $facid;
1296 $this->update_price(1);
1297 return 1;
1298 } else {
1299 $this->error = $this->db->lasterror();
1300 return -1;
1301 }
1302 }
1303
1304
1310 public function getNextDate()
1311 {
1312 if (empty($this->date_when)) {
1313 return false;
1314 }
1315 return dol_time_plus_duree($this->date_when, $this->frequency, $this->unit_frequency, 1);
1316 }
1317
1323 public function isMaxNbGenReached()
1324 {
1325 $ret = false;
1326 if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1327 $ret = true;
1328 }
1329 return $ret;
1330 }
1331
1338 public function strikeIfMaxNbGenReached($ret)
1339 {
1340 return $this->isMaxNbGenReached() ? '<strike>'.$ret.'</strike>' : $ret;
1341 }
1342
1354 public function createRecurringInvoices($restrictioninvoiceid = 0, $forcevalidation = 0, $notrigger = 0)
1355 {
1356 global $conf, $langs, $db, $user, $hookmanager;
1357
1358 $error = 0;
1359 $nb_create = 0;
1360
1361 // Load translation files required by the page
1362 $langs->loadLangs(array("main", "bills"));
1363
1364 $now = dol_now();
1365 $tmparray = dol_getdate($now);
1366 $today = dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year']); // Today is last second of current day
1367
1368 $this->output = '';
1369
1370 dol_syslog("createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid." forcevalidation=".$forcevalidation);
1371
1372 $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_rec';
1373 $sql .= ' WHERE frequency > 0'; // A recurring invoice is an invoice with a frequency
1374 $sql .= " AND (date_when IS NULL OR date_when <= '".$this->db->idate($today)."')";
1375 $sql .= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1376 $sql .= ' AND suspended = 0';
1377 $sql .= ' AND entity = '.$conf->entity; // MUST STAY = $conf->entity here
1378 if ($restrictioninvoiceid > 0) {
1379 $sql .= ' AND rowid = '.((int) $restrictioninvoiceid);
1380 }
1381 $sql .= $this->db->order('entity', 'ASC');
1382 //print $sql;exit;
1383 $parameters = array(
1384 'restrictioninvoiceid' => $restrictioninvoiceid,
1385 'forcevalidation' => $forcevalidation,
1386 );
1387 $reshook = $hookmanager->executeHooks('beforeCreationOfRecurringInvoices', $parameters, $sql); // note that $sql might be modified by hooks
1388
1389 $resql = $this->db->query($sql);
1390 if ($resql) {
1391 $i = 0;
1392 $num = $this->db->num_rows($resql);
1393
1394 if ($num) {
1395 $this->output .= $langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n";
1396 } else {
1397 $this->output .= $langs->trans("NoQualifiedRecurringInvoiceTemplateFound");
1398 }
1399
1400 $saventity = $conf->entity;
1401
1402 while ($i < $num) { // Loop on each template invoice. If $num = 0, test is false at first pass.
1403 $line = $this->db->fetch_object($resql);
1404
1405 $this->db->begin();
1406
1407 $invoiceidgenerated = 0;
1408
1409 $facture = null;
1410 $facturerec = new FactureRec($this->db);
1411 $facturerec->fetch($line->rowid);
1412
1413 if ($facturerec->id > 0) {
1414 // Set entity context
1415 $conf->entity = $facturerec->entity;
1416
1417 dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity);
1418
1419 $facture = new Facture($this->db);
1420 $facture->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice
1421 $facture->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice
1422
1423 $facture->type = self::TYPE_STANDARD;
1424 $facture->subtype = $facturerec->subtype;
1425 $facture->statut = self::STATUS_DRAFT; // deprecated
1426 $facture->status = self::STATUS_DRAFT;
1427 $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.
1428 $facture->socid = $facturerec->socid;
1429 if (!empty($facturerec->fk_multicurrency)) {
1430 $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1431 $facture->multicurrency_code = $facturerec->multicurrency_code;
1432 $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1433 }
1434
1435 if (isset($facture->array_options) && isset($facturerec->array_options)) {
1436 foreach ($facturerec->array_options as $key => $value) {
1437 if (isset($facture->array_options[$key])) {
1438 $facture->array_options[$key] = $value;
1439 }
1440 }
1441 }
1442
1443 $invoiceidgenerated = $facture->create($user);
1444 if ($invoiceidgenerated <= 0) {
1445 $this->errors = $facture->errors;
1446 $this->error = $facture->error;
1447 $error++;
1448 }
1449
1450
1451 if (!$error && ($facturerec->auto_validate || $forcevalidation)) {
1452 $result = $facture->validate($user);
1453 if ($result <= 0) {
1454 $this->errors = $facture->errors;
1455 $this->error = $facture->error;
1456 $error++;
1457 }
1458 }
1459 if (!$error && $facturerec->generate_pdf) {
1460 // We refresh the object in order to have all necessary data (like date_lim_reglement)
1461 $facture->fetch($facture->id);
1462 $result = $facture->generateDocument($facturerec->model_pdf, $langs);
1463 if ($result <= 0) {
1464 $this->errors = $facture->errors;
1465 $this->error = $facture->error;
1466 $error++;
1467 }
1468 }
1469 } else {
1470 $error++;
1471 $this->error = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity."\n";
1472 $this->errors[] = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity;
1473 dol_syslog("createRecurringInvoices Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity);
1474 }
1475
1476 if (!$error && $invoiceidgenerated >= 0) {
1477 $this->db->commit("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1478 dol_syslog("createRecurringInvoices Process invoice template ".$facturerec->ref." is finished with a success generation");
1479 $nb_create++;
1480 $this->output .= $langs->trans("InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref)."\n";
1481 } else {
1482 $this->output .= $langs->trans("InvoiceGeneratedFromTemplateError", $facture->ref, $facturerec->ref, $this->error)."\n";
1483 $this->db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1484 }
1485
1486 $parameters = array(
1487 'cpt' => $i,
1488 'total' => $num,
1489 'errorCount' => $error,
1490 'invoiceidgenerated' => $invoiceidgenerated,
1491 'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks.
1492 'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks.
1493 );
1494 $reshook = $hookmanager->executeHooks('afterCreationOfRecurringInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null)
1495
1496 $i++;
1497 }
1498
1499 $conf->entity = $saventity; // Restore entity context
1500 } else {
1501 dol_print_error($this->db);
1502 }
1503
1504 $this->output = trim($this->output);
1505
1506 return $error ? $error : 0;
1507 }
1508
1521 public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1)
1522 {
1523 global $langs, $hookmanager;
1524
1525 $result = '';
1526
1527 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("RepeatableInvoice").'</u>';
1528 if (!empty($this->ref)) {
1529 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1530 }
1531 if ($this->frequency > 0) {
1532 $label .= '<br><b>'.$langs->trans('Frequency').':</b> '.$langs->trans('FrequencyPer_'.$this->unit_frequency, $this->frequency);
1533 }
1534 if (!empty($this->date_last_gen)) {
1535 $label .= '<br><b>'.$langs->trans('DateLastGeneration').':</b> '.dol_print_date($this->date_last_gen, 'dayhour');
1536 }
1537 if ($this->frequency > 0) {
1538 if (!empty($this->date_when)) {
1539 $label .= '<br><b>'.$langs->trans('NextDateToExecution').':</b> ';
1540 $label .= (empty($this->suspended) ? '' : '<strike>').dol_print_date($this->date_when, 'day').(empty($this->suspended) ? '' : '</strike>'); // No hour for this property
1541 if (!empty($this->suspended)) {
1542 $label .= ' ('.$langs->trans("Disabled").')';
1543 }
1544 }
1545 }
1546
1547 $url = DOL_URL_ROOT.'/compta/facture/card-rec.php?facid='.$this->id;
1548
1549 if ($short) {
1550 return $url;
1551 }
1552
1553 if ($option != 'nolink') {
1554 // Add param to save lastsearch_values or not
1555 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1556 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1557 $add_save_lastsearch_values = 1;
1558 }
1559 if ($add_save_lastsearch_values) {
1560 $url .= '&save_lastsearch_values=1';
1561 }
1562 }
1563
1564 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1565 $linkend = '</a>';
1566
1567 $result .= $linkstart;
1568 if ($withpicto) {
1569 $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);
1570 }
1571 if ($withpicto != 2) {
1572 $result .= dol_trunc($this->ref, $max);
1573 }
1574 $result .= $linkend;
1575 global $action;
1576 $hookmanager->initHooks(array($this->element . 'dao'));
1577 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1578 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1579 if ($reshook > 0) {
1580 $result = $hookmanager->resPrint;
1581 } else {
1582 $result .= $hookmanager->resPrint;
1583 }
1584 return $result;
1585 }
1586
1594 public function getLibStatut($mode = 0, $alreadypaid = -1)
1595 {
1596 return $this->LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->type) ? 0 : $this->type);
1597 }
1598
1599 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1611 public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0, $nbofopendirectdebitorcredittransfer = 0)
1612 {
1613 // phpcs:enable
1614 global $langs;
1615 $langs->load('bills');
1616
1617 $labelStatus = $langs->transnoentitiesnoconv('Active');
1618 $statusType = 'status0';
1619
1620 //print "$recur,$status,$mode,$alreadypaid,$type";
1621 if ($mode == 0) {
1622 if ($recur) {
1623 if ($status == self::STATUS_SUSPENDED) {
1624 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1625 } else {
1626 $labelStatus = $langs->transnoentitiesnoconv('Active');
1627 }
1628 } else {
1629 if ($status == self::STATUS_SUSPENDED) {
1630 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1631 } else {
1632 $labelStatus = $langs->transnoentitiesnoconv("Draft");
1633 }
1634 }
1635 } elseif ($mode == 1) {
1636 $prefix = 'Short';
1637 if ($recur) {
1638 if ($status == self::STATUS_SUSPENDED) {
1639 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1640 } else {
1641 $labelStatus = $langs->transnoentitiesnoconv('Active');
1642 }
1643 } else {
1644 if ($status == self::STATUS_SUSPENDED) {
1645 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1646 } else {
1647 $labelStatus = $langs->transnoentitiesnoconv("Draft");
1648 }
1649 }
1650 } elseif ($mode == 2) {
1651 if ($recur) {
1652 if ($status == self::STATUS_SUSPENDED) {
1653 $statusType = 'status6';
1654 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1655 } else {
1656 $statusType = 'status4';
1657 $labelStatus = $langs->transnoentitiesnoconv('Active');
1658 }
1659 } else {
1660 if ($status == self::STATUS_SUSPENDED) {
1661 $statusType = 'status6';
1662 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1663 } else {
1664 $statusType = 'status0';
1665 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1666 }
1667 }
1668 } elseif ($mode == 3) {
1669 if ($recur) {
1670 $prefix = 'Short';
1671 if ($status == self::STATUS_SUSPENDED) {
1672 $statusType = 'status6';
1673 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1674 } else {
1675 $statusType = 'status4';
1676 $labelStatus = $langs->transnoentitiesnoconv('Active');
1677 }
1678 } else {
1679 if ($status == self::STATUS_SUSPENDED) {
1680 $statusType = 'status6';
1681 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1682 } else {
1683 $statusType = 'status0';
1684 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1685 }
1686 }
1687 } elseif ($mode == 4) {
1688 $prefix = '';
1689 if ($recur) {
1690 if ($status == self::STATUS_SUSPENDED) {
1691 $statusType = 'status6';
1692 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1693 } else {
1694 $statusType = 'status4';
1695 $labelStatus = $langs->transnoentitiesnoconv('Active');
1696 }
1697 } else {
1698 if ($status == self::STATUS_SUSPENDED) {
1699 $statusType = 'status6';
1700 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1701 } else {
1702 $statusType = 'status0';
1703 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1704 }
1705 }
1706 } elseif ($mode == 5 || $mode == 6) {
1707 $prefix = '';
1708 if ($mode == 5) {
1709 $prefix = 'Short';
1710 }
1711 if ($recur) {
1712 if ($status == self::STATUS_SUSPENDED) {
1713 $statusType = 'status6';
1714 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1715 } else {
1716 $statusType = 'status4';
1717 $labelStatus = $langs->transnoentitiesnoconv('Active');
1718 }
1719 } else {
1720 if ($status == self::STATUS_SUSPENDED) {
1721 $statusType = 'status6';
1722 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1723 } else {
1724 $statusType = 'status0';
1725 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1726 }
1727 }
1728 }
1729
1730 $labelStatusShort = $labelStatus;
1731
1732 return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1733 }
1734
1742 public function getNextNumRef($soc, $mode = 'next')
1743 {
1744 // Not used for recurring invoices
1745 return '';
1746 }
1747
1754 public function info($id)
1755 {
1756 $sql = 'SELECT c.rowid, datec, tms as datem,';
1757 $sql .= ' fk_user_author, fk_user_modif';
1758 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as c';
1759 $sql .= ' WHERE c.rowid = '.((int) $id);
1760
1761 $result = $this->db->query($sql);
1762 if ($result) {
1763 if ($this->db->num_rows($result)) {
1764 $obj = $this->db->fetch_object($result);
1765
1766 $this->id = $obj->rowid;
1767
1768 $this->user_creation_id = $obj->fk_user_author;
1769 $this->user_modification_id = $obj->fk_user_modif;
1770 $this->date_creation = $this->db->jdate($obj->datec);
1771 $this->date_modification = $this->db->jdate($obj->datem);
1772 }
1773 $this->db->free($result);
1774 } else {
1775 dol_print_error($this->db);
1776 }
1777 }
1778
1787 public function initAsSpecimen($option = '')
1788 {
1789 global $langs;
1790
1791 $now = dol_now();
1792 $arraynow = dol_getdate($now);
1793 $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1794
1795 // Load array of products prodids
1796 $num_prods = 0;
1797 $prodids = array();
1798
1799 $sql = "SELECT rowid";
1800 $sql .= " FROM ".MAIN_DB_PREFIX."product";
1801 $sql .= " WHERE entity IN (".getEntity('product').")";
1802 $sql .= $this->db->plimit(100);
1803
1804 $resql = $this->db->query($sql);
1805 if ($resql) {
1806 $num_prods = $this->db->num_rows($resql);
1807 $i = 0;
1808 while ($i < $num_prods) {
1809 $i++;
1810 $row = $this->db->fetch_row($resql);
1811 $prodids[$i] = $row[0];
1812 }
1813 }
1814
1815 // Initialize parameters
1816 $this->id = 0;
1817 $this->ref = 'SPECIMEN';
1818 $this->title = 'SPECIMEN';
1819 $this->specimen = 1;
1820 $this->socid = 1;
1821 $this->date = $nownotime;
1822 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
1823 $this->cond_reglement_id = 1;
1824 $this->cond_reglement_code = 'RECEP';
1825 $this->date_lim_reglement = $this->calculate_date_lim_reglement();
1826 $this->mode_reglement_id = 0; // Not forced to show payment mode CHQ + VIR
1827 $this->mode_reglement_code = ''; // Not forced to show payment mode CHQ + VIR
1828 $this->note_public = 'This is a comment (public)';
1829 $this->note_private = 'This is a comment (private)';
1830 $this->note = 'This is a comment (private)';
1831 $this->fk_incoterms = 0;
1832 $this->location_incoterms = '';
1833
1834 if (empty($option) || $option != 'nolines') {
1835 // Lines
1836 $nbp = min(1000, GETPOSTINT('nblines') ? GETPOSTINT('nblines') : 5); // We can force the nb of lines to test from command line (but not more than 1000)
1837 $xnbp = 0;
1838 while ($xnbp < $nbp) {
1839 $line = new FactureLigne($this->db);
1840 $line->desc = $langs->trans("Description")." ".$xnbp;
1841 $line->qty = 1;
1842 $line->subprice = 100;
1843 $line->tva_tx = 19.6;
1844 $line->localtax1_tx = 0;
1845 $line->localtax2_tx = 0;
1846 $line->remise_percent = 0;
1847 if ($xnbp == 1) { // Qty is negative (product line)
1848 $prodid = mt_rand(1, $num_prods);
1849 $line->fk_product = $prodids[$prodid];
1850 $line->qty = -1;
1851 $line->total_ht = -100;
1852 $line->total_ttc = -119.6;
1853 $line->total_tva = -19.6;
1854 } elseif ($xnbp == 2) { // UP is negative (free line)
1855 $line->subprice = -100;
1856 $line->total_ht = -100;
1857 $line->total_ttc = -119.6;
1858 $line->total_tva = -19.6;
1859 $line->remise_percent = 0;
1860 } elseif ($xnbp == 3) { // Discount is 50% (product line)
1861 $prodid = mt_rand(1, $num_prods);
1862 $line->fk_product = $prodids[$prodid];
1863 $line->total_ht = 50;
1864 $line->total_ttc = 59.8;
1865 $line->total_tva = 9.8;
1866 $line->remise_percent = 50;
1867 } else { // (product line)
1868 $prodid = mt_rand(1, $num_prods);
1869 $line->fk_product = $prodids[$prodid];
1870 $line->total_ht = 100;
1871 $line->total_ttc = 119.6;
1872 $line->total_tva = 19.6;
1873 $line->remise_percent = 00;
1874 }
1875
1876 $this->lines[$xnbp] = $line;
1877 $xnbp++;
1878
1879 $this->total_ht += $line->total_ht;
1880 $this->total_tva += $line->total_tva;
1881 $this->total_ttc += $line->total_ttc;
1882 }
1883 $this->revenuestamp = 0;
1884
1885 // Add a line "offered"
1886 $line = new FactureLigne($this->db);
1887 $line->desc = $langs->trans("Description")." (offered line)";
1888 $line->qty = 1;
1889 $line->subprice = 100;
1890 $line->tva_tx = 19.6;
1891 $line->localtax1_tx = 0;
1892 $line->localtax2_tx = 0;
1893 $line->remise_percent = 100;
1894 $line->total_ht = 0;
1895 $line->total_ttc = 0; // 90 * 1.196
1896 $line->total_tva = 0;
1897 $prodid = mt_rand(1, $num_prods);
1898 $line->fk_product = $prodids[$prodid];
1899
1900 $this->lines[$xnbp] = $line;
1901 $xnbp++;
1902 }
1903
1904 $this->usenewprice = 0;
1905
1906 return 1;
1907 }
1908
1917 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1918 {
1919 $tables = array(
1920 'facture_rec'
1921 );
1922
1923 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1924 }
1925
1934 public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1935 {
1936 $tables = array(
1937 'facturedet_rec'
1938 );
1939
1940 return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1941 }
1942
1951 public function setFrequencyAndUnit($frequency, $unit, $notrigger = 0)
1952 {
1953 global $user;
1954
1955 if (!$this->table_element) {
1956 dol_syslog(get_class($this)."::setFrequencyAndUnit was called on object with property table_element not defined", LOG_ERR);
1957 return -1;
1958 }
1959
1960 if (!empty($frequency) && empty($unit)) {
1961 dol_syslog(get_class($this)."::setFrequencyAndUnit was called on object with params frequency defined but unit not defined", LOG_ERR);
1962 return -2;
1963 }
1964
1965 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1966 $sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null');
1967 if (!empty($unit)) {
1968 $sql .= ', unit_frequency = \''.$this->db->escape($unit).'\'';
1969 }
1970 $sql .= " WHERE rowid = ".((int) $this->id);
1971
1972 dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG);
1973 if ($this->db->query($sql)) {
1974 $this->frequency = $frequency;
1975 if (!empty($unit)) {
1976 $this->unit_frequency = $unit;
1977 }
1978
1979 if (!$notrigger) {
1980 // Call trigger
1981 $result = $this->call_trigger('BILLREC_MODIFY', $user);
1982 if ($result < 0) {
1983 return $result;
1984 }
1985 // End call triggers
1986 }
1987
1988 return 1;
1989 } else {
1990 dol_print_error($this->db);
1991 return -1;
1992 }
1993 }
1994
2003 public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
2004 {
2005 global $user;
2006
2007 if (!$this->table_element) {
2008 dol_syslog(get_class($this)."::setNextDate was called on object with property table_element not defined", LOG_ERR);
2009 return -1;
2010 }
2011 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2012 $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
2013 if ($increment_nb_gen_done > 0) {
2014 $sql .= ', nb_gen_done = nb_gen_done + 1';
2015 }
2016 $sql .= " WHERE rowid = ".((int) $this->id);
2017
2018 dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG);
2019 if ($this->db->query($sql)) {
2020 $this->date_when = $date;
2021 if ($increment_nb_gen_done > 0) {
2022 $this->nb_gen_done++;
2023
2024 if (getDolGlobalInt('MAIN_SUSPEND_FACTURE_REC_ON_MAX_GEN_REACHED') && $this->isMaxNbGenReached()) {
2025 $resSuspend = $this->setValueFrom('suspended', 1);
2026
2027 if ($resSuspend <= 0) {
2028 dol_syslog(__METHOD__ . '::setValueFrom Error : ' . $this->error, LOG_ERR);
2029 return -1;
2030 }
2031 }
2032 }
2033
2034 if (!$notrigger) {
2035 // Call trigger
2036 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2037 if ($result < 0) {
2038 return $result;
2039 }
2040 // End call triggers
2041 }
2042 return 1;
2043 } else {
2044 dol_print_error($this->db);
2045 return -1;
2046 }
2047 }
2048
2056 public function setMaxPeriod($nb, $notrigger = 0)
2057 {
2058 global $user;
2059
2060 if (!$this->table_element) {
2061 dol_syslog(get_class($this)."::setMaxPeriod was called on object with property table_element not defined", LOG_ERR);
2062 return -1;
2063 }
2064
2065 if (empty($nb)) {
2066 $nb = 0;
2067 }
2068
2069 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2070 $sql .= ' SET nb_gen_max = '.((int) $nb);
2071 $sql .= " WHERE rowid = ".((int) $this->id);
2072
2073 dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG);
2074 if ($this->db->query($sql)) {
2075 $this->nb_gen_max = $nb;
2076
2077 if (!$notrigger) {
2078 // Call trigger
2079 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2080 if ($result < 0) {
2081 return $result;
2082 }
2083 // End call triggers
2084 }
2085
2086 return 1;
2087 } else {
2088 dol_print_error($this->db);
2089 return -1;
2090 }
2091 }
2092
2100 public function setAutoValidate($validate, $notrigger = 0)
2101 {
2102 global $user;
2103
2104 if (!$this->table_element) {
2105 dol_syslog(get_class($this)."::setAutoValidate was called on object with property table_element not defined", LOG_ERR);
2106 return -1;
2107 }
2108
2109 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2110 $sql .= ' SET auto_validate = '.((int) $validate);
2111 $sql .= " WHERE rowid = ".((int) $this->id);
2112
2113 dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
2114 if ($this->db->query($sql)) {
2115 $this->auto_validate = $validate;
2116
2117 if (!$notrigger) {
2118 // Call trigger
2119 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2120 if ($result < 0) {
2121 return $result;
2122 }
2123 // End call triggers
2124 }
2125
2126 return 1;
2127 } else {
2128 dol_print_error($this->db);
2129 return -1;
2130 }
2131 }
2132
2140 public function setGeneratePdf($validate, $notrigger = 0)
2141 {
2142 global $user;
2143
2144 if (!$this->table_element) {
2145 dol_syslog(get_class($this)."::setGeneratePdf was called on object with property table_element not defined", LOG_ERR);
2146 return -1;
2147 }
2148
2149 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2150 $sql .= ' SET generate_pdf = '.((int) $validate);
2151 $sql .= " WHERE rowid = ".((int) $this->id);
2152
2153 dol_syslog(get_class($this)."::setGeneratePdf", LOG_DEBUG);
2154 if ($this->db->query($sql)) {
2155 $this->generate_pdf = $validate;
2156
2157 if (!$notrigger) {
2158 // Call trigger
2159 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2160 if ($result < 0) {
2161 return $result;
2162 }
2163 // End call triggers
2164 }
2165
2166 return 1;
2167 } else {
2168 dol_print_error($this->db);
2169 return -1;
2170 }
2171 }
2172
2180 public function setModelPdf($model, $notrigger = 0)
2181 {
2182 global $user;
2183 if (!$this->table_element) {
2184 dol_syslog(get_class($this)."::setModelPdf was called on object with property table_element not defined", LOG_ERR);
2185 return -1;
2186 }
2187
2188 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2189 $sql .= " SET modelpdf = '".$this->db->escape($model)."'";
2190 $sql .= " WHERE rowid = ".((int) $this->id);
2191
2192 dol_syslog(get_class($this)."::setModelPdf", LOG_DEBUG);
2193 if ($this->db->query($sql)) {
2194 $this->model_pdf = $model;
2195
2196 if (!$notrigger) {
2197 // Call trigger
2198 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2199 if ($result < 0) {
2200 return $result;
2201 }
2202 // End call triggers
2203 }
2204
2205 return 1;
2206 } else {
2207 dol_print_error($this->db);
2208 return -1;
2209 }
2210 }
2211}
2212
2213
2214
2220{
2224 public $element = 'facturedetrec';
2225
2229 public $table_element = 'facturedet_rec';
2230
2234 public $parent_element = 'facturerec';
2235
2239 public $fk_parent_attribute = 'fk_facture';
2240
2242
2245 public $fk_facture;
2249 public $fk_parent_line;
2250
2254 public $fk_product_fournisseur_price;
2259 public $fk_fournprice;
2260
2264 public $rang;
2265 //public $situation_percent; // Not supported on recurring invoice line
2266
2270 public $desc;
2274 public $description;
2275
2280 public $fk_product_type;
2281
2285 public $fk_contract_line;
2286
2287
2295 public function delete(User $user, $notrigger = 0)
2296 {
2297 $error = 0;
2298
2299 $this->db->begin();
2300
2301 if (!$error) {
2302 if (!$notrigger) {
2303 // Call triggers
2304 $result = $this->call_trigger('LINEBILLREC_DELETE', $user);
2305 if ($result < 0) {
2306 $error++;
2307 } // Do also here what you must do to rollback action if trigger fail
2308 // End call triggers
2309 }
2310 }
2311
2312 if (!$error) {
2313 $result = $this->deleteExtraFields();
2314 if ($result < 0) {
2315 $error++;
2316 }
2317 }
2318
2319 if (!$error) {
2320 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.((int) $this->id);
2321
2322 $res = $this->db->query($sql);
2323 if (!$res) {
2324 $error++;
2325 $this->errors[] = $this->db->lasterror();
2326 }
2327 }
2328
2329 // Commit or rollback
2330 if ($error) {
2331 $this->db->rollback();
2332 return -1;
2333 } else {
2334 $this->db->commit();
2335 return 1;
2336 }
2337 }
2338
2339
2346 public function fetch($rowid)
2347 {
2348 $sql = 'SELECT l.rowid, l.fk_facture, l.fk_parent_line, l.fk_product, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx,';
2349 $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2350 $sql .= ' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2351 $sql .= ' l.rang, l.special_code,';
2352 $sql .= ' l.fk_unit, l.fk_contract_line,';
2353 $sql .= ' l.import_key, l.fk_multicurrency,';
2354 $sql .= ' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2355 $sql .= ' l.buy_price_ht, l.fk_product_fournisseur_price,';
2356 $sql .= ' l.fk_user_author, l.fk_user_modif,';
2357 $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2358 $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
2359 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
2360 $sql .= ' WHERE l.rowid = '.((int) $rowid);
2361 $sql .= ' ORDER BY l.rang';
2362
2363 dol_syslog('FactureRec::fetch', LOG_DEBUG);
2364 $result = $this->db->query($sql);
2365 if ($result) {
2366 $objp = $this->db->fetch_object($result);
2367
2368 $this->id = $objp->rowid;
2369 $this->fk_facture = $objp->fk_facture;
2370 $this->fk_parent_line = $objp->fk_parent_line;
2371 $this->label = $objp->custom_label; // Label line
2372 $this->desc = $objp->description; // Description line
2373 $this->description = $objp->description; // Description line
2374 $this->product_type = $objp->product_type; // Type of line
2375 $this->ref = $objp->product_ref; // Ref product
2376 $this->product_ref = $objp->product_ref; // Ref product
2377 $this->libelle = $objp->product_label; // deprecated
2378 $this->product_label = $objp->product_label; // Label product
2379 $this->product_desc = $objp->product_desc; // Description product
2380 $this->fk_product_type = $objp->fk_product_type; // Type of product
2381 $this->qty = $objp->qty;
2382 $this->price = $objp->price;
2383 $this->subprice = $objp->subprice;
2384 $this->vat_src_code = $objp->vat_src_code;
2385 $this->tva_tx = $objp->tva_tx;
2386 $this->localtax1_tx = $objp->localtax1_tx;
2387 $this->localtax2_tx = $objp->localtax2_tx;
2388 $this->localtax1_type = $objp->localtax1_type;
2389 $this->localtax2_type = $objp->localtax2_type;
2390 $this->remise_percent = $objp->remise_percent;
2391 //$this->fk_remise_except = $objp->fk_remise_except;
2392 $this->fk_product = $objp->fk_product;
2393 $this->date_start_fill = $objp->date_start_fill;
2394 $this->date_end_fill = $objp->date_end_fill;
2395 $this->info_bits = $objp->info_bits;
2396 $this->total_ht = $objp->total_ht;
2397 $this->total_tva = $objp->total_tva;
2398 $this->total_ttc = $objp->total_ttc;
2399
2400 $this->rang = $objp->rang;
2401 $this->special_code = $objp->special_code;
2402 $this->fk_unit = $objp->fk_unit;
2403 $this->fk_contract_line = $objp->fk_contract_line;
2404 $this->import_key = $objp->import_key;
2405 $this->fk_multicurrency = $objp->fk_multicurrency;
2406 $this->multicurrency_code = $objp->multicurrency_code;
2407 $this->multicurrency_subprice = $objp->multicurrency_subprice;
2408 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2409 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2410 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2411
2412 $this->buy_price_ht = $objp->buy_price_ht;
2413
2414 $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2415 $this->fk_user_author = $objp->fk_user_author;
2416 $this->fk_user_modif = $objp->fk_user_modif;
2417
2418 $this->db->free($result);
2419 return 1;
2420 } else {
2421 $this->error = $this->db->lasterror();
2422 return -3;
2423 }
2424 }
2425
2426
2434 public function update(User $user, $notrigger = 0)
2435 {
2436 global $conf;
2437
2438 $error = 0;
2439
2440 // Clean parameters
2441 if (empty($this->fk_parent_line)) {
2442 $this->fk_parent_line = 0;
2443 }
2444
2445 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2446
2447 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET";
2448 $sql .= " fk_facture = ".((int) $this->fk_facture);
2449 $sql .= ", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
2450 $sql .= ", label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
2451 $sql .= ", description='".$this->db->escape($this->desc)."'";
2452 $sql .= ", price=".price2num($this->price);
2453 $sql .= ", qty=".price2num($this->qty);
2454 $sql .= ", tva_tx=".price2num($this->tva_tx);
2455 $sql .= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'";
2456 $sql .= ", localtax1_tx=".price2num($this->localtax1_tx);
2457 $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
2458 $sql .= ", localtax2_tx=".price2num($this->localtax2_tx);
2459 $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
2460 $sql .= ", fk_product=".($this->fk_product > 0 ? $this->fk_product : "null");
2461 $sql .= ", product_type=".((int) $this->product_type);
2462 $sql .= ", remise_percent=".price2num($this->remise_percent);
2463 $sql .= ", subprice=".price2num($this->subprice);
2464 $sql .= ", info_bits=".price2num($this->info_bits);
2465 $sql .= ", date_start_fill=".(int) $this->date_start_fill;
2466 $sql .= ", date_end_fill=".(int) $this->date_end_fill;
2467 if (empty($this->skip_update_total)) {
2468 $sql .= ", total_ht=".price2num($this->total_ht);
2469 $sql .= ", total_tva=".price2num($this->total_tva);
2470 $sql .= ", total_localtax1=".price2num($this->total_localtax1);
2471 $sql .= ", total_localtax2=".price2num($this->total_localtax2);
2472 $sql .= ", total_ttc=".price2num($this->total_ttc);
2473 }
2474 $sql .= ", rang=".((int) $this->rang);
2475 $sql .= ", special_code=".((int) $this->special_code);
2476 $sql .= ", fk_unit=".($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : "null");
2477 $sql .= ", fk_contract_line=".($this->fk_contract_line ? $this->fk_contract_line : "null");
2478 $sql .= " WHERE rowid = ".((int) $this->id);
2479
2480 $this->db->begin();
2481
2482 dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
2483
2484 $resql = $this->db->query($sql);
2485 if ($resql) {
2486 if (!$error) {
2487 $result = $this->insertExtraFields();
2488 if ($result < 0) {
2489 $error++;
2490 }
2491 }
2492
2493 if (!$error && !$notrigger) {
2494 // Call trigger
2495 $result = $this->call_trigger('LINEBILLREC_MODIFY', $user);
2496 if ($result < 0) {
2497 $error++;
2498 }
2499 // End call triggers
2500 }
2501
2502 if ($error) {
2503 $this->db->rollback();
2504 return -2;
2505 } else {
2506 $this->db->commit();
2507 return 1;
2508 }
2509 } else {
2510 $this->error = $this->db->lasterror();
2511 $this->db->rollback();
2512 return -2;
2513 }
2514 }
2515}
$object ref
Definition info.php:89
Superclass for invoice 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.
setValueFrom($field, $value, $table='', $id=null, $format='', $id_field='', $fuser=null, $trigkey='', $fk_user_field='fk_user_modif')
Setter generic.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage invoices.
Class to manage invoice lines.
Class to manage invoice lines of templates.
fetch($rowid)
Get line of template invoice.
update(User $user, $notrigger=0)
Update a line to invoice_rec.
Class to manage invoice templates.
setFrequencyAndUnit($frequency, $unit, $notrigger=0)
Update frequency and unit.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of object status.
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type='HT', $info_bits=0, $fk_remise_except=0, $pu_ttc=0, $type=0, $rang=-1, $special_code=0, $label='', $fk_unit=null, $pu_ht_devise=0, $date_start_fill=0, $date_end_fill=0, $fk_fournprice=null, $pa_ht=0, $fk_parent_line=0)
Add a line to invoice.
updateline($rowid, $desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type='HT', $info_bits=0, $fk_remise_except=0, $pu_ttc=0, $type=0, $rang=-1, $special_code=0, $label='', $fk_unit=null, $pu_ht_devise=0, $notrigger=0, $date_start_fill=0, $date_end_fill=0, $fk_fournprice=null, $pa_ht=0, $fk_parent_line=0)
Update a line to invoice.
LibStatut($recur, $status, $mode=0, $alreadypaid=-1, $type=0, $nbofopendirectdebitorcredittransfer=0)
Return label of a status.
fetch_lines()
Get lines of template invoices into this->lines.
info($id)
Load miscellaneous information for tab "Info".
getLinesArray()
Create an array of invoice lines.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1)
Return clickable name (with picto eventually)
isMaxNbGenReached()
Return if maximum number of generation is reached.
setModelPdf($model, $notrigger=0)
Update the model for documents.
getNextNumRef($soc, $mode='next')
Return next reference of invoice not already used (or last reference)
__construct(DoliDB $db)
Constructor.
setNextDate($date, $increment_nb_gen_done=0, $notrigger=0)
Update the next date of execution.
setAutoValidate($validate, $notrigger=0)
Update the auto validate flag of invoice.
initAsSpecimen($option='')
Initialise an instance with random values.
setMaxPeriod($nb, $notrigger=0)
Update the maximum period.
update(User $user, $notrigger=0)
Update a line invoice_rec.
create($user, $facid, $notrigger=0, $onlylines=array())
Create a predefined invoice.
strikeIfMaxNbGenReached($ret)
Format string to output with by striking the string if max number of generation was reached.
fetch($rowid, $ref='', $ref_ext='', $noextrafields=0, $nolines=0)
Load object and lines.
setGeneratePdf($validate, $notrigger=0)
Update the auto generate documents.
static replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a product id with another one.
getNextDate()
Return the next date of.
createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0, $notrigger=0)
Create all recurrents invoices (for all entities if multicompany is used).
Class to manage products or services.
Class to manage Dolibarr users.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after 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_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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:90
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:150