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();
1134 public function update($user =
null, $notrigger = 0)
1136 global $conf, $langs;
1140 if (empty($this->
type)) {
1143 if (isset($this->
ref)) {
1144 $this->
ref = trim($this->
ref);
1146 if (isset($this->ref_supplier)) {
1147 $this->ref_supplier = trim($this->ref_supplier);
1149 if (isset($this->ref_ext)) {
1150 $this->ref_ext = trim($this->ref_ext);
1152 if (isset($this->entity)) {
1153 $this->entity = trim($this->entity);
1155 if (isset($this->
type)) {
1158 if (isset($this->fk_soc)) {
1159 $this->fk_soc = trim($this->fk_soc);
1161 if (isset($this->label)) {
1162 $this->label = trim($this->label);
1164 if (isset($this->libelle)) {
1165 $this->libelle = trim($this->libelle);
1167 if (isset($this->paye)) {
1168 $this->paye = trim($this->paye);
1170 if (isset($this->close_code)) {
1171 $this->close_code = trim($this->close_code);
1173 if (isset($this->close_note)) {
1174 $this->close_note = trim($this->close_note);
1176 if (isset($this->localtax1)) {
1177 $this->localtax1 = trim($this->localtax1);
1179 if (isset($this->localtax2)) {
1180 $this->localtax2 = trim($this->localtax2);
1182 if (empty($this->total_ht)) {
1183 $this->total_ht = 0;
1185 if (empty($this->total_tva)) {
1186 $this->total_tva = 0;
1190 if (isset($this->total_ttc)) {
1191 $this->total_ttc = trim($this->total_ttc);
1193 if (isset($this->statut)) {
1194 $this->statut = (int) $this->statut;
1196 if (isset($this->status)) {
1197 $this->status = (int) $this->status;
1199 if (isset($this->author)) {
1200 $this->author = trim($this->author);
1202 if (isset($this->fk_user_valid)) {
1203 $this->fk_user_valid = trim($this->fk_user_valid);
1205 if (isset($this->fk_facture_source)) {
1206 $this->fk_facture_source = trim($this->fk_facture_source);
1208 if (isset($this->fk_project)) {
1209 if (empty($this->fk_project)) $this->fk_project =
null;
1210 else $this->fk_project = intval($this->fk_project);
1212 if (isset($this->cond_reglement_id)) {
1213 $this->cond_reglement_id = trim($this->cond_reglement_id);
1215 if (isset($this->note_private)) {
1216 $this->note = trim($this->note_private);
1218 if (isset($this->note_public)) {
1219 $this->note_public = trim($this->note_public);
1221 if (isset($this->model_pdf)) {
1222 $this->model_pdf = trim($this->model_pdf);
1224 if (isset($this->import_key)) {
1225 $this->import_key = trim($this->import_key);
1233 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn SET";
1234 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1235 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1236 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1237 $sql .=
" entity=".(isset($this->entity) ? $this->entity :
"null").
",";
1238 $sql .=
" type=".(isset($this->
type) ? $this->
type :
"null").
",";
1239 $sql .=
" fk_soc=".(isset($this->fk_soc) ? $this->fk_soc :
"null").
",";
1240 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
1241 $sql .=
" datef=".(dol_strlen($this->date) != 0 ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
1243 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
1245 $sql .=
" libelle=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
1246 $sql .=
" paye=".(isset($this->paye) ? $this->paye :
"null").
",";
1247 $sql .=
" close_code=".(isset($this->close_code) ?
"'".$this->db->escape($this->close_code).
"'" :
"null").
",";
1248 $sql .=
" close_note=".(isset($this->close_note) ?
"'".$this->db->escape($this->close_note).
"'" :
"null").
",";
1250 $sql .=
" localtax1=".(isset($this->localtax1) ? $this->localtax1 :
"null").
",";
1251 $sql .=
" localtax2=".(isset($this->localtax2) ? $this->localtax2 :
"null").
",";
1252 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
1253 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
1254 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
1255 $sql .=
" fk_statut=".(isset($this->status) ? $this->status : (isset($this->statut) ? $this->statut :
"null")).
",";
1256 $sql .=
" fk_user_author=".(isset($this->author) ? $this->author :
"null").
",";
1257 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? $this->fk_user_valid :
"null").
",";
1258 $sql .=
" fk_facture_source=".(isset($this->fk_facture_source) ? $this->fk_facture_source :
"null").
",";
1259 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
1260 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
1261 $sql .=
" date_lim_reglement=".(dol_strlen($this->date_echeance) != 0 ?
"'".$this->db->idate($this->date_echeance).
"'" :
'null').
",";
1262 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1263 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1264 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1265 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
1266 $sql .=
" WHERE rowid=".((int) $this->
id);
1270 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1276 if ($this->
db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1277 $this->errors[] = $langs->trans(
'ErrorRefAlreadyExists');
1279 $this->errors[] =
"Error ".$this->db->lasterror();
1293 $result = $this->
call_trigger(
'BILL_SUPPLIER_MODIFY', $user);
1303 foreach ($this->errors as $errmsg) {
1304 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1305 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1307 $this->
db->rollback();
1310 $this->
db->commit();
1325 global $conf, $langs;
1327 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1328 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1333 $result =
$remise->fetch($idremise);
1336 if (
$remise->fk_invoice_supplier) {
1337 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
1338 $this->
db->rollback();
1343 $facligne->fk_facture_fourn = $this->id;
1344 $facligne->fk_remise_except =
$remise->id;
1345 $facligne->desc =
$remise->description;
1346 $facligne->vat_src_code =
$remise->vat_src_code;
1347 $facligne->tva_tx =
$remise->tva_tx;
1348 $facligne->subprice = -
$remise->amount_ht;
1349 $facligne->fk_product = 0;
1350 $facligne->product_type = 0;
1352 $facligne->remise_percent = 0;
1353 $facligne->rang = -1;
1354 $facligne->info_bits = 2;
1356 if (!empty($conf->global->MAIN_ADD_LINE_AT_POSITION)) {
1357 $facligne->rang = 1;
1358 $linecount = count($this->lines);
1359 for ($ii = 1; $ii <= $linecount; $ii++) {
1365 if (
$remise->fk_invoice_supplier_source > 0) {
1367 $srcinvoice->fetch(
$remise->fk_invoice_supplier_source);
1368 $totalcostpriceofinvoice = 0;
1369 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmargin.class.php';
1371 $arraytmp = $formmargin->getMarginInfosArray($srcinvoice,
false);
1372 $facligne->pa_ht = $arraytmp[
'pa_total'];
1375 $facligne->total_ht = -
$remise->amount_ht;
1376 $facligne->total_tva = -
$remise->amount_tva;
1377 $facligne->total_ttc = -
$remise->amount_ttc;
1379 $facligne->multicurrency_subprice = -
$remise->multicurrency_subprice;
1380 $facligne->multicurrency_total_ht = -
$remise->multicurrency_total_ht;
1381 $facligne->multicurrency_total_tva = -
$remise->multicurrency_total_tva;
1382 $facligne->multicurrency_total_ttc = -
$remise->multicurrency_total_ttc;
1384 $lineid = $facligne->insert();
1389 $result =
$remise->link_to_invoice($lineid, 0,
'supplier');
1391 $this->error =
$remise->error;
1392 $this->
db->rollback();
1396 $this->
db->commit();
1399 $this->error = $facligne->error;
1400 $this->
db->rollback();
1404 $this->error = $facligne->error;
1405 $this->
db->rollback();
1409 $this->
db->rollback();
1422 public function delete(
User $user, $notrigger = 0)
1424 global $langs, $conf;
1428 dol_syslog(
"FactureFournisseur::delete rowid=".$rowid, LOG_DEBUG);
1435 if (!$error && !$notrigger) {
1437 $result = $this->
call_trigger(
'BILL_SUPPLIER_DELETE', $user);
1439 $this->
db->rollback();
1447 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'societe_remise_except';
1448 $sql .=
' WHERE fk_invoice_supplier_source = '.((int) $rowid);
1449 $sql .=
' AND fk_invoice_supplier_line IS NULL';
1454 $list_rowid_det = array();
1455 foreach ($this->lines as $key => $invoiceline) {
1456 $list_rowid_det[] = $invoiceline->rowid;
1460 if (count($list_rowid_det)) {
1461 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
1462 $sql .=
' SET fk_invoice_supplier = NULL, fk_invoice_supplier_line = NULL';
1463 $sql .=
' WHERE fk_invoice_supplier_line IN ('.$this->db->sanitize(join(
',', $list_rowid_det)).
')';
1465 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1466 if (!$this->
db->query($sql)) {
1473 $main = MAIN_DB_PREFIX.
'facture_fourn_det';
1474 $ef = $main.
"_extrafields";
1475 $sqlef =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_facture_fourn = ".((int) $rowid).
")";
1476 $resqlef = $this->
db->query($sqlef);
1477 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn_det WHERE fk_facture_fourn = '.((int) $rowid);
1478 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1480 if ($resqlef &&
$resql) {
1481 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn WHERE rowid = '.((int) $rowid);
1482 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
1483 $resql2 = $this->
db->query($sql);
1513 if ($conf->fournisseur->facture->dir_output) {
1514 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1517 $dir = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$ref;
1518 $file = $dir.
"/".$ref.
".pdf";
1519 if (file_exists($file)) {
1521 $this->error =
'ErrorFailToDeleteFile';
1525 if (file_exists($dir)) {
1529 $this->error =
'ErrorFailToDeleteDir';
1541 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
1546 dol_syslog(get_class($this).
"::delete $this->id by $user->id", LOG_DEBUG);
1547 $this->
db->commit();
1550 $this->error = $this->
db->lasterror();
1551 $this->
db->rollback();
1568 public function set_paid($user, $close_code =
'', $close_note =
'')
1571 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
1572 return $this->
setPaid($user, $close_code, $close_note);
1583 public function setPaid($user, $close_code =
'', $close_note =
'')
1587 if ($this->paye != 1) {
1592 dol_syslog(
"FactureFournisseur::set_paid", LOG_DEBUG);
1594 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn SET';
1595 $sql .=
' fk_statut = '.self::STATUS_CLOSED;
1600 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
1603 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
1605 $sql .=
', fk_user_closing = '.((int) $user->id);
1606 $sql .=
", date_closing = '".$this->db->idate($now).
"'";
1607 $sql .=
' WHERE rowid = '.((int) $this->
id);
1612 $result = $this->
call_trigger(
'BILL_SUPPLIER_PAYED', $user);
1619 $this->error = $this->
db->error();
1624 $this->
db->commit();
1627 $this->
db->rollback();
1649 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
1667 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn';
1668 $sql .=
' SET paye=0, fk_statut='.self::STATUS_VALIDATED.
', close_code=null, close_note=null,';
1669 $sql .=
' date_closing=null,';
1670 $sql .=
' fk_user_closing=null';
1671 $sql .=
' WHERE rowid = '.((int) $this->
id);
1673 dol_syslog(get_class($this).
"::set_unpaid", LOG_DEBUG);
1677 $result = $this->
call_trigger(
'BILL_SUPPLIER_UNPAYED', $user);
1684 $this->error = $this->
db->error();
1689 $this->
db->commit();
1692 $this->
db->rollback();
1707 public function setCanceled($user, $close_code =
'', $close_note =
'')
1709 dol_syslog(get_class($this).
"::setCanceled rowid=".((
int) $this->
id), LOG_DEBUG);
1713 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture_fourn SET';
1714 $sql .=
' fk_statut='.self::STATUS_ABANDONED;
1716 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
1719 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
1721 $sql .=
" WHERE rowid = ".((int) $this->
id);
1727 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
1728 $sql .=
' SET fk_invoice_supplier = NULL';
1729 $sql .=
' WHERE fk_invoice_supplier = '.((int) $this->
id);
1734 $result = $this->
call_trigger(
'BILL_SUPPLIER_CANCEL', $user);
1736 $this->
db->rollback();
1741 $this->
db->commit();
1744 $this->error = $this->
db->error().
" sql=".$sql;
1745 $this->
db->rollback();
1749 $this->error = $this->
db->error().
" sql=".$sql;
1750 $this->
db->rollback();
1764 public function validate($user, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
1766 global $conf, $langs;
1768 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1773 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number.
', idwarehouse='.$idwarehouse);
1780 if ($this->statut > self::STATUS_DRAFT) {
1781 dol_syslog(get_class($this).
"::validate no draft status", LOG_WARNING);
1784 if (preg_match(
'/^'.preg_quote($langs->trans(
"CopyOf").
' ').
'/', $this->ref_supplier)) {
1785 $langs->load(
"errors");
1786 $this->error = $langs->trans(
"ErrorFieldFormat", $langs->transnoentities(
"RefSupplier")).
'. '.$langs->trans(
'RemoveString', $langs->transnoentitiesnoconv(
"CopyOf"));
1789 if (count($this->lines) <= 0) {
1790 $langs->load(
"errors");
1791 $this->error = $langs->trans(
"ErrorObjectMustHaveLinesToBeValidated", $this->
ref);
1798 if ($force_number) {
1799 $num = $force_number;
1800 } elseif (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
1807 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn";
1808 $sql .=
" SET ref='".$this->db->escape($num).
"', fk_statut = 1, fk_user_valid = ".((int) $user->id).
", date_valid = '".$this->
db->idate($now).
"'";
1809 $sql .=
" WHERE rowid = ".((int) $this->
id);
1811 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
1815 if (!$error && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
1816 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1817 $langs->load(
"agenda");
1819 $cpt = count($this->lines);
1820 for ($i = 0; $i < $cpt; $i++) {
1821 if ($this->lines[$i]->fk_product > 0) {
1822 $this->line = $this->lines[$i];
1824 $mouvP->origin = &$this;
1825 $mouvP->setOrigin($this->element, $this->
id);
1827 $up_ht_disc = $this->lines[$i]->pu_ht;
1828 if (!empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) {
1829 $up_ht_disc =
price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100,
'MU');
1832 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
1834 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
1845 if (!$error && empty($notrigger)) {
1847 $result = $this->
call_trigger(
'BILL_SUPPLIER_VALIDATE', $user);
1855 $this->oldref = $this->ref;
1858 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
1860 $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).
"'";
1861 $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;
1864 $error++; $this->error = $this->
db->lasterror();
1870 $dirsource = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$oldref;
1871 $dirdest = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'invoice_supplier').$newref;
1872 if (!$error && file_exists($dirsource)) {
1873 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
1875 if (@rename($dirsource, $dirdest)) {
1878 $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,
'/'));
1879 foreach ($listoffiles as $fileentry) {
1880 $dirsource = $fileentry[
'name'];
1881 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
1882 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
1883 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
1884 @rename($dirsource, $dirdest);
1899 $this->
db->commit();
1902 $this->
db->rollback();
1906 $this->error = $this->
db->error();
1907 $this->
db->rollback();
1922 global $conf, $langs;
1926 if ($this->statut == self::STATUS_DRAFT) {
1927 dol_syslog(__METHOD__.
" already draft status", LOG_WARNING);
1935 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn";
1936 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT;
1937 $sql .=
" WHERE rowid = ".((int) $this->
id);
1939 $result = $this->
db->query($sql);
1942 $this->oldcopy = clone $this;
1946 if ($result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
1947 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1948 $langs->load(
"agenda");
1950 $cpt = count($this->lines);
1951 for ($i = 0; $i < $cpt; $i++) {
1952 if ($this->lines[$i]->fk_product > 0) {
1954 $mouvP->origin = &$this;
1955 $mouvP->setOrigin($this->element, $this->
id);
1958 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
1960 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
1966 if (!$error && empty($notrigger)) {
1968 $result = $this->
call_trigger(
'BILL_SUPPLIER_UNVALIDATE', $user);
1975 $this->
db->commit();
1978 $this->
db->rollback();
1982 $this->error = $this->
db->error();
1983 $this->
db->rollback();
2022 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)
2024 global $langs, $mysoc, $conf;
2026 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);
2027 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2029 if ($this->statut == self::STATUS_DRAFT) {
2031 if (empty($remise_percent)) {
2032 $remise_percent = 0;
2037 if (empty($info_bits)) {
2043 if (empty($ventil)) {
2046 if (empty($txtva)) {
2049 if (empty($txlocaltax1)) {
2052 if (empty($txlocaltax2)) {
2056 $remise_percent =
price2num($remise_percent);
2059 if (!preg_match(
'/\((.*)\)/', $txtva)) {
2065 if ($date_start && $date_end && $date_start > $date_end) {
2066 $langs->load(
"errors");
2067 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2073 if ($fk_product > 0) {
2074 if (!empty($conf->global->SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY)) {
2076 dol_syslog(get_class($this).
"::addline we check supplier prices fk_product=".$fk_product.
" qty=".$qty.
" ref_supplier=".$ref_supplier);
2078 if ($prod->fetch($fk_product) > 0) {
2079 $product_type = $prod->type;
2080 $label = $prod->label;
2081 $fk_prod_fourn_price = 0;
2085 $result = $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product,
'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
2088 $pu = $prod->fourn_pu;
2090 $ref_supplier = $prod->ref_supplier;
2092 if ($remise_percent == 0 && $prod->remise_percent != 0) {
2093 $remise_percent = $prod->remise_percent;
2097 $langs->load(
"errors");
2098 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2099 $this->
db->rollback();
2100 dol_syslog(get_class($this).
"::addline we did not found supplier price, so we can't guess unit price");
2105 if ($result == -1) {
2106 $langs->load(
"errors");
2107 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2108 $this->
db->rollback();
2109 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_DEBUG);
2113 $this->error = $prod->error;
2114 $this->
db->rollback();
2115 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_ERR);
2119 $this->error = $prod->error;
2120 $this->
db->rollback();
2125 $product_type = $type;
2128 if (!empty($conf->multicurrency->enabled) && $pu_devise > 0) {
2137 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
2138 $vat_src_code = $reg[1];
2139 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
2147 $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);
2148 $total_ht = $tabprice[0];
2149 $total_tva = $tabprice[1];
2150 $total_ttc = $tabprice[2];
2151 $total_localtax1 = $tabprice[9];
2152 $total_localtax2 = $tabprice[10];
2153 $pu_ht = $tabprice[3];
2156 $multicurrency_total_ht = $tabprice[16];
2157 $multicurrency_total_tva = $tabprice[17];
2158 $multicurrency_total_ttc = $tabprice[18];
2159 $pu_ht_devise = $tabprice[19];
2168 $rang = $rangmax + 1;
2174 $this->line->context = $this->context;
2176 $this->line->fk_facture_fourn = $this->id;
2178 $this->line->desc = $desc;
2179 $this->line->ref_supplier = $ref_supplier;
2181 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs($qty) : $qty);
2182 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
2184 $this->line->vat_src_code = $vat_src_code;
2185 $this->line->tva_tx = $txtva;
2186 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
2187 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
2188 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2189 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2191 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ht) : $total_ht);
2192 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_tva) : $total_tva);
2193 $this->line->total_localtax1 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax1) : $total_localtax1);
2194 $this->line->total_localtax2 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax2) : $total_localtax2);
2195 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ttc) : $total_ttc);
2197 $this->line->fk_product = $fk_product;
2198 $this->line->product_type = $type;
2199 $this->line->remise_percent = $remise_percent;
2200 $this->line->date_start = $date_start;
2201 $this->line->date_end = $date_end;
2202 $this->line->fk_code_ventilation = $ventil;
2203 $this->line->rang = $rang;
2204 $this->line->info_bits = $info_bits;
2205 $this->line->fk_remise_except = $fk_remise_except;
2207 $this->line->special_code = ((string) $special_code !=
'' ? $special_code : $this->special_code);
2208 $this->line->fk_parent_line = $fk_parent_line;
2209 $this->line->origin = $this->origin;
2210 $this->line->origin_id = $origin_id;
2211 $this->line->fk_unit = $fk_unit;
2214 $this->line->fk_multicurrency = $this->fk_multicurrency;
2215 $this->line->multicurrency_code = $this->multicurrency_code;
2216 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht_devise) : $pu_ht_devise);
2218 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ht) : $multicurrency_total_ht);
2219 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_tva) : $multicurrency_total_tva);
2220 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
2222 if (is_array($array_options) && count($array_options) > 0) {
2223 $this->line->array_options = $array_options;
2226 $result = $this->line->insert($notrigger);
2229 if (!empty($fk_parent_line)) {
2231 } elseif ($rang > 0 && $rang <= count($this->lines)) {
2232 $linecount = count($this->lines);
2233 for ($ii = $rang; $ii <= $linecount; $ii++) {
2239 $result = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2241 $this->
db->commit();
2242 return $this->line->id;
2244 $this->error = $this->
db->error();
2245 $this->
db->rollback();
2249 $this->error = $this->line->error;
2250 $this->errors = $this->line->errors;
2251 $this->
db->rollback();
2284 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)
2286 global $mysoc, $langs;
2288 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);
2289 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2293 $remise_percent =
price2num($remise_percent);
2302 if ($date_start && $date_end && $date_start > $date_end) {
2303 $langs->load(
"errors");
2304 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2309 if (empty($vatrate)) {
2312 if (empty($txlocaltax1)) {
2315 if (empty($txlocaltax2)) {
2333 if (preg_match(
'/\((.*)\)/', $vatrate, $reg)) {
2334 $vat_src_code = $reg[1];
2335 $vatrate = preg_replace(
'/\s*\(.*\)/',
'', $vatrate);
2338 $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);
2339 $total_ht = $tabprice[0];
2340 $total_tva = $tabprice[1];
2341 $total_ttc = $tabprice[2];
2342 $pu_ht = $tabprice[3];
2343 $pu_tva = $tabprice[4];
2344 $pu_ttc = $tabprice[5];
2345 $total_localtax1 = $tabprice[9];
2346 $total_localtax2 = $tabprice[10];
2349 $multicurrency_total_ht = $tabprice[16];
2350 $multicurrency_total_tva = $tabprice[17];
2351 $multicurrency_total_ttc = $tabprice[18];
2352 $pu_ht_devise = $tabprice[19];
2354 if (empty($info_bits)) {
2361 $line->fetch_optionals();
2363 $staticline = clone $line;
2367 $result = $product->fetch($idproduct);
2368 $product_type = $product->type;
2370 $idproduct = $staticline->fk_product;
2371 $product_type = $type;
2374 $line->oldline = $staticline;
2375 $line->context = $this->context;
2377 $line->description = $desc;
2379 $line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs($qty) : $qty);
2380 $line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
2381 $line->pu_ht = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
2382 $line->pu_ttc = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ttc) : $pu_ttc);
2384 $line->remise_percent = $remise_percent;
2385 $line->ref_supplier = $ref_supplier;
2387 $line->date_start = $date_start;
2388 $line->date_end = $date_end;
2390 $line->vat_src_code = $vat_src_code;
2391 $line->tva_tx = $vatrate;
2392 $line->localtax1_tx = $txlocaltax1;
2393 $line->localtax2_tx = $txlocaltax2;
2394 $line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2395 $line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2397 $line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ht) : $total_ht);
2398 $line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_tva) : $total_tva);
2399 $line->total_localtax1 = $total_localtax1;
2400 $line->total_localtax2 = $total_localtax2;
2401 $line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ttc) : $total_ttc);
2403 $line->fk_product = $idproduct;
2404 $line->product_type = $product_type;
2405 $line->info_bits = $info_bits;
2406 $line->fk_unit = $fk_unit;
2407 $line->rang = $rang;
2409 if (is_array($array_options) && count($array_options) > 0) {
2411 foreach ($array_options as $key => $value) {
2412 $line->array_options[$key] = $array_options[$key];
2417 $line->multicurrency_subprice = $pu_ht_devise;
2418 $line->multicurrency_total_ht = $multicurrency_total_ht;
2419 $line->multicurrency_total_tva = $multicurrency_total_tva;
2420 $line->multicurrency_total_ttc = $multicurrency_total_ttc;
2422 $res = $line->update($notrigger);
2425 $this->errors[] = $line->error;
2428 $res = $this->
update_price(
'',
'auto', 0, $this->thirdparty);
2450 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
2451 $sql .=
' SET fk_invoice_supplier_line = NULL';
2452 $sql .=
' WHERE fk_invoice_supplier_line = '.((int) $rowid);
2454 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
2455 $result = $this->
db->query($sql);
2457 $this->error = $this->
db->error();
2458 $this->
db->rollback();
2464 if ($line->fetch($rowid) < 1) {
2468 $res = $line->delete($notrigger);
2471 $this->errors[] = $line->error;
2472 $this->
db->rollback();
2478 $this->
db->commit();
2481 $this->
db->rollback();
2482 $this->error = $this->
db->lasterror();
2497 $sql =
'SELECT c.rowid, datec, tms as datem, ';
2498 $sql .=
' fk_user_author, fk_user_modif, fk_user_valid';
2499 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as c';
2500 $sql .=
' WHERE c.rowid = '.((int) $id);
2502 $result = $this->
db->query($sql);
2504 if ($this->
db->num_rows($result)) {
2505 $obj = $this->
db->fetch_object($result);
2507 $this->
id = $obj->rowid;
2508 if ($obj->fk_user_author) {
2509 $cuser =
new User($this->
db);
2510 $cuser->fetch($obj->fk_user_author);
2511 $this->user_creation = $cuser;
2513 if ($obj->fk_user_valid) {
2514 $vuser =
new User($this->
db);
2515 $vuser->fetch($obj->fk_user_valid);
2516 $this->user_validation = $vuser;
2518 if ($obj->fk_user_modif) {
2519 $muser =
new User($this->
db);
2520 $muser->fetch($obj->fk_user_modif);
2521 $this->user_modification = $muser;
2523 $this->date_creation = $this->
db->jdate($obj->datec);
2524 $this->date_modification = $this->
db->jdate($obj->datem);
2527 $this->
db->free($result);
2549 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut,";
2550 $sql .=
" ff.rowid as rowidnext";
2551 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
2552 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2553 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as ff ON f.rowid = ff.fk_facture_source";
2554 $sql .=
" WHERE (f.fk_statut = ".self::STATUS_VALIDATED.
" OR (f.fk_statut = ".self::STATUS_ABANDONED.
" AND f.close_code = '".self::CLOSECODE_ABANDONED.
"'))";
2555 $sql .=
" AND f.entity = ".$conf->entity;
2556 $sql .=
" AND f.paye = 0";
2557 $sql .=
" AND pf.fk_paiementfourn IS NULL";
2558 $sql .=
" AND ff.fk_statut IS NULL";
2560 $sql .=
" AND f.fk_soc = ".((int) $socid);
2562 $sql .=
" ORDER BY f.ref";
2564 dol_syslog(get_class($this).
"::list_replacable_supplier_invoices", LOG_DEBUG);
2567 while ($obj = $this->
db->fetch_object(
$resql)) {
2568 $return[$obj->rowid] = array(
2569 'id' => $obj->rowid,
2571 'status' => $obj->fk_statut
2577 $this->error = $this->
db->error();
2599 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut, f.type, f.paye, pf.fk_paiementfourn";
2600 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
2601 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf ON f.rowid = pf.fk_facturefourn";
2602 $sql .=
" WHERE f.entity = ".$conf->entity;
2603 $sql .=
" AND f.fk_statut in (".self::STATUS_VALIDATED.
",".self::STATUS_CLOSED.
")";
2604 $sql .=
" AND NOT EXISTS (SELECT rowid from ".MAIN_DB_PREFIX.
"facture_fourn as ff WHERE f.rowid = ff.fk_facture_source";
2605 $sql .=
" AND ff.type=".self::TYPE_REPLACEMENT.
")";
2606 $sql .=
" AND f.type != ".self::TYPE_CREDIT_NOTE;
2608 $sql .=
" AND f.fk_soc = ".((int) $socid);
2610 $sql .=
" ORDER BY f.ref";
2612 dol_syslog(get_class($this).
"::list_qualified_avoir_supplier_invoices", LOG_DEBUG);
2615 while ($obj = $this->
db->fetch_object(
$resql)) {
2617 if ($obj->fk_statut == self::STATUS_VALIDATED) {
2620 if ($obj->fk_statut == self::STATUS_CLOSED) {
2624 $paymentornot = ($obj->fk_paiementfourn ? 1 : 0);
2625 $return[$obj->rowid] = array(
'ref'=>$obj->ref,
'status'=>$obj->fk_statut,
'type'=>$obj->type,
'paye'=>$obj->paye,
'paymentornot'=>$paymentornot);
2631 $this->error = $this->
db->error();
2646 global $conf, $langs;
2648 $sql =
'SELECT ff.rowid, ff.date_lim_reglement as datefin, ff.fk_statut as status, ff.total_ht, ff.total_ttc';
2649 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn as ff';
2650 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2651 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON ff.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2653 $sql .=
' WHERE ff.paye = 0';
2654 $sql .=
' AND ff.fk_statut > 0';
2655 $sql .=
" AND ff.entity = ".$conf->entity;
2657 $sql .=
' AND ff.fk_soc = '.((int) $user->socid);
2662 $langs->load(
"bills");
2666 $response->warning_delay = $conf->facture->fournisseur->warning_delay / 60 / 60 / 24;
2667 $response->label = $langs->trans(
"SupplierBillsToPay");
2668 $response->labelShort = $langs->trans(
"StatusToPay");
2670 $response->url = DOL_URL_ROOT.
'/fourn/facture/list.php?search_status=1&mainmenu=billing&leftmenu=suppliers_bills';
2671 $response->img =
img_object($langs->trans(
"Bills"),
"bill");
2675 while ($obj = $this->
db->fetch_object(
$resql)) {
2676 $facturestatic->date_echeance = $this->
db->jdate($obj->datefin);
2677 $facturestatic->statut = $obj->status;
2678 $facturestatic->status = $obj->status;
2680 $response->nbtodo++;
2681 $response->total += $obj->total_ht;
2683 if ($facturestatic->hasDelay()) {
2684 $response->nbtodolate++;
2685 $response->url_late = DOL_URL_ROOT.
'/fourn/facture/list.php?search_option=late&mainmenu=billing&leftmenu=suppliers_bills';
2693 $this->error = $this->
db->error();
2712 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
2714 global $langs, $conf, $user, $hookmanager;
2718 if ($option ==
'withdraw') {
2719 $url = DOL_URL_ROOT.
'/compta/facture/prelevement.php?facid='.$this->
id.
'&type=bank-transfer';
2720 } elseif ($option ==
'document') {
2721 $url = DOL_URL_ROOT.
'/fourn/facture/document.php?facid='.$this->id;
2723 $url = DOL_URL_ROOT.
'/fourn/facture/card.php?facid='.$this->id;
2730 if ($option !==
'nolink') {
2732 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2733 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
2734 $add_save_lastsearch_values = 1;
2736 if ($add_save_lastsearch_values) {
2737 $url .=
'&save_lastsearch_values=1';
2741 $picto = $this->picto;
2742 if ($this->
type == self::TYPE_REPLACEMENT) {
2745 if ($this->
type == self::TYPE_CREDIT_NOTE) {
2748 if ($this->
type == self::TYPE_DEPOSIT) {
2752 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"SupplierInvoice").
'</u>';
2753 if ($this->
type == self::TYPE_REPLACEMENT) {
2754 $label =
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"InvoiceReplace").
'</u>';
2755 } elseif ($this->
type == self::TYPE_CREDIT_NOTE) {
2756 $label =
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"CreditNote").
'</u>';
2757 } elseif ($this->
type == self::TYPE_DEPOSIT) {
2758 $label =
'<u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"Deposit").
'</u>';
2760 if (isset($this->status)) {
2762 if (isset($this->alreadypaid)) {
2763 $alreadypaid = $this->alreadypaid;
2766 $label .=
' '.$this->getLibStatut(5, $alreadypaid);
2768 if (!empty($this->
ref)) {
2769 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
2771 if (!empty($this->ref_supplier)) {
2772 $label .=
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
2774 if (!empty($this->label)) {
2775 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
2777 if (!empty($this->date)) {
2778 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
2780 if (!empty($this->total_ht)) {
2781 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2783 if (!empty($this->total_tva)) {
2784 $label .=
'<br><b>'.$langs->trans(
'AmountVAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2786 if (!empty($this->total_ttc)) {
2787 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2790 $label .=
' - '.$moretitle;
2799 if (empty($notooltip)) {
2800 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2801 $label = $langs->trans(
"ShowSupplierInvoice");
2802 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
2804 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
2805 $linkclose .=
' class="classfortooltip"';
2808 $linkstart =
'<a href="'.$url.
'"';
2809 $linkstart .= $linkclose.
'>';
2812 $result .= $linkstart;
2814 $result .=
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
2816 if ($withpicto != 2) {
2817 $result .= ($max ?
dol_trunc($ref, $max) : $ref);
2819 $result .= $linkend;
2821 if ($addlinktonotes) {
2822 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
2825 $result .=
' <span class="note inline-block">';
2826 $result .=
'<a href="'.DOL_URL_ROOT.
'/fourn/facture/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
2829 $result .=
'</span>';
2833 $hookmanager->initHooks(array($this->element .
'dao'));
2834 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
2835 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2837 $result = $hookmanager->resPrint;
2839 $result .= $hookmanager->resPrint;
2854 global $db, $langs, $conf;
2855 $langs->load(
"orders");
2858 if (empty($conf->global->INVOICE_SUPPLIER_ADDON_NUMBER)) {
2859 $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER =
'mod_facture_fournisseur_cactus';
2864 $file = $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER.
".php";
2865 $classname = $conf->global->INVOICE_SUPPLIER_ADDON_NUMBER;
2868 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
2870 foreach ($dirmodels as $reldir) {
2871 $dir =
dol_buildpath($reldir.
"core/modules/supplier_invoice/");
2874 $mybool |= @include_once $dir.$file;
2877 if ($mybool ===
false) {
2882 $obj =
new $classname();
2884 $numref = $obj->getNumRef($soc, $this, $mode);
2886 if ($numref !=
"") {
2889 $this->error = $obj->error;
2905 global $langs, $conf;
2906 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
2914 $sql =
"SELECT rowid";
2915 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2916 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2917 $sql .= $this->
db->plimit(100);
2921 $num_prods = $this->
db->num_rows(
$resql);
2923 while ($i < $num_prods) {
2925 $row = $this->
db->fetch_row(
$resql);
2926 $prodids[$i] = $row[0];
2932 $this->
ref =
'SPECIMEN';
2933 $this->ref_supplier =
'SUPPLIER_REF_SPECIMEN';
2934 $this->specimen = 1;
2937 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
2938 $this->cond_reglement_code =
'RECEP';
2939 $this->mode_reglement_code =
'CHQ';
2941 $this->note_public =
'This is a comment (public)';
2942 $this->note_private =
'This is a comment (private)';
2944 $this->multicurrency_tx = 1;
2945 $this->multicurrency_code = $conf->currency;
2948 if (empty($option) || $option !=
'nolines') {
2951 while ($xnbp < $nbp) {
2953 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
2955 $line->subprice = 100;
2958 $line->tva_tx = 19.6;
2959 $line->localtax1_tx = 0;
2960 $line->localtax2_tx = 0;
2962 $line->total_ht = 50;
2963 $line->total_ttc = 59.8;
2964 $line->total_tva = 9.8;
2965 $line->remise_percent = 50;
2967 $line->total_ht = 100;
2968 $line->total_ttc = 119.6;
2969 $line->total_tva = 19.6;
2970 $line->remise_percent = 0;
2973 if ($num_prods > 0) {
2974 $prodid = mt_rand(1, $num_prods);
2975 $line->fk_product = $prodids[$prodid];
2977 $line->product_type = 0;
2979 $this->lines[$xnbp] = $line;
2981 $this->total_ht += $line->total_ht;
2982 $this->total_tva += $line->total_tva;
2983 $this->total_ttc += $line->total_ttc;
2989 $this->amount_ht = $xnbp * 100;
2990 $this->total_ht = $xnbp * 100;
2991 $this->total_tva = $xnbp * 19.6;
2992 $this->total_ttc = $xnbp * 119.6;
3004 global $conf, $user;
3006 $this->nb = array();
3010 $sql =
"SELECT count(f.rowid) as nb";
3011 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
3012 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON f.fk_soc = s.rowid";
3013 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3014 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3015 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3018 $sql .=
" ".$clause.
" f.entity = ".$conf->entity;
3022 while ($obj = $this->
db->fetch_object(
$resql)) {
3023 $this->nb[
"supplier_invoices"] = $obj->nb;
3029 $this->error = $this->
db->error();
3044 global $conf, $langs;
3053 $object->fetch($fromid);
3058 $object->fetch_thirdparty();
3061 $object->ref_supplier = (empty($this->ref_supplier) ? $langs->trans(
"CopyOf").
' '.$object->ref_supplier : $this->ref_supplier);
3062 $object->author = $user->id;
3063 $object->user_valid =
'';
3064 $object->fk_facture_source = 0;
3065 $object->date_creation =
'';
3066 $object->date_validation =
'';
3067 $object->date = (empty($this->date) ?
'' : $this->date);
3068 $object->date_echeance =
'';
3069 $object->ref_client =
'';
3070 $object->close_code =
'';
3071 $object->close_note =
'';
3072 if ($conf->global->MAIN_DONT_KEEP_NOTE_ON_CLONING == 1) {
3073 $object->note_private =
'';
3074 $object->note_public =
'';
3078 foreach ($object->lines as $i => $line) {
3079 if (isset($object->lines[$i]->info_bits) && ($object->lines[$i]->info_bits & 0x02) == 0x02) {
3080 unset($object->lines[$i]);
3085 $object->context[
'createfromclone'] =
'createfromclone';
3086 $result = $object->create($user);
3090 $this->error = $object->error;
3091 $this->errors = $object->errors;
3098 unset($object->context[
'createfromclone']);
3102 $this->
db->commit();
3105 $this->
db->rollback();
3121 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3123 global $conf, $user, $langs;
3125 $langs->load(
"suppliers");
3126 $outputlangs->load(
"products");
3129 if (empty($modele)) {
3130 if (!empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) {
3131 $modele = $conf->global->INVOICE_SUPPLIER_ADDON_PDF;
3137 if (empty($modele)) {
3140 $modelpath =
"core/modules/supplier_invoice/doc/";
3142 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3154 return $user->rights->fournisseur->facture;
3202 if (!$this->date_echeance) {
3206 $status = isset($this->status) ? $this->status : $this->statut;
3208 return ($status == self::STATUS_VALIDATED) && ($this->date_echeance < ($now - $conf->facture->fournisseur->warning_delay));
3220 $sql =
"SELECT fk_invoice_supplier FROM ".MAIN_DB_PREFIX.
"societe_remise_except WHERE fk_invoice_supplier_source = ".((int) $this->
id);
3223 $obj = $this->
db->fetch_object(
$resql);
3224 if (!empty($obj->fk_invoice_supplier)) {
3243 public $element =
'facture_fourn_det';
3248 public $table_element =
'facture_fourn_det';
3262 public $product_ref;
3269 public $ref_supplier;
3275 public $product_desc;
3302 public $fk_facture_fourn;
3315 public $description;
3320 public $skip_update_total;
3325 public $situation_percent;
3336 public $vat_src_code;
3348 public $localtax1_tx;
3354 public $localtax2_tx;
3366 public $remise_percent;
3390 public $total_localtax1;
3396 public $total_localtax2;
3407 public $product_type;
3413 public $product_label;
3427 public $fk_remise_except;
3432 public $fk_parent_line;
3434 public $special_code;
3445 public $localtax1_type;
3451 public $localtax2_type;
3457 public $fk_multicurrency;
3459 public $multicurrency_code;
3460 public $multicurrency_subprice;
3461 public $multicurrency_total_ht;
3462 public $multicurrency_total_tva;
3463 public $multicurrency_total_ttc;
3484 $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';
3485 $sql .=
', f.localtax1_type, f.localtax2_type, f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2, f.fk_remise_except';
3486 $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';
3487 $sql .=
', p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc';
3488 $sql .=
', f.multicurrency_subprice, f.multicurrency_total_ht, f.multicurrency_total_tva, multicurrency_total_ttc';
3489 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn_det as f';
3490 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON f.fk_product = p.rowid';
3491 $sql .=
' WHERE f.rowid = '.((int) $rowid);
3492 $sql .=
' ORDER BY f.rang, f.rowid';
3494 $query = $this->
db->query($sql);
3497 $this->errors[] = $this->
db->error();
3501 if (!$this->
db->num_rows($query)) {
3505 $obj = $this->
db->fetch_object($query);
3507 $this->
id = $obj->rowid;
3508 $this->
rowid = $obj->rowid;
3509 $this->fk_facture_fourn = $obj->fk_facture_fourn;
3511 $this->date_start = $obj->date_start;
3512 $this->date_end = $obj->date_end;
3513 $this->product_ref = $obj->product_ref;
3514 $this->ref_supplier = $obj->ref_supplier;
3515 $this->product_desc = $obj->product_desc;
3517 $this->subprice = $obj->pu_ht;
3518 $this->pu_ht = $obj->pu_ht;
3519 $this->pu_ttc = $obj->pu_ttc;
3520 $this->tva_tx = $obj->tva_tx;
3521 $this->localtax1_tx = $obj->localtax1_tx;
3522 $this->localtax2_tx = $obj->localtax2_tx;
3523 $this->localtax1_type = $obj->localtax1_type;
3524 $this->localtax2_type = $obj->localtax2_type;
3526 $this->qty = $obj->qty;
3527 $this->remise_percent = $obj->remise_percent;
3528 $this->fk_remise_except = $obj->fk_remise_except;
3530 $this->total_ht = $obj->total_ht;
3531 $this->total_tva = $obj->total_tva;
3532 $this->total_localtax1 = $obj->total_localtax1;
3533 $this->total_localtax2 = $obj->total_localtax2;
3534 $this->total_ttc = $obj->total_ttc;
3535 $this->fk_product = $obj->fk_product;
3536 $this->product_type = $obj->product_type;
3537 $this->product_label = $obj->product_label;
3538 $this->info_bits = $obj->info_bits;
3539 $this->tva_npr = ($obj->info_bits & 1 == 1) ? 1 : 0;
3540 $this->fk_parent_line = $obj->fk_parent_line;
3541 $this->special_code = $obj->special_code;
3542 $this->rang = $obj->rang;
3543 $this->fk_unit = $obj->fk_unit;
3545 $this->multicurrency_subprice = $obj->multicurrency_subprice;
3546 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
3547 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
3548 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
3561 public function delete($notrigger = 0)
3563 global $user, $conf;
3565 dol_syslog(get_class($this).
"::deleteline rowid=".((
int) $this->
id), LOG_DEBUG);
3572 if ($this->
call_trigger(
'LINEBILL_SUPPLIER_DELETE', $user) < 0) {
3584 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
3590 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture_fourn_det ';
3591 $sql .=
" WHERE rowid = ".((int) $this->
id);
3592 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
3596 $this->error = $this->
db->lasterror();
3601 $this->
db->commit();
3604 $this->
db->rollback();
3623 if (empty($this->qty)) {
3627 if ($this->product_type < 0) {
3632 if (empty($this->remise_percent)) {
3633 $this->remise_percent = 0;
3635 if (empty($this->tva_tx)) {
3638 if (empty($this->localtax1_tx)) {
3639 $this->localtax1_tx = 0;
3641 if (empty($this->localtax2_tx)) {
3642 $this->localtax2_tx = 0;
3645 if (empty($this->pa_ht)) {
3648 if (empty($this->multicurrency_subprice)) {
3649 $this->multicurrency_subprice = 0;
3651 if (empty($this->multicurrency_total_ht)) {
3652 $this->multicurrency_total_ht = 0;
3654 if (empty($this->multicurrency_total_tva)) {
3655 $this->multicurrency_total_tva = 0;
3657 if (empty($this->multicurrency_total_ttc)) {
3658 $this->multicurrency_total_ttc = 0;
3661 $fk_product = (int) $this->fk_product;
3662 $fk_unit = (int) $this->fk_unit;
3666 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn_det SET";
3667 $sql .=
" description = '".$this->db->escape($this->
description).
"'";
3668 $sql .=
", ref = '".$this->db->escape($this->ref_supplier ? $this->ref_supplier : $this->
ref).
"'";
3669 $sql .=
", date_start = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3670 $sql .=
", date_end = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3671 $sql .=
", pu_ht = ".price2num($this->pu_ht);
3672 $sql .=
", pu_ttc = ".price2num($this->pu_ttc);
3673 $sql .=
", qty = ".price2num($this->qty);
3674 $sql .=
", remise_percent = ".price2num($this->remise_percent);
3675 if ($this->fk_remise_except > 0) $sql .=
", fk_remise_except=".((int) $this->fk_remise_except);
3676 else $sql .=
", fk_remise_except=null";
3677 $sql .=
", vat_src_code = '".$this->db->escape(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
3678 $sql .=
", tva_tx = ".price2num($this->tva_tx);
3679 $sql .=
", localtax1_tx = ".price2num($this->localtax1_tx);
3680 $sql .=
", localtax2_tx = ".price2num($this->localtax2_tx);
3681 $sql .=
", localtax1_type = '".$this->db->escape($this->localtax1_type).
"'";
3682 $sql .=
", localtax2_type = '".$this->db->escape($this->localtax2_type).
"'";
3683 $sql .=
", total_ht = ".price2num($this->total_ht);
3684 $sql .=
", tva= ".price2num($this->total_tva);
3685 $sql .=
", total_localtax1= ".price2num($this->total_localtax1);
3686 $sql .=
", total_localtax2= ".price2num($this->total_localtax2);
3687 $sql .=
", total_ttc = ".price2num($this->total_ttc);
3688 $sql .=
", fk_product = ".($fk_product > 0 ? (int) $fk_product :
'null');
3689 $sql .=
", product_type = ".((int) $this->product_type);
3690 $sql .=
", info_bits = ".((int) $this->info_bits);
3691 $sql .=
", fk_unit = ".($fk_unit > 0 ? (int) $fk_unit :
'null');
3693 if (!empty($this->rang)) {
3694 $sql .=
", rang=".((int) $this->rang);
3698 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
3699 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
3700 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
3701 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
3703 $sql .=
" WHERE rowid = ".((int) $this->
id);
3705 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3709 $this->
db->rollback();
3710 $this->error = $this->
db->lasterror();
3714 $this->
rowid = $this->id;
3724 if (!$error && !$notrigger) {
3725 global $langs, $user;
3728 if ($this->
call_trigger(
'LINEBILL_SUPPLIER_MODIFY', $user) < 0) {
3729 $this->
db->rollback();
3736 $this->
db->rollback();
3740 $this->
db->commit();
3752 global $user, $conf, $langs;
3756 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
3759 $this->desc = trim($this->desc);
3760 if (empty($this->tva_tx)) {
3763 if (empty($this->localtax1_tx)) {
3764 $this->localtax1_tx = 0;
3766 if (empty($this->localtax2_tx)) {
3767 $this->localtax2_tx = 0;
3769 if (empty($this->localtax1_type)) {
3770 $this->localtax1_type =
'0';
3772 if (empty($this->localtax2_type)) {
3773 $this->localtax2_type =
'0';
3775 if (empty($this->total_tva)) {
3776 $this->total_tva = 0;
3778 if (empty($this->total_localtax1)) {
3779 $this->total_localtax1 = 0;
3781 if (empty($this->total_localtax2)) {
3782 $this->total_localtax2 = 0;
3784 if (empty($this->rang)) {
3787 if (empty($this->remise_percent)) {
3788 $this->remise_percent = 0;
3790 if (empty($this->info_bits)) {
3791 $this->info_bits = 0;
3793 if (empty($this->subprice)) {
3794 $this->subprice = 0;
3796 if (empty($this->special_code)) {
3797 $this->special_code = 0;
3799 if (empty($this->fk_parent_line)) {
3800 $this->fk_parent_line = 0;
3802 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
3803 $this->situation_percent = 100;
3806 if (empty($this->pa_ht)) {
3809 if (empty($this->multicurrency_subprice)) {
3810 $this->multicurrency_subprice = 0;
3812 if (empty($this->multicurrency_total_ht)) {
3813 $this->multicurrency_total_ht = 0;
3815 if (empty($this->multicurrency_total_tva)) {
3816 $this->multicurrency_total_tva = 0;
3818 if (empty($this->multicurrency_total_ttc)) {
3819 $this->multicurrency_total_ttc = 0;
3824 if ($this->product_type < 0) {
3825 $this->error =
'ErrorProductTypeMustBe0orMore';
3828 if (!empty($this->fk_product) && $this->fk_product > 0) {
3832 $this->error =
'ErrorProductIdDoesNotExists';
3840 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
3841 $sql .=
' (fk_facture_fourn, fk_parent_line, label, description, ref, qty,';
3842 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
3843 $sql .=
' fk_product, product_type, remise_percent, fk_remise_except, pu_ht, pu_ttc,';
3844 $sql .=
' date_start, date_end, fk_code_ventilation, rang, special_code,';
3845 $sql .=
' info_bits, total_ht, tva, total_ttc, total_localtax1, total_localtax2, fk_unit';
3846 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
3848 $sql .=
" VALUES (".$this->fk_facture_fourn.
",";
3849 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
3850 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
3851 $sql .=
" '".$this->db->escape($this->desc ? $this->desc : $this->
description).
"',";
3852 $sql .=
" '".$this->db->escape($this->ref_supplier).
"',";
3853 $sql .=
" ".price2num($this->qty).
",";
3855 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
3856 $sql .=
" ".price2num($this->tva_tx).
",";
3857 $sql .=
" ".price2num($this->localtax1_tx).
",";
3858 $sql .=
" ".price2num($this->localtax2_tx).
",";
3859 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3860 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3861 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
3862 $sql .=
" ".((int) $this->product_type).
",";
3863 $sql .=
" ".price2num($this->remise_percent).
",";
3864 $sql .=
' '.(! empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) :
"null").
',';
3865 $sql .=
" ".price2num($this->subprice).
",";
3866 $sql .=
" ".(!empty($this->qty) ?
price2num($this->total_ttc / $this->qty) :
price2num($this->total_ttc)).
",";
3867 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3868 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3869 $sql .=
' '.(!empty($this->fk_code_ventilation) ? $this->fk_code_ventilation : 0).
',';
3870 $sql .=
' '.((int) $this->rang).
',';
3871 $sql .=
' '.((int) $this->special_code).
',';
3872 $sql .=
" ".((int) $this->info_bits).
",";
3873 $sql .=
" ".price2num($this->total_ht).
",";
3874 $sql .=
" ".price2num($this->total_tva).
",";
3875 $sql .=
" ".price2num($this->total_ttc).
",";
3876 $sql .=
" ".price2num($this->total_localtax1).
",";
3877 $sql .=
" ".price2num($this->total_localtax2);
3878 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
3879 $sql .=
", ".(int) $this->fk_multicurrency;
3880 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
3881 $sql .=
", ".price2num($this->multicurrency_subprice);
3882 $sql .=
", ".price2num($this->multicurrency_total_ht);
3883 $sql .=
", ".price2num($this->multicurrency_total_tva);
3884 $sql .=
", ".price2num($this->multicurrency_total_ttc);
3889 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
3890 $this->
rowid = $this->id;
3901 if ($this->fk_remise_except) {
3903 $result = $discount->fetch($this->fk_remise_except);
3908 if ($discount->fk_facture_line > 0) {
3909 if (empty($noerrorifdiscountalreadylinked)) {
3910 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
3911 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
3912 $this->
db->rollback();
3916 $result = $discount->link_to_invoice($this->
rowid, 0);
3918 $this->error = $discount->error;
3919 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
3920 $this->
db->rollback();
3925 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
3926 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
3927 $this->
db->rollback();
3931 $this->error = $discount->error;
3932 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
3933 $this->
db->rollback();
3938 if (!$error && !$notrigger) {
3940 $result = $this->
call_trigger(
'LINEBILL_SUPPLIER_CREATE', $user);
3942 $this->
db->rollback();
3948 $this->
db->commit();
3951 $this->error = $this->
db->error();
3952 $this->
db->rollback();
3969 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn_det SET";
3970 $sql .=
" total_ht = ".price2num($this->total_ht);
3971 $sql .=
", tva= ".price2num($this->total_tva);
3972 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
3973 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
3974 $sql .=
", total_ttc = ".price2num($this->total_ttc);
3975 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
3977 dol_syslog(
"FactureFournisseurLigne.class.php::update_total", LOG_DEBUG);
3981 $this->
db->commit();
3984 $this->error = $this->
db->error();
3985 $this->
db->rollback();