dolibarr 24.0.3
fournisseur.facture.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
5 * Copyright (C) 2005 Marc Barilley <marc@ocebo.com>
6 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2010-2023 Juanjo Menent <jmenent@simnandez.es>
8 * Copyright (C) 2013-2019 Philippe Grand <philippe.grand@atoo-net.com>
9 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
10 * Copyright (C) 2014-2016 Marcos García <marcosgdf@gmail.com>
11 * Copyright (C) 2015 Bahfir Abbes <bafbes@gmail.com>
12 * Copyright (C) 2015-2022 Ferran Marcet <fmarcet@2byte.es>
13 * Copyright (C) 2016-2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
14 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
15 * Copyright (C) 2018-2026 Frédéric France <frederic.france@free.fr>
16 * Copyright (C) 2022 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
17 * Copyright (C) 2023 Nick Fragoulis
18 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
19 * Copyright (C) 2026 Vincent de Grandpré <vincent@de-grandpre.quebec>
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 3 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <https://www.gnu.org/licenses/>.
33 */
34
41require_once DOL_DOCUMENT_ROOT.'/core/class/commoninvoice.class.php';
42require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
43require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.ligne.class.php';
44require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
45require_once DOL_DOCUMENT_ROOT.'/subtotals/class/commonsubtotal.class.php';
46
47if (isModEnabled('accounting')) {
48 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
49 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
50}
51
56{
57 use CommonSubtotal;
58
62 public $element = 'invoice_supplier';
63
67 public $table_element = 'facture_fourn';
68
72 public $table_element_line = 'facture_fourn_det';
73
77 public $class_element_line = 'SupplierInvoiceLine';
81 public $fk_element = 'fk_facture_fourn';
82
86 public $picto = 'supplier_invoice';
87
92 public $restrictiononfksoc = 1;
93
97 protected $table_ref_field = 'ref';
98
102 public $rowid;
103
107 public $ref;
108
112 public $ref_supplier;
113
118 public $libelle;
122 public $label;
123
124 //Check constants for types
125 public $type = self::TYPE_STANDARD;
126
133 public $statut;
134
140 public $status;
141
148 public $fk_statut;
149
155 public $paye;
160 public $paid;
161
166 public $author;
167
172 public $fk_user_valid;
173
179 public $datec;
180
186 public $date_echeance;
187
191 public $date_pointoftax;
192
197 public $amount = 0;
202 public $remise = 0;
203
208 public $tva;
209
210 // Warning: Do not set default value into property definition. it must stay null.
211 // For example to avoid to have substitution done when object is generic and not yet defined.
213 public $localtax1;
215 public $localtax2;
217 public $total_ht;
219 public $total_tva;
221 public $total_localtax1;
223 public $total_localtax2;
225 public $total_ttc;
226
232 public $note;
234 public $note_private;
236 public $note_public;
238 public $propalid;
239
243 public $fk_account; // default bank account
244
248 public $transport_mode_id;
249
253 public $vat_reverse_charge;
254
258 public $extraparams = array();
259
263 public $lines = array();
264
269 public $fournisseur;
270
272
275 public $fk_facture_source;
276
278 public $fac_rec;
280 public $fk_fac_rec_source;
281
282 public $fields = array(
283 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
284 'ref' => array('type' => 'varchar(255)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'showoncombobox' => 1, 'position' => 15),
285 'ref_supplier' => array('type' => 'varchar(255)', 'label' => 'RefSupplier', 'enabled' => 1, 'visible' => -1, 'position' => 20),
286 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 25, 'index' => 1),
287 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 30),
288 'type' => array('type' => 'smallint(6)', 'label' => 'Type', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35),
289 'subtype' => array('type' => 'smallint(6)', 'label' => 'InvoiceSubtype', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 36),
290 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 40),
291 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 45),
292 'datef' => array('type' => 'date', 'label' => 'Date', 'enabled' => 1, 'visible' => -1, 'position' => 50),
293 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 55),
294 'libelle' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'position' => 60),
295 'paye' => array('type' => 'smallint(6)', 'label' => 'Paye', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 65),
296 'amount' => array('type' => 'double(24,8)', 'label' => 'Amount', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
297 'remise' => array('type' => 'double(24,8)', 'label' => 'Discount', 'enabled' => 1, 'visible' => -1, 'position' => 75),
298 'close_code' => array('type' => 'varchar(16)', 'label' => 'CloseCode', 'enabled' => 1, 'visible' => -1, 'position' => 80),
299 'close_note' => array('type' => 'varchar(128)', 'label' => 'CloseNote', 'enabled' => 1, 'visible' => -1, 'position' => 85),
300 'tva' => array('type' => 'double(24,8)', 'label' => 'Tva', 'enabled' => 1, 'visible' => -1, 'position' => 90),
301 'localtax1' => array('type' => 'double(24,8)', 'label' => 'Localtax1', 'enabled' => 1, 'visible' => -1, 'position' => 95),
302 'localtax2' => array('type' => 'double(24,8)', 'label' => 'Localtax2', 'enabled' => 1, 'visible' => -1, 'position' => 100),
303 'total_ht' => array('type' => 'double(24,8)', 'label' => 'TotalHT', 'enabled' => 1, 'visible' => -1, 'position' => 105),
304 'total_tva' => array('type' => 'double(24,8)', 'label' => 'TotalVAT', 'enabled' => 1, 'visible' => -1, 'position' => 110),
305 'total_ttc' => array('type' => 'double(24,8)', 'label' => 'TotalTTC', 'enabled' => 1, 'visible' => -1, 'position' => 115),
306 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'position' => 125),
307 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 130),
308 'fk_user_valid' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserValidation', 'enabled' => 1, 'visible' => -1, 'position' => 135),
309 'fk_facture_source' => array('type' => 'integer', 'label' => 'Fk facture source', 'enabled' => 1, 'visible' => -1, 'position' => 140),
310 'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label' => 'Project', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 145),
311 'fk_account' => array('type' => 'integer', 'label' => 'Account', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 150),
312 'fk_cond_reglement' => array('type' => 'integer', 'label' => 'PaymentTerm', 'enabled' => 1, 'visible' => -1, 'position' => 155),
313 'fk_mode_reglement' => array('type' => 'integer', 'label' => 'PaymentMode', 'enabled' => 1, 'visible' => -1, 'position' => 160),
314 'date_lim_reglement' => array('type' => 'date', 'label' => 'DateLimReglement', 'enabled' => 1, 'visible' => -1, 'position' => 165),
315 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 170),
316 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 175),
317 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'ModelPdf', 'enabled' => 1, 'visible' => 0, 'position' => 180),
318 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 190),
319 'fk_incoterms' => array('type' => 'integer', 'label' => 'IncotermCode', 'enabled' => 1, 'visible' => -1, 'position' => 195),
320 'location_incoterms' => array('type' => 'varchar(255)', 'label' => 'IncotermLocation', 'enabled' => 1, 'visible' => -1, 'position' => 200),
321 'fk_multicurrency' => array('type' => 'integer', 'label' => 'MulticurrencyId', 'enabled' => 1, 'visible' => -1, 'position' => 205),
322 'multicurrency_code' => array('type' => 'varchar(255)', 'label' => 'MulticurrencyCode', 'enabled' => 1, 'visible' => -1, 'position' => 210),
323 'multicurrency_tx' => array('type' => 'double(24,8)', 'label' => 'MulticurrencyRate', 'enabled' => 1, 'visible' => -1, 'position' => 215),
324 'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'MulticurrencyTotalHT', 'enabled' => 1, 'visible' => -1, 'position' => 220),
325 'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'MulticurrencyTotalVAT', 'enabled' => 1, 'visible' => -1, 'position' => 225),
326 'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'MulticurrencyTotalTTC', 'enabled' => 1, 'visible' => -1, 'position' => 230),
327 'date_pointoftax' => array('type' => 'date', 'label' => 'DatePointOfTax', 'enabled' => 'getDolGlobalString("INVOICE_POINTOFTAX_DATE")', 'visible' => -1, 'position' => 235),
328 'date_valid' => array('type' => 'date', 'label' => 'DateValidation', 'enabled' => 1, 'visible' => -1, 'position' => 240),
329 'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'Last main doc', 'enabled' => 1, 'visible' => -1, 'position' => 245),
330 'fk_statut' => array('type' => 'smallint(6)', 'label' => 'Status', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 500),
331 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 900),
332 );
333
334
338 const TYPE_STANDARD = 0;
339
344
349
353 const TYPE_DEPOSIT = 3;
354
358 const STATUS_DRAFT = 0;
359
364
372 const STATUS_CLOSED = 2;
373
382
383 const CLOSECODE_DISCOUNTVAT = 'discount_vat';
384 const CLOSECODE_BADCREDIT = 'badsupplier';
385 const CLOSECODE_ABANDONED = 'abandon';
386 const CLOSECODE_REPLACED = 'replaced';
387
388
394 public function __construct($db)
395 {
396 $this->db = $db;
397
398 $this->ismultientitymanaged = 1;
399 }
400
407 public function create($user)
408 {
409 global $langs, $conf, $hookmanager;
410
411 $error = 0;
412 $now = dol_now();
413
414 // Clean parameters
415 if (isset($this->ref_supplier)) {
416 $this->ref_supplier = trim($this->ref_supplier);
417 }
418 if (empty($this->type)) {
419 $this->type = self::TYPE_STANDARD;
420 }
421 if (empty($this->date)) {
422 $this->date = $now;
423 }
424
425 // Multicurrency (test on $this->multicurrency_tx because we should take the default rate only if not using origin rate)
426 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
427 list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $this->date);
428 } else {
429 $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
430 }
431 if (empty($this->fk_multicurrency)) {
432 $this->multicurrency_code = $conf->currency;
433 $this->fk_multicurrency = 0;
434 $this->multicurrency_tx = 1;
435 }
436 $this->entity = setEntity($this);
437
438 $this->db->begin();
439
440 // Defaults
441 $originaldatewhen = 0;
442 $nextdatewhen = 0;
443 $previousdaynextdatewhen = 0;
444 $_facrec = null;
445
446 // Create invoice from a template recurring invoice
447 if ($this->fac_rec > 0) {
448 $this->fk_fac_rec_source = $this->fac_rec;
449
450 require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture-rec.class.php';
451 $_facrec = new FactureFournisseurRec($this->db);
452 $result = $_facrec->fetch($this->fac_rec);
453 $result = $_facrec->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 0); // This load $_facrec->linkedObjectsIds
454
455 // Define some dates
456 if (!empty($_facrec->frequency)) {
457 $originaldatewhen = $_facrec->date_when;
458 // date_when is read from database in the timezone of the server (jdate), so delays must be added in this timezone
459 $nextdatewhen = dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency, 0, 'tzserver');
460 $previousdaynextdatewhen = dol_time_plus_duree($nextdatewhen, -1, 'd', 0, 'tzserver');
461 $this->socid = $_facrec->socid;
462 }
463
464 $this->entity = $_facrec->entity; // Invoice created in same entity than template
465
466 // Fields coming from GUI
467 // @TODO Value of template should be used as default value on the form on the GUI, and we should here always use the value from GUI
468 // set by posted page with $object->xxx = ... and this section should be removed.
469 $this->fk_project = GETPOSTINT('projectid') > 0 ? (GETPOSTINT('projectid')) : $_facrec->fk_project;
470 $this->note_public = GETPOST('note_public', 'restricthtml') ? GETPOST('note_public', 'restricthtml') : $_facrec->note_public;
471 $this->note_private = GETPOST('note_private', 'restricthtml') ? GETPOST('note_private', 'restricthtml') : $_facrec->note_private;
472 $this->model_pdf = GETPOST('model', 'alpha') ? GETPOST('model', 'alpha') : $_facrec->model_pdf;
473 $this->cond_reglement_id = GETPOSTINT('cond_reglement_id') > 0 ? (GETPOSTINT('cond_reglement_id')) : $_facrec->cond_reglement_id;
474 $this->mode_reglement_id = GETPOSTINT('mode_reglement_id') > 0 ? (GETPOSTINT('mode_reglement_id')) : $_facrec->mode_reglement_id;
475 $this->fk_account = GETPOST('fk_account') > 0 ? ((int) GETPOST('fk_account')) : $_facrec->fk_account;
476
477 // Set here to have this defined for substitution into notes, should be recalculated after adding lines to get same result
478 $this->total_ht = $_facrec->total_ht;
479 $this->total_ttc = $_facrec->total_ttc;
480
481 // Fields always coming from template
482 $this->fk_incoterms = $_facrec->fk_incoterms;
483 $this->location_incoterms = $_facrec->location_incoterms;
484
485 // Clean parameters
486 if (! $this->type) {
487 $this->type = self::TYPE_STANDARD;
488 }
489 $this->note_public = trim((string) $this->note_public);
490 $this->note_private = trim((string) $this->note_private);
491 $this->note_private = dol_concatdesc($this->note_private, $langs->trans("GeneratedFromRecurringInvoice", $_facrec->title));
492
493 $this->array_options = $_facrec->array_options;
494
495 if (! $this->mode_reglement_id) {
496 $this->mode_reglement_id = 0;
497 }
498 $this->status = self::STATUS_DRAFT;
499 $this->statut = self::STATUS_DRAFT; // deprecated
500
501 $this->linked_objects = $_facrec->linkedObjectsIds;
502 // We do not add link to template invoice or next invoice will be linked to all generated invoices
503 //$this->linked_objects['facturerec'][0] = $this->fac_rec;
504
505 $forceduedate = $this->calculate_date_lim_reglement();
506
507 // For recurring invoices, update date and number of last generation of recurring template invoice, before inserting new invoice
508 if ($_facrec->frequency > 0) {
509 $this->ref_supplier = trim($this->ref_supplier . '_' . ($_facrec->nb_gen_done + 1));
510 dol_syslog("This is a recurring invoice so we set date_last_gen and next date_when");
511 if (empty($_facrec->date_when)) {
512 $_facrec->date_when = $now;
513 }
514 $next_date = $_facrec->getNextDate(); // Calculate next date
515 $result = $_facrec->setValueFrom('date_last_gen', $now, '', 0, 'date', '', $user, '');
516 //$_facrec->setValueFrom('nb_gen_done', $_facrec->nb_gen_done + 1); // Not required, +1 already included into setNextDate when second param is 1.
517 $result = $_facrec->setNextDate($next_date, 1);
518 }
519
520 // Define lang of customer
521 $outputlangs = $langs;
522 $newlang = '';
523
524 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && isset($this->thirdparty->default_lang)) {
525 $newlang = $this->thirdparty->default_lang; // for proposal, order, invoice, ...
526 }
527 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && property_exists($this, 'default_lang') && isset($this->default_lang)) { // @phan-suppress-current-line PhanUndeclaredProperty
528 $newlang = $this->default_lang; // for thirdparty @phan-suppress-current-line PhanUndeclaredProperty
529 }
530 if (!empty($newlang)) {
531 $outputlangs = new Translate("", $conf);
532 $outputlangs->setDefaultLang($newlang);
533 } // Array of possible substitutions (See also file mailing-send.php that should manage same substitutions)
534 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $this);
535 $substitutionarray['__INVOICE_PREVIOUS_MONTH__'] = dol_print_date(dol_time_plus_duree($this->date, -1, 'm', 0, 'tzserver'), '%m');
536 $substitutionarray['__INVOICE_MONTH__'] = dol_print_date($this->date, '%m');
537 $substitutionarray['__INVOICE_NEXT_MONTH__'] = dol_print_date(dol_time_plus_duree($this->date, 1, 'm', 0, 'tzserver'), '%m');
538 $substitutionarray['__INVOICE_PREVIOUS_MONTH_TEXT__'] = dol_print_date(dol_time_plus_duree($this->date, -1, 'm', 0, 'tzserver'), '%B');
539 $substitutionarray['__INVOICE_MONTH_TEXT__'] = dol_print_date($this->date, '%B');
540 $substitutionarray['__INVOICE_NEXT_MONTH_TEXT__'] = dol_print_date(dol_time_plus_duree($this->date, 1, 'm', 0, 'tzserver'), '%B');
541 $substitutionarray['__INVOICE_PREVIOUS_YEAR__'] = dol_print_date(dol_time_plus_duree($this->date, -1, 'y', 0, 'tzserver'), '%Y');
542 $substitutionarray['__INVOICE_YEAR__'] = dol_print_date($this->date, '%Y');
543 $substitutionarray['__INVOICE_NEXT_YEAR__'] = dol_print_date(dol_time_plus_duree($this->date, 1, 'y', 0, 'tzserver'), '%Y'); // Only for template invoice
544 $substitutionarray['__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] = $originaldatewhen ? dol_print_date($originaldatewhen, 'dayhour') : '';
545 $substitutionarray['__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] = $nextdatewhen ? dol_print_date($nextdatewhen, 'dayhour') : '';
546 $substitutionarray['__INVOICE_PREVIOUS_DATE_NEXT_INVOICE_AFTER_GEN__'] = $previousdaynextdatewhen ? dol_print_date($previousdaynextdatewhen, 'dayhour') : '';
547 $substitutionarray['__INVOICE_COUNTER_CURRENT__'] = $_facrec->nb_gen_done;
548 $substitutionarray['__INVOICE_COUNTER_MAX__'] = $_facrec->nb_gen_max;
549
550 complete_substitutions_array($substitutionarray, $outputlangs);
551
552 $this->note_public = make_substitutions($this->note_public, $substitutionarray);
553 $this->note_private = make_substitutions($this->note_private, $substitutionarray);
554 }
555
556 // Define due date if not already defined
557 if (!empty($forceduedate)) {
558 $this->date_echeance = $forceduedate;
559 }
560
561 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn (";
562 $sql .= "ref";
563 $sql .= ", ref_supplier";
564 $sql .= ", ref_ext";
565 $sql .= ", entity";
566 $sql .= ", type";
567 $sql .= ", subtype";
568 $sql .= ", libelle";
569 $sql .= ", fk_soc";
570 $sql .= ", datec";
571 $sql .= ", datef";
572 $sql .= ", date_pointoftax";
573 $sql .= ", vat_reverse_charge";
574 $sql .= ", fk_projet";
575 $sql .= ", fk_cond_reglement";
576 $sql .= ", fk_mode_reglement";
577 $sql .= ", fk_account";
578 $sql .= ", note_private";
579 $sql .= ", note_public";
580 $sql .= ", fk_user_author";
581 $sql .= ", date_lim_reglement";
582 $sql .= ", fk_incoterms, location_incoterms";
583 $sql .= ", fk_multicurrency";
584 $sql .= ", multicurrency_code";
585 $sql .= ", multicurrency_tx";
586 $sql .= ", fk_facture_source";
587 $sql .= ", fk_fac_rec_source";
588 $sql .= ")";
589 $sql .= " VALUES (";
590 $sql .= "'(PROV)'";
591 $sql .= ", '".$this->db->escape($this->ref_supplier)."'";
592 $sql .= ", '".$this->db->escape($this->ref_ext)."'";
593 $sql .= ", ".((int) $this->entity);
594 $sql .= ", '".$this->db->escape((string) $this->type)."'";
595 $sql .= ", ".(isset($this->subtype) ? (int) $this->subtype : "null");
596 $sql .= ", '".$this->db->escape(isset($this->label) ? $this->label : (isset($this->libelle) ? $this->libelle : ''))."'";
597 $sql .= ", ".((int) $this->socid);
598 $sql .= ", '".$this->db->idate($now)."'";
599 $sql .= ", '".$this->db->idate($this->date)."'";
600 $sql .= ", ".(empty($this->date_pointoftax) ? "null" : "'".$this->db->idate($this->date_pointoftax)."'");
601 $sql .= ", ".($this->vat_reverse_charge != '' ? ((int) $this->vat_reverse_charge) : 0);
602 $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : "null");
603 $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : "null");
604 $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "null");
605 $sql .= ", ".($this->fk_account > 0 ? ((int) $this->fk_account) : 'NULL');
606 $sql .= ", '".$this->db->escape($this->note_private)."'";
607 $sql .= ", '".$this->db->escape($this->note_public)."'";
608 $sql .= ", ".((int) $user->id).",";
609 $sql .= $this->date_echeance != '' ? "'".$this->db->idate($this->date_echeance)."'" : "null";
610 $sql .= ", ".(int) $this->fk_incoterms;
611 $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
612 $sql .= ", ".(int) $this->fk_multicurrency;
613 $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
614 $sql .= ", ".(float) $this->multicurrency_tx;
615 $sql .= ", ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null");
616 $sql .= ", ".(isset($this->fk_fac_rec_source) ? ((int) $this->fk_fac_rec_source) : "NULL");
617 $sql .= ")";
618
619 dol_syslog(get_class($this)."::create", LOG_DEBUG);
620 $resql = $this->db->query($sql);
621 if ($resql) {
622 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn');
623
624 // Update ref with new one
625 $this->ref = '(PROV'.$this->id.')';
626 $sql = 'UPDATE '.MAIN_DB_PREFIX."facture_fourn SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
627
628 dol_syslog(get_class($this)."::create", LOG_DEBUG);
629 $resql = $this->db->query($sql);
630 if (!$resql) {
631 $error++;
632 }
633
634 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
635 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
636 }
637
638 // Add object linked
639 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
640 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
641 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, ...))
642 foreach ($tmp_origin_id as $origin_id) {
643 $ret = $this->add_object_linked($origin, $origin_id);
644 if (!$ret) {
645 dol_print_error($this->db);
646 $error++;
647 }
648 }
649 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
650 $origin_id = $tmp_origin_id;
651 $ret = $this->add_object_linked($origin, $origin_id);
652 if (!$ret) {
653 dol_print_error($this->db);
654 $error++;
655 }
656 }
657 }
658 }
659
660 if (!$error && empty($this->fac_rec) && count($this->lines) && is_object($this->lines[0])) { // If this->lines is array of InvoiceLines (preferred mode)
661 dol_syslog("There is ".count($this->lines)." lines that are invoice lines objects");
662 foreach ($this->lines as $i => $val) {
663 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn, special_code, fk_remise_except)';
664 $sql .= " VALUES (".((int) $this->id).", ".((int) $this->lines[$i]->special_code).", ".($this->lines[$i]->fk_remise_except > 0 ? ((int) $this->lines[$i]->fk_remise_except) : 'NULL').')';
665
666 $resql_insert = $this->db->query($sql);
667 if ($resql_insert) {
668 $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det');
669
670 $res = $this->updateline(
671 $idligne,
672 $this->lines[$i]->desc ? $this->lines[$i]->desc : $this->lines[$i]->description,
673 $this->lines[$i]->subprice,
674 $this->lines[$i]->tva_tx.($this->lines[$i]->vat_src_code ? ' ('.$this->lines[$i]->vat_src_code.')' : ''),
675 $this->lines[$i]->localtax1_tx,
676 $this->lines[$i]->localtax2_tx,
677 $this->lines[$i]->qty,
678 $this->lines[$i]->fk_product,
679 'HT',
680 (!empty($this->lines[$i]->info_bits) ? $this->lines[$i]->info_bits : ''),
681 $this->lines[$i]->product_type,
682 $this->lines[$i]->remise_percent,
683 0,
684 $this->lines[$i]->date_start,
685 $this->lines[$i]->date_end,
686 $this->lines[$i]->array_options,
687 $this->lines[$i]->fk_unit,
688 $this->lines[$i]->multicurrency_subprice,
689 $this->lines[$i]->ref_supplier
690 );
691 } else {
692 $this->error = $this->db->lasterror();
693 $this->db->rollback();
694 return -5;
695 }
696 }
697 } elseif (!$error && empty($this->fac_rec)) { // If this->lines is an array of invoice line arrays
698 dol_syslog("There is ".count($this->lines)." lines that are array lines");
699 foreach ($this->lines as $i => $val) {
700 $line = $this->lines[$i];
701
702 // Test and convert into object this->lines[$i]. When coming from REST API, we may still have an array
703 //if (! is_object($line)) $line=json_decode(json_encode($line), false); // convert recursively array into object.
704 if (!is_object($line)) {
705 $line = (object) $line;
706 }
707
708 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn, special_code, fk_remise_except)';
709 $sql .= " VALUES (".((int) $this->id).", ".((int) $this->lines[$i]->special_code).", ".($this->lines[$i]->fk_remise_except > 0 ? ((int) $this->lines[$i]->fk_remise_except) : 'NULL').')';
710
711 $resql_insert = $this->db->query($sql);
712 if ($resql_insert) {
713 $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det');
714
715 $this->updateline(
716 $idligne,
717 $line->desc ? $line->desc : $line->description,
718 $line->subprice,
719 $line->tva_tx,
720 $line->localtax1_tx,
721 $line->localtax2_tx,
722 $line->qty,
723 $line->fk_product,
724 'HT',
725 (!empty($line->info_bits) ? $line->info_bits : ''),
726 $line->product_type,
727 $line->remise_percent,
728 0,
729 $line->date_start,
730 $line->date_end,
731 $line->array_options,
732 $line->fk_unit,
733 $line->multicurrency_subprice,
734 $line->ref_supplier
735 );
736 } else {
737 $this->error = $this->db->lasterror();
738 $this->db->rollback();
739 return -5;
740 }
741 }
742 }
743
744 /*
745 * Insert lines of template invoices
746 */
747 if (! $error && $this->fac_rec > 0 && $_facrec instanceof FactureFournisseurRec) {
748 foreach ($_facrec->lines as $i => $val) {
749 $product_type = $_facrec->lines[$i]->product_type;
750 if ($_facrec->lines[$i]->fk_product) {
751 $prod = new Product($this->db);
752 $res = $prod->fetch($_facrec->lines[$i]->fk_product);
753 }
754
755 // For line from template invoice, we use data from template invoice
756 /*
757 $tva_tx = get_default_tva($mysoc,$soc,$prod->id);
758 $tva_npr = get_default_npr($mysoc,$soc,$prod->id);
759 if (empty($tva_tx)) $tva_npr=0;
760 $localtax1_tx=get_localtax($tva_tx,1,$soc,$mysoc,$tva_npr);
761 $localtax2_tx=get_localtax($tva_tx,2,$soc,$mysoc,$tva_npr);
762 */
763 $tva_tx = $_facrec->lines[$i]->tva_tx . ($_facrec->lines[$i]->vat_src_code ? '(' . $_facrec->lines[$i]->vat_src_code . ')' : '');
764 $tva_npr = $_facrec->lines[$i]->info_bits;
765 if (empty($tva_tx)) {
766 $tva_npr = 0;
767 }
768 $localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
769 $localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
770
771 // $fk_product_fournisseur_price not used and does not exist on line
772 // $fk_product_fournisseur_price = empty($_facrec->lines[$i]->fk_product_fournisseur_price) ? null : $_facrec->lines[$i]->fk_product_fournisseur_price;
773 $buyprice = empty($_facrec->lines[$i]->buyprice) ? 0 : $_facrec->lines[$i]->buyprice;
774
775 // If buyprice not defined from template invoice, we try to guess the best value
776 if (! $buyprice && $_facrec->lines[$i]->fk_product > 0) {
777 require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php';
778 $producttmp = new ProductFournisseur($this->db);
779 $producttmp->fetch($_facrec->lines[$i]->fk_product);
780
781 // If margin module defined on costprice, we try the costprice
782 // If not defined or if module margin defined and pmp and stock module enabled, we try pmp price
783 // else we get the best supplier price
784 if (getDolGlobalString('MARGIN_TYPE') == 'costprice' && !empty($producttmp->cost_price)) {
785 $buyprice = $producttmp->cost_price;
786 } elseif (isModEnabled('stock') && (getDolGlobalString('MARGIN_TYPE') == 'costprice' || getDolGlobalString('MARGIN_TYPE') == 'pmp') && !empty($producttmp->pmp)) {
787 $buyprice = $producttmp->pmp;
788 } else {
789 if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0) {
790 if ($producttmp->product_fourn_price_id > 0) {
791 $buyprice = price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent / 100) + $producttmp->fourn_remise, 'MU');
792 }
793 }
794 }
795 }
796
797 $result_insert = $this->addline(
798 $_facrec->lines[$i]->desc ? $_facrec->lines[$i]->desc : $_facrec->lines[$i]->description,
799 $_facrec->lines[$i]->pu_ht,
800 $tva_tx,
801 $localtax1_tx,
802 $localtax2_tx,
803 $_facrec->lines[$i]->qty,
804 $_facrec->lines[$i]->fk_product,
805 $_facrec->lines[$i]->remise_percent,
806 ($_facrec->lines[$i]->date_start == 1 && $this->date) ? $this->date : '',
807 ($_facrec->lines[$i]->date_end == 1 && $previousdaynextdatewhen) ? $previousdaynextdatewhen : '',
808 0,
809 $_facrec->lines[$i]->info_bits,
810 'HT',
811 $product_type,
812 $_facrec->lines[$i]->rang,
813 0,
814 $_facrec->lines[$i]->array_options,
815 $_facrec->lines[$i]->fk_unit,
816 0,
817 0,
818 $_facrec->lines[$i]->ref_supplier,
819 $_facrec->lines[$i]->special_code,
820 0,
821 0
822 );
823 if ($result_insert < 0) {
824 $error++;
825 $this->error = $this->db->error();
826 break;
827 }
828 }
829 }
830
831
832 // Update total price
833 $result = $this->update_price(1);
834 if ($result > 0) {
835 // Actions on extra fields
836 if (!$error) {
837 $result = $this->insertExtraFields(); // This also set $this->error or $this->errors if errors are found
838 if ($result < 0) {
839 $error++;
840 }
841 }
842
843 if (!$error) {
844 // Call trigger
845 $result = $this->call_trigger('BILL_SUPPLIER_CREATE', $user);
846 if ($result < 0) {
847 $error++;
848 }
849 // End call triggers
850 }
851
852 if (!$error) {
853 $this->db->commit();
854 return $this->id;
855 } else {
856 $this->db->rollback();
857 return -4;
858 }
859 } else {
860 $this->error = $langs->trans('FailedToUpdatePrice');
861 $this->db->rollback();
862 return -3;
863 }
864 } else {
865 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
866 $this->error = $langs->trans('ErrorRefAlreadyExists');
867 $this->db->rollback();
868 return -1;
869 } else {
870 $this->error = $this->db->lasterror();
871 $this->db->rollback();
872 return -2;
873 }
874 }
875 }
876
885 public function fetch($id = 0, $ref = '', $ref_ext = '')
886 {
887 if (empty($id) && empty($ref) && empty($ref_ext)) {
888 return -1;
889 }
890
891 $sql = "SELECT";
892 $sql .= " t.rowid,";
893 $sql .= " t.ref,";
894 $sql .= " t.ref_supplier,";
895 $sql .= " t.ref_ext,";
896 $sql .= " t.entity,";
897 $sql .= " t.type,";
898 $sql .= " t.subtype,";
899 $sql .= " t.fk_soc,";
900 $sql .= " t.datec,";
901 $sql .= " t.datef,";
902 $sql .= " t.date_pointoftax,";
903 $sql .= " t.tms as datem,";
904 $sql .= " t.libelle as label,";
905 $sql .= " t.paye as paid,";
906 $sql .= " t.close_code,";
907 $sql .= " t.close_note,";
908 $sql .= " t.tva,";
909 $sql .= " t.localtax1,";
910 $sql .= " t.localtax2,";
911 $sql .= " t.total_ht,";
912 $sql .= " t.total_tva,";
913 $sql .= " t.total_ttc,";
914 $sql .= " t.fk_statut as status,";
915 $sql .= " t.fk_user_author,";
916 $sql .= " t.fk_user_valid,";
917 $sql .= " t.fk_facture_source,";
918 $sql .= " t.vat_reverse_charge,";
919 $sql .= " t.fk_fac_rec_source,";
920 $sql .= " t.fk_projet as fk_project,";
921 $sql .= " t.fk_cond_reglement,";
922 $sql .= " t.fk_account,";
923 $sql .= " t.fk_mode_reglement,";
924 $sql .= " t.date_lim_reglement,";
925 $sql .= " t.note_private,";
926 $sql .= " t.note_public,";
927 $sql .= " t.model_pdf,";
928 $sql .= " t.last_main_doc,";
929 $sql .= " t.import_key,";
930 $sql .= " t.extraparams,";
931 $sql .= " cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc,";
932 $sql .= " p.code as mode_reglement_code, p.libelle as mode_reglement_label,";
933 $sql .= ' s.nom as socnom, s.rowid as socid,';
934 $sql .= ' t.fk_incoterms, t.location_incoterms,';
935 $sql .= " i.libelle as label_incoterms,";
936 $sql .= ' t.fk_transport_mode,';
937 $sql .= ' t.fk_multicurrency, t.multicurrency_code, t.multicurrency_tx, t.multicurrency_total_ht, t.multicurrency_total_tva, t.multicurrency_total_ttc';
938 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as t';
939 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON (t.fk_soc = s.rowid)";
940 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_payment_term as cr ON t.fk_cond_reglement = cr.rowid";
941 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as p ON t.fk_mode_reglement = p.id";
942 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON t.fk_incoterms = i.rowid';
943 if ($id) {
944 $sql .= " WHERE t.rowid = ".((int) $id);
945 } else {
946 $sql .= ' WHERE t.entity IN ('.getEntity('supplier_invoice').')'; // Don't use entity if you use rowid
947 if ($ref) {
948 $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
949 }
950 if ($ref_ext) {
951 $sql .= " AND t.ref_ext = '".$this->db->escape($ref_ext)."'";
952 }
953 }
954
955 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
956 $resql = $this->db->query($sql);
957 if ($resql) {
958 if ($this->db->num_rows($resql)) {
959 $obj = $this->db->fetch_object($resql);
960
961 $this->id = $obj->rowid;
962 $this->ref = $obj->ref ? $obj->ref : $obj->rowid; // We take rowid if ref is empty for backward compatibility
963
964 $this->ref_supplier = $obj->ref_supplier;
965 $this->ref_ext = $obj->ref_ext;
966 $this->entity = $obj->entity;
967 $this->type = empty($obj->type) ? self::TYPE_STANDARD : $obj->type;
968 $this->subtype = $obj->subtype;
969 $this->socid = $obj->fk_soc;
970 $this->date = $this->db->jdate($obj->datef);
971 $this->date_pointoftax = $this->db->jdate($obj->date_pointoftax);
972 $this->date_creation = $this->db->jdate($obj->datec);
973 $this->datec = $this->db->jdate($obj->datec);
974 $this->date_modification = $this->db->jdate($obj->datem);
975 $this->tms = $this->db->jdate($obj->datem);
976 $this->libelle = $obj->label; // deprecated
977 $this->label = $obj->label;
978 $this->paye = $obj->paid;
979 $this->paid = $obj->paid;
980 $this->close_code = $obj->close_code;
981 $this->close_note = $obj->close_note;
982 $this->total_localtax1 = $obj->localtax1;
983 $this->total_localtax2 = $obj->localtax2;
984 $this->total_ht = $obj->total_ht;
985 $this->total_tva = $obj->total_tva;
986 $this->total_ttc = $obj->total_ttc;
987 $this->status = $obj->status;
988 $this->statut = $obj->status; // For backward compatibility
989 $this->fk_statut = $obj->status; // For backward compatibility
990 $this->user_creation_id = $obj->fk_user_author;
991 $this->author = $obj->fk_user_author; // deprecated
992 $this->user_validation_id = $obj->fk_user_valid;
993 $this->fk_user_valid = $obj->fk_user_valid;
994 $this->fk_facture_source = $obj->fk_facture_source;
995 $this->vat_reverse_charge = empty($obj->vat_reverse_charge) ? 0 : 1;
996 $this->fk_fac_rec_source = $obj->fk_fac_rec_source;
997 $this->fk_project = $obj->fk_project;
998 $this->cond_reglement_id = $obj->fk_cond_reglement;
999 $this->cond_reglement_code = $obj->cond_reglement_code;
1000 $this->cond_reglement = $obj->cond_reglement_label; // deprecated
1001 $this->cond_reglement_label = $obj->cond_reglement_label;
1002 $this->cond_reglement_doc = $obj->cond_reglement_doc;
1003 $this->fk_account = $obj->fk_account;
1004 $this->mode_reglement_id = $obj->fk_mode_reglement;
1005 $this->mode_reglement_code = $obj->mode_reglement_code;
1006 $this->mode_reglement = $obj->mode_reglement_label;
1007 $this->date_echeance = $this->db->jdate($obj->date_lim_reglement);
1008 $this->note = $obj->note_private; // deprecated
1009 $this->note_private = $obj->note_private;
1010 $this->note_public = $obj->note_public;
1011 $this->model_pdf = $obj->model_pdf;
1012 $this->last_main_doc = $obj->last_main_doc;
1013 $this->import_key = $obj->import_key;
1014
1015 //Incoterms
1016 $this->fk_incoterms = $obj->fk_incoterms;
1017 $this->location_incoterms = $obj->location_incoterms;
1018 $this->label_incoterms = $obj->label_incoterms;
1019 $this->transport_mode_id = $obj->fk_transport_mode;
1020
1021 // Multicurrency
1022 $this->fk_multicurrency = $obj->fk_multicurrency;
1023 $this->multicurrency_code = $obj->multicurrency_code;
1024 $this->multicurrency_tx = $obj->multicurrency_tx;
1025 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1026 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1027 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1028
1029 $this->extraparams = isset($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array();
1030
1031 $this->socid = $obj->socid;
1032
1033 $this->thirdparty = null; // Clear if another value was already set by fetch_thirdparty
1034
1035 // Retrieve all extrafield
1036 // fetch optionals attributes and labels
1037 $this->fetch_optionals();
1038
1039 $result = $this->fetch_lines();
1040 if ($result < 0) {
1041 $this->error = $this->db->lasterror();
1042 return -3;
1043 }
1044 } else {
1045 $this->error = 'Bill with id '.$id.' not found';
1046 dol_syslog(get_class($this).'::fetch '.$this->error);
1047 return 0;
1048 }
1049
1050 $this->db->free($resql);
1051 return 1;
1052 } else {
1053 $this->error = "Error ".$this->db->lasterror();
1054 return -1;
1055 }
1056 }
1057
1058
1059 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1065 public function fetch_lines()
1066 {
1067 // phpcs:enable
1068 $this->lines = array();
1069
1070 $sql = 'SELECT f.rowid, f.ref as ref_supplier, f.description as line_desc, f.date_start, f.date_end, f.pu_ht, f.pu_ttc, f.qty, f.remise_percent, f.vat_src_code, f.tva_tx';
1071 $sql .= ', f.localtax1_tx, f.localtax2_tx, f.localtax1_type, f.localtax2_type, f.total_localtax1, f.total_localtax2, f.fk_facture_fourn, f.fk_remise_except';
1072 $sql .= ', f.total_ht, f.tva as total_tva, f.total_ttc, f.fk_product, f.product_type, f.info_bits, f.rang, f.special_code, f.fk_parent_line, f.fk_unit, f.extraparams';
1073 $sql .= ', p.rowid as product_id, p.ref as product_ref, p.label as label, p.barcode as product_barcode, p.description as product_desc';
1074 $sql .= ', f.fk_code_ventilation, f.fk_multicurrency, f.multicurrency_code, f.multicurrency_subprice, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc';
1075 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_det as f';
1076 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON f.fk_product = p.rowid';
1077 $sql .= ' WHERE fk_facture_fourn='.((int) $this->id);
1078 $sql .= ' ORDER BY f.rang, f.rowid';
1079
1080 dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
1081
1082 $resql_rows = $this->db->query($sql);
1083 if ($resql_rows) {
1084 $num_rows = $this->db->num_rows($resql_rows);
1085 if ($num_rows) {
1086 $i = 0;
1087 while ($i < $num_rows) {
1088 $obj = $this->db->fetch_object($resql_rows);
1089
1090 $line = new SupplierInvoiceLine($this->db);
1091
1092 $line->id = $obj->rowid;
1093 $line->rowid = $obj->rowid;
1094
1095 $line->description = $obj->line_desc;
1096 $line->desc = $obj->line_desc;
1097 $line->date_start = $this->db->jdate($obj->date_start);
1098 $line->date_end = $this->db->jdate($obj->date_end);
1099
1100 $line->product_ref = $obj->product_ref;
1101 $line->ref = $obj->product_ref;
1102 $line->ref_supplier = $obj->ref_supplier;
1103 $line->libelle = $obj->label;
1104 $line->label = $obj->label;
1105 $line->product_barcode = $obj->product_barcode;
1106 $line->product_desc = $obj->product_desc;
1107 $line->subprice = $obj->pu_ht;
1108 $line->pu_ht = $obj->pu_ht; // deprecated
1109 $line->subprice_ttc = $obj->pu_ttc;
1110 $line->pu_ttc = $obj->pu_ttc; // deprcated
1111 $line->vat_src_code = $obj->vat_src_code;
1112 $line->tva_tx = $obj->tva_tx;
1113 $line->localtax1_tx = $obj->localtax1_tx;
1114 $line->localtax2_tx = $obj->localtax2_tx;
1115 $line->localtax1_type = $obj->localtax1_type;
1116 $line->localtax2_type = $obj->localtax2_type;
1117 $line->qty = $obj->qty;
1118 $line->remise_percent = $obj->remise_percent;
1119 $line->fk_remise_except = $obj->fk_remise_except;
1120 //$line->tva = $obj->total_tva; // deprecated
1121 $line->total_ht = $obj->total_ht;
1122 $line->total_ttc = $obj->total_ttc;
1123 $line->total_tva = $obj->total_tva;
1124 $line->total_localtax1 = $obj->total_localtax1;
1125 $line->total_localtax2 = $obj->total_localtax2;
1126 $line->fk_facture_fourn = $obj->fk_facture_fourn;
1127 $line->fk_product = $obj->fk_product;
1128 $line->product_type = $obj->product_type;
1129 $line->product_label = $obj->label;
1130 $line->info_bits = $obj->info_bits;
1131 $line->fk_parent_line = $obj->fk_parent_line;
1132 $line->special_code = $obj->special_code;
1133 $line->rang = $obj->rang;
1134 $line->fk_unit = $obj->fk_unit;
1135
1136 $line->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array();
1137
1138 // Accountancy
1139 $line->fk_code_ventilation = $obj->fk_code_ventilation;
1140
1141 // Multicurrency
1142 $line->fk_multicurrency = $obj->fk_multicurrency;
1143 $line->multicurrency_code = $obj->multicurrency_code;
1144 $line->multicurrency_subprice = $obj->multicurrency_subprice;
1145 $line->multicurrency_total_ht = $obj->multicurrency_total_ht;
1146 $line->multicurrency_total_tva = $obj->multicurrency_total_tva;
1147 $line->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1148
1149 // Extra fields
1150 $line->fetch_optionals();
1151
1152 $this->lines[$i] = $line;
1153
1154 $i++;
1155 }
1156 }
1157 $this->db->free($resql_rows);
1158 return 1;
1159 } else {
1160 $this->error = $this->db->error();
1161 dol_syslog(get_class($this)."::fetch_lines - No lines:{$this->error} Error:{$this->error}", LOG_DEBUG);
1162 return -3;
1163 }
1164 }
1165
1166
1174 public function update($user = null, $notrigger = 0)
1175 {
1176 global $langs;
1177 $error = 0;
1178
1179 // Clean parameters
1180 if (empty($this->type)) {
1181 $this->type = self::TYPE_STANDARD;
1182 }
1183 if (isset($this->ref)) {
1184 $this->ref = trim($this->ref);
1185 }
1186 if (isset($this->ref_supplier)) {
1187 $this->ref_supplier = trim($this->ref_supplier);
1188 }
1189 if (isset($this->ref_ext)) {
1190 $this->ref_ext = trim($this->ref_ext);
1191 }
1192 if (isset($this->entity)) {
1193 $this->entity = (int) $this->entity;
1194 }
1195 if (isset($this->type)) {
1196 $this->type = (int) $this->type;
1197 }
1198 if (isset($this->subtype)) {
1199 $this->subtype = (int) $this->subtype;
1200 }
1201 if (isset($this->socid)) {
1202 $this->socid = (int) $this->socid;
1203 }
1204 if (isset($this->label)) {
1205 $this->label = trim($this->label);
1206 }
1207 if (isset($this->paid)) {
1208 $this->paid = (int) (bool) $this->paid;
1209 $this->paye = $this->paid; // deprecated
1210 } elseif (isset($this->paye)) {
1211 $this->paid = (int) (bool) $this->paye;
1212 $this->paye = $this->paid; // deprecared
1213 }
1214 if (isset($this->close_code)) {
1215 $this->close_code = trim($this->close_code);
1216 }
1217 if (isset($this->close_note)) {
1218 $this->close_note = trim($this->close_note);
1219 }
1220 if (empty($this->total_ht)) {
1221 $this->total_ht = 0;
1222 }
1223 if (empty($this->total_tva)) {
1224 $this->total_tva = 0;
1225 }
1226 if (empty($this->total_localtax1)) {
1227 $this->total_localtax1 = 0;
1228 }
1229 if (empty($this->total_localtax2)) {
1230 $this->total_localtax2 = 0;
1231 }
1232 if (isset($this->total_ttc)) {
1233 $this->total_ttc = (float) $this->total_ttc;
1234 }
1235 if (isset($this->status)) {
1236 $this->status = (int) $this->status;
1237 $this->statut = $this->status;
1238 } elseif (isset($this->statut)) {
1239 $this->status = (int) $this->statut;
1240 $this->statut = $this->status;
1241 }
1242 if (isset($this->author)) { // TODO: user_creation_id?
1243 $this->author = (int) $this->author;
1244 }
1245 if (isset($this->fk_user_valid)) {
1246 $this->fk_user_valid = (int) $this->fk_user_valid;
1247 }
1248 if (isset($this->fk_facture_source)) {
1249 $this->fk_facture_source = (int) $this->fk_facture_source;
1250 }
1251 if (isset($this->fk_project)) {
1252 if (empty($this->fk_project)) {
1253 $this->fk_project = 0;
1254 } else {
1255 $this->fk_project = (int) $this->fk_project;
1256 }
1257 }
1258 if (isset($this->mode_reglement_id)) {
1259 $this->mode_reglement_id = (int) $this->mode_reglement_id;
1260 }
1261 if (isset($this->cond_reglement_id)) {
1262 $this->cond_reglement_id = (int) $this->cond_reglement_id;
1263 }
1264 if (isset($this->note_private)) {
1265 $this->note_private = trim($this->note_private);
1266 $this->note = $this->note_private;
1267 }
1268 if (isset($this->note_public)) {
1269 $this->note_public = trim($this->note_public);
1270 }
1271 if (isset($this->model_pdf)) {
1272 $this->model_pdf = trim($this->model_pdf);
1273 }
1274 if (isset($this->import_key)) {
1275 $this->import_key = trim($this->import_key);
1276 }
1277
1278
1279 // Check parameters
1280 // Put here code to add control on parameters values
1281
1282 if (dol_strlen((string) $this->date_modification) == 0) {
1283 $this->tms = dol_now();
1284 }
1285
1286 // Update request
1287 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn SET";
1288 $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1289 $sql .= " ref_supplier=".(isset($this->ref_supplier) ? "'".$this->db->escape($this->ref_supplier)."'" : "null").",";
1290 $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1291 $sql .= " entity=".(isset($this->entity) ? ((int) $this->entity) : "null").",";
1292 $sql .= " type=".(isset($this->type) ? ((int) $this->type) : "null").",";
1293 $sql .= " subtype=".(isset($this->subtype) ? (int) $this->subtype : "null").",";
1294 $sql .= " fk_soc=".(isset($this->socid) ? ((int) $this->socid) : "null").",";
1295 $sql .= " datec=".(dol_strlen((string) $this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
1296 $sql .= " datef=".(dol_strlen((string) $this->date) != 0 ? "'".$this->db->idate($this->date)."'" : 'null').",";
1297 $sql .= " date_pointoftax=".(dol_strlen((string) $this->date_pointoftax) != 0 ? "'".$this->db->idate($this->date_pointoftax)."'" : 'null').",";
1298 if (dol_strlen((string) $this->date_modification) != 0) {
1299 $sql .= " tms=".(dol_strlen((string) $this->date_modification) != 0 ? "'".$this->db->idate($this->date_modification)."'" : 'null').",";
1300 } elseif (dol_strlen((string) $this->tms) != 0) { // For backward compatibility
1301 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
1302 }
1303 $sql .= " libelle=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
1304 $sql .= " paye=".(isset($this->paid) ? ((int) $this->paid) : "0").",";
1305 $sql .= " close_code=".(isset($this->close_code) ? "'".$this->db->escape($this->close_code)."'" : "null").",";
1306 $sql .= " close_note=".(isset($this->close_note) ? "'".$this->db->escape($this->close_note)."'" : "null").",";
1307 $sql .= " localtax1=".(isset($this->total_localtax1) ? ((float) $this->total_localtax1) : "null").",";
1308 $sql .= " localtax2=".(isset($this->total_localtax2) ? ((float) $this->total_localtax2) : "null").",";
1309 $sql .= " total_ht=".(isset($this->total_ht) ? ((float) $this->total_ht) : "null").",";
1310 $sql .= " total_tva=".(isset($this->total_tva) ? ((float) $this->total_tva) : "null").",";
1311 $sql .= " total_ttc=".(isset($this->total_ttc) ? ((float) $this->total_ttc) : "null").",";
1312 $sql .= " fk_statut=".(isset($this->status) ? ((int) $this->status) : (isset($this->statut) ? ((int) $this->statut) : "null")).",";
1313 $sql .= " fk_user_author=".(isset($this->author) ? ((int) $this->author) : "null").",";
1314 $sql .= " fk_user_valid=".(isset($this->fk_user_valid) ? ((int) $this->fk_user_valid) : "null").",";
1315 $sql .= " fk_facture_source=".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").",";
1316 $sql .= " vat_reverse_charge = ".($this->vat_reverse_charge != '' ? ((int) $this->vat_reverse_charge) : 0).",";
1317 $sql .= " fk_projet=".(!empty($this->fk_project) ? ((int) $this->fk_project) : "null").",";
1318 $sql .= " fk_mode_reglement=".(isset($this->mode_reglement_id) ? ((int) $this->mode_reglement_id) : "null").",";
1319 $sql .= " fk_cond_reglement=".(isset($this->cond_reglement_id) ? ((int) $this->cond_reglement_id) : "null").",";
1320 $sql .= " date_lim_reglement=".(dol_strlen((string) $this->date_echeance) != 0 ? "'".$this->db->idate($this->date_echeance)."'" : 'null').",";
1321 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1322 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1323 $sql .= " model_pdf=".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").",";
1324 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
1325 $sql .= " WHERE rowid=".((int) $this->id);
1326
1327 $this->db->begin();
1328
1329 dol_syslog(get_class($this)."::update", LOG_DEBUG);
1330 $resql = $this->db->query($sql);
1331
1332 if (!$resql) {
1333 $error++;
1334
1335 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1336 $this->errors[] = $langs->trans('ErrorRefAlreadyExists');
1337 } else {
1338 $this->errors[] = "Error ".$this->db->lasterror();
1339 }
1340 }
1341
1342 if (!$error) {
1343 $result = $this->insertExtraFields();
1344 if ($result < 0) {
1345 $error++;
1346 }
1347 }
1348
1349 if (!$error && !$notrigger) {
1350 // Call trigger
1351 $result = $this->call_trigger('BILL_SUPPLIER_MODIFY', $user);
1352 if ($result < 0) {
1353 $error++;
1354 }
1355 // End call triggers
1356 }
1357
1358 // Commit or rollback
1359 if ($error) {
1360 foreach ($this->errors as $errmsg) {
1361 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1362 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1363 }
1364 $this->db->rollback();
1365 return -1 * $error;
1366 } else {
1367 $this->db->commit();
1368 return 1;
1369 }
1370 }
1371
1372 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1379 public function insert_discount($idremise)
1380 {
1381 // phpcs:enable
1382 global $conf, $langs;
1383
1384 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1385 include_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1386
1387 $this->db->begin();
1388
1389 $remise = new DiscountAbsolute($this->db);
1390 $result = $remise->fetch($idremise);
1391
1392 if ($result > 0) {
1393 if ($this->socid > 0 && $remise->fk_soc != $this->socid) { // The discount must belong to the thirdparty of the invoice
1394 $this->error = $langs->trans("ErrorDiscountNotSameCompany");
1395 $this->db->rollback();
1396 return -6;
1397 }
1398 if ($remise->fk_invoice_supplier) { // Protection against multiple submission
1399 $this->error = $langs->trans("ErrorDiscountAlreadyUsed");
1400 $this->db->rollback();
1401 return -5;
1402 }
1403
1404 $facligne = new SupplierInvoiceLine($this->db);
1405 $facligne->fk_facture_fourn = $this->id;
1406 $facligne->fk_remise_except = $remise->id;
1407 $facligne->desc = $remise->description; // Line description
1408 $facligne->vat_src_code = $remise->vat_src_code;
1409 $facligne->tva_tx = $remise->tva_tx;
1410 $facligne->localtax1_tx = $remise->localtax1_tx;
1411 $facligne->localtax1_type = $remise->localtax1_type;
1412 $facligne->localtax2_tx = $remise->localtax1_tx;
1413 $facligne->localtax2_type = $remise->localtax1_type;
1414 $facligne->subprice = -(float) $remise->amount_ht;
1415 $facligne->fk_product = 0; // Predefined Product ID
1416 $facligne->product_type = 0;
1417 $facligne->qty = 1;
1418 $facligne->remise_percent = 0;
1419 $facligne->rang = -1;
1420 $facligne->info_bits = 2;
1421
1422 if (getDolGlobalString('MAIN_ADD_LINE_AT_POSITION')) {
1423 $facligne->rang = 1;
1424 $linecount = count($this->lines);
1425 for ($ii = 1; $ii <= $linecount; $ii++) {
1426 $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1);
1427 }
1428 }
1429
1430 // Get buy/cost price of invoice that is source of discount
1431 if ($remise->fk_invoice_supplier_source > 0) {
1432 $srcinvoice = new FactureFournisseur($this->db);
1433 $srcinvoice->fetch($remise->fk_invoice_supplier_source);
1434 $totalcostpriceofinvoice = 0;
1435 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmargin.class.php'; // TODO Move this into commonobject
1436 $formmargin = new FormMargin($this->db);
1437 $arraytmp = $formmargin->getMarginInfosArray($srcinvoice, false);
1438 $facligne->pa_ht = $arraytmp['pa_total'];
1439 }
1440
1441 $facligne->total_ht = -(float) $remise->amount_ht;
1442 $facligne->total_tva = -(float) $remise->amount_tva;
1443 $facligne->total_ttc = -(float) $remise->amount_ttc;
1444 $facligne->total_localtax1 = -(float) $remise->total_localtax1;
1445 $facligne->total_localtax2 = -(float) $remise->total_localtax2;
1446
1447 // The discount line must carry the currency of the invoice, like any other line
1448 $facligne->fk_multicurrency = $this->fk_multicurrency;
1449 $facligne->multicurrency_code = $this->multicurrency_code;
1450 $facligne->multicurrency_subprice = -$remise->multicurrency_subprice;
1451 $facligne->multicurrency_total_ht = -$remise->multicurrency_total_ht;
1452 $facligne->multicurrency_total_tva = -$remise->multicurrency_total_tva;
1453 $facligne->multicurrency_total_ttc = -$remise->multicurrency_total_ttc;
1454
1455 $lineid = $facligne->insert();
1456 if ($lineid > 0) {
1457 $result = $this->update_price(1);
1458 if ($result > 0) {
1459 // Create link between discount and invoice line
1460 $result = $remise->link_to_invoice($lineid, 0);
1461 if ($result < 0) {
1462 $this->error = $remise->error;
1463 $this->db->rollback();
1464 return -4;
1465 }
1466
1467 $this->db->commit();
1468 return 1;
1469 } else {
1470 $this->error = $facligne->error;
1471 $this->db->rollback();
1472 return -1;
1473 }
1474 } else {
1475 $this->error = $facligne->error;
1476 $this->db->rollback();
1477 return -2;
1478 }
1479 } else {
1480 $this->db->rollback();
1481 return -3;
1482 }
1483 }
1484
1485
1493 public function delete(User $user, $notrigger = 0)
1494 {
1495 global $conf;
1496
1497 $rowid = $this->id;
1498
1499 dol_syslog("FactureFournisseur::delete rowid=".$rowid, LOG_DEBUG);
1500
1501 // Test to avoid invoice deletion (invoice transferred into accountancy, with payment, ...), same test as Facture::delete() does
1502 $result = $this->is_erasable();
1503 if ($result <= 0) {
1504 dol_syslog(get_class($this)."::delete refused, invoice is not erasable (code ".$result.")", LOG_DEBUG);
1505 return 0;
1506 }
1507
1508 $error = 0;
1509 $this->db->begin();
1510
1511 if (!$notrigger) {
1512 // Call trigger
1513 $result = $this->call_trigger('BILL_SUPPLIER_DELETE', $user);
1514 if ($result < 0) {
1515 $this->db->rollback();
1516 return -1;
1517 }
1518 // Fin appel triggers
1519 }
1520
1521 // Remove linked categories.
1522 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_supplier_invoice";
1523 $sql .= " WHERE fk_supplier_invoice = ".((int) $this->id);
1524
1525 $result = $this->db->query($sql);
1526 if (!$result) {
1527 $error++;
1528 $this->error = $this->db->lasterror();
1529 $this->errors[] = $this->error;
1530 }
1531
1532 if (!$error) {
1533 // If invoice was converted into a discount not yet consumed, we remove discount
1534 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'societe_remise_except';
1535 $sql .= ' WHERE fk_invoice_supplier_source = '.((int) $rowid);
1536 $sql .= ' AND fk_invoice_supplier_line IS NULL';
1537 $resql = $this->db->query($sql);
1538
1539 // If invoice has consumned discounts
1540 $this->fetch_lines();
1541 $list_rowid_det = array();
1542 foreach ($this->lines as $key => $invoiceline) {
1543 $list_rowid_det[] = $invoiceline->id;
1544 }
1545
1546 // Consumned discounts are freed
1547 if (count($list_rowid_det)) {
1548 $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except';
1549 $sql .= ' SET fk_invoice_supplier = NULL, fk_invoice_supplier_line = NULL';
1550 $sql .= ' WHERE fk_invoice_supplier_line IN ('.$this->db->sanitize(implode(',', $list_rowid_det)).')';
1551
1552 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1553 if (!$this->db->query($sql)) {
1554 $error++;
1555 }
1556 }
1557 }
1558
1559 if (!$error) {
1560 $sql_main_table = MAIN_DB_PREFIX.'facture_fourn_det';
1561 $sql_ef_table = $sql_main_table."_extrafields";
1562 $sqlef = "DELETE FROM $sql_ef_table WHERE fk_object IN (SELECT rowid FROM ".$sql_main_table." WHERE fk_facture_fourn = ".((int) $rowid).")";
1563 $resqlef = $this->db->query($sqlef);
1564 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det WHERE fk_facture_fourn = '.((int) $rowid);
1565 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1566 $resql = $this->db->query($sql);
1567 if ($resqlef && $resql) {
1568 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn WHERE rowid = '.((int) $rowid);
1569 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1570 $resql2 = $this->db->query($sql);
1571 if (!$resql2) {
1572 $error++;
1573 }
1574 } else {
1575 $error++;
1576 }
1577 }
1578
1579 if (!$error) {
1580 // Delete linked object
1581 $res = $this->deleteObjectLinked();
1582 if ($res < 0) {
1583 $error++;
1584 }
1585 }
1586
1587 if (!$error) {
1588 // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
1589 $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive
1590 $this->deleteEcmFiles(1); // Deleting files physically is done later with the dol_delete_dir_recursive
1591
1592 // We remove directory
1593 $ref = dol_sanitizeFileName($this->ref);
1594 if ($conf->fournisseur->facture->dir_output && !empty($ref)) {
1595 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1596
1597 $dir = $conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$ref;
1598 $file = $dir."/".$ref.".pdf";
1599 if (file_exists($file)) {
1600 if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
1601 $this->error = 'ErrorFailToDeleteFile';
1602 $error++;
1603 }
1604 }
1605 if (file_exists($dir)) {
1606 $res = @dol_delete_dir_recursive($dir);
1607
1608 if (!$res) {
1609 $this->error = 'ErrorFailToDeleteDir';
1610 $error++;
1611 }
1612 }
1613 }
1614 }
1615
1616 // Remove extrafields
1617 if (!$error) {
1618 $result = $this->deleteExtraFields();
1619 if ($result < 0) {
1620 $error++;
1621 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1622 }
1623 }
1624
1625 if (!$error) {
1626 dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
1627 $this->db->commit();
1628 return 1;
1629 } else {
1630 $this->error = $this->db->lasterror();
1631 $this->db->rollback();
1632 return -$error;
1633 }
1634 }
1635
1636
1637 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1648 public function set_paid($user, $close_code = '', $close_note = '')
1649 {
1650 // phpcs:enable
1651 dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
1652 return $this->setPaid($user, $close_code, $close_note);
1653 }
1654
1663 public function setPaid($user, $close_code = '', $close_note = '')
1664 {
1665 $error = 0;
1666
1667 if ($this->paid != 1) {
1668 $this->db->begin();
1669
1670 $now = dol_now();
1671
1672 dol_syslog("FactureFournisseur::setPaid", LOG_DEBUG);
1673
1674 $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn SET';
1675 $sql .= ' fk_statut = '.self::STATUS_CLOSED;
1676 if (!$close_code) {
1677 $sql .= ', paye=1';
1678 }
1679 if ($close_code) {
1680 $sql .= ", close_code='".$this->db->escape($close_code)."'";
1681 }
1682 if ($close_note) {
1683 $sql .= ", close_note='".$this->db->escape($close_note)."'";
1684 }
1685 $sql .= ', fk_user_closing = '.((int) $user->id);
1686 $sql .= ", date_closing = '".$this->db->idate($now)."'";
1687 $sql .= ' WHERE rowid = '.((int) $this->id);
1688
1689 $resql = $this->db->query($sql);
1690 if ($resql) {
1691 // Call trigger
1692 $result = $this->call_trigger('BILL_SUPPLIER_PAYED', $user);
1693 if ($result < 0) {
1694 $error++;
1695 }
1696 // End call triggers
1697 } else {
1698 $error++;
1699 $this->error = $this->db->error();
1700 dol_print_error($this->db);
1701 }
1702
1703 if (!$error) {
1704 $this->db->commit();
1705 return 1;
1706 } else {
1707 $this->db->rollback();
1708 return -1;
1709 }
1710 } else {
1711 return 0;
1712 }
1713 }
1714
1715 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1726 public function set_unpaid($user)
1727 {
1728 // phpcs:enable
1729 dol_syslog(get_class($this)."::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
1730 return $this->setUnpaid($user);
1731 }
1732
1741 public function setUnpaid($user)
1742 {
1743 $error = 0;
1744
1745 $this->db->begin();
1746
1747 $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn';
1748 $sql .= ' SET paye=0, fk_statut='.self::STATUS_VALIDATED.', close_code=null, close_note=null,';
1749 $sql .= ' date_closing=null,';
1750 $sql .= ' fk_user_closing=null';
1751 $sql .= ' WHERE rowid = '.((int) $this->id);
1752
1753 dol_syslog(get_class($this)."::set_unpaid", LOG_DEBUG);
1754 $resql = $this->db->query($sql);
1755 if ($resql) {
1756 // Call trigger
1757 $result = $this->call_trigger('BILL_SUPPLIER_UNPAYED', $user);
1758 if ($result < 0) {
1759 $error++;
1760 }
1761 // End call triggers
1762 } else {
1763 $error++;
1764 $this->error = $this->db->error();
1765 dol_print_error($this->db);
1766 }
1767
1768 if (!$error) {
1769 $this->db->commit();
1770 return 1;
1771 } else {
1772 $this->db->rollback();
1773 return -1;
1774 }
1775 }
1776
1787 public function setCanceled($user, $close_code = '', $close_note = '')
1788 {
1789 dol_syslog(get_class($this)."::setCanceled rowid=".((int) $this->id), LOG_DEBUG);
1790
1791 $this->db->begin();
1792
1793 $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn SET';
1794 $sql .= ' fk_statut='.self::STATUS_ABANDONED;
1795 if ($close_code) {
1796 $sql .= ", close_code='".$this->db->escape($close_code)."'";
1797 }
1798 if ($close_note) {
1799 $sql .= ", close_note='".$this->db->escape($close_note)."'";
1800 }
1801 $sql .= " WHERE rowid = ".((int) $this->id);
1802
1803 $resql = $this->db->query($sql);
1804 if ($resql) {
1805 // Bound discounts are deducted from the invoice
1806 // as they have not been used since the invoice is abandoned.
1807 $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except';
1808 $sql .= ' SET fk_invoice_supplier = NULL';
1809 $sql .= ' WHERE fk_invoice_supplier = '.((int) $this->id);
1810
1811 $resql = $this->db->query($sql);
1812 if ($resql) {
1813 // Call trigger
1814 $result = $this->call_trigger('BILL_SUPPLIER_CANCELED', $user);
1815 if ($result < 0) {
1816 $this->db->rollback();
1817 return -1;
1818 }
1819 // End call triggers
1820
1821 $this->db->commit();
1822 return 1;
1823 } else {
1824 $this->error = $this->db->error()." sql=".$sql;
1825 $this->db->rollback();
1826 return -1;
1827 }
1828 } else {
1829 $this->error = $this->db->error()." sql=".$sql;
1830 $this->db->rollback();
1831 return -2;
1832 }
1833 }
1834
1844 public function validate($user, $force_number = '', $idwarehouse = 0, $notrigger = 0)
1845 {
1846 global $mysoc, $conf, $langs;
1847
1848 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1849
1850 $now = dol_now();
1851
1852 $error = 0;
1853 dol_syslog(get_class($this).'::validate user='.$user->id.', force_number='.$force_number.', idwarehouse='.$idwarehouse);
1854
1855 // Force to have object complete for checks
1856 $this->fetch_thirdparty();
1857 $this->fetch_lines();
1858
1859 // Check parameters
1860 if ($this->status > self::STATUS_DRAFT) { // This is to avoid to validate twice (avoid errors on logs and stock management)
1861 dol_syslog(get_class($this)."::validate no draft status", LOG_WARNING);
1862 return 0;
1863 }
1864 if (preg_match('/^'.preg_quote($langs->trans("CopyOf").' ', '/').'/', $this->ref_supplier)) {
1865 $langs->load("errors");
1866 $this->error = $langs->trans("ErrorFieldFormat", $langs->transnoentities("RefSupplier")).'. '.$langs->trans('RemoveString', $langs->transnoentitiesnoconv("CopyOf"));
1867 return -1;
1868 }
1869 if (count($this->lines) <= 0) {
1870 $langs->load("errors");
1871 $this->error = $langs->trans("ErrorObjectMustHaveLinesToBeValidated", $this->ref);
1872 return -1;
1873 }
1874
1875 // Check for mandatory fields in thirdparty (defined into setup)
1876 if (!empty($this->thirdparty) && is_object($this->thirdparty)) {
1877 $array_to_check = array('IDPROF1', 'IDPROF2', 'IDPROF3', 'IDPROF4', 'IDPROF5', 'IDPROF6', 'EMAIL', 'ACCOUNTANCY_CODE_SUPPLIER');
1878 foreach ($array_to_check as $key) {
1879 $keymin = strtolower($key);
1880 if ($keymin == 'accountancy_code_supplier') {
1881 $keymin = 'code_compta_fournisseur';
1882 }
1883 if (!property_exists($this->thirdparty, $keymin)) {
1884 continue;
1885 }
1886 $vallabel = $this->thirdparty->$keymin;
1887
1888 $i = (int) preg_replace('/[^0-9]/', '', $key);
1889 if ($i > 0) {
1890 if ($this->thirdparty->isACompany()) {
1891 // Check for mandatory prof id (but only if country is other than ours)
1892 if ($mysoc->country_id > 0 && $this->thirdparty->country_id == $mysoc->country_id) {
1893 $idprof_mandatory = 'SOCIETE_'.$key.'_INVOICE_MANDATORY';
1894 if (!$vallabel && getDolGlobalString($idprof_mandatory)) {
1895 $langs->load("errors");
1896 $this->error = $langs->trans('ErrorProdIdIsMandatory', $langs->transcountry('ProfId'.$i, $this->thirdparty->country_code)).' ('.$langs->trans("ForbiddenBySetupRules").') ['.$langs->trans('Company').' : '.$this->thirdparty->name.']';
1897 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
1898 return -1;
1899 }
1900 }
1901 }
1902 } else {
1903 if ($key == 'EMAIL') {
1904 // Check for mandatory
1905 if (getDolGlobalString('SOCIETE_EMAIL_INVOICE_MANDATORY') && !isValidEmail($this->thirdparty->email)) {
1906 $langs->load("errors");
1907 $this->error = $langs->trans("ErrorBadEMail", $this->thirdparty->email).' ('.$langs->trans("ForbiddenBySetupRules").') ['.$langs->trans('Company').' : '.$this->thirdparty->name.']';
1908 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
1909 return -1;
1910 }
1911 } elseif ($key == 'ACCOUNTANCY_CODE_SUPPLIER') {
1912 // Check for mandatory
1913 if (getDolGlobalString('SOCIETE_ACCOUNTANCY_CODE_SUPPLIER_INVOICE_MANDATORY') && empty($this->thirdparty->code_compta_fournisseur)) {
1914 $langs->load("errors");
1915 $this->error = $langs->trans("ErrorAccountancyCodeSupplierIsMandatory", $this->thirdparty->name).' ('.$langs->trans("ForbiddenBySetupRules").')';
1916 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
1917 return -1;
1918 }
1919 }
1920 }
1921 }
1922 }
1923
1924 $this->db->begin();
1925
1926 // Define new ref
1927 if ($force_number) {
1928 $num = $force_number;
1929 } elseif (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref)) { // empty should not happened, but when it occurs, the test save life
1930 $num = $this->getNextNumRef($this->thirdparty);
1931 } else {
1932 $num = (string) $this->ref;
1933 }
1934 $this->newref = dol_sanitizeFileName($num);
1935
1936 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn";
1937 $sql .= " SET ref='".$this->db->escape($num)."', fk_statut = 1, fk_user_valid = ".((int) $user->id).", date_valid = '".$this->db->idate($now)."'";
1938 $sql .= " WHERE rowid = ".((int) $this->id);
1939
1940 dol_syslog(get_class($this)."::validate", LOG_DEBUG);
1941 $resql = $this->db->query($sql);
1942 if ($resql) {
1943 // If we increment the main product and its components upon validation of the supplier invoice
1944 if (isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL')) {
1945 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
1946 $langs->load("agenda");
1947
1948 $cpt = count($this->lines);
1949 for ($i = 0; $i < $cpt; $i++) {
1950 if ($this->lines[$i]->fk_product > 0) {
1951 $mouvP = new MouvementStock($this->db);
1952 $mouvP->origin = &$this;
1953 $mouvP->setOrigin($this->element, $this->id);
1954 // We increase stock for product
1955 $up_ht_disc = $this->lines[$i]->subprice;
1956 if (!empty($this->lines[$i]->remise_percent) && !getDolGlobalString('STOCK_EXCLUDE_DISCOUNT_FOR_PMP')) {
1957 $up_ht_disc = price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
1958 }
1960 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("InvoiceValidatedInDolibarr", $num));
1961 } else {
1962 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("InvoiceValidatedInDolibarr", $num));
1963 }
1964 if ($result < 0) {
1965 $this->setErrorsFromObject($mouvP);
1966 return -2;
1967 }
1968 }
1969 }
1970 }
1971
1972 // Triggers call
1973 if (empty($notrigger)) {
1974 // Call trigger
1975 $result = $this->call_trigger('BILL_SUPPLIER_VALIDATE', $user);
1976 if ($result < 0) {
1977 $error++;
1978 }
1979 // End call triggers
1980 }
1981
1982 if (!$error) {
1983 $this->oldref = $this->ref;
1984
1985 // Rename directory if dir was a temporary ref
1986 if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
1987 // Now we rename also files into index
1988 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'fournisseur/facture/".$this->db->escape(get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier')).$this->db->escape($this->newref)."'";
1989 $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'fournisseur/facture/".$this->db->escape(get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier')).$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
1990 $resql = $this->db->query($sql);
1991 if (!$resql) {
1992 $error++;
1993 $this->error = $this->db->lasterror();
1994 }
1995 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'fournisseur/facture/".$this->db->escape(get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier')).$this->db->escape($this->newref)."'";
1996 $sql .= " WHERE filepath = 'fournisseur/facture/".$this->db->escape(get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier')).$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
1997 $resql = $this->db->query($sql);
1998 if (!$resql) {
1999 $error++;
2000 $this->error = $this->db->lasterror();
2001 }
2002
2003 // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
2004 $oldref = dol_sanitizeFileName($this->ref);
2005 $dirsource = $conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$oldref;
2006 $dirdest = $conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$this->newref;
2007 if (!$error && file_exists($dirsource)) {
2008 dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest);
2009
2010 if (@rename($dirsource, $dirdest)) {
2011 dol_syslog("Rename ok");
2012 // Rename docs starting with $oldref with $this->newref
2013 $listoffiles = dol_dir_list($conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$this->newref, 'files', 1, '^'.preg_quote($oldref, '/'));
2014 foreach ($listoffiles as $fileentry) {
2015 $dirsource = $fileentry['name'];
2016 $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $this->newref, $dirsource);
2017 $dirsource = $fileentry['path'].'/'.$dirsource;
2018 $dirdest = $fileentry['path'].'/'.$dirdest;
2019 @rename($dirsource, $dirdest);
2020 }
2021 }
2022 }
2023 }
2024 }
2025
2026 // Set new ref and define current status
2027 if (!$error) {
2028 $this->ref = $this->newref;
2029 $this->statut = self::STATUS_VALIDATED;
2031 //$this->date_validation=$now; this is stored into log table
2032 }
2033
2034 if (!$error) {
2035 $this->db->commit();
2036 return 1;
2037 } else {
2038 $this->db->rollback();
2039 return -1;
2040 }
2041 } else {
2042 $this->error = $this->db->error();
2043 $this->db->rollback();
2044 return -1;
2045 }
2046 }
2047
2056 public function setDraft($user, $idwarehouse = -1, $notrigger = 0)
2057 {
2058 // phpcs:enable
2059 global $conf, $langs;
2060
2061 $error = 0;
2062
2063 if ($this->status == self::STATUS_DRAFT) {
2064 dol_syslog(__METHOD__." already draft status", LOG_WARNING);
2065 return 0;
2066 }
2067
2068 dol_syslog(__METHOD__, LOG_DEBUG);
2069
2070 $this->db->begin();
2071
2072 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn";
2073 $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
2074 $sql .= " WHERE rowid = ".((int) $this->id);
2075
2076 $result = $this->db->query($sql);
2077 if ($result) {
2078 $this->oldcopy = clone $this;
2079
2080 // If we increment the main product and its components upon supplier invoice validation, we decrement upon customer invoice validation
2081 if ($result >= 0 && isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL')) {
2082 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
2083 $langs->load("agenda");
2084
2085 $cpt = count($this->lines);
2086 for ($i = 0; $i < $cpt; $i++) {
2087 if ($this->lines[$i]->fk_product > 0) {
2088 $mouvP = new MouvementStock($this->db);
2089 $mouvP->origin = &$this;
2090 $mouvP->setOrigin($this->element, $this->id);
2091 // We increase stock for product
2093 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->transnoentitiesnoconv("InvoiceBackToDraftInDolibarr", $this->ref));
2094 } else {
2095 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->transnoentitiesnoconv("InvoiceBackToDraftInDolibarr", $this->ref));
2096 }
2097 if ($result < 0) {
2098 $error++;
2099 $this->setErrorsFromObject($mouvP);
2100 dol_syslog(__METHOD__." stock movement failed for line ".$i.": ".$mouvP->error, LOG_ERR);
2101 break;
2102 }
2103 }
2104 }
2105 }
2106 // Triggers call
2107 if (!$error && empty($notrigger)) {
2108 // Call trigger
2109 $result = $this->call_trigger('BILL_SUPPLIER_UNVALIDATE', $user);
2110 if ($result < 0) {
2111 $error++;
2112 }
2113 // End call triggers
2114 }
2115 if ($error == 0) {
2116 $this->db->commit();
2117 return 1;
2118 } else {
2119 $this->db->rollback();
2120 return -1;
2121 }
2122 } else {
2123 $this->error = $this->db->error();
2124 $this->db->rollback();
2125 return -1;
2126 }
2127 }
2128
2129
2164 public function addline($desc, $pu, $txtva, $txlocaltax1, $txlocaltax2, $qty, $fk_product = 0, $remise_percent = 0, $date_start = 0, $date_end = 0, $fk_code_ventilation = 0, $info_bits = 0, $price_base_type = 'HT', $type = 0, $rang = -1, $notrigger = 0, $array_options = [], $fk_unit = null, $origin_id = 0, $pu_devise = 0, $ref_supplier = '', $special_code = 0, $fk_parent_line = 0, $fk_remise_except = 0, $origin_type = '')
2165 {
2166 global $langs, $mysoc;
2167
2168 dol_syslog(get_class($this)."::addline $desc,$pu,$qty,$txtva,$fk_product,$remise_percent,$date_start,$date_end,$fk_code_ventilation,$info_bits,$price_base_type,$type,$fk_unit,fk_remise_except=$fk_remise_except", LOG_DEBUG);
2169 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2170
2171 if ($this->status == self::STATUS_DRAFT) {
2172 // Clean parameters
2173 if (empty($remise_percent)) {
2174 $remise_percent = 0;
2175 }
2176 if (empty($qty)) {
2177 $qty = 0;
2178 }
2179 if (empty($info_bits)) {
2180 $info_bits = 0;
2181 }
2182 if (empty($rang)) {
2183 $rang = 0;
2184 }
2185 if (empty($fk_code_ventilation)) {
2186 $fk_code_ventilation = 0;
2187 }
2188 if (empty($txtva)) {
2189 $txtva = 0;
2190 }
2191 if (empty($txlocaltax1)) {
2192 $txlocaltax1 = 0;
2193 }
2194 if (empty($txlocaltax2)) {
2195 $txlocaltax2 = 0;
2196 }
2197
2198 $remise_percent = price2num($remise_percent);
2199 $qty = price2num($qty);
2200 $pu = price2num($pu);
2201 if (!preg_match('/\‍((.*)\‍)/', (string) $txtva)) {
2202 $txtva = price2num($txtva); // $txtva can have format '5,1' or '5.1' or '5.1(XXX)', we must clean only if '5,1'
2203 }
2204 $txlocaltax1 = price2num($txlocaltax1);
2205 $txlocaltax2 = price2num($txlocaltax2);
2206
2207 if ($date_start && $date_end && $date_start > $date_end) {
2208 $langs->load("errors");
2209 $this->error = $langs->trans('ErrorStartDateGreaterEnd');
2210 return -1;
2211 }
2212
2213 $this->db->begin();
2214
2215 if ($fk_product > 0) {
2216 if (getDolGlobalInt('SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY') == 1) { // Not the common case
2217 // Check quantity is enough
2218 dol_syslog(get_class($this)."::addline we check supplier prices fk_product=".$fk_product." qty=".$qty." ref_supplier=".$ref_supplier);
2219 $prod = new ProductFournisseur($this->db);
2220 if ($prod->fetch($fk_product) > 0) {
2221 $product_type = $prod->type;
2222 $label = $prod->label;
2223 $fk_prod_fourn_price = 0;
2224
2225 // We use 'none' instead of $ref_supplier, because $ref_supplier may not exists anymore. So we will take the first supplier price ok.
2226 // If we want a dedicated supplier price, we must provide $fk_prod_fourn_price.
2227 $result = $prod->get_buyprice($fk_prod_fourn_price, (float) $qty, $fk_product, 'none', ($this->fk_soc ? $this->fk_soc : $this->socid)); // Search on couple $fk_prod_fourn_price/$qty first, then on triplet $qty/$fk_product/$ref_supplier/$this->fk_soc
2228 if ($result > 0) {
2229 if (empty($pu)) {
2230 $pu = $prod->fourn_pu; // Unit price supplier price set by get_buyprice
2231 }
2232 $ref_supplier = $prod->ref_supplier; // Ref supplier price set by get_buyprice
2233 // is remise percent not keyed but present for the product we add it
2234 if ($remise_percent == 0 && $prod->remise_percent != 0) {
2235 $remise_percent = $prod->remise_percent;
2236 }
2237 }
2238 if ($result == 0) { // If result == 0, we failed to found the supplier reference price
2239 $langs->load("errors");
2240 $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
2241 $this->db->rollback();
2242 dol_syslog(get_class($this)."::addline we did not found supplier price, so we can't guess unit price");
2243 //$pu = $prod->fourn_pu; // We do not overwrite unit price
2244 //$ref = $prod->ref_fourn; // We do not overwrite ref supplier price
2245 return -1;
2246 }
2247 if ($result == -1) {
2248 $langs->load("errors");
2249 $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
2250 $this->db->rollback();
2251 dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_DEBUG);
2252 return -1;
2253 }
2254 if ($result < -1) {
2255 $this->error = $prod->error;
2256 $this->db->rollback();
2257 dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_ERR);
2258 return -1;
2259 }
2260 } else {
2261 $this->error = $prod->error;
2262 $this->db->rollback();
2263 return -1;
2264 }
2265 }
2266 } else {
2267 $product_type = $type;
2268 }
2269
2270 if (isModEnabled("multicurrency") && $pu_devise > 0) {
2271 $pu = 0;
2272 }
2273
2274 $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
2275
2276 // Clean vat code
2277 $reg = array();
2278 $vat_src_code = '';
2279 if (preg_match('/\‍((.*)\‍)/', $txtva, $reg)) {
2280 $vat_src_code = $reg[1];
2281 $txtva = preg_replace('/\s*\‍(.*\‍)/', '', $txtva); // Remove code into vatrate.
2282 }
2283
2284 // Calculation of the gross total (TTC) and VAT for the line from qty, pu, remise_percent and txtva
2285 // VERY IMPORTANT: It's at the time of line insertion that we must store the net, VAT, and gross amounts,
2286 // and this is done at the line level, which has its own VAT rate
2287
2288 $tabprice = calcul_price_total((float) $qty, $pu, (float) $remise_percent, $txtva, (float) $txlocaltax1, (float) $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_devise);
2289 $total_ht = $tabprice[0];
2290 $total_tva = $tabprice[1];
2291 $total_ttc = $tabprice[2];
2292 $total_localtax1 = $tabprice[9];
2293 $total_localtax2 = $tabprice[10];
2294 $pu_ht = $tabprice[3];
2295
2296 // MultiCurrency
2297 $multicurrency_total_ht = $tabprice[16];
2298 $multicurrency_total_tva = $tabprice[17];
2299 $multicurrency_total_ttc = $tabprice[18];
2300 $pu_ht_devise = $tabprice[19];
2301
2302 // Check parameters
2303 if ($type < 0) {
2304 return -1;
2305 }
2306
2307 if ($rang < 0) {
2308 $rangmax = $this->line_max();
2309 $rang = $rangmax + 1;
2310 }
2311
2312 // Same gate as on the customer invoice side: the -abs() forcing on credit note lines is relaxed only when
2313 // both FACTURE_ENABLE_NEGATIVE_LINES and INVOICE_KEEP_DISCOUNT_LINES_AS_IN_ORIGIN are enabled.
2314 $apply_abs_price_on_credit_note = false;
2315 if ($this->type == self::TYPE_CREDIT_NOTE && !(getDolGlobalInt('FACTURE_ENABLE_NEGATIVE_LINES') && getDolGlobalInt('INVOICE_KEEP_DISCOUNT_LINES_AS_IN_ORIGIN'))) {
2316 $apply_abs_price_on_credit_note = true;
2317 }
2318
2319 // Insert line
2320 $supplierinvoiceline = new SupplierInvoiceLine($this->db);
2321
2322 $supplierinvoiceline->context = $this->context;
2323
2324 $supplierinvoiceline->fk_facture_fourn = $this->id;
2325 //$supplierinvoiceline->label=$label; // deprecated
2326 $supplierinvoiceline->desc = $desc;
2327 $supplierinvoiceline->ref_supplier = $ref_supplier;
2328
2329 $supplierinvoiceline->qty = ($this->type == self::TYPE_CREDIT_NOTE ? abs((float) $qty) : (float) $qty); // For credit note, quantity is always positive and unit price negative
2330 $supplierinvoiceline->subprice = ($apply_abs_price_on_credit_note ? -abs((float) $pu_ht) : (float) $pu_ht); // For credit note, unit price always negative, always positive otherwise
2331
2332 $supplierinvoiceline->vat_src_code = $vat_src_code;
2333 $supplierinvoiceline->tva_tx = $txtva;
2334 $supplierinvoiceline->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
2335 $supplierinvoiceline->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
2336 $supplierinvoiceline->localtax1_type = empty($localtaxes_type[0]) ? 0 : $localtaxes_type[0];
2337 $supplierinvoiceline->localtax2_type = empty($localtaxes_type[2]) ? 0 : $localtaxes_type[2];
2338
2339 $supplierinvoiceline->total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $total_ht) : (float) $total_ht); // For credit note and if qty is negative, total is negative
2340 $supplierinvoiceline->total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $total_tva) : (float) $total_tva); // For credit note and if qty is negative, total is negative
2341 $supplierinvoiceline->total_localtax1 = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $total_localtax1) : (float) $total_localtax1); // For credit note and if qty is negative, total is negative
2342 $supplierinvoiceline->total_localtax2 = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $total_localtax2) : (float) $total_localtax2); // For credit note and if qty is negative, total is negative
2343 $supplierinvoiceline->total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $total_ttc) : (float) $total_ttc); // For credit note and if qty is negative, total is negative
2344
2345 $supplierinvoiceline->fk_product = $fk_product;
2346 $supplierinvoiceline->product_type = $type;
2347 $supplierinvoiceline->remise_percent = $remise_percent;
2348 $supplierinvoiceline->date_start = $date_start;
2349 $supplierinvoiceline->date_end = $date_end;
2350 $supplierinvoiceline->fk_code_ventilation = $fk_code_ventilation;
2351 $supplierinvoiceline->rang = $rang;
2352 $supplierinvoiceline->info_bits = $info_bits;
2353 $supplierinvoiceline->fk_remise_except = $fk_remise_except;
2354
2355
2356 $supplierinvoiceline->special_code = (int) $special_code;
2357 $supplierinvoiceline->fk_parent_line = $fk_parent_line;
2358 $supplierinvoiceline->origin = $this->origin;
2359 $supplierinvoiceline->origin_type = $origin_type;
2360 $supplierinvoiceline->origin_id = $origin_id;
2361 $supplierinvoiceline->fk_unit = $fk_unit;
2362
2363 // Multicurrency
2364 $supplierinvoiceline->fk_multicurrency = $this->fk_multicurrency;
2365 $supplierinvoiceline->multicurrency_code = $this->multicurrency_code;
2366 $supplierinvoiceline->multicurrency_subprice = ($apply_abs_price_on_credit_note ? -abs((float) $pu_ht_devise) : (float) $pu_ht_devise); // For credit note, unit price always negative, always positive otherwise
2367
2368 $supplierinvoiceline->multicurrency_total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $multicurrency_total_ht) : (float) $multicurrency_total_ht); // For credit note and if qty is negative, total is negative
2369 $supplierinvoiceline->multicurrency_total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $multicurrency_total_tva) : (float) $multicurrency_total_tva); // For credit note and if qty is negative, total is negative
2370 $supplierinvoiceline->multicurrency_total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $multicurrency_total_ttc) : (float) $multicurrency_total_ttc); // For credit note and if qty is negative, total is negative
2371
2372 if (is_array($array_options) && count($array_options) > 0) {
2373 $supplierinvoiceline->array_options = $array_options;
2374 }
2375
2376 $result = $supplierinvoiceline->insert($notrigger);
2377 if ($result > 0) {
2378 // Update denormalized fields at the order level
2379 $result = $this->update_price(1, 'auto', 0, $this->thirdparty); // The addline method is designed to add line from user input so total calculation with update_price must be done using 'auto' mode.
2380
2381 if ($result > 0) {
2382 if (!isset($this->context['createfromclone'])) {
2383 if (!empty($fk_parent_line)) {
2384 // Always reorder if child line
2385 $this->line_order(true, 'DESC');
2386 } elseif ($rang > 0 && $rang <= count($this->lines)) {
2387 // Update all rank of all other lines starting from the same $ranktouse
2388 $linecount = count($this->lines);
2389 for ($ii = $rang; $ii <= $linecount; $ii++) {
2390 $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1);
2391 }
2392 }
2393
2394 $this->lines[] = $supplierinvoiceline;
2395 }
2396
2397 $this->db->commit();
2398 return $supplierinvoiceline->id;
2399 } else {
2400 $this->error = $this->db->error();
2401 $this->db->rollback();
2402 return -1;
2403 }
2404 } else {
2405 $this->error = $supplierinvoiceline->error;
2406 $this->errors = $supplierinvoiceline->errors;
2407 $this->db->rollback();
2408 return -2;
2409 }
2410 } else {
2411 return 0;
2412 }
2413 }
2414
2440 public function updateline($id, $desc, $pu, $vatrate, $txlocaltax1 = 0, $txlocaltax2 = 0, $qty = 1, $idproduct = 0, $price_base_type = 'HT', $info_bits = 0, $type = 0, $remise_percent = 0, $notrigger = 0, $date_start = '', $date_end = '', $array_options = [], $fk_unit = null, $pu_devise = 0, $ref_supplier = '', $rang = 0)
2441 {
2442 global $mysoc, $langs;
2443
2444 if (!$this->isLineOfObject($id)) {
2445 $this->error = 'ErrorLineIDDoesNotMatchWithObjectID';
2446 return -1;
2447 }
2448
2449 dol_syslog(get_class($this)."::updateline $id,$desc,$pu,$vatrate,$qty,$idproduct,$price_base_type,$info_bits,$type,$remise_percent,$notrigger,$date_start,$date_end,$fk_unit,$pu_devise,$ref_supplier", LOG_DEBUG);
2450 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2451
2452 $pu = price2num($pu);
2453 $qty = price2num($qty);
2454 $remise_percent = (float) price2num($remise_percent);
2455 $pu_devise = price2num($pu_devise);
2456
2457 // Check parameters
2458 //if (! is_numeric($pu) || ! is_numeric($qty)) return -1;
2459 if ($type < 0) {
2460 return -1;
2461 }
2462
2463 if ($date_start && $date_end && $date_start > $date_end) {
2464 $langs->load("errors");
2465 $this->error = $langs->trans('ErrorStartDateGreaterEnd');
2466 return -1;
2467 }
2468
2469 // Clean parameters
2470 if (empty($vatrate)) {
2471 $vatrate = 0;
2472 }
2473 if (empty($txlocaltax1)) {
2474 $txlocaltax1 = 0;
2475 }
2476 if (empty($txlocaltax2)) {
2477 $txlocaltax2 = 0;
2478 }
2479
2480 $txlocaltax1 = (float) price2num($txlocaltax1);
2481 $txlocaltax2 = (float) price2num($txlocaltax2);
2482
2483 // Calculation of the gross total (TTC) and VAT for the line from qty, pu, remise_percent and txtva
2484 // VERY IMPORTANT: It's at the time of line insertion that we must store the net, VAT, and gross amounts,
2485 // and this is done at the line level, which has its own VAT rate
2486
2487 $localtaxes_type = getLocalTaxesFromRate($vatrate, 0, $mysoc, $this->thirdparty);
2488
2489 $reg = array();
2490
2491 // Clean vat code
2492 $vat_src_code = '';
2493 if (preg_match('/\‍((.*)\‍)/', (string) $vatrate, $reg)) {
2494 $vat_src_code = $reg[1];
2495 $vatrate = preg_replace('/\s*\‍(.*\‍)/', '', (string) $vatrate); // Remove code into vatrate.
2496 }
2497
2498 $tabprice = calcul_price_total((float) $qty, (float) $pu, $remise_percent, $vatrate, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, (float) $pu_devise);
2499 $total_ht = $tabprice[0];
2500 $total_tva = $tabprice[1];
2501 $total_ttc = $tabprice[2];
2502 $pu_ht = $tabprice[3];
2503 $pu_tva = $tabprice[4];
2504 $pu_ttc = $tabprice[5];
2505 $total_localtax1 = $tabprice[9];
2506 $total_localtax2 = $tabprice[10];
2507
2508 // MultiCurrency
2509 $multicurrency_total_ht = $tabprice[16];
2510 $multicurrency_total_tva = $tabprice[17];
2511 $multicurrency_total_ttc = $tabprice[18];
2512 $pu_ht_devise = $tabprice[19];
2513
2514 if (empty($info_bits)) {
2515 $info_bits = 0;
2516 }
2517
2518 // Fetch current line from the database and then clone the object and set it in $oldline property
2519 $line = new SupplierInvoiceLine($this->db);
2520 $line->fetch($id);
2521 $line->fetch_optionals();
2522
2523 $staticline = clone $line;
2524
2525 if ($idproduct) {
2526 $product = new Product($this->db);
2527 $result = $product->fetch($idproduct);
2528 $product_type = $product->type;
2529 } else {
2530 $idproduct = $staticline->fk_product;
2531 $product_type = $type;
2532 }
2533
2534 $line->oldline = $staticline;
2535 $line->context = $this->context;
2536
2537 // Same gate as on the customer invoice side: the -abs() forcing on credit note lines is relaxed only when
2538 // both FACTURE_ENABLE_NEGATIVE_LINES and INVOICE_KEEP_DISCOUNT_LINES_AS_IN_ORIGIN are enabled.
2539 $apply_abs_price_on_credit_note = false;
2540 if ($this->type == self::TYPE_CREDIT_NOTE && !(getDolGlobalInt('FACTURE_ENABLE_NEGATIVE_LINES') && getDolGlobalInt('INVOICE_KEEP_DISCOUNT_LINES_AS_IN_ORIGIN'))) {
2541 $apply_abs_price_on_credit_note = true;
2542 }
2543
2544 $line->description = $desc;
2545 $line->desc = $desc;
2546
2547 $line->qty = ($this->type == self::TYPE_CREDIT_NOTE ? abs((float) $qty) : (float) $qty); // For credit note, quantity is always positive and unit price negative
2548
2549 $line->subprice = ($apply_abs_price_on_credit_note ? -abs((float) $pu_ht) : (float) $pu_ht); // For credit note, unit price always negative, always positive otherwise
2550 $line->pu_ht = $line->subprice; // deprecated
2551 $line->subprice_ttc = ($apply_abs_price_on_credit_note ? -abs((float) $pu_ttc) : (float) $pu_ttc); // For credit note, unit price always negative, always positive otherwise
2552 $line->pu_ttc = $line->subprice_ttc; // deprecated
2553
2554 $line->remise_percent = $remise_percent;
2555 $line->ref_supplier = $ref_supplier;
2556
2557 $line->date_start = $date_start;
2558 $line->date_end = $date_end;
2559
2560 $line->vat_src_code = $vat_src_code;
2561 $line->tva_tx = $vatrate;
2562 $line->localtax1_tx = $txlocaltax1;
2563 $line->localtax2_tx = $txlocaltax2;
2564 $line->localtax1_type = empty($localtaxes_type[0]) ? 0 : $localtaxes_type[0];
2565 $line->localtax2_type = empty($localtaxes_type[2]) ? 0 : $localtaxes_type[2];
2566
2567 $line->total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $total_ht) : (float) $total_ht);
2568 $line->total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $total_tva) : (float) $total_tva);
2569 $line->total_localtax1 = (float) $total_localtax1;
2570 $line->total_localtax2 = (float) $total_localtax2;
2571 $line->total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((float) $total_ttc) : (float) $total_ttc);
2572
2573 $line->fk_product = $idproduct;
2574 $line->product_type = $product_type;
2575 $line->info_bits = $info_bits;
2576 $line->fk_unit = $fk_unit;
2577 $line->rang = $rang;
2578
2579 if (is_array($array_options) && count($array_options) > 0) {
2580 // We replace values in this->line->array_options only for entries defined into $array_options
2581 foreach ($array_options as $key => $value) {
2582 $line->array_options[$key] = $array_options[$key];
2583 }
2584 }
2585
2586 // Multicurrency
2587 $line->multicurrency_subprice = (float) $pu_ht_devise;
2588 $line->multicurrency_total_ht = (float) $multicurrency_total_ht;
2589 $line->multicurrency_total_tva = (float) $multicurrency_total_tva;
2590 $line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
2591
2592 $res = $line->update($notrigger);
2593
2594 if ($res < 1) {
2595 $this->setErrorsFromObject($line);
2596 } else {
2597 // Update total price into invoice record
2598 $res = $this->update_price(1, 'auto', 0, $this->thirdparty);
2599 }
2600
2601 return $res;
2602 }
2603
2611 public function deleteLine($rowid, $notrigger = 0)
2612 {
2613 if (!$rowid) {
2614 $rowid = $this->id;
2615 }
2616
2617 $line = new SupplierInvoiceLine($this->db);
2618
2619 if ($line->fetch($rowid) < 1) {
2620 return -1;
2621 }
2622 if ($this->id > 0 && (int) $line->fk_facture_fourn !== (int) $this->id) {
2623 $this->error = 'ErrorLineIDDoesNotMatchWithObjectID';
2624 return -1;
2625 }
2626
2627 $this->db->begin();
2628
2629 // Free the discount linked to a line of invoice
2630 $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except';
2631 $sql .= ' SET fk_invoice_supplier_line = NULL';
2632 $sql .= ' WHERE fk_invoice_supplier_line = '.((int) $rowid);
2633
2634 dol_syslog(get_class($this)."::deleteline", LOG_DEBUG);
2635 $result = $this->db->query($sql);
2636 if (!$result) {
2637 $this->error = $this->db->error();
2638 $this->db->rollback();
2639 return -2;
2640 }
2641
2642 $res = $line->delete($notrigger);
2643
2644 if ($res < 1) {
2645 $this->errors[] = $line->error;
2646 $this->db->rollback();
2647 return -3;
2648 } else {
2649 $res = $this->update_price(1);
2650
2651 if ($res > 0) {
2652 $this->db->commit();
2653 return 1;
2654 } else {
2655 $this->db->rollback();
2656 $this->error = $this->db->lasterror();
2657 return -4;
2658 }
2659 }
2660 }
2661
2662
2669 public function info($id)
2670 {
2671 $sql = 'SELECT c.rowid, datec, tms as datem, ';
2672 $sql .= ' fk_user_author, fk_user_modif, fk_user_valid';
2673 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as c';
2674 $sql .= ' WHERE c.rowid = '.((int) $id);
2675
2676 $result = $this->db->query($sql);
2677 if ($result) {
2678 if ($this->db->num_rows($result)) {
2679 $obj = $this->db->fetch_object($result);
2680
2681 $this->id = $obj->rowid;
2682
2683 $this->user_creation_id = $obj->fk_user_author;
2684 $this->user_validation_id = $obj->fk_user_valid;
2685 $this->user_modification_id = $obj->fk_user_modif;
2686 $this->date_creation = $this->db->jdate($obj->datec);
2687 $this->date_modification = $this->db->jdate($obj->datem);
2688 //$this->date_validation = $obj->datev; // This field is not available. Should be store into log table and using this function should be replaced with showing content of log (like for supplier orders)
2689 }
2690 $this->db->free($result);
2691 } else {
2692 dol_print_error($this->db);
2693 }
2694 }
2695
2696 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2705 public function list_replacable_supplier_invoices($socid = 0)
2706 {
2707 // phpcs:enable
2708 global $conf;
2709
2710 $return = array();
2711
2712 $sql = "SELECT f.rowid as rowid, f.ref, f.fk_statut,";
2713 $sql .= " ff.rowid as rowidnext";
2714 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
2715 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2716 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as ff ON f.rowid = ff.fk_facture_source";
2717 $sql .= " WHERE (f.fk_statut = ".self::STATUS_VALIDATED." OR (f.fk_statut = ".self::STATUS_ABANDONED." AND f.close_code = '".self::CLOSECODE_ABANDONED."'))";
2718 $sql .= " AND f.entity = ".((int) $conf->entity);
2719 $sql .= " AND f.paye = 0"; // Not closed completely
2720 $sql .= " AND pf.fk_paiementfourn IS NULL"; // No payment already done
2721 $sql .= " AND ff.fk_statut IS NULL"; // Return true (is null) if it is not a replacing invoice (we can't replace a replacing invoice)
2722 if ($socid > 0) {
2723 $sql .= " AND f.fk_soc = ".((int) $socid);
2724 }
2725 $sql .= " ORDER BY f.ref";
2726
2727 dol_syslog(get_class($this)."::list_replacable_supplier_invoices", LOG_DEBUG);
2728 $resql = $this->db->query($sql);
2729 if ($resql) {
2730 while ($obj = $this->db->fetch_object($resql)) {
2731 $return[$obj->rowid] = array(
2732 'id' => $obj->rowid,
2733 'ref' => $obj->ref,
2734 'status' => $obj->fk_statut
2735 );
2736 }
2737 //print_r($return);
2738 return $return;
2739 } else {
2740 $this->error = $this->db->error();
2741 return -1;
2742 }
2743 }
2744
2745 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2754 public function list_qualified_avoir_supplier_invoices($socid = 0)
2755 {
2756 // phpcs:enable
2757 global $conf;
2758
2759 $return = array();
2760
2761 $sql = "SELECT f.rowid as rowid, f.ref, f.fk_statut, f.type, f.subtype, f.paye as paid, pf.fk_paiementfourn";
2762 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
2763 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2764 $sql .= " WHERE f.entity = ".((int) $conf->entity);
2765 $sql .= " AND f.fk_statut in (".self::STATUS_VALIDATED.",".self::STATUS_CLOSED.")";
2766 $sql .= " AND NOT EXISTS (SELECT rowid from ".MAIN_DB_PREFIX."facture_fourn as ff WHERE f.rowid = ff.fk_facture_source";
2767 $sql .= " AND ff.type=".self::TYPE_REPLACEMENT.")";
2768 $sql .= " AND f.type != ".self::TYPE_CREDIT_NOTE; // Type non 2 si facture non avoir
2769 if ($socid > 0) {
2770 $sql .= " AND f.fk_soc = ".((int) $socid);
2771 }
2772 $sql .= " ORDER BY f.ref";
2773
2774 dol_syslog(get_class($this)."::list_qualified_avoir_supplier_invoices", LOG_DEBUG);
2775 $resql = $this->db->query($sql);
2776 if ($resql) {
2777 while ($obj = $this->db->fetch_object($resql)) {
2778 $qualified = 0;
2779 if ($obj->fk_statut == self::STATUS_VALIDATED) {
2780 $qualified = 1;
2781 }
2782 if ($obj->fk_statut == self::STATUS_CLOSED) {
2783 $qualified = 1;
2784 }
2785 if ($qualified) {
2786 $paymentornot = ($obj->fk_paiementfourn ? 1 : 0);
2787 $return[$obj->rowid] = array('ref' => (string) $obj->ref, 'status' => (int) $obj->fk_statut, 'type' => (int) $obj->type, 'paid' => (int) $obj->paid, 'paye' => (int) $obj->paid, 'paymentornot' => (int) $paymentornot);
2788 }
2789 }
2790
2791 return $return;
2792 } else {
2793 $this->error = $this->db->error();
2794 return -1;
2795 }
2796 }
2797
2798 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2805 public function load_board($user)
2806 {
2807 // phpcs:enable
2808 global $conf, $langs;
2809
2810 $sql = 'SELECT ff.rowid, ff.date_lim_reglement as datefin, ff.fk_statut as status, ff.total_ht, ff.total_ttc';
2811 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as ff';
2812 if (empty($user->socid) && !$user->hasRight("societe", "client", "voir")) {
2813 $sql .= " JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON ff.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2814 }
2815 $sql .= ' WHERE ff.paye = 0';
2816 $sql .= " AND ff.fk_statut IN (".self::STATUS_VALIDATED.")";
2817 $sql .= " AND ff.entity = ".((int) $conf->entity);
2818 if ($user->socid) {
2819 $sql .= ' AND ff.fk_soc = '.((int) $user->socid);
2820 }
2821
2822 $resql = $this->db->query($sql);
2823 if ($resql) {
2824 $langs->load("bills");
2825 $now = dol_now();
2826
2827 $response = new WorkboardResponse();
2828 $response->warning_delay = $conf->facture->fournisseur->warning_delay / 60 / 60 / 24;
2829 $response->label = $langs->trans("SupplierBillsToPay");
2830 $response->labelShort = $langs->trans("StatusToPay");
2831
2832 $response->url = DOL_URL_ROOT.'/fourn/facture/list.php?search_status=1&mainmenu=billing&leftmenu=suppliers_bills';
2833 $response->img = img_object($langs->trans("Bills"), "bill");
2834
2835 $facturestatic = new FactureFournisseur($this->db);
2836
2837 while ($obj = $this->db->fetch_object($resql)) {
2838 $facturestatic->date_echeance = $this->db->jdate($obj->datefin);
2839 $facturestatic->statut = $obj->status; // For backward compatibility
2840 $facturestatic->status = $obj->status;
2841
2842 $response->nbtodo++;
2843 $response->total += (float) $obj->total_ht;
2844
2845 if ($facturestatic->hasDelay()) {
2846 $response->nbtodolate++;
2847 $response->url_late = DOL_URL_ROOT.'/fourn/facture/list.php?search_option=late&mainmenu=billing&leftmenu=suppliers_bills';
2848 }
2849 }
2850
2851 $this->db->free($resql);
2852 return $response;
2853 } else {
2854 dol_print_error($this->db);
2855 $this->error = $this->db->error();
2856 return -1;
2857 }
2858 }
2859
2867 public function getTooltipContentArray($params)
2868 {
2869 global $conf, $langs, $mysoc;
2870
2871 $langs->load('bills');
2872
2873 $datas = [];
2874 $moretitle = $params['moretitle'] ?? '';
2875
2876 $picto = $this->picto;
2877 if ($this->type == self::TYPE_REPLACEMENT) {
2878 $picto .= 'r'; // Replacement invoice
2879 }
2880 if ($this->type == self::TYPE_CREDIT_NOTE) {
2881 $picto .= 'a'; // Credit note
2882 }
2883 if ($this->type == self::TYPE_DEPOSIT) {
2884 $picto .= 'd'; // Deposit invoice
2885 }
2886
2887 $datas['picto'] = img_picto('', $picto).' <u class="paddingrightonly">'.$langs->trans("SupplierInvoice").'</u>';
2888 if ($this->type == self::TYPE_REPLACEMENT) {
2889 $datas['picto'] .= '<u class="paddingrightonly">'.$langs->transnoentitiesnoconv("InvoiceReplacement").'</u>';
2890 } elseif ($this->type == self::TYPE_CREDIT_NOTE) {
2891 $datas['picto'] .= '<u class="paddingrightonly">'.$langs->transnoentitiesnoconv("CreditNote").'</u>';
2892 } elseif ($this->type == self::TYPE_DEPOSIT) {
2893 $datas['picto'] .= '<u class="paddingrightonly">'.$langs->transnoentitiesnoconv("Deposit").'</u>';
2894 }
2895 if (isset($this->status)) {
2896 $alreadypaid = -1;
2897 if (isset($this->totalpaid)) {
2898 $alreadypaid = $this->totalpaid;
2899 }
2900
2901 $datas['picto'] .= ' '.$this->getLibStatut(5, $alreadypaid);
2902 }
2903 if ($moretitle) {
2904 $datas['picto'] .= ' - '.$moretitle;
2905 }
2906 if (!empty($this->ref)) {
2907 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
2908 }
2909 if (!empty($this->ref_supplier)) {
2910 $datas['refsupplier'] = '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
2911 }
2912 if (!empty($this->label)) {
2913 $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
2914 }
2915 if (!empty($this->date)) {
2916 $datas['date'] = '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
2917 }
2918 if (!empty($this->date_echeance)) {
2919 $datas['date_echeance'] = '<br><b>'.$langs->trans('DateDue').':</b> '.dol_print_date($this->date_echeance, 'day');
2920 }
2921 if (!empty($this->total_ht)) {
2922 $datas['amountht'] = '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2923 }
2924 if (!empty($this->total_tva)) {
2925 $datas['totaltva'] = '<br><b>'.$langs->trans('AmountVAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2926 }
2927 if (!empty($this->total_localtax1) && $this->total_localtax1 != 0) {
2928 // We keep test != 0 because $this->total_localtax1 can be '0.00000000'
2929 $datas['amountlt1'] = '<br><b>'.$langs->transcountry('AmountLT1', $mysoc->country_code).':</b> '.price($this->total_localtax1, 0, $langs, 0, -1, -1, $conf->currency);
2930 }
2931 if (!empty($this->total_localtax2) && $this->total_localtax2 != 0) {
2932 $datas['amountlt2'] = '<br><b>'.$langs->transcountry('AmountLT2', $mysoc->country_code).':</b> '.price($this->total_localtax2, 0, $langs, 0, -1, -1, $conf->currency);
2933 }
2934 if (!empty($this->revenuestamp)) {
2935 $datas['amountrevenustamp'] = '<br><b>'.$langs->trans('RevenueStamp').':</b> '.price($this->revenuestamp, 0, $langs, 0, -1, -1, $conf->currency);
2936 }
2937 if (!empty($this->total_ttc)) {
2938 $datas['totalttc'] = '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2939 }
2940 return $datas;
2941 }
2942
2956 public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
2957 {
2958 global $langs, $user, $hookmanager;
2959
2960 $result = '';
2961
2962 if ($option == 'withdraw') {
2963 $url = DOL_URL_ROOT.'/compta/facture/prelevement.php?facid='.$this->id.'&type=bank-transfer';
2964 } elseif ($option == 'document') {
2965 $url = DOL_URL_ROOT.'/fourn/facture/document.php?facid='.$this->id;
2966 } else {
2967 $url = DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$this->id;
2968 }
2969
2970 if ($short) {
2971 return $url;
2972 }
2973
2974 if ($option !== 'nolink') {
2975 // Add param to save lastsearch_values or not
2976 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2977 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
2978 $add_save_lastsearch_values = 1;
2979 }
2980 if ($add_save_lastsearch_values) {
2981 $url .= '&save_lastsearch_values=1';
2982 }
2983 }
2984
2985 $picto = $this->picto;
2986 if ($this->type == self::TYPE_REPLACEMENT) {
2987 $picto .= 'r'; // Replacement invoice
2988 }
2989 if ($this->type == self::TYPE_CREDIT_NOTE) {
2990 $picto .= 'a'; // Credit note
2991 }
2992 if ($this->type == self::TYPE_DEPOSIT) {
2993 $picto .= 'd'; // Deposit invoice
2994 }
2995
2996 $params = [
2997 'id' => $this->id,
2998 'objecttype' => $this->element,
2999 'option' => $option,
3000 'moretitle' => $moretitle,
3001 ];
3002 $classfortooltip = 'classfortooltip';
3003 $dataparams = '';
3004 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
3005 $classfortooltip = 'classforajaxtooltip';
3006 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
3007 $label = '';
3008 } else {
3009 $label = implode($this->getTooltipContentArray($params));
3010 }
3011
3012 $ref = $this->ref;
3013 if (empty($ref)) {
3014 $ref = $this->id;
3015 }
3016
3017 $linkclose = '';
3018 if (empty($notooltip)) {
3019 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
3020 $label = $langs->trans("ShowSupplierInvoice");
3021 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
3022 }
3023 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
3024 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
3025 }
3026
3027 $linkstart = '<a href="'.$url.'"';
3028 $linkstart .= $linkclose.'>';
3029 $linkend = '</a>';
3030
3031 $result .= $linkstart;
3032 if ($withpicto) {
3033 $result .= img_object(($notooltip ? '' : $label), ($picto ? $picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
3034 }
3035 if ($withpicto != 2) {
3036 $result .= ($max ? dol_trunc($ref, $max) : $ref);
3037 }
3038 $result .= $linkend;
3039
3040 if ($addlinktonotes) {
3041 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3042 if ($txttoshow) {
3043 $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($txttoshow, 1);
3044 $result .= ' <span class="note inline-block">';
3045 $result .= '<a href="'.DOL_URL_ROOT.'/fourn/facture/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
3046 $result .= img_picto('', 'note');
3047 $result .= '</a>';
3048 $result .= '</span>';
3049 }
3050 }
3051 global $action;
3052 $hookmanager->initHooks(array($this->element . 'dao'));
3053 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
3054 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
3055 if ($reshook > 0) {
3056 $result = $hookmanager->resPrint;
3057 } else {
3058 $result .= $hookmanager->resPrint;
3059 }
3060 return $result;
3061 }
3062
3073 public function setCategories($categories)
3074 {
3075 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
3076 return parent::setCategoriesCommon($categories, Categorie::TYPE_SUPPLIER_INVOICE);
3077 }
3078
3087 public function getNextNumRef($soc, $mode = 'next')
3088 {
3089 global $db, $langs, $conf;
3090 $langs->load("orders");
3091
3092 // Clean parameters (if not defined or using deprecated value)
3093 if (!getDolGlobalString('INVOICE_SUPPLIER_ADDON_NUMBER')) {
3094 $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER = 'mod_facture_fournisseur_cactus';
3095 }
3096
3097 $mybool = false;
3098
3099 $file = getDolGlobalString('INVOICE_SUPPLIER_ADDON_NUMBER') . ".php";
3100 $classname = getDolGlobalString('INVOICE_SUPPLIER_ADDON_NUMBER');
3101
3102 // Include file with class
3103 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
3104
3105 foreach ($dirmodels as $reldir) {
3106 $dir = dol_buildpath($reldir."core/modules/supplier_invoice/");
3107
3108 // Load file with numbering class (if found)
3109 $mybool = ((bool) @include_once $dir.$file) || $mybool;
3110 }
3111
3112 if (!$mybool) {
3113 dol_print_error(null, "Failed to include file ".$file);
3114 return '';
3115 }
3116
3117 $obj = new $classname();
3118 '@phan-var-force ModeleNumRefSuppliersInvoices $obj';
3119 $numref = "";
3120 $numref = $obj->getNextValue($soc, $this, $mode);
3121
3122 if ($numref != "") {
3123 return $numref;
3124 } else {
3125 $this->error = $obj->error;
3126 return -1;
3127 }
3128 }
3129
3130
3139 public function initAsSpecimen($option = '')
3140 {
3141 global $langs, $conf;
3142 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
3143
3144 $now = dol_now();
3145
3146 // Load array of products prodids
3147 $num_prods = 0;
3148 $prodids = array();
3149
3150 $sql = "SELECT rowid";
3151 $sql .= " FROM ".MAIN_DB_PREFIX."product";
3152 $sql .= " WHERE entity IN (".getEntity('product').")";
3153 $sql .= $this->db->plimit(100);
3154
3155 $resql = $this->db->query($sql);
3156 if ($resql) {
3157 $num_prods = $this->db->num_rows($resql);
3158 $i = 0;
3159 while ($i < $num_prods) {
3160 $i++;
3161 $row = $this->db->fetch_row($resql);
3162 $prodids[$i] = $row[0];
3163 }
3164 }
3165
3166 // Initialise parameters
3167 $this->id = 0;
3168 $this->ref = 'SPECIMEN';
3169 $this->ref_supplier = 'SUPPLIER_REF_SPECIMEN';
3170 $this->specimen = 1;
3171 $this->socid = 1;
3172 $this->date = $now;
3173 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
3174 $this->cond_reglement_code = 'RECEP';
3175 $this->mode_reglement_code = 'CHQ';
3176
3177 $this->note_public = 'This is a comment (public)';
3178 $this->note_private = 'This is a comment (private)';
3179
3180 $this->multicurrency_tx = 1;
3181 $this->multicurrency_code = $conf->currency;
3182
3183 $xnbp = 0;
3184 if (empty($option) || $option != 'nolines') {
3185 // Lines
3186 $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)
3187 while ($xnbp < $nbp) {
3188 $line = new SupplierInvoiceLine($this->db);
3189 $line->desc = $langs->trans("Description")." ".$xnbp;
3190 $line->qty = 1;
3191 $line->subprice = 100;
3192 $line->price = 100;
3193 $line->tva_tx = 19.6;
3194 $line->localtax1_tx = 0;
3195 $line->localtax2_tx = 0;
3196 if ($xnbp == 2) {
3197 $line->total_ht = 50;
3198 $line->total_ttc = 59.8;
3199 $line->total_tva = 9.8;
3200 $line->remise_percent = 50;
3201 } else {
3202 $line->total_ht = 100;
3203 $line->total_ttc = 119.6;
3204 $line->total_tva = 19.6;
3205 $line->remise_percent = 0;
3206 }
3207
3208 if ($num_prods > 0) {
3209 $prodid = mt_rand(1, $num_prods);
3210 $line->fk_product = $prodids[$prodid];
3211 }
3212 $line->product_type = 0;
3213
3214 $this->lines[$xnbp] = $line;
3215
3216 $this->total_ht += $line->total_ht;
3217 $this->total_tva += $line->total_tva;
3218 $this->total_ttc += $line->total_ttc;
3219
3220 $xnbp++;
3221 }
3222 }
3223
3224 $this->total_ht = $xnbp * 100;
3225 $this->total_tva = $xnbp * 19.6;
3226 $this->total_ttc = $xnbp * 119.6;
3227
3228 return 1;
3229 }
3230
3236 public function loadStateBoard()
3237 {
3238 global $conf, $user;
3239
3240 $this->nb = array();
3241
3242 $clause = "WHERE";
3243
3244 $sql = "SELECT count(f.rowid) as nb";
3245 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
3246 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
3247 if (empty($user->socid) && !$user->hasRight("societe", "client", "voir")) {
3248 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3249 $sql .= " WHERE sc.fk_user = ".((int) $user->id);
3250 $clause = "AND";
3251 }
3252 $sql .= " ".$clause." f.entity = ".((int) $conf->entity);
3253
3254 $resql = $this->db->query($sql);
3255 if ($resql) {
3256 while ($obj = $this->db->fetch_object($resql)) {
3257 $this->nb["supplier_invoices"] = $obj->nb;
3258 }
3259 $this->db->free($resql);
3260 return 1;
3261 } else {
3262 dol_print_error($this->db);
3263 $this->error = $this->db->error();
3264 return -1;
3265 }
3266 }
3267
3276 public function createFromClone(User $user, $fromid, $invertdetail = 0)
3277 {
3278 global $conf, $langs, $hookmanager;
3279
3280 $error = 0;
3281
3282 $object = new FactureFournisseur($this->db);
3283
3284 $this->db->begin();
3285
3286 // Load source object
3287 $object->fetch($fromid);
3288 $objFrom = clone $object;
3289
3290 $object->id = 0;
3291 $object->statut = self::STATUS_DRAFT; // For backward compatibility
3292 $object->status = self::STATUS_DRAFT;
3293
3294 $object->fetch_thirdparty(); // We need it to recalculate VAT localtaxes according to main sale taxes and vendor
3295
3296 // Clear fields
3297 $object->ref_supplier = (empty($this->ref_supplier) ? $langs->trans("CopyOf").' '.$object->ref_supplier : $this->ref_supplier);
3298 $object->author = $user->id; // FIXME? user_validation_id is replacement for author
3299 $object->user_validation_id = 0; // FIXME? user_validation_id is replacement for author
3300 $object->fk_facture_source = 0;
3301 $object->date_creation = '';
3302 $object->date_validation = '';
3303 $object->date = (empty($this->date) ? dol_now() : $this->date);
3304 $object->ref_client = '';
3305 $object->close_code = '';
3306 $object->close_note = '';
3307 if (getDolGlobalInt('MAIN_DONT_KEEP_NOTE_ON_CLONING') == 1) {
3308 $object->note_private = '';
3309 $object->note_public = '';
3310 }
3311
3312 $object->date_echeance = $object->calculate_date_lim_reglement();
3313
3314 // Loop on each line of new invoice
3315 foreach ($object->lines as $i => $line) {
3316 if (isset($object->lines[$i]->info_bits) && ($object->lines[$i]->info_bits & 0x02) == 0x02) { // We do not clone line of discounts
3317 unset($object->lines[$i]);
3318 }
3319 }
3320
3321 // Create clone
3322 $object->context['createfromclone'] = 'createfromclone';
3323 $result = $object->create($user);
3324
3325 // Other options
3326 if ($result < 0) {
3327 $this->error = $object->error;
3328 $this->errors = $object->errors;
3329 $error++;
3330 }
3331
3332 if (!$error) {
3333 // Hook of thirdparty module
3334 if (is_object($hookmanager)) {
3335 $parameters = array('objFrom' => $objFrom);
3336 $action = '';
3337 $reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
3338 if ($reshook < 0) {
3339 $this->setErrorsFromObject($hookmanager);
3340 $error++;
3341 }
3342 }
3343 }
3344
3345 unset($object->context['createfromclone']);
3346
3347 // End
3348 if (!$error) {
3349 $this->db->commit();
3350 return $object->id;
3351 } else {
3352 $this->db->rollback();
3353 return -1;
3354 }
3355 }
3356
3368 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
3369 {
3370 global $langs;
3371
3372 $langs->load("suppliers");
3373 $outputlangs->load("products");
3374
3375 // Set the model on the model name to use
3376 if (empty($modele)) {
3377 if (getDolGlobalString('INVOICE_SUPPLIER_ADDON_PDF')) {
3378 $modele = getDolGlobalString('INVOICE_SUPPLIER_ADDON_PDF');
3379 } else {
3380 $modele = ''; // No default value. For supplier invoice, we allow to disable all PDF generation
3381 }
3382 } elseif ($modele == 'auto') {
3383 $modele = 'canelle';
3384 }
3385
3386 if (empty($modele)) {
3387 return 0;
3388 } else {
3389 $modelpath = "core/modules/supplier_invoice/doc/";
3390
3391 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3392 }
3393 }
3394
3399 public function getRights()
3400 {
3401 global $user;
3402
3403 return $user->hasRight("fournisseur", "facture");
3404 }
3405
3414 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
3415 {
3416 $tables = array(
3417 'facture_fourn'
3418 );
3419
3420 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
3421 }
3422
3431 public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
3432 {
3433 $tables = array(
3434 'facture_fourn_det'
3435 );
3436
3437 return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
3438 }
3439
3445 public function hasDelay()
3446 {
3447 global $conf;
3448
3449 $now = dol_now();
3450
3451 if (!$this->date_echeance) {
3452 return false;
3453 }
3454
3455 $status = isset($this->status) ? $this->status : $this->statut;
3456
3457 return ($status == self::STATUS_VALIDATED) && ($this->date_echeance < ($now - $conf->facture->fournisseur->warning_delay));
3458 }
3459
3465 public function isCreditNoteUsed()
3466 {
3467 $isUsed = false;
3468
3469 $sql = "SELECT fk_invoice_supplier FROM ".MAIN_DB_PREFIX."societe_remise_except WHERE fk_invoice_supplier_source = ".((int) $this->id);
3470 $resql = $this->db->query($sql);
3471 if (!empty($resql)) {
3472 $obj = $this->db->fetch_object($resql);
3473 if (!empty($obj->fk_invoice_supplier)) {
3474 $isUsed = true;
3475 }
3476 }
3477
3478 return $isUsed;
3479 }
3487 public function getKanbanView($option = '', $arraydata = null)
3488 {
3489 global $langs;
3490
3491 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
3492
3493 $picto = $this->picto;
3494 if ($this->type == self::TYPE_REPLACEMENT) {
3495 $picto .= 'r'; // Replacement invoice
3496 }
3497 if ($this->type == self::TYPE_CREDIT_NOTE) {
3498 $picto .= 'a'; // Credit note
3499 }
3500 if ($this->type == self::TYPE_DEPOSIT) {
3501 $picto .= 'd'; // Deposit invoice
3502 }
3503
3504 $return = '<div class="box-flex-item box-flex-grow-zero">';
3505 $return .= '<div class="info-box info-box-sm">';
3506 $return .= '<span class="info-box-icon bg-infobox-action">';
3507 $return .= img_picto('', $picto);
3508 $return .= '</span>';
3509 $return .= '<div class="info-box-content">';
3510 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . $this->getNomUrl(1) . '</span>';
3511 if ($selected >= 0) {
3512 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
3513 }
3514 if (!empty($arraydata['thirdparty'])) {
3515 $return .= '<br><span class="info-box-label">'.$arraydata['thirdparty'].'</span>';
3516 }
3517 if (!empty($this->date)) {
3518 $return .= '<br><span class="info-box-label">'.dol_print_date($this->date, 'day').'</span>';
3519 }
3520 if (!empty($this->total_ht)) {
3521 $return .= ' &nbsp; <span class="info-box-label amount" title="'.dol_escape_htmltag($langs->trans("AmountHT")).'">'.price($this->total_ht);
3522 $return .= ' '.$langs->trans("HT");
3523 $return .= '</span>';
3524 }
3525 $alreadypaid = (empty($arraydata['alreadypaid']) ? 0 : $arraydata['alreadypaid']);
3526 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3, $alreadypaid).'</div>';
3527
3528 $return .= '</div>';
3529 $return .= '</div>';
3530 $return .= '</div>';
3531
3532 return $return;
3533 }
3534
3541 public function setVATReverseCharge($vatreversecharge)
3542 {
3543 if (!$this->table_element) {
3544 dol_syslog(get_class($this)."::setVATReverseCharge was called on object with property table_element not defined", LOG_ERR);
3545 return -1;
3546 }
3547
3548 dol_syslog(get_class($this).'::setVATReverseCharge('.$vatreversecharge.')');
3549
3550 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
3551 $sql .= " SET vat_reverse_charge = ".((int) $vatreversecharge);
3552 $sql .= " WHERE rowid=".((int) $this->id);
3553
3554 if ($this->db->query($sql)) {
3555 $this->vat_reverse_charge = ($vatreversecharge == 0) ? 0 : 1;
3556 return 1;
3557 } else {
3558 dol_syslog(get_class($this).'::setVATReverseCharge Error ', LOG_DEBUG);
3559 $this->error = $this->db->error();
3560 return 0;
3561 }
3562 }
3563
3575 public function sendEmailsRemindersOnSupplierInvoiceDueDate($nbdays = 0, $paymentmode = 'all', $template = '', $datetouse = 'duedate', $forcerecipient = '')
3576 {
3577 global $conf, $langs, $user;
3578
3579 $this->output = '';
3580 $this->error = '';
3581 $nbMailSend = 0;
3582
3583 $error = 0;
3584 $errorsMsg = array();
3585
3586 $langs->load('bills');
3587
3588 if (!isModEnabled(!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') ? 'fournisseur' : 'supplier_invoice')) { // Should not happen. If module disabled, cron job should not be visible.
3589 $this->output .= $langs->trans('ModuleNotEnabled', $langs->transnoentitiesnoconv('Suppliers'));
3590 return 0;
3591 }
3592 if (!in_array($datetouse, array('duedate', 'invoicedate'))) {
3593 $this->output .= 'Bad value for parameter datetouse. Must be "duedate" or "invoicedate"';
3594 return 0;
3595 }
3596
3597 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
3598 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
3599 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
3600 $formmail = new FormMail($this->db);
3601
3602 $now = dol_now();
3603 $tmpidate = dol_get_first_hour(dol_time_plus_duree($now, $nbdays, 'd'), 'gmt');
3604
3605 $tmpinvoice = new FactureFournisseur($this->db);
3606
3607 dol_syslog(__METHOD__." start", LOG_INFO);
3608
3609 // Select all action comm reminder
3610 $sql = "SELECT rowid as id FROM ".MAIN_DB_PREFIX."facture_fourn as f";
3611 if (!empty($paymentmode) && $paymentmode != 'all') {
3612 $sql .= ", ".MAIN_DB_PREFIX."c_paiement as cp";
3613 }
3614 $sql .= " WHERE f.paye = 0"; // Only unpaid
3615 $sql .= " AND f.fk_statut = ".self::STATUS_VALIDATED; // Only validated status
3616 if ($datetouse == 'invoicedate') {
3617 $sql .= " AND f.datef = '".$this->db->idate($tmpidate, 'gmt')."'";
3618 } else {
3619 $sql .= " AND f.date_lim_reglement = '".$this->db->idate($tmpidate, 'gmt')."'";
3620 }
3621 $sql .= " AND f.entity IN (".getEntity('supplier_invoice', 0).")"; // One batch process only one company (no sharing)
3622 if (!empty($paymentmode) && $paymentmode != 'all') {
3623 $sql .= " AND f.fk_mode_reglement = cp.id AND cp.code = '".$this->db->escape($paymentmode)."'";
3624 }
3625 // TODO Add a filter to check there is no payment started yet
3626 if ($datetouse == 'invoicedate') {
3627 $sql .= $this->db->order("datef", "ASC");
3628 } else {
3629 $sql .= $this->db->order("date_lim_reglement", "ASC");
3630 }
3631
3632 $resql = $this->db->query($sql);
3633
3634 $stmpidate = dol_print_date($tmpidate, 'day', 'gmt');
3635 if ($datetouse == 'invoicedate') {
3636 $this->output .= $langs->transnoentitiesnoconv("SearchValidatedSupplierInvoicesWithDate", $stmpidate);
3637 } else {
3638 $this->output .= $langs->transnoentitiesnoconv("SearchUnpaidSupplierInvoicesWithDueDate", $stmpidate);
3639 }
3640 if (!empty($paymentmode) && $paymentmode != 'all') {
3641 $this->output .= ' ('.$langs->transnoentitiesnoconv("PaymentMode").' '.$paymentmode.')';
3642 }
3643 $this->output .= '<br>';
3644
3645 if ($resql) {
3646 while ($obj = $this->db->fetch_object($resql)) {
3647 if (!$error) {
3648 // Load event
3649 $res = $tmpinvoice->fetch($obj->id);
3650 if ($res > 0) {
3651 $tmpinvoice->fetch_thirdparty();
3652
3653 $outputlangs = new Translate('', $conf);
3654 if ($tmpinvoice->thirdparty->default_lang) {
3655 $outputlangs->setDefaultLang($tmpinvoice->thirdparty->default_lang);
3656 $outputlangs->loadLangs(array("main", "suppliers"));
3657 } else {
3658 $outputlangs = $langs;
3659 }
3660
3661 // Select email template according to language of recipient
3662 $templateId = 0;
3663 $templateLabel = '';
3664 if (empty($template) || $template == 'EmailTemplateCode') {
3665 $templateLabel = '(SendingReminderEmailOnUnpaidSupplierInvoice)';
3666 } else {
3667 if (is_numeric($template)) {
3668 $templateId = $template;
3669 } else {
3670 $templateLabel = $template;
3671 }
3672 }
3673
3674 $arraymessage = $formmail->getEMailTemplate($this->db, 'invoice_supplier_send', $user, $outputlangs, $templateId, 1, $templateLabel);
3675 if (is_numeric($arraymessage) && $arraymessage <= 0) {
3676 $langs->load("errors");
3677 $this->output .= $langs->trans('ErrorFailedToFindEmailTemplate', $template);
3678 return 0;
3679 }
3680
3681 // PREPARE EMAIL
3682 $errormesg = '';
3683
3684 // Make substitution in email content
3685 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $tmpinvoice);
3686
3687 complete_substitutions_array($substitutionarray, $outputlangs, $tmpinvoice);
3688
3689 // Topic
3690 $sendTopic = make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv('InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
3691
3692 // Content
3693 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
3694
3695 $sendContent = make_substitutions($content, $substitutionarray, $outputlangs, 1);
3696
3697 // Recipient
3698 $to = array();
3699 if ($forcerecipient) { // If a recipient was forced
3700 $to = array($forcerecipient);
3701 } else {
3702 $res = $tmpinvoice->fetch_thirdparty();
3703 $recipient = $tmpinvoice->thirdparty;
3704 if ($res > 0) {
3705 $tmparraycontact = $tmpinvoice->liste_contact(-1, 'internal', 0, 'SALESREPFOLL');
3706 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
3707 foreach ($tmparraycontact as $data_email) {
3708 if (!empty($data_email['email'])) {
3709 $to[] = $data_email['email'];
3710 }
3711 }
3712 }
3713 if (empty($to) && !empty($recipient->email)) {
3714 $to[] = $recipient->email;
3715 }
3716 if (empty($to)) {
3717 $errormesg = "Failed to send remind to thirdparty id=".$tmpinvoice->socid.". No email defined for supplier invoice or customer.";
3718 $error++;
3719 }
3720 } else {
3721 $errormesg = "Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
3722 $error++;
3723 }
3724 }
3725
3726 // Sender
3727 $email_from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
3728 if (!empty($arraymessage->email_from)) { // If a sender is defined into template, we use it in priority
3729 $email_from = (string) $arraymessage->email_from;
3730 }
3731 if (empty($email_from)) {
3732 $errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
3733 $error++;
3734 }
3735
3736 if (!$error && !empty($to)) {
3737 $this->db->begin();
3738
3739 $to = implode(',', $to);
3740 if (!empty($arraymessage->email_to)) { // If a recipient is defined into template, we add it
3741 $to = $to.','.$arraymessage->email_to;
3742 }
3743
3744 // Errors Recipient
3745 $errors_to = getDolGlobalString('MAIN_MAIL_ERRORS_TO');
3746
3747 $trackid = 'inv'.$tmpinvoice->id;
3748 $sendcontext = 'standard';
3749
3750 $email_tocc = '';
3751 if (!empty($arraymessage->email_tocc)) { // If a CC is defined into template, we use it
3752 $email_tocc = (string) $arraymessage->email_tocc;
3753 }
3754
3755 $email_tobcc = '';
3756 if (!empty($arraymessage->email_tobcc)) { // If a BCC is defined into template, we use it
3757 $email_tobcc = (string) $arraymessage->email_tobcc;
3758 }
3759
3760 // Mail Creation
3761 $cMailFile = new CMailFile($sendTopic, $to, $email_from, $sendContent, array(), array(), array(), $email_tocc, $email_tobcc, 0, 1, $errors_to, '', $trackid, '', $sendcontext, '');
3762
3763 // Sending Mail
3764 if ($cMailFile->sendfile()) {
3765 $nbMailSend++;
3766
3767 // Add a line into event table
3768 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
3769
3770 // Insert record of emails sent
3771 $actioncomm = new ActionComm($this->db);
3772
3773 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
3774 $actioncomm->socid = $tmpinvoice->thirdparty->id; // To link to a company
3775 $actioncomm->contact_id = 0;
3776
3777 $actioncomm->code = 'AC_EMAIL';
3778 $actioncomm->label = 'sendEmailsRemindersOnInvoiceDueDateOK (nbdays='.$nbdays.' paymentmode='.$paymentmode.' template='.$template.' datetouse='.$datetouse.' forcerecipient='.$forcerecipient.')';
3779 $actioncomm->note_private = $sendContent;
3780 $actioncomm->fk_project = $tmpinvoice->fk_project;
3781 $actioncomm->datep = dol_now();
3782 $actioncomm->datef = $actioncomm->datep;
3783 $actioncomm->percentage = -1; // Not applicable
3784 $actioncomm->authorid = $user->id; // User saving action
3785 $actioncomm->userownerid = $user->id; // Owner of action
3786 // Fields when action is an email (content should be added into note)
3787 $actioncomm->email_msgid = $cMailFile->msgid;
3788 $actioncomm->email_subject = $sendTopic;
3789 $actioncomm->email_from = $email_from;
3790 $actioncomm->email_sender = '';
3791 $actioncomm->email_to = $to;
3792 //$actioncomm->email_tocc = $sendtocc;
3793 //$actioncomm->email_tobcc = $sendtobcc;
3794 //$actioncomm->email_subject = $subject;
3795 $actioncomm->errors_to = $errors_to;
3796
3797 $actioncomm->elementtype = 'invoice_supplier';
3798 $actioncomm->elementid = $tmpinvoice->id;
3799 $actioncomm->fk_element = $tmpinvoice->id;
3800
3801 //$actioncomm->extraparams = $extraparams;
3802
3803 $actioncomm->create($user);
3804 } else {
3805 $errormesg = $cMailFile->error.' : '.$to;
3806 $error++;
3807
3808 // Add a line into event table
3809 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
3810
3811 // Insert record of emails sent
3812 $actioncomm = new ActionComm($this->db);
3813
3814 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
3815 $actioncomm->socid = $tmpinvoice->thirdparty->id; // To link to a company
3816 $actioncomm->contact_id = 0;
3817
3818 $actioncomm->code = 'AC_EMAIL';
3819 $actioncomm->label = 'sendEmailsRemindersOnInvoiceDueDateKO';
3820 $actioncomm->note_private = $errormesg;
3821 $actioncomm->fk_project = $tmpinvoice->fk_project;
3822 $actioncomm->datep = dol_now();
3823 $actioncomm->datef = $actioncomm->datep;
3824 $actioncomm->percentage = -1; // Not applicable
3825 $actioncomm->authorid = $user->id; // User saving action
3826 $actioncomm->userownerid = $user->id; // Owner of action
3827 // Fields when action is an email (content should be added into note)
3828 $actioncomm->email_msgid = $cMailFile->msgid;
3829 $actioncomm->email_from = $email_from;
3830 $actioncomm->email_sender = '';
3831 $actioncomm->email_to = $to;
3832 //$actioncomm->email_tocc = $sendtocc;
3833 //$actioncomm->email_tobcc = $sendtobcc;
3834 //$actioncomm->email_subject = $subject;
3835 $actioncomm->errors_to = $errors_to;
3836
3837 //$actioncomm->extraparams = $extraparams;
3838
3839 $actioncomm->create($user);
3840 }
3841
3842 $this->db->commit(); // We always commit
3843 }
3844
3845 if ($errormesg) {
3846 $errorsMsg[] = $errormesg;
3847 }
3848 } else {
3849 $errorsMsg[] = 'Failed to fetch record invoice with ID = '.$obj->id;
3850 $error++;
3851 }
3852 }
3853 }
3854 } else {
3855 $error++;
3856 }
3857
3858 if (!$error) {
3859 $this->output .= 'Nb of emails sent : '.$nbMailSend;
3860
3861 dol_syslog(__METHOD__." end - ".$this->output, LOG_INFO);
3862
3863 return 0;
3864 } else {
3865 $this->error = 'Nb of emails sent : '.$nbMailSend.', '.(empty($errorsMsg) ? $error : implode(', ', $errorsMsg));
3866
3867 dol_syslog(__METHOD__." end - ".$this->error, LOG_INFO);
3868
3869 return $error;
3870 }
3871 }
3872}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$object ref
Definition info.php:90
Class to manage agenda events (actions)
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Superclass for invoice classes.
calculate_date_lim_reglement($cond_reglement=0)
Returns an invoice payment deadline based on the invoice settlement conditions and billing date.
is_erasable()
Return if an invoice can be deleted Rule is: If invoice is draft and has a temporary ref -> yes (1) I...
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...
line_order($renum=false, $rowidorder='ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
deleteEcmFiles($mode=0)
Delete related files of object in database.
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.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setErrorsFromObject($object)
setErrorsFromObject
isLineOfObject($lineid)
Check that a line belongs to this object, using $this->table_element_line and $this->fk_element.
updateRangOfLine($rowid, $rang)
Update position of line (rang)
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.
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.
Class to manage absolute discounts.
Class to manage Dolibarr database access.
Class to manage suppliers invoices.
const TYPE_DEPOSIT
Deposit invoice.
create($user)
Create supplier invoice into database.
list_qualified_avoir_supplier_invoices($socid=0)
Return list of qualifying invoices for correction by credit note Invoices that respect the following ...
list_replacable_supplier_invoices($socid=0)
Return list of replaceable invoices Status valid or abandoned for other reason + not paid + no paymen...
deleteLine($rowid, $notrigger=0)
Delete a detail line from database.
set_unpaid($user)
Tag the invoice as not fully paid + trigger call BILL_UNPAYED Function used when a direct debit payme...
setCanceled($user, $close_code='', $close_note='')
Tag invoice as canceled, with no payment on it (example for replacement invoice or payment never rece...
fetch($id=0, $ref='', $ref_ext='')
Load object in memory from database.
setCategories($categories)
Sets object to given categories.
info($id)
Loads the info order information into the invoice object.
const TYPE_CREDIT_NOTE
Credit note invoice.
isCreditNoteUsed()
Is credit note used.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
load_board($user)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0)
Return clickable name (with optional picto)
getTooltipContentArray($params)
getTooltipContentArray
setPaid($user, $close_code='', $close_note='')
Tag invoice as a paid invoice.
update($user=null, $notrigger=0)
Update database.
addline($desc, $pu, $txtva, $txlocaltax1, $txlocaltax2, $qty, $fk_product=0, $remise_percent=0, $date_start=0, $date_end=0, $fk_code_ventilation=0, $info_bits=0, $price_base_type='HT', $type=0, $rang=-1, $notrigger=0, $array_options=[], $fk_unit=null, $origin_id=0, $pu_devise=0, $ref_supplier='', $special_code=0, $fk_parent_line=0, $fk_remise_except=0, $origin_type='')
Adds an invoice line (associated with no predefined product/service) The parameters are already suppo...
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
const TYPE_REPLACEMENT
Replacement invoice.
setUnpaid($user)
Tag the invoice as not fully paid + trigger call BILL_UNPAYED Function used when a direct debit payme...
const STATUS_VALIDATED
Validated (need to be paid)
setDraft($user, $idwarehouse=-1, $notrigger=0)
Set draft status.
getNextNumRef($soc, $mode='next')
Return next reference of supplier invoice not already used (or last reference) according to numbering...
updateline($id, $desc, $pu, $vatrate, $txlocaltax1=0, $txlocaltax2=0, $qty=1, $idproduct=0, $price_base_type='HT', $info_bits=0, $type=0, $remise_percent=0, $notrigger=0, $date_start='', $date_end='', $array_options=[], $fk_unit=null, $pu_devise=0, $ref_supplier='', $rang=0)
Update a line detail in the database.
sendEmailsRemindersOnSupplierInvoiceDueDate($nbdays=0, $paymentmode='all', $template='', $datetouse='duedate', $forcerecipient='')
Send reminders by emails for supplier invoices validated that are due.
insert_discount($idremise)
Add a discount line into an invoice (as an invoice line) using an existing absolute discount (Consume...
initAsSpecimen($option='')
Initialise an instance with random values.
const TYPE_STANDARD
Standard invoice.
validate($user, $force_number='', $idwarehouse=0, $notrigger=0)
Tag invoice as validated + call trigger BILL_VALIDATE.
set_paid($user, $close_code='', $close_note='')
Tag invoice as a paid invoice.
createFromClone(User $user, $fromid, $invertdetail=0)
Load an object from its id and create a new one in database.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template model.
getRights()
Returns the rights used for this class.
const STATUS_ABANDONED
Classified abandoned and no payment done.
hasDelay()
Is the payment of the supplier invoice having a delay?
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
loadStateBoard()
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
const STATUS_CLOSED
Classified paid.
setVATReverseCharge($vatreversecharge)
Change the option VAT reverse charge.
Class to manage invoice templates.
Class to manage a HTML form to send a unitary email Usage: $formail = new FormMail($db) $formmail->pr...
Class to manage "other" html components Only common components are here.
Class to manage stock movements.
static getIdAndTxFromCode($dbs, $code, $date_document=0)
Get id and rate of currency from code.
static getIdFromCode($dbs, $code)
Get id of currency from code.
Class to manage predefined suppliers products.
Class to manage products or services.
Class to manage line invoices.
Class to manage translations.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
global $mysoc
dol_get_first_hour($date, $gm='tzserver')
Return GMT time for first hour of a given GMT date (it removes hours, min and second part)
Definition date.lib.php:680
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0, $tz='')
Add a delay to a date.
Definition date.lib.php:129
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:64
$date_start
Variables from include:
dol_now($mode='gmt')
Return date for now.
setEntity($currentobject)
Set entity id to use when to create an object.
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)
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
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)
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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.
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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.
isValidEmail($address, $acceptsupervisorkey=0, $acceptuserkey=0)
Return true if email syntax is ok.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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...
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $notused, $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
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:492
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:137