43require_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
44require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
45require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.ligne.class.php';
46require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
47require_once DOL_DOCUMENT_ROOT.
'/subtotals/class/commonsubtotal.class.php';
50 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formaccounting.class.php';
51 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
64 public $element =
'invoice_supplier';
69 public $table_element =
'facture_fourn';
74 public $table_element_line =
'facture_fourn_det';
79 public $class_element_line =
'SupplierInvoiceLine';
83 public $fk_element =
'fk_facture_fourn';
88 public $picto =
'supplier_invoice';
94 public $restrictiononfksoc = 1;
114 public $ref_supplier;
174 public $fk_user_valid;
188 public $date_echeance;
218 public $total_localtax1;
220 public $total_localtax2;
231 public $note_private;
245 public $transport_mode_id;
250 public $vat_reverse_charge;
255 public $extraparams = array();
260 public $lines = array();
272 public $fk_facture_source;
277 public $fk_fac_rec_source;
279 public $fields = array(
280 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
281 'ref' => array(
'type' =>
'varchar(255)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'showoncombobox' => 1,
'position' => 15),
282 'ref_supplier' => array(
'type' =>
'varchar(255)',
'label' =>
'RefSupplier',
'enabled' => 1,
'visible' => -1,
'position' => 20),
283 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 25,
'index' => 1),
284 'ref_ext' => array(
'type' =>
'varchar(255)',
'label' =>
'RefExt',
'enabled' => 1,
'visible' => 0,
'position' => 30),
285 'type' => array(
'type' =>
'smallint(6)',
'label' =>
'Type',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 35),
286 'subtype' => array(
'type' =>
'smallint(6)',
'label' =>
'InvoiceSubtype',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 36),
287 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => -1,
'notnull' => 1,
'position' => 40),
288 'datec' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -1,
'position' => 45),
289 'datef' => array(
'type' =>
'date',
'label' =>
'Date',
'enabled' => 1,
'visible' => -1,
'position' => 50),
290 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 55),
291 'libelle' => array(
'type' =>
'varchar(255)',
'label' =>
'Label',
'enabled' => 1,
'visible' => -1,
'position' => 60),
292 'paye' => array(
'type' =>
'smallint(6)',
'label' =>
'Paye',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 65),
293 'amount' => array(
'type' =>
'double(24,8)',
'label' =>
'Amount',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 70),
294 'remise' => array(
'type' =>
'double(24,8)',
'label' =>
'Discount',
'enabled' => 1,
'visible' => -1,
'position' => 75),
295 'close_code' => array(
'type' =>
'varchar(16)',
'label' =>
'CloseCode',
'enabled' => 1,
'visible' => -1,
'position' => 80),
296 'close_note' => array(
'type' =>
'varchar(128)',
'label' =>
'CloseNote',
'enabled' => 1,
'visible' => -1,
'position' => 85),
297 'tva' => array(
'type' =>
'double(24,8)',
'label' =>
'Tva',
'enabled' => 1,
'visible' => -1,
'position' => 90),
298 'localtax1' => array(
'type' =>
'double(24,8)',
'label' =>
'Localtax1',
'enabled' => 1,
'visible' => -1,
'position' => 95),
299 'localtax2' => array(
'type' =>
'double(24,8)',
'label' =>
'Localtax2',
'enabled' => 1,
'visible' => -1,
'position' => 100),
300 'total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalHT',
'enabled' => 1,
'visible' => -1,
'position' => 105),
301 'total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalVAT',
'enabled' => 1,
'visible' => -1,
'position' => 110),
302 'total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalTTC',
'enabled' => 1,
'visible' => -1,
'position' => 115),
303 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => -1,
'position' => 125),
304 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 130),
305 'fk_user_valid' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserValidation',
'enabled' => 1,
'visible' => -1,
'position' => 135),
306 'fk_facture_source' => array(
'type' =>
'integer',
'label' =>
'SourceInvoice',
'enabled' => 1,
'visible' => -1,
'position' => 140),
307 'fk_projet' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 145),
308 'fk_account' => array(
'type' =>
'integer',
'label' =>
'Account',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 150),
309 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentTerm',
'enabled' => 1,
'visible' => -1,
'position' => 155),
310 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentMode',
'enabled' => 1,
'visible' => -1,
'position' => 160),
311 'date_lim_reglement' => array(
'type' =>
'date',
'label' =>
'DateLimReglement',
'enabled' => 1,
'visible' => -1,
'position' => 165),
312 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 170),
313 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 175),
314 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'ModelPdf',
'enabled' => 1,
'visible' => 0,
'position' => 180),
315 'extraparams' => array(
'type' =>
'varchar(255)',
'label' =>
'Extraparams',
'enabled' => 1,
'visible' => -1,
'position' => 190),
316 'fk_incoterms' => array(
'type' =>
'integer',
'label' =>
'IncotermCode',
'enabled' => 1,
'visible' => -1,
'position' => 195),
317 'location_incoterms' => array(
'type' =>
'varchar(255)',
'label' =>
'IncotermLocation',
'enabled' => 1,
'visible' => -1,
'position' => 200),
318 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'MulticurrencyId',
'enabled' => 1,
'visible' => -1,
'position' => 205),
319 'multicurrency_code' => array(
'type' =>
'varchar(255)',
'label' =>
'MulticurrencyCode',
'enabled' => 1,
'visible' => -1,
'position' => 210),
320 'multicurrency_tx' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyRate',
'enabled' => 1,
'visible' => -1,
'position' => 215),
321 'multicurrency_total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyTotalHT',
'enabled' => 1,
'visible' => -1,
'position' => 220),
322 'multicurrency_total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyTotalVAT',
'enabled' => 1,
'visible' => -1,
'position' => 225),
323 'multicurrency_total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyTotalTTC',
'enabled' => 1,
'visible' => -1,
'position' => 230),
324 'date_pointoftax' => array(
'type' =>
'date',
'label' =>
'DatePointOfTax',
'enabled' =>
'getDolGlobalString("INVOICE_POINTOFTAX_DATE")',
'visible' => -1,
'position' => 235),
325 'date_valid' => array(
'type' =>
'date',
'label' =>
'DateValidation',
'enabled' => 1,
'visible' => -1,
'position' => 240),
326 'last_main_doc' => array(
'type' =>
'varchar(255)',
'label' =>
'Last main doc',
'enabled' => 1,
'visible' => -1,
'position' => 245),
327 'fk_statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Status',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 500),
328 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => -2,
'position' => 900),
380 const CLOSECODE_DISCOUNTVAT =
'discount_vat';
381 const CLOSECODE_BADCREDIT =
'badsupplier';
382 const CLOSECODE_ABANDONED =
'abandon';
383 const CLOSECODE_REPLACED =
'replaced';
395 $this->ismultientitymanaged = 1;
406 global $langs,
$conf, $hookmanager;
412 if (isset($this->ref_supplier)) {
413 $this->ref_supplier = trim($this->ref_supplier);
415 if (empty($this->
type)) {
418 if (empty($this->
date)) {
423 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
428 if (empty($this->fk_multicurrency)) {
429 $this->multicurrency_code =
$conf->currency;
430 $this->fk_multicurrency = 0;
431 $this->multicurrency_tx = 1;
438 $originaldatewhen = 0;
440 $previousdaynextdatewhen = 0;
444 if ($this->fac_rec > 0) {
445 $this->fk_fac_rec_source = $this->fac_rec;
447 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture-rec.class.php';
449 $result = $_facrec->fetch($this->fac_rec);
450 $result = $_facrec->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
453 if (!empty($_facrec->frequency)) {
454 $originaldatewhen = $_facrec->date_when;
456 $nextdatewhen =
dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency, 0,
'tzserver');
458 $this->socid = $_facrec->socid;
461 $this->entity = $_facrec->entity;
466 $this->fk_project =
GETPOSTINT(
'projectid') > 0 ? (
GETPOSTINT(
'projectid')) : $_facrec->fk_project;
467 $this->note_public =
GETPOST(
'note_public',
'restricthtml') ?
GETPOST(
'note_public',
'restricthtml') : $_facrec->note_public;
468 $this->note_private =
GETPOST(
'note_private',
'restricthtml') ?
GETPOST(
'note_private',
'restricthtml') : $_facrec->note_private;
469 $this->model_pdf =
GETPOST(
'model',
'alpha') ?
GETPOST(
'model',
'alpha') : $_facrec->model_pdf;
470 $this->cond_reglement_id =
GETPOSTINT(
'cond_reglement_id') > 0 ? (
GETPOSTINT(
'cond_reglement_id')) : $_facrec->cond_reglement_id;
471 $this->mode_reglement_id =
GETPOSTINT(
'mode_reglement_id') > 0 ? (
GETPOSTINT(
'mode_reglement_id')) : $_facrec->mode_reglement_id;
472 $this->fk_account =
GETPOST(
'fk_account') > 0 ? ((int)
GETPOST(
'fk_account')) : $_facrec->fk_account;
475 $this->total_ht = $_facrec->total_ht;
476 $this->total_ttc = $_facrec->total_ttc;
479 $this->fk_incoterms = $_facrec->fk_incoterms;
480 $this->location_incoterms = $_facrec->location_incoterms;
486 $this->note_public = trim((
string) $this->note_public);
487 $this->note_private = trim((
string) $this->note_private);
488 $this->note_private =
dol_concatdesc($this->note_private, $langs->trans(
"GeneratedFromRecurringInvoice", $_facrec->title));
490 $this->array_options = $_facrec->array_options;
492 if (! $this->mode_reglement_id) {
493 $this->mode_reglement_id = 0;
498 $this->linked_objects = $_facrec->linkedObjectsIds;
505 if ($_facrec->frequency > 0) {
506 $this->ref_supplier = trim($this->ref_supplier .
'_' . ($_facrec->nb_gen_done + 1));
507 dol_syslog(
"This is a recurring invoice so we set date_last_gen and next date_when");
508 if (empty($_facrec->date_when)) {
509 $_facrec->date_when = $now;
511 $next_date = $_facrec->getNextDate();
512 $result = $_facrec->setValueFrom(
'date_last_gen', $now,
'', 0,
'date',
'', $user,
'');
514 $result = $_facrec->setNextDate($next_date, 1);
518 $outputlangs = $langs;
521 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && isset($this->thirdparty->default_lang)) {
522 $newlang = $this->thirdparty->default_lang;
524 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && empty($newlang) && property_exists($this,
'default_lang') && isset($this->default_lang)) {
525 $newlang = $this->default_lang;
527 if (!empty($newlang)) {
529 $outputlangs->setDefaultLang($newlang);
531 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null, $this);
541 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] = $originaldatewhen ?
dol_print_date($originaldatewhen,
'dayhour') :
'';
542 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] = $nextdatewhen ?
dol_print_date($nextdatewhen,
'dayhour') :
'';
543 $substitutionarray[
'__INVOICE_PREVIOUS_DATE_NEXT_INVOICE_AFTER_GEN__'] = $previousdaynextdatewhen ?
dol_print_date($previousdaynextdatewhen,
'dayhour') :
'';
544 $substitutionarray[
'__INVOICE_COUNTER_CURRENT__'] = $_facrec->nb_gen_done;
545 $substitutionarray[
'__INVOICE_COUNTER_MAX__'] = $_facrec->nb_gen_max;
554 if (!empty($forceduedate)) {
555 $this->date_echeance = $forceduedate;
558 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture_fourn (";
560 $sql .=
", ref_supplier";
569 $sql .=
", date_pointoftax";
570 $sql .=
", vat_reverse_charge";
571 $sql .=
", fk_projet";
572 $sql .=
", fk_cond_reglement";
573 $sql .=
", fk_mode_reglement";
574 $sql .=
", fk_account";
575 $sql .=
", note_private";
576 $sql .=
", note_public";
577 $sql .=
", fk_user_author";
578 $sql .=
", date_lim_reglement";
579 $sql .=
", fk_incoterms, location_incoterms";
580 $sql .=
", fk_multicurrency";
581 $sql .=
", multicurrency_code";
582 $sql .=
", multicurrency_tx";
583 $sql .=
", fk_facture_source";
584 $sql .=
", fk_fac_rec_source";
588 $sql .=
", '".$this->db->escape($this->ref_supplier).
"'";
589 $sql .=
", '".$this->db->escape((
string) $this->ref_ext).
"'";
590 $sql .=
", ".((int) $this->entity);
591 $sql .=
", '".$this->db->escape((
string) $this->type).
"'";
592 $sql .=
", ".(isset($this->subtype) ? (int) $this->subtype :
"null");
593 $sql .=
", '".$this->db->escape(isset($this->label) ? $this->label : (isset($this->libelle) ? $this->libelle :
'')).
"'";
594 $sql .=
", ".((int) $this->socid);
595 $sql .=
", '".$this->db->idate($now).
"'";
596 $sql .=
", '".$this->db->idate($this->
date).
"'";
597 $sql .=
", ".(empty($this->date_pointoftax) ?
"null" :
"'".$this->db->idate($this->date_pointoftax).
"'");
598 $sql .=
", ".($this->vat_reverse_charge !=
'' ? ((int) $this->vat_reverse_charge) : 0);
599 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
600 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
601 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
602 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
603 $sql .=
", '".$this->db->escape($this->note_private).
"'";
604 $sql .=
", '".$this->db->escape($this->note_public).
"'";
605 $sql .=
", ".((int) $user->id).
",";
606 $sql .= $this->date_echeance !=
'' ?
"'".$this->db->idate($this->date_echeance).
"'" :
"null";
607 $sql .=
", ".(int) $this->fk_incoterms;
608 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
609 $sql .=
", ".(int) $this->fk_multicurrency;
610 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
611 $sql .=
", ".(float) $this->multicurrency_tx;
612 $sql .=
", ".($this->fk_facture_source ? ((int) $this->fk_facture_source) :
"null");
613 $sql .=
", ".(isset($this->fk_fac_rec_source) ? ((int) $this->fk_fac_rec_source) :
"NULL");
616 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
617 $resql = $this->db->query($sql);
619 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn');
622 $this->
ref =
'(PROV'.$this->id.
')';
623 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"facture_fourn SET ref='".$this->db->escape($this->
ref).
"' WHERE rowid=".((int) $this->
id);
625 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
626 $resql = $this->db->query($sql);
631 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
632 $this->linked_objects = $this->linkedObjectsIds;
636 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
637 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
638 if (is_array($tmp_origin_id)) {
639 foreach ($tmp_origin_id as $origin_id) {
642 dol_print_error($this->db);
647 $origin_id = $tmp_origin_id;
650 dol_print_error($this->db);
657 if (!$error && empty($this->fac_rec) && count($this->lines) && is_object($this->lines[0])) {
658 dol_syslog(
"There is ".count($this->lines).
" lines that are invoice lines objects");
659 foreach ($this->lines as $i => $val) {
660 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facture_fourn_det (fk_facture_fourn, special_code, fk_remise_except)';
661 $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').
')';
663 $resql_insert = $this->db->query($sql);
665 $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn_det');
668 $line_price_base_type = $this->lines[$i]->getPriceBaseType();
669 $line_pu = ($line_price_base_type ===
'TTC') ? (
float) $this->lines[$i]->subprice_ttc : $this->lines[$i]->subprice;
672 $this->lines[$i]->desc ? $this->lines[$i]->desc : $this->lines[$i]->description,
674 $this->lines[$i]->tva_tx.($this->lines[$i]->vat_src_code ?
' ('.$this->lines[$i]->vat_src_code.
')' :
''),
675 $this->lines[$i]->localtax1_tx,
676 $this->lines[$i]->localtax2_tx,
677 $this->lines[$i]->qty,
678 $this->lines[$i]->fk_product,
679 $line_price_base_type,
680 (!empty($this->lines[$i]->info_bits) ? $this->lines[$i]->info_bits :
''),
681 $this->lines[$i]->product_type,
682 $this->lines[$i]->remise_percent,
684 $this->lines[$i]->date_start,
685 $this->lines[$i]->date_end,
686 $this->lines[$i]->array_options,
687 $this->lines[$i]->fk_unit,
688 $this->lines[$i]->multicurrency_subprice,
689 $this->lines[$i]->ref_supplier
692 $this->error = $this->db->lasterror();
693 $this->db->rollback();
697 } elseif (!$error && empty($this->fac_rec)) {
698 dol_syslog(
"There is ".count($this->lines).
" lines that are array lines");
699 foreach ($this->lines as $i => $val) {
700 $line = $this->lines[$i];
704 if (!is_object($line)) {
705 $line = (object) $line;
708 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facture_fourn_det (fk_facture_fourn, special_code, fk_remise_except)';
709 $sql .=
" VALUES (".((int) $this->
id).
", ".((int) $this->lines[$i]->special_code).
", ".($this->lines[$i]->fk_remise_except > 0 ? ((int) $this->lines[$i]->fk_remise_except) :
'NULL').
')';
711 $resql_insert = $this->db->query($sql);
713 $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn_det');
717 $line->desc ? $line->desc : $line->description,
725 (!empty($line->info_bits) ? $line->info_bits :
''),
727 $line->remise_percent,
731 $line->array_options,
733 $line->multicurrency_subprice,
737 $this->error = $this->db->lasterror();
738 $this->db->rollback();
748 foreach ($_facrec->lines as $i => $val) {
749 $product_type = $_facrec->lines[$i]->product_type;
750 if ($_facrec->lines[$i]->fk_product) {
751 $prod =
new Product($this->db);
752 $res = $prod->fetch($_facrec->lines[$i]->fk_product);
763 $tva_tx = $_facrec->lines[$i]->tva_tx . ($_facrec->lines[$i]->vat_src_code ?
'(' . $_facrec->lines[$i]->vat_src_code .
')' :
'');
764 $tva_npr = $_facrec->lines[$i]->info_bits;
765 if (empty($tva_tx)) {
768 $localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
769 $localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
773 $buyprice = empty($_facrec->lines[$i]->buyprice) ? 0 : $_facrec->lines[$i]->buyprice;
776 if (! $buyprice && $_facrec->lines[$i]->fk_product > 0) {
777 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.product.class.php';
779 $producttmp->fetch($_facrec->lines[$i]->fk_product);
784 if (
getDolGlobalString(
'MARGIN_TYPE') ==
'costprice' && !empty($producttmp->cost_price)) {
785 $buyprice = $producttmp->cost_price;
787 $buyprice = $producttmp->pmp;
789 if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0) {
790 if ($producttmp->product_fourn_price_id > 0) {
791 $buyprice =
price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent / 100) + $producttmp->fourn_remise,
'MU');
797 $result_insert = $this->
addline(
798 $_facrec->lines[$i]->desc ? $_facrec->lines[$i]->desc : $_facrec->lines[$i]->description,
799 $_facrec->lines[$i]->pu_ht,
803 $_facrec->lines[$i]->qty,
804 $_facrec->lines[$i]->fk_product,
805 $_facrec->lines[$i]->remise_percent,
806 ($_facrec->lines[$i]->date_start == 1 && $this->date) ? $this->
date :
'',
807 ($_facrec->lines[$i]->date_end == 1 && $previousdaynextdatewhen) ? $previousdaynextdatewhen :
'',
809 $_facrec->lines[$i]->info_bits,
812 $_facrec->lines[$i]->rang,
814 $_facrec->lines[$i]->array_options,
815 $_facrec->lines[$i]->fk_unit,
818 $_facrec->lines[$i]->ref_supplier,
819 $_facrec->lines[$i]->special_code,
823 if ($result_insert < 0) {
825 $this->error = $this->db->error();
845 $result = $this->call_trigger(
'BILL_SUPPLIER_CREATE', $user);
856 $this->db->rollback();
860 $this->error = $langs->trans(
'FailedToUpdatePrice');
861 $this->db->rollback();
865 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
866 $this->error = $langs->trans(
'ErrorRefAlreadyExists');
867 $this->db->rollback();
870 $this->error = $this->db->lasterror();
871 $this->db->rollback();
885 public function fetch($id = 0, $ref =
'', $ref_ext =
'')
887 if (empty($id) && empty($ref) && empty($ref_ext)) {
894 $sql .=
" t.ref_supplier,";
895 $sql .=
" t.ref_ext,";
896 $sql .=
" t.entity,";
898 $sql .=
" t.subtype,";
899 $sql .=
" t.fk_soc,";
902 $sql .=
" t.date_pointoftax,";
903 $sql .=
" t.tms as datem,";
904 $sql .=
" t.libelle as label,";
905 $sql .=
" t.paye as paid,";
906 $sql .=
" t.close_code,";
907 $sql .=
" t.close_note,";
909 $sql .=
" t.localtax1,";
910 $sql .=
" t.localtax2,";
911 $sql .=
" t.total_ht,";
912 $sql .=
" t.total_tva,";
913 $sql .=
" t.total_ttc,";
914 $sql .=
" t.fk_statut as status,";
915 $sql .=
" t.fk_user_author,";
916 $sql .=
" t.fk_user_valid,";
917 $sql .=
" t.fk_facture_source,";
918 $sql .=
" t.vat_reverse_charge,";
919 $sql .=
" t.fk_fac_rec_source,";
920 $sql .=
" t.fk_projet as fk_project,";
921 $sql .=
" t.fk_cond_reglement,";
922 $sql .=
" t.fk_account,";
923 $sql .=
" t.fk_mode_reglement,";
924 $sql .=
" t.date_lim_reglement,";
925 $sql .=
" t.note_private,";
926 $sql .=
" t.note_public,";
927 $sql .=
" t.model_pdf,";
928 $sql .=
" t.last_main_doc,";
929 $sql .=
" t.import_key,";
930 $sql .=
" t.extraparams,";
931 $sql .=
" cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc,";
932 $sql .=
" p.code as mode_reglement_code, p.libelle as mode_reglement_label,";
933 $sql .=
' s.nom as socnom, s.rowid as socid,';
934 $sql .=
' t.fk_incoterms, t.location_incoterms,';
935 $sql .=
" i.libelle as label_incoterms,";
936 $sql .=
' t.fk_transport_mode,';
937 $sql .=
' t.fk_multicurrency, t.multicurrency_code, t.multicurrency_tx, t.multicurrency_total_ht, t.multicurrency_total_tva, t.multicurrency_total_ttc';
938 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as t';
939 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON (t.fk_soc = s.rowid)";
940 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_payment_term as cr ON t.fk_cond_reglement = cr.rowid";
941 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as p ON t.fk_mode_reglement = p.id";
942 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON t.fk_incoterms = i.rowid';
944 $sql .=
" WHERE t.rowid = ".((int) $id);
946 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_invoice').
')';
948 $sql .=
" AND t.ref = '".$this->db->escape($ref).
"'";
951 $sql .=
" AND t.ref_ext = '".$this->db->escape($ref_ext).
"'";
955 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
956 $resql = $this->db->query($sql);
958 if ($this->db->num_rows($resql)) {
959 $obj = $this->db->fetch_object($resql);
961 $this->
id = $obj->rowid;
962 $this->
ref = $obj->ref ? $obj->ref : $obj->rowid;
964 $this->ref_supplier = $obj->ref_supplier;
965 $this->ref_ext = $obj->ref_ext;
966 $this->entity = $obj->entity;
968 $this->subtype = $obj->subtype;
969 $this->socid = $obj->fk_soc;
970 $this->
date = $this->db->jdate($obj->datef);
971 $this->date_pointoftax = $this->db->jdate($obj->date_pointoftax);
972 $this->date_creation = $this->db->jdate($obj->datec);
973 $this->datec = $this->db->jdate($obj->datec);
974 $this->date_modification = $this->db->jdate($obj->datem);
975 $this->tms = $this->db->jdate($obj->datem);
976 $this->libelle = $obj->label;
977 $this->label = $obj->label;
978 $this->paye = $obj->paid;
979 $this->paid = $obj->paid;
980 $this->close_code = $obj->close_code;
981 $this->close_note = $obj->close_note;
982 $this->total_localtax1 = $obj->localtax1;
983 $this->total_localtax2 = $obj->localtax2;
984 $this->total_ht = $obj->total_ht;
985 $this->total_tva = $obj->total_tva;
986 $this->total_ttc = $obj->total_ttc;
987 $this->
status = $obj->status;
988 $this->statut = $obj->status;
989 $this->fk_statut = $obj->status;
990 $this->user_creation_id = $obj->fk_user_author;
991 $this->author = $obj->fk_user_author;
992 $this->user_validation_id = $obj->fk_user_valid;
993 $this->fk_user_valid = $obj->fk_user_valid;
994 $this->fk_facture_source = $obj->fk_facture_source;
995 $this->vat_reverse_charge = empty($obj->vat_reverse_charge) ? 0 : 1;
996 $this->fk_fac_rec_source = $obj->fk_fac_rec_source;
997 $this->fk_project = $obj->fk_project;
998 $this->cond_reglement_id = $obj->fk_cond_reglement;
999 $this->cond_reglement_code = $obj->cond_reglement_code;
1000 $this->cond_reglement_label = $obj->cond_reglement_label;
1001 $this->cond_reglement_doc = $obj->cond_reglement_doc;
1002 $this->fk_account = $obj->fk_account;
1003 $this->mode_reglement_id = $obj->fk_mode_reglement;
1004 $this->mode_reglement_code = $obj->mode_reglement_code;
1005 $this->mode_reglement = $obj->mode_reglement_label;
1006 $this->date_echeance = $this->db->jdate($obj->date_lim_reglement);
1007 $this->note = $obj->note_private;
1008 $this->note_private = $obj->note_private;
1009 $this->note_public = $obj->note_public;
1010 $this->model_pdf = $obj->model_pdf;
1011 $this->last_main_doc = $obj->last_main_doc;
1012 $this->import_key = $obj->import_key;
1015 $this->fk_incoterms = $obj->fk_incoterms;
1016 $this->location_incoterms = $obj->location_incoterms;
1017 $this->label_incoterms = $obj->label_incoterms;
1018 $this->transport_mode_id = $obj->fk_transport_mode;
1021 $this->fk_multicurrency = $obj->fk_multicurrency;
1022 $this->multicurrency_code = $obj->multicurrency_code;
1023 $this->multicurrency_tx = $obj->multicurrency_tx;
1024 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1025 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1026 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1028 $this->extraparams = isset($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
1030 $this->socid = $obj->socid;
1032 $this->thirdparty =
null;
1040 $this->error = $this->db->lasterror();
1044 $this->error =
'Bill with id '.$id.
' not found';
1045 dol_syslog(get_class($this).
'::fetch '.$this->error);
1049 $this->db->free($resql);
1052 $this->error =
"Error ".$this->db->lasterror();
1067 $this->lines = array();
1069 $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';
1070 $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';
1071 $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';
1072 $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';
1073 $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';
1074 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn_det as f';
1075 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON f.fk_product = p.rowid';
1076 $sql .=
' WHERE fk_facture_fourn='.((int) $this->
id);
1077 $sql .=
' ORDER BY f.rang, f.rowid';
1079 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
1081 $resql_rows = $this->db->query($sql);
1083 $num_rows = $this->db->num_rows($resql_rows);
1086 while ($i < $num_rows) {
1087 $obj = $this->db->fetch_object($resql_rows);
1091 $line->id = $obj->rowid;
1092 $line->rowid = $obj->rowid;
1094 $line->description = $obj->line_desc;
1095 $line->desc = $obj->line_desc;
1096 $line->date_start = $this->db->jdate($obj->date_start);
1097 $line->date_end = $this->db->jdate($obj->date_end);
1099 $line->product_ref = $obj->product_ref;
1100 $line->ref = $obj->product_ref;
1101 $line->ref_supplier = $obj->ref_supplier;
1102 $line->libelle = $obj->label;
1103 $line->label = $obj->label;
1104 $line->product_barcode = $obj->product_barcode;
1105 $line->product_desc = $obj->product_desc;
1106 $line->subprice = $obj->pu_ht;
1107 $line->pu_ht = $obj->pu_ht;
1108 $line->subprice_ttc = $obj->pu_ttc;
1109 $line->pu_ttc = $obj->pu_ttc;
1110 $line->vat_src_code = $obj->vat_src_code;
1111 $line->tva_tx = $obj->tva_tx;
1112 $line->localtax1_tx = $obj->localtax1_tx;
1113 $line->localtax2_tx = $obj->localtax2_tx;
1114 $line->localtax1_type = $obj->localtax1_type;
1115 $line->localtax2_type = $obj->localtax2_type;
1116 $line->qty = $obj->qty;
1117 $line->remise_percent = $obj->remise_percent;
1118 $line->fk_remise_except = $obj->fk_remise_except;
1120 $line->total_ht = $obj->total_ht;
1121 $line->total_ttc = $obj->total_ttc;
1122 $line->total_tva = $obj->total_tva;
1123 $line->total_localtax1 = $obj->total_localtax1;
1124 $line->total_localtax2 = $obj->total_localtax2;
1125 $line->fk_facture_fourn = $obj->fk_facture_fourn;
1126 $line->fk_product = $obj->fk_product;
1127 $line->product_type = $obj->product_type;
1128 $line->product_label = $obj->label;
1129 $line->info_bits = $obj->info_bits;
1130 $line->fk_parent_line = $obj->fk_parent_line;
1131 $line->special_code = $obj->special_code;
1132 $line->rang = $obj->rang;
1133 $line->fk_unit = $obj->fk_unit;
1135 $line->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
1138 $line->fk_code_ventilation = $obj->fk_code_ventilation;
1141 $line->fk_multicurrency = $obj->fk_multicurrency;
1142 $line->multicurrency_code = $obj->multicurrency_code;
1143 $line->multicurrency_subprice = $obj->multicurrency_subprice;
1144 $line->multicurrency_total_ht = $obj->multicurrency_total_ht;
1145 $line->multicurrency_total_tva = $obj->multicurrency_total_tva;
1146 $line->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1149 $line->fetch_optionals();
1151 $this->lines[$i] = $line;
1156 $this->db->free($resql_rows);
1159 $this->error = $this->db->error();
1160 dol_syslog(get_class($this).
"::fetch_lines - No lines:{$this->error} Error:{$this->error}", LOG_DEBUG);
1173 public function update($user =
null, $notrigger = 0)
1179 if (empty($this->
type)) {
1182 if (isset($this->
ref)) {
1183 $this->
ref = trim($this->
ref);
1185 if (isset($this->ref_supplier)) {
1186 $this->ref_supplier = trim($this->ref_supplier);
1188 if (isset($this->ref_ext)) {
1189 $this->ref_ext = trim($this->ref_ext);
1191 if (isset($this->entity)) {
1192 $this->entity = (int) $this->entity;
1194 if (isset($this->
type)) {
1197 if (isset($this->subtype)) {
1198 $this->subtype = (int) $this->subtype;
1200 if (isset($this->socid)) {
1201 $this->socid = (int) $this->socid;
1203 if (isset($this->label)) {
1204 $this->label = trim($this->label);
1206 if (isset($this->paid)) {
1207 $this->paid = (int) (
bool) $this->paid;
1208 $this->paye = $this->paid;
1209 } elseif (isset($this->paye)) {
1210 $this->paid = (int) (
bool) $this->paye;
1211 $this->paye = $this->paid;
1213 if (isset($this->close_code)) {
1214 $this->close_code = trim($this->close_code);
1216 if (isset($this->close_note)) {
1217 $this->close_note = trim($this->close_note);
1219 if (empty($this->total_ht)) {
1220 $this->total_ht = 0;
1222 if (empty($this->total_tva)) {
1223 $this->total_tva = 0;
1225 if (empty($this->total_localtax1)) {
1226 $this->total_localtax1 = 0;
1228 if (empty($this->total_localtax2)) {
1229 $this->total_localtax2 = 0;
1231 if (isset($this->total_ttc)) {
1232 $this->total_ttc = (float) $this->total_ttc;
1234 if (isset($this->
status)) {
1236 $this->statut = $this->status;
1237 } elseif (isset($this->statut)) {
1238 $this->
status = (int) $this->statut;
1239 $this->statut = $this->status;
1241 if (isset($this->author)) {
1242 $this->author = (int) $this->author;
1244 if (isset($this->fk_user_valid)) {
1245 $this->fk_user_valid = (int) $this->fk_user_valid;
1247 if (isset($this->fk_facture_source)) {
1248 $this->fk_facture_source = (int) $this->fk_facture_source;
1250 if (isset($this->fk_project)) {
1251 if (empty($this->fk_project)) {
1252 $this->fk_project = 0;
1254 $this->fk_project = (int) $this->fk_project;
1257 if (isset($this->mode_reglement_id)) {
1258 $this->mode_reglement_id = (int) $this->mode_reglement_id;
1260 if (isset($this->cond_reglement_id)) {
1261 $this->cond_reglement_id = (int) $this->cond_reglement_id;
1263 if (isset($this->note_private)) {
1264 $this->note_private = trim($this->note_private);
1265 $this->note = $this->note_private;
1267 if (isset($this->note_public)) {
1268 $this->note_public = trim($this->note_public);
1270 if (isset($this->model_pdf)) {
1271 $this->model_pdf = trim($this->model_pdf);
1273 if (isset($this->import_key)) {
1274 $this->import_key = trim($this->import_key);
1281 if (
dol_strlen((
string) $this->date_modification) == 0) {
1287 dol_syslog(get_class($this).
"::update ".$this->error, LOG_ERR);
1292 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn SET";
1293 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1294 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1295 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1296 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) :
"null").
",";
1297 $sql .=
" type=".(isset($this->
type) ? ((int) $this->
type) :
"null").
",";
1298 $sql .=
" subtype=".(isset($this->subtype) ? (int) $this->subtype :
"null").
",";
1299 $sql .=
" fk_soc=".(isset($this->socid) ? ((int) $this->socid) :
"null").
",";
1300 $sql .=
" datec=".(dol_strlen((
string) $this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
1301 $sql .=
" datef=".(dol_strlen((
string) $this->date) != 0 ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
1302 $sql .=
" date_pointoftax=".(dol_strlen((
string) $this->date_pointoftax) != 0 ?
"'".$this->db->idate($this->date_pointoftax).
"'" :
'null').
",";
1303 if (
dol_strlen((
string) $this->date_modification) != 0) {
1304 $sql .=
" tms=".(dol_strlen((
string) $this->date_modification) != 0 ?
"'".$this->db->idate($this->date_modification).
"'" :
'null').
",";
1305 } elseif (
dol_strlen((
string) $this->tms) != 0) {
1306 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
1308 $sql .=
" libelle=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
1309 $sql .=
" paye=".(isset($this->paid) ? ((int) $this->paid) :
"0").
",";
1310 $sql .=
" close_code=".(isset($this->close_code) ?
"'".$this->db->escape($this->close_code).
"'" :
"null").
",";
1311 $sql .=
" close_note=".(isset($this->close_note) ?
"'".$this->db->escape($this->close_note).
"'" :
"null").
",";
1312 $sql .=
" localtax1=".(isset($this->total_localtax1) ? ((float) $this->total_localtax1) :
"null").
",";
1313 $sql .=
" localtax2=".(isset($this->total_localtax2) ? ((float) $this->total_localtax2) :
"null").
",";
1314 $sql .=
" total_ht=".(isset($this->total_ht) ? ((float) $this->total_ht) :
"null").
",";
1315 $sql .=
" total_tva=".(isset($this->total_tva) ? ((float) $this->total_tva) :
"null").
",";
1316 $sql .=
" total_ttc=".(isset($this->total_ttc) ? ((float) $this->total_ttc) :
"null").
",";
1317 $sql .=
" fk_statut=".(isset($this->
status) ? ((int) $this->
status) : (isset($this->statut) ? ((int) $this->statut) :
"null")).
",";
1318 $sql .=
" fk_user_author=".(isset($this->author) ? ((int) $this->author) :
"null").
",";
1319 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? ((int) $this->fk_user_valid) :
"null").
",";
1320 $sql .=
" fk_facture_source=".($this->fk_facture_source ? ((int) $this->fk_facture_source) :
"null").
",";
1321 $sql .=
" vat_reverse_charge = ".($this->vat_reverse_charge !=
'' ? ((int) $this->vat_reverse_charge) : 0).
",";
1322 $sql .=
" fk_projet=".(!empty($this->fk_project) ? ((int) $this->fk_project) :
"null").
",";
1323 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? ((int) $this->mode_reglement_id) :
"null").
",";
1324 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? ((int) $this->cond_reglement_id) :
"null").
",";
1325 $sql .=
" date_lim_reglement=".(dol_strlen((
string) $this->date_echeance) != 0 ?
"'".$this->db->idate($this->date_echeance).
"'" :
'null').
",";
1326 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1327 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1328 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1329 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
1330 $sql .=
" WHERE rowid=".((int) $this->
id);
1334 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1335 $resql = $this->db->query($sql);
1340 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1341 $this->errors[] = $langs->trans(
'ErrorRefAlreadyExists');
1343 $this->errors[] =
"Error ".$this->db->lasterror();
1354 if (!$error && !$notrigger) {
1356 $result = $this->call_trigger(
'BILL_SUPPLIER_MODIFY', $user);
1365 foreach ($this->errors as $errmsg) {
1366 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1367 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1369 $this->db->rollback();
1372 $this->db->commit();
1389 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1390 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1395 $result = $remise->fetch($idremise);
1398 if ($this->socid > 0 && $remise->fk_soc != $this->socid) {
1399 $this->error = $langs->trans(
"ErrorDiscountNotSameCompany");
1400 $this->db->rollback();
1403 if ($remise->fk_invoice_supplier) {
1404 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
1405 $this->db->rollback();
1410 $facligne->fk_facture_fourn = $this->id;
1411 $facligne->fk_remise_except = $remise->id;
1412 $facligne->desc = $remise->description;
1413 $facligne->vat_src_code = $remise->vat_src_code;
1414 $facligne->tva_tx = $remise->tva_tx;
1415 $facligne->localtax1_tx = $remise->localtax1_tx;
1416 $facligne->localtax1_type = $remise->localtax1_type;
1417 $facligne->localtax2_tx = $remise->localtax1_tx;
1418 $facligne->localtax2_type = $remise->localtax1_type;
1419 $facligne->subprice = -(float) $remise->amount_ht;
1420 $facligne->fk_product = 0;
1421 $facligne->product_type = 0;
1423 $facligne->remise_percent = 0;
1424 $facligne->rang = -1;
1425 $facligne->info_bits = 2;
1428 $facligne->rang = 1;
1429 $linecount = count($this->lines);
1430 for ($ii = 1; $ii <= $linecount; $ii++) {
1436 if ($remise->fk_invoice_supplier_source > 0) {
1438 $srcinvoice->fetch($remise->fk_invoice_supplier_source);
1439 $totalcostpriceofinvoice = 0;
1440 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmargin.class.php';
1442 $arraytmp = $formmargin->getMarginInfosArray($srcinvoice,
false);
1443 $facligne->pa_ht = $arraytmp[
'pa_total'];
1446 $facligne->total_ht = -(float) $remise->amount_ht;
1447 $facligne->total_tva = -(float) $remise->amount_tva;
1448 $facligne->total_ttc = -(float) $remise->amount_ttc;
1449 $facligne->total_localtax1 = -(float) $remise->total_localtax1;
1450 $facligne->total_localtax2 = -(float) $remise->total_localtax2;
1453 $facligne->fk_multicurrency = $this->fk_multicurrency;
1454 $facligne->multicurrency_code = $this->multicurrency_code;
1455 $facligne->multicurrency_subprice = -$remise->multicurrency_subprice;
1456 $facligne->multicurrency_total_ht = -$remise->multicurrency_total_ht;
1457 $facligne->multicurrency_total_tva = -$remise->multicurrency_total_tva;
1458 $facligne->multicurrency_total_ttc = -$remise->multicurrency_total_ttc;
1460 $lineid = $facligne->insert();
1465 $result = $remise->link_to_invoice($lineid, 0);
1467 $this->error = $remise->error;
1468 $this->db->rollback();
1472 $this->db->commit();
1475 $this->error = $facligne->error;
1476 $this->db->rollback();
1480 $this->error = $facligne->error;
1481 $this->db->rollback();
1485 $this->db->rollback();
1498 public function delete(
User $user, $notrigger = 0)
1504 dol_syslog(
"FactureFournisseur::delete rowid=".$rowid, LOG_DEBUG);
1509 dol_syslog(get_class($this).
"::delete refused, invoice is not erasable (code ".$result.
")", LOG_DEBUG);
1518 $result = $this->call_trigger(
'BILL_SUPPLIER_DELETE', $user);
1520 $this->db->rollback();
1527 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"categorie_supplier_invoice";
1528 $sql .=
" WHERE fk_supplier_invoice = ".((int) $this->
id);
1530 $result = $this->db->query($sql);
1533 $this->error = $this->db->lasterror();
1534 $this->errors[] = $this->error;
1539 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'societe_remise_except';
1540 $sql .=
' WHERE fk_invoice_supplier_source = '.((int) $rowid);
1541 $sql .=
' AND fk_invoice_supplier_line IS NULL';
1542 $resql = $this->db->query($sql);
1546 $list_rowid_det = array();
1547 foreach ($this->lines as $key => $invoiceline) {
1548 $list_rowid_det[] = $invoiceline->id;
1552 if (count($list_rowid_det)) {
1553 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
1554 $sql .=
' SET fk_invoice_supplier = NULL, fk_invoice_supplier_line = NULL';
1555 $sql .=
' WHERE fk_invoice_supplier_line IN ('.$this->db->sanitize(implode(
',', $list_rowid_det)).
')';
1557 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1558 if (!$this->db->query($sql)) {
1565 $sql_main_table = MAIN_DB_PREFIX.
'facture_fourn_det';
1566 $sql_ef_table = $sql_main_table.
"_extrafields";
1567 $sqlef =
"DELETE FROM $sql_ef_table WHERE fk_object IN (SELECT rowid FROM ".$sql_main_table.
" WHERE fk_facture_fourn = ".((int) $rowid).
")";
1568 $resqlef = $this->db->query($sqlef);
1569 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn_det WHERE fk_facture_fourn = '.((int) $rowid);
1570 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1571 $resql = $this->db->query($sql);
1572 if ($resqlef && $resql) {
1573 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn WHERE rowid = '.((int) $rowid);
1574 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1575 $resql2 = $this->db->query($sql);
1599 if ($conf->fournisseur->facture->dir_output && !empty($ref)) {
1600 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1602 $dir =
$conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$ref;
1603 $file = $dir.
"/".$ref.
".pdf";
1604 if (file_exists($file)) {
1606 $this->error =
'ErrorFailToDeleteFile';
1610 if (file_exists($dir)) {
1614 $this->error =
'ErrorFailToDeleteDir';
1626 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
1631 dol_syslog(get_class($this).
"::delete $this->id by $user->id", LOG_DEBUG);
1632 $this->db->commit();
1635 $this->error = $this->db->lasterror();
1636 $this->db->rollback();
1653 public function set_paid($user, $close_code =
'', $close_note =
'')
1656 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
1657 return $this->
setPaid($user, $close_code, $close_note);
1668 public function setPaid($user, $close_code =
'', $close_note =
'')
1672 if ($this->paid != 1) {
1677 dol_syslog(
"FactureFournisseur::setPaid", LOG_DEBUG);
1679 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn SET';
1680 $sql .=
' fk_statut = '.self::STATUS_CLOSED;
1685 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
1688 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
1690 $sql .=
', fk_user_closing = '.((int) $user->id);
1691 $sql .=
", date_closing = '".$this->db->idate($now).
"'";
1692 $sql .=
' WHERE rowid = '.((int) $this->
id);
1694 $resql = $this->db->query($sql);
1697 $result = $this->call_trigger(
'BILL_SUPPLIER_PAYED', $user);
1704 $this->error = $this->db->error();
1705 dol_print_error($this->db);
1709 $this->db->commit();
1712 $this->db->rollback();
1734 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
1752 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn';
1753 $sql .=
' SET paye=0, fk_statut='.self::STATUS_VALIDATED.
', close_code=null, close_note=null,';
1754 $sql .=
' date_closing=null,';
1755 $sql .=
' fk_user_closing=null';
1756 $sql .=
' WHERE rowid = '.((int) $this->
id);
1758 dol_syslog(get_class($this).
"::set_unpaid", LOG_DEBUG);
1759 $resql = $this->db->query($sql);
1762 $result = $this->call_trigger(
'BILL_SUPPLIER_UNPAYED', $user);
1769 $this->error = $this->db->error();
1770 dol_print_error($this->db);
1774 $this->db->commit();
1777 $this->db->rollback();
1792 public function setCanceled($user, $close_code =
'', $close_note =
'')
1794 dol_syslog(get_class($this).
"::setCanceled rowid=".((
int) $this->
id), LOG_DEBUG);
1798 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn SET';
1799 $sql .=
' fk_statut='.self::STATUS_ABANDONED;
1801 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
1804 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
1806 $sql .=
" WHERE rowid = ".((int) $this->
id);
1808 $resql = $this->db->query($sql);
1812 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
1813 $sql .=
' SET fk_invoice_supplier = NULL';
1814 $sql .=
' WHERE fk_invoice_supplier = '.((int) $this->
id);
1816 $resql = $this->db->query($sql);
1819 $result = $this->call_trigger(
'BILL_SUPPLIER_CANCEL', $user);
1821 $this->db->rollback();
1826 $this->db->commit();
1829 $this->error = $this->db->error().
" sql=".$sql;
1830 $this->db->rollback();
1834 $this->error = $this->db->error().
" sql=".$sql;
1835 $this->db->rollback();
1849 public function validate($user, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
1853 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1858 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number.
', idwarehouse='.$idwarehouse);
1865 if ($this->
status > self::STATUS_DRAFT) {
1866 dol_syslog(get_class($this).
"::validate no draft status", LOG_WARNING);
1869 if (preg_match(
'/^'.preg_quote($langs->trans(
"CopyOf").
' ',
'/').
'/', $this->ref_supplier)) {
1870 $langs->load(
"errors");
1871 $this->error = $langs->trans(
"ErrorFieldFormat", $langs->transnoentities(
"RefSupplier")).
'. '.$langs->trans(
'RemoveString', $langs->transnoentitiesnoconv(
"CopyOf"));
1874 if (count($this->lines) <= 0) {
1875 $langs->load(
"errors");
1876 $this->error = $langs->trans(
"ErrorObjectMustHaveLinesToBeValidated", $this->
ref);
1881 if (!empty($this->thirdparty) && is_object($this->thirdparty)) {
1882 $array_to_check = array(
'IDPROF1',
'IDPROF2',
'IDPROF3',
'IDPROF4',
'IDPROF5',
'IDPROF6',
'EMAIL',
'ACCOUNTANCY_CODE_SUPPLIER');
1883 foreach ($array_to_check as $key) {
1884 $keymin = strtolower($key);
1885 if ($keymin ==
'accountancy_code_supplier') {
1886 $keymin =
'code_compta_fournisseur';
1888 if (!property_exists($this->thirdparty, $keymin)) {
1891 $vallabel = $this->thirdparty->$keymin;
1893 $i = (int) preg_replace(
'/[^0-9]/',
'', $key);
1895 if ($this->thirdparty->isACompany()) {
1897 if (
$mysoc->country_id > 0 && $this->thirdparty->country_id ==
$mysoc->country_id) {
1898 $idprof_mandatory =
'SOCIETE_'.$key.
'_INVOICE_MANDATORY';
1900 $langs->load(
"errors");
1901 $this->error = $langs->trans(
'ErrorProdIdIsMandatory', $langs->transcountry(
'ProfId'.$i, $this->thirdparty->country_code)).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
1902 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
1908 if ($key ==
'EMAIL') {
1911 $langs->load(
"errors");
1912 $this->error = $langs->trans(
"ErrorBadEMail", $this->thirdparty->email).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
1913 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
1916 } elseif ($key ==
'ACCOUNTANCY_CODE_SUPPLIER') {
1918 if (
getDolGlobalString(
'SOCIETE_ACCOUNTANCY_CODE_SUPPLIER_INVOICE_MANDATORY') && empty($this->thirdparty->code_compta_fournisseur)) {
1919 $langs->load(
"errors");
1920 $this->error = $langs->trans(
"ErrorAccountancyCodeSupplierIsMandatory", $this->thirdparty->name).
' ('.$langs->trans(
"ForbiddenBySetupRules").
')';
1921 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
1932 if ($force_number) {
1933 $num = $force_number;
1934 } elseif (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
1937 $num = (string) $this->
ref;
1941 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn";
1942 $sql .=
" SET ref='".$this->db->escape($num).
"', fk_statut = 1, fk_user_valid = ".((int) $user->id).
", date_valid = '".$this->db->idate($now).
"'";
1943 $sql .=
" WHERE rowid = ".((int) $this->
id);
1945 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
1946 $resql = $this->db->query($sql);
1950 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1951 $langs->load(
"agenda");
1953 $cpt = count($this->lines);
1954 for ($i = 0; $i < $cpt; $i++) {
1955 if ($this->lines[$i]->fk_product > 0) {
1957 $mouvP->origin = &$this;
1958 $mouvP->setOrigin($this->element, $this->
id);
1960 $up_ht_disc = $this->lines[$i]->subprice;
1961 if (!empty($this->lines[$i]->remise_percent) && !
getDolGlobalString(
'STOCK_EXCLUDE_DISCOUNT_FOR_PMP')) {
1962 $up_ht_disc =
price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100,
'MU');
1965 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->transnoentitiesnoconv(
"InvoiceValidatedInDolibarr", $num));
1967 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->transnoentitiesnoconv(
"InvoiceValidatedInDolibarr", $num));
1978 if (empty($notrigger)) {
1980 $result = $this->call_trigger(
'BILL_SUPPLIER_VALIDATE', $user);
1988 $this->oldref = $this->ref;
1991 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
1993 $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).
"'";
1994 $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);
1995 $resql = $this->db->query($sql);
1998 $this->error = $this->db->lasterror();
2000 $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).
"'";
2001 $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);
2002 $resql = $this->db->query($sql);
2005 $this->error = $this->db->lasterror();
2010 $dirsource =
$conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$oldref;
2011 $dirdest =
$conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$this->newref;
2012 if (!$error && file_exists($dirsource)) {
2013 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
2015 if (@rename($dirsource, $dirdest)) {
2018 $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,
'/'));
2019 foreach ($listoffiles as $fileentry) {
2020 $dirsource = $fileentry[
'name'];
2021 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $this->newref, $dirsource);
2022 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
2023 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
2024 @rename($dirsource, $dirdest);
2033 $this->
ref = $this->newref;
2040 $this->db->commit();
2043 $this->db->rollback();
2047 $this->error = $this->db->error();
2048 $this->db->rollback();
2061 public function setDraft($user, $idwarehouse = -1, $notrigger = 0)
2068 if ($this->
status == self::STATUS_DRAFT) {
2069 dol_syslog(__METHOD__.
" already draft status", LOG_WARNING);
2077 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn";
2078 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT;
2079 $sql .=
" WHERE rowid = ".((int) $this->
id);
2081 $result = $this->db->query($sql);
2083 $this->oldcopy = clone $this;
2087 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2088 $langs->load(
"agenda");
2090 $cpt = count($this->lines);
2091 for ($i = 0; $i < $cpt; $i++) {
2092 if ($this->lines[$i]->fk_product > 0) {
2094 $mouvP->origin = &$this;
2095 $mouvP->setOrigin($this->element, $this->
id);
2098 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->transnoentitiesnoconv(
"InvoiceBackToDraftInDolibarr", $this->ref));
2100 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->transnoentitiesnoconv(
"InvoiceBackToDraftInDolibarr", $this->ref));
2105 dol_syslog(__METHOD__.
" stock movement failed for line ".$i.
": ".$mouvP->error, LOG_ERR);
2112 if (!$error && empty($notrigger)) {
2114 $result = $this->call_trigger(
'BILL_SUPPLIER_UNVALIDATE', $user);
2121 $this->db->commit();
2124 $this->db->rollback();
2128 $this->error = $this->db->error();
2129 $this->db->rollback();
2169 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 =
'')
2173 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);
2174 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2176 if ($this->
status == self::STATUS_DRAFT) {
2178 if (empty($remise_percent)) {
2179 $remise_percent = 0;
2184 if (empty($info_bits)) {
2190 if (empty($fk_code_ventilation)) {
2191 $fk_code_ventilation = 0;
2193 if (empty($txtva)) {
2196 if (empty($txlocaltax1)) {
2199 if (empty($txlocaltax2)) {
2203 $remise_percent =
price2num($remise_percent);
2206 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
2213 $langs->load(
"errors");
2214 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2220 if ($fk_product > 0) {
2221 if (
getDolGlobalInt(
'SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY') == 1) {
2223 dol_syslog(get_class($this).
"::addline we check supplier prices fk_product=".$fk_product.
" qty=".$qty.
" ref_supplier=".$ref_supplier);
2225 if ($prod->fetch($fk_product) > 0) {
2226 $product_type = $prod->type;
2227 $label = $prod->label;
2228 $fk_prod_fourn_price = 0;
2232 $result = $prod->get_buyprice($fk_prod_fourn_price, (
float) $qty, $fk_product,
'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
2235 $pu = $prod->fourn_pu;
2237 $ref_supplier = $prod->ref_supplier;
2239 if ($remise_percent == 0 && $prod->remise_percent != 0) {
2240 $remise_percent = $prod->remise_percent;
2244 $langs->load(
"errors");
2245 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2246 $this->db->rollback();
2247 dol_syslog(get_class($this).
"::addline we did not found supplier price, so we can't guess unit price");
2252 if ($result == -1) {
2253 $langs->load(
"errors");
2254 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2255 $this->db->rollback();
2256 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_DEBUG);
2260 $this->error = $prod->error;
2261 $this->db->rollback();
2262 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_ERR);
2266 $this->error = $prod->error;
2267 $this->db->rollback();
2272 $product_type = $type;
2284 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
2285 $vat_src_code = $reg[1];
2286 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
2293 $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, $this->multicurrency_code);
2294 $total_ht = $tabprice[0];
2295 $total_tva = $tabprice[1];
2296 $total_ttc = $tabprice[2];
2297 $total_localtax1 = $tabprice[9];
2298 $total_localtax2 = $tabprice[10];
2299 $pu_ht = $tabprice[3];
2302 $multicurrency_total_ht = $tabprice[16];
2303 $multicurrency_total_tva = $tabprice[17];
2304 $multicurrency_total_ttc = $tabprice[18];
2305 $pu_ht_devise = $tabprice[19];
2314 $rang = $rangmax + 1;
2319 $apply_abs_price_on_credit_note =
false;
2321 $apply_abs_price_on_credit_note =
true;
2327 $supplierinvoiceline->context = $this->context;
2329 $supplierinvoiceline->fk_facture_fourn = $this->id;
2331 $supplierinvoiceline->desc = $desc;
2332 $supplierinvoiceline->ref_supplier = $ref_supplier;
2334 $supplierinvoiceline->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs((
float) $qty) : (float) $qty);
2336 $supplierinvoiceline->subprice = ($apply_abs_price_on_credit_note ? -abs((
float) $pu_ht) : (float) $pu_ht);
2338 $supplierinvoiceline->subprice_ttc = ($price_base_type ===
'TTC') ? ($apply_abs_price_on_credit_note ? -abs((
float) $tabprice[5]) : (float) $tabprice[5]) : 0;
2340 $supplierinvoiceline->vat_src_code = $vat_src_code;
2341 $supplierinvoiceline->tva_tx = $txtva;
2342 $supplierinvoiceline->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
2343 $supplierinvoiceline->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
2344 $supplierinvoiceline->localtax1_type = empty($localtaxes_type[0]) ? 0 : $localtaxes_type[0];
2345 $supplierinvoiceline->localtax2_type = empty($localtaxes_type[2]) ? 0 : $localtaxes_type[2];
2347 $supplierinvoiceline->total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_ht) : (float) $total_ht);
2348 $supplierinvoiceline->total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_tva) : (float) $total_tva);
2349 $supplierinvoiceline->total_localtax1 = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_localtax1) : (float) $total_localtax1);
2350 $supplierinvoiceline->total_localtax2 = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_localtax2) : (float) $total_localtax2);
2351 $supplierinvoiceline->total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_ttc) : (float) $total_ttc);
2353 $supplierinvoiceline->fk_product = $fk_product;
2354 $supplierinvoiceline->product_type = $type;
2355 $supplierinvoiceline->remise_percent = $remise_percent;
2357 $supplierinvoiceline->date_end = $date_end;
2358 $supplierinvoiceline->fk_code_ventilation = $fk_code_ventilation;
2359 $supplierinvoiceline->rang = $rang;
2360 $supplierinvoiceline->info_bits = $info_bits;
2361 $supplierinvoiceline->fk_remise_except = $fk_remise_except;
2364 $supplierinvoiceline->special_code = (int) $special_code;
2365 $supplierinvoiceline->fk_parent_line = $fk_parent_line;
2366 $supplierinvoiceline->origin = $this->origin;
2367 $supplierinvoiceline->origin_type = $origin_type;
2368 $supplierinvoiceline->origin_id = $origin_id;
2369 $supplierinvoiceline->fk_unit = $fk_unit;
2372 $supplierinvoiceline->fk_multicurrency = $this->fk_multicurrency;
2373 $supplierinvoiceline->multicurrency_code = $this->multicurrency_code;
2374 $supplierinvoiceline->multicurrency_subprice = ($apply_abs_price_on_credit_note ? -abs((
float) $pu_ht_devise) : (float) $pu_ht_devise);
2376 $supplierinvoiceline->multicurrency_total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $multicurrency_total_ht) : (float) $multicurrency_total_ht);
2377 $supplierinvoiceline->multicurrency_total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $multicurrency_total_tva) : (float) $multicurrency_total_tva);
2378 $supplierinvoiceline->multicurrency_total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $multicurrency_total_ttc) : (float) $multicurrency_total_ttc);
2380 if (is_array($array_options) && count($array_options) > 0) {
2381 $supplierinvoiceline->array_options = $array_options;
2384 $result = $supplierinvoiceline->insert($notrigger);
2387 $result = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2390 if (!isset($this->context[
'createfromclone'])) {
2391 if (!empty($fk_parent_line)) {
2394 } elseif ($rang > 0 && $rang <= count($this->lines)) {
2396 $linecount = count($this->lines);
2397 for ($ii = $rang; $ii <= $linecount; $ii++) {
2402 $this->lines[] = $supplierinvoiceline;
2405 $this->db->commit();
2406 return $supplierinvoiceline->id;
2408 $this->error = $this->db->error();
2409 $this->db->rollback();
2413 $this->error = $supplierinvoiceline->error;
2414 $this->errors = $supplierinvoiceline->errors;
2415 $this->db->rollback();
2448 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)
2453 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2457 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);
2458 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2462 $remise_percent = (float)
price2num($remise_percent);
2472 $langs->load(
"errors");
2473 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2478 if (empty($vatrate)) {
2481 if (empty($txlocaltax1)) {
2484 if (empty($txlocaltax2)) {
2488 $txlocaltax1 = (float)
price2num($txlocaltax1);
2489 $txlocaltax2 = (float)
price2num($txlocaltax2);
2501 if (preg_match(
'/\((.*)\)/', (
string) $vatrate, $reg)) {
2502 $vat_src_code = $reg[1];
2503 $vatrate = preg_replace(
'/\s*\(.*\)/',
'', (
string) $vatrate);
2506 $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, $this->multicurrency_code);
2507 $total_ht = $tabprice[0];
2508 $total_tva = $tabprice[1];
2509 $total_ttc = $tabprice[2];
2510 $pu_ht = $tabprice[3];
2511 $pu_tva = $tabprice[4];
2512 $pu_ttc = $tabprice[5];
2513 $total_localtax1 = $tabprice[9];
2514 $total_localtax2 = $tabprice[10];
2517 $multicurrency_total_ht = $tabprice[16];
2518 $multicurrency_total_tva = $tabprice[17];
2519 $multicurrency_total_ttc = $tabprice[18];
2520 $pu_ht_devise = $tabprice[19];
2522 if (empty($info_bits)) {
2529 $line->fetch_optionals();
2531 $staticline = clone $line;
2534 $product =
new Product($this->db);
2535 $result = $product->fetch($idproduct);
2536 $product_type = $product->type;
2538 $idproduct = $staticline->fk_product;
2539 $product_type = $type;
2542 $line->oldline = $staticline;
2543 $line->context = $this->context;
2547 $apply_abs_price_on_credit_note =
false;
2549 $apply_abs_price_on_credit_note =
true;
2552 $line->desc = $desc;
2553 $line->description = $desc;
2554 $line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs((
float) $qty) : (float) $qty);
2556 $line->subprice = ($apply_abs_price_on_credit_note ? -abs((
float) $pu_ht) : (float) $pu_ht);
2558 $line->subprice_ttc = ($price_base_type ===
'TTC') ? ($apply_abs_price_on_credit_note ? -abs((
float) $pu_ttc) : (float) $pu_ttc) : 0;
2560 $line->remise_percent = $remise_percent;
2561 $line->ref_supplier = $ref_supplier;
2564 $line->date_end = $date_end;
2566 $line->vat_src_code = $vat_src_code;
2567 $line->tva_tx = $vatrate;
2568 $line->localtax1_tx = $txlocaltax1;
2569 $line->localtax2_tx = $txlocaltax2;
2570 $line->localtax1_type = empty($localtaxes_type[0]) ? 0 : $localtaxes_type[0];
2571 $line->localtax2_type = empty($localtaxes_type[2]) ? 0 : $localtaxes_type[2];
2573 $line->total_ht = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_ht) : (float) $total_ht);
2574 $line->total_tva = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_tva) : (float) $total_tva);
2575 $line->total_localtax1 = (float) $total_localtax1;
2576 $line->total_localtax2 = (float) $total_localtax2;
2577 $line->total_ttc = (($apply_abs_price_on_credit_note || $qty < 0) ? -abs((
float) $total_ttc) : (float) $total_ttc);
2579 $line->fk_product = $idproduct;
2580 $line->product_type = $product_type;
2581 $line->info_bits = $info_bits;
2582 $line->fk_unit = $fk_unit;
2583 $line->rang = $rang;
2585 if (is_array($array_options) && count($array_options) > 0) {
2587 foreach ($array_options as $key => $value) {
2588 $line->array_options[$key] = $array_options[$key];
2593 $line->multicurrency_subprice = (float) $pu_ht_devise;
2594 $line->multicurrency_total_ht = (float) $multicurrency_total_ht;
2595 $line->multicurrency_total_tva = (float) $multicurrency_total_tva;
2596 $line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
2598 $res = $line->update($notrigger);
2604 $res = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2625 if ($line->fetch($rowid) < 1) {
2628 if ($this->
id > 0 && (
int) $line->fk_facture_fourn !== (
int) $this->id) {
2629 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2636 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
2637 $sql .=
' SET fk_invoice_supplier_line = NULL';
2638 $sql .=
' WHERE fk_invoice_supplier_line = '.((int) $rowid);
2640 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
2641 $result = $this->db->query($sql);
2643 $this->error = $this->db->error();
2644 $this->db->rollback();
2648 $res = $line->delete($notrigger);
2651 $this->errors[] = $line->error;
2652 $this->db->rollback();
2658 $this->db->commit();
2661 $this->db->rollback();
2662 $this->error = $this->db->lasterror();
2677 $sql =
'SELECT c.rowid, datec, tms as datem, ';
2678 $sql .=
' fk_user_author, fk_user_modif, fk_user_valid';
2679 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as c';
2680 $sql .=
' WHERE c.rowid = '.((int) $id);
2682 $result = $this->db->query($sql);
2684 if ($this->db->num_rows($result)) {
2685 $obj = $this->db->fetch_object($result);
2687 $this->
id = $obj->rowid;
2689 $this->user_creation_id = $obj->fk_user_author;
2690 $this->user_validation_id = $obj->fk_user_valid;
2691 $this->user_modification_id = $obj->fk_user_modif;
2692 $this->date_creation = $this->db->jdate($obj->datec);
2693 $this->date_modification = $this->db->jdate($obj->datem);
2696 $this->db->free($result);
2698 dol_print_error($this->db);
2718 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut,";
2719 $sql .=
" ff.rowid as rowidnext";
2720 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
2721 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2722 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as ff ON f.rowid = ff.fk_facture_source";
2723 $sql .=
" WHERE (f.fk_statut = ".self::STATUS_VALIDATED.
" OR (f.fk_statut = ".self::STATUS_ABANDONED.
" AND f.close_code = '".self::CLOSECODE_ABANDONED.
"'))";
2724 $sql .=
" AND f.entity = ".((int)
$conf->entity);
2725 $sql .=
" AND f.paye = 0";
2726 $sql .=
" AND pf.fk_paiementfourn IS NULL";
2727 $sql .=
" AND ff.fk_statut IS NULL";
2729 $sql .=
" AND f.fk_soc = ".((int) $socid);
2731 $sql .=
" ORDER BY f.ref";
2733 dol_syslog(get_class($this).
"::list_replacable_supplier_invoices", LOG_DEBUG);
2734 $resql = $this->db->query($sql);
2736 while ($obj = $this->db->fetch_object($resql)) {
2737 $return[$obj->rowid] = array(
2738 'id' => $obj->rowid,
2740 'status' => $obj->fk_statut
2746 $this->error = $this->db->error();
2767 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut, f.type, f.subtype, f.paye as paid, pf.fk_paiementfourn";
2768 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
2769 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2770 $sql .=
" WHERE f.entity = ".((int)
$conf->entity);
2771 $sql .=
" AND f.fk_statut in (".self::STATUS_VALIDATED.
",".self::STATUS_CLOSED.
")";
2772 $sql .=
" AND NOT EXISTS (SELECT rowid from ".MAIN_DB_PREFIX.
"facture_fourn as ff WHERE f.rowid = ff.fk_facture_source";
2773 $sql .=
" AND ff.type=".self::TYPE_REPLACEMENT.
")";
2774 $sql .=
" AND f.type != ".self::TYPE_CREDIT_NOTE;
2776 $sql .=
" AND f.fk_soc = ".((int) $socid);
2778 $sql .=
" ORDER BY f.ref";
2780 dol_syslog(get_class($this).
"::list_qualified_avoir_supplier_invoices", LOG_DEBUG);
2781 $resql = $this->db->query($sql);
2783 while ($obj = $this->db->fetch_object($resql)) {
2785 if ($obj->fk_statut == self::STATUS_VALIDATED) {
2788 if ($obj->fk_statut == self::STATUS_CLOSED) {
2792 $paymentornot = ($obj->fk_paiementfourn ? 1 : 0);
2793 $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);
2799 $this->error = $this->db->error();
2814 global
$conf, $langs;
2821 $sql =
'SELECT COUNT(ff.rowid) as nb, SUM(ff.total_ht) as total,';
2822 $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";
2823 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as ff';
2824 if (empty($user->socid) && !$user->hasRight(
"societe",
"client",
"voir")) {
2825 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON ff.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2827 $sql .=
' WHERE ff.paye = 0';
2828 $sql .=
" AND ff.fk_statut IN (".self::STATUS_VALIDATED.
")";
2829 $sql .=
" AND ff.entity = ".((int)
$conf->entity);
2831 $sql .=
' AND ff.fk_soc = '.((int) $user->socid);
2834 $resql = $this->db->query($sql);
2836 $langs->load(
"bills");
2839 $response->warning_delay =
$conf->warning_delays[
'supplier_invoice'] / 60 / 60 / 24;
2840 $response->label = $langs->trans(
"SupplierBillsToPay");
2841 $response->labelShort = $langs->trans(
"StatusToPay");
2843 $response->url = DOL_URL_ROOT.
'/fourn/facture/list.php?search_status=1&mainmenu=billing&leftmenu=suppliers_bills';
2844 $response->img = img_object($langs->trans(
"Bills"),
"bill");
2846 $obj = $this->db->fetch_object($resql);
2848 $response->nbtodo = (int) $obj->nb;
2849 $response->total = (float) $obj->total;
2850 $response->nbtodolate = (int) $obj->nblate;
2851 if ($response->nbtodolate > 0) {
2852 $response->url_late = DOL_URL_ROOT.
'/fourn/facture/list.php?search_option=late&mainmenu=billing&leftmenu=suppliers_bills';
2856 $this->db->free($resql);
2859 dol_print_error($this->db);
2860 $this->error = $this->db->error();
2876 $langs->load(
'bills');
2879 $moretitle = $params[
'moretitle'] ??
'';
2881 $picto = $this->picto;
2882 if ($this->
type == self::TYPE_REPLACEMENT) {
2885 if ($this->
type == self::TYPE_CREDIT_NOTE) {
2888 if ($this->
type == self::TYPE_DEPOSIT) {
2892 $datas[
'picto'] = img_picto(
'', $picto).
' <u class="paddingrightonly">'.$langs->trans(
"SupplierInvoice").
'</u>';
2893 if ($this->
type == self::TYPE_REPLACEMENT) {
2894 $datas[
'picto'] .=
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"InvoiceReplacement").
'</u>';
2895 } elseif ($this->
type == self::TYPE_CREDIT_NOTE) {
2896 $datas[
'picto'] .=
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"CreditNote").
'</u>';
2897 } elseif ($this->
type == self::TYPE_DEPOSIT) {
2898 $datas[
'picto'] .=
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"Deposit").
'</u>';
2900 if (isset($this->
status)) {
2902 if (isset($this->totalpaid)) {
2903 $alreadypaid = $this->totalpaid;
2906 $datas[
'picto'] .=
' '.$this->getLibStatut(5, $alreadypaid);
2909 $datas[
'picto'] .=
' - '.$moretitle;
2911 if (!empty($this->
ref)) {
2912 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
2914 if (!empty($this->ref_supplier)) {
2915 $datas[
'refsupplier'] =
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
2917 if (empty($params[
'nofetch'])) {
2918 $langs->load(
'companies');
2919 if (empty($this->thirdparty)) {
2922 if (is_object($this->thirdparty)) {
2923 $datas[
'supplier'] =
'<br><b>'.$langs->trans(
'Supplier').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
2926 if (!empty($this->label)) {
2927 $datas[
'label'] =
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
2929 if (!empty($this->
date)) {
2930 $datas[
'date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->
date,
'day');
2932 if (!empty($this->date_echeance)) {
2933 $datas[
'date_echeance'] =
'<br><b>'.$langs->trans(
'DateDue').
':</b> '.
dol_print_date($this->date_echeance,
'day');
2935 if (!empty($this->total_ht)) {
2936 $datas[
'amountht'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1,
$conf->currency);
2938 if (!empty($this->total_tva)) {
2939 $datas[
'totaltva'] =
'<br><b>'.$langs->trans(
'AmountVAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1,
$conf->currency);
2941 if (!empty($this->total_localtax1) && $this->total_localtax1 != 0) {
2943 $datas[
'amountlt1'] =
'<br><b>'.$langs->transcountry(
'AmountLT1',
$mysoc->country_code).
':</b> '.
price($this->total_localtax1, 0, $langs, 0, -1, -1,
$conf->currency);
2945 if (!empty($this->total_localtax2) && $this->total_localtax2 != 0) {
2946 $datas[
'amountlt2'] =
'<br><b>'.$langs->transcountry(
'AmountLT2',
$mysoc->country_code).
':</b> '.
price($this->total_localtax2, 0, $langs, 0, -1, -1,
$conf->currency);
2948 if (!empty($this->revenuestamp)) {
2949 $datas[
'amountrevenustamp'] =
'<br><b>'.$langs->trans(
'RevenueStamp').
':</b> '.
price($this->revenuestamp, 0, $langs, 0, -1, -1,
$conf->currency);
2951 if (!empty($this->total_ttc)) {
2952 $datas[
'totalttc'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1,
$conf->currency);
2967 $context = parent::getObjectLinesDisplayContext();
2969 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
2970 $context[
'seller'] = (is_object($this->thirdparty) ? $this->thirdparty :
new Societe($this->db));
2971 $context[
'buyer'] =
$mysoc;
2972 $context[
'forceall'] = 1;
2973 $context[
'inputalsopricewithtax'] = 1;
2975 $context[
'senderissupplier'] = (
getDolGlobalInt(
'SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY') ?
getDolGlobalInt(
'SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY') : 2);
2993 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
2995 global $langs, $user, $hookmanager;
2999 if ($option ==
'withdraw') {
3000 $url = DOL_URL_ROOT.
'/compta/facture/prelevement.php?facid='.$this->
id.
'&type=bank-transfer';
3001 } elseif ($option ==
'document') {
3002 $url = DOL_URL_ROOT.
'/fourn/facture/document.php?facid='.$this->id;
3004 $url = DOL_URL_ROOT.
'/fourn/facture/card.php?facid='.$this->id;
3011 if ($option !==
'nolink') {
3013 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3014 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3015 $add_save_lastsearch_values = 1;
3017 if ($add_save_lastsearch_values) {
3018 $url .=
'&save_lastsearch_values=1';
3022 $picto = $this->picto;
3023 if ($this->
type == self::TYPE_REPLACEMENT) {
3026 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3029 if ($this->
type == self::TYPE_DEPOSIT) {
3035 'objecttype' => $this->element,
3036 'option' => $option,
3037 'moretitle' => $moretitle,
3039 $classfortooltip =
'classfortooltip';
3042 $classfortooltip =
'classforajaxtooltip';
3043 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
3055 if (empty($notooltip)) {
3057 $label = $langs->trans(
"ShowSupplierInvoice");
3058 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
3060 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
3061 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
3064 $linkstart =
'<a href="'.$url.
'"';
3065 $linkstart .= $linkclose.
'>';
3068 $result .= $linkstart;
3070 $result .= img_object(($notooltip ?
'' : $label), ($picto ? $picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'"'), 0, 0, $notooltip ? 0 : 1);
3072 if ($withpicto != 2) {
3073 $result .= ($max ?
dol_trunc($ref, $max) : $ref);
3075 $result .= $linkend;
3077 if ($addlinktonotes) {
3078 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3081 $result .=
' <span class="note inline-block">';
3082 $result .=
'<a href="'.DOL_URL_ROOT.
'/fourn/facture/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3083 $result .= img_picto(
'',
'note');
3085 $result .=
'</span>';
3089 $hookmanager->initHooks(array($this->element .
'dao'));
3090 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
3091 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3093 $result = $hookmanager->resPrint;
3095 $result .= $hookmanager->resPrint;
3112 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
3113 return parent::setCategoriesCommon($categories, Categorie::TYPE_SUPPLIER_INVOICE);
3126 global $langs,
$conf;
3127 $langs->load(
"orders");
3131 $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER =
'mod_facture_fournisseur_cactus';
3140 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
3142 foreach ($dirmodels as $reldir) {
3143 $dir =
dol_buildpath($reldir.
"core/modules/supplier_invoice/");
3146 $mybool = ((bool) @include_once $dir.$file) || $mybool;
3150 dol_print_error(
null,
"Failed to include file ".$file);
3154 $obj =
new $classname();
3155 '@phan-var-force ModeleNumRefSuppliersInvoices $obj';
3157 $numref = $obj->getNextValue($soc, $this, $mode);
3159 if ($numref !=
"") {
3162 $this->error = $obj->error;
3178 global $langs,
$conf;
3179 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
3187 $sql =
"SELECT rowid";
3188 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3189 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3190 $sql .= $this->db->plimit(100);
3192 $resql = $this->db->query($sql);
3194 $num_prods = $this->db->num_rows($resql);
3196 while ($i < $num_prods) {
3198 $row = $this->db->fetch_row($resql);
3199 $prodids[$i] = $row[0];
3205 $this->
ref =
'SPECIMEN';
3206 $this->ref_supplier =
'SUPPLIER_REF_SPECIMEN';
3207 $this->specimen = 1;
3210 $this->date_lim_reglement = $this->
date + 3600 * 24 * 30;
3211 $this->cond_reglement_code =
'RECEP';
3212 $this->mode_reglement_code =
'CHQ';
3214 $this->note_public =
'This is a comment (public)';
3215 $this->note_private =
'This is a comment (private)';
3217 $this->multicurrency_tx = 1;
3218 $this->multicurrency_code =
$conf->currency;
3221 if (empty($option) || $option !=
'nolines') {
3224 while ($xnbp < $nbp) {
3226 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3228 $line->subprice = 100;
3230 $line->tva_tx = 19.6;
3231 $line->localtax1_tx = 0;
3232 $line->localtax2_tx = 0;
3234 $line->total_ht = 50;
3235 $line->total_ttc = 59.8;
3236 $line->total_tva = 9.8;
3237 $line->remise_percent = 50;
3239 $line->total_ht = 100;
3240 $line->total_ttc = 119.6;
3241 $line->total_tva = 19.6;
3242 $line->remise_percent = 0;
3245 if ($num_prods > 0) {
3246 $prodid = mt_rand(1, $num_prods);
3247 $line->fk_product = $prodids[$prodid];
3249 $line->product_type = 0;
3251 $this->lines[$xnbp] = $line;
3253 $this->total_ht += $line->total_ht;
3254 $this->total_tva += $line->total_tva;
3255 $this->total_ttc += $line->total_ttc;
3261 $this->total_ht = $xnbp * 100;
3262 $this->total_tva = $xnbp * 19.6;
3263 $this->total_ttc = $xnbp * 119.6;
3275 global
$conf, $user;
3277 $this->nb = array();
3281 $sql =
"SELECT count(f.rowid) as nb";
3282 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
3283 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON f.fk_soc = s.rowid";
3284 if (empty($user->socid) && !$user->hasRight(
"societe",
"client",
"voir")) {
3285 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3286 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3289 $sql .=
" ".$clause.
" f.entity = ".((int)
$conf->entity);
3291 $resql = $this->db->query($sql);
3293 while ($obj = $this->db->fetch_object($resql)) {
3294 $this->nb[
"supplier_invoices"] = $obj->nb;
3296 $this->db->free($resql);
3299 dol_print_error($this->db);
3300 $this->error = $this->db->error();
3315 global $langs, $hookmanager;
3334 $object->ref_supplier = (empty($this->ref_supplier) ? $langs->trans(
"CopyOf").
' '.
$object->ref_supplier : $this->ref_supplier);
3336 $object->user_validation_id = 0;
3337 $object->fk_facture_source = 0;
3339 $object->date_validation =
'';
3341 $object->ref_client =
'';
3349 $object->date_echeance =
$object->calculate_date_lim_reglement();
3352 foreach (
$object->lines as $i => $line) {
3353 if (isset(
$object->lines[$i]->info_bits) && (
$object->lines[$i]->info_bits & 0x02) == 0x02) {
3359 $object->context[
'createfromclone'] =
'createfromclone';
3360 $result =
$object->create($user);
3364 $this->error =
$object->error;
3365 $this->errors =
$object->errors;
3371 if (is_object($hookmanager)) {
3372 $parameters = array(
'objFrom' => $objFrom);
3374 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters,
$object, $action);
3382 unset(
$object->context[
'createfromclone']);
3386 $this->db->commit();
3389 $this->db->rollback();
3405 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3409 $langs->load(
"suppliers");
3410 $outputlangs->load(
"products");
3413 if (empty($modele)) {
3419 } elseif ($modele ==
'auto') {
3420 $modele =
'canelle';
3423 if (empty($modele)) {
3426 $modelpath =
"core/modules/supplier_invoice/doc/";
3428 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3441 if ($mode ==
'create') {
3444 if (!in_array($mode, array(
'read',
'write',
'delete'))) {
3448 return (
bool) $user->hasRight(
"fournisseur",
"facture", $mode);
3496 if (!$this->date_echeance) {
3500 $status = isset($this->
status) ? $this->
status : $this->statut;
3502 return ($status == self::STATUS_VALIDATED) && ($this->date_echeance < ($now -
$conf->facture->fournisseur->warning_delay));
3514 $sql =
"SELECT fk_invoice_supplier FROM ".MAIN_DB_PREFIX.
"societe_remise_except WHERE fk_invoice_supplier_source = ".((int) $this->
id);
3515 $resql = $this->db->query($sql);
3516 if (!empty($resql)) {
3517 $obj = $this->db->fetch_object($resql);
3518 if (!empty($obj->fk_invoice_supplier)) {
3536 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
3538 $picto = $this->picto;
3539 if ($this->
type == self::TYPE_REPLACEMENT) {
3542 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3545 if ($this->
type == self::TYPE_DEPOSIT) {
3549 $return =
'<div class="box-flex-item box-flex-grow-zero">';
3550 $return .=
'<div class="info-box info-box-sm">';
3551 $return .=
'<span class="info-box-icon bg-infobox-action">';
3552 $return .= img_picto(
'', $picto);
3553 $return .=
'</span>';
3554 $return .=
'<div class="info-box-content">';
3555 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . $this->
getNomUrl(1) .
'</span>';
3556 if ($selected >= 0) {
3557 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
3559 if (!empty($arraydata[
'thirdparty'])) {
3560 $return .=
'<br><span class="info-box-label">'.$arraydata[
'thirdparty'].
'</span>';
3562 if (!empty($this->
date)) {
3563 $return .=
'<br><span class="info-box-label">'.dol_print_date($this->
date,
'day').
'</span>';
3565 if (!empty($this->total_ht)) {
3566 $return .=
' <span class="info-box-label amount" title="'.dol_escape_htmltag($langs->trans(
"AmountHT")).
'">'.
price($this->total_ht);
3567 $return .=
' '.$langs->trans(
"HT");
3568 $return .=
'</span>';
3570 $alreadypaid = (empty($arraydata[
'alreadypaid']) ? 0 : $arraydata[
'alreadypaid']);
3571 $return .=
'<br><div class="info-box-status">'.$this->getLibStatut(3, $alreadypaid).
'</div>';
3573 $return .=
'</div>';
3574 $return .=
'</div>';
3575 $return .=
'</div>';
3588 if (!$this->table_element) {
3589 dol_syslog(get_class($this).
"::setVATReverseCharge was called on object with property table_element not defined", LOG_ERR);
3593 dol_syslog(get_class($this).
'::setVATReverseCharge('.$vatreversecharge.
')');
3595 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
3596 $sql .=
" SET vat_reverse_charge = ".((int) $vatreversecharge);
3597 $sql .=
" WHERE rowid=".((int) $this->
id);
3599 if ($this->db->query($sql)) {
3600 $this->vat_reverse_charge = ($vatreversecharge == 0) ? 0 : 1;
3603 dol_syslog(get_class($this).
'::setVATReverseCharge Error ', LOG_DEBUG);
3604 $this->error = $this->db->error();
3623 global
$conf, $langs, $user;
3630 $errorsMsg = array();
3632 $langs->load(
'bills');
3635 $this->output .= $langs->trans(
'ModuleNotEnabled', $langs->transnoentitiesnoconv(
'Suppliers'));
3638 if (!in_array($datetouse, array(
'duedate',
'invoicedate'))) {
3639 $this->output .=
'Bad value for parameter datetouse. Must be "duedate" or "invoicedate"';
3644 $arrayforcerecipient = array();
3645 foreach (explode(
';', (
string) $forcerecipient) as $tmpvalue) {
3646 $tmpvalue = trim($tmpvalue);
3647 if ($tmpvalue !==
'') {
3648 if (!in_array($tmpvalue, array(
'thirdpartyemail',
'internalfollowingcontact',
'salesrepresentative')) && !
isValidEmail($tmpvalue)) {
3649 $this->output .=
'Bad value "'.$tmpvalue.
'" for parameter forcerecipient. Must be an email, "thirdpartyemail", "internalfollowingcontact" or "salesrepresentative"';
3652 $arrayforcerecipient[] = $tmpvalue;
3655 if (empty($arrayforcerecipient)) {
3656 $this->output .=
'Parameter forcerecipient is mandatory. Must be a list of values separated by ";" among an email, "thirdpartyemail", "internalfollowingcontact" or "salesrepresentative"';
3659 $forcerecipient = implode(
';', $arrayforcerecipient);
3661 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
3662 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
3663 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
3664 $formmail =
new FormMail($this->db);
3672 $labelreminderok =
'sendEmailsRemindersOnInvoiceDueDateOK (nbdays='.$nbdays.
' paymentmode='.$paymentmode.
' template='.$template.
' datetouse='.$datetouse.
' forcerecipient='.$forcerecipient.
')';
3677 $sql =
"SELECT rowid as id FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
3678 if (!empty($paymentmode) && $paymentmode !=
'all') {
3679 $sql .=
", ".MAIN_DB_PREFIX.
"c_paiement as cp";
3681 $sql .=
" WHERE f.paye = 0";
3682 $sql .=
" AND f.fk_statut = ".self::STATUS_VALIDATED;
3683 if ($datetouse ==
'invoicedate') {
3684 $sql .=
" AND f.datef = '".$this->db->idate($tmpidate,
'gmt').
"'";
3686 $sql .=
" AND f.date_lim_reglement = '".$this->db->idate($tmpidate,
'gmt').
"'";
3688 $sql .=
" AND f.entity IN (".getEntity(
'supplier_invoice', 0).
")";
3689 if (!empty($paymentmode) && $paymentmode !=
'all') {
3690 $sql .=
" AND f.fk_mode_reglement = cp.id AND cp.code = '".$this->db->escape($paymentmode).
"'";
3692 $sql .=
" AND f.type <> ".self::TYPE_CREDIT_NOTE;
3693 $sql .=
" AND f.total_ttc > 0";
3695 $sql .=
" AND NOT EXISTS (SELECT a.id FROM ".MAIN_DB_PREFIX.
"actioncomm as a";
3696 $sql .=
" WHERE a.elementtype = 'invoice_supplier' AND a.fk_element = f.rowid AND a.code = 'AC_EMAIL'";
3697 $sql .=
" AND a.label = '".$this->db->escape($labelreminderok).
"'";
3699 if ($datetouse ==
'invoicedate') {
3700 $sql .= $this->db->order(
"datef",
"ASC");
3702 $sql .= $this->db->order(
"date_lim_reglement",
"ASC");
3705 $resql = $this->db->query($sql);
3708 if ($datetouse ==
'invoicedate') {
3709 $this->output .= $langs->transnoentitiesnoconv(
"SearchValidatedSupplierInvoicesWithDate", $stmpidate);
3711 $this->output .= $langs->transnoentitiesnoconv(
"SearchUnpaidSupplierInvoicesWithDueDate", $stmpidate);
3713 if (!empty($paymentmode) && $paymentmode !=
'all') {
3714 $this->output .=
' ('.$langs->transnoentitiesnoconv(
"PaymentMode").
' '.$paymentmode.
')';
3716 $this->output .=
'<br>';
3719 while ($obj = $this->db->fetch_object($resql)) {
3720 $errorforinvoice = 0;
3722 $res = $tmpinvoice->fetch($obj->id);
3724 $tmpinvoice->fetch_thirdparty();
3727 if ($tmpinvoice->thirdparty->default_lang) {
3728 $outputlangs->setDefaultLang($tmpinvoice->thirdparty->default_lang);
3729 $outputlangs->loadLangs(array(
"main",
"suppliers"));
3731 $outputlangs = $langs;
3736 $templateLabel =
'';
3737 if (empty($template) || $template ==
'EmailTemplateCode') {
3738 $templateLabel =
'(SendingReminderEmailOnUnpaidSupplierInvoice)';
3740 if (is_numeric($template)) {
3741 $templateId = $template;
3743 $templateLabel = $template;
3747 $arraymessage = $formmail->getEMailTemplate($this->db,
'invoice_supplier_send', $user, $outputlangs, $templateId, 1, $templateLabel);
3748 if (is_numeric($arraymessage) && $arraymessage <= 0) {
3749 $langs->load(
"errors");
3750 $this->output .= $langs->trans(
'ErrorFailedToFindEmailTemplate', $template);
3758 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null, $tmpinvoice);
3763 $sendTopic =
make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv(
'InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
3766 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
3772 $res = $tmpinvoice->fetch_thirdparty();
3773 $recipient = $tmpinvoice->thirdparty;
3775 foreach ($arrayforcerecipient as $tmpforcerecipient) {
3776 if ($tmpforcerecipient ==
'thirdpartyemail') {
3777 if (!empty($recipient->email)) {
3778 $to[] = $recipient->email;
3780 } elseif ($tmpforcerecipient ==
'internalfollowingcontact') {
3781 $tmparraycontact = $tmpinvoice->liste_contact(-1,
'internal', 0,
'SALESREPFOLL');
3782 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
3783 foreach ($tmparraycontact as $data_email) {
3784 if (!empty($data_email[
'email'])) {
3785 $to[] = $data_email[
'email'];
3789 } elseif ($tmpforcerecipient ==
'salesrepresentative') {
3790 $tmparraysalesrep = $recipient->getSalesRepresentatives($user);
3791 if (is_array($tmparraysalesrep)) {
3792 foreach ($tmparraysalesrep as $salesrep) {
3793 if (!empty($salesrep[
'email']) && !empty($salesrep[
'status'])) {
3794 $to[] = $salesrep[
'email'];
3799 $to[] = $tmpforcerecipient;
3802 $to = array_values(array_unique(array_filter($to)));
3804 $errormesg =
"Failed to send remind to thirdparty id=".$tmpinvoice->socid.
". No email found for recipients ".$forcerecipient.
".";
3809 $errormesg =
"Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
3816 if (!empty($arraymessage->email_from)) {
3817 $email_from = (string) $arraymessage->email_from;
3819 if (empty($email_from)) {
3820 $errormesg =
"Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
3825 if (!$errorforinvoice && !empty($to)) {
3828 $to = implode(
',', $to);
3829 if (!empty($arraymessage->email_to)) {
3830 $to = $to.
','.$arraymessage->email_to;
3836 $trackid =
'inv'.$tmpinvoice->id;
3837 $sendcontext =
'standard';
3840 if (!empty($arraymessage->email_tocc)) {
3841 $email_tocc = (string) $arraymessage->email_tocc;
3845 if (!empty($arraymessage->email_tobcc)) {
3846 $email_tobcc = (string) $arraymessage->email_tobcc;
3850 $cMailFile =
new CMailFile($sendTopic, $to, $email_from, $sendContent, array(), array(), array(), $email_tocc, $email_tobcc, 0, 1, $errors_to,
'', $trackid,
'', $sendcontext,
'');
3853 if ($cMailFile->sendfile()) {
3857 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
3862 $actioncomm->type_code =
'AC_OTH_AUTO';
3863 $actioncomm->socid = $tmpinvoice->thirdparty->id;
3864 $actioncomm->contact_id = 0;
3866 $actioncomm->code =
'AC_EMAIL';
3867 $actioncomm->label = $labelreminderok;
3868 $actioncomm->note_private = $sendContent;
3869 $actioncomm->fk_project = $tmpinvoice->fk_project;
3870 $actioncomm->datep =
dol_now();
3871 $actioncomm->datef = $actioncomm->datep;
3872 $actioncomm->percentage = -1;
3873 $actioncomm->authorid = $user->id;
3874 $actioncomm->userownerid = $user->id;
3876 $actioncomm->email_msgid = $cMailFile->msgid;
3877 $actioncomm->email_subject = $sendTopic;
3878 $actioncomm->email_from = $email_from;
3879 $actioncomm->email_sender =
'';
3880 $actioncomm->email_to = $to;
3884 $actioncomm->errors_to = $errors_to;
3886 $actioncomm->elementtype =
'invoice_supplier';
3887 $actioncomm->elementid = $tmpinvoice->id;
3888 $actioncomm->fk_element = $tmpinvoice->id;
3892 $actioncomm->create($user);
3894 $errormesg = $cMailFile->error.
' : '.$to;
3899 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
3904 $actioncomm->type_code =
'AC_OTH_AUTO';
3905 $actioncomm->socid = $tmpinvoice->thirdparty->id;
3906 $actioncomm->contact_id = 0;
3908 $actioncomm->code =
'AC_EMAIL';
3909 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateKO';
3910 $actioncomm->note_private = $errormesg;
3911 $actioncomm->fk_project = $tmpinvoice->fk_project;
3912 $actioncomm->datep =
dol_now();
3913 $actioncomm->datef = $actioncomm->datep;
3914 $actioncomm->percentage = -1;
3915 $actioncomm->authorid = $user->id;
3916 $actioncomm->userownerid = $user->id;
3918 $actioncomm->email_msgid = $cMailFile->msgid;
3919 $actioncomm->email_from = $email_from;
3920 $actioncomm->email_sender =
'';
3921 $actioncomm->email_to = $to;
3925 $actioncomm->errors_to = $errors_to;
3929 $actioncomm->create($user);
3932 $this->db->commit();
3936 $errorsMsg[] = $errormesg;
3939 $errorsMsg[] =
'Failed to fetch record invoice with ID = '.$obj->id;
3948 $this->output .=
'Nb of emails sent : '.$nbMailSend;
3950 dol_syslog(__METHOD__.
" end - ".$this->output, LOG_INFO);
3954 $this->error =
'Nb of emails sent : '.$nbMailSend.
', '.(empty($errorsMsg) ? $error : implode(
', ', $errorsMsg));
3956 dol_syslog(__METHOD__.
" end - ".$this->error, LOG_INFO);
if(! $sortfield) if(! $sortorder) $object
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage agenda events (actions)
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Superclass for invoice classes.
checkPointOfTaxNotChanged()
Check that the point of tax date of a validated invoice is not changed by an update.
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)
getObjectLinesDisplayContext()
Return the display context of the lines, as defined by the card before printObjectLines() and formAdd...
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.
sendEmailsRemindersOnSupplierInvoiceDueDate($nbdays=0, $paymentmode='all', $template='', $datetouse='duedate', $forcerecipient='internalfollowingcontact')
Send reminders by emails for supplier invoices validated that are due.
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.
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.
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.
hasUserPermission(User $user, $mode='write')
Check if user has a permission on supplier invoices.
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 third parties objects (customers, suppliers, prospects...)
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, $tz='')
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.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
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