42require_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
43require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
44require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.ligne.class.php';
45require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
46require_once DOL_DOCUMENT_ROOT.
'/subtotals/class/commonsubtotal.class.php';
49 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formaccounting.class.php';
50 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
63 public $element =
'invoice_supplier';
68 public $table_element =
'facture_fourn';
73 public $table_element_line =
'facture_fourn_det';
78 public $class_element_line =
'SupplierInvoiceLine';
82 public $fk_element =
'fk_facture_fourn';
87 public $picto =
'supplier_invoice';
93 public $restrictiononfksoc = 1;
113 public $ref_supplier;
173 public $fk_user_valid;
187 public $date_echeance;
192 public $date_pointoftax;
222 public $total_localtax1;
224 public $total_localtax2;
235 public $note_private;
249 public $transport_mode_id;
254 public $vat_reverse_charge;
259 public $extraparams = array();
264 public $lines = array();
276 public $fk_facture_source;
281 public $fk_fac_rec_source;
283 public $fields = array(
284 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
285 'ref' => array(
'type' =>
'varchar(255)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'showoncombobox' => 1,
'position' => 15),
286 'ref_supplier' => array(
'type' =>
'varchar(255)',
'label' =>
'RefSupplier',
'enabled' => 1,
'visible' => -1,
'position' => 20),
287 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 25,
'index' => 1),
288 'ref_ext' => array(
'type' =>
'varchar(255)',
'label' =>
'RefExt',
'enabled' => 1,
'visible' => 0,
'position' => 30),
289 'type' => array(
'type' =>
'smallint(6)',
'label' =>
'Type',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 35),
290 'subtype' => array(
'type' =>
'smallint(6)',
'label' =>
'InvoiceSubtype',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 36),
291 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => -1,
'notnull' => 1,
'position' => 40),
292 'datec' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -1,
'position' => 45),
293 'datef' => array(
'type' =>
'date',
'label' =>
'Date',
'enabled' => 1,
'visible' => -1,
'position' => 50),
294 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 55),
295 'libelle' => array(
'type' =>
'varchar(255)',
'label' =>
'Label',
'enabled' => 1,
'visible' => -1,
'position' => 60),
296 'paye' => array(
'type' =>
'smallint(6)',
'label' =>
'Paye',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 65),
297 'amount' => array(
'type' =>
'double(24,8)',
'label' =>
'Amount',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 70),
298 'remise' => array(
'type' =>
'double(24,8)',
'label' =>
'Discount',
'enabled' => 1,
'visible' => -1,
'position' => 75),
299 'close_code' => array(
'type' =>
'varchar(16)',
'label' =>
'CloseCode',
'enabled' => 1,
'visible' => -1,
'position' => 80),
300 'close_note' => array(
'type' =>
'varchar(128)',
'label' =>
'CloseNote',
'enabled' => 1,
'visible' => -1,
'position' => 85),
301 'tva' => array(
'type' =>
'double(24,8)',
'label' =>
'Tva',
'enabled' => 1,
'visible' => -1,
'position' => 90),
302 'localtax1' => array(
'type' =>
'double(24,8)',
'label' =>
'Localtax1',
'enabled' => 1,
'visible' => -1,
'position' => 95),
303 'localtax2' => array(
'type' =>
'double(24,8)',
'label' =>
'Localtax2',
'enabled' => 1,
'visible' => -1,
'position' => 100),
304 'total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalHT',
'enabled' => 1,
'visible' => -1,
'position' => 105),
305 'total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalVAT',
'enabled' => 1,
'visible' => -1,
'position' => 110),
306 'total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalTTC',
'enabled' => 1,
'visible' => -1,
'position' => 115),
307 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => -1,
'position' => 125),
308 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 130),
309 'fk_user_valid' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserValidation',
'enabled' => 1,
'visible' => -1,
'position' => 135),
310 'fk_facture_source' => array(
'type' =>
'integer',
'label' =>
'SourceInvoice',
'enabled' => 1,
'visible' => -1,
'position' => 140),
311 'fk_projet' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 145),
312 'fk_account' => array(
'type' =>
'integer',
'label' =>
'Account',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 150),
313 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentTerm',
'enabled' => 1,
'visible' => -1,
'position' => 155),
314 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentMode',
'enabled' => 1,
'visible' => -1,
'position' => 160),
315 'date_lim_reglement' => array(
'type' =>
'date',
'label' =>
'DateLimReglement',
'enabled' => 1,
'visible' => -1,
'position' => 165),
316 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 170),
317 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 175),
318 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'ModelPdf',
'enabled' => 1,
'visible' => 0,
'position' => 180),
319 'extraparams' => array(
'type' =>
'varchar(255)',
'label' =>
'Extraparams',
'enabled' => 1,
'visible' => -1,
'position' => 190),
320 'fk_incoterms' => array(
'type' =>
'integer',
'label' =>
'IncotermCode',
'enabled' => 1,
'visible' => -1,
'position' => 195),
321 'location_incoterms' => array(
'type' =>
'varchar(255)',
'label' =>
'IncotermLocation',
'enabled' => 1,
'visible' => -1,
'position' => 200),
322 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'MulticurrencyId',
'enabled' => 1,
'visible' => -1,
'position' => 205),
323 'multicurrency_code' => array(
'type' =>
'varchar(255)',
'label' =>
'MulticurrencyCode',
'enabled' => 1,
'visible' => -1,
'position' => 210),
324 'multicurrency_tx' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyRate',
'enabled' => 1,
'visible' => -1,
'position' => 215),
325 'multicurrency_total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyTotalHT',
'enabled' => 1,
'visible' => -1,
'position' => 220),
326 'multicurrency_total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyTotalVAT',
'enabled' => 1,
'visible' => -1,
'position' => 225),
327 'multicurrency_total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyTotalTTC',
'enabled' => 1,
'visible' => -1,
'position' => 230),
328 'date_pointoftax' => array(
'type' =>
'date',
'label' =>
'DatePointOfTax',
'enabled' =>
'getDolGlobalString("INVOICE_POINTOFTAX_DATE")',
'visible' => -1,
'position' => 235),
329 'date_valid' => array(
'type' =>
'date',
'label' =>
'DateValidation',
'enabled' => 1,
'visible' => -1,
'position' => 240),
330 'last_main_doc' => array(
'type' =>
'varchar(255)',
'label' =>
'Last main doc',
'enabled' => 1,
'visible' => -1,
'position' => 245),
331 'fk_statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Status',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 500),
332 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => -2,
'position' => 900),
384 const CLOSECODE_DISCOUNTVAT =
'discount_vat';
385 const CLOSECODE_BADCREDIT =
'badsupplier';
386 const CLOSECODE_ABANDONED =
'abandon';
387 const CLOSECODE_REPLACED =
'replaced';
399 $this->ismultientitymanaged = 1;
410 global $langs,
$conf, $hookmanager;
416 if (isset($this->ref_supplier)) {
417 $this->ref_supplier = trim($this->ref_supplier);
419 if (empty($this->
type)) {
422 if (empty($this->
date)) {
427 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
432 if (empty($this->fk_multicurrency)) {
433 $this->multicurrency_code =
$conf->currency;
434 $this->fk_multicurrency = 0;
435 $this->multicurrency_tx = 1;
442 $originaldatewhen = 0;
444 $previousdaynextdatewhen = 0;
448 if ($this->fac_rec > 0) {
449 $this->fk_fac_rec_source = $this->fac_rec;
451 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture-rec.class.php';
453 $result = $_facrec->fetch($this->fac_rec);
454 $result = $_facrec->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
457 if (!empty($_facrec->frequency)) {
458 $originaldatewhen = $_facrec->date_when;
459 $nextdatewhen =
dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency);
461 $this->socid = $_facrec->socid;
464 $this->entity = $_facrec->entity;
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;
478 $this->total_ht = $_facrec->total_ht;
479 $this->total_ttc = $_facrec->total_ttc;
482 $this->fk_incoterms = $_facrec->fk_incoterms;
483 $this->location_incoterms = $_facrec->location_incoterms;
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));
493 $this->array_options = $_facrec->array_options;
495 if (! $this->mode_reglement_id) {
496 $this->mode_reglement_id = 0;
501 $this->linked_objects = $_facrec->linkedObjectsIds;
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;
514 $next_date = $_facrec->getNextDate();
515 $result = $_facrec->setValueFrom(
'date_last_gen', $now,
'', 0,
'date',
'', $user,
'');
517 $result = $_facrec->setNextDate($next_date, 1);
521 $outputlangs = $langs;
524 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && isset($this->thirdparty->default_lang)) {
525 $newlang = $this->thirdparty->default_lang;
527 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && empty($newlang) && property_exists($this,
'default_lang') && isset($this->default_lang)) {
528 $newlang = $this->default_lang;
530 if (!empty($newlang)) {
532 $outputlangs->setDefaultLang($newlang);
534 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null, $this);
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;
557 if (!empty($forceduedate)) {
558 $this->date_echeance = $forceduedate;
561 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture_fourn (";
563 $sql .=
", ref_supplier";
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";
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");
619 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
620 $resql = $this->db->query($sql);
622 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn');
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);
628 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
629 $resql = $this->db->query($sql);
634 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
635 $this->linked_objects = $this->linkedObjectsIds;
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)) {
642 foreach ($tmp_origin_id as $origin_id) {
645 dol_print_error($this->db);
650 $origin_id = $tmp_origin_id;
653 dol_print_error($this->db);
660 if (!$error && empty($this->fac_rec) && count($this->lines) && is_object($this->lines[0])) {
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').
')';
666 $resql_insert = $this->db->query($sql);
668 $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn_det');
671 $line_price_base_type = $this->lines[$i]->getPriceBaseType();
672 $line_pu = ($line_price_base_type ===
'TTC') ? (
float) $this->lines[$i]->subprice_ttc : $this->lines[$i]->subprice;
675 $this->lines[$i]->desc ? $this->lines[$i]->desc : $this->lines[$i]->description,
677 $this->lines[$i]->tva_tx.($this->lines[$i]->vat_src_code ?
' ('.$this->lines[$i]->vat_src_code.
')' :
''),
678 $this->lines[$i]->localtax1_tx,
679 $this->lines[$i]->localtax2_tx,
680 $this->lines[$i]->qty,
681 $this->lines[$i]->fk_product,
682 $line_price_base_type,
683 (!empty($this->lines[$i]->info_bits) ? $this->lines[$i]->info_bits :
''),
684 $this->lines[$i]->product_type,
685 $this->lines[$i]->remise_percent,
687 $this->lines[$i]->date_start,
688 $this->lines[$i]->date_end,
689 $this->lines[$i]->array_options,
690 $this->lines[$i]->fk_unit,
691 $this->lines[$i]->multicurrency_subprice,
692 $this->lines[$i]->ref_supplier
695 $this->error = $this->db->lasterror();
696 $this->db->rollback();
700 } elseif (!$error && empty($this->fac_rec)) {
701 dol_syslog(
"There is ".count($this->lines).
" lines that are array lines");
702 foreach ($this->lines as $i => $val) {
703 $line = $this->lines[$i];
707 if (!is_object($line)) {
708 $line = (object) $line;
711 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facture_fourn_det (fk_facture_fourn, special_code, fk_remise_except)';
712 $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').
')';
714 $resql_insert = $this->db->query($sql);
716 $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn_det');
720 $line->desc ? $line->desc : $line->description,
728 (!empty($line->info_bits) ? $line->info_bits :
''),
730 $line->remise_percent,
734 $line->array_options,
736 $line->multicurrency_subprice,
740 $this->error = $this->db->lasterror();
741 $this->db->rollback();
751 foreach ($_facrec->lines as $i => $val) {
752 $product_type = $_facrec->lines[$i]->product_type;
753 if ($_facrec->lines[$i]->fk_product) {
755 $res =
$prod->fetch($_facrec->lines[$i]->fk_product);
766 $tva_tx = $_facrec->lines[$i]->tva_tx . ($_facrec->lines[$i]->vat_src_code ?
'(' . $_facrec->lines[$i]->vat_src_code .
')' :
'');
767 $tva_npr = $_facrec->lines[$i]->info_bits;
768 if (empty($tva_tx)) {
771 $localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
772 $localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
776 $buyprice = empty($_facrec->lines[$i]->buyprice) ? 0 : $_facrec->lines[$i]->buyprice;
779 if (! $buyprice && $_facrec->lines[$i]->fk_product > 0) {
780 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.product.class.php';
782 $producttmp->fetch($_facrec->lines[$i]->fk_product);
787 if (
getDolGlobalString(
'MARGIN_TYPE') ==
'costprice' && !empty($producttmp->cost_price)) {
788 $buyprice = $producttmp->cost_price;
790 $buyprice = $producttmp->pmp;
792 if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0) {
793 if ($producttmp->product_fourn_price_id > 0) {
794 $buyprice =
price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent / 100) + $producttmp->fourn_remise,
'MU');
800 $result_insert = $this->
addline(
801 $_facrec->lines[$i]->desc ? $_facrec->lines[$i]->desc : $_facrec->lines[$i]->description,
802 $_facrec->lines[$i]->pu_ht,
806 $_facrec->lines[$i]->qty,
807 $_facrec->lines[$i]->fk_product,
808 $_facrec->lines[$i]->remise_percent,
809 ($_facrec->lines[$i]->date_start == 1 && $this->date) ? $this->
date :
'',
810 ($_facrec->lines[$i]->date_end == 1 && $previousdaynextdatewhen) ? $previousdaynextdatewhen :
'',
812 $_facrec->lines[$i]->info_bits,
815 $_facrec->lines[$i]->rang,
817 $_facrec->lines[$i]->array_options,
818 $_facrec->lines[$i]->fk_unit,
821 $_facrec->lines[$i]->ref_supplier,
822 $_facrec->lines[$i]->special_code,
826 if ($result_insert < 0) {
828 $this->error = $this->db->error();
848 $result = $this->call_trigger(
'BILL_SUPPLIER_CREATE', $user);
859 $this->db->rollback();
863 $this->error = $langs->trans(
'FailedToUpdatePrice');
864 $this->db->rollback();
868 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
869 $this->error = $langs->trans(
'ErrorRefAlreadyExists');
870 $this->db->rollback();
873 $this->error = $this->db->lasterror();
874 $this->db->rollback();
888 public function fetch($id = 0, $ref =
'', $ref_ext =
'')
890 if (empty($id) && empty($ref) && empty($ref_ext)) {
897 $sql .=
" t.ref_supplier,";
898 $sql .=
" t.ref_ext,";
899 $sql .=
" t.entity,";
901 $sql .=
" t.subtype,";
902 $sql .=
" t.fk_soc,";
905 $sql .=
" t.date_pointoftax,";
906 $sql .=
" t.tms as datem,";
907 $sql .=
" t.libelle as label,";
908 $sql .=
" t.paye as paid,";
909 $sql .=
" t.close_code,";
910 $sql .=
" t.close_note,";
912 $sql .=
" t.localtax1,";
913 $sql .=
" t.localtax2,";
914 $sql .=
" t.total_ht,";
915 $sql .=
" t.total_tva,";
916 $sql .=
" t.total_ttc,";
917 $sql .=
" t.fk_statut as status,";
918 $sql .=
" t.fk_user_author,";
919 $sql .=
" t.fk_user_valid,";
920 $sql .=
" t.fk_facture_source,";
921 $sql .=
" t.vat_reverse_charge,";
922 $sql .=
" t.fk_fac_rec_source,";
923 $sql .=
" t.fk_projet as fk_project,";
924 $sql .=
" t.fk_cond_reglement,";
925 $sql .=
" t.fk_account,";
926 $sql .=
" t.fk_mode_reglement,";
927 $sql .=
" t.date_lim_reglement,";
928 $sql .=
" t.note_private,";
929 $sql .=
" t.note_public,";
930 $sql .=
" t.model_pdf,";
931 $sql .=
" t.last_main_doc,";
932 $sql .=
" t.import_key,";
933 $sql .=
" t.extraparams,";
934 $sql .=
" cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc,";
935 $sql .=
" p.code as mode_reglement_code, p.libelle as mode_reglement_label,";
936 $sql .=
' s.nom as socnom, s.rowid as socid,';
937 $sql .=
' t.fk_incoterms, t.location_incoterms,';
938 $sql .=
" i.libelle as label_incoterms,";
939 $sql .=
' t.fk_transport_mode,';
940 $sql .=
' t.fk_multicurrency, t.multicurrency_code, t.multicurrency_tx, t.multicurrency_total_ht, t.multicurrency_total_tva, t.multicurrency_total_ttc';
941 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as t';
942 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON (t.fk_soc = s.rowid)";
943 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_payment_term as cr ON t.fk_cond_reglement = cr.rowid";
944 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as p ON t.fk_mode_reglement = p.id";
945 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON t.fk_incoterms = i.rowid';
947 $sql .=
" WHERE t.rowid = ".((int) $id);
949 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_invoice').
')';
951 $sql .=
" AND t.ref = '".$this->db->escape($ref).
"'";
954 $sql .=
" AND t.ref_ext = '".$this->db->escape($ref_ext).
"'";
958 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
959 $resql = $this->db->query($sql);
961 if ($this->db->num_rows($resql)) {
962 $obj = $this->db->fetch_object($resql);
964 $this->
id = $obj->rowid;
965 $this->
ref = $obj->ref ? $obj->ref : $obj->rowid;
967 $this->ref_supplier = $obj->ref_supplier;
968 $this->ref_ext = $obj->ref_ext;
969 $this->entity = $obj->entity;
971 $this->subtype = $obj->subtype;
972 $this->socid = $obj->fk_soc;
973 $this->
date = $this->db->jdate($obj->datef);
974 $this->date_pointoftax = $this->db->jdate($obj->date_pointoftax);
975 $this->date_creation = $this->db->jdate($obj->datec);
976 $this->datec = $this->db->jdate($obj->datec);
977 $this->date_modification = $this->db->jdate($obj->datem);
978 $this->tms = $this->db->jdate($obj->datem);
979 $this->libelle = $obj->label;
980 $this->label = $obj->label;
981 $this->paye = $obj->paid;
982 $this->paid = $obj->paid;
983 $this->close_code = $obj->close_code;
984 $this->close_note = $obj->close_note;
985 $this->total_localtax1 = $obj->localtax1;
986 $this->total_localtax2 = $obj->localtax2;
987 $this->total_ht = $obj->total_ht;
988 $this->total_tva = $obj->total_tva;
989 $this->total_ttc = $obj->total_ttc;
990 $this->
status = $obj->status;
991 $this->statut = $obj->status;
992 $this->fk_statut = $obj->status;
993 $this->user_creation_id = $obj->fk_user_author;
994 $this->author = $obj->fk_user_author;
995 $this->user_validation_id = $obj->fk_user_valid;
996 $this->fk_user_valid = $obj->fk_user_valid;
997 $this->fk_facture_source = $obj->fk_facture_source;
998 $this->vat_reverse_charge = empty($obj->vat_reverse_charge) ? 0 : 1;
999 $this->fk_fac_rec_source = $obj->fk_fac_rec_source;
1000 $this->fk_project = $obj->fk_project;
1001 $this->cond_reglement_id = $obj->fk_cond_reglement;
1002 $this->cond_reglement_code = $obj->cond_reglement_code;
1003 $this->cond_reglement_label = $obj->cond_reglement_label;
1004 $this->cond_reglement_doc = $obj->cond_reglement_doc;
1005 $this->fk_account = $obj->fk_account;
1006 $this->mode_reglement_id = $obj->fk_mode_reglement;
1007 $this->mode_reglement_code = $obj->mode_reglement_code;
1008 $this->mode_reglement = $obj->mode_reglement_label;
1009 $this->date_echeance = $this->db->jdate($obj->date_lim_reglement);
1010 $this->note = $obj->note_private;
1011 $this->note_private = $obj->note_private;
1012 $this->note_public = $obj->note_public;
1013 $this->model_pdf = $obj->model_pdf;
1014 $this->last_main_doc = $obj->last_main_doc;
1015 $this->import_key = $obj->import_key;
1018 $this->fk_incoterms = $obj->fk_incoterms;
1019 $this->location_incoterms = $obj->location_incoterms;
1020 $this->label_incoterms = $obj->label_incoterms;
1021 $this->transport_mode_id = $obj->fk_transport_mode;
1024 $this->fk_multicurrency = $obj->fk_multicurrency;
1025 $this->multicurrency_code = $obj->multicurrency_code;
1026 $this->multicurrency_tx = $obj->multicurrency_tx;
1027 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1028 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1029 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1031 $this->extraparams = isset($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
1033 $this->socid = $obj->socid;
1035 $this->thirdparty =
null;
1043 $this->error = $this->db->lasterror();
1047 $this->error =
'Bill with id '.$id.
' not found';
1048 dol_syslog(get_class($this).
'::fetch '.$this->error);
1052 $this->db->free($resql);
1055 $this->error =
"Error ".$this->db->lasterror();
1070 $this->lines = array();
1072 $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';
1073 $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';
1074 $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';
1075 $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';
1076 $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';
1077 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn_det as f';
1078 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON f.fk_product = p.rowid';
1079 $sql .=
' WHERE fk_facture_fourn='.((int) $this->
id);
1080 $sql .=
' ORDER BY f.rang, f.rowid';
1082 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
1084 $resql_rows = $this->db->query($sql);
1086 $num_rows = $this->db->num_rows($resql_rows);
1089 while ($i < $num_rows) {
1090 $obj = $this->db->fetch_object($resql_rows);
1094 $line->id = $obj->rowid;
1095 $line->rowid = $obj->rowid;
1097 $line->description = $obj->line_desc;
1098 $line->desc = $obj->line_desc;
1099 $line->date_start = $this->db->jdate($obj->date_start);
1100 $line->date_end = $this->db->jdate($obj->date_end);
1102 $line->product_ref = $obj->product_ref;
1103 $line->ref = $obj->product_ref;
1104 $line->ref_supplier = $obj->ref_supplier;
1105 $line->libelle = $obj->label;
1106 $line->label = $obj->label;
1107 $line->product_barcode = $obj->product_barcode;
1108 $line->product_desc = $obj->product_desc;
1109 $line->subprice = $obj->pu_ht;
1110 $line->pu_ht = $obj->pu_ht;
1111 $line->subprice_ttc = $obj->pu_ttc;
1112 $line->pu_ttc = $obj->pu_ttc;
1113 $line->vat_src_code = $obj->vat_src_code;
1114 $line->tva_tx = $obj->tva_tx;
1115 $line->localtax1_tx = $obj->localtax1_tx;
1116 $line->localtax2_tx = $obj->localtax2_tx;
1117 $line->localtax1_type = $obj->localtax1_type;
1118 $line->localtax2_type = $obj->localtax2_type;
1119 $line->qty = $obj->qty;
1120 $line->remise_percent = $obj->remise_percent;
1121 $line->fk_remise_except = $obj->fk_remise_except;
1123 $line->total_ht = $obj->total_ht;
1124 $line->total_ttc = $obj->total_ttc;
1125 $line->total_tva = $obj->total_tva;
1126 $line->total_localtax1 = $obj->total_localtax1;
1127 $line->total_localtax2 = $obj->total_localtax2;
1128 $line->fk_facture_fourn = $obj->fk_facture_fourn;
1129 $line->fk_product = $obj->fk_product;
1130 $line->product_type = $obj->product_type;
1131 $line->product_label = $obj->label;
1132 $line->info_bits = $obj->info_bits;
1133 $line->fk_parent_line = $obj->fk_parent_line;
1134 $line->special_code = $obj->special_code;
1135 $line->rang = $obj->rang;
1136 $line->fk_unit = $obj->fk_unit;
1138 $line->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
1141 $line->fk_code_ventilation = $obj->fk_code_ventilation;
1144 $line->fk_multicurrency = $obj->fk_multicurrency;
1145 $line->multicurrency_code = $obj->multicurrency_code;
1146 $line->multicurrency_subprice = $obj->multicurrency_subprice;
1147 $line->multicurrency_total_ht = $obj->multicurrency_total_ht;
1148 $line->multicurrency_total_tva = $obj->multicurrency_total_tva;
1149 $line->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1152 $line->fetch_optionals();
1154 $this->lines[$i] = $line;
1159 $this->db->free($resql_rows);
1162 $this->error = $this->db->error();
1163 dol_syslog(get_class($this).
"::fetch_lines - No lines:{$this->error} Error:{$this->error}", LOG_DEBUG);
1176 public function update($user =
null, $notrigger = 0)
1182 if (empty($this->
type)) {
1185 if (isset($this->
ref)) {
1186 $this->
ref = trim($this->
ref);
1188 if (isset($this->ref_supplier)) {
1189 $this->ref_supplier = trim($this->ref_supplier);
1191 if (isset($this->ref_ext)) {
1192 $this->ref_ext = trim($this->ref_ext);
1194 if (isset($this->entity)) {
1195 $this->entity = (int) $this->entity;
1197 if (isset($this->
type)) {
1200 if (isset($this->subtype)) {
1201 $this->subtype = (int) $this->subtype;
1203 if (isset($this->socid)) {
1204 $this->socid = (int) $this->socid;
1206 if (isset($this->label)) {
1207 $this->label = trim($this->label);
1209 if (isset($this->paid)) {
1210 $this->paid = (int) (
bool) $this->paid;
1211 $this->paye = $this->paid;
1212 } elseif (isset($this->paye)) {
1213 $this->paid = (int) (
bool) $this->paye;
1214 $this->paye = $this->paid;
1216 if (isset($this->close_code)) {
1217 $this->close_code = trim($this->close_code);
1219 if (isset($this->close_note)) {
1220 $this->close_note = trim($this->close_note);
1222 if (empty($this->total_ht)) {
1223 $this->total_ht = 0;
1225 if (empty($this->total_tva)) {
1226 $this->total_tva = 0;
1228 if (empty($this->total_localtax1)) {
1229 $this->total_localtax1 = 0;
1231 if (empty($this->total_localtax2)) {
1232 $this->total_localtax2 = 0;
1234 if (isset($this->total_ttc)) {
1235 $this->total_ttc = (float) $this->total_ttc;
1237 if (isset($this->
status)) {
1239 $this->statut = $this->status;
1240 } elseif (isset($this->statut)) {
1241 $this->
status = (int) $this->statut;
1242 $this->statut = $this->status;
1244 if (isset($this->author)) {
1245 $this->author = (int) $this->author;
1247 if (isset($this->fk_user_valid)) {
1248 $this->fk_user_valid = (int) $this->fk_user_valid;
1250 if (isset($this->fk_facture_source)) {
1251 $this->fk_facture_source = (int) $this->fk_facture_source;
1253 if (isset($this->fk_project)) {
1254 if (empty($this->fk_project)) {
1255 $this->fk_project = 0;
1257 $this->fk_project = (int) $this->fk_project;
1260 if (isset($this->mode_reglement_id)) {
1261 $this->mode_reglement_id = (int) $this->mode_reglement_id;
1263 if (isset($this->cond_reglement_id)) {
1264 $this->cond_reglement_id = (int) $this->cond_reglement_id;
1266 if (isset($this->note_private)) {
1267 $this->note_private = trim($this->note_private);
1268 $this->note = $this->note_private;
1270 if (isset($this->note_public)) {
1271 $this->note_public = trim($this->note_public);
1273 if (isset($this->model_pdf)) {
1274 $this->model_pdf = trim($this->model_pdf);
1276 if (isset($this->import_key)) {
1277 $this->import_key = trim($this->import_key);
1284 if (
dol_strlen((
string) $this->date_modification) == 0) {
1289 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn SET";
1290 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1291 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1292 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1293 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) :
"null").
",";
1294 $sql .=
" type=".(isset($this->
type) ? ((int) $this->
type) :
"null").
",";
1295 $sql .=
" subtype=".(isset($this->subtype) ? (int) $this->subtype :
"null").
",";
1296 $sql .=
" fk_soc=".(isset($this->socid) ? ((int) $this->socid) :
"null").
",";
1297 $sql .=
" datec=".(dol_strlen((
string) $this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
1298 $sql .=
" datef=".(dol_strlen((
string) $this->date) != 0 ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
1299 $sql .=
" date_pointoftax=".(dol_strlen((
string) $this->date_pointoftax) != 0 ?
"'".$this->db->idate($this->date_pointoftax).
"'" :
'null').
",";
1300 if (
dol_strlen((
string) $this->date_modification) != 0) {
1301 $sql .=
" tms=".(dol_strlen((
string) $this->date_modification) != 0 ?
"'".$this->db->idate($this->date_modification).
"'" :
'null').
",";
1302 } elseif (
dol_strlen((
string) $this->tms) != 0) {
1303 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
1305 $sql .=
" libelle=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
1306 $sql .=
" paye=".(isset($this->paid) ? ((int) $this->paid) :
"0").
",";
1307 $sql .=
" close_code=".(isset($this->close_code) ?
"'".$this->db->escape($this->close_code).
"'" :
"null").
",";
1308 $sql .=
" close_note=".(isset($this->close_note) ?
"'".$this->db->escape($this->close_note).
"'" :
"null").
",";
1309 $sql .=
" localtax1=".(isset($this->total_localtax1) ? ((float) $this->total_localtax1) :
"null").
",";
1310 $sql .=
" localtax2=".(isset($this->total_localtax2) ? ((float) $this->total_localtax2) :
"null").
",";
1311 $sql .=
" total_ht=".(isset($this->total_ht) ? ((float) $this->total_ht) :
"null").
",";
1312 $sql .=
" total_tva=".(isset($this->total_tva) ? ((float) $this->total_tva) :
"null").
",";
1313 $sql .=
" total_ttc=".(isset($this->total_ttc) ? ((float) $this->total_ttc) :
"null").
",";
1314 $sql .=
" fk_statut=".(isset($this->
status) ? ((int) $this->
status) : (isset($this->statut) ? ((int) $this->statut) :
"null")).
",";
1315 $sql .=
" fk_user_author=".(isset($this->author) ? ((int) $this->author) :
"null").
",";
1316 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? ((int) $this->fk_user_valid) :
"null").
",";
1317 $sql .=
" fk_facture_source=".($this->fk_facture_source ? ((int) $this->fk_facture_source) :
"null").
",";
1318 $sql .=
" vat_reverse_charge = ".($this->vat_reverse_charge !=
'' ? ((int) $this->vat_reverse_charge) : 0).
",";
1319 $sql .=
" fk_projet=".(!empty($this->fk_project) ? ((int) $this->fk_project) :
"null").
",";
1320 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? ((int) $this->mode_reglement_id) :
"null").
",";
1321 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? ((int) $this->cond_reglement_id) :
"null").
",";
1322 $sql .=
" date_lim_reglement=".(dol_strlen((
string) $this->date_echeance) != 0 ?
"'".$this->db->idate($this->date_echeance).
"'" :
'null').
",";
1323 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1324 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1325 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1326 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
1327 $sql .=
" WHERE rowid=".((int) $this->
id);
1331 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1332 $resql = $this->db->query($sql);
1337 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1338 $this->errors[] = $langs->trans(
'ErrorRefAlreadyExists');
1340 $this->errors[] =
"Error ".$this->db->lasterror();
1351 if (!$error && !$notrigger) {
1353 $result = $this->call_trigger(
'BILL_SUPPLIER_MODIFY', $user);
1362 foreach ($this->errors as $errmsg) {
1363 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1364 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1366 $this->db->rollback();
1369 $this->db->commit();
1386 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1387 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1392 $result = $remise->fetch($idremise);
1395 if ($this->socid > 0 && $remise->fk_soc != $this->socid) {
1396 $this->error = $langs->trans(
"ErrorDiscountNotSameCompany");
1397 $this->db->rollback();
1400 if ($remise->fk_invoice_supplier) {
1401 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
1402 $this->db->rollback();
1407 $facligne->fk_facture_fourn = $this->id;
1408 $facligne->fk_remise_except = $remise->id;
1409 $facligne->desc = $remise->description;
1410 $facligne->vat_src_code = $remise->vat_src_code;
1411 $facligne->tva_tx = $remise->tva_tx;
1412 $facligne->localtax1_tx = $remise->localtax1_tx;
1413 $facligne->localtax1_type = $remise->localtax1_type;
1414 $facligne->localtax2_tx = $remise->localtax1_tx;
1415 $facligne->localtax2_type = $remise->localtax1_type;
1416 $facligne->subprice = -(float) $remise->amount_ht;
1417 $facligne->fk_product = 0;
1418 $facligne->product_type = 0;
1420 $facligne->remise_percent = 0;
1421 $facligne->rang = -1;
1422 $facligne->info_bits = 2;
1425 $facligne->rang = 1;
1426 $linecount = count($this->lines);
1427 for ($ii = 1; $ii <= $linecount; $ii++) {
1433 if ($remise->fk_invoice_supplier_source > 0) {
1435 $srcinvoice->fetch($remise->fk_invoice_supplier_source);
1436 $totalcostpriceofinvoice = 0;
1437 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmargin.class.php';
1439 $arraytmp = $formmargin->getMarginInfosArray($srcinvoice,
false);
1440 $facligne->pa_ht = $arraytmp[
'pa_total'];
1443 $facligne->total_ht = -(float) $remise->amount_ht;
1444 $facligne->total_tva = -(float) $remise->amount_tva;
1445 $facligne->total_ttc = -(float) $remise->amount_ttc;
1446 $facligne->total_localtax1 = -(float) $remise->total_localtax1;
1447 $facligne->total_localtax2 = -(float) $remise->total_localtax2;
1450 $facligne->fk_multicurrency = $this->fk_multicurrency;
1451 $facligne->multicurrency_code = $this->multicurrency_code;
1452 $facligne->multicurrency_subprice = -$remise->multicurrency_subprice;
1453 $facligne->multicurrency_total_ht = -$remise->multicurrency_total_ht;
1454 $facligne->multicurrency_total_tva = -$remise->multicurrency_total_tva;
1455 $facligne->multicurrency_total_ttc = -$remise->multicurrency_total_ttc;
1457 $lineid = $facligne->insert();
1462 $result = $remise->link_to_invoice($lineid, 0);
1464 $this->error = $remise->error;
1465 $this->db->rollback();
1469 $this->db->commit();
1472 $this->error = $facligne->error;
1473 $this->db->rollback();
1477 $this->error = $facligne->error;
1478 $this->db->rollback();
1482 $this->db->rollback();
1495 public function delete(
User $user, $notrigger = 0)
1501 dol_syslog(
"FactureFournisseur::delete rowid=".$rowid, LOG_DEBUG);
1506 dol_syslog(get_class($this).
"::delete refused, invoice is not erasable (code ".$result.
")", LOG_DEBUG);
1515 $result = $this->call_trigger(
'BILL_SUPPLIER_DELETE', $user);
1517 $this->db->rollback();
1524 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"categorie_supplier_invoice";
1525 $sql .=
" WHERE fk_supplier_invoice = ".((int) $this->
id);
1527 $result = $this->db->query($sql);
1530 $this->error = $this->db->lasterror();
1531 $this->errors[] = $this->error;
1536 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'societe_remise_except';
1537 $sql .=
' WHERE fk_invoice_supplier_source = '.((int) $rowid);
1538 $sql .=
' AND fk_invoice_supplier_line IS NULL';
1539 $resql = $this->db->query($sql);
1543 $list_rowid_det = array();
1544 foreach ($this->lines as $key => $invoiceline) {
1545 $list_rowid_det[] = $invoiceline->id;
1549 if (count($list_rowid_det)) {
1550 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
1551 $sql .=
' SET fk_invoice_supplier = NULL, fk_invoice_supplier_line = NULL';
1552 $sql .=
' WHERE fk_invoice_supplier_line IN ('.$this->db->sanitize(implode(
',', $list_rowid_det)).
')';
1554 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1555 if (!$this->db->query($sql)) {
1562 $sql_main_table = MAIN_DB_PREFIX.
'facture_fourn_det';
1563 $sql_ef_table = $sql_main_table.
"_extrafields";
1564 $sqlef =
"DELETE FROM $sql_ef_table WHERE fk_object IN (SELECT rowid FROM ".$sql_main_table.
" WHERE fk_facture_fourn = ".((int) $rowid).
")";
1565 $resqlef = $this->db->query($sqlef);
1566 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn_det WHERE fk_facture_fourn = '.((int) $rowid);
1567 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1568 $resql = $this->db->query($sql);
1569 if ($resqlef && $resql) {
1570 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn WHERE rowid = '.((int) $rowid);
1571 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1572 $resql2 = $this->db->query($sql);
1596 if ($conf->fournisseur->facture->dir_output && !empty($ref)) {
1597 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1599 $dir =
$conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$ref;
1600 $file = $dir.
"/".$ref.
".pdf";
1601 if (file_exists($file)) {
1603 $this->error =
'ErrorFailToDeleteFile';
1607 if (file_exists($dir)) {
1611 $this->error =
'ErrorFailToDeleteDir';
1623 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
1628 dol_syslog(get_class($this).
"::delete $this->id by $user->id", LOG_DEBUG);
1629 $this->db->commit();
1632 $this->error = $this->db->lasterror();
1633 $this->db->rollback();
1650 public function set_paid($user, $close_code =
'', $close_note =
'')
1653 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
1654 return $this->
setPaid($user, $close_code, $close_note);
1665 public function setPaid($user, $close_code =
'', $close_note =
'')
1669 if ($this->paid != 1) {
1674 dol_syslog(
"FactureFournisseur::setPaid", LOG_DEBUG);
1676 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn SET';
1677 $sql .=
' fk_statut = '.self::STATUS_CLOSED;
1682 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
1685 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
1687 $sql .=
', fk_user_closing = '.((int) $user->id);
1688 $sql .=
", date_closing = '".$this->db->idate($now).
"'";
1689 $sql .=
' WHERE rowid = '.((int) $this->
id);
1691 $resql = $this->db->query($sql);
1694 $result = $this->call_trigger(
'BILL_SUPPLIER_PAYED', $user);
1701 $this->error = $this->db->error();
1702 dol_print_error($this->db);
1706 $this->db->commit();
1709 $this->db->rollback();
1731 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
1749 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn';
1750 $sql .=
' SET paye=0, fk_statut='.self::STATUS_VALIDATED.
', close_code=null, close_note=null,';
1751 $sql .=
' date_closing=null,';
1752 $sql .=
' fk_user_closing=null';
1753 $sql .=
' WHERE rowid = '.((int) $this->
id);
1755 dol_syslog(get_class($this).
"::set_unpaid", LOG_DEBUG);
1756 $resql = $this->db->query($sql);
1759 $result = $this->call_trigger(
'BILL_SUPPLIER_UNPAYED', $user);
1766 $this->error = $this->db->error();
1767 dol_print_error($this->db);
1771 $this->db->commit();
1774 $this->db->rollback();
1789 public function setCanceled($user, $close_code =
'', $close_note =
'')
1791 dol_syslog(get_class($this).
"::setCanceled rowid=".((
int) $this->
id), LOG_DEBUG);
1795 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn SET';
1796 $sql .=
' fk_statut='.self::STATUS_ABANDONED;
1798 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
1801 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
1803 $sql .=
" WHERE rowid = ".((int) $this->
id);
1805 $resql = $this->db->query($sql);
1809 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
1810 $sql .=
' SET fk_invoice_supplier = NULL';
1811 $sql .=
' WHERE fk_invoice_supplier = '.((int) $this->
id);
1813 $resql = $this->db->query($sql);
1816 $result = $this->call_trigger(
'BILL_SUPPLIER_CANCEL', $user);
1818 $this->db->rollback();
1823 $this->db->commit();
1826 $this->error = $this->db->error().
" sql=".$sql;
1827 $this->db->rollback();
1831 $this->error = $this->db->error().
" sql=".$sql;
1832 $this->db->rollback();
1846 public function validate($user, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
1850 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1855 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number.
', idwarehouse='.$idwarehouse);
1862 if ($this->
status > self::STATUS_DRAFT) {
1863 dol_syslog(get_class($this).
"::validate no draft status", LOG_WARNING);
1866 if (preg_match(
'/^'.preg_quote($langs->trans(
"CopyOf").
' ',
'/').
'/', $this->ref_supplier)) {
1867 $langs->load(
"errors");
1868 $this->error = $langs->trans(
"ErrorFieldFormat", $langs->transnoentities(
"RefSupplier")).
'. '.$langs->trans(
'RemoveString', $langs->transnoentitiesnoconv(
"CopyOf"));
1871 if (count($this->lines) <= 0) {
1872 $langs->load(
"errors");
1873 $this->error = $langs->trans(
"ErrorObjectMustHaveLinesToBeValidated", $this->
ref);
1878 if (!empty($this->thirdparty) && is_object($this->thirdparty)) {
1879 $array_to_check = array(
'IDPROF1',
'IDPROF2',
'IDPROF3',
'IDPROF4',
'IDPROF5',
'IDPROF6',
'EMAIL',
'ACCOUNTANCY_CODE_SUPPLIER');
1880 foreach ($array_to_check as $key) {
1881 $keymin = strtolower($key);
1882 if ($keymin ==
'accountancy_code_supplier') {
1883 $keymin =
'code_compta_fournisseur';
1885 if (!property_exists($this->thirdparty, $keymin)) {
1888 $vallabel = $this->thirdparty->$keymin;
1890 $i = (int) preg_replace(
'/[^0-9]/',
'', $key);
1892 if ($this->thirdparty->isACompany()) {
1894 if (
$mysoc->country_id > 0 && $this->thirdparty->country_id ==
$mysoc->country_id) {
1895 $idprof_mandatory =
'SOCIETE_'.$key.
'_INVOICE_MANDATORY';
1897 $langs->load(
"errors");
1898 $this->error = $langs->trans(
'ErrorProdIdIsMandatory', $langs->transcountry(
'ProfId'.$i, $this->thirdparty->country_code)).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
1899 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
1905 if ($key ==
'EMAIL') {
1908 $langs->load(
"errors");
1909 $this->error = $langs->trans(
"ErrorBadEMail", $this->thirdparty->email).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
1910 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
1913 } elseif ($key ==
'ACCOUNTANCY_CODE_SUPPLIER') {
1915 if (
getDolGlobalString(
'SOCIETE_ACCOUNTANCY_CODE_SUPPLIER_INVOICE_MANDATORY') && empty($this->thirdparty->code_compta_fournisseur)) {
1916 $langs->load(
"errors");
1917 $this->error = $langs->trans(
"ErrorAccountancyCodeSupplierIsMandatory", $this->thirdparty->name).
' ('.$langs->trans(
"ForbiddenBySetupRules").
')';
1918 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
1929 if ($force_number) {
1930 $num = $force_number;
1931 } elseif (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
1934 $num = (string) $this->
ref;
1938 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn";
1939 $sql .=
" SET ref='".$this->db->escape($num).
"', fk_statut = 1, fk_user_valid = ".((int) $user->id).
", date_valid = '".$this->db->idate($now).
"'";
1940 $sql .=
" WHERE rowid = ".((int) $this->
id);
1942 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
1943 $resql = $this->db->query($sql);
1947 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1948 $langs->load(
"agenda");
1950 $cpt = count($this->lines);
1951 for ($i = 0; $i < $cpt; $i++) {
1952 if ($this->lines[$i]->fk_product > 0) {
1954 $mouvP->origin = &$this;
1955 $mouvP->setOrigin($this->element, $this->
id);
1957 $up_ht_disc = $this->lines[$i]->subprice;
1958 if (!empty($this->lines[$i]->remise_percent) && !
getDolGlobalString(
'STOCK_EXCLUDE_DISCOUNT_FOR_PMP')) {
1959 $up_ht_disc =
price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100,
'MU');
1962 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
1964 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
1975 if (empty($notrigger)) {
1977 $result = $this->call_trigger(
'BILL_SUPPLIER_VALIDATE', $user);
1985 $this->oldref = $this->ref;
1988 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
1990 $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).
"'";
1991 $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);
1992 $resql = $this->db->query($sql);
1995 $this->error = $this->db->lasterror();
1997 $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).
"'";
1998 $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);
1999 $resql = $this->db->query($sql);
2002 $this->error = $this->db->lasterror();
2007 $dirsource =
$conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$oldref;
2008 $dirdest =
$conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$this->newref;
2009 if (!$error && file_exists($dirsource)) {
2010 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
2012 if (@rename($dirsource, $dirdest)) {
2015 $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,
'/'));
2016 foreach ($listoffiles as $fileentry) {
2017 $dirsource = $fileentry[
'name'];
2018 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $this->newref, $dirsource);
2019 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
2020 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
2021 @rename($dirsource, $dirdest);
2030 $this->
ref = $this->newref;
2037 $this->db->commit();
2040 $this->db->rollback();
2044 $this->error = $this->db->error();
2045 $this->db->rollback();
2058 public function setDraft($user, $idwarehouse = -1, $notrigger = 0)
2065 if ($this->
status == self::STATUS_DRAFT) {
2066 dol_syslog(__METHOD__.
" already draft status", LOG_WARNING);
2074 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn";
2075 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT;
2076 $sql .=
" WHERE rowid = ".((int) $this->
id);
2078 $result = $this->db->query($sql);
2080 $this->oldcopy = clone $this;
2084 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2085 $langs->load(
"agenda");
2087 $cpt = count($this->lines);
2088 for ($i = 0; $i < $cpt; $i++) {
2089 if ($this->lines[$i]->fk_product > 0) {
2091 $mouvP->origin = &$this;
2092 $mouvP->setOrigin($this->element, $this->
id);
2095 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
2097 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
2103 if (empty($notrigger)) {
2105 $result = $this->call_trigger(
'BILL_SUPPLIER_UNVALIDATE', $user);
2112 $this->db->commit();
2115 $this->db->rollback();
2119 $this->error = $this->db->error();
2120 $this->db->rollback();
2160 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 =
'')
2164 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);
2165 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2167 if ($this->
status == self::STATUS_DRAFT) {
2169 if (empty($remise_percent)) {
2170 $remise_percent = 0;
2175 if (empty($info_bits)) {
2181 if (empty($fk_code_ventilation)) {
2182 $fk_code_ventilation = 0;
2184 if (empty($txtva)) {
2187 if (empty($txlocaltax1)) {
2190 if (empty($txlocaltax2)) {
2194 $remise_percent =
price2num($remise_percent);
2197 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
2204 $langs->load(
"errors");
2205 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2211 if ($fk_product > 0) {
2212 if (
getDolGlobalInt(
'SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY') == 1) {
2214 dol_syslog(get_class($this).
"::addline we check supplier prices fk_product=".$fk_product.
" qty=".$qty.
" ref_supplier=".$ref_supplier);
2216 if (
$prod->fetch($fk_product) > 0) {
2217 $product_type =
$prod->type;
2218 $label =
$prod->label;
2219 $fk_prod_fourn_price = 0;
2223 $result =
$prod->get_buyprice($fk_prod_fourn_price, (
float) $qty, $fk_product,
'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
2226 $pu =
$prod->fourn_pu;
2228 $ref_supplier =
$prod->ref_supplier;
2230 if ($remise_percent == 0 &&
$prod->remise_percent != 0) {
2231 $remise_percent =
$prod->remise_percent;
2235 $langs->load(
"errors");
2236 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2237 $this->db->rollback();
2238 dol_syslog(get_class($this).
"::addline we did not found supplier price, so we can't guess unit price");
2243 if ($result == -1) {
2244 $langs->load(
"errors");
2245 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2246 $this->db->rollback();
2247 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_DEBUG);
2251 $this->error =
$prod->error;
2252 $this->db->rollback();
2253 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_ERR);
2257 $this->error =
$prod->error;
2258 $this->db->rollback();
2263 $product_type = $type;
2275 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
2276 $vat_src_code = $reg[1];
2277 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
2284 $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);
2285 $total_ht = $tabprice[0];
2286 $total_tva = $tabprice[1];
2287 $total_ttc = $tabprice[2];
2288 $total_localtax1 = $tabprice[9];
2289 $total_localtax2 = $tabprice[10];
2290 $pu_ht = $tabprice[3];
2293 $multicurrency_total_ht = $tabprice[16];
2294 $multicurrency_total_tva = $tabprice[17];
2295 $multicurrency_total_ttc = $tabprice[18];
2296 $pu_ht_devise = $tabprice[19];
2305 $rang = $rangmax + 1;
2310 $apply_abs_price_on_credit_note =
false;
2312 $apply_abs_price_on_credit_note =
true;
2318 $supplierinvoiceline->context = $this->context;
2320 $supplierinvoiceline->fk_facture_fourn = $this->id;
2322 $supplierinvoiceline->desc = $desc;
2323 $supplierinvoiceline->ref_supplier = $ref_supplier;
2325 $supplierinvoiceline->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs((
float) $qty) : (float) $qty);
2327 $supplierinvoiceline->subprice = ($apply_abs_price_on_credit_note ? -abs((
float) $pu_ht) : (float) $pu_ht);
2329 $supplierinvoiceline->subprice_ttc = ($price_base_type ===
'TTC') ? ($apply_abs_price_on_credit_note ? -abs((
float) $tabprice[5]) : (float) $tabprice[5]) : 0;
2331 $supplierinvoiceline->vat_src_code = $vat_src_code;
2332 $supplierinvoiceline->tva_tx = $txtva;
2333 $supplierinvoiceline->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
2334 $supplierinvoiceline->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
2335 $supplierinvoiceline->localtax1_type = empty($localtaxes_type[0]) ? 0 : $localtaxes_type[0];
2336 $supplierinvoiceline->localtax2_type = empty($localtaxes_type[2]) ? 0 : $localtaxes_type[2];
2338 $supplierinvoiceline->total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_ht) : (float) $total_ht);
2339 $supplierinvoiceline->total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_tva) : (float) $total_tva);
2340 $supplierinvoiceline->total_localtax1 = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_localtax1) : (float) $total_localtax1);
2341 $supplierinvoiceline->total_localtax2 = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_localtax2) : (float) $total_localtax2);
2342 $supplierinvoiceline->total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_ttc) : (float) $total_ttc);
2344 $supplierinvoiceline->fk_product = $fk_product;
2345 $supplierinvoiceline->product_type = $type;
2346 $supplierinvoiceline->remise_percent = $remise_percent;
2348 $supplierinvoiceline->date_end = $date_end;
2349 $supplierinvoiceline->fk_code_ventilation = $fk_code_ventilation;
2350 $supplierinvoiceline->rang = $rang;
2351 $supplierinvoiceline->info_bits = $info_bits;
2352 $supplierinvoiceline->fk_remise_except = $fk_remise_except;
2355 $supplierinvoiceline->special_code = (int) $special_code;
2356 $supplierinvoiceline->fk_parent_line = $fk_parent_line;
2357 $supplierinvoiceline->origin = $this->origin;
2358 $supplierinvoiceline->origin_type = $origin_type;
2359 $supplierinvoiceline->origin_id = $origin_id;
2360 $supplierinvoiceline->fk_unit = $fk_unit;
2363 $supplierinvoiceline->fk_multicurrency = $this->fk_multicurrency;
2364 $supplierinvoiceline->multicurrency_code = $this->multicurrency_code;
2365 $supplierinvoiceline->multicurrency_subprice = ($apply_abs_price_on_credit_note ? -abs((
float) $pu_ht_devise) : (float) $pu_ht_devise);
2367 $supplierinvoiceline->multicurrency_total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $multicurrency_total_ht) : (float) $multicurrency_total_ht);
2368 $supplierinvoiceline->multicurrency_total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $multicurrency_total_tva) : (float) $multicurrency_total_tva);
2369 $supplierinvoiceline->multicurrency_total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $multicurrency_total_ttc) : (float) $multicurrency_total_ttc);
2371 if (is_array($array_options) && count($array_options) > 0) {
2372 $supplierinvoiceline->array_options = $array_options;
2375 $result = $supplierinvoiceline->insert($notrigger);
2378 $result = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2381 if (!isset($this->context[
'createfromclone'])) {
2382 if (!empty($fk_parent_line)) {
2385 } elseif ($rang > 0 && $rang <= count($this->lines)) {
2387 $linecount = count($this->lines);
2388 for ($ii = $rang; $ii <= $linecount; $ii++) {
2393 $this->lines[] = $supplierinvoiceline;
2396 $this->db->commit();
2397 return $supplierinvoiceline->id;
2399 $this->error = $this->db->error();
2400 $this->db->rollback();
2404 $this->error = $supplierinvoiceline->error;
2405 $this->errors = $supplierinvoiceline->errors;
2406 $this->db->rollback();
2439 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)
2444 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2448 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);
2449 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2453 $remise_percent = (float)
price2num($remise_percent);
2463 $langs->load(
"errors");
2464 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2469 if (empty($vatrate)) {
2472 if (empty($txlocaltax1)) {
2475 if (empty($txlocaltax2)) {
2479 $txlocaltax1 = (float)
price2num($txlocaltax1);
2480 $txlocaltax2 = (float)
price2num($txlocaltax2);
2492 if (preg_match(
'/\((.*)\)/', (
string) $vatrate, $reg)) {
2493 $vat_src_code = $reg[1];
2494 $vatrate = preg_replace(
'/\s*\(.*\)/',
'', (
string) $vatrate);
2497 $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);
2498 $total_ht = $tabprice[0];
2499 $total_tva = $tabprice[1];
2500 $total_ttc = $tabprice[2];
2501 $pu_ht = $tabprice[3];
2502 $pu_tva = $tabprice[4];
2503 $pu_ttc = $tabprice[5];
2504 $total_localtax1 = $tabprice[9];
2505 $total_localtax2 = $tabprice[10];
2508 $multicurrency_total_ht = $tabprice[16];
2509 $multicurrency_total_tva = $tabprice[17];
2510 $multicurrency_total_ttc = $tabprice[18];
2511 $pu_ht_devise = $tabprice[19];
2513 if (empty($info_bits)) {
2520 $line->fetch_optionals();
2522 $staticline = clone $line;
2525 $product =
new Product($this->db);
2526 $result = $product->fetch($idproduct);
2527 $product_type = $product->type;
2529 $idproduct = $staticline->fk_product;
2530 $product_type = $type;
2533 $line->oldline = $staticline;
2534 $line->context = $this->context;
2538 $apply_abs_price_on_credit_note =
false;
2540 $apply_abs_price_on_credit_note =
true;
2543 $line->desc = $desc;
2545 $line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs((
float) $qty) : (float) $qty);
2547 $line->subprice = ($apply_abs_price_on_credit_note ? -abs((
float) $pu_ht) : (float) $pu_ht);
2549 $line->subprice_ttc = ($price_base_type ===
'TTC') ? ($apply_abs_price_on_credit_note ? -abs((
float) $pu_ttc) : (float) $pu_ttc) : 0;
2551 $line->remise_percent = $remise_percent;
2552 $line->ref_supplier = $ref_supplier;
2555 $line->date_end = $date_end;
2557 $line->vat_src_code = $vat_src_code;
2558 $line->tva_tx = $vatrate;
2559 $line->localtax1_tx = $txlocaltax1;
2560 $line->localtax2_tx = $txlocaltax2;
2561 $line->localtax1_type = empty($localtaxes_type[0]) ? 0 : $localtaxes_type[0];
2562 $line->localtax2_type = empty($localtaxes_type[2]) ? 0 : $localtaxes_type[2];
2564 $line->total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_ht) : (float) $total_ht);
2565 $line->total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_tva) : (float) $total_tva);
2566 $line->total_localtax1 = (float) $total_localtax1;
2567 $line->total_localtax2 = (float) $total_localtax2;
2568 $line->total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_ttc) : (float) $total_ttc);
2570 $line->fk_product = $idproduct;
2571 $line->product_type = $product_type;
2572 $line->info_bits = $info_bits;
2573 $line->fk_unit = $fk_unit;
2574 $line->rang = $rang;
2576 if (is_array($array_options) && count($array_options) > 0) {
2578 foreach ($array_options as $key => $value) {
2579 $line->array_options[$key] = $array_options[$key];
2584 $line->multicurrency_subprice = (float) $pu_ht_devise;
2585 $line->multicurrency_total_ht = (float) $multicurrency_total_ht;
2586 $line->multicurrency_total_tva = (float) $multicurrency_total_tva;
2587 $line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
2589 $res = $line->update($notrigger);
2595 $res = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2616 if ($line->fetch($rowid) < 1) {
2619 if ($this->
id > 0 && (
int) $line->fk_facture_fourn !== (
int) $this->id) {
2620 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2627 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
2628 $sql .=
' SET fk_invoice_supplier_line = NULL';
2629 $sql .=
' WHERE fk_invoice_supplier_line = '.((int) $rowid);
2631 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
2632 $result = $this->db->query($sql);
2634 $this->error = $this->db->error();
2635 $this->db->rollback();
2639 $res = $line->delete($notrigger);
2642 $this->errors[] = $line->error;
2643 $this->db->rollback();
2649 $this->db->commit();
2652 $this->db->rollback();
2653 $this->error = $this->db->lasterror();
2668 $sql =
'SELECT c.rowid, datec, tms as datem, ';
2669 $sql .=
' fk_user_author, fk_user_modif, fk_user_valid';
2670 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as c';
2671 $sql .=
' WHERE c.rowid = '.((int) $id);
2673 $result = $this->db->query($sql);
2675 if ($this->db->num_rows($result)) {
2676 $obj = $this->db->fetch_object($result);
2678 $this->
id = $obj->rowid;
2680 $this->user_creation_id = $obj->fk_user_author;
2681 $this->user_validation_id = $obj->fk_user_valid;
2682 $this->user_modification_id = $obj->fk_user_modif;
2683 $this->date_creation = $this->db->jdate($obj->datec);
2684 $this->date_modification = $this->db->jdate($obj->datem);
2687 $this->db->free($result);
2689 dol_print_error($this->db);
2709 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut,";
2710 $sql .=
" ff.rowid as rowidnext";
2711 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
2712 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2713 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as ff ON f.rowid = ff.fk_facture_source";
2714 $sql .=
" WHERE (f.fk_statut = ".self::STATUS_VALIDATED.
" OR (f.fk_statut = ".self::STATUS_ABANDONED.
" AND f.close_code = '".self::CLOSECODE_ABANDONED.
"'))";
2715 $sql .=
" AND f.entity = ".((int)
$conf->entity);
2716 $sql .=
" AND f.paye = 0";
2717 $sql .=
" AND pf.fk_paiementfourn IS NULL";
2718 $sql .=
" AND ff.fk_statut IS NULL";
2720 $sql .=
" AND f.fk_soc = ".((int) $socid);
2722 $sql .=
" ORDER BY f.ref";
2724 dol_syslog(get_class($this).
"::list_replacable_supplier_invoices", LOG_DEBUG);
2725 $resql = $this->db->query($sql);
2727 while ($obj = $this->db->fetch_object($resql)) {
2728 $return[$obj->rowid] = array(
2729 'id' => $obj->rowid,
2731 'status' => $obj->fk_statut
2737 $this->error = $this->db->error();
2758 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut, f.type, f.subtype, f.paye as paid, pf.fk_paiementfourn";
2759 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
2760 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2761 $sql .=
" WHERE f.entity = ".((int)
$conf->entity);
2762 $sql .=
" AND f.fk_statut in (".self::STATUS_VALIDATED.
",".self::STATUS_CLOSED.
")";
2763 $sql .=
" AND NOT EXISTS (SELECT rowid from ".MAIN_DB_PREFIX.
"facture_fourn as ff WHERE f.rowid = ff.fk_facture_source";
2764 $sql .=
" AND ff.type=".self::TYPE_REPLACEMENT.
")";
2765 $sql .=
" AND f.type != ".self::TYPE_CREDIT_NOTE;
2767 $sql .=
" AND f.fk_soc = ".((int) $socid);
2769 $sql .=
" ORDER BY f.ref";
2771 dol_syslog(get_class($this).
"::list_qualified_avoir_supplier_invoices", LOG_DEBUG);
2772 $resql = $this->db->query($sql);
2774 while ($obj = $this->db->fetch_object($resql)) {
2776 if ($obj->fk_statut == self::STATUS_VALIDATED) {
2779 if ($obj->fk_statut == self::STATUS_CLOSED) {
2783 $paymentornot = ($obj->fk_paiementfourn ? 1 : 0);
2784 $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);
2790 $this->error = $this->db->error();
2805 global
$conf, $langs;
2812 $sql =
'SELECT COUNT(ff.rowid) as nb, SUM(ff.total_ht) as total,';
2813 $sql .=
" SUM(CASE WHEN ff.date_lim_reglement IS NOT NULL AND ff.date_lim_reglement < '".$this->db->idate($now -
$conf->facture->fournisseur->warning_delay).
"' THEN 1 ELSE 0 END) as nblate";
2814 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as ff';
2815 if (empty($user->socid) && !$user->hasRight(
"societe",
"client",
"voir")) {
2816 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON ff.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2818 $sql .=
' WHERE ff.paye = 0';
2819 $sql .=
" AND ff.fk_statut IN (".self::STATUS_VALIDATED.
")";
2820 $sql .=
" AND ff.entity = ".((int)
$conf->entity);
2822 $sql .=
' AND ff.fk_soc = '.((int) $user->socid);
2825 $resql = $this->db->query($sql);
2827 $langs->load(
"bills");
2830 $response->warning_delay =
$conf->warning_delays[
'supplier_invoice'] / 60 / 60 / 24;
2831 $response->label = $langs->trans(
"SupplierBillsToPay");
2832 $response->labelShort = $langs->trans(
"StatusToPay");
2834 $response->url = DOL_URL_ROOT.
'/fourn/facture/list.php?search_status=1&mainmenu=billing&leftmenu=suppliers_bills';
2835 $response->img = img_object($langs->trans(
"Bills"),
"bill");
2837 $obj = $this->db->fetch_object($resql);
2839 $response->nbtodo = (int) $obj->nb;
2840 $response->total = (float) $obj->total;
2841 $response->nbtodolate = (int) $obj->nblate;
2842 if ($response->nbtodolate > 0) {
2843 $response->url_late = DOL_URL_ROOT.
'/fourn/facture/list.php?search_option=late&mainmenu=billing&leftmenu=suppliers_bills';
2847 $this->db->free($resql);
2850 dol_print_error($this->db);
2851 $this->error = $this->db->error();
2867 $langs->load(
'bills');
2870 $moretitle = $params[
'moretitle'] ??
'';
2872 $picto = $this->picto;
2873 if ($this->
type == self::TYPE_REPLACEMENT) {
2876 if ($this->
type == self::TYPE_CREDIT_NOTE) {
2879 if ($this->
type == self::TYPE_DEPOSIT) {
2883 $datas[
'picto'] = img_picto(
'', $picto).
' <u class="paddingrightonly">'.$langs->trans(
"SupplierInvoice").
'</u>';
2884 if ($this->
type == self::TYPE_REPLACEMENT) {
2885 $datas[
'picto'] .=
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"InvoiceReplacement").
'</u>';
2886 } elseif ($this->
type == self::TYPE_CREDIT_NOTE) {
2887 $datas[
'picto'] .=
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"CreditNote").
'</u>';
2888 } elseif ($this->
type == self::TYPE_DEPOSIT) {
2889 $datas[
'picto'] .=
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"Deposit").
'</u>';
2891 if (isset($this->
status)) {
2893 if (isset($this->totalpaid)) {
2894 $alreadypaid = $this->totalpaid;
2897 $datas[
'picto'] .=
' '.$this->getLibStatut(5, $alreadypaid);
2900 $datas[
'picto'] .=
' - '.$moretitle;
2902 if (!empty($this->
ref)) {
2903 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
2905 if (!empty($this->ref_supplier)) {
2906 $datas[
'refsupplier'] =
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
2908 if (empty($params[
'nofetch'])) {
2909 $langs->load(
'companies');
2910 if (empty($this->thirdparty)) {
2913 if (is_object($this->thirdparty)) {
2914 $datas[
'supplier'] =
'<br><b>'.$langs->trans(
'Supplier').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
2917 if (!empty($this->label)) {
2918 $datas[
'label'] =
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
2920 if (!empty($this->
date)) {
2921 $datas[
'date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->
date,
'day');
2923 if (!empty($this->date_echeance)) {
2924 $datas[
'date_echeance'] =
'<br><b>'.$langs->trans(
'DateDue').
':</b> '.
dol_print_date($this->date_echeance,
'day');
2926 if (!empty($this->total_ht)) {
2927 $datas[
'amountht'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1,
$conf->currency);
2929 if (!empty($this->total_tva)) {
2930 $datas[
'totaltva'] =
'<br><b>'.$langs->trans(
'AmountVAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1,
$conf->currency);
2932 if (!empty($this->total_localtax1) && $this->total_localtax1 != 0) {
2934 $datas[
'amountlt1'] =
'<br><b>'.$langs->transcountry(
'AmountLT1',
$mysoc->country_code).
':</b> '.
price($this->total_localtax1, 0, $langs, 0, -1, -1,
$conf->currency);
2936 if (!empty($this->total_localtax2) && $this->total_localtax2 != 0) {
2937 $datas[
'amountlt2'] =
'<br><b>'.$langs->transcountry(
'AmountLT2',
$mysoc->country_code).
':</b> '.
price($this->total_localtax2, 0, $langs, 0, -1, -1,
$conf->currency);
2939 if (!empty($this->revenuestamp)) {
2940 $datas[
'amountrevenustamp'] =
'<br><b>'.$langs->trans(
'RevenueStamp').
':</b> '.
price($this->revenuestamp, 0, $langs, 0, -1, -1,
$conf->currency);
2942 if (!empty($this->total_ttc)) {
2943 $datas[
'totalttc'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1,
$conf->currency);
2961 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
2963 global $langs, $user, $hookmanager;
2967 if ($option ==
'withdraw') {
2968 $url = DOL_URL_ROOT.
'/compta/facture/prelevement.php?facid='.$this->
id.
'&type=bank-transfer';
2969 } elseif ($option ==
'document') {
2970 $url = DOL_URL_ROOT.
'/fourn/facture/document.php?facid='.$this->id;
2972 $url = DOL_URL_ROOT.
'/fourn/facture/card.php?facid='.$this->id;
2979 if ($option !==
'nolink') {
2981 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2982 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
2983 $add_save_lastsearch_values = 1;
2985 if ($add_save_lastsearch_values) {
2986 $url .=
'&save_lastsearch_values=1';
2990 $picto = $this->picto;
2991 if ($this->
type == self::TYPE_REPLACEMENT) {
2994 if ($this->
type == self::TYPE_CREDIT_NOTE) {
2997 if ($this->
type == self::TYPE_DEPOSIT) {
3003 'objecttype' => $this->element,
3004 'option' => $option,
3005 'moretitle' => $moretitle,
3007 $classfortooltip =
'classfortooltip';
3010 $classfortooltip =
'classforajaxtooltip';
3011 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
3023 if (empty($notooltip)) {
3025 $label = $langs->trans(
"ShowSupplierInvoice");
3026 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
3028 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
3029 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
3032 $linkstart =
'<a href="'.$url.
'"';
3033 $linkstart .= $linkclose.
'>';
3036 $result .= $linkstart;
3038 $result .= img_object(($notooltip ?
'' : $label), ($picto ? $picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'"'), 0, 0, $notooltip ? 0 : 1);
3040 if ($withpicto != 2) {
3041 $result .= ($max ?
dol_trunc($ref, $max) : $ref);
3043 $result .= $linkend;
3045 if ($addlinktonotes) {
3046 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3049 $result .=
' <span class="note inline-block">';
3050 $result .=
'<a href="'.DOL_URL_ROOT.
'/fourn/facture/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3051 $result .= img_picto(
'',
'note');
3053 $result .=
'</span>';
3057 $hookmanager->initHooks(array($this->element .
'dao'));
3058 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
3059 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3061 $result = $hookmanager->resPrint;
3063 $result .= $hookmanager->resPrint;
3080 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
3081 return parent::setCategoriesCommon($categories, Categorie::TYPE_SUPPLIER_INVOICE);
3094 global $langs,
$conf;
3095 $langs->load(
"orders");
3099 $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER =
'mod_facture_fournisseur_cactus';
3108 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
3110 foreach ($dirmodels as $reldir) {
3111 $dir =
dol_buildpath($reldir.
"core/modules/supplier_invoice/");
3114 $mybool = ((bool) @include_once $dir.$file) || $mybool;
3118 dol_print_error(
null,
"Failed to include file ".$file);
3122 $obj =
new $classname();
3123 '@phan-var-force ModeleNumRefSuppliersInvoices $obj';
3125 $numref = $obj->getNextValue($soc, $this, $mode);
3127 if ($numref !=
"") {
3130 $this->error = $obj->error;
3146 global $langs,
$conf;
3147 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
3155 $sql =
"SELECT rowid";
3156 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3157 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3158 $sql .= $this->db->plimit(100);
3160 $resql = $this->db->query($sql);
3162 $num_prods = $this->db->num_rows($resql);
3164 while ($i < $num_prods) {
3166 $row = $this->db->fetch_row($resql);
3167 $prodids[$i] = $row[0];
3173 $this->
ref =
'SPECIMEN';
3174 $this->ref_supplier =
'SUPPLIER_REF_SPECIMEN';
3175 $this->specimen = 1;
3178 $this->date_lim_reglement = $this->
date + 3600 * 24 * 30;
3179 $this->cond_reglement_code =
'RECEP';
3180 $this->mode_reglement_code =
'CHQ';
3182 $this->note_public =
'This is a comment (public)';
3183 $this->note_private =
'This is a comment (private)';
3185 $this->multicurrency_tx = 1;
3186 $this->multicurrency_code =
$conf->currency;
3189 if (empty($option) || $option !=
'nolines') {
3192 while ($xnbp < $nbp) {
3194 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3196 $line->subprice = 100;
3198 $line->tva_tx = 19.6;
3199 $line->localtax1_tx = 0;
3200 $line->localtax2_tx = 0;
3202 $line->total_ht = 50;
3203 $line->total_ttc = 59.8;
3204 $line->total_tva = 9.8;
3205 $line->remise_percent = 50;
3207 $line->total_ht = 100;
3208 $line->total_ttc = 119.6;
3209 $line->total_tva = 19.6;
3210 $line->remise_percent = 0;
3213 if ($num_prods > 0) {
3214 $prodid = mt_rand(1, $num_prods);
3215 $line->fk_product = $prodids[$prodid];
3217 $line->product_type = 0;
3219 $this->lines[$xnbp] = $line;
3221 $this->total_ht += $line->total_ht;
3222 $this->total_tva += $line->total_tva;
3223 $this->total_ttc += $line->total_ttc;
3229 $this->total_ht = $xnbp * 100;
3230 $this->total_tva = $xnbp * 19.6;
3231 $this->total_ttc = $xnbp * 119.6;
3243 global
$conf, $user;
3245 $this->nb = array();
3249 $sql =
"SELECT count(f.rowid) as nb";
3250 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
3251 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON f.fk_soc = s.rowid";
3252 if (empty($user->socid) && !$user->hasRight(
"societe",
"client",
"voir")) {
3253 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3254 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3257 $sql .=
" ".$clause.
" f.entity = ".((int)
$conf->entity);
3259 $resql = $this->db->query($sql);
3261 while ($obj = $this->db->fetch_object($resql)) {
3262 $this->nb[
"supplier_invoices"] = $obj->nb;
3264 $this->db->free($resql);
3267 dol_print_error($this->db);
3268 $this->error = $this->db->error();
3283 global $langs, $hookmanager;
3302 $object->ref_supplier = (empty($this->ref_supplier) ? $langs->trans(
"CopyOf").
' '.
$object->ref_supplier : $this->ref_supplier);
3304 $object->user_validation_id = 0;
3305 $object->fk_facture_source = 0;
3307 $object->date_validation =
'';
3309 $object->ref_client =
'';
3317 $object->date_echeance =
$object->calculate_date_lim_reglement();
3320 foreach (
$object->lines as $i => $line) {
3321 if (isset(
$object->lines[$i]->info_bits) && (
$object->lines[$i]->info_bits & 0x02) == 0x02) {
3327 $object->context[
'createfromclone'] =
'createfromclone';
3328 $result =
$object->create($user);
3332 $this->error =
$object->error;
3333 $this->errors =
$object->errors;
3339 if (is_object($hookmanager)) {
3340 $parameters = array(
'objFrom' => $objFrom);
3342 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters,
$object, $action);
3350 unset(
$object->context[
'createfromclone']);
3354 $this->db->commit();
3357 $this->db->rollback();
3373 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3377 $langs->load(
"suppliers");
3378 $outputlangs->load(
"products");
3381 if (empty($modele)) {
3387 } elseif ($modele ==
'auto') {
3388 $modele =
'canelle';
3391 if (empty($modele)) {
3394 $modelpath =
"core/modules/supplier_invoice/doc/";
3396 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3408 return $user->hasRight(
"fournisseur",
"facture");
3456 if (!$this->date_echeance) {
3460 $status = isset($this->
status) ? $this->
status : $this->statut;
3462 return ($status == self::STATUS_VALIDATED) && ($this->date_echeance < ($now -
$conf->facture->fournisseur->warning_delay));
3474 $sql =
"SELECT fk_invoice_supplier FROM ".MAIN_DB_PREFIX.
"societe_remise_except WHERE fk_invoice_supplier_source = ".((int) $this->
id);
3475 $resql = $this->db->query($sql);
3476 if (!empty($resql)) {
3477 $obj = $this->db->fetch_object($resql);
3478 if (!empty($obj->fk_invoice_supplier)) {
3496 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
3498 $picto = $this->picto;
3499 if ($this->
type == self::TYPE_REPLACEMENT) {
3502 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3505 if ($this->
type == self::TYPE_DEPOSIT) {
3509 $return =
'<div class="box-flex-item box-flex-grow-zero">';
3510 $return .=
'<div class="info-box info-box-sm">';
3511 $return .=
'<span class="info-box-icon bg-infobox-action">';
3512 $return .= img_picto(
'', $picto);
3513 $return .=
'</span>';
3514 $return .=
'<div class="info-box-content">';
3515 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . $this->
getNomUrl(1) .
'</span>';
3516 if ($selected >= 0) {
3517 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
3519 if (!empty($arraydata[
'thirdparty'])) {
3520 $return .=
'<br><span class="info-box-label">'.$arraydata[
'thirdparty'].
'</span>';
3522 if (!empty($this->
date)) {
3523 $return .=
'<br><span class="info-box-label">'.dol_print_date($this->
date,
'day').
'</span>';
3525 if (!empty($this->total_ht)) {
3526 $return .=
' <span class="info-box-label amount" title="'.dol_escape_htmltag($langs->trans(
"AmountHT")).
'">'.
price($this->total_ht);
3527 $return .=
' '.$langs->trans(
"HT");
3528 $return .=
'</span>';
3530 $alreadypaid = (empty($arraydata[
'alreadypaid']) ? 0 : $arraydata[
'alreadypaid']);
3531 $return .=
'<br><div class="info-box-status">'.$this->getLibStatut(3, $alreadypaid).
'</div>';
3533 $return .=
'</div>';
3534 $return .=
'</div>';
3535 $return .=
'</div>';
3548 if (!$this->table_element) {
3549 dol_syslog(get_class($this).
"::setVATReverseCharge was called on object with property table_element not defined", LOG_ERR);
3553 dol_syslog(get_class($this).
'::setVATReverseCharge('.$vatreversecharge.
')');
3555 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
3556 $sql .=
" SET vat_reverse_charge = ".((int) $vatreversecharge);
3557 $sql .=
" WHERE rowid=".((int) $this->
id);
3559 if ($this->db->query($sql)) {
3560 $this->vat_reverse_charge = ($vatreversecharge == 0) ? 0 : 1;
3563 dol_syslog(get_class($this).
'::setVATReverseCharge Error ', LOG_DEBUG);
3564 $this->error = $this->db->error();
3582 global
$conf, $langs, $user;
3589 $errorsMsg = array();
3591 $langs->load(
'bills');
3594 $this->output .= $langs->trans(
'ModuleNotEnabled', $langs->transnoentitiesnoconv(
'Suppliers'));
3597 if (!in_array($datetouse, array(
'duedate',
'invoicedate'))) {
3598 $this->output .=
'Bad value for parameter datetouse. Must be "duedate" or "invoicedate"';
3602 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
3603 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
3604 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
3605 $formmail =
new FormMail($this->db);
3613 $labelreminderok =
'sendEmailsRemindersOnInvoiceDueDateOK (nbdays='.$nbdays.
' paymentmode='.$paymentmode.
' template='.$template.
' datetouse='.$datetouse.
' forcerecipient='.$forcerecipient.
')';
3618 $sql =
"SELECT rowid as id FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
3619 if (!empty($paymentmode) && $paymentmode !=
'all') {
3620 $sql .=
", ".MAIN_DB_PREFIX.
"c_paiement as cp";
3622 $sql .=
" WHERE f.paye = 0";
3623 $sql .=
" AND f.fk_statut = ".self::STATUS_VALIDATED;
3624 if ($datetouse ==
'invoicedate') {
3625 $sql .=
" AND f.datef = '".$this->db->idate($tmpidate,
'gmt').
"'";
3627 $sql .=
" AND f.date_lim_reglement = '".$this->db->idate($tmpidate,
'gmt').
"'";
3629 $sql .=
" AND f.entity IN (".getEntity(
'supplier_invoice', 0).
")";
3630 if (!empty($paymentmode) && $paymentmode !=
'all') {
3631 $sql .=
" AND f.fk_mode_reglement = cp.id AND cp.code = '".$this->db->escape($paymentmode).
"'";
3633 $sql .=
" AND f.type <> ".self::TYPE_CREDIT_NOTE;
3634 $sql .=
" AND f.total_ttc > 0";
3636 $sql .=
" AND NOT EXISTS (SELECT a.id FROM ".MAIN_DB_PREFIX.
"actioncomm as a";
3637 $sql .=
" WHERE a.elementtype = 'invoice_supplier' AND a.fk_element = f.rowid AND a.code = 'AC_EMAIL'";
3638 $sql .=
" AND a.label = '".$this->db->escape($labelreminderok).
"'";
3640 if ($datetouse ==
'invoicedate') {
3641 $sql .= $this->db->order(
"datef",
"ASC");
3643 $sql .= $this->db->order(
"date_lim_reglement",
"ASC");
3646 $resql = $this->db->query($sql);
3649 if ($datetouse ==
'invoicedate') {
3650 $this->output .= $langs->transnoentitiesnoconv(
"SearchValidatedSupplierInvoicesWithDate", $stmpidate);
3652 $this->output .= $langs->transnoentitiesnoconv(
"SearchUnpaidSupplierInvoicesWithDueDate", $stmpidate);
3654 if (!empty($paymentmode) && $paymentmode !=
'all') {
3655 $this->output .=
' ('.$langs->transnoentitiesnoconv(
"PaymentMode").
' '.$paymentmode.
')';
3657 $this->output .=
'<br>';
3660 while ($obj = $this->db->fetch_object($resql)) {
3661 $errorforinvoice = 0;
3663 $res = $tmpinvoice->fetch($obj->id);
3665 $tmpinvoice->fetch_thirdparty();
3668 if ($tmpinvoice->thirdparty->default_lang) {
3669 $outputlangs->setDefaultLang($tmpinvoice->thirdparty->default_lang);
3670 $outputlangs->loadLangs(array(
"main",
"suppliers"));
3672 $outputlangs = $langs;
3677 $templateLabel =
'';
3678 if (empty($template) || $template ==
'EmailTemplateCode') {
3679 $templateLabel =
'(SendingReminderEmailOnUnpaidSupplierInvoice)';
3681 if (is_numeric($template)) {
3682 $templateId = $template;
3684 $templateLabel = $template;
3688 $arraymessage = $formmail->getEMailTemplate($this->db,
'invoice_supplier_send', $user, $outputlangs, $templateId, 1, $templateLabel);
3689 if (is_numeric($arraymessage) && $arraymessage <= 0) {
3690 $langs->load(
"errors");
3691 $this->output .= $langs->trans(
'ErrorFailedToFindEmailTemplate', $template);
3699 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null, $tmpinvoice);
3704 $sendTopic =
make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv(
'InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
3707 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
3713 if ($forcerecipient) {
3714 $to = array($forcerecipient);
3716 $res = $tmpinvoice->fetch_thirdparty();
3717 $recipient = $tmpinvoice->thirdparty;
3719 $tmparraycontact = $tmpinvoice->liste_contact(-1,
'internal', 0,
'SALESREPFOLL');
3720 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
3721 foreach ($tmparraycontact as $data_email) {
3722 if (!empty($data_email[
'email'])) {
3723 $to[] = $data_email[
'email'];
3727 if (empty($to) && !empty($recipient->email)) {
3728 $to[] = $recipient->email;
3731 $errormesg =
"Failed to send remind to thirdparty id=".$tmpinvoice->socid.
". No email defined for supplier invoice or customer.";
3736 $errormesg =
"Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
3744 if (!empty($arraymessage->email_from)) {
3745 $email_from = (string) $arraymessage->email_from;
3747 if (empty($email_from)) {
3748 $errormesg =
"Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
3753 if (!$errorforinvoice && !empty($to)) {
3756 $to = implode(
',', $to);
3757 if (!empty($arraymessage->email_to)) {
3758 $to = $to.
','.$arraymessage->email_to;
3764 $trackid =
'inv'.$tmpinvoice->id;
3765 $sendcontext =
'standard';
3768 if (!empty($arraymessage->email_tocc)) {
3769 $email_tocc = (string) $arraymessage->email_tocc;
3773 if (!empty($arraymessage->email_tobcc)) {
3774 $email_tobcc = (string) $arraymessage->email_tobcc;
3778 $cMailFile =
new CMailFile($sendTopic, $to, $email_from, $sendContent, array(), array(), array(), $email_tocc, $email_tobcc, 0, 1, $errors_to,
'', $trackid,
'', $sendcontext,
'');
3781 if ($cMailFile->sendfile()) {
3785 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
3790 $actioncomm->type_code =
'AC_OTH_AUTO';
3791 $actioncomm->socid = $tmpinvoice->thirdparty->id;
3792 $actioncomm->contact_id = 0;
3794 $actioncomm->code =
'AC_EMAIL';
3795 $actioncomm->label = $labelreminderok;
3796 $actioncomm->note_private = $sendContent;
3797 $actioncomm->fk_project = $tmpinvoice->fk_project;
3798 $actioncomm->datep =
dol_now();
3799 $actioncomm->datef = $actioncomm->datep;
3800 $actioncomm->percentage = -1;
3801 $actioncomm->authorid = $user->id;
3802 $actioncomm->userownerid = $user->id;
3804 $actioncomm->email_msgid = $cMailFile->msgid;
3805 $actioncomm->email_subject = $sendTopic;
3806 $actioncomm->email_from = $email_from;
3807 $actioncomm->email_sender =
'';
3808 $actioncomm->email_to = $to;
3812 $actioncomm->errors_to = $errors_to;
3814 $actioncomm->elementtype =
'invoice_supplier';
3815 $actioncomm->elementid = $tmpinvoice->id;
3816 $actioncomm->fk_element = $tmpinvoice->id;
3820 $actioncomm->create($user);
3822 $errormesg = $cMailFile->error.
' : '.$to;
3827 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
3832 $actioncomm->type_code =
'AC_OTH_AUTO';
3833 $actioncomm->socid = $tmpinvoice->thirdparty->id;
3834 $actioncomm->contact_id = 0;
3836 $actioncomm->code =
'AC_EMAIL';
3837 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateKO';
3838 $actioncomm->note_private = $errormesg;
3839 $actioncomm->fk_project = $tmpinvoice->fk_project;
3840 $actioncomm->datep =
dol_now();
3841 $actioncomm->datef = $actioncomm->datep;
3842 $actioncomm->percentage = -1;
3843 $actioncomm->authorid = $user->id;
3844 $actioncomm->userownerid = $user->id;
3846 $actioncomm->email_msgid = $cMailFile->msgid;
3847 $actioncomm->email_from = $email_from;
3848 $actioncomm->email_sender =
'';
3849 $actioncomm->email_to = $to;
3853 $actioncomm->errors_to = $errors_to;
3857 $actioncomm->create($user);
3860 $this->db->commit();
3864 $errorsMsg[] = $errormesg;
3867 $errorsMsg[] =
'Failed to fetch record invoice with ID = '.$obj->id;
3876 $this->output .=
'Nb of emails sent : '.$nbMailSend;
3878 dol_syslog(__METHOD__.
" end - ".$this->output, LOG_INFO);
3882 $this->error =
'Nb of emails sent : '.$nbMailSend.
', '.(empty($errorsMsg) ? $error : implode(
', ', $errorsMsg));
3884 dol_syslog(__METHOD__.
" end - ".$this->error, LOG_INFO);
if(! $sortfield) if(! $sortorder) $object
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
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.
getTooltipContent($params)
getTooltipContent
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...
__construct($db)
Constructor.
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)
fetch_lines()
Load this->lines.
const STATUS_CLOSED
Classified paid.
setVATReverseCharge($vatreversecharge)
Change the option VAT reverse charge.
Class to manage invoice templates.
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...
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)
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
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.
$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.
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 '.
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_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)
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.
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