38 include_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
40 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
43 if (!empty($conf->accounting->enabled)) {
44 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formaccounting.class.php';
46 if (!empty($conf->accounting->enabled)) {
47 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
58 public $element =
'invoice_supplier';
63 public $table_element =
'facture_fourn';
68 public $table_element_line =
'facture_fourn_det';
73 public $fk_element =
'fk_facture_fourn';
78 public $picto =
'supplier_invoice';
84 public $ismultientitymanaged = 1;
90 public $restrictiononfksoc = 1;
110 public $ref_supplier;
183 public $date_echeance;
208 public $total_localtax1;
209 public $total_localtax2;
218 public $note_private;
222 public $cond_reglement_id;
223 public $cond_reglement_code;
224 public $cond_reglement_label;
225 public $cond_reglement_doc;
232 public $mode_reglement_id;
233 public $mode_reglement_code;
238 public $transport_mode_id;
240 public $extraparams = array();
246 public $lines = array();
257 public $fk_multicurrency;
259 public $multicurrency_code;
260 public $multicurrency_tx;
261 public $multicurrency_total_ht;
262 public $multicurrency_total_tva;
263 public $multicurrency_total_ttc;
268 public $fk_facture_source;
273 public $fields = array(
274 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
275 'ref' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>15),
276 'ref_supplier' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefSupplier',
'enabled'=>1,
'visible'=>-1,
'position'=>20),
277 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>25,
'index'=>1),
278 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefExt',
'enabled'=>1,
'visible'=>0,
'position'=>30),
279 'type' =>array(
'type'=>
'smallint(6)',
'label'=>
'Type',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35),
280 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>-1,
'notnull'=>1,
'position'=>40),
281 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>45),
282 'datef' =>array(
'type'=>
'date',
'label'=>
'Date',
'enabled'=>1,
'visible'=>-1,
'position'=>50),
283 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>55),
284 'libelle' =>array(
'type'=>
'varchar(255)',
'label'=>
'Label',
'enabled'=>1,
'visible'=>-1,
'position'=>60),
285 'paye' =>array(
'type'=>
'smallint(6)',
'label'=>
'Paye',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>65),
286 'amount' =>array(
'type'=>
'double(24,8)',
'label'=>
'Amount',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>70),
287 'remise' =>array(
'type'=>
'double(24,8)',
'label'=>
'Discount',
'enabled'=>1,
'visible'=>-1,
'position'=>75),
288 'close_code' =>array(
'type'=>
'varchar(16)',
'label'=>
'CloseCode',
'enabled'=>1,
'visible'=>-1,
'position'=>80),
289 'close_note' =>array(
'type'=>
'varchar(128)',
'label'=>
'CloseNote',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
290 'tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Tva',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
291 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax1',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
292 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax2',
'enabled'=>1,
'visible'=>-1,
'position'=>100),
293 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>-1,
'position'=>105),
294 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalVAT',
'enabled'=>1,
'visible'=>-1,
'position'=>110),
295 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
296 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'position'=>125),
297 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>130),
298 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>135),
299 'fk_facture_source' =>array(
'type'=>
'integer',
'label'=>
'Fk facture source',
'enabled'=>1,
'visible'=>-1,
'position'=>140),
300 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label'=>
'Project',
'enabled'=>
'$conf->project->enabled',
'visible'=>-1,
'position'=>145),
301 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'Account',
'enabled'=>
'$conf->banque->enabled',
'visible'=>-1,
'position'=>150),
302 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'position'=>155),
303 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>160),
304 'date_lim_reglement' =>array(
'type'=>
'date',
'label'=>
'DateLimReglement',
'enabled'=>1,
'visible'=>-1,
'position'=>165),
305 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>170),
306 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>175),
307 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'ModelPdf',
'enabled'=>1,
'visible'=>0,
'position'=>180),
308 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>190),
309 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
310 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLocation',
'enabled'=>1,
'visible'=>-1,
'position'=>200),
311 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'MulticurrencyId',
'enabled'=>1,
'visible'=>-1,
'position'=>205),
312 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'MulticurrencyCode',
'enabled'=>1,
'visible'=>-1,
'position'=>210),
313 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyRate',
'enabled'=>1,
'visible'=>-1,
'position'=>215),
314 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyTotalHT',
'enabled'=>1,
'visible'=>-1,
'position'=>220),
315 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyTotalVAT',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
316 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyTotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>230),
317 'date_pointoftax' =>array(
'type'=>
'date',
'label'=>
'Date pointoftax',
'enabled'=>1,
'visible'=>-1,
'position'=>235),
318 'date_valid' =>array(
'type'=>
'date',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>240),
319 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'Last main doc',
'enabled'=>1,
'visible'=>-1,
'position'=>245),
320 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>500),
321 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>900),
373 const CLOSECODE_DISCOUNTVAT =
'discount_vat';
374 const CLOSECODE_BADCREDIT =
'badsupplier';
375 const CLOSECODE_ABANDONED =
'abandon';
376 const CLOSECODE_REPLACED =
'replaced';
396 global $langs, $conf, $hookmanager;
402 if (isset($this->ref_supplier)) {
403 $this->ref_supplier = trim($this->ref_supplier);
405 if (empty($this->
type)) {
408 if (empty($this->date)) {
413 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
418 if (empty($this->fk_multicurrency)) {
419 $this->multicurrency_code = $conf->currency;
420 $this->fk_multicurrency = 0;
421 $this->multicurrency_tx = 1;
427 if ($this->fac_rec > 0) {
428 $this->fk_fac_rec_source = $this->fac_rec;
430 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture-rec.class.php';
432 $result = $_facrec->fetch($this->fac_rec);
433 $result = $_facrec->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
436 if (! empty($_facrec->frequency)) {
437 $originaldatewhen = $_facrec->date_when;
438 $nextdatewhen =
dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency);
440 $this->socid = $_facrec->socid;
443 $this->entity = $_facrec->entity;
446 $this->fk_project =
GETPOST(
'projectid',
'int') > 0 ? ((int)
GETPOST(
'projectid',
'int')) : $_facrec->fk_projet;
447 $this->fk_projet = $this->fk_project;
448 $this->note_public =
GETPOST(
'note_public',
'restricthtml') ?
GETPOST(
'note_public',
'restricthtml') : $_facrec->note_public;
449 $this->note_private =
GETPOST(
'note_private',
'restricthtml') ?
GETPOST(
'note_private',
'restricthtml') : $_facrec->note_private;
450 $this->model_pdf =
GETPOST(
'model',
'alpha') ?
GETPOST(
'model',
'alpha') : $_facrec->model_pdf;
451 $this->cond_reglement_id =
GETPOST(
'cond_reglement_id',
'int') > 0 ? ((int)
GETPOST(
'cond_reglement_id',
'int')) : $_facrec->cond_reglement_id;
452 $this->mode_reglement_id =
GETPOST(
'mode_reglement_id',
'int') > 0 ? ((int)
GETPOST(
'mode_reglement_id',
'int')) : $_facrec->mode_reglement_id;
453 $this->fk_account =
GETPOST(
'fk_account') > 0 ? ((int)
GETPOST(
'fk_account')) : $_facrec->fk_account;
456 $this->total_ht = $_facrec->total_ht;
457 $this->total_ttc = $_facrec->total_ttc;
460 $this->fk_incoterms = $_facrec->fk_incoterms;
461 $this->location_incoterms = $_facrec->location_incoterms;
467 if (! empty(
GETPOST(
'ref_supplier'))) {
468 $this->ref_supplier = trim($this->ref_supplier);
470 $this->ref_supplier = trim($this->ref_supplier .
'_' . ($_facrec->nb_gen_done + 1));
472 $this->note_public = trim($this->note_public);
473 $this->note_private = trim($this->note_private);
474 $this->note_private =
dol_concatdesc($this->note_private, $langs->trans(
"GeneratedFromRecurringInvoice", $_facrec->titre));
476 $this->array_options = $_facrec->array_options;
478 if (! $this->mode_reglement_id) {
479 $this->mode_reglement_id = 0;
481 $this->brouillon = 1;
485 $this->linked_objects = $_facrec->linkedObjectsIds;
492 if ($_facrec->frequency > 0) {
493 dol_syslog(
"This is a recurring invoice so we set date_last_gen and next date_when");
494 if (empty($_facrec->date_when)) {
495 $_facrec->date_when = $now;
497 $next_date = $_facrec->getNextDate();
498 $result = $_facrec->setValueFrom(
'date_last_gen', $now,
'',
null,
'date',
'', $user,
'');
500 $result = $_facrec->setNextDate($next_date, 1);
504 $outputlangs = $langs;
507 if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($this->thirdparty->default_lang)) {
508 $newlang = $this->thirdparty->default_lang;
510 if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($this->default_lang)) {
511 $newlang = $this->default_lang;
513 if (! empty($newlang)) {
515 $outputlangs->setDefaultLang($newlang);
521 $substitutionarray[
'__INVOICE_MONTH__'] =
dol_print_date($this->date,
'%m');
524 $substitutionarray[
'__INVOICE_MONTH_TEXT__'] =
dol_print_date($this->date,
'%B');
527 $substitutionarray[
'__INVOICE_YEAR__'] =
dol_print_date($this->date,
'%Y');
530 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] =
dol_print_date($originaldatewhen,
'dayhour');
531 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] =
dol_print_date($nextdatewhen,
'dayhour');
532 $substitutionarray[
'__INVOICE_PREVIOUS_DATE_NEXT_INVOICE_AFTER_GEN__'] =
dol_print_date($previousdaynextdatewhen,
'dayhour');
533 $substitutionarray[
'__INVOICE_COUNTER_CURRENT__'] = $_facrec->nb_gen_done;
534 $substitutionarray[
'__INVOICE_COUNTER_MAX__'] = $_facrec->nb_gen_max;
543 if (! empty($forceduedate)) {
544 $this->date_echeance = $forceduedate;
547 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture_fourn (";
549 $sql .=
", ref_supplier";
557 $sql .=
", fk_projet";
558 $sql .=
", fk_cond_reglement";
559 $sql .=
", fk_mode_reglement";
560 $sql .=
", fk_account";
561 $sql .=
", note_private";
562 $sql .=
", note_public";
563 $sql .=
", fk_user_author";
564 $sql .=
", date_lim_reglement";
565 $sql .=
", fk_incoterms, location_incoterms";
566 $sql .=
", fk_multicurrency";
567 $sql .=
", multicurrency_code";
568 $sql .=
", multicurrency_tx";
569 $sql .=
", fk_facture_source";
570 $sql .=
", fk_fac_rec_source";
574 $sql .=
", '".$this->db->escape($this->ref_supplier).
"'";
575 $sql .=
", '".$this->db->escape($this->ref_ext).
"'";
576 $sql .=
", ".((int) $conf->entity);
577 $sql .=
", '".$this->db->escape($this->
type).
"'";
578 $sql .=
", '".$this->db->escape(isset($this->label) ? $this->label : (isset($this->libelle) ? $this->libelle :
'')).
"'";
579 $sql .=
", ".((int) $this->socid);
580 $sql .=
", '".$this->db->idate($now).
"'";
581 $sql .=
", '".$this->db->idate($this->date).
"'";
582 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
583 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
584 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
585 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
586 $sql .=
", '".$this->db->escape($this->note_private).
"'";
587 $sql .=
", '".$this->db->escape($this->note_public).
"'";
588 $sql .=
", ".((int) $user->id).
",";
589 $sql .= $this->date_echeance !=
'' ?
"'".$this->db->idate($this->date_echeance).
"'" :
"null";
590 $sql .=
", ".(int) $this->fk_incoterms;
591 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
592 $sql .=
", ".(int) $this->fk_multicurrency;
593 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
594 $sql .=
", ".(double) $this->multicurrency_tx;
595 $sql .=
", ".(isset($this->fk_facture_source) ? $this->fk_facture_source :
"NULL");
596 $sql .=
", ".(isset($this->fk_fac_rec_source) ? $this->fk_fac_rec_source :
"NULL");
599 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
602 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn');
605 $this->
ref =
'(PROV'.$this->id.
')';
606 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"facture_fourn SET ref='".$this->
db->escape($this->
ref).
"' WHERE rowid=".((int) $this->
id);
608 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
614 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
615 $this->linked_objects = $this->linkedObjectsIds;
619 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
620 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
621 if (is_array($tmp_origin_id)) {
622 foreach ($tmp_origin_id as $origin_id) {
631 $origin_id = $tmp_origin_id;
641 if (!$error && empty($this->fac_rec) && count($this->lines) && is_object($this->lines[0])) {
642 dol_syslog(
"There is ".count($this->lines).
" lines that are invoice lines objects");
643 foreach ($this->lines as $i => $val) {
644 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facture_fourn_det (fk_facture_fourn, special_code, fk_remise_except)';
645 $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').
')';
647 $resql_insert = $this->
db->query($sql);
649 $idligne = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn_det');
654 $this->lines[$i]->pu_ht,
655 $this->lines[$i]->tva_tx.($this->lines[$i]->vat_src_code ?
' ('.$this->lines[$i]->vat_src_code.
')' :
''),
656 $this->lines[$i]->localtax1_tx,
657 $this->lines[$i]->localtax2_tx,
658 $this->lines[$i]->qty,
659 $this->lines[$i]->fk_product,
661 (!empty($this->lines[$i]->info_bits) ? $this->lines[$i]->info_bits :
''),
662 $this->lines[$i]->product_type,
663 $this->lines[$i]->remise_percent,
665 $this->lines[$i]->date_start,
666 $this->lines[$i]->date_end,
667 $this->lines[$i]->array_options,
668 $this->lines[$i]->fk_unit,
669 $this->lines[$i]->multicurrency_subprice,
670 $this->lines[$i]->ref_supplier
673 $this->error = $this->
db->lasterror();
674 $this->
db->rollback();
678 } elseif (!$error && empty($this->fac_rec)) {
679 dol_syslog(
"There is ".count($this->lines).
" lines that are array lines");
680 foreach ($this->lines as $i => $val) {
681 $line = $this->lines[$i];
685 if (!is_object($line)) {
686 $line = (object) $line;
689 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facture_fourn_det (fk_facture_fourn, special_code, fk_remise_except)';
690 $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').
')';
692 $resql_insert = $this->
db->query($sql);
694 $idligne = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'facture_fourn_det');
706 (!empty($line->info_bits) ? $line->info_bits :
''),
708 $line->remise_percent,
712 $line->array_options,
714 $line->multicurrency_subprice,
718 $this->error = $this->
db->lasterror();
719 $this->
db->rollback();
728 if (! $error && $this->fac_rec > 0) {
729 foreach ($_facrec->lines as $i => $val) {
730 if ($_facrec->lines[$i]->fk_product) {
732 $res = $prod->fetch($_facrec->lines[$i]->fk_product);
743 $tva_tx = $_facrec->lines[$i]->tva_tx . ($_facrec->lines[$i]->vat_src_code ?
'(' . $_facrec->lines[$i]->vat_src_code .
')' :
'');
744 $tva_npr = $_facrec->lines[$i]->info_bits;
745 if (empty($tva_tx)) {
748 $localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
749 $localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
751 $fk_product_fournisseur_price = empty($_facrec->lines[$i]->fk_product_fournisseur_price) ? null : $_facrec->lines[$i]->fk_product_fournisseur_price;
752 $buyprice = empty($_facrec->lines[$i]->buyprice) ? 0 : $_facrec->lines[$i]->buyprice;
755 if (! $buyprice && $_facrec->lines[$i]->fk_product > 0) {
756 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.product.class.php';
758 $producttmp->fetch($_facrec->lines[$i]->fk_product);
763 if ($conf->global->MARGIN_TYPE ==
'costprice' && ! empty($producttmp->cost_price)) {
764 $buyprice = $producttmp->cost_price;
765 } elseif (! empty($conf->stock->enabled) && ($conf->global->MARGIN_TYPE ==
'costprice' || $conf->global->MARGIN_TYPE ==
'pmp') && ! empty($producttmp->pmp)) {
766 $buyprice = $producttmp->pmp;
768 if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0) {
769 if ($producttmp->product_fourn_price_id > 0) {
770 $buyprice =
price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent / 100) + $producttmp->fourn_remise,
'MU');
776 $result_insert = $this->
addline(
777 $_facrec->lines[$i]->description,
778 $_facrec->lines[$i]->pu_ht,
782 $_facrec->lines[$i]->qty,
783 $_facrec->lines[$i]->fk_product,
784 $_facrec->lines[$i]->remise_percent,
785 ($_facrec->lines[$i]->date_start == 1 && $this->date) ? $this->date :
'',
786 ($_facrec->lines[$i]->date_end == 1 && $previousdaynextdatewhen) ? $previousdaynextdatewhen :
'',
788 $_facrec->lines[$i]->info_bits,
791 $_facrec->lines[$i]->rang,
793 $_facrec->lines[$i]->array_options,
794 $_facrec->lines[$i]->fk_unit,
797 $_facrec->lines[$i]->ref_supplier,
798 $_facrec->lines[$i]->special_code,
802 if ($result_insert < 0) {
804 $this->error = $this->
db->error();
824 $result = $this->
call_trigger(
'BILL_SUPPLIER_CREATE', $user);
835 $this->
db->rollback();
839 $this->error = $langs->trans(
'FailedToUpdatePrice');
840 $this->
db->rollback();
844 if ($this->
db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
845 $this->error = $langs->trans(
'ErrorRefAlreadyExists');
846 $this->
db->rollback();
849 $this->error = $this->
db->lasterror();
850 $this->
db->rollback();
863 public function fetch($id =
'', $ref =
'')
870 $sql .=
" t.ref_supplier,";
871 $sql .=
" t.ref_ext,";
872 $sql .=
" t.entity,";
874 $sql .=
" t.fk_soc,";
878 $sql .=
" t.libelle as label,";
880 $sql .=
" t.close_code,";
881 $sql .=
" t.close_note,";
883 $sql .=
" t.localtax1,";
884 $sql .=
" t.localtax2,";
885 $sql .=
" t.total_ht,";
886 $sql .=
" t.total_tva,";
887 $sql .=
" t.total_ttc,";
888 $sql .=
" t.fk_statut as status,";
889 $sql .=
" t.fk_user_author,";
890 $sql .=
" t.fk_user_valid,";
891 $sql .=
" t.fk_facture_source,";
892 $sql .=
" t.fk_fac_rec_source,";
893 $sql .=
" t.fk_projet as fk_project,";
894 $sql .=
" t.fk_cond_reglement,";
895 $sql .=
" t.fk_account,";
896 $sql .=
" t.fk_mode_reglement,";
897 $sql .=
" t.date_lim_reglement,";
898 $sql .=
" t.note_private,";
899 $sql .=
" t.note_public,";
900 $sql .=
" t.model_pdf,";
901 $sql .=
" t.import_key,";
902 $sql .=
" t.extraparams,";
903 $sql .=
" cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc,";
904 $sql .=
" p.code as mode_reglement_code, p.libelle as mode_reglement_label,";
905 $sql .=
' s.nom as socnom, s.rowid as socid,';
906 $sql .=
' t.fk_incoterms, t.location_incoterms,';
907 $sql .=
" i.libelle as label_incoterms,";
908 $sql .=
' t.fk_transport_mode,';
909 $sql .=
' t.fk_multicurrency, t.multicurrency_code, t.multicurrency_tx, t.multicurrency_total_ht, t.multicurrency_total_tva, t.multicurrency_total_ttc';
910 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as t';
911 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON (t.fk_soc = s.rowid)";
912 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_payment_term as cr ON t.fk_cond_reglement = cr.rowid";
913 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as p ON t.fk_mode_reglement = p.id";
914 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON t.fk_incoterms = i.rowid';
916 $sql .=
" WHERE t.rowid=".((int) $id);
919 $sql .=
" WHERE t.ref='".$this->db->escape($ref).
"' AND t.entity IN (".
getEntity(
'supplier_invoice').
")";
922 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
926 $obj = $this->
db->fetch_object(
$resql);
928 $this->
id = $obj->rowid;
929 $this->
ref = $obj->ref ? $obj->ref : $obj->rowid;
931 $this->ref_supplier = $obj->ref_supplier;
932 $this->ref_ext = $obj->ref_ext;
933 $this->entity = $obj->entity;
935 $this->fk_soc = $obj->fk_soc;
936 $this->datec = $this->
db->jdate($obj->datec);
937 $this->date = $this->
db->jdate($obj->datef);
938 $this->datep = $this->
db->jdate($obj->datef);
939 $this->tms = $this->
db->jdate($obj->tms);
940 $this->libelle = $obj->label;
941 $this->label = $obj->label;
942 $this->paye = $obj->paye;
943 $this->paid = $obj->paye;
944 $this->close_code = $obj->close_code;
945 $this->close_note = $obj->close_note;
946 $this->total_localtax1 = $obj->localtax1;
947 $this->total_localtax2 = $obj->localtax2;
948 $this->total_ht = $obj->total_ht;
949 $this->total_tva = $obj->total_tva;
950 $this->total_ttc = $obj->total_ttc;
951 $this->status = $obj->status;
952 $this->statut = $obj->status;
953 $this->fk_statut = $obj->status;
954 $this->fk_user_author = $obj->fk_user_author;
955 $this->author = $obj->fk_user_author;
956 $this->fk_user_valid = $obj->fk_user_valid;
957 $this->fk_facture_source = $obj->fk_facture_source;
958 $this->fk_fac_rec_source = $obj->fk_fac_rec_source;
959 $this->fk_project = $obj->fk_project;
960 $this->cond_reglement_id = $obj->fk_cond_reglement;
961 $this->cond_reglement_code = $obj->cond_reglement_code;
962 $this->cond_reglement = $obj->cond_reglement_label;
963 $this->cond_reglement_label = $obj->cond_reglement_label;
964 $this->cond_reglement_doc = $obj->cond_reglement_doc;
965 $this->fk_account = $obj->fk_account;
966 $this->mode_reglement_id = $obj->fk_mode_reglement;
967 $this->mode_reglement_code = $obj->mode_reglement_code;
968 $this->mode_reglement = $obj->mode_reglement_label;
969 $this->date_echeance = $this->
db->jdate($obj->date_lim_reglement);
970 $this->note = $obj->note_private;
971 $this->note_private = $obj->note_private;
972 $this->note_public = $obj->note_public;
973 $this->model_pdf = $obj->model_pdf;
974 $this->modelpdf = $obj->model_pdf;
975 $this->import_key = $obj->import_key;
978 $this->fk_incoterms = $obj->fk_incoterms;
979 $this->location_incoterms = $obj->location_incoterms;
980 $this->label_incoterms = $obj->label_incoterms;
981 $this->transport_mode_id = $obj->fk_transport_mode;
984 $this->fk_multicurrency = $obj->fk_multicurrency;
985 $this->multicurrency_code = $obj->multicurrency_code;
986 $this->multicurrency_tx = $obj->multicurrency_tx;
987 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
988 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
989 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
991 $this->extraparams = (array) json_decode($obj->extraparams,
true);
993 $this->socid = $obj->socid;
994 $this->socnom = $obj->socnom;
1000 if ($this->statut == self::STATUS_DRAFT) {
1001 $this->brouillon = 1;
1006 $this->error = $this->
db->lasterror();
1010 $this->error =
'Bill with id '.$id.
' not found';
1011 dol_syslog(get_class($this).
'::fetch '.$this->error);
1018 $this->error =
"Error ".$this->db->lasterror();
1033 $this->lines = array();
1035 $sql =
'SELECT f.rowid, f.ref as ref_supplier, f.description, f.date_start, f.date_end, f.pu_ht, f.pu_ttc, f.qty, f.remise_percent, f.vat_src_code, f.tva_tx';
1036 $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';
1037 $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';
1038 $sql .=
', p.rowid as product_id, p.ref as product_ref, p.label as label, p.description as product_desc';
1039 $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';
1040 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn_det as f';
1041 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON f.fk_product = p.rowid';
1042 $sql .=
' WHERE fk_facture_fourn='.((int) $this->
id);
1043 $sql .=
' ORDER BY f.rang, f.rowid';
1045 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
1047 $resql_rows = $this->
db->query($sql);
1049 $num_rows = $this->
db->num_rows($resql_rows);
1052 while ($i < $num_rows) {
1053 $obj = $this->
db->fetch_object($resql_rows);
1057 $line->id = $obj->rowid;
1058 $line->rowid = $obj->rowid;
1059 $line->description = $obj->description;
1060 $line->date_start = $obj->date_start;
1061 $line->date_end = $obj->date_end;
1063 $line->product_ref = $obj->product_ref;
1064 $line->ref = $obj->product_ref;
1065 $line->ref_supplier = $obj->ref_supplier;
1066 $line->libelle = $obj->label;
1067 $line->label = $obj->label;
1068 $line->product_desc = $obj->product_desc;
1069 $line->subprice = $obj->pu_ht;
1070 $line->pu_ht = $obj->pu_ht;
1071 $line->pu_ttc = $obj->pu_ttc;
1073 $line->vat_src_code = $obj->vat_src_code;
1074 $line->tva_tx = $obj->tva_tx;
1075 $line->localtax1_tx = $obj->localtax1_tx;
1076 $line->localtax2_tx = $obj->localtax2_tx;
1077 $line->localtax1_type = $obj->localtax1_type;
1078 $line->localtax2_type = $obj->localtax2_type;
1079 $line->qty = $obj->qty;
1080 $line->remise_percent = $obj->remise_percent;
1081 $line->fk_remise_except = $obj->fk_remise_except;
1083 $line->total_ht = $obj->total_ht;
1084 $line->total_ttc = $obj->total_ttc;
1085 $line->total_tva = $obj->total_tva;
1086 $line->total_localtax1 = $obj->total_localtax1;
1087 $line->total_localtax2 = $obj->total_localtax2;
1088 $line->fk_facture_fourn = $obj->fk_facture_fourn;
1089 $line->fk_product = $obj->fk_product;
1090 $line->product_type = $obj->product_type;
1091 $line->product_label = $obj->label;
1092 $line->info_bits = $obj->info_bits;
1093 $line->fk_parent_line = $obj->fk_parent_line;
1094 $line->special_code = $obj->special_code;
1095 $line->rang = $obj->rang;
1096 $line->fk_unit = $obj->fk_unit;
1099 $line->code_ventilation = $obj->fk_code_ventilation;
1100 $line->fk_accounting_account = $obj->fk_code_ventilation;
1103 $line->fk_multicurrency = $obj->fk_multicurrency;
1104 $line->multicurrency_code = $obj->multicurrency_code;
1105 $line->multicurrency_subprice = $obj->multicurrency_subprice;
1106 $line->multicurrency_total_ht = $obj->multicurrency_total_ht;
1107 $line->multicurrency_total_tva = $obj->multicurrency_total_tva;
1108 $line->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1111 $line->fetch_optionals();
1113 $this->lines[$i] = $line;
1118 $this->
db->free($resql_rows);
1121 $this->error = $this->
db->error();
1122 dol_syslog(get_class($this).
"::fetch_lines - No lines:{$this->error} Error:{$this->error}", LOG_DEBUG);
1135 public function update($user =
null, $notrigger = 0)
1137 global $conf, $langs;
1141 if (empty($this->
type)) {
1144 if (isset($this->
ref)) {
1145 $this->
ref = trim($this->
ref);
1147 if (isset($this->ref_supplier)) {
1148 $this->ref_supplier = trim($this->ref_supplier);
1150 if (isset($this->ref_ext)) {
1151 $this->ref_ext = trim($this->ref_ext);
1153 if (isset($this->entity)) {
1154 $this->entity = trim($this->entity);
1156 if (isset($this->
type)) {
1159 if (isset($this->fk_soc)) {
1160 $this->fk_soc = trim($this->fk_soc);
1162 if (isset($this->label)) {
1163 $this->label = trim($this->label);
1165 if (isset($this->libelle)) {
1166 $this->libelle = trim($this->libelle);
1168 if (isset($this->paye)) {
1169 $this->paye = trim($this->paye);
1171 if (isset($this->close_code)) {
1172 $this->close_code = trim($this->close_code);
1174 if (isset($this->close_note)) {
1175 $this->close_note = trim($this->close_note);
1177 if (isset($this->localtax1)) {
1178 $this->localtax1 = trim($this->localtax1);
1180 if (isset($this->localtax2)) {
1181 $this->localtax2 = trim($this->localtax2);
1183 if (empty($this->total_ht)) {
1184 $this->total_ht = 0;
1186 if (empty($this->total_tva)) {
1187 $this->total_tva = 0;
1191 if (isset($this->total_ttc)) {
1192 $this->total_ttc = trim($this->total_ttc);
1194 if (isset($this->statut)) {
1195 $this->statut = (int) $this->statut;
1197 if (isset($this->status)) {
1198 $this->status = (int) $this->status;
1200 if (isset($this->author)) {
1201 $this->author = trim($this->author);
1203 if (isset($this->fk_user_valid)) {
1204 $this->fk_user_valid = trim($this->fk_user_valid);
1206 if (isset($this->fk_facture_source)) {
1207 $this->fk_facture_source = trim($this->fk_facture_source);
1209 if (isset($this->fk_project)) {
1210 if (empty($this->fk_project)) $this->fk_project =
null;
1211 else $this->fk_project = intval($this->fk_project);
1213 if (isset($this->cond_reglement_id)) {
1214 $this->cond_reglement_id = trim($this->cond_reglement_id);
1216 if (isset($this->note_private)) {
1217 $this->note = trim($this->note_private);
1219 if (isset($this->note_public)) {
1220 $this->note_public = trim($this->note_public);
1222 if (isset($this->model_pdf)) {
1223 $this->model_pdf = trim($this->model_pdf);
1225 if (isset($this->import_key)) {
1226 $this->import_key = trim($this->import_key);
1234 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn SET";
1235 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1236 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1237 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1238 $sql .=
" entity=".(isset($this->entity) ? $this->entity :
"null").
",";
1239 $sql .=
" type=".(isset($this->
type) ? $this->
type :
"null").
",";
1240 $sql .=
" fk_soc=".(isset($this->fk_soc) ? $this->fk_soc :
"null").
",";
1241 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
1242 $sql .=
" datef=".(dol_strlen($this->date) != 0 ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
1244 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
1246 $sql .=
" libelle=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
1247 $sql .=
" paye=".(isset($this->paye) ? $this->paye :
"null").
",";
1248 $sql .=
" close_code=".(isset($this->close_code) ?
"'".$this->db->escape($this->close_code).
"'" :
"null").
",";
1249 $sql .=
" close_note=".(isset($this->close_note) ?
"'".$this->db->escape($this->close_note).
"'" :
"null").
",";
1251 $sql .=
" localtax1=".(isset($this->localtax1) ? $this->localtax1 :
"null").
",";
1252 $sql .=
" localtax2=".(isset($this->localtax2) ? $this->localtax2 :
"null").
",";
1253 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
1254 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
1255 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
1256 $sql .=
" fk_statut=".(isset($this->status) ? $this->status : (isset($this->statut) ? $this->statut :
"null")).
",";
1257 $sql .=
" fk_user_author=".(isset($this->author) ? $this->author :
"null").
",";
1258 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? $this->fk_user_valid :
"null").
",";
1259 $sql .=
" fk_facture_source=".(isset($this->fk_facture_source) ? $this->fk_facture_source :
"null").
",";
1260 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
1261 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
1262 $sql .=
" date_lim_reglement=".(dol_strlen($this->date_echeance) != 0 ?
"'".$this->db->idate($this->date_echeance).
"'" :
'null').
",";
1263 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1264 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1265 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1266 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
1267 $sql .=
" WHERE rowid=".((int) $this->
id);
1271 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1277 if ($this->
db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1278 $this->errors[] = $langs->trans(
'ErrorRefAlreadyExists');
1280 $this->errors[] =
"Error ".$this->db->lasterror();
1294 $result = $this->
call_trigger(
'BILL_SUPPLIER_MODIFY', $user);
1304 foreach ($this->errors as $errmsg) {
1305 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1306 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1308 $this->
db->rollback();
1311 $this->
db->commit();
1326 global $conf, $langs;
1328 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1329 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1334 $result =
$remise->fetch($idremise);
1337 if (
$remise->fk_invoice_supplier) {
1338 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
1339 $this->
db->rollback();
1344 $facligne->fk_facture_fourn = $this->id;
1345 $facligne->fk_remise_except =
$remise->id;
1346 $facligne->desc =
$remise->description;
1347 $facligne->vat_src_code =
$remise->vat_src_code;
1348 $facligne->tva_tx =
$remise->tva_tx;
1349 $facligne->subprice = -
$remise->amount_ht;
1350 $facligne->fk_product = 0;
1351 $facligne->product_type = 0;
1353 $facligne->remise_percent = 0;
1354 $facligne->rang = -1;
1355 $facligne->info_bits = 2;
1357 if (!empty($conf->global->MAIN_ADD_LINE_AT_POSITION)) {
1358 $facligne->rang = 1;
1359 $linecount = count($this->lines);
1360 for ($ii = 1; $ii <= $linecount; $ii++) {
1366 if (
$remise->fk_invoice_supplier_source > 0) {
1368 $srcinvoice->fetch(
$remise->fk_invoice_supplier_source);
1369 $totalcostpriceofinvoice = 0;
1370 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmargin.class.php';
1372 $arraytmp = $formmargin->getMarginInfosArray($srcinvoice,
false);
1373 $facligne->pa_ht = $arraytmp[
'pa_total'];
1376 $facligne->total_ht = -
$remise->amount_ht;
1377 $facligne->total_tva = -
$remise->amount_tva;
1378 $facligne->total_ttc = -
$remise->amount_ttc;
1380 $facligne->multicurrency_subprice = -
$remise->multicurrency_subprice;
1381 $facligne->multicurrency_total_ht = -
$remise->multicurrency_total_ht;
1382 $facligne->multicurrency_total_tva = -
$remise->multicurrency_total_tva;
1383 $facligne->multicurrency_total_ttc = -
$remise->multicurrency_total_ttc;
1385 $lineid = $facligne->insert();
1390 $result =
$remise->link_to_invoice($lineid, 0,
'supplier');
1392 $this->error =
$remise->error;
1393 $this->
db->rollback();
1397 $this->
db->commit();
1400 $this->error = $facligne->error;
1401 $this->
db->rollback();
1405 $this->error = $facligne->error;
1406 $this->
db->rollback();
1410 $this->
db->rollback();
1423 public function delete(
User $user, $notrigger = 0)
1425 global $langs, $conf;
1429 dol_syslog(
"FactureFournisseur::delete rowid=".$rowid, LOG_DEBUG);
1436 if (!$error && !$notrigger) {
1438 $result = $this->
call_trigger(
'BILL_SUPPLIER_DELETE', $user);
1440 $this->
db->rollback();
1448 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'societe_remise_except';
1449 $sql .=
' WHERE fk_invoice_supplier_source = '.((int) $rowid);
1450 $sql .=
' AND fk_invoice_supplier_line IS NULL';
1455 $list_rowid_det = array();
1456 foreach ($this->lines as $key => $invoiceline) {
1457 $list_rowid_det[] = $invoiceline->rowid;
1461 if (count($list_rowid_det)) {
1462 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
1463 $sql .=
' SET fk_invoice_supplier = NULL, fk_invoice_supplier_line = NULL';
1464 $sql .=
' WHERE fk_invoice_supplier_line IN ('.$this->db->sanitize(join(
',', $list_rowid_det)).
')';
1466 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1467 if (!$this->
db->query($sql)) {
1474 $main = MAIN_DB_PREFIX.
'facture_fourn_det';
1475 $ef = $main.
"_extrafields";
1476 $sqlef =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_facture_fourn = ".((int) $rowid).
")";
1477 $resqlef = $this->
db->query($sqlef);
1478 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn_det WHERE fk_facture_fourn = '.((int) $rowid);
1479 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1481 if ($resqlef &&
$resql) {
1482 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn WHERE rowid = '.((int) $rowid);
1483 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1484 $resql2 = $this->
db->query($sql);
1514 if ($conf->fournisseur->facture->dir_output) {
1515 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1518 $dir = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$ref;
1519 $file = $dir.
"/".$ref.
".pdf";
1520 if (file_exists($file)) {
1522 $this->error =
'ErrorFailToDeleteFile';
1526 if (file_exists($dir)) {
1530 $this->error =
'ErrorFailToDeleteDir';
1542 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
1547 dol_syslog(get_class($this).
"::delete $this->id by $user->id", LOG_DEBUG);
1548 $this->
db->commit();
1551 $this->error = $this->
db->lasterror();
1552 $this->
db->rollback();
1569 public function set_paid($user, $close_code =
'', $close_note =
'')
1572 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
1573 return $this->
setPaid($user, $close_code, $close_note);
1584 public function setPaid($user, $close_code =
'', $close_note =
'')
1588 if ($this->paye != 1) {
1593 dol_syslog(
"FactureFournisseur::set_paid", LOG_DEBUG);
1595 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn SET';
1596 $sql .=
' fk_statut = '.self::STATUS_CLOSED;
1601 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
1604 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
1606 $sql .=
', fk_user_closing = '.((int) $user->id);
1607 $sql .=
", date_closing = '".$this->db->idate($now).
"'";
1608 $sql .=
' WHERE rowid = '.((int) $this->
id);
1613 $result = $this->
call_trigger(
'BILL_SUPPLIER_PAYED', $user);
1620 $this->error = $this->
db->error();
1625 $this->
db->commit();
1628 $this->
db->rollback();
1650 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
1668 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn';
1669 $sql .=
' SET paye=0, fk_statut='.self::STATUS_VALIDATED.
', close_code=null, close_note=null,';
1670 $sql .=
' date_closing=null,';
1671 $sql .=
' fk_user_closing=null';
1672 $sql .=
' WHERE rowid = '.((int) $this->
id);
1674 dol_syslog(get_class($this).
"::set_unpaid", LOG_DEBUG);
1678 $result = $this->
call_trigger(
'BILL_SUPPLIER_UNPAYED', $user);
1685 $this->error = $this->
db->error();
1690 $this->
db->commit();
1693 $this->
db->rollback();
1708 public function setCanceled($user, $close_code =
'', $close_note =
'')
1710 dol_syslog(get_class($this).
"::setCanceled rowid=".((
int) $this->
id), LOG_DEBUG);
1714 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn SET';
1715 $sql .=
' fk_statut='.self::STATUS_ABANDONED;
1717 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
1720 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
1722 $sql .=
" WHERE rowid = ".((int) $this->
id);
1728 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
1729 $sql .=
' SET fk_invoice_supplier = NULL';
1730 $sql .=
' WHERE fk_invoice_supplier = '.((int) $this->
id);
1735 $result = $this->
call_trigger(
'BILL_SUPPLIER_CANCEL', $user);
1737 $this->
db->rollback();
1742 $this->
db->commit();
1745 $this->error = $this->
db->error().
" sql=".$sql;
1746 $this->
db->rollback();
1750 $this->error = $this->
db->error().
" sql=".$sql;
1751 $this->
db->rollback();
1765 public function validate($user, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
1767 global $conf, $langs;
1769 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1774 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number.
', idwarehouse='.$idwarehouse);
1781 if ($this->statut > self::STATUS_DRAFT) {
1782 dol_syslog(get_class($this).
"::validate no draft status", LOG_WARNING);
1785 if (preg_match(
'/^'.preg_quote($langs->trans(
"CopyOf").
' ').
'/', $this->ref_supplier)) {
1786 $langs->load(
"errors");
1787 $this->error = $langs->trans(
"ErrorFieldFormat", $langs->transnoentities(
"RefSupplier")).
'. '.$langs->trans(
'RemoveString', $langs->transnoentitiesnoconv(
"CopyOf"));
1790 if (count($this->lines) <= 0) {
1791 $langs->load(
"errors");
1792 $this->error = $langs->trans(
"ErrorObjectMustHaveLinesToBeValidated", $this->
ref);
1799 if ($force_number) {
1800 $num = $force_number;
1801 } elseif (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
1808 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn";
1809 $sql .=
" SET ref='".$this->db->escape($num).
"', fk_statut = 1, fk_user_valid = ".((int) $user->id).
", date_valid = '".$this->
db->idate($now).
"'";
1810 $sql .=
" WHERE rowid = ".((int) $this->
id);
1812 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
1816 if (!$error && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
1817 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1818 $langs->load(
"agenda");
1820 $cpt = count($this->lines);
1821 for ($i = 0; $i < $cpt; $i++) {
1822 if ($this->lines[$i]->fk_product > 0) {
1823 $this->line = $this->lines[$i];
1825 $mouvP->origin = &$this;
1826 $mouvP->setOrigin($this->element, $this->
id);
1828 $up_ht_disc = $this->lines[$i]->pu_ht;
1829 if (!empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) {
1830 $up_ht_disc =
price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100,
'MU');
1833 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
1835 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
1846 if (!$error && empty($notrigger)) {
1848 $result = $this->
call_trigger(
'BILL_SUPPLIER_VALIDATE', $user);
1856 $this->oldref = $this->ref;
1859 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
1861 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->
db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'fournisseur/facture/".
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$this->db->escape($this->newref).
"'";
1862 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'fournisseur/facture/".
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
1865 $error++; $this->error = $this->
db->lasterror();
1871 $dirsource = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$oldref;
1872 $dirdest = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$newref;
1873 if (!$error && file_exists($dirsource)) {
1874 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
1876 if (@rename($dirsource, $dirdest)) {
1879 $listoffiles =
dol_dir_list($conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
1880 foreach ($listoffiles as $fileentry) {
1881 $dirsource = $fileentry[
'name'];
1882 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
1883 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
1884 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
1885 @rename($dirsource, $dirdest);
1900 $this->
db->commit();
1903 $this->
db->rollback();
1907 $this->error = $this->
db->error();
1908 $this->
db->rollback();
1923 global $conf, $langs;
1927 if ($this->statut == self::STATUS_DRAFT) {
1928 dol_syslog(__METHOD__.
" already draft status", LOG_WARNING);
1936 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn";
1937 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT;
1938 $sql .=
" WHERE rowid = ".((int) $this->
id);
1940 $result = $this->
db->query($sql);
1943 $this->oldcopy = clone $this;
1947 if ($result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
1948 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1949 $langs->load(
"agenda");
1951 $cpt = count($this->lines);
1952 for ($i = 0; $i < $cpt; $i++) {
1953 if ($this->lines[$i]->fk_product > 0) {
1955 $mouvP->origin = &$this;
1956 $mouvP->setOrigin($this->element, $this->
id);
1959 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
1961 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
1967 if (!$error && empty($notrigger)) {
1969 $result = $this->
call_trigger(
'BILL_SUPPLIER_UNVALIDATE', $user);
1976 $this->
db->commit();
1979 $this->
db->rollback();
1983 $this->error = $this->
db->error();
1984 $this->
db->rollback();
2023 public function addline($desc, $pu, $txtva, $txlocaltax1, $txlocaltax2, $qty, $fk_product = 0, $remise_percent = 0, $date_start =
'', $date_end =
'', $ventil = 0, $info_bits =
'', $price_base_type =
'HT', $type = 0, $rang = -1, $notrigger =
false, $array_options = 0, $fk_unit =
null, $origin_id = 0, $pu_devise = 0, $ref_supplier =
'', $special_code =
'', $fk_parent_line = 0, $fk_remise_except = 0)
2025 global $langs, $mysoc, $conf;
2027 dol_syslog(get_class($this).
"::addline $desc,$pu,$qty,$txtva,$fk_product,$remise_percent,$date_start,$date_end,$ventil,$info_bits,$price_base_type,$type,$fk_unit,fk_remise_except=$fk_remise_except", LOG_DEBUG);
2028 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2030 if ($this->statut == self::STATUS_DRAFT) {
2032 if (empty($remise_percent)) {
2033 $remise_percent = 0;
2038 if (empty($info_bits)) {
2044 if (empty($ventil)) {
2047 if (empty($txtva)) {
2050 if (empty($txlocaltax1)) {
2053 if (empty($txlocaltax2)) {
2057 $remise_percent =
price2num($remise_percent);
2060 if (!preg_match(
'/\((.*)\)/', $txtva)) {
2066 if ($date_start && $date_end && $date_start > $date_end) {
2067 $langs->load(
"errors");
2068 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2074 if ($fk_product > 0) {
2075 if (!empty($conf->global->SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY)) {
2077 dol_syslog(get_class($this).
"::addline we check supplier prices fk_product=".$fk_product.
" qty=".$qty.
" ref_supplier=".$ref_supplier);
2079 if ($prod->fetch($fk_product) > 0) {
2080 $product_type = $prod->type;
2081 $label = $prod->label;
2082 $fk_prod_fourn_price = 0;
2086 $result = $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product,
'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
2089 $pu = $prod->fourn_pu;
2091 $ref_supplier = $prod->ref_supplier;
2093 if ($remise_percent == 0 && $prod->remise_percent != 0) {
2094 $remise_percent = $prod->remise_percent;
2098 $langs->load(
"errors");
2099 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2100 $this->
db->rollback();
2101 dol_syslog(get_class($this).
"::addline we did not found supplier price, so we can't guess unit price");
2106 if ($result == -1) {
2107 $langs->load(
"errors");
2108 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2109 $this->
db->rollback();
2110 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_DEBUG);
2114 $this->error = $prod->error;
2115 $this->
db->rollback();
2116 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_ERR);
2120 $this->error = $prod->error;
2121 $this->
db->rollback();
2126 $product_type = $type;
2129 if (!empty($conf->multicurrency->enabled) && $pu_devise > 0) {
2138 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
2139 $vat_src_code = $reg[1];
2140 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
2148 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_devise);
2149 $total_ht = $tabprice[0];
2150 $total_tva = $tabprice[1];
2151 $total_ttc = $tabprice[2];
2152 $total_localtax1 = $tabprice[9];
2153 $total_localtax2 = $tabprice[10];
2154 $pu_ht = $tabprice[3];
2157 $multicurrency_total_ht = $tabprice[16];
2158 $multicurrency_total_tva = $tabprice[17];
2159 $multicurrency_total_ttc = $tabprice[18];
2160 $pu_ht_devise = $tabprice[19];
2169 $rang = $rangmax + 1;
2175 $this->line->context = $this->context;
2177 $this->line->fk_facture_fourn = $this->id;
2179 $this->line->desc = $desc;
2180 $this->line->ref_supplier = $ref_supplier;
2182 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs($qty) : $qty);
2183 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
2185 $this->line->vat_src_code = $vat_src_code;
2186 $this->line->tva_tx = $txtva;
2187 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
2188 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
2189 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2190 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2192 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ht) : $total_ht);
2193 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_tva) : $total_tva);
2194 $this->line->total_localtax1 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax1) : $total_localtax1);
2195 $this->line->total_localtax2 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax2) : $total_localtax2);
2196 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ttc) : $total_ttc);
2198 $this->line->fk_product = $fk_product;
2199 $this->line->product_type = $type;
2200 $this->line->remise_percent = $remise_percent;
2201 $this->line->date_start = $date_start;
2202 $this->line->date_end = $date_end;
2203 $this->line->fk_code_ventilation = $ventil;
2204 $this->line->rang = $rang;
2205 $this->line->info_bits = $info_bits;
2206 $this->line->fk_remise_except = $fk_remise_except;
2208 $this->line->special_code = ((string) $special_code !=
'' ? $special_code : $this->special_code);
2209 $this->line->fk_parent_line = $fk_parent_line;
2210 $this->line->origin = $this->origin;
2211 $this->line->origin_id = $origin_id;
2212 $this->line->fk_unit = $fk_unit;
2215 $this->line->fk_multicurrency = $this->fk_multicurrency;
2216 $this->line->multicurrency_code = $this->multicurrency_code;
2217 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht_devise) : $pu_ht_devise);
2219 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ht) : $multicurrency_total_ht);
2220 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_tva) : $multicurrency_total_tva);
2221 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
2223 if (is_array($array_options) && count($array_options) > 0) {
2224 $this->line->array_options = $array_options;
2227 $result = $this->line->insert($notrigger);
2230 if (!empty($fk_parent_line)) {
2232 } elseif ($rang > 0 && $rang <= count($this->lines)) {
2233 $linecount = count($this->lines);
2234 for ($ii = $rang; $ii <= $linecount; $ii++) {
2240 $result = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2242 $this->
db->commit();
2243 return $this->line->id;
2245 $this->error = $this->
db->error();
2246 $this->
db->rollback();
2250 $this->error = $this->line->error;
2251 $this->errors = $this->line->errors;
2252 $this->
db->rollback();
2285 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 =
false, $date_start =
'', $date_end =
'', $array_options = 0, $fk_unit =
null, $pu_devise = 0, $ref_supplier =
'', $rang = 0)
2287 global $mysoc, $langs;
2289 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);
2290 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2294 $remise_percent =
price2num($remise_percent);
2303 if ($date_start && $date_end && $date_start > $date_end) {
2304 $langs->load(
"errors");
2305 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2310 if (empty($vatrate)) {
2313 if (empty($txlocaltax1)) {
2316 if (empty($txlocaltax2)) {
2334 if (preg_match(
'/\((.*)\)/', $vatrate, $reg)) {
2335 $vat_src_code = $reg[1];
2336 $vatrate = preg_replace(
'/\s*\(.*\)/',
'', $vatrate);
2339 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $vatrate, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_devise);
2340 $total_ht = $tabprice[0];
2341 $total_tva = $tabprice[1];
2342 $total_ttc = $tabprice[2];
2343 $pu_ht = $tabprice[3];
2344 $pu_tva = $tabprice[4];
2345 $pu_ttc = $tabprice[5];
2346 $total_localtax1 = $tabprice[9];
2347 $total_localtax2 = $tabprice[10];
2350 $multicurrency_total_ht = $tabprice[16];
2351 $multicurrency_total_tva = $tabprice[17];
2352 $multicurrency_total_ttc = $tabprice[18];
2353 $pu_ht_devise = $tabprice[19];
2355 if (empty($info_bits)) {
2362 $line->fetch_optionals();
2364 $staticline = clone $line;
2368 $result = $product->fetch($idproduct);
2369 $product_type = $product->type;
2371 $idproduct = $staticline->fk_product;
2372 $product_type = $type;
2375 $line->oldline = $staticline;
2376 $line->context = $this->context;
2378 $line->description = $desc;
2380 $line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs($qty) : $qty);
2381 $line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
2382 $line->pu_ht = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
2383 $line->pu_ttc = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ttc) : $pu_ttc);
2385 $line->remise_percent = $remise_percent;
2386 $line->ref_supplier = $ref_supplier;
2388 $line->date_start = $date_start;
2389 $line->date_end = $date_end;
2391 $line->vat_src_code = $vat_src_code;
2392 $line->tva_tx = $vatrate;
2393 $line->localtax1_tx = $txlocaltax1;
2394 $line->localtax2_tx = $txlocaltax2;
2395 $line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2396 $line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2398 $line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ht) : $total_ht);
2399 $line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_tva) : $total_tva);
2400 $line->total_localtax1 = $total_localtax1;
2401 $line->total_localtax2 = $total_localtax2;
2402 $line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ttc) : $total_ttc);
2404 $line->fk_product = $idproduct;
2405 $line->product_type = $product_type;
2406 $line->info_bits = $info_bits;
2407 $line->fk_unit = $fk_unit;
2408 $line->rang = $rang;
2410 if (is_array($array_options) && count($array_options) > 0) {
2412 foreach ($array_options as $key => $value) {
2413 $line->array_options[$key] = $array_options[$key];
2418 $line->multicurrency_subprice = $pu_ht_devise;
2419 $line->multicurrency_total_ht = $multicurrency_total_ht;
2420 $line->multicurrency_total_tva = $multicurrency_total_tva;
2421 $line->multicurrency_total_ttc = $multicurrency_total_ttc;
2423 $res = $line->update($notrigger);
2426 $this->errors[] = $line->error;
2429 $res = $this->
update_price(
'',
'auto', 0, $this->thirdparty);
2451 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
2452 $sql .=
' SET fk_invoice_supplier_line = NULL';
2453 $sql .=
' WHERE fk_invoice_supplier_line = '.((int) $rowid);
2455 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
2456 $result = $this->
db->query($sql);
2458 $this->error = $this->
db->error();
2459 $this->
db->rollback();
2465 if ($line->fetch($rowid) < 1) {
2469 $res = $line->delete($notrigger);
2472 $this->errors[] = $line->error;
2473 $this->
db->rollback();
2479 $this->
db->commit();
2482 $this->
db->rollback();
2483 $this->error = $this->
db->lasterror();
2498 $sql =
'SELECT c.rowid, datec, tms as datem, ';
2499 $sql .=
' fk_user_author, fk_user_modif, fk_user_valid';
2500 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as c';
2501 $sql .=
' WHERE c.rowid = '.((int) $id);
2503 $result = $this->
db->query($sql);
2505 if ($this->
db->num_rows($result)) {
2506 $obj = $this->
db->fetch_object($result);
2508 $this->
id = $obj->rowid;
2509 if ($obj->fk_user_author) {
2510 $cuser =
new User($this->
db);
2511 $cuser->fetch($obj->fk_user_author);
2512 $this->user_creation = $cuser;
2514 if ($obj->fk_user_valid) {
2515 $vuser =
new User($this->
db);
2516 $vuser->fetch($obj->fk_user_valid);
2517 $this->user_validation = $vuser;
2519 if ($obj->fk_user_modif) {
2520 $muser =
new User($this->
db);
2521 $muser->fetch($obj->fk_user_modif);
2522 $this->user_modification = $muser;
2524 $this->date_creation = $this->
db->jdate($obj->datec);
2525 $this->date_modification = $this->
db->jdate($obj->datem);
2528 $this->
db->free($result);
2550 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut,";
2551 $sql .=
" ff.rowid as rowidnext";
2552 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
2553 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2554 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as ff ON f.rowid = ff.fk_facture_source";
2555 $sql .=
" WHERE (f.fk_statut = ".self::STATUS_VALIDATED.
" OR (f.fk_statut = ".self::STATUS_ABANDONED.
" AND f.close_code = '".self::CLOSECODE_ABANDONED.
"'))";
2556 $sql .=
" AND f.entity = ".$conf->entity;
2557 $sql .=
" AND f.paye = 0";
2558 $sql .=
" AND pf.fk_paiementfourn IS NULL";
2559 $sql .=
" AND ff.fk_statut IS NULL";
2561 $sql .=
" AND f.fk_soc = ".((int) $socid);
2563 $sql .=
" ORDER BY f.ref";
2565 dol_syslog(get_class($this).
"::list_replacable_supplier_invoices", LOG_DEBUG);
2568 while ($obj = $this->
db->fetch_object(
$resql)) {
2569 $return[$obj->rowid] = array(
2570 'id' => $obj->rowid,
2572 'status' => $obj->fk_statut
2578 $this->error = $this->
db->error();
2600 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut, f.type, f.paye, pf.fk_paiementfourn";
2601 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
2602 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2603 $sql .=
" WHERE f.entity = ".$conf->entity;
2604 $sql .=
" AND f.fk_statut in (".self::STATUS_VALIDATED.
",".self::STATUS_CLOSED.
")";
2605 $sql .=
" AND NOT EXISTS (SELECT rowid from ".MAIN_DB_PREFIX.
"facture_fourn as ff WHERE f.rowid = ff.fk_facture_source";
2606 $sql .=
" AND ff.type=".self::TYPE_REPLACEMENT.
")";
2607 $sql .=
" AND f.type != ".self::TYPE_CREDIT_NOTE;
2609 $sql .=
" AND f.fk_soc = ".((int) $socid);
2611 $sql .=
" ORDER BY f.ref";
2613 dol_syslog(get_class($this).
"::list_qualified_avoir_supplier_invoices", LOG_DEBUG);
2616 while ($obj = $this->
db->fetch_object(
$resql)) {
2618 if ($obj->fk_statut == self::STATUS_VALIDATED) {
2621 if ($obj->fk_statut == self::STATUS_CLOSED) {
2625 $paymentornot = ($obj->fk_paiementfourn ? 1 : 0);
2626 $return[$obj->rowid] = array(
'ref'=>$obj->ref,
'status'=>$obj->fk_statut,
'type'=>$obj->type,
'paye'=>$obj->paye,
'paymentornot'=>$paymentornot);
2632 $this->error = $this->
db->error();
2647 global $conf, $langs;
2649 $sql =
'SELECT ff.rowid, ff.date_lim_reglement as datefin, ff.fk_statut as status, ff.total_ht, ff.total_ttc';
2650 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as ff';
2651 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2652 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON ff.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2654 $sql .=
' WHERE ff.paye = 0';
2655 $sql .=
' AND ff.fk_statut > 0';
2656 $sql .=
" AND ff.entity = ".$conf->entity;
2658 $sql .=
' AND ff.fk_soc = '.((int) $user->socid);
2663 $langs->load(
"bills");
2667 $response->warning_delay = $conf->facture->fournisseur->warning_delay / 60 / 60 / 24;
2668 $response->label = $langs->trans(
"SupplierBillsToPay");
2669 $response->labelShort = $langs->trans(
"StatusToPay");
2671 $response->url = DOL_URL_ROOT.
'/fourn/facture/list.php?search_status=1&mainmenu=billing&leftmenu=suppliers_bills';
2672 $response->img =
img_object($langs->trans(
"Bills"),
"bill");
2676 while ($obj = $this->
db->fetch_object(
$resql)) {
2677 $facturestatic->date_echeance = $this->
db->jdate($obj->datefin);
2678 $facturestatic->statut = $obj->status;
2679 $facturestatic->status = $obj->status;
2681 $response->nbtodo++;
2682 $response->total += $obj->total_ht;
2684 if ($facturestatic->hasDelay()) {
2685 $response->nbtodolate++;
2686 $response->url_late = DOL_URL_ROOT.
'/fourn/facture/list.php?search_option=late&mainmenu=billing&leftmenu=suppliers_bills';
2694 $this->error = $this->
db->error();
2713 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
2715 global $langs, $conf, $user, $hookmanager;
2719 if ($option ==
'withdraw') {
2720 $url = DOL_URL_ROOT.
'/compta/facture/prelevement.php?facid='.$this->
id.
'&type=bank-transfer';
2721 } elseif ($option ==
'document') {
2722 $url = DOL_URL_ROOT.
'/fourn/facture/document.php?facid='.$this->id;
2724 $url = DOL_URL_ROOT.
'/fourn/facture/card.php?facid='.$this->id;
2731 if ($option !==
'nolink') {
2733 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2734 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
2735 $add_save_lastsearch_values = 1;
2737 if ($add_save_lastsearch_values) {
2738 $url .=
'&save_lastsearch_values=1';
2742 $picto = $this->picto;
2743 if ($this->
type == self::TYPE_REPLACEMENT) {
2746 if ($this->
type == self::TYPE_CREDIT_NOTE) {
2749 if ($this->
type == self::TYPE_DEPOSIT) {
2753 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"SupplierInvoice").
'</u>';
2754 if ($this->
type == self::TYPE_REPLACEMENT) {
2755 $label =
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"InvoiceReplace").
'</u>';
2756 } elseif ($this->
type == self::TYPE_CREDIT_NOTE) {
2757 $label =
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"CreditNote").
'</u>';
2758 } elseif ($this->
type == self::TYPE_DEPOSIT) {
2759 $label =
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"Deposit").
'</u>';
2761 if (isset($this->status)) {
2763 if (isset($this->alreadypaid)) {
2764 $alreadypaid = $this->alreadypaid;
2767 $label .=
' '.$this->getLibStatut(5, $alreadypaid);
2769 if (!empty($this->
ref)) {
2770 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
2772 if (!empty($this->ref_supplier)) {
2773 $label .=
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
2775 if (!empty($this->label)) {
2776 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
2778 if (!empty($this->date)) {
2779 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
2781 if (!empty($this->total_ht)) {
2782 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2784 if (!empty($this->total_tva)) {
2785 $label .=
'<br><b>'.$langs->trans(
'AmountVAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2787 if (!empty($this->total_ttc)) {
2788 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2791 $label .=
' - '.$moretitle;
2800 if (empty($notooltip)) {
2801 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2802 $label = $langs->trans(
"ShowSupplierInvoice");
2803 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
2805 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
2806 $linkclose .=
' class="classfortooltip"';
2809 $linkstart =
'<a href="'.$url.
'"';
2810 $linkstart .= $linkclose.
'>';
2813 $result .= $linkstart;
2815 $result .=
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
2817 if ($withpicto != 2) {
2818 $result .= ($max ?
dol_trunc($ref, $max) : $ref);
2820 $result .= $linkend;
2822 if ($addlinktonotes) {
2823 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
2826 $result .=
' <span class="note inline-block">';
2827 $result .=
'<a href="'.DOL_URL_ROOT.
'/fourn/facture/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
2830 $result .=
'</span>';
2834 $hookmanager->initHooks(array($this->element .
'dao'));
2835 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
2836 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2838 $result = $hookmanager->resPrint;
2840 $result .= $hookmanager->resPrint;
2855 global $db, $langs, $conf;
2856 $langs->load(
"orders");
2859 if (empty($conf->global->INVOICE_SUPPLIER_ADDON_NUMBER)) {
2860 $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER =
'mod_facture_fournisseur_cactus';
2865 $file = $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER.
".php";
2866 $classname = $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER;
2869 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
2871 foreach ($dirmodels as $reldir) {
2872 $dir =
dol_buildpath($reldir.
"core/modules/supplier_invoice/");
2875 $mybool |= @include_once $dir.$file;
2878 if ($mybool ===
false) {
2883 $obj =
new $classname();
2885 $numref = $obj->getNumRef($soc, $this, $mode);
2887 if ($numref !=
"") {
2890 $this->error = $obj->error;
2906 global $langs, $conf;
2907 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
2915 $sql =
"SELECT rowid";
2916 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2917 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2918 $sql .= $this->
db->plimit(100);
2922 $num_prods = $this->
db->num_rows(
$resql);
2924 while ($i < $num_prods) {
2926 $row = $this->
db->fetch_row(
$resql);
2927 $prodids[$i] = $row[0];
2933 $this->
ref =
'SPECIMEN';
2934 $this->ref_supplier =
'SUPPLIER_REF_SPECIMEN';
2935 $this->specimen = 1;
2938 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
2939 $this->cond_reglement_code =
'RECEP';
2940 $this->mode_reglement_code =
'CHQ';
2942 $this->note_public =
'This is a comment (public)';
2943 $this->note_private =
'This is a comment (private)';
2945 $this->multicurrency_tx = 1;
2946 $this->multicurrency_code = $conf->currency;
2949 if (empty($option) || $option !=
'nolines') {
2952 while ($xnbp < $nbp) {
2954 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
2956 $line->subprice = 100;
2959 $line->tva_tx = 19.6;
2960 $line->localtax1_tx = 0;
2961 $line->localtax2_tx = 0;
2963 $line->total_ht = 50;
2964 $line->total_ttc = 59.8;
2965 $line->total_tva = 9.8;
2966 $line->remise_percent = 50;
2968 $line->total_ht = 100;
2969 $line->total_ttc = 119.6;
2970 $line->total_tva = 19.6;
2971 $line->remise_percent = 0;
2974 if ($num_prods > 0) {
2975 $prodid = mt_rand(1, $num_prods);
2976 $line->fk_product = $prodids[$prodid];
2978 $line->product_type = 0;
2980 $this->lines[$xnbp] = $line;
2982 $this->total_ht += $line->total_ht;
2983 $this->total_tva += $line->total_tva;
2984 $this->total_ttc += $line->total_ttc;
2990 $this->amount_ht = $xnbp * 100;
2991 $this->total_ht = $xnbp * 100;
2992 $this->total_tva = $xnbp * 19.6;
2993 $this->total_ttc = $xnbp * 119.6;
3005 global $conf, $user;
3007 $this->nb = array();
3011 $sql =
"SELECT count(f.rowid) as nb";
3012 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
3013 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON f.fk_soc = s.rowid";
3014 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3015 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3016 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3019 $sql .=
" ".$clause.
" f.entity = ".$conf->entity;
3023 while ($obj = $this->
db->fetch_object(
$resql)) {
3024 $this->nb[
"supplier_invoices"] = $obj->nb;
3030 $this->error = $this->
db->error();
3045 global $conf, $langs;
3054 $object->fetch($fromid);
3059 $object->fetch_thirdparty();
3062 $object->ref_supplier = (empty($this->ref_supplier) ? $langs->trans(
"CopyOf").
' '.$object->ref_supplier : $this->ref_supplier);
3063 $object->author = $user->id;
3064 $object->user_valid =
'';
3065 $object->fk_facture_source = 0;
3066 $object->date_creation =
'';
3067 $object->date_validation =
'';
3068 $object->date = (empty($this->date) ?
'' : $this->date);
3069 $object->date_echeance =
'';
3070 $object->ref_client =
'';
3071 $object->close_code =
'';
3072 $object->close_note =
'';
3073 if ($conf->global->MAIN_DONT_KEEP_NOTE_ON_CLONING == 1) {
3074 $object->note_private =
'';
3075 $object->note_public =
'';
3079 foreach ($object->lines as $i => $line) {
3080 if (isset($object->lines[$i]->info_bits) && ($object->lines[$i]->info_bits & 0x02) == 0x02) {
3081 unset($object->lines[$i]);
3086 $object->context[
'createfromclone'] =
'createfromclone';
3087 $result = $object->create($user);
3091 $this->error = $object->error;
3092 $this->errors = $object->errors;
3099 unset($object->context[
'createfromclone']);
3103 $this->
db->commit();
3106 $this->
db->rollback();
3122 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3124 global $conf, $user, $langs;
3126 $langs->load(
"suppliers");
3127 $outputlangs->load(
"products");
3130 if (empty($modele)) {
3131 if (!empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) {
3132 $modele = $conf->global->INVOICE_SUPPLIER_ADDON_PDF;
3138 if (empty($modele)) {
3141 $modelpath =
"core/modules/supplier_invoice/doc/";
3143 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3155 return $user->rights->fournisseur->facture;
3203 if (!$this->date_echeance) {
3207 $status = isset($this->status) ? $this->status : $this->statut;
3209 return ($status == self::STATUS_VALIDATED) && ($this->date_echeance < ($now - $conf->facture->fournisseur->warning_delay));
3221 $sql =
"SELECT fk_invoice_supplier FROM ".MAIN_DB_PREFIX.
"societe_remise_except WHERE fk_invoice_supplier_source = ".((int) $this->
id);
3224 $obj = $this->
db->fetch_object(
$resql);
3225 if (!empty($obj->fk_invoice_supplier)) {
3244 public $element =
'facture_fourn_det';
3249 public $table_element =
'facture_fourn_det';
3263 public $product_ref;
3270 public $ref_supplier;
3276 public $product_desc;
3303 public $fk_facture_fourn;
3316 public $description;
3321 public $skip_update_total;
3326 public $situation_percent;
3337 public $vat_src_code;
3349 public $localtax1_tx;
3355 public $localtax2_tx;
3367 public $remise_percent;
3391 public $total_localtax1;
3397 public $total_localtax2;
3408 public $product_type;
3414 public $product_label;
3428 public $fk_remise_except;
3433 public $fk_parent_line;
3435 public $special_code;
3446 public $localtax1_type;
3452 public $localtax2_type;
3458 public $fk_multicurrency;
3460 public $multicurrency_code;
3461 public $multicurrency_subprice;
3462 public $multicurrency_total_ht;
3463 public $multicurrency_total_tva;
3464 public $multicurrency_total_ttc;
3485 $sql =
'SELECT f.rowid, f.ref as ref_supplier, f.description, f.date_start, f.date_end, f.pu_ht, f.pu_ttc, f.qty, f.remise_percent, f.tva_tx';
3486 $sql .=
', f.localtax1_type, f.localtax2_type, f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2, f.fk_remise_except';
3487 $sql .=
', f.total_ht, f.tva as total_tva, f.total_ttc, f.fk_facture_fourn, f.fk_product, f.product_type, f.info_bits, f.rang, f.special_code, f.fk_parent_line, f.fk_unit';
3488 $sql .=
', p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc';
3489 $sql .=
', f.multicurrency_subprice, f.multicurrency_total_ht, f.multicurrency_total_tva, multicurrency_total_ttc';
3490 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn_det as f';
3491 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON f.fk_product = p.rowid';
3492 $sql .=
' WHERE f.rowid = '.((int) $rowid);
3493 $sql .=
' ORDER BY f.rang, f.rowid';
3495 $query = $this->
db->query($sql);
3498 $this->errors[] = $this->
db->error();
3502 if (!$this->
db->num_rows($query)) {
3506 $obj = $this->
db->fetch_object($query);
3508 $this->
id = $obj->rowid;
3509 $this->
rowid = $obj->rowid;
3510 $this->fk_facture_fourn = $obj->fk_facture_fourn;
3512 $this->date_start = $obj->date_start;
3513 $this->date_end = $obj->date_end;
3514 $this->product_ref = $obj->product_ref;
3515 $this->ref_supplier = $obj->ref_supplier;
3516 $this->product_desc = $obj->product_desc;
3518 $this->subprice = $obj->pu_ht;
3519 $this->pu_ht = $obj->pu_ht;
3520 $this->pu_ttc = $obj->pu_ttc;
3521 $this->tva_tx = $obj->tva_tx;
3522 $this->localtax1_tx = $obj->localtax1_tx;
3523 $this->localtax2_tx = $obj->localtax2_tx;
3524 $this->localtax1_type = $obj->localtax1_type;
3525 $this->localtax2_type = $obj->localtax2_type;
3527 $this->qty = $obj->qty;
3528 $this->remise_percent = $obj->remise_percent;
3529 $this->fk_remise_except = $obj->fk_remise_except;
3531 $this->total_ht = $obj->total_ht;
3532 $this->total_tva = $obj->total_tva;
3533 $this->total_localtax1 = $obj->total_localtax1;
3534 $this->total_localtax2 = $obj->total_localtax2;
3535 $this->total_ttc = $obj->total_ttc;
3536 $this->fk_product = $obj->fk_product;
3537 $this->product_type = $obj->product_type;
3538 $this->product_label = $obj->product_label;
3539 $this->info_bits = $obj->info_bits;
3540 $this->tva_npr = ($obj->info_bits & 1 == 1) ? 1 : 0;
3541 $this->fk_parent_line = $obj->fk_parent_line;
3542 $this->special_code = $obj->special_code;
3543 $this->rang = $obj->rang;
3544 $this->fk_unit = $obj->fk_unit;
3546 $this->multicurrency_subprice = $obj->multicurrency_subprice;
3547 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
3548 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
3549 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
3562 public function delete($notrigger = 0)
3564 global $user, $conf;
3566 dol_syslog(get_class($this).
"::deleteline rowid=".((
int) $this->
id), LOG_DEBUG);
3573 if ($this->
call_trigger(
'LINEBILL_SUPPLIER_DELETE', $user) < 0) {
3585 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
3591 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn_det ';
3592 $sql .=
" WHERE rowid = ".((int) $this->
id);
3593 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
3597 $this->error = $this->
db->lasterror();
3602 $this->
db->commit();
3605 $this->
db->rollback();
3624 if (empty($this->qty)) {
3628 if ($this->product_type < 0) {
3633 if (empty($this->remise_percent)) {
3634 $this->remise_percent = 0;
3636 if (empty($this->tva_tx)) {
3639 if (empty($this->localtax1_tx)) {
3640 $this->localtax1_tx = 0;
3642 if (empty($this->localtax2_tx)) {
3643 $this->localtax2_tx = 0;
3646 if (empty($this->pa_ht)) {
3649 if (empty($this->multicurrency_subprice)) {
3650 $this->multicurrency_subprice = 0;
3652 if (empty($this->multicurrency_total_ht)) {
3653 $this->multicurrency_total_ht = 0;
3655 if (empty($this->multicurrency_total_tva)) {
3656 $this->multicurrency_total_tva = 0;
3658 if (empty($this->multicurrency_total_ttc)) {
3659 $this->multicurrency_total_ttc = 0;
3662 $fk_product = (int) $this->fk_product;
3663 $fk_unit = (int) $this->fk_unit;
3667 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn_det SET";
3668 $sql .=
" description = '".$this->db->escape($this->
description).
"'";
3669 $sql .=
", ref = '".$this->db->escape($this->ref_supplier ? $this->ref_supplier : $this->
ref).
"'";
3670 $sql .=
", date_start = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3671 $sql .=
", date_end = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3672 $sql .=
", pu_ht = ".price2num($this->pu_ht);
3673 $sql .=
", pu_ttc = ".price2num($this->pu_ttc);
3674 $sql .=
", qty = ".price2num($this->qty);
3675 $sql .=
", remise_percent = ".price2num($this->remise_percent);
3676 if ($this->fk_remise_except > 0) $sql .=
", fk_remise_except=".((int) $this->fk_remise_except);
3677 else $sql .=
", fk_remise_except=null";
3678 $sql .=
", vat_src_code = '".$this->db->escape(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
3679 $sql .=
", tva_tx = ".price2num($this->tva_tx);
3680 $sql .=
", localtax1_tx = ".price2num($this->localtax1_tx);
3681 $sql .=
", localtax2_tx = ".price2num($this->localtax2_tx);
3682 $sql .=
", localtax1_type = '".$this->db->escape($this->localtax1_type).
"'";
3683 $sql .=
", localtax2_type = '".$this->db->escape($this->localtax2_type).
"'";
3684 $sql .=
", total_ht = ".price2num($this->total_ht);
3685 $sql .=
", tva= ".price2num($this->total_tva);
3686 $sql .=
", total_localtax1= ".price2num($this->total_localtax1);
3687 $sql .=
", total_localtax2= ".price2num($this->total_localtax2);
3688 $sql .=
", total_ttc = ".price2num($this->total_ttc);
3689 $sql .=
", fk_product = ".($fk_product > 0 ? (int) $fk_product :
'null');
3690 $sql .=
", product_type = ".((int) $this->product_type);
3691 $sql .=
", info_bits = ".((int) $this->info_bits);
3692 $sql .=
", fk_unit = ".($fk_unit > 0 ? (int) $fk_unit :
'null');
3694 if (!empty($this->rang)) {
3695 $sql .=
", rang=".((int) $this->rang);
3699 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
3700 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
3701 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
3702 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
3704 $sql .=
" WHERE rowid = ".((int) $this->
id);
3706 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3710 $this->
db->rollback();
3711 $this->error = $this->
db->lasterror();
3715 $this->
rowid = $this->id;
3725 if (!$error && !$notrigger) {
3726 global $langs, $user;
3729 if ($this->
call_trigger(
'LINEBILL_SUPPLIER_MODIFY', $user) < 0) {
3730 $this->
db->rollback();
3737 $this->
db->rollback();
3741 $this->
db->commit();
3753 global $user, $conf, $langs;
3757 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
3760 $this->desc = trim($this->desc);
3761 if (empty($this->tva_tx)) {
3764 if (empty($this->localtax1_tx)) {
3765 $this->localtax1_tx = 0;
3767 if (empty($this->localtax2_tx)) {
3768 $this->localtax2_tx = 0;
3770 if (empty($this->localtax1_type)) {
3771 $this->localtax1_type =
'0';
3773 if (empty($this->localtax2_type)) {
3774 $this->localtax2_type =
'0';
3776 if (empty($this->total_tva)) {
3777 $this->total_tva = 0;
3779 if (empty($this->total_localtax1)) {
3780 $this->total_localtax1 = 0;
3782 if (empty($this->total_localtax2)) {
3783 $this->total_localtax2 = 0;
3785 if (empty($this->rang)) {
3788 if (empty($this->remise_percent)) {
3789 $this->remise_percent = 0;
3791 if (empty($this->info_bits)) {
3792 $this->info_bits = 0;
3794 if (empty($this->subprice)) {
3795 $this->subprice = 0;
3797 if (empty($this->special_code)) {
3798 $this->special_code = 0;
3800 if (empty($this->fk_parent_line)) {
3801 $this->fk_parent_line = 0;
3803 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
3804 $this->situation_percent = 100;
3807 if (empty($this->pa_ht)) {
3810 if (empty($this->multicurrency_subprice)) {
3811 $this->multicurrency_subprice = 0;
3813 if (empty($this->multicurrency_total_ht)) {
3814 $this->multicurrency_total_ht = 0;
3816 if (empty($this->multicurrency_total_tva)) {
3817 $this->multicurrency_total_tva = 0;
3819 if (empty($this->multicurrency_total_ttc)) {
3820 $this->multicurrency_total_ttc = 0;
3825 if ($this->product_type < 0) {
3826 $this->error =
'ErrorProductTypeMustBe0orMore';
3829 if (!empty($this->fk_product) && $this->fk_product > 0) {
3833 $this->error =
'ErrorProductIdDoesNotExists';
3841 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
3842 $sql .=
' (fk_facture_fourn, fk_parent_line, label, description, ref, qty,';
3843 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
3844 $sql .=
' fk_product, product_type, remise_percent, fk_remise_except, pu_ht, pu_ttc,';
3845 $sql .=
' date_start, date_end, fk_code_ventilation, rang, special_code,';
3846 $sql .=
' info_bits, total_ht, tva, total_ttc, total_localtax1, total_localtax2, fk_unit';
3847 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
3849 $sql .=
" VALUES (".$this->fk_facture_fourn.
",";
3850 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
3851 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
3852 $sql .=
" '".$this->db->escape($this->desc ? $this->desc : $this->
description).
"',";
3853 $sql .=
" '".$this->db->escape($this->ref_supplier).
"',";
3854 $sql .=
" ".price2num($this->qty).
",";
3856 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
3857 $sql .=
" ".price2num($this->tva_tx).
",";
3858 $sql .=
" ".price2num($this->localtax1_tx).
",";
3859 $sql .=
" ".price2num($this->localtax2_tx).
",";
3860 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3861 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3862 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
3863 $sql .=
" ".((int) $this->product_type).
",";
3864 $sql .=
" ".price2num($this->remise_percent).
",";
3865 $sql .=
' '.(! empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) :
"null").
',';
3866 $sql .=
" ".price2num($this->subprice).
",";
3867 $sql .=
" ".(!empty($this->qty) ?
price2num($this->total_ttc / $this->qty) :
price2num($this->total_ttc)).
",";
3868 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3869 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3870 $sql .=
' '.(!empty($this->fk_code_ventilation) ? $this->fk_code_ventilation : 0).
',';
3871 $sql .=
' '.((int) $this->rang).
',';
3872 $sql .=
' '.((int) $this->special_code).
',';
3873 $sql .=
" ".((int) $this->info_bits).
",";
3874 $sql .=
" ".price2num($this->total_ht).
",";
3875 $sql .=
" ".price2num($this->total_tva).
",";
3876 $sql .=
" ".price2num($this->total_ttc).
",";
3877 $sql .=
" ".price2num($this->total_localtax1).
",";
3878 $sql .=
" ".price2num($this->total_localtax2);
3879 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
3880 $sql .=
", ".(int) $this->fk_multicurrency;
3881 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
3882 $sql .=
", ".price2num($this->multicurrency_subprice);
3883 $sql .=
", ".price2num($this->multicurrency_total_ht);
3884 $sql .=
", ".price2num($this->multicurrency_total_tva);
3885 $sql .=
", ".price2num($this->multicurrency_total_ttc);
3890 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
3891 $this->
rowid = $this->id;
3902 if ($this->fk_remise_except) {
3904 $result = $discount->fetch($this->fk_remise_except);
3909 if ($discount->fk_facture_line > 0) {
3910 if (empty($noerrorifdiscountalreadylinked)) {
3911 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
3912 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
3913 $this->
db->rollback();
3917 $result = $discount->link_to_invoice($this->
rowid, 0);
3919 $this->error = $discount->error;
3920 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
3921 $this->
db->rollback();
3926 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
3927 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
3928 $this->
db->rollback();
3932 $this->error = $discount->error;
3933 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
3934 $this->
db->rollback();
3939 if (!$error && !$notrigger) {
3941 $result = $this->
call_trigger(
'LINEBILL_SUPPLIER_CREATE', $user);
3943 $this->
db->rollback();
3949 $this->
db->commit();
3952 $this->error = $this->
db->error();
3953 $this->
db->rollback();
3970 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn_det SET";
3971 $sql .=
" total_ht = ".price2num($this->total_ht);
3972 $sql .=
", tva= ".price2num($this->total_tva);
3973 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
3974 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
3975 $sql .=
", total_ttc = ".price2num($this->total_ttc);
3976 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
3978 dol_syslog(
"FactureFournisseurLigne.class.php::update_total", LOG_DEBUG);
3982 $this->
db->commit();
3985 $this->error = $this->
db->error();
3986 $this->
db->rollback();