dolibarr 22.0.5
facture-rec.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
7 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2017-2025 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2023 Nick Fragoulis
11 * Copyright (C) 2024-2025 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';
39require_once DOL_DOCUMENT_ROOT.'/subtotals/class/commonsubtotal.class.php';
40
41
46{
47 use CommonSubtotal;
48
49 const TRIGGER_PREFIX = 'BILLREC';
50
54 public $element = 'facturerec';
55
59 public $table_element = 'facture_rec';
60
64 public $table_element_line = 'facturedet_rec';
65
69 public $fk_element = 'fk_facture';
70
74 public $picto = 'bill';
75
79 public $entity;
80
84 protected $table_ref_field = 'titre';
85
89 public $title;
90
95 public $titre;
96
100 public $multicurrency_subprice;
104 public $socid;
108 public $number;
112 public $date;
113 //public $remise;
114 //public $remise_absolue;
115 //public $remise_percent;
116
122 public $total;
123
129 public $tva;
130
134 public $date_last_gen;
138 public $date_when;
142 public $nb_gen_done;
146 public $nb_gen_max;
147
151 public $user_author;
152
156 public $frequency;
157
161 public $unit_frequency;
162
166 public $rule_for_lines_dates;
167
171 public $rang;
172
176 public $special_code;
177
181 public $usenewprice = 0;
182
186 public $date_lim_reglement;
190 public $cond_reglement_code; // Code in llx_c_paiement
194 public $mode_reglement_code; // Code in llx_c_paiement
195
199 public $fk_societe_rib;
200
204 public $suspended; // status
205
209 public $auto_validate; // 0 to create in draft, 1 to create and validate the new invoice
210
214 public $generate_pdf; // 1 to generate PDF on invoice generation (default)
215
219 public $usenewcurrencyrate;
220
221
222
223 const PAYMENTCODETOEDITSOCIETERIB = "PRE";
224
225
250 // BEGIN MODULEBUILDER PROPERTIES
254 public $fields = array(
255 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
256 'titre' => array('type' => 'varchar(100)', 'label' => 'Titre', 'enabled' => 1, 'showoncombobox' => 1, 'visible' => -1, 'position' => 15),
257 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 20, 'index' => 1),
258 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 25),
259 'subtype' => array('type' => 'smallint(6)', 'label' => 'InvoiceSubtype', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
260 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 35),
261 'total_tva' => array('type' => 'double(24,8)', 'label' => 'Tva', 'enabled' => 1, 'visible' => -1, 'position' => 55, 'isameasure' => 1),
262 'localtax1' => array('type' => 'double(24,8)', 'label' => 'Localtax1', 'enabled' => 1, 'visible' => -1, 'position' => 60, 'isameasure' => 1),
263 'localtax2' => array('type' => 'double(24,8)', 'label' => 'Localtax2', 'enabled' => 1, 'visible' => -1, 'position' => 65, 'isameasure' => 1),
264 'total_ht' => array('type' => 'double(24,8)', 'label' => 'Total', 'enabled' => 1, 'visible' => -1, 'position' => 70, 'isameasure' => 1),
265 'total_ttc' => array('type' => 'double(24,8)', 'label' => 'Total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 75, 'isameasure' => 1),
266 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 80),
267 '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),
268 'fk_cond_reglement' => array('type' => 'integer', 'label' => 'Fk cond reglement', 'enabled' => 1, 'visible' => -1, 'position' => 90),
269 'fk_mode_reglement' => array('type' => 'integer', 'label' => 'Fk mode reglement', 'enabled' => 1, 'visible' => -1, 'position' => 95),
270 'date_lim_reglement' => array('type' => 'date', 'label' => 'Date lim reglement', 'enabled' => 1, 'visible' => -1, 'position' => 100),
271 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 105),
272 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 110),
273 'modelpdf' => array('type' => 'varchar(255)', 'label' => 'Modelpdf', 'enabled' => 1, 'visible' => -1, 'position' => 115),
274 'date_when' => array('type' => 'datetime', 'label' => 'Date when', 'enabled' => 1, 'visible' => -1, 'position' => 130),
275 'date_last_gen' => array('type' => 'datetime', 'label' => 'Date last gen', 'enabled' => 1, 'visible' => -1, 'position' => 135),
276 'nb_gen_done' => array('type' => 'integer', 'label' => 'Nb gen done', 'enabled' => 1, 'visible' => -1, 'position' => 140),
277 'nb_gen_max' => array('type' => 'integer', 'label' => 'Nb gen max', 'enabled' => 1, 'visible' => -1, 'position' => 145),
278 'frequency' => array('type' => 'integer', 'label' => 'Frequency', 'enabled' => 1, 'visible' => -1, 'position' => 150),
279 'unit_frequency' => array('type' => 'varchar(2)', 'label' => 'UnitFrequency', 'enabled' => 1, 'visible' => -1, 'position' => 152),
280 'rule_for_lines_dates' => array('type' => 'varchar(255)', 'label' => 'RuleForLinesDates', 'enabled' => 1, 'visible' => 1, 'position' => 153, 'arrayofkeyval' => array('prepaid' => "Prepaid", 'postpaid' => "Postpaid"), 'default' => 'prepaid'),
281 'usenewprice' => array('type' => 'integer', 'label' => 'UseNewPrice', 'enabled' => 1, 'visible' => 0, 'position' => 155),
282 'revenuestamp' => array('type' => 'double(24,8)', 'label' => 'RevenueStamp', 'enabled' => 1, 'visible' => -1, 'position' => 160, 'isameasure' => 1),
283 'auto_validate' => array('type' => 'integer', 'label' => 'Auto validate', 'enabled' => 1, 'visible' => -1, 'position' => 165),
284 'generate_pdf' => array('type' => 'integer', 'label' => 'Generate pdf', 'enabled' => 1, 'visible' => -1, 'position' => 170),
285 'fk_account' => array('type' => 'integer', 'label' => 'Fk account', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 175),
286 'fk_multicurrency' => array('type' => 'integer', 'label' => 'Fk multicurrency', 'enabled' => 1, 'visible' => -1, 'position' => 180),
287 'multicurrency_code' => array('type' => 'varchar(255)', 'label' => 'Multicurrency code', 'enabled' => 1, 'visible' => -1, 'position' => 185),
288 'multicurrency_tx' => array('type' => 'double(24,8)', 'label' => 'Multicurrency tx', 'enabled' => 1, 'visible' => -1, 'position' => 190, 'isameasure' => 1),
289 'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ht', 'enabled' => 1, 'visible' => -1, 'position' => 195, 'isameasure' => 1),
290 'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total tva', 'enabled' => 1, 'visible' => -1, 'position' => 200, 'isameasure' => 1),
291 'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 205, 'isameasure' => 1),
292 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 210),
293 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 215),
294 'suspended' => array('type' => 'integer', 'label' => 'Suspended', 'enabled' => 1, 'visible' => -1, 'position' => 225),
295 'fk_societe_rib' => array('type' => 'integer', 'label' => 'Fk Societe RIB', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 175),
296 );
297 // END MODULEBUILDER PROPERTIES
298
299 const STATUS_NOTSUSPENDED = 0;
300 const STATUS_SUSPENDED = 1;
301
302
303
309 public function __construct(DoliDB $db)
310 {
311 $this->db = $db;
312 }
313
323 public function create($user, $facid, $notrigger = 0, $onlylines = array())
324 {
325 global $conf;
326
327 $error = 0;
328 $now = dol_now();
329
330 // Clean parameters
331 $this->titre = trim(isset($this->titre) ? $this->titre : $this->title); // deprecated
332 $this->title = trim($this->title);
333 $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
334 if (empty($this->suspended)) {
335 $this->suspended = 0;
336 }
337 if (empty($this->rule_for_lines_dates)) {
338 $this->rule_for_lines_dates = 'prepaid';
339 }
340
341 // No frequency defined then no next date to execution
342 if (empty($this->frequency)) {
343 $this->frequency = 0;
344 $this->date_when = null;
345 }
346
347 $this->frequency = abs($this->frequency);
348 $this->nb_gen_done = 0;
349 $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
350 $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
351 $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
352
353 $this->db->begin();
354
355 // Load invoice template
356 $facsrc = new Facture($this->db);
357 $result = $facsrc->fetch($facid);
358 if ($result > 0) {
359 $this->socid = $facsrc->socid;
360
361 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_rec (";
362 $sql .= "titre";
363 $sql .= ", fk_soc";
364 $sql .= ", subtype";
365 $sql .= ", entity";
366 $sql .= ", datec";
367 $sql .= ", amount";
368 //$sql .= ", remise";
369 $sql .= ", note_private";
370 $sql .= ", note_public";
371 $sql .= ", modelpdf";
372 $sql .= ", fk_user_author";
373 $sql .= ", fk_projet";
374 $sql .= ", fk_account";
375 $sql .= ", fk_cond_reglement";
376 $sql .= ", fk_mode_reglement";
377 $sql .= ", usenewprice";
378 $sql .= ", frequency";
379 $sql .= ", unit_frequency";
380 $sql .= ", date_when";
381 $sql .= ", date_last_gen";
382 $sql .= ", nb_gen_done";
383 $sql .= ", nb_gen_max";
384 $sql .= ", auto_validate";
385 $sql .= ", generate_pdf";
386 $sql .= ", fk_multicurrency";
387 $sql .= ", multicurrency_code";
388 $sql .= ", multicurrency_tx";
389 $sql .= ", suspended";
390 $sql .= ", fk_societe_rib";
391 $sql .= ", rule_for_lines_dates";
392 $sql .= ") VALUES (";
393 $sql .= "'".$this->db->escape($this->titre ? $this->titre : $this->title)."'";
394 $sql .= ", ".((int) $this->socid);
395 $sql .= ", ".($this->subtype ? "'".$this->db->escape((string) $this->subtype)."'" : "null");
396 $sql .= ", ".((int) $conf->entity);
397 $sql .= ", '".$this->db->idate($now)."'";
398 $sql .= ", ".(!empty($facsrc->total_ttc) ? ((float) $facsrc->total_ttc) : '0');
399 //$sql .= ", ".(!empty($facsrc->remise_absolue) ? ((float) $this->remise_absolue) : '0');
400 $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
401 $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
402 $sql .= ", ".(!empty($this->model_pdf) ? ("'".$this->db->escape($this->model_pdf)."'") : "NULL");
403 $sql .= ", ".((int) $user->id);
404 $sql .= ", ".(!empty($this->fk_project) ? ((int) $this->fk_project) : "null");
405 $sql .= ", ".(!empty($facsrc->fk_account) ? ((int) $facsrc->fk_account) : "null");
406 $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : "null");
407 $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "null");
408 $sql .= ", ".((int) $this->usenewprice);
409 $sql .= ", ".((int) $this->frequency);
410 $sql .= ", '".$this->db->escape($this->unit_frequency)."'";
411 $sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'NULL');
412 $sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'NULL');
413 $sql .= ", ".((int) $this->nb_gen_done);
414 $sql .= ", ".((int) $this->nb_gen_max);
415 $sql .= ", ".((int) $this->auto_validate);
416 $sql .= ", ".((int) $this->generate_pdf);
417 $sql .= ", ".((int) $facsrc->fk_multicurrency);
418 $sql .= ", '".$this->db->escape($facsrc->multicurrency_code)."'";
419 $sql .= ", ".((float) $facsrc->multicurrency_tx);
420 $sql .= ", ".((int) $this->suspended);
421 $sql .= ", ".(!empty($this->fk_societe_rib) ? ((int) $this->fk_societe_rib) : 'NULL');
422 $sql .= ", '".$this->db->escape($this->rule_for_lines_dates)."'";
423 $sql .= ")";
424
425 if ($this->db->query($sql)) {
426 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec");
427
428 // Fields used into addline later
429 $this->fk_multicurrency = $facsrc->fk_multicurrency;
430 $this->multicurrency_code = $facsrc->multicurrency_code;
431 $this->multicurrency_tx = $facsrc->multicurrency_tx;
432
433 // Add lines
434 $fk_parent_line = 0;
435
436 $num = count($facsrc->lines);
437 for ($i = 0; $i < $num; $i++) {
438 $facline = $facsrc->lines[$i];
439 '@phan-var-force FactureLigneRec $facline';
440 if (!empty($onlylines) && !in_array($facline->id, $onlylines)) {
441 continue; // Skip unselected lines
442 }
443
444 // Reset fk_parent_line for no child products and special product
445 if (($facline->product_type != 9 && empty($facline->fk_parent_line)) || $facline->product_type == 9) {
446 $fk_parent_line = 0;
447 }
448
449 $tva_tx = $facline->tva_tx;
450 if (!empty($facline->vat_src_code) && !preg_match('/\‍(/', (string) $tva_tx)) {
451 $tva_tx .= ' ('.$facline->vat_src_code.')';
452 }
453
454 $default_start_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_START');
455 $default_end_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_END');
456
457 $result_insert = $this->addline(
458 $facline->desc,
459 $facline->subprice,
460 $facline->qty,
461 $tva_tx,
462 $facline->localtax1_tx,
463 $facline->localtax2_tx,
464 $facline->fk_product,
465 $facline->remise_percent,
466 'HT',
467 $facline->info_bits,
468 0,
469 0,
470 $facline->product_type,
471 $facline->rang,
472 $facline->special_code,
473 $facline->label,
474 $facline->fk_unit,
475 $facline->multicurrency_subprice,
476 $default_start_fill,
477 $default_end_fill,
478 0,
479 $facline->pa_ht,
480 $fk_parent_line
481 );
482
483 // Defined the new fk_parent_line
484 if ($result_insert > 0 && $facline->product_type == 9) {
485 $fk_parent_line = $result_insert;
486 }
487
488 if ($result_insert < 0) {
489 $error++;
490 } else {
491 $objectline = new FactureLigneRec($this->db);
492
493 $result2 = $objectline->fetch($result_insert);
494 if ($result2 > 0) {
495 // Extrafields
496 if (method_exists($facline, 'fetch_optionals')) {
497 $facline->fetch_optionals($facline->id);
498 $objectline->array_options = $facline->array_options;
499 }
500
501 $result = $objectline->insertExtraFields();
502 if ($result < 0) {
503 $error++;
504 }
505
506 $objectline->extraparams = $facline->extraparams;
507 $result = $objectline->setExtraParameters();
508 if ($result < 0) {
509 $error++;
510 }
511 } elseif ($result2 < 0) {
512 $this->errors[] = $objectline->error;
513 $error++;
514 }
515 }
516 }
517
518 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
519 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
520 }
521
522 // Add object linked
523 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
524 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
525 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, ...))
526 foreach ($tmp_origin_id as $origin_id) {
527 $ret = $this->add_object_linked($origin, $origin_id);
528 if (!$ret) {
529 $this->error = $this->db->lasterror();
530 $error++;
531 }
532 }
533 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
534 $origin_id = $tmp_origin_id;
535 $ret = $this->add_object_linked($origin, $origin_id);
536 if (!$ret) {
537 $this->error = $this->db->lasterror();
538 $error++;
539 }
540 }
541 }
542 }
543
544 if (!$error) {
545 $result = $this->insertExtraFields();
546 if ($result < 0) {
547 $error++;
548 }
549 }
550
551 if (!$error && !$notrigger) {
552 // Call trigger
553 $result = $this->call_trigger('BILLREC_CREATE', $user);
554 if ($result < 0) {
555 $this->db->rollback();
556 return -2;
557 }
558 // End call triggers
559 }
560
561 if ($error) {
562 $this->db->rollback();
563 return -3;
564 } else {
565 $this->db->commit();
566 return $this->id;
567 }
568 } else {
569 $this->error = $this->db->lasterror();
570 $this->db->rollback();
571 return -2;
572 }
573 } else {
574 $this->db->rollback();
575 return -1;
576 }
577 }
578
579
587 public function update(User $user, $notrigger = 0)
588 {
589 $error = 0;
590
591 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec SET";
592 $sql .= " entity = ".((int) $this->entity).",";
593 $sql .= " titre = '".$this->db->escape($this->title)."',";
594 $sql .= " suspended = ".((int) $this->suspended).",";
595 $sql .= " fk_soc = ".((int) $this->socid).",";
596 $sql .= " total_tva = ".((float) $this->total_tva).",";
597 $sql .= " localtax1 = ".((float) $this->total_localtax1).",";
598 $sql .= " localtax2 = ".((float) $this->total_localtax2).",";
599 $sql .= " total_ht = ".((float) $this->total_ht).",";
600 $sql .= " total_ttc = ".((float) $this->total_ttc).",";
601 $sql .= " fk_societe_rib = ".(!empty($this->fk_societe_rib) ? ((int) $this->fk_societe_rib) : 'NULL');
602
603 // TODO Add missing fields
604 $sql .= " WHERE rowid = ".((int) $this->id);
605
606 $this->db->begin();
607
608 dol_syslog(get_class($this)."::update", LOG_DEBUG);
609
610 $resql = $this->db->query($sql);
611 if ($resql) {
612 if (!$error) {
613 $result = $this->insertExtraFields();
614 if ($result < 0) {
615 $error++;
616 }
617 }
618
619 if (!$error && !$notrigger) {
620 // Call trigger
621 $result = $this->call_trigger('BILLREC_MODIFY', $user);
622 if ($result < 0) {
623 $this->db->rollback();
624 return -2;
625 }
626 // End call triggers
627 }
628 $this->db->commit();
629 return 1;
630 } else {
631 $this->error = $this->db->lasterror();
632 $this->db->rollback();
633 return -1;
634 }
635 }
636
647 public function fetch($rowid, $ref = '', $ref_ext = '', $noextrafields = 0, $nolines = 0)
648 {
649 dol_syslog('FactureRec::fetch', LOG_DEBUG);
650
651 $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';
652 $sql .= ', f.date_lim_reglement as dlr';
653 $sql .= ', f.note_private, f.note_public, f.fk_user_author';
654 $sql .= ', f.modelpdf as model_pdf';
655 $sql .= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project';
656 $sql .= ', f.fk_account, f.fk_societe_rib';
657 $sql .= ', f.frequency, f.unit_frequency, f.rule_for_lines_dates, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.usenewprice, f.auto_validate';
658 $sql .= ', f.generate_pdf';
659 $sql .= ", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
660 $sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
661 $sql .= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
662 //$sql.= ', el.fk_source';
663 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as f';
664 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
665 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id';
666 //$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = f.rowid AND el.targettype = 'facture'";
667 $sql .= ' WHERE f.entity IN ('.getEntity('invoice').')';
668 if ($rowid) {
669 $sql .= ' AND f.rowid = '.((int) $rowid);
670 } elseif ($ref) {
671 $sql .= " AND f.titre = '".$this->db->escape($ref)."'";
672 } else {
673 $sql .= ' AND f.rowid = 0';
674 }
675 /* This field are not used for template invoice
676 if ($ref_ext) $sql.= " AND f.ref_ext='".$this->db->escape($ref_ext)."'";
677 */
678
679 $result = $this->db->query($sql);
680 if ($result) {
681 if ($this->db->num_rows($result)) {
682 $obj = $this->db->fetch_object($result);
683
684 $this->id = $obj->rowid;
685 $this->entity = $obj->entity;
686 $this->titre = $obj->title; // deprecated
687 $this->title = $obj->title;
688 $this->ref = $obj->title;
689 $this->subtype = $obj->subtype;
690 $this->suspended = $obj->suspended;
691 $this->total_ht = $obj->total_ht;
692 $this->total_tva = $obj->total_tva;
693 $this->total_localtax1 = $obj->localtax1;
694 $this->total_localtax2 = $obj->localtax2;
695 $this->total_ttc = $obj->total_ttc;
696 $this->socid = $obj->fk_soc;
697 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
698 $this->mode_reglement_id = $obj->fk_mode_reglement;
699 $this->mode_reglement_code = $obj->mode_reglement_code;
700 $this->mode_reglement = $obj->mode_reglement_libelle;
701 $this->cond_reglement_id = $obj->fk_cond_reglement;
702 $this->cond_reglement_code = $obj->cond_reglement_code;
703 $this->cond_reglement = $obj->cond_reglement_libelle;
704 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
705 $this->fk_project = $obj->fk_project;
706 $this->fk_account = $obj->fk_account;
707 $this->fk_societe_rib = $obj->fk_societe_rib;
708 $this->note_private = $obj->note_private;
709 $this->note_public = $obj->note_public;
710 $this->user_author = $obj->fk_user_author; // deprecated
711 $this->user_creation_id = $obj->fk_user_author;
712 $this->model_pdf = $obj->model_pdf;
713 //$this->special_code = $obj->special_code;
714 $this->frequency = $obj->frequency;
715 $this->unit_frequency = $obj->unit_frequency;
716 $this->rule_for_lines_dates = $obj->rule_for_lines_dates;
717 $this->date_when = $this->db->jdate($obj->date_when);
718 $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
719 $this->nb_gen_done = $obj->nb_gen_done;
720 $this->nb_gen_max = $obj->nb_gen_max;
721 $this->usenewprice = $obj->usenewprice;
722 $this->auto_validate = $obj->auto_validate;
723 $this->generate_pdf = $obj->generate_pdf;
724
725 // Multicurrency
726 $this->fk_multicurrency = $obj->fk_multicurrency;
727 $this->multicurrency_code = $obj->multicurrency_code;
728 $this->multicurrency_tx = $obj->multicurrency_tx;
729 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
730 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
731 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
732
733 // Retrieve all extrafield
734 // fetch optionals attributes and labels
735 if (empty($noextrafields)) {
736 $result = $this->fetch_optionals();
737 if ($result < 0) {
738 $this->error = $this->db->lasterror();
739 return -4;
740 }
741 }
742
743 // Retrieve lines
744 if (empty($nolines)) {
745 $result = $this->fetch_lines();
746 if ($result < 0) {
747 $this->error = $this->db->lasterror();
748 return -3;
749 }
750 }
751
752 return 1;
753 } else {
754 $this->error = 'Bill with id '.$rowid.' or ref '.$ref.' not found';
755 dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
756 return -2;
757 }
758 } else {
759 $this->error = $this->db->error();
760 return -1;
761 }
762 }
763
764
770 public function getLinesArray()
771 {
772 return $this->fetch_lines();
773 }
774
775
776 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
782 public function fetch_lines()
783 {
784 // phpcs:enable
785
786 $this->lines = array();
787
788 dol_syslog('FactureRec::fetch_lines', LOG_DEBUG);
789
790 $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, ';
791 $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
792 $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,';
793 $sql .= ' l.rang, l.special_code,';
794 $sql .= ' l.fk_unit, l.fk_contract_line, l.extraparams,';
795 $sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
796 $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
797 $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
798 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
799 $sql .= ' WHERE l.fk_facture = '.((int) $this->id);
800 $sql .= ' ORDER BY l.rang';
801
802 $result = $this->db->query($sql);
803 if ($result) {
804 $num = $this->db->num_rows($result);
805 $i = 0;
806 while ($i < $num) {
807 $objp = $this->db->fetch_object($result);
808 $line = new FactureLigneRec($this->db);
809
810 $line->id = $objp->rowid;
811 $line->rowid = $objp->rowid;
812 $line->fk_facture = $objp->fk_facture;
813 $line->fk_parent_line = $objp->fk_parent_line;
814 $line->desc = $objp->description; // Description line
815 $line->description = $objp->description; // Description line
816 $line->ref = $objp->product_ref; // Ref product
817 $line->product_ref = $objp->product_ref; // Ref product
818 $line->libelle = $objp->product_label; // deprecated
819 $line->product_label = $objp->product_label; // Label product
820 $line->product_desc = $objp->product_desc; // Description product
821 $line->product_type = $objp->product_type; // Type of line
822 $line->fk_product_type = $objp->fk_product_type; // Type of product
823 $line->qty = $objp->qty;
824 $line->subprice = $objp->subprice;
825
826 $line->label = $objp->custom_label; // @deprecated
827
828 $line->vat_src_code = $objp->vat_src_code;
829 $line->tva_tx = $objp->tva_tx;
830 $line->localtax1_tx = $objp->localtax1_tx;
831 $line->localtax2_tx = $objp->localtax2_tx;
832 $line->localtax1_type = $objp->localtax1_type;
833 $line->localtax2_type = $objp->localtax2_type;
834 $line->remise_percent = $objp->remise_percent;
835 //$line->fk_remise_except = $objp->fk_remise_except;
836 $line->fk_product = $objp->fk_product;
837 $line->date_start_fill = $objp->date_start_fill;
838 $line->date_end_fill = $objp->date_end_fill;
839 $line->info_bits = $objp->info_bits;
840 $line->total_ht = $objp->total_ht;
841 $line->total_tva = $objp->total_tva;
842 $line->total_ttc = $objp->total_ttc;
843
844 $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
845 $line->fk_fournprice = $objp->fk_product_fournisseur_price; // For backward compatibility
846
847 $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
848
849 $line->buyprice = $marginInfos[0];
850 $line->pa_ht = $marginInfos[0]; // For backward compatibility
851 $line->marge_tx = (string) $marginInfos[1];
852 $line->marque_tx = (string) $marginInfos[2];
853 $line->rang = $objp->rang;
854 $line->special_code = $objp->special_code;
855 $line->fk_unit = $objp->fk_unit;
856 $line->fk_contract_line = $objp->fk_contract_line;
857
858 $line->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
859
860 // Ne plus utiliser
861 $line->price = $objp->price;
862 $line->remise = $objp->remise;
863
864 $line->fetch_optionals();
865
866 // Multicurrency
867 $line->fk_multicurrency = $objp->fk_multicurrency;
868 $line->multicurrency_code = $objp->multicurrency_code;
869 $line->multicurrency_subprice = $objp->multicurrency_subprice;
870 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
871 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
872 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
873
874 $this->lines[$i] = $line;
875
876 $i++;
877 }
878
879 $this->db->free($result);
880 return 1;
881 } else {
882 $this->error = $this->db->lasterror();
883 return -3;
884 }
885 }
886
887
896 public function delete(User $user, $notrigger = 0, $idwarehouse = -1)
897 {
898 $rowid = $this->id;
899
900 dol_syslog(get_class($this)."::delete rowid=".((int) $rowid), LOG_DEBUG);
901
902 $error = 0;
903 $this->db->begin();
904
905 $main = MAIN_DB_PREFIX.'facturedet_rec';
906 $ef = $main."_extrafields";
907
908 $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_facture = ".((int) $rowid).")";
909 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = ".((int) $rowid);
910
911 if ($this->db->query($sqlef) && $this->db->query($sql)) {
912 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_rec WHERE rowid = ".((int) $rowid);
913 dol_syslog($sql);
914 if ($this->db->query($sql)) {
915 // Delete linked object
916 $res = $this->deleteObjectLinked();
917 if ($res < 0) {
918 $error = -3;
919 }
920 // Delete extrafields
921 $res = $this->deleteExtraFields();
922 if ($res < 0) {
923 $error = -4;
924 }
925 } else {
926 $this->error = $this->db->lasterror();
927 $error = -1;
928 }
929 } else {
930 $this->error = $this->db->lasterror();
931 $error = -2;
932 }
933 if (!$error && !$notrigger) {
934 // Call trigger
935 $result = $this->call_trigger('BILLREC_DELETE', $user);
936 if ($result < 0) {
937 $error++;
938 }
939 // End call triggers
940 }
941 if (!$error) {
942 $this->db->commit();
943 return 1;
944 } else {
945 $this->db->rollback();
946 return $error;
947 }
948 }
949
950
979 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)
980 {
981 global $mysoc;
982
983 $facid = $this->id;
984
985 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);
986 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
987
988 // Check parameters
989 if ($type < 0) {
990 return -1;
991 }
992
993 $localtaxes_type = getLocalTaxesFromRate((float) $txtva, 0, $this->thirdparty, $mysoc);
994
995 // Clean vat code
996 $reg = array();
997 $vat_src_code = '';
998 if (preg_match('/\‍((.*)\‍)/', (string) $txtva, $reg)) {
999 $vat_src_code = $reg[1];
1000 $txtva = preg_replace('/\s*\‍(.*\‍)/', '', (string) $txtva); // Remove code from vatrate.
1001 }
1002
1003
1004 // Clean parameters
1005 $remise_percent = price2num($remise_percent);
1006 if (empty($remise_percent)) {
1007 $remise_percent = 0;
1008 }
1009 $qty = (float) price2num($qty);
1010 $pu_ht = (float) price2num($pu_ht);
1011 $pu_ttc = (float) price2num($pu_ttc);
1012 if (!preg_match('/\‍((.*)\‍)/', $txtva)) {
1013 $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1014 }
1015 $txlocaltax1 = (float) price2num($txlocaltax1);
1016 $txlocaltax2 = (float) price2num($txlocaltax2);
1017 if (empty($txtva)) {
1018 $txtva = 0;
1019 }
1020 if (empty($txlocaltax1)) {
1021 $txlocaltax1 = 0;
1022 }
1023 if (empty($txlocaltax2)) {
1024 $txlocaltax2 = 0;
1025 }
1026 if (empty($info_bits)) {
1027 $info_bits = 0;
1028 }
1029
1030 if ($price_base_type == 'HT') {
1031 $pu = $pu_ht;
1032 } else {
1033 $pu = $pu_ttc;
1034 }
1035
1036 // Calcul du total TTC et de la TVA pour la ligne a partir de
1037 // qty, pu, remise_percent et txtva
1038 // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1039 // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1040
1041 $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);
1042 $total_ht = $tabprice[0];
1043 $total_tva = $tabprice[1];
1044 $total_ttc = $tabprice[2];
1045 $total_localtax1 = $tabprice[9];
1046 $total_localtax2 = $tabprice[10];
1047 $pu_ht = $tabprice[3];
1048
1049 // MultiCurrency
1050 $multicurrency_total_ht = $tabprice[16];
1051 $multicurrency_total_tva = $tabprice[17];
1052 $multicurrency_total_ttc = $tabprice[18];
1053 $pu_ht_devise = $tabprice[19];
1054
1055 $product_type = $type;
1056 if ($fk_product) {
1057 $product = new Product($this->db);
1058 $result = $product->fetch($fk_product);
1059 $product_type = $product->type;
1060 }
1061
1062 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1063 $fk_parent_line = 0;
1064 }
1065
1066 // Rank to use
1067 $ranktouse = $rang;
1068 if ($ranktouse == -1) {
1069 $rangmax = $this->line_max(0);
1070 $ranktouse = $rangmax + 1;
1071 }
1072
1073 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (";
1074 $sql .= "fk_facture";
1075 $sql .= ", fk_parent_line";
1076 $sql .= ", label";
1077 $sql .= ", description";
1078 $sql .= ", price";
1079 $sql .= ", qty";
1080 $sql .= ", tva_tx";
1081 $sql .= ", vat_src_code";
1082 $sql .= ", localtax1_tx";
1083 $sql .= ", localtax1_type";
1084 $sql .= ", localtax2_tx";
1085 $sql .= ", localtax2_type";
1086 $sql .= ", fk_product";
1087 $sql .= ", product_type";
1088 $sql .= ", remise_percent";
1089 $sql .= ", subprice";
1090 $sql .= ", remise";
1091 $sql .= ", total_ht";
1092 $sql .= ", total_tva";
1093 $sql .= ", total_localtax1";
1094 $sql .= ", total_localtax2";
1095 $sql .= ", total_ttc";
1096 $sql .= ", date_start_fill";
1097 $sql .= ", date_end_fill";
1098 $sql .= ", fk_product_fournisseur_price";
1099 $sql .= ", buy_price_ht";
1100 $sql .= ", info_bits";
1101 $sql .= ", rang";
1102 $sql .= ", special_code";
1103 $sql .= ", fk_unit";
1104 $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
1105 $sql .= ") VALUES (";
1106 $sql .= " ".((int) $facid);
1107 $sql .= ", ".($fk_parent_line > 0 ? ((int) $fk_parent_line) : "null");
1108 $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1109 $sql .= ", '".$this->db->escape($desc)."'";
1110 $sql .= ", ".price2num($pu_ht);
1111 $sql .= ", ".price2num($qty);
1112 $sql .= ", ".price2num($txtva);
1113 $sql .= ", '".$this->db->escape($vat_src_code)."'";
1114 $sql .= ", ".price2num($txlocaltax1);
1115 $sql .= ", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] : '')."'";
1116 $sql .= ", ".price2num($txlocaltax2);
1117 $sql .= ", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] : '')."'";
1118 $sql .= ", ".(!empty($fk_product) ? "'".$this->db->escape((string) $fk_product)."'" : "null");
1119 $sql .= ", ".((int) $product_type);
1120 $sql .= ", ".price2num($remise_percent);
1121 $sql .= ", ".price2num($pu_ht);
1122 $sql .= ", null";
1123 $sql .= ", ".price2num($total_ht);
1124 $sql .= ", ".price2num($total_tva);
1125 $sql .= ", ".price2num($total_localtax1);
1126 $sql .= ", ".price2num($total_localtax2);
1127 $sql .= ", ".price2num($total_ttc);
1128 $sql .= ", ".(int) $date_start_fill;
1129 $sql .= ", ".(int) $date_end_fill;
1130 $sql .= ", ".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1131 $sql .= ", ".($pa_ht ? price2num($pa_ht) : 0);
1132 $sql .= ", ".((int) $info_bits);
1133 $sql .= ", ".((int) $ranktouse);
1134 $sql .= ", ".((int) $special_code);
1135 $sql .= ", ".($fk_unit ? ((int) $fk_unit) : "null");
1136 $sql .= ", ".(int) $this->fk_multicurrency;
1137 $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
1138 $sql .= ", ".price2num($pu_ht_devise, 'CU');
1139 $sql .= ", ".price2num($multicurrency_total_ht, 'CT');
1140 $sql .= ", ".price2num($multicurrency_total_tva, 'CT');
1141 $sql .= ", ".price2num($multicurrency_total_ttc, 'CT');
1142 $sql .= ")";
1143
1144 dol_syslog(get_class($this)."::addline", LOG_DEBUG);
1145 if ($this->db->query($sql)) {
1146 $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX."facturedet_rec");
1147 $this->id = $facid;
1148 $this->update_price(1);
1149 return $lineId;
1150 } else {
1151 $this->error = $this->db->lasterror();
1152 return -1;
1153 }
1154 }
1155
1186 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)
1187 {
1188 global $mysoc;
1189
1190 $facid = $this->id;
1191
1192 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);
1193 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1194
1195 // Clean parameters
1196 if (empty($remise_percent)) {
1197 $remise_percent = 0;
1198 }
1199
1200 // Check parameters
1201 if ($type < 0) {
1202 return -1;
1203 }
1204
1205 // Clean parameters
1206 $remise_percent = (float) price2num($remise_percent);
1207 $qty = (float) price2num($qty);
1208 if (empty($info_bits)) {
1209 $info_bits = 0;
1210 }
1211 $pu_ht = (float) price2num($pu_ht);
1212 $pu_ttc = (float) price2num($pu_ttc);
1213 $pu_ht_devise = (float) price2num($pu_ht_devise);
1214 if (!preg_match('/\‍((.*)\‍)/', (string) $txtva)) {
1215 $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1216 }
1217 $txlocaltax1 = (float) price2num($txlocaltax1);
1218 $txlocaltax2 = (float) price2num($txlocaltax2);
1219 if (empty($txlocaltax1)) {
1220 $txlocaltax1 = 0;
1221 }
1222 if (empty($txlocaltax2)) {
1223 $txlocaltax2 = 0;
1224 }
1225
1226 if (empty($this->multicurrency_subprice)) {
1227 $this->multicurrency_subprice = 0;
1228 }
1229 if (empty($this->multicurrency_total_ht)) {
1230 $this->multicurrency_total_ht = 0;
1231 }
1232 if (empty($this->multicurrency_total_tva)) {
1233 $this->multicurrency_total_tva = 0;
1234 }
1235 if (empty($this->multicurrency_total_ttc)) {
1236 $this->multicurrency_total_ttc = 0;
1237 }
1238
1239 if ($price_base_type == 'HT') {
1240 $pu = $pu_ht;
1241 } else {
1242 $pu = $pu_ttc;
1243 }
1244
1245 // Calculate total with, without tax and tax from qty, pu, remise_percent and txtva
1246 // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1247 // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1248
1249 $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
1250
1251 // Clean vat code
1252 $vat_src_code = '';
1253 $reg = array();
1254 if (preg_match('/\‍((.*)\‍)/', $txtva, $reg)) {
1255 $vat_src_code = $reg[1];
1256 $txtva = preg_replace('/\s*\‍(.*\‍)/', '', $txtva); // Remove code into vatrate.
1257 }
1258
1259 $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);
1260
1261 $total_ht = $tabprice[0];
1262 $total_tva = $tabprice[1];
1263 $total_ttc = $tabprice[2];
1264 $total_localtax1 = $tabprice[9];
1265 $total_localtax2 = $tabprice[10];
1266 $pu_ht = $tabprice[3];
1267 $pu_tva = $tabprice[4];
1268 $pu_ttc = $tabprice[5];
1269
1270 // MultiCurrency
1271 $multicurrency_total_ht = $tabprice[16];
1272 $multicurrency_total_tva = $tabprice[17];
1273 $multicurrency_total_ttc = $tabprice[18];
1274 $pu_ht_devise = $tabprice[19];
1275
1276 $product_type = $type;
1277 if ($fk_product) {
1278 $product = new Product($this->db);
1279 $result = $product->fetch($fk_product);
1280 $product_type = $product->type;
1281 }
1282
1283 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1284 $fk_parent_line = 0;
1285 }
1286
1287 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET ";
1288 $sql .= "fk_facture = ".((int) $facid);
1289 $sql .= ", fk_parent_line = ".($fk_parent_line > 0 ? ((int) $fk_parent_line) : "null");
1290 $sql .= ", label=".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1291 $sql .= ", description='".$this->db->escape($desc)."'";
1292 $sql .= ", price=".price2num($pu_ht);
1293 $sql .= ", qty=".price2num($qty);
1294 $sql .= ", tva_tx=".price2num($txtva);
1295 $sql .= ", vat_src_code='".$this->db->escape($vat_src_code)."'";
1296 $sql .= ", localtax1_tx=".((float) $txlocaltax1);
1297 $sql .= ", localtax1_type='".$this->db->escape($localtaxes_type[0])."'";
1298 $sql .= ", localtax2_tx=".((float) $txlocaltax2);
1299 $sql .= ", localtax2_type='".$this->db->escape($localtaxes_type[2])."'";
1300 $sql .= ", fk_product=".(!empty($fk_product) ? "'".$this->db->escape((string) $fk_product)."'" : "null");
1301 $sql .= ", product_type=".((int) $product_type);
1302 $sql .= ", remise_percent='".price2num($remise_percent)."'";
1303 $sql .= ", subprice='".price2num($pu_ht)."'";
1304 $sql .= ", total_ht='".price2num($total_ht)."'";
1305 $sql .= ", total_tva='".price2num($total_tva)."'";
1306 $sql .= ", total_localtax1='".price2num($total_localtax1)."'";
1307 $sql .= ", total_localtax2='".price2num($total_localtax2)."'";
1308 $sql .= ", total_ttc='".price2num($total_ttc)."'";
1309 $sql .= ", date_start_fill=".((int) $date_start_fill);
1310 $sql .= ", date_end_fill=".((int) $date_end_fill);
1311 $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? (int) $fk_fournprice : 'null');
1312 $sql .= ", buy_price_ht=".($pa_ht ? price2num($pa_ht) : 0);
1313 $sql .= ", info_bits=".((int) $info_bits);
1314 $sql .= ", rang=".((int) $rang);
1315 $sql .= ", special_code=".((int) $special_code);
1316 $sql .= ", fk_unit=".($fk_unit ? "'".$this->db->escape((string) $fk_unit)."'" : "null");
1317 $sql .= ', multicurrency_subprice = '.price2num($pu_ht_devise);
1318 $sql .= ', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1319 $sql .= ', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1320 $sql .= ', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1321 $sql .= " WHERE rowid = ".((int) $rowid);
1322
1323 dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1324 if ($this->db->query($sql)) {
1325 $this->id = $facid;
1326 $this->update_price(1);
1327 return 1;
1328 } else {
1329 $this->error = $this->db->lasterror();
1330 return -1;
1331 }
1332 }
1333
1334
1340 public function getNextDate()
1341 {
1342 if (empty($this->date_when)) {
1343 return false;
1344 }
1345 return dol_time_plus_duree($this->date_when, $this->frequency, $this->unit_frequency, 1);
1346 }
1347
1353 public function isMaxNbGenReached()
1354 {
1355 $ret = false;
1356 if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1357 $ret = true;
1358 }
1359 return $ret;
1360 }
1361
1368 public function strikeIfMaxNbGenReached($ret)
1369 {
1370 return $this->isMaxNbGenReached() ? '<strike>'.$ret.'</strike>' : $ret;
1371 }
1372
1384 public function createRecurringInvoices($restrictioninvoiceid = 0, $forcevalidation = 0, $notrigger = 0)
1385 {
1386 global $conf, $langs, $user, $hookmanager, $action;
1387
1388 $error = 0;
1389 $nb_create = 0;
1390
1391 // Load translation files required by the page
1392 $langs->loadLangs(array("main", "bills"));
1393
1394 $now = dol_now();
1395 $tmparray = dol_getdate($now);
1396 $today = dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year']); // Today is last second of current day
1397
1398 $this->output = '';
1399
1400 dol_syslog("createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid." forcevalidation=".$forcevalidation);
1401
1402 $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_rec';
1403 $sql .= ' WHERE frequency > 0'; // A recurring invoice is an invoice with a frequency
1404 $sql .= " AND (date_when IS NULL OR date_when <= '".$this->db->idate($today)."')";
1405 $sql .= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1406 $sql .= ' AND suspended = 0';
1407 $sql .= ' AND entity = '.$conf->entity; // MUST STAY = $conf->entity here
1408 if ($restrictioninvoiceid > 0) {
1409 $sql .= ' AND rowid = '.((int) $restrictioninvoiceid);
1410 }
1411 $sql .= $this->db->order('entity', 'ASC');
1412 //print $sql;exit;
1413 $parameters = array(
1414 'restrictioninvoiceid' => $restrictioninvoiceid,
1415 'forcevalidation' => $forcevalidation,
1416 );
1417 $reshook = $hookmanager->executeHooks('beforeCreationOfRecurringInvoices', $parameters, $sql); // note that $sql might be modified by hooks
1418
1419 $resql = $this->db->query($sql);
1420 if ($resql) {
1421 $i = 0;
1422 $num = $this->db->num_rows($resql);
1423
1424 if ($num) {
1425 $this->output .= $langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n";
1426 } else {
1427 $this->output .= $langs->trans("NoQualifiedRecurringInvoiceTemplateFound");
1428 }
1429
1430 $saventity = $conf->entity;
1431
1432 while ($i < $num) { // Loop on each template invoice. If $num = 0, test is false at first pass.
1433 $line = $this->db->fetch_object($resql);
1434
1435 $this->db->begin();
1436
1437 $errorforinvoice = 0;
1438 $invoiceidgenerated = 0;
1439
1440 $facture = null;
1441 $facturerec = new FactureRec($this->db);
1442 $facturerec->fetch($line->rowid);
1443
1444 if ($facturerec->id > 0) {
1445 // Set entity context
1446 $conf->entity = $facturerec->entity;
1447
1448 dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity);
1449
1450 $facture = new Facture($this->db);
1451 $facture->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice
1452 $facture->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice
1453
1454 $facture->type = self::TYPE_STANDARD;
1455 $facture->subtype = $facturerec->subtype;
1456 $facture->statut = self::STATUS_DRAFT; // deprecated
1457 $facture->status = self::STATUS_DRAFT;
1458 $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.
1459 $facture->socid = $facturerec->socid;
1460
1461 if (!empty($facturerec->fk_multicurrency)) {
1462 $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1463 $facture->multicurrency_code = $facturerec->multicurrency_code;
1464 $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1465 }
1466
1467 if (isset($facture->array_options) && isset($facturerec->array_options)) {
1468 foreach ($facturerec->array_options as $key => $value) {
1469 if (isset($facture->array_options[$key])) {
1470 $facture->array_options[$key] = $value;
1471 }
1472 }
1473 }
1474
1475 $parameters['facture'] = &$facture;
1476 $reshook = $hookmanager->executeHooks('beforeCreationOfEachRecurringInvoice', $parameters, $facturerec, $action); // note that $facturerec or $facture might be modified by hooks
1477
1478 // Create invoice. This may update prices according to multiplrice rules
1479 $invoiceidgenerated = $facture->create($user, 0, 0, (isModEnabled('multicurrency') ? $facturerec->usenewcurrencyrate : 0));
1480 if ($invoiceidgenerated <= 0) {
1481 $this->setErrorsFromObject($facture);
1482 $error++;
1483 $errorforinvoice++;
1484 }
1485
1486
1487 if (!$errorforinvoice && ($facturerec->auto_validate || $forcevalidation)) {
1488 $result = $facture->validate($user);
1489 if ($result <= 0) {
1490 $this->setErrorsFromObject($facture);
1491 $error++;
1492 $errorforinvoice++;
1493 }
1494 }
1495 if (!$errorforinvoice && $facturerec->generate_pdf) {
1496 // We refresh the object in order to have all necessary data (like date_lim_reglement)
1497 $facture->fetch($facture->id);
1498 $result = $facture->generateDocument($facturerec->model_pdf, $langs);
1499 if ($result <= 0) {
1500 $this->setErrorsFromObject($facture);
1501 $error++;
1502 $errorforinvoice++;
1503 }
1504 }
1505 } else {
1506 $error++;
1507 $this->error = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity."\n";
1508 $this->errors[] = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity;
1509 dol_syslog("createRecurringInvoices Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity);
1510 }
1511
1512 // Commit or rollback
1513 if (!$error && $invoiceidgenerated >= 0) {
1514 $this->db->commit("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1515 dol_syslog("createRecurringInvoices Process invoice template ".$facturerec->ref." is finished with a success generation");
1516 $nb_create++;
1517 $this->output .= $langs->trans("InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref)."\n";
1518 } else {
1519 $this->output .= $langs->trans("InvoiceGeneratedFromTemplateError", $facture->ref, $facturerec->ref, $this->error)."\n";
1520 $this->db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1521 }
1522
1523 $parameters = array(
1524 'cpt' => $i,
1525 'total' => $num,
1526 'errorCount' => $error,
1527 'errorForInvoice' => $errorforinvoice,
1528 'invoiceidgenerated' => $invoiceidgenerated,
1529 'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks.
1530 'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks.
1531 );
1532 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable
1533 $reshook = $hookmanager->executeHooks('afterCreationOfRecurringInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null)
1534
1535 $i++;
1536 }
1537
1538 $conf->entity = $saventity; // Restore entity context
1539 } else {
1540 dol_print_error($this->db);
1541 }
1542
1543 $this->output = trim($this->output);
1544
1545 return $error ? $error : 0;
1546 }
1547
1560 public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1)
1561 {
1562 global $langs, $hookmanager;
1563
1564 $result = '';
1565
1566 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("RepeatableInvoice").'</u>';
1567 if (!empty($this->ref)) {
1568 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1569 }
1570 if ($this->frequency > 0) {
1571 $label .= '<br><b>'.$langs->trans('Frequency').':</b> '.$langs->trans('FrequencyPer_'.$this->unit_frequency, $this->frequency);
1572 }
1573 if (!empty($this->date_last_gen)) {
1574 $label .= '<br><b>'.$langs->trans('DateLastGeneration').':</b> '.dol_print_date($this->date_last_gen, 'dayhour');
1575 }
1576 if ($this->frequency > 0) {
1577 if (!empty($this->date_when)) {
1578 $label .= '<br><b>'.$langs->trans('NextDateToExecution').':</b> ';
1579 $label .= (empty($this->suspended) ? '' : '<strike>').dol_print_date($this->date_when, 'day').(empty($this->suspended) ? '' : '</strike>'); // No hour for this property
1580 if (!empty($this->suspended)) {
1581 $label .= ' ('.$langs->trans("Disabled").')';
1582 }
1583 }
1584 }
1585
1586 $url = DOL_URL_ROOT.'/compta/facture/card-rec.php?facid='.$this->id;
1587
1588 if ($short) {
1589 return $url;
1590 }
1591
1592 if ($option != 'nolink') {
1593 // Add param to save lastsearch_values or not
1594 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1595 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1596 $add_save_lastsearch_values = 1;
1597 }
1598 if ($add_save_lastsearch_values) {
1599 $url .= '&save_lastsearch_values=1';
1600 }
1601 }
1602
1603 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1604 $linkend = '</a>';
1605
1606 $result .= $linkstart;
1607 if ($withpicto) {
1608 $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);
1609 }
1610 if ($withpicto != 2) {
1611 $result .= dol_trunc($this->ref, $max);
1612 }
1613 $result .= $linkend;
1614 global $action;
1615 $hookmanager->initHooks(array($this->element . 'dao'));
1616 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1617 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1618 if ($reshook > 0) {
1619 $result = $hookmanager->resPrint;
1620 } else {
1621 $result .= $hookmanager->resPrint;
1622 }
1623 return $result;
1624 }
1625
1633 public function getLibStatut($mode = 0, $alreadypaid = -1)
1634 {
1635 return $this->LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->type) ? 0 : $this->type);
1636 }
1637
1638 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1650 public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0, $nbofopendirectdebitorcredittransfer = 0)
1651 {
1652 // phpcs:enable
1653 global $langs;
1654 $langs->load('bills');
1655
1656 $labelStatus = $langs->transnoentitiesnoconv('Active');
1657 $statusType = 'status0';
1658
1659 //print "$recur,$status,$mode,$alreadypaid,$type";
1660 if ($mode == 0) {
1661 if ($recur) {
1662 if ($status == self::STATUS_SUSPENDED) {
1663 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1664 } else {
1665 $labelStatus = $langs->transnoentitiesnoconv('Active');
1666 }
1667 } else {
1668 if ($status == self::STATUS_SUSPENDED) {
1669 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1670 } else {
1671 $labelStatus = $langs->transnoentitiesnoconv("Draft");
1672 }
1673 }
1674 } elseif ($mode == 1) {
1675 $prefix = 'Short';
1676 if ($recur) {
1677 if ($status == self::STATUS_SUSPENDED) {
1678 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1679 } else {
1680 $labelStatus = $langs->transnoentitiesnoconv('Active');
1681 }
1682 } else {
1683 if ($status == self::STATUS_SUSPENDED) {
1684 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1685 } else {
1686 $labelStatus = $langs->transnoentitiesnoconv("Draft");
1687 }
1688 }
1689 } elseif ($mode == 2) {
1690 if ($recur) {
1691 if ($status == self::STATUS_SUSPENDED) {
1692 $statusType = 'status6';
1693 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1694 } else {
1695 $statusType = 'status4';
1696 $labelStatus = $langs->transnoentitiesnoconv('Active');
1697 }
1698 } else {
1699 if ($status == self::STATUS_SUSPENDED) {
1700 $statusType = 'status6';
1701 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1702 } else {
1703 $statusType = 'status0';
1704 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1705 }
1706 }
1707 } elseif ($mode == 3) {
1708 if ($recur) {
1709 $prefix = 'Short';
1710 if ($status == self::STATUS_SUSPENDED) {
1711 $statusType = 'status6';
1712 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1713 } else {
1714 $statusType = 'status4';
1715 $labelStatus = $langs->transnoentitiesnoconv('Active');
1716 }
1717 } else {
1718 if ($status == self::STATUS_SUSPENDED) {
1719 $statusType = 'status6';
1720 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1721 } else {
1722 $statusType = 'status0';
1723 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1724 }
1725 }
1726 } elseif ($mode == 4) {
1727 $prefix = '';
1728 if ($recur) {
1729 if ($status == self::STATUS_SUSPENDED) {
1730 $statusType = 'status6';
1731 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1732 } else {
1733 $statusType = 'status4';
1734 $labelStatus = $langs->transnoentitiesnoconv('Active');
1735 }
1736 } else {
1737 if ($status == self::STATUS_SUSPENDED) {
1738 $statusType = 'status6';
1739 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1740 } else {
1741 $statusType = 'status0';
1742 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1743 }
1744 }
1745 } elseif ($mode == 5 || $mode == 6) {
1746 $prefix = '';
1747 if ($mode == 5) {
1748 $prefix = 'Short';
1749 }
1750 if ($recur) {
1751 if ($status == self::STATUS_SUSPENDED) {
1752 $statusType = 'status6';
1753 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1754 } else {
1755 $statusType = 'status4';
1756 $labelStatus = $langs->transnoentitiesnoconv('Active');
1757 }
1758 } else {
1759 if ($status == self::STATUS_SUSPENDED) {
1760 $statusType = 'status6';
1761 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1762 } else {
1763 $statusType = 'status0';
1764 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1765 }
1766 }
1767 }
1768
1769 $labelStatusShort = $labelStatus;
1770
1771 return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1772 }
1773
1781 public function getNextNumRef($soc, $mode = 'next')
1782 {
1783 // Not used for recurring invoices
1784 return '';
1785 }
1786
1793 public function info($id)
1794 {
1795 $sql = 'SELECT c.rowid, datec, tms as datem,';
1796 $sql .= ' fk_user_author, fk_user_modif';
1797 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as c';
1798 $sql .= ' WHERE c.rowid = '.((int) $id);
1799
1800 $result = $this->db->query($sql);
1801 if ($result) {
1802 if ($this->db->num_rows($result)) {
1803 $obj = $this->db->fetch_object($result);
1804
1805 $this->id = $obj->rowid;
1806
1807 $this->user_creation_id = $obj->fk_user_author;
1808 $this->user_modification_id = $obj->fk_user_modif;
1809 $this->date_creation = $this->db->jdate($obj->datec);
1810 $this->date_modification = $this->db->jdate($obj->datem);
1811 }
1812 $this->db->free($result);
1813 } else {
1814 dol_print_error($this->db);
1815 }
1816 }
1817
1826 public function initAsSpecimen($option = '')
1827 {
1828 global $langs;
1829
1830 $now = dol_now();
1831 $arraynow = dol_getdate($now);
1832 $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1833
1834 // Load array of products prodids
1835 $num_prods = 0;
1836 $prodids = array();
1837
1838 $sql = "SELECT rowid";
1839 $sql .= " FROM ".MAIN_DB_PREFIX."product";
1840 $sql .= " WHERE entity IN (".getEntity('product').")";
1841 $sql .= $this->db->plimit(100);
1842
1843 $resql = $this->db->query($sql);
1844 if ($resql) {
1845 $num_prods = $this->db->num_rows($resql);
1846 $i = 0;
1847 while ($i < $num_prods) {
1848 $i++;
1849 $row = $this->db->fetch_row($resql);
1850 $prodids[$i] = $row[0];
1851 }
1852 }
1853
1854 // Initialize parameters
1855 $this->id = 0;
1856 $this->ref = 'SPECIMEN';
1857 $this->title = 'SPECIMEN';
1858 $this->specimen = 1;
1859 $this->socid = 1;
1860 $this->date = $nownotime;
1861 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
1862 $this->cond_reglement_id = 1;
1863 $this->cond_reglement_code = 'RECEP';
1864 $this->date_lim_reglement = $this->calculate_date_lim_reglement();
1865 $this->mode_reglement_id = 0; // Not forced to show payment mode CHQ + VIR
1866 $this->mode_reglement_code = ''; // Not forced to show payment mode CHQ + VIR
1867 $this->note_public = 'This is a comment (public)';
1868 $this->note_private = 'This is a comment (private)';
1869 $this->fk_incoterms = 0;
1870 $this->location_incoterms = '';
1871
1872 if (empty($option) || $option != 'nolines') {
1873 // Lines
1874 $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)
1875 $xnbp = 0;
1876 while ($xnbp < $nbp) {
1877 $line = new FactureLigne($this->db);
1878 $line->desc = $langs->trans("Description")." ".$xnbp;
1879 $line->qty = 1;
1880 $line->subprice = 100;
1881 $line->tva_tx = 19.6;
1882 $line->localtax1_tx = 0;
1883 $line->localtax2_tx = 0;
1884 $line->remise_percent = 0;
1885 if ($xnbp == 1) { // Qty is negative (product line)
1886 $prodid = mt_rand(1, $num_prods);
1887 $line->fk_product = $prodids[$prodid];
1888 $line->qty = -1;
1889 $line->total_ht = -100;
1890 $line->total_ttc = -119.6;
1891 $line->total_tva = -19.6;
1892 } elseif ($xnbp == 2) { // UP is negative (free line)
1893 $line->subprice = -100;
1894 $line->total_ht = -100;
1895 $line->total_ttc = -119.6;
1896 $line->total_tva = -19.6;
1897 $line->remise_percent = 0;
1898 } elseif ($xnbp == 3) { // Discount is 50% (product line)
1899 $prodid = mt_rand(1, $num_prods);
1900 $line->fk_product = $prodids[$prodid];
1901 $line->total_ht = 50;
1902 $line->total_ttc = 59.8;
1903 $line->total_tva = 9.8;
1904 $line->remise_percent = 50;
1905 } else { // (product line)
1906 $prodid = mt_rand(1, $num_prods);
1907 $line->fk_product = $prodids[$prodid];
1908 $line->total_ht = 100;
1909 $line->total_ttc = 119.6;
1910 $line->total_tva = 19.6;
1911 $line->remise_percent = 00;
1912 }
1913
1914 $this->lines[$xnbp] = $line;
1915 $xnbp++;
1916
1917 $this->total_ht += $line->total_ht;
1918 $this->total_tva += $line->total_tva;
1919 $this->total_ttc += $line->total_ttc;
1920 }
1921 $this->revenuestamp = 0;
1922
1923 // Add a line "offered"
1924 $line = new FactureLigne($this->db);
1925 $line->desc = $langs->trans("Description")." (offered line)";
1926 $line->qty = 1;
1927 $line->subprice = 100;
1928 $line->tva_tx = 19.6;
1929 $line->localtax1_tx = 0;
1930 $line->localtax2_tx = 0;
1931 $line->remise_percent = 100;
1932 $line->total_ht = 0;
1933 $line->total_ttc = 0; // 90 * 1.196
1934 $line->total_tva = 0;
1935 $prodid = mt_rand(1, $num_prods);
1936 $line->fk_product = $prodids[$prodid];
1937
1938 $this->lines[$xnbp] = $line;
1939 $xnbp++;
1940 }
1941
1942 $this->usenewprice = 0;
1943
1944 return 1;
1945 }
1946
1955 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1956 {
1957 $tables = array(
1958 'facture_rec'
1959 );
1960
1961 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1962 }
1963
1972 public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1973 {
1974 $tables = array(
1975 'facturedet_rec'
1976 );
1977
1978 return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1979 }
1980
1989 public function setFrequencyAndUnit($frequency, $unit, $notrigger = 0)
1990 {
1991 global $user;
1992
1993 if (!$this->table_element) {
1994 dol_syslog(get_class($this)."::setFrequencyAndUnit was called on object with property table_element not defined", LOG_ERR);
1995 return -1;
1996 }
1997
1998 if (!empty($frequency) && empty($unit)) {
1999 dol_syslog(get_class($this)."::setFrequencyAndUnit was called on object with params frequency defined but unit not defined", LOG_ERR);
2000 return -2;
2001 }
2002
2003 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2004 $sql .= ' SET frequency = '.($frequency ? $this->db->escape((string) $frequency) : 'null');
2005 if (!empty($unit)) {
2006 $sql .= ', unit_frequency = \''.$this->db->escape($unit).'\'';
2007 }
2008 $sql .= " WHERE rowid = ".((int) $this->id);
2009
2010 dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG);
2011 if ($this->db->query($sql)) {
2012 $this->frequency = $frequency;
2013 if (!empty($unit)) {
2014 $this->unit_frequency = $unit;
2015 }
2016
2017 if (!$notrigger) {
2018 // Call trigger
2019 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2020 if ($result < 0) {
2021 return $result;
2022 }
2023 // End call triggers
2024 }
2025
2026 return 1;
2027 } else {
2028 dol_print_error($this->db);
2029 return -1;
2030 }
2031 }
2032
2041 public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
2042 {
2043 global $user;
2044
2045 if (!$this->table_element) {
2046 dol_syslog(get_class($this)."::setNextDate was called on object with property table_element not defined", LOG_ERR);
2047 return -1;
2048 }
2049 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2050 $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
2051 if ($increment_nb_gen_done > 0) {
2052 $sql .= ', nb_gen_done = nb_gen_done + 1';
2053 }
2054 $sql .= " WHERE rowid = ".((int) $this->id);
2055
2056 dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG);
2057 if ($this->db->query($sql)) {
2058 $this->date_when = $date;
2059 if ($increment_nb_gen_done > 0) {
2060 $this->nb_gen_done++;
2061
2062 if (getDolGlobalInt('MAIN_SUSPEND_FACTURE_REC_ON_MAX_GEN_REACHED') && $this->isMaxNbGenReached()) {
2063 $resSuspend = $this->setValueFrom('suspended', 1);
2064
2065 if ($resSuspend <= 0) {
2066 dol_syslog(__METHOD__ . '::setValueFrom Error : ' . $this->error, LOG_ERR);
2067 return -1;
2068 }
2069 }
2070 }
2071
2072 if (!$notrigger) {
2073 // Call trigger
2074 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2075 if ($result < 0) {
2076 return $result;
2077 }
2078 // End call triggers
2079 }
2080 return 1;
2081 } else {
2082 dol_print_error($this->db);
2083 return -1;
2084 }
2085 }
2086
2094 public function setMaxPeriod($nb, $notrigger = 0)
2095 {
2096 global $user;
2097
2098 if (!$this->table_element) {
2099 dol_syslog(get_class($this)."::setMaxPeriod was called on object with property table_element not defined", LOG_ERR);
2100 return -1;
2101 }
2102
2103 if (empty($nb)) {
2104 $nb = 0;
2105 }
2106
2107 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2108 $sql .= ' SET nb_gen_max = '.((int) $nb);
2109 $sql .= " WHERE rowid = ".((int) $this->id);
2110
2111 dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG);
2112 if ($this->db->query($sql)) {
2113 $this->nb_gen_max = $nb;
2114
2115 if (!$notrigger) {
2116 // Call trigger
2117 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2118 if ($result < 0) {
2119 return $result;
2120 }
2121 // End call triggers
2122 }
2123
2124 return 1;
2125 } else {
2126 dol_print_error($this->db);
2127 return -1;
2128 }
2129 }
2130
2138 public function setAutoValidate($validate, $notrigger = 0)
2139 {
2140 global $user;
2141
2142 if (!$this->table_element) {
2143 dol_syslog(get_class($this)."::setAutoValidate was called on object with property table_element not defined", LOG_ERR);
2144 return -1;
2145 }
2146
2147 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2148 $sql .= ' SET auto_validate = '.((int) $validate);
2149 $sql .= " WHERE rowid = ".((int) $this->id);
2150
2151 dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
2152 if ($this->db->query($sql)) {
2153 $this->auto_validate = $validate;
2154
2155 if (!$notrigger) {
2156 // Call trigger
2157 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2158 if ($result < 0) {
2159 return $result;
2160 }
2161 // End call triggers
2162 }
2163
2164 return 1;
2165 } else {
2166 dol_print_error($this->db);
2167 return -1;
2168 }
2169 }
2170
2178 public function setGeneratePdf($validate, $notrigger = 0)
2179 {
2180 global $user;
2181
2182 if (!$this->table_element) {
2183 dol_syslog(get_class($this)."::setGeneratePdf was called on object with property table_element not defined", LOG_ERR);
2184 return -1;
2185 }
2186
2187 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2188 $sql .= ' SET generate_pdf = '.((int) $validate);
2189 $sql .= " WHERE rowid = ".((int) $this->id);
2190
2191 dol_syslog(get_class($this)."::setGeneratePdf", LOG_DEBUG);
2192 if ($this->db->query($sql)) {
2193 $this->generate_pdf = $validate;
2194
2195 if (!$notrigger) {
2196 // Call trigger
2197 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2198 if ($result < 0) {
2199 return $result;
2200 }
2201 // End call triggers
2202 }
2203
2204 return 1;
2205 } else {
2206 dol_print_error($this->db);
2207 return -1;
2208 }
2209 }
2210
2218 public function setModelPdf($model, $notrigger = 0)
2219 {
2220 global $user;
2221 if (!$this->table_element) {
2222 dol_syslog(get_class($this)."::setModelPdf was called on object with property table_element not defined", LOG_ERR);
2223 return -1;
2224 }
2225
2226 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2227 $sql .= " SET modelpdf = '".$this->db->escape($model)."'";
2228 $sql .= " WHERE rowid = ".((int) $this->id);
2229
2230 dol_syslog(get_class($this)."::setModelPdf", LOG_DEBUG);
2231 if ($this->db->query($sql)) {
2232 $this->model_pdf = $model;
2233
2234 if (!$notrigger) {
2235 // Call trigger
2236 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2237 if ($result < 0) {
2238 return $result;
2239 }
2240 // End call triggers
2241 }
2242
2243 return 1;
2244 } else {
2245 dol_print_error($this->db);
2246 return -1;
2247 }
2248 }
2249}
2250
2251
2252
2258{
2262 public $element = 'facturedetrec';
2263
2267 public $table_element = 'facturedet_rec';
2268
2272 public $parent_element = 'facturerec';
2273
2277 public $fk_parent_attribute = 'fk_facture';
2278
2280
2283 public $fk_facture;
2287 public $fk_parent_line;
2288
2292 public $fk_product_fournisseur_price;
2297 public $fk_fournprice;
2298
2302 public $rang;
2303 //public $situation_percent; // Not supported on recurring invoice line
2304
2308 public $desc;
2312 public $description;
2313
2318 public $fk_product_type;
2319
2323 public $fk_contract_line;
2324
2325
2333 public function delete(User $user, $notrigger = 0)
2334 {
2335 $error = 0;
2336
2337 $this->db->begin();
2338
2339 if (!$error) {
2340 if (!$notrigger) {
2341 // Call triggers
2342 $result = $this->call_trigger('LINEBILLREC_DELETE', $user);
2343 if ($result < 0) {
2344 $error++;
2345 } // Do also here what you must do to rollback action if trigger fail
2346 // End call triggers
2347 }
2348 }
2349
2350 if (!$error) {
2351 $result = $this->deleteExtraFields();
2352 if ($result < 0) {
2353 $error++;
2354 }
2355 }
2356
2357 if (!$error) {
2358 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.((int) $this->id);
2359
2360 $res = $this->db->query($sql);
2361 if (!$res) {
2362 $error++;
2363 $this->errors[] = $this->db->lasterror();
2364 }
2365 }
2366
2367 // Commit or rollback
2368 if ($error) {
2369 $this->db->rollback();
2370 return -1;
2371 } else {
2372 $this->db->commit();
2373 return 1;
2374 }
2375 }
2376
2377
2384 public function fetch($rowid)
2385 {
2386 $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,';
2387 $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2388 $sql .= ' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2389 $sql .= ' l.rang, l.special_code,';
2390 $sql .= ' l.fk_unit, l.fk_contract_line,';
2391 $sql .= ' l.import_key, l.fk_multicurrency,';
2392 $sql .= ' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2393 $sql .= ' l.buy_price_ht, l.fk_product_fournisseur_price,';
2394 $sql .= ' l.fk_user_author, l.fk_user_modif, l.extraparams,';
2395 $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2396 $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
2397 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
2398 $sql .= ' WHERE l.rowid = '.((int) $rowid);
2399 $sql .= ' ORDER BY l.rang';
2400
2401 dol_syslog('FactureRec::fetch', LOG_DEBUG);
2402 $result = $this->db->query($sql);
2403 if ($result) {
2404 $objp = $this->db->fetch_object($result);
2405
2406 $this->id = $objp->rowid;
2407 $this->fk_facture = $objp->fk_facture;
2408 $this->fk_parent_line = $objp->fk_parent_line;
2409 $this->label = $objp->custom_label; // Label line
2410 $this->desc = $objp->description; // Description line
2411 $this->description = $objp->description; // Description line
2412 $this->product_type = $objp->product_type; // Type of line
2413 $this->ref = $objp->product_ref; // Ref product
2414 $this->product_ref = $objp->product_ref; // Ref product
2415 $this->libelle = $objp->product_label; // deprecated
2416 $this->product_label = $objp->product_label; // Label product
2417 $this->product_desc = $objp->product_desc; // Description product
2418 $this->fk_product_type = $objp->fk_product_type; // Type of product
2419 $this->qty = $objp->qty;
2420 $this->price = $objp->price;
2421 $this->subprice = $objp->subprice;
2422 $this->vat_src_code = $objp->vat_src_code;
2423 $this->tva_tx = $objp->tva_tx;
2424 $this->localtax1_tx = $objp->localtax1_tx;
2425 $this->localtax2_tx = $objp->localtax2_tx;
2426 $this->localtax1_type = $objp->localtax1_type;
2427 $this->localtax2_type = $objp->localtax2_type;
2428 $this->remise_percent = $objp->remise_percent;
2429 //$this->fk_remise_except = $objp->fk_remise_except;
2430 $this->fk_product = $objp->fk_product;
2431 $this->date_start_fill = $objp->date_start_fill;
2432 $this->date_end_fill = $objp->date_end_fill;
2433 $this->info_bits = $objp->info_bits;
2434 $this->total_ht = $objp->total_ht;
2435 $this->total_tva = $objp->total_tva;
2436 $this->total_ttc = $objp->total_ttc;
2437
2438 $this->rang = $objp->rang;
2439 $this->special_code = $objp->special_code;
2440 $this->fk_unit = $objp->fk_unit;
2441 $this->fk_contract_line = $objp->fk_contract_line;
2442 $this->import_key = $objp->import_key;
2443 $this->fk_multicurrency = $objp->fk_multicurrency;
2444 $this->multicurrency_code = $objp->multicurrency_code;
2445 $this->multicurrency_subprice = $objp->multicurrency_subprice;
2446 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2447 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2448 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2449
2450 $this->buy_price_ht = $objp->buy_price_ht;
2451
2452 $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2453 $this->fk_user_author = $objp->fk_user_author;
2454 $this->fk_user_modif = $objp->fk_user_modif;
2455
2456 $this->db->free($result);
2457 return 1;
2458 } else {
2459 $this->error = $this->db->lasterror();
2460 return -3;
2461 }
2462 }
2463
2464
2472 public function update(User $user, $notrigger = 0)
2473 {
2474 global $conf;
2475
2476 $error = 0;
2477
2478 // Clean parameters
2479 if (empty($this->fk_parent_line)) {
2480 $this->fk_parent_line = 0;
2481 }
2482
2483 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2484
2485 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET";
2486 $sql .= " fk_facture = ".((int) $this->fk_facture);
2487 $sql .= ", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
2488 $sql .= ", label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
2489 $sql .= ", description='".$this->db->escape($this->desc)."'";
2490 $sql .= ", price=".price2num($this->price);
2491 $sql .= ", qty=".price2num($this->qty);
2492 $sql .= ", tva_tx=".price2num($this->tva_tx);
2493 $sql .= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'";
2494 $sql .= ", localtax1_tx=".price2num($this->localtax1_tx);
2495 $sql .= ", localtax1_type='".$this->db->escape((string) $this->localtax1_type)."'";
2496 $sql .= ", localtax2_tx=".price2num($this->localtax2_tx);
2497 $sql .= ", localtax2_type='".$this->db->escape((string) $this->localtax2_type)."'";
2498 $sql .= ", fk_product=".($this->fk_product > 0 ? $this->fk_product : "null");
2499 $sql .= ", product_type=".((int) $this->product_type);
2500 $sql .= ", remise_percent=".price2num($this->remise_percent);
2501 $sql .= ", subprice=".price2num($this->subprice);
2502 $sql .= ", info_bits=".price2num($this->info_bits);
2503 $sql .= ", date_start_fill=".(int) $this->date_start_fill;
2504 $sql .= ", date_end_fill=".(int) $this->date_end_fill;
2505 if (empty($this->skip_update_total)) {
2506 $sql .= ", total_ht=".price2num($this->total_ht);
2507 $sql .= ", total_tva=".price2num($this->total_tva);
2508 $sql .= ", total_localtax1=".price2num($this->total_localtax1);
2509 $sql .= ", total_localtax2=".price2num($this->total_localtax2);
2510 $sql .= ", total_ttc=".price2num($this->total_ttc);
2511 }
2512 $sql .= ", rang=".((int) $this->rang);
2513 $sql .= ", special_code=".((int) $this->special_code);
2514 $sql .= ", fk_unit=".($this->fk_unit ? "'".$this->db->escape((string) $this->fk_unit)."'" : "null");
2515 $sql .= ", fk_contract_line=".($this->fk_contract_line ? $this->fk_contract_line : "null");
2516 $sql .= " WHERE rowid = ".((int) $this->id);
2517
2518 $this->db->begin();
2519
2520 dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
2521
2522 $resql = $this->db->query($sql);
2523 if ($resql) {
2524 if (!$error) {
2525 $result = $this->insertExtraFields();
2526 if ($result < 0) {
2527 $error++;
2528 }
2529 }
2530
2531 if (!$error && !$notrigger) {
2532 // Call trigger
2533 $result = $this->call_trigger('LINEBILLREC_MODIFY', $user);
2534 if ($result < 0) {
2535 $error++;
2536 }
2537 // End call triggers
2538 }
2539
2540 if ($error) {
2541 $this->db->rollback();
2542 return -2;
2543 } else {
2544 $this->db->commit();
2545 return 1;
2546 }
2547 } else {
2548 $this->error = $this->db->lasterror();
2549 $this->db->rollback();
2550 return -2;
2551 }
2552 }
2553}
$object ref
Definition info.php:90
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.
setErrorsFromObject($object)
setErrorsFromObject
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 generations 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=null, $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|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:158