442 public function create(
User $user, $notrigger = 0, $forceduedate = 0)
444 global $langs, $conf, $mysoc, $hookmanager;
446 $origin_user_author_id = ($user->id > 0 ? (int) $user->id : 0);
448 if (empty($this->
type)) {
452 $this->ref_client = trim($this->ref_client);
454 $this->note_private = (isset($this->note_private) ? trim($this->note_private) :
'');
455 $this->note = (isset($this->note) ? trim($this->note) : $this->note_private);
456 $this->note_public = trim($this->note_public);
457 if (!$this->cond_reglement_id) {
458 $this->cond_reglement_id = 0;
460 if (!$this->mode_reglement_id) {
461 $this->mode_reglement_id = 0;
466 if (!empty($this->multicurrency_code)) {
468 if (empty($this->multicurrency_tx)) {
476 $this->fk_multicurrency = 0;
478 if (empty($this->fk_multicurrency)) {
479 $this->multicurrency_code = $conf->currency;
480 $this->fk_multicurrency = 0;
481 $this->multicurrency_tx = 1;
484 dol_syslog(get_class($this).
"::create user=".$user->id.
" date=".$this->date);
487 if (empty($this->date)) {
488 $this->error =
"Try to create an invoice with an empty parameter (date)";
489 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
493 $result = $soc->fetch($this->socid);
495 $this->error =
"Failed to fetch company: ".$soc->error;
496 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
501 $this->date_creation = $now;
505 $originaldatewhen =
null;
506 $nextdatewhen =
null;
507 $previousdaynextdatewhen =
null;
510 if ($this->fac_rec > 0) {
511 $this->fk_fac_rec_source = $this->fac_rec;
514 $origin_user_author_id = ($this->fk_user_author > 0 ? $this->fk_user_author : $origin_user_author_id);
516 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
518 $result = $_facrec->fetch($this->fac_rec);
519 $result = $_facrec->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
522 $originaldatewhen = $_facrec->date_when;
523 $nextdatewhen =
null;
524 $previousdaynextdatewhen =
null;
525 if ($originaldatewhen) {
526 $nextdatewhen =
dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency);
530 if (!empty($_facrec->frequency)) {
531 $this->socid = $_facrec->socid;
533 $this->entity = $_facrec->entity;
538 $this->fk_project =
GETPOSTINT(
'projectid') > 0 ?
GETPOSTINT(
'projectid') : $_facrec->fk_project;
539 $this->note_public = GETPOSTISSET(
'note_public') ?
GETPOST(
'note_public',
'restricthtml') : $_facrec->note_public;
540 $this->note_private = GETPOSTISSET(
'note_private') ?
GETPOST(
'note_private',
'restricthtml') : $_facrec->note_private;
541 $this->model_pdf = GETPOSTISSET(
'model') ?
GETPOST(
'model',
'alpha') : $_facrec->model_pdf;
542 $this->cond_reglement_id =
GETPOSTINT(
'cond_reglement_id') > 0 ?
GETPOSTINT(
'cond_reglement_id') : $_facrec->cond_reglement_id;
543 $this->mode_reglement_id =
GETPOSTINT(
'mode_reglement_id') > 0 ?
GETPOSTINT(
'mode_reglement_id') : $_facrec->mode_reglement_id;
544 $this->fk_account =
GETPOST(
'fk_account') > 0 ?
GETPOSTINT(
'fk_account') : $_facrec->fk_account;
547 $this->total_ht = $_facrec->total_ht;
548 $this->total_ttc = $_facrec->total_ttc;
553 $this->fk_incoterms = $_facrec->fk_incoterms;
554 $this->location_incoterms = $_facrec->location_incoterms;
560 $this->ref_client = trim($this->ref_client);
561 $this->ref_customer = trim($this->ref_customer);
562 $this->note_public = trim($this->note_public);
563 $this->note_private = trim($this->note_private);
564 $this->note_private =
dol_concatdesc($this->note_private, $langs->trans(
"GeneratedFromRecurringInvoice", $_facrec->ref));
566 $this->array_options = $_facrec->array_options;
568 if (!$this->mode_reglement_id) {
569 $this->mode_reglement_id = 0;
574 $this->linked_objects = $_facrec->linkedObjectsIds;
579 if ($_facrec->frequency > 0) {
580 dol_syslog(
"This is a recurring invoice so we set date_last_gen and next date_when");
581 if (empty($_facrec->date_when)) {
582 $_facrec->date_when = $now;
584 $next_date = $_facrec->getNextDate();
585 $result = $_facrec->setValueFrom(
'date_last_gen', $now,
'',
null,
'date',
'', $user,
'');
587 $result = $_facrec->setNextDate($next_date, 1);
591 $outputlangs = $langs;
594 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && empty($newlang) && isset($this->thirdparty->default_lang)) {
595 $newlang = $this->thirdparty->default_lang;
597 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && empty($newlang) && isset($this->default_lang)) {
598 $newlang = $this->default_lang;
600 if (!empty($newlang)) {
602 $outputlangs->setDefaultLang($newlang);
608 $substitutionarray[
'__INVOICE_MONTH__'] =
dol_print_date($this->date,
'%m');
611 $substitutionarray[
'__INVOICE_MONTH_TEXT__'] =
dol_print_date($this->date,
'%B');
614 $substitutionarray[
'__INVOICE_YEAR__'] =
dol_print_date($this->date,
'%Y');
617 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] = (isset($originaldatewhen) ?
dol_print_date($originaldatewhen,
'dayhour') :
'');
618 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] = (isset($nextdatewhen) ?
dol_print_date($nextdatewhen,
'dayhour') :
'');
619 $substitutionarray[
'__INVOICE_PREVIOUS_DATE_NEXT_INVOICE_AFTER_GEN__'] = (isset($previousdaynextdatewhen) ?
dol_print_date($previousdaynextdatewhen,
'dayhour') :
'');
620 $substitutionarray[
'__INVOICE_COUNTER_CURRENT__'] = $_facrec->nb_gen_done;
621 $substitutionarray[
'__INVOICE_COUNTER_MAX__'] = $_facrec->nb_gen_max;
632 if (empty($forceduedate)) {
638 $this->date_lim_reglement = $duedate;
640 $this->date_lim_reglement = $forceduedate;
644 $socid = $this->socid;
646 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture (";
655 $sql .=
", date_pointoftax";
656 $sql .=
", note_private";
657 $sql .=
", note_public";
658 $sql .=
", ref_client";
659 $sql .=
", fk_account";
660 $sql .=
", module_source, pos_source, fk_fac_rec_source, fk_facture_source, fk_user_author, fk_projet";
661 $sql .=
", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf";
662 $sql .=
", situation_cycle_ref, situation_counter, situation_final";
663 $sql .=
", fk_incoterms, location_incoterms";
664 $sql .=
", fk_multicurrency";
665 $sql .=
", multicurrency_code";
666 $sql .=
", multicurrency_tx";
667 $sql .=
", retained_warranty";
668 $sql .=
", retained_warranty_date_limit";
669 $sql .=
", retained_warranty_fk_cond_reglement";
673 $sql .=
", ".setEntity($this);
674 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
675 $sql .=
", '".$this->db->escape($this->
type).
"'";
676 $sql .=
", ".($this->subtype ?
"'".$this->db->escape($this->subtype).
"'" :
"null");
677 $sql .=
", ".((int) $socid);
678 $sql .=
", '".$this->db->idate($this->date_creation).
"'";
679 $sql .=
", '".$this->db->idate($this->date).
"'";
680 $sql .=
", ".(empty($this->date_pointoftax) ?
"null" :
"'".$this->db->idate($this->date_pointoftax).
"'");
681 $sql .=
", ".($this->note_private ?
"'".$this->db->escape($this->note_private).
"'" :
"null");
682 $sql .=
", ".($this->note_public ?
"'".$this->db->escape($this->note_public).
"'" :
"null");
683 $sql .=
", ".($this->ref_customer ?
"'".$this->db->escape($this->ref_customer).
"'" : ($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null"));
684 $sql .=
", ".($this->fk_account > 0 ? $this->fk_account :
'NULL');
685 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
686 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
687 $sql .=
", ".($this->fk_fac_rec_source ?
"'".$this->db->escape($this->fk_fac_rec_source).
"'" :
"null");
688 $sql .=
", ".($this->fk_facture_source ?
"'".$this->db->escape($this->fk_facture_source).
"'" :
"null");
689 $sql .=
", ".($origin_user_author_id > 0 ? (int) $origin_user_author_id :
"null");
690 $sql .=
", ".($this->fk_project ? (int) $this->fk_project :
"null");
691 $sql .=
", ".((int) $this->cond_reglement_id);
692 $sql .=
", ".((int) $this->mode_reglement_id);
693 $sql .=
", '".$this->db->idate($this->date_lim_reglement).
"'";
694 $sql .=
", ".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null");
695 $sql .=
", ".($this->situation_cycle_ref ?
"'".$this->db->escape($this->situation_cycle_ref).
"'" :
"null");
696 $sql .=
", ".($this->situation_counter ?
"'".$this->db->escape($this->situation_counter).
"'" :
"null");
697 $sql .=
", ".($this->situation_final ? (int) $this->situation_final : 0);
698 $sql .=
", ".(int) $this->fk_incoterms;
699 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
700 $sql .=
", ".(int) $this->fk_multicurrency;
701 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
702 $sql .=
", ".(float) $this->multicurrency_tx;
703 $sql .=
", ".(empty($this->retained_warranty) ?
"0" : $this->db->escape($this->retained_warranty));
704 $sql .=
", ".(!empty($this->retained_warranty_date_limit) ?
"'".$this->db->idate($this->retained_warranty_date_limit).
"'" :
'NULL');
705 $sql .=
", ".(int) $this->retained_warranty_fk_cond_reglement;
708 $resql = $this->db->query($sql);
710 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture');
713 $this->
ref =
'(PROV'.$this->id.
')';
714 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"facture SET ref='".$this->db->escape($this->
ref).
"' WHERE rowid=".((int) $this->
id);
716 $resql = $this->db->query($sql);
721 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
722 $this->linked_objects = $this->linkedObjectsIds;
726 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
727 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
728 if (is_array($tmp_origin_id)) {
729 foreach ($tmp_origin_id as $origin_id) {
732 $this->error = $this->db->lasterror();
737 $origin_id = $tmp_origin_id;
740 $this->error = $this->db->lasterror();
748 if (!$error && $this->
id &&
getDolGlobalString(
'MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) {
749 $originforcontact = $this->origin;
750 $originidforcontact = $this->origin_id;
751 if ($originforcontact ==
'shipping') {
752 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
754 $exp->fetch($this->origin_id);
755 $exp->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
756 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
757 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
758 $originforcontact =
'commande';
759 if (is_object($value)) {
760 $originidforcontact = $value->id;
762 $originidforcontact = $value;
769 $sqlcontact =
"SELECT ctc.code, ctc.source, ec.fk_socpeople FROM ".MAIN_DB_PREFIX.
"element_contact as ec, ".MAIN_DB_PREFIX.
"c_type_contact as ctc";
770 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
772 $resqlcontact = $this->db->query($sqlcontact);
774 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
776 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
786 if (!$error && empty($this->fac_rec) && count($this->lines) && is_object($this->lines[0])) {
789 dol_syslog(
"There is ".count($this->lines).
" lines into ->lines that are InvoiceLines");
790 foreach ($this->lines as $i => $val) {
791 $newinvoiceline = $this->lines[$i];
793 $newinvoiceline->context = $this->context;
795 $newinvoiceline->fk_facture = $this->id;
797 $newinvoiceline->origin = $this->lines[$i]->element;
798 $newinvoiceline->origin_id = $this->lines[$i]->id;
801 if ($this->lines[$i]->date_start_fill == 1 && $originaldatewhen) {
802 $newinvoiceline->date_start = $originaldatewhen;
804 if ($this->lines[$i]->date_end_fill == 1 && $previousdaynextdatewhen) {
805 $newinvoiceline->date_end = $previousdaynextdatewhen;
810 if (($newinvoiceline->product_type != 9 && empty($newinvoiceline->fk_parent_line)) || $newinvoiceline->product_type == 9) {
815 $vatrate = $newinvoiceline->tva_tx;
816 if ($newinvoiceline->vat_src_code && ! preg_match(
'/\(.*\)/', (
string) $vatrate)) {
817 $vatrate .=
' ('.$newinvoiceline->vat_src_code.
')';
820 $newinvoiceline->fk_parent_line = $fk_parent_line;
824 $discount->fetch($newinvoiceline->fk_remise_except);
826 $discountId = $soc->set_remise_except($discount->amount_ht, $user, $discount->description, $discount->tva_tx);
827 $newinvoiceline->fk_remise_except = $discountId;
831 $newinvoiceline->desc,
832 $newinvoiceline->subprice,
833 $newinvoiceline->qty,
835 $newinvoiceline->localtax1_tx,
836 $newinvoiceline->localtax2_tx,
837 $newinvoiceline->fk_product,
838 $newinvoiceline->remise_percent,
839 $newinvoiceline->date_start,
840 $newinvoiceline->date_end,
841 $newinvoiceline->fk_code_ventilation,
842 $newinvoiceline->info_bits,
843 $newinvoiceline->fk_remise_except,
846 $newinvoiceline->product_type,
847 $newinvoiceline->rang,
848 $newinvoiceline->special_code,
849 $newinvoiceline->element,
852 $newinvoiceline->fk_fournprice,
853 $newinvoiceline->pa_ht,
854 $newinvoiceline->label,
855 $newinvoiceline->array_options,
856 $newinvoiceline->situation_percent,
857 $newinvoiceline->fk_prev_id,
858 $newinvoiceline->fk_unit,
859 $newinvoiceline->multicurrency_subprice,
860 $newinvoiceline->ref_ext,
865 if ($result > 0 && $newinvoiceline->product_type == 9) {
866 $fk_parent_line = $result;
870 $this->error = $newinvoiceline->error;
871 $this->errors = $newinvoiceline->errors;
876 } elseif (!$error && empty($this->fac_rec)) {
879 dol_syslog(
"There is ".count($this->lines).
" lines into ->lines as a simple array");
881 foreach ($this->lines as $i => $val) {
882 $line = $this->lines[$i];
886 if (!is_object($line)) {
887 $line = (object) $line;
892 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
897 $vatrate = $line->tva_tx;
898 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', (
string) $vatrate)) {
899 $vatrate .=
' ('.$line->vat_src_code.
')';
903 $originid = $line->origin_id;
904 $origintype = $line->origin;
906 $originid = $line->id;
907 $origintype = $this->element;
911 if (empty($line->ref_ext)) {
923 $line->remise_percent,
926 $line->fk_code_ventilation,
928 $line->fk_remise_except,
937 $line->fk_fournprice,
940 $line->array_options,
941 $line->situation_percent,
944 $line->multicurrency_subprice,
949 $this->error = $this->db->lasterror();
951 $this->db->rollback();
956 if ($result > 0 && $line->product_type == 9) {
957 $fk_parent_line = $result;
966 if (!$error && $this->fac_rec > 0) {
967 dol_syslog(
"There is ".count($_facrec->lines).
" lines from recurring invoice");
970 foreach ($_facrec->lines as $i => $val) {
971 if ($_facrec->lines[$i]->fk_product) {
972 $prod =
new Product($this->db);
973 $res = $prod->fetch($_facrec->lines[$i]->fk_product);
977 if (($_facrec->lines[$i]->product_type != 9 && empty($_facrec->lines[$i]->fk_parent_line)) || $_facrec->lines[$i]->product_type == 9) {
989 $tva_tx = $_facrec->lines[$i]->tva_tx.($_facrec->lines[$i]->vat_src_code ?
'('.$_facrec->lines[$i]->vat_src_code.
')' :
'');
990 $tva_npr = $_facrec->lines[$i]->info_bits;
991 if (empty($tva_tx)) {
994 $localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
995 $localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
997 $fk_product_fournisseur_price = empty($_facrec->lines[$i]->fk_product_fournisseur_price) ? null : $_facrec->lines[$i]->fk_product_fournisseur_price;
998 $buyprice = empty($_facrec->lines[$i]->buyprice) ? 0 : $_facrec->lines[$i]->buyprice;
1001 if (!$buyprice && $_facrec->lines[$i]->fk_product > 0) {
1002 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
1004 $producttmp->fetch($_facrec->lines[$i]->fk_product);
1009 if (
getDolGlobalString(
'MARGIN_TYPE') ==
'costprice' && !empty($producttmp->cost_price)) {
1010 $buyprice = $producttmp->cost_price;
1012 $buyprice = $producttmp->pmp;
1014 if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0) {
1015 if ($producttmp->product_fourn_price_id > 0) {
1016 $buyprice =
price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent / 100) + $producttmp->fourn_remise,
'MU');
1022 $result_insert = $this->
addline(
1023 $_facrec->lines[$i]->desc,
1024 $_facrec->lines[$i]->subprice,
1025 $_facrec->lines[$i]->qty,
1029 $_facrec->lines[$i]->fk_product,
1030 $_facrec->lines[$i]->remise_percent,
1031 ($_facrec->lines[$i]->date_start_fill == 1 && $originaldatewhen) ? $originaldatewhen :
'',
1032 ($_facrec->lines[$i]->date_end_fill == 1 && $previousdaynextdatewhen) ? $previousdaynextdatewhen :
'',
1038 $_facrec->lines[$i]->product_type,
1039 $_facrec->lines[$i]->rang,
1040 $_facrec->lines[$i]->special_code,
1044 $fk_product_fournisseur_price,
1046 $_facrec->lines[$i]->label,
1047 empty($_facrec->lines[$i]->array_options) ? null : $_facrec->lines[$i]->array_options,
1050 $_facrec->lines[$i]->fk_unit,
1051 $_facrec->lines[$i]->multicurrency_subprice,
1052 $_facrec->lines[$i]->ref_ext,
1057 if ($result_insert > 0 && $_facrec->lines[$i]->product_type == 9) {
1058 $fk_parent_line = $result_insert;
1061 if ($result_insert < 0) {
1063 $this->error = $this->db->error();
1082 if (!$error && !$notrigger) {
1091 $this->db->commit();
1094 $this->db->rollback();
1098 $this->error = $langs->trans(
'FailedToUpdatePrice');
1099 $this->db->rollback();
1103 dol_syslog(get_class($this).
"::create error ".$this->error, LOG_ERR);
1104 $this->db->rollback();
1108 $this->error = $this->db->error();
1109 $this->db->rollback();
1125 $facture =
new Facture($this->db);
1131 if (!empty($this->array_options)) {
1132 $facture->array_options = $this->array_options;
1135 foreach ($this->lines as &$line) {
1136 $line->fetch_optionals();
1140 $facture->type = $this->type;
1141 $facture->subtype = $this->subtype;
1142 $facture->socid = $this->socid;
1143 $facture->date = $this->date;
1144 $facture->date_pointoftax = $this->date_pointoftax;
1145 $facture->note_public = $this->note_public;
1146 $facture->note_private = $this->note_private;
1147 $facture->ref_client = $this->ref_client;
1148 $facture->ref_customer = $this->ref_customer;
1149 $facture->model_pdf = $this->model_pdf;
1150 $facture->fk_project = $this->fk_project;
1151 $facture->cond_reglement_id = $this->cond_reglement_id;
1152 $facture->mode_reglement_id = $this->mode_reglement_id;
1156 $facture->origin = $this->origin;
1157 $facture->origin_id = $this->origin_id;
1158 $facture->fk_account = $this->fk_account;
1160 $facture->lines = $this->lines;
1161 $facture->situation_counter = $this->situation_counter;
1162 $facture->situation_cycle_ref = $this->situation_cycle_ref;
1163 $facture->situation_final = $this->situation_final;
1165 $facture->retained_warranty = $this->retained_warranty;
1166 $facture->retained_warranty_fk_cond_reglement = $this->retained_warranty_fk_cond_reglement;
1167 $facture->retained_warranty_date_limit = $this->retained_warranty_date_limit;
1169 $facture->fk_user_author = $user->id;
1170 $facture->user_creation_id = $user->id;
1174 foreach ($facture->lines as $i => $tmpline) {
1175 $facture->lines[$i]->fk_prev_id = $this->lines[$i]->rowid;
1176 if ($invertdetail) {
1177 $facture->lines[$i]->subprice = -$facture->lines[$i]->subprice;
1178 $facture->lines[$i]->total_ht = -$facture->lines[$i]->total_ht;
1179 $facture->lines[$i]->total_tva = -$facture->lines[$i]->total_tva;
1180 $facture->lines[$i]->total_localtax1 = -$facture->lines[$i]->total_localtax1;
1181 $facture->lines[$i]->total_localtax2 = -$facture->lines[$i]->total_localtax2;
1182 $facture->lines[$i]->total_ttc = -$facture->lines[$i]->total_ttc;
1183 $facture->lines[$i]->ref_ext =
'';
1187 dol_syslog(get_class($this).
"::createFromCurrent invertdetail=".$invertdetail.
" socid=".$this->socid.
" nboflines=".count($facture->lines));
1189 $facid = $facture->create($user);
1191 $this->error = $facture->error;
1192 $this->errors = $facture->errors;
1196 foreach ($this->linkedObjectsIds as $typeObject => $Tfk_object) {
1197 foreach ($Tfk_object as $fk_object) {
1198 $facture->add_object_linked($typeObject, $fk_object);
1202 $facture->add_object_linked(
'facture', $this->fk_facture_source);
1628 global $conf, $langs, $hookmanager, $action;
1630 if (! in_array($origin->element, array(
'propal',
'commande'))) {
1631 $origin->error =
'ErrorCanOnlyAutomaticallyGenerateADepositFromProposalOrOrder';
1636 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'DateInvoice'));
1640 require_once DOL_DOCUMENT_ROOT .
'/core/lib/date.lib.php';
1643 $origin->error =
'ErrorDateIsInFuture';
1647 if ($payment_terms_id <= 0) {
1648 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'PaymentConditionsShort'));
1652 $payment_conditions_deposit_percent =
getDictionaryValue(
'c_payment_term',
'deposit_percent', $origin->cond_reglement_id);
1654 if (empty($payment_conditions_deposit_percent)) {
1655 $origin->error =
'ErrorPaymentConditionsNotEligibleToDepositCreation';
1659 if (empty($origin->deposit_percent)) {
1660 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'DepositPercent'));
1664 $deposit =
new self($origin->db);
1665 $deposit->socid = $origin->socid;
1667 $deposit->fk_project = $origin->fk_project;
1668 $deposit->ref_client = $origin->ref_client;
1669 $deposit->date = $date;
1670 $deposit->mode_reglement_id = $origin->mode_reglement_id;
1671 $deposit->cond_reglement_id = $payment_terms_id;
1672 $deposit->availability_id = $origin->availability_id;
1673 $deposit->demand_reason_id = $origin->demand_reason_id;
1674 $deposit->fk_account = $origin->fk_account;
1675 $deposit->fk_incoterms = $origin->fk_incoterms;
1676 $deposit->location_incoterms = $origin->location_incoterms;
1677 $deposit->fk_multicurrency = $origin->fk_multicurrency;
1678 $deposit->multicurrency_code = $origin->multicurrency_code;
1679 $deposit->multicurrency_tx = $origin->multicurrency_tx;
1680 $deposit->module_source = $origin->module_source;
1681 $deposit->pos_source = $origin->pos_source;
1682 $deposit->model_pdf =
'crabe';
1684 $modelByTypeConfName =
'FACTURE_ADDON_PDF_' . $deposit->type;
1693 $deposit->note_private = $origin->note_private;
1694 $deposit->note_public = $origin->note_public;
1697 $deposit->origin = $origin->element;
1698 $deposit->origin_id = $origin->id;
1700 $origin->fetch_optionals();
1702 foreach ($origin->array_options as $extrakey => $value) {
1703 $deposit->array_options[$extrakey] = $value;
1706 $deposit->linked_objects[$deposit->origin] = $deposit->origin_id;
1708 foreach ($overrideFields as $key => $value) {
1709 $deposit->$key = $value;
1712 $deposit->context[
'createdepositfromorigin'] =
'createdepositfromorigin';
1714 $origin->db->begin();
1717 $createReturn = $deposit->create($user, $notrigger);
1719 if ($createReturn <= 0) {
1720 $origin->db->rollback();
1721 $origin->error = $deposit->error;
1722 $origin->errors = $deposit->errors;
1726 $amount_ttc_diff = 0;
1727 $amountdeposit = array();
1728 $descriptions = array();
1731 $amount = $origin->total_ttc * ($origin->deposit_percent / 100);
1733 $TTotalByTva = array();
1734 foreach ($origin->lines as &$line) {
1735 if (!empty($line->special_code)) {
1738 $TTotalByTva[$line->tva_tx] += $line->total_ttc;
1739 $descriptions[$line->tva_tx] .=
'<li>' . (!empty($line->product_ref) ? $line->product_ref .
' - ' :
'');
1740 $descriptions[$line->tva_tx] .= (!empty($line->product_label) ? $line->product_label .
' - ' :
'');
1741 $descriptions[$line->tva_tx] .= $langs->trans(
'Qty') .
' : ' . $line->qty;
1742 $descriptions[$line->tva_tx] .=
' - ' . $langs->trans(
'TotalHT') .
' : ' .
price($line->total_ht) .
'</li>';
1745 foreach ($TTotalByTva as $tva => &$total) {
1746 $coef = $total / $origin->total_ttc;
1747 $am = $amount * $coef;
1748 $amount_ttc_diff += $am;
1749 $amountdeposit[$tva] += $am / (1 + $tva / 100);
1753 $lines = $origin->lines;
1754 $numlines = count($lines);
1755 for ($i = 0; $i < $numlines; $i++) {
1756 if (empty($lines[$i]->qty)) {
1759 if (!empty($lines[$i]->special_code)) {
1763 $totalamount += $lines[$i]->total_ht;
1764 $tva_tx = $lines[$i]->tva_tx;
1765 $amountdeposit[$tva_tx] += ($lines[$i]->total_ht * $origin->deposit_percent) / 100;
1766 $descriptions[$tva_tx] .=
'<li>' . (!empty($lines[$i]->product_ref) ? $lines[$i]->product_ref .
' - ' :
'');
1767 $descriptions[$tva_tx] .= (!empty($lines[$i]->product_label) ? $lines[$i]->product_label .
' - ' :
'');
1768 $descriptions[$tva_tx] .= $langs->trans(
'Qty') .
' : ' . $lines[$i]->qty;
1769 $descriptions[$tva_tx] .=
' - ' . $langs->trans(
'TotalHT') .
' : ' .
price($lines[$i]->total_ht) .
'</li>';
1772 if ($totalamount == 0) {
1773 $amountdeposit[0] = 0;
1776 $amount_ttc_diff = $amountdeposit[0];
1779 foreach ($amountdeposit as $tva => $amount) {
1780 if (empty($amount)) {
1784 $descline =
'(DEPOSIT) ('. $origin->deposit_percent .
'%) - '.$origin->ref;
1787 if (
getDolGlobalString(
'INVOICE_DEPOSIT_VARIABLE_MODE_DETAIL_LINES_IN_DESCRIPTION') && !empty($descriptions[$tva])) {
1788 $descline .=
'<ul>' . $descriptions[$tva] .
'</ul>';
1791 $addlineResult = $deposit->addline(
1798 (!
getDolGlobalString(
'INVOICE_PRODUCTID_DEPOSIT') ? 0 : $conf->global->INVOICE_PRODUCTID_DEPOSIT),
1818 if ($addlineResult < 0) {
1819 $origin->db->rollback();
1820 $origin->error = $deposit->error;
1821 $origin->errors = $deposit->errors;
1826 $diff = $deposit->total_ttc - $amount_ttc_diff;
1829 $deposit->fetch_lines();
1830 $subprice_diff = $deposit->lines[0]->subprice - $diff / (1 + $deposit->lines[0]->tva_tx / 100);
1832 $updatelineResult = $deposit->updateline(
1833 $deposit->lines[0]->id,
1834 $deposit->lines[0]->desc,
1836 $deposit->lines[0]->qty,
1837 $deposit->lines[0]->remise_percent,
1838 $deposit->lines[0]->date_start,
1839 $deposit->lines[0]->date_end,
1840 $deposit->lines[0]->tva_tx,
1844 $deposit->lines[0]->info_bits,
1845 $deposit->lines[0]->product_type,
1849 $deposit->lines[0]->pa_ht,
1850 $deposit->lines[0]->label,
1856 if ($updatelineResult < 0) {
1857 $origin->db->rollback();
1858 $origin->error = $deposit->error;
1859 $origin->errors = $deposit->errors;
1864 $hookmanager->initHooks(array(
'invoicedao'));
1866 $parameters = array(
'objFrom' => $origin);
1867 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $deposit, $action);
1870 $origin->db->rollback();
1871 $origin->error = $hookmanager->error;
1872 $origin->errors = $hookmanager->errors;
1876 if (!empty($autoValidateDeposit)) {
1877 $validateReturn = $deposit->validate($user,
'', 0, $notrigger);
1879 if ($validateReturn < 0) {
1880 $origin->db->rollback();
1881 $origin->error = $deposit->error;
1882 $origin->errors = $deposit->errors;
1887 unset($deposit->context[
'createdepositfromorigin']);
1889 $origin->db->commit();
2112 public function fetch($rowid, $ref =
'', $ref_ext =
'', $notused = 0, $fetch_situation =
false)
2114 if (empty($rowid) && empty($ref) && empty($ref_ext)) {
2118 $sql =
'SELECT f.rowid, f.entity, f.ref, f.ref_client, f.ref_ext, f.type, f.subtype, f.fk_soc';
2119 $sql .=
', f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc, f.revenuestamp';
2120 $sql .=
', f.datef as df, f.date_pointoftax';
2121 $sql .=
', f.date_lim_reglement as dlr';
2122 $sql .=
', f.datec as datec';
2123 $sql .=
', f.date_valid as datev';
2124 $sql .=
', f.tms as datem';
2125 $sql .=
', f.note_private, f.note_public, f.fk_statut as status, f.paye, f.close_code, f.close_note, f.fk_user_author, f.fk_user_valid, f.fk_user_modif, f.model_pdf, f.last_main_doc';
2126 $sql .=
', f.fk_facture_source, f.fk_fac_rec_source';
2127 $sql .=
', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project, f.extraparams';
2128 $sql .=
', f.situation_cycle_ref, f.situation_counter, f.situation_final';
2129 $sql .=
', f.fk_account';
2130 $sql .=
", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
2131 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
2132 $sql .=
', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
2133 $sql .=
', f.fk_incoterms, f.location_incoterms';
2134 $sql .=
', f.module_source, f.pos_source';
2135 $sql .=
", i.libelle as label_incoterms";
2136 $sql .=
", f.retained_warranty as retained_warranty, f.retained_warranty_date_limit as retained_warranty_date_limit, f.retained_warranty_fk_cond_reglement as retained_warranty_fk_cond_reglement";
2137 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
2138 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
2139 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON f.fk_mode_reglement = p.id';
2140 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON f.fk_incoterms = i.rowid';
2143 $sql .=
" WHERE f.rowid = ".((int) $rowid);
2145 $sql .=
' WHERE f.entity IN ('.getEntity(
'invoice').
')';
2147 $sql .=
" AND f.ref = '".$this->db->escape($ref).
"'";
2150 $sql .=
" AND f.ref_ext = '".$this->db->escape($ref_ext).
"'";
2154 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
2155 $resql = $this->db->query($sql);
2157 if ($this->db->num_rows($resql)) {
2158 $obj = $this->db->fetch_object($resql);
2160 $this->
id = $obj->rowid;
2161 $this->entity = $obj->entity;
2163 $this->
ref = $obj->ref;
2164 $this->ref_client = $obj->ref_client;
2165 $this->ref_customer = $obj->ref_client;
2166 $this->ref_ext = $obj->ref_ext;
2167 $this->
type = $obj->type;
2168 $this->subtype = $obj->subtype;
2169 $this->date = $this->db->jdate($obj->df);
2170 $this->date_pointoftax = $this->db->jdate($obj->date_pointoftax);
2171 $this->date_creation = $this->db->jdate($obj->datec);
2172 $this->date_validation = $this->db->jdate($obj->datev);
2173 $this->date_modification = $this->db->jdate($obj->datem);
2174 $this->datem = $this->db->jdate($obj->datem);
2175 $this->total_ht = $obj->total_ht;
2176 $this->total_tva = $obj->total_tva;
2177 $this->total_localtax1 = $obj->localtax1;
2178 $this->total_localtax2 = $obj->localtax2;
2179 $this->total_ttc = $obj->total_ttc;
2180 $this->revenuestamp = $obj->revenuestamp;
2181 $this->paye = $obj->paye;
2182 $this->close_code = $obj->close_code;
2183 $this->close_note = $obj->close_note;
2185 $this->socid = $obj->fk_soc;
2186 $this->thirdparty =
null;
2188 $this->fk_project = $obj->fk_project;
2189 $this->project =
null;
2191 $this->
statut = $obj->status;
2192 $this->
status = $obj->status;
2194 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
2195 $this->mode_reglement_id = $obj->fk_mode_reglement;
2196 $this->mode_reglement_code = $obj->mode_reglement_code;
2197 $this->mode_reglement = $obj->mode_reglement_libelle;
2198 $this->cond_reglement_id = $obj->fk_cond_reglement;
2199 $this->cond_reglement_code = $obj->cond_reglement_code;
2200 $this->cond_reglement = $obj->cond_reglement_libelle;
2201 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
2202 $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account :
null;
2203 $this->fk_facture_source = $obj->fk_facture_source;
2204 $this->fk_fac_rec_source = $obj->fk_fac_rec_source;
2205 $this->note = $obj->note_private;
2206 $this->note_private = $obj->note_private;
2207 $this->note_public = $obj->note_public;
2208 $this->user_creation_id = $obj->fk_user_author;
2209 $this->user_validation_id = $obj->fk_user_valid;
2210 $this->user_modification_id = $obj->fk_user_modif;
2211 $this->fk_user_author = $obj->fk_user_author;
2212 $this->fk_user_valid = $obj->fk_user_valid;
2213 $this->fk_user_modif = $obj->fk_user_modif;
2214 $this->model_pdf = $obj->model_pdf;
2215 $this->last_main_doc = $obj->last_main_doc;
2216 $this->situation_cycle_ref = $obj->situation_cycle_ref;
2217 $this->situation_counter = $obj->situation_counter;
2218 $this->situation_final = $obj->situation_final;
2219 $this->retained_warranty = $obj->retained_warranty;
2220 $this->retained_warranty_date_limit = $this->db->jdate($obj->retained_warranty_date_limit);
2221 $this->retained_warranty_fk_cond_reglement = $obj->retained_warranty_fk_cond_reglement;
2223 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
2226 $this->fk_incoterms = $obj->fk_incoterms;
2227 $this->location_incoterms = $obj->location_incoterms;
2228 $this->label_incoterms = $obj->label_incoterms;
2230 $this->module_source = $obj->module_source;
2231 $this->pos_source = $obj->pos_source;
2234 $this->fk_multicurrency = $obj->fk_multicurrency;
2235 $this->multicurrency_code = $obj->multicurrency_code;
2236 $this->multicurrency_tx = $obj->multicurrency_tx;
2237 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
2238 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
2239 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
2241 if (($this->
type == self::TYPE_SITUATION || ($this->
type == self::TYPE_CREDIT_NOTE && $this->situation_cycle_ref > 0)) && $fetch_situation) {
2250 $this->lines = array();
2254 $this->error = $this->db->error();
2258 $this->db->free($resql);
2262 $this->error =
'Invoice with id='.$rowid.
' or ref='.$ref.
' or ref_ext='.$ref_ext.
' not found';
2264 dol_syslog(__METHOD__.$this->error, LOG_WARNING);
2268 $this->error = $this->db->lasterror();
2283 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2286 $this->lines = array();
2288 $sql =
'SELECT l.rowid, l.fk_facture, l.fk_product, l.fk_parent_line, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx,';
2289 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise_percent, l.fk_remise_except, l.subprice, l.ref_ext,';
2290 $sql .=
' l.situation_percent, l.fk_prev_id,';
2291 $sql .=
' l.rang, l.special_code, l.batch, l.fk_warehouse,';
2292 $sql .=
' l.date_start as date_start, l.date_end as date_end,';
2293 $sql .=
' l.info_bits, l.total_ht, l.total_tva, l.total_localtax1, l.total_localtax2, l.total_ttc, l.fk_code_ventilation, l.fk_product_fournisseur_price as fk_fournprice, l.buy_price_ht as pa_ht,';
2294 $sql .=
' l.fk_unit,';
2295 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2296 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc, p.barcode as product_barcode';
2297 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet as l';
2298 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
2299 $sql .=
' WHERE l.fk_facture = '.((int) $this->
id);
2300 $sql .=
' ORDER BY l.rang, l.rowid';
2302 dol_syslog(get_class($this).
'::fetch_lines', LOG_DEBUG);
2303 $result = $this->db->query($sql);
2305 $num = $this->db->num_rows($result);
2308 $objp = $this->db->fetch_object($result);
2311 $line->id = $objp->rowid;
2312 $line->rowid = $objp->rowid;
2313 $line->fk_facture = $objp->fk_facture;
2314 $line->label = $objp->custom_label;
2315 $line->desc = $objp->description;
2316 $line->description = $objp->description;
2317 $line->product_type = $objp->product_type;
2318 $line->ref = $objp->product_ref;
2319 $line->product_ref = $objp->product_ref;
2320 $line->libelle = $objp->product_label;
2321 $line->product_label = $objp->product_label;
2322 $line->product_barcode = $objp->product_barcode;
2323 $line->product_desc = $objp->product_desc;
2324 $line->fk_product_type = $objp->fk_product_type;
2325 $line->qty = $objp->qty;
2326 $line->subprice = $objp->subprice;
2327 $line->ref_ext = $objp->ref_ext;
2329 $line->vat_src_code = $objp->vat_src_code;
2330 $line->tva_tx = $objp->tva_tx;
2331 $line->localtax1_tx = $objp->localtax1_tx;
2332 $line->localtax2_tx = $objp->localtax2_tx;
2333 $line->localtax1_type = $objp->localtax1_type;
2334 $line->localtax2_type = $objp->localtax2_type;
2335 $line->remise_percent = $objp->remise_percent;
2336 $line->fk_remise_except = $objp->fk_remise_except;
2337 $line->fk_product = $objp->fk_product;
2338 $line->date_start = $this->db->jdate($objp->date_start);
2339 $line->date_end = $this->db->jdate($objp->date_end);
2340 $line->info_bits = $objp->info_bits;
2341 $line->total_ht = $objp->total_ht;
2342 $line->total_tva = $objp->total_tva;
2343 $line->total_localtax1 = $objp->total_localtax1;
2344 $line->total_localtax2 = $objp->total_localtax2;
2345 $line->total_ttc = $objp->total_ttc;
2347 $line->fk_fournprice = $objp->fk_fournprice;
2348 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2349 $line->pa_ht = $marginInfos[0];
2350 $line->marge_tx = $marginInfos[1];
2351 $line->marque_tx = $marginInfos[2];
2352 $line->rang = $objp->rang;
2353 $line->special_code = $objp->special_code;
2354 $line->fk_parent_line = $objp->fk_parent_line;
2355 $line->situation_percent = $objp->situation_percent;
2356 $line->fk_prev_id = $objp->fk_prev_id;
2357 $line->fk_unit = $objp->fk_unit;
2359 $line->batch = $objp->batch;
2360 $line->fk_warehouse = $objp->fk_warehouse;
2363 $line->fk_accounting_account = $objp->fk_code_ventilation;
2366 $line->fk_multicurrency = $objp->fk_multicurrency;
2367 $line->multicurrency_code = $objp->multicurrency_code;
2368 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2369 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2370 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2371 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2373 $line->fetch_optionals();
2376 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2377 $tmpproduct =
new Product($this->db);
2378 $tmpproduct->fetch($objp->fk_product);
2379 $tmpproduct->getMultiLangs();
2381 $line->multilangs = $tmpproduct->multilangs;
2384 $this->lines[$i] = $line;
2388 $this->db->free($result);
2391 $this->error = $this->db->error();
2445 if (empty($this->
type)) {
2448 if (isset($this->subtype)) {
2449 $this->subtype = (int) trim((
string) $this->subtype);
2451 if (isset($this->
ref)) {
2452 $this->
ref = trim($this->
ref);
2454 if (isset($this->ref_ext)) {
2455 $this->ref_ext = trim($this->ref_ext);
2457 if (isset($this->ref_client)) {
2458 $this->ref_client = trim($this->ref_client);
2460 if (isset($this->increment)) {
2461 $this->increment = trim($this->increment);
2463 if (isset($this->close_code)) {
2464 $this->close_code = trim($this->close_code);
2466 if (isset($this->close_note)) {
2467 $this->close_note = trim($this->close_note);
2469 if (isset($this->note) || isset($this->note_private)) {
2470 $this->note = (isset($this->note) ? trim($this->note) : trim($this->note_private));
2472 if (isset($this->note) || isset($this->note_private)) {
2473 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
2475 if (isset($this->note_public)) {
2476 $this->note_public = trim($this->note_public);
2478 if (isset($this->model_pdf)) {
2479 $this->model_pdf = trim($this->model_pdf);
2481 if (isset($this->import_key)) {
2482 $this->import_key = trim($this->import_key);
2484 if (isset($this->retained_warranty)) {
2485 $this->retained_warranty = (float) $this->retained_warranty;
2493 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture SET";
2494 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
2495 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
2496 $sql .=
" type=".(isset($this->
type) ? $this->db->escape($this->
type) :
"null").
",";
2497 $sql .=
" subtype=".(isset($this->subtype) ? $this->db->escape($this->subtype) :
"null").
",";
2498 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
2499 $sql .=
" increment=".(isset($this->increment) ?
"'".$this->db->escape($this->increment).
"'" :
"null").
",";
2500 $sql .=
" fk_soc=".(isset($this->socid) ? $this->db->escape($this->socid) :
"null").
",";
2501 $sql .=
" datec=".(strval($this->date_creation) !=
'' ?
"'".$this->db->idate($this->date_creation).
"'" :
'null').
",";
2502 $sql .=
" datef=".(strval($this->date) !=
'' ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
2503 $sql .=
" date_pointoftax=".(strval($this->date_pointoftax) !=
'' ?
"'".$this->db->idate($this->date_pointoftax).
"'" :
'null').
",";
2504 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
2505 $sql .=
" paye=".(isset($this->paye) ? $this->db->escape($this->paye) : 0).
",";
2506 $sql .=
" close_code=".(isset($this->close_code) ?
"'".$this->db->escape($this->close_code).
"'" :
"null").
",";
2507 $sql .=
" close_note=".(isset($this->close_note) ?
"'".$this->db->escape($this->close_note).
"'" :
"null").
",";
2508 $sql .=
" total_tva=".(isset($this->total_tva) ? (float) $this->total_tva :
"null").
",";
2509 $sql .=
" localtax1=".(isset($this->total_localtax1) ? (float) $this->total_localtax1 :
"null").
",";
2510 $sql .=
" localtax2=".(isset($this->total_localtax2) ? (float) $this->total_localtax2 :
"null").
",";
2511 $sql .=
" total_ht=".(isset($this->total_ht) ? (float) $this->total_ht :
"null").
",";
2512 $sql .=
" total_ttc=".(isset($this->total_ttc) ? (float) $this->total_ttc :
"null").
",";
2513 $sql .=
" revenuestamp=".((isset($this->revenuestamp) && $this->revenuestamp !=
'') ? (
int) $this->revenuestamp :
"null").
",";
2514 $sql .=
" fk_statut=".(isset($this->
status) ? (int) $this->
status :
"null").
",";
2515 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? (int) $this->fk_user_valid :
"null").
",";
2516 $sql .=
" fk_facture_source=".(isset($this->fk_facture_source) ? (int) $this->fk_facture_source :
"null").
",";
2517 $sql .=
" fk_projet=".(isset($this->fk_project) ? (int) $this->fk_project :
"null").
",";
2518 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? (int) $this->cond_reglement_id :
"null").
",";
2519 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? (int) $this->mode_reglement_id :
"null").
",";
2520 $sql .=
" date_lim_reglement=".(strval($this->date_lim_reglement) !=
'' ?
"'".$this->db->idate($this->date_lim_reglement).
"'" :
'null').
",";
2521 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
2522 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
2523 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
2524 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
",";
2525 $sql .=
" situation_cycle_ref=".(empty($this->situation_cycle_ref) ?
"null" : (int) $this->situation_cycle_ref).
",";
2526 $sql .=
" situation_counter=".(empty($this->situation_counter) ?
"null" : (int) $this->situation_counter).
",";
2527 $sql .=
" situation_final=".(empty($this->situation_final) ?
"0" : (int) $this->situation_final).
",";
2528 $sql .=
" retained_warranty=".(empty($this->retained_warranty) ?
"0" : (float) $this->retained_warranty).
",";
2529 $sql .=
" retained_warranty_date_limit=".(strval($this->retained_warranty_date_limit) !=
'' ?
"'".$this->db->idate($this->retained_warranty_date_limit).
"'" :
'null').
",";
2530 $sql .=
" retained_warranty_fk_cond_reglement=".(isset($this->retained_warranty_fk_cond_reglement) ? (int) $this->retained_warranty_fk_cond_reglement :
"null");
2531 $sql .=
" WHERE rowid=".((int) $this->
id);
2535 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
2536 $resql = $this->db->query($sql);
2539 $this->errors[] =
"Error ".$this->db->lasterror();
2549 if (!$error && !$notrigger) {
2560 foreach ($this->errors as $errmsg) {
2561 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
2562 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2564 $this->db->rollback();
2567 $this->db->commit();
3156 public function validate($user, $force_number =
'', $idwarehouse = 0, $notrigger = 0, $batch_rule = 0)
3158 global $conf, $langs, $mysoc;
3159 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3161 $productStatic =
null;
3162 $warehouseStatic =
null;
3163 if ($batch_rule > 0) {
3164 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
3165 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
3166 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
3167 $productStatic =
new Product($this->db);
3168 $warehouseStatic =
new Entrepot($this->db);
3175 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number.
', idwarehouse='.$idwarehouse);
3182 if ($this->
status != self::STATUS_DRAFT) {
3183 dol_syslog(get_class($this).
"::validate Current status is not draft. operation canceled.", LOG_WARNING);
3186 if (count($this->lines) <= 0) {
3187 $langs->load(
"errors");
3188 $this->error = $langs->trans(
"ErrorObjectMustHaveLinesToBeValidated", $this->
ref);
3192 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !$user->hasRight(
'facture',
'invoice_advance',
'validate'))) {
3193 $this->error =
'Permission denied';
3194 dol_syslog(get_class($this).
"::validate ".$this->error.
' MAIN_USE_ADVANCED_PERMS=' .
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS'), LOG_ERR);
3197 if ((preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) &&
3205 if (!$last_of_type[0]) {
3206 $this->error = $langs->transnoentities(
"ErrorInvoiceIsNotLastOfSameType", $this->
ref,
dol_print_date($this->date,
'day'),
dol_print_date($last_of_type[1],
'day'));
3212 if (!empty($this->thirdparty) && is_object($this->thirdparty)) {
3213 $array_to_check = array(
'IDPROF1',
'IDPROF2',
'IDPROF3',
'IDPROF4',
'IDPROF5',
'IDPROF6',
'EMAIL',
'ACCOUNTANCY_CODE_CUSTOMER');
3214 foreach ($array_to_check as $key) {
3215 $keymin = strtolower($key);
3216 if (!property_exists($this->thirdparty, $keymin)) {
3219 $vallabel = $this->thirdparty->$keymin;
3221 $i = (int) preg_replace(
'/[^0-9]/',
'', $key);
3223 if ($this->thirdparty->isACompany()) {
3225 if ($mysoc->country_id > 0 && $this->thirdparty->country_id == $mysoc->country_id) {
3226 $idprof_mandatory =
'SOCIETE_'.$key.
'_INVOICE_MANDATORY';
3227 if (!$vallabel && !empty($conf->global->$idprof_mandatory)) {
3228 $langs->load(
"errors");
3229 $this->error = $langs->trans(
'ErrorProdIdIsMandatory', $langs->transcountry(
'ProfId'.$i, $this->thirdparty->country_code)).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
3230 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3236 if ($key ==
'EMAIL') {
3239 $langs->load(
"errors");
3240 $this->error = $langs->trans(
"ErrorBadEMail", $this->thirdparty->email).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
3241 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3245 if ($key ==
'ACCOUNTANCY_CODE_CUSTOMER') {
3247 if (
getDolGlobalString(
'SOCIETE_ACCOUNTANCY_CODE_CUSTOMER_INVOICE_MANDATORY') && empty($this->thirdparty->code_compta)) {
3248 $langs->load(
"errors");
3249 $this->error = $langs->trans(
"ErrorAccountancyCodeCustomerIsMandatory", $this->thirdparty->name).
' ('.$langs->trans(
"ForbiddenBySetupRules").
')';
3250 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3259 $array_to_check = array(
'REF_CLIENT' =>
'RefCustomer');
3260 foreach ($array_to_check as $key => $val) {
3261 $keymin = strtolower($key);
3262 $vallabel = $this->$keymin;
3265 $keymandatory =
'INVOICE_'.$key.
'_MANDATORY_FOR_VALIDATION';
3267 $langs->load(
"errors");
3269 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv($val));
3276 if ($this->
type == self::TYPE_REPLACEMENT) {
3278 if ($this->fk_facture_source <= 0) {
3279 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"InvoiceReplacement"));
3280 $this->db->rollback();
3285 $facreplaced =
new Facture($this->db);
3286 $result = $facreplaced->fetch($this->fk_facture_source);
3288 $this->error = $langs->trans(
"ErrorBadInvoice");
3289 $this->db->rollback();
3294 $idreplacement = $facreplaced->getIdReplacingInvoice(
'validated');
3295 if ($idreplacement && $idreplacement != $this->
id) {
3296 $facreplacement =
new Facture($this->db);
3297 $facreplacement->fetch($idreplacement);
3298 $this->error = $langs->trans(
"ErrorInvoiceAlreadyReplaced", $facreplaced->ref, $facreplacement->ref);
3299 $this->db->rollback();
3303 $result = $facreplaced->setCanceled($user, self::CLOSECODE_REPLACED,
'');
3305 $this->error = $facreplaced->error;
3306 $this->db->rollback();
3312 if ($force_number) {
3313 $num = $force_number;
3314 } elseif (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
3330 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
3331 $sql .=
" SET ref = '".$this->db->escape($num).
"', fk_statut = ".self::STATUS_VALIDATED.
", fk_user_valid = ".($user->id > 0 ? $user->id :
"null").
", date_valid = '".$this->db->idate($now).
"'";
3333 $sql .=
", datef='".$this->db->idate($this->date).
"'";
3334 $sql .=
", date_lim_reglement='".$this->db->idate($this->date_lim_reglement).
"'";
3336 $sql .=
" WHERE rowid = ".((int) $this->
id);
3338 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
3339 $resql = $this->db->query($sql);
3341 $this->error = $this->db->lasterror();
3346 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref))) {
3352 $result = $this->thirdparty->setAsCustomer();
3355 if ($this->
type != self::TYPE_DEPOSIT && $result >= 0 && isModEnabled(
'stock') &&
getDolGlobalString(
'STOCK_CALCULATE_ON_BILL') && $idwarehouse > 0) {
3356 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
3357 $langs->load(
"agenda");
3360 $cpt = count($this->lines);
3361 for ($i = 0; $i < $cpt; $i++) {
3362 if ($this->lines[$i]->fk_product > 0) {
3364 $mouvP->origin = &$this;
3365 $mouvP->setOrigin($this->element, $this->
id);
3368 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3371 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"InvoiceValidatedInDolibarr", $num),
'',
'', $this->lines[$i]->batch);
3374 $this->error = $mouvP->error;
3375 $this->errors = array_merge($this->errors, $mouvP->errors);
3381 $is_batch_line =
false;
3382 if ($batch_rule > 0) {
3383 $productStatic->fetch($this->lines[$i]->fk_product);
3384 if ($productStatic->hasbatch()) {
3385 $is_batch_line =
true;
3386 $product_qty_remain = $this->lines[$i]->qty;
3392 $sortfield =
'pl.sellby,pl.eatby,pb.qty,pl.rowid';
3393 $sortorder =
'ASC,ASC,ASC,ASC';
3396 $resBatchList = $productbatch->findAllForProduct($productStatic->id, $idwarehouse, (
getDolGlobalInt(
'STOCK_ALLOW_NEGATIVE_TRANSFER') ?
null : 0), $sortfield, $sortorder);
3397 if (!is_array($resBatchList)) {
3399 $this->error = $this->db->lasterror();
3403 $batchList = $resBatchList;
3404 if (empty($batchList)) {
3406 $langs->load(
'errors');
3407 $warehouseStatic->fetch($idwarehouse);
3408 $this->error = $langs->trans(
'ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref);
3409 dol_syslog(__METHOD__.
' Error: '.$langs->transnoentitiesnoconv(
'ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR);
3412 foreach ($batchList as $batch) {
3413 if ($batch->qty <= 0) {
3418 if ($batch->qty >= $product_qty_remain) {
3419 $product_batch_qty = $product_qty_remain;
3422 $product_batch_qty = $batch->qty;
3424 $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_batch_qty, $this->lines[$i]->subprice, $langs->trans(
'InvoiceValidatedInDolibarr', $num),
'',
'',
'', $batch->batch);
3427 $this->error = $mouvP->error;
3428 $this->errors = array_merge($this->errors, $mouvP->errors);
3432 $product_qty_remain -= $product_batch_qty;
3434 if ($product_qty_remain <= 0) {
3439 if (!$error && $product_qty_remain > 0) {
3442 $batch = $batchList[0];
3443 $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_qty_remain, $this->lines[$i]->subprice, $langs->trans(
'InvoiceValidatedInDolibarr', $num),
'',
'',
'', $batch->batch);
3446 $this->error = $mouvP->error;
3447 $this->errors = array_merge($this->errors, $mouvP->errors);
3451 $langs->load(
'errors');
3452 $warehouseStatic->fetch($idwarehouse);
3453 $this->error = $langs->trans(
'ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref);
3454 dol_syslog(__METHOD__.
' Error: '.$langs->transnoentitiesnoconv(
'ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR);
3461 if (!$is_batch_line) {
3462 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
3465 $this->error = $mouvP->error;
3466 $this->errors = array_merge($this->errors, $mouvP->errors);
3479 if (!$error && $this->
type == self::TYPE_CREDIT_NOTE && $this->fk_facture_source > 0) {
3480 $invoice_situation =
new Facture($this->db);
3481 $result = $invoice_situation->fetch($this->fk_facture_source);
3482 if ($result > 0 && $invoice_situation->type == self::TYPE_SITUATION && $invoice_situation->situation_final == 1) {
3483 $invoice_situation->situation_final = 0;
3485 $result = $invoice_situation->setFinal($user, 1);
3488 $this->error = $invoice_situation->error;
3489 $this->errors = array_merge($this->errors, $invoice_situation->errors);
3495 if (!$error && !$notrigger) {
3505 $this->oldref = $this->ref;
3508 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
3510 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'facture/".$this->db->escape($this->newref).
"'";
3511 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'facture/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
3512 $resql = $this->db->query($sql);
3515 $this->error = $this->db->lasterror();
3517 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filepath = 'facture/".$this->db->escape($this->newref).
"'";
3518 $sql .=
" WHERE filepath = 'facture/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
3519 $resql = $this->db->query($sql);
3522 $this->error = $this->db->lasterror();
3528 $dirsource = $conf->facture->dir_output.
'/'.$oldref;
3529 $dirdest = $conf->facture->dir_output.
'/'.$newref;
3530 if (!$error && file_exists($dirsource)) {
3531 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
3533 if (@rename($dirsource, $dirdest)) {
3536 $listoffiles =
dol_dir_list($conf->facture->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
3537 foreach ($listoffiles as $fileentry) {
3538 $dirsource = $fileentry[
'name'];
3539 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
3540 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
3541 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
3542 @rename($dirsource, $dirdest);
3560 $this->date_validation = $now;
3565 $nboflines = count($this->lines);
3566 while (($i < $nboflines) && $final) {
3568 $previousprogress = $this->lines[$i]->get_allprev_progress($this->lines[$i]->fk_facture);
3569 $current_progress = floatval($this->lines[$i]->situation_percent);
3570 $full_progress = $previousprogress + $current_progress;
3571 $final = ($full_progress == 100);
3573 $final = ($this->lines[$i]->situation_percent == 100);
3578 if (empty($final)) {
3579 $this->situation_final = 0;
3581 $this->situation_final = 1;
3592 $this->db->commit();
3595 $this->db->rollback();
3790 $remise_percent = 0,
3793 $fk_code_ventilation = 0,
3795 $fk_remise_except = 0,
3796 $price_base_type =
'HT',
3803 $fk_parent_line = 0,
3804 $fk_fournprice =
null,
3807 $array_options = array(),
3808 $situation_percent = 100,
3813 $noupdateafterinsertline = 0
3817 dol_syslog(__METHOD__.
": using line label is deprecated", LOG_WARNING);
3821 global $mysoc, $conf, $langs;
3823 dol_syslog(get_class($this).
"::addline id=$this->id, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, fk_product=$fk_product, remise_percent=$remise_percent, date_start=$date_start, date_end=$date_end, fk_code_ventilation=$fk_code_ventilation, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, type=$type, fk_unit=$fk_unit, desc=".
dol_trunc($desc, 25), LOG_DEBUG);
3825 if ($this->
status == self::STATUS_DRAFT) {
3826 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3829 if (empty($remise_percent)) {
3830 $remise_percent = 0;
3835 if (empty($info_bits)) {
3841 if (empty($fk_code_ventilation)) {
3842 $fk_code_ventilation = 0;
3844 if (empty($txtva)) {
3847 if (empty($txlocaltax1)) {
3850 if (empty($txlocaltax2)) {
3853 if (empty($fk_parent_line) || $fk_parent_line < 0) {
3854 $fk_parent_line = 0;
3856 if (empty($fk_prev_id)) {
3857 $fk_prev_id =
'null';
3859 if (!isset($situation_percent) || $situation_percent > 100 || (
string) $situation_percent ==
'') {
3860 $situation_percent = 100;
3862 if (empty($ref_ext)) {
3866 $remise_percent = (float)
price2num($remise_percent);
3869 $pu_ht_devise =
price2num($pu_ht_devise);
3872 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
3878 if ($price_base_type ==
'HT') {
3889 if ($date_start && $date_end && $date_start > $date_end) {
3890 $langs->load(
"errors");
3891 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3897 $product_type = $type;
3898 if (!empty($fk_product) && $fk_product > 0) {
3899 $product =
new Product($this->db);
3900 $result = $product->fetch($fk_product);
3901 $product_type = $product->type;
3903 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_INVOICE') && $product_type == 0 && $product->stock_reel < $qty) {
3904 $langs->load(
"errors");
3905 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnInvoice', $product->ref);
3906 $this->db->rollback();
3916 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3917 $vat_src_code = $reg[1];
3918 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3926 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx, $pu_ht_devise);
3928 $total_ht = $tabprice[0];
3929 $total_tva = $tabprice[1];
3930 $total_ttc = $tabprice[2];
3931 $total_localtax1 = $tabprice[9];
3932 $total_localtax2 = $tabprice[10];
3933 $pu_ht = $tabprice[3];
3936 $multicurrency_total_ht = $tabprice[16];
3937 $multicurrency_total_tva = $tabprice[17];
3938 $multicurrency_total_ttc = $tabprice[18];
3939 $pu_ht_devise = $tabprice[19];
3943 if ($ranktouse == -1) {
3944 $rangmax = $this->
line_max($fk_parent_line);
3945 $ranktouse = $rangmax + 1;
3951 $this->line->context = $this->context;
3953 $this->line->fk_facture = $this->id;
3954 $this->line->label = $label;
3955 $this->line->desc = $desc;
3956 $this->line->ref_ext = $ref_ext;
3958 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs((
float) $qty) : $qty);
3959 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
3961 $this->line->vat_src_code = $vat_src_code;
3962 $this->line->tva_tx = $txtva;
3963 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
3964 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
3965 $this->line->localtax1_type = empty($localtaxes_type[0]) ? 0 : $localtaxes_type[0];
3966 $this->line->localtax2_type = empty($localtaxes_type[2]) ? 0 : $localtaxes_type[2];
3968 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ht) : $total_ht);
3969 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ttc) : $total_ttc);
3970 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_tva) : $total_tva);
3971 $this->line->total_localtax1 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax1) : $total_localtax1);
3972 $this->line->total_localtax2 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax2) : $total_localtax2);
3974 $this->line->fk_product = $fk_product;
3975 $this->line->product_type = $product_type;
3976 $this->line->remise_percent = $remise_percent;
3977 $this->line->date_start = $date_start;
3978 $this->line->date_end = $date_end;
3979 $this->line->fk_code_ventilation = $fk_code_ventilation;
3980 $this->line->rang = $ranktouse;
3981 $this->line->info_bits = $info_bits;
3982 $this->line->fk_remise_except = $fk_remise_except;
3984 $this->line->special_code = $special_code;
3985 $this->line->fk_parent_line = $fk_parent_line;
3986 $this->line->origin = $origin;
3987 $this->line->origin_id = $origin_id;
3988 $this->line->situation_percent = $situation_percent;
3989 $this->line->fk_prev_id = $fk_prev_id;
3990 $this->line->fk_unit = $fk_unit;
3993 $this->line->fk_fournprice = $fk_fournprice;
3994 $this->line->pa_ht = $pa_ht;
3997 $this->line->fk_multicurrency = $this->fk_multicurrency;
3998 $this->line->multicurrency_code = $this->multicurrency_code;
3999 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht_devise) : $pu_ht_devise);
4001 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ht) : $multicurrency_total_ht);
4002 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_tva) : $multicurrency_total_tva);
4003 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
4005 if (is_array($array_options) && count($array_options) > 0) {
4006 $this->line->array_options = $array_options;
4009 $result = $this->line->insert();
4012 if (!empty($fk_parent_line)) {
4014 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
4015 $linecount = count($this->lines);
4016 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
4022 if (empty($noupdateafterinsertline)) {
4027 $this->db->commit();
4028 return $this->line->id;
4030 $this->error = $this->db->lasterror();
4031 $this->db->rollback();
4035 $this->error = $this->line->error;
4036 $this->errors = $this->line->errors;
4037 $this->db->rollback();
4041 $this->errors[] =
'status of invoice must be Draft to allow use of ->addline()';
4042 dol_syslog(get_class($this).
"::addline status of invoice must be Draft to allow use of ->addline()", LOG_ERR);
4078 public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $price_base_type =
'HT', $info_bits = 0, $type = self::TYPE_STANDARD, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice =
null, $pa_ht = 0, $label =
'', $special_code = 0, $array_options = array(), $situation_percent = 100, $fk_unit =
null, $pu_ht_devise = 0, $notrigger = 0, $ref_ext =
'', $rang = 0)
4080 global $conf, $user;
4083 dol_syslog(__METHOD__.
": using line label is deprecated", LOG_WARNING);
4086 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
4088 global $mysoc, $langs;
4090 dol_syslog(get_class($this).
"::updateline rowid=$rowid, desc=$desc, pu=$pu, qty=$qty, remise_percent=$remise_percent, date_start=$date_start, date_end=$date_end, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, price_base_type=$price_base_type, info_bits=$info_bits, type=$type, fk_parent_line=$fk_parent_line pa_ht=$pa_ht, special_code=$special_code, fk_unit=$fk_unit, pu_ht_devise=$pu_ht_devise", LOG_DEBUG);
4092 if ($this->
status == self::STATUS_DRAFT) {
4095 if (!$this->error) {
4096 $this->error = $langs->trans(
'invoiceLineProgressError');
4102 if ($date_start && $date_end && $date_start > $date_end) {
4103 $langs->load(
"errors");
4104 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
4114 if (empty($fk_parent_line) || $fk_parent_line < 0) {
4115 $fk_parent_line = 0;
4117 if (empty($special_code) || $special_code == 3) {
4120 if (!isset($situation_percent) || $situation_percent > 100 || (
string) $situation_percent ==
'') {
4121 $situation_percent = 100;
4123 if (empty($ref_ext)) {
4127 $remise_percent = (float)
price2num($remise_percent);
4130 $pu_ht_devise =
price2num($pu_ht_devise);
4132 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
4135 $txlocaltax1 = (float)
price2num($txlocaltax1);
4136 $txlocaltax2 = (float)
price2num($txlocaltax2);
4152 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
4153 $vat_src_code = $reg[1];
4154 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
4157 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx, $pu_ht_devise);
4159 $total_ht = $tabprice[0];
4160 $total_tva = $tabprice[1];
4161 $total_ttc = $tabprice[2];
4162 $total_localtax1 = $tabprice[9];
4163 $total_localtax2 = $tabprice[10];
4164 $pu_ht = $tabprice[3];
4165 $pu_tva = $tabprice[4];
4166 $pu_ttc = $tabprice[5];
4169 $multicurrency_total_ht = $tabprice[16];
4170 $multicurrency_total_tva = $tabprice[17];
4171 $multicurrency_total_ttc = $tabprice[18];
4172 $pu_ht_devise = $tabprice[19];
4177 if ($remise_percent > 0) {
4178 $remise = round(((
float) $pu * (
float) $remise_percent / 100), 2);
4179 $price = ((float) $pu - $remise);
4185 $line->fetch($rowid);
4186 $line->fetch_optionals();
4188 if (!empty($line->fk_product)) {
4189 $product =
new Product($this->db);
4190 $result = $product->fetch($line->fk_product);
4191 $product_type = $product->type;
4193 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_INVOICE') && $product_type == 0 && $product->stock_reel < $qty) {
4194 $langs->load(
"errors");
4195 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnInvoice', $product->ref);
4196 $this->db->rollback();
4201 $staticline = clone $line;
4203 $line->oldline = $staticline;
4204 $this->line = $line;
4205 $this->line->context = $this->context;
4206 $this->line->rang = $rang;
4209 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
4210 $rangmax = $this->
line_max($fk_parent_line);
4211 $this->line->rang = $rangmax + 1;
4214 $this->line->id = $rowid;
4215 $this->line->rowid = $rowid;
4216 $this->line->label = $label;
4217 $this->line->desc = $desc;
4218 $this->line->ref_ext = $ref_ext;
4219 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs((
float) $qty) : $qty);
4221 $this->line->vat_src_code = $vat_src_code;
4222 $this->line->tva_tx = $txtva;
4223 $this->line->localtax1_tx = $txlocaltax1;
4224 $this->line->localtax2_tx = $txlocaltax2;
4225 $this->line->localtax1_type = empty($localtaxes_type[0]) ? 0 : $localtaxes_type[0];
4226 $this->line->localtax2_type = empty($localtaxes_type[2]) ? 0 : $localtaxes_type[2];
4228 $this->line->remise_percent = $remise_percent;
4229 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
4230 $this->line->date_start = $date_start;
4231 $this->line->date_end = $date_end;
4232 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ht) : $total_ht);
4233 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_tva) : $total_tva);
4234 $this->line->total_localtax1 = $total_localtax1;
4235 $this->line->total_localtax2 = $total_localtax2;
4236 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ttc) : $total_ttc);
4237 $this->line->info_bits = $info_bits;
4238 $this->line->special_code = $special_code;
4239 $this->line->product_type = $type;
4240 $this->line->fk_parent_line = $fk_parent_line;
4241 $this->line->skip_update_total = $skip_update_total;
4242 $this->line->situation_percent = $situation_percent;
4243 $this->line->fk_unit = $fk_unit;
4245 $this->line->fk_fournprice = $fk_fournprice;
4246 $this->line->pa_ht = $pa_ht;
4249 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht_devise) : $pu_ht_devise);
4250 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ht) : $multicurrency_total_ht);
4251 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_tva) : $multicurrency_total_tva);
4252 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
4254 if (is_array($array_options) && count($array_options) > 0) {
4256 foreach ($array_options as $key => $value) {
4257 $this->line->array_options[$key] = $array_options[$key];
4261 $result = $this->line->update($user, $notrigger);
4264 if (!empty($fk_parent_line)) {
4270 $this->db->commit();
4273 $this->error = $this->line->error;
4274 $this->db->rollback();
4278 $this->error =
"Invoice statut makes operation forbidden";
5023 global $conf, $langs, $user;
5027 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
5032 $sql =
"SELECT rowid";
5033 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
5034 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
5035 $sql .= $this->db->plimit(100);
5037 $resql = $this->db->query($sql);
5039 $num_prods = $this->db->num_rows($resql);
5041 while ($i < $num_prods) {
5043 $row = $this->db->fetch_row($resql);
5044 $prodids[$i] = $row[0];
5048 if (empty($num_prods)) {
5055 $this->
ref =
'SPECIMEN';
5056 $this->specimen = 1;
5058 $this->date = $nownotime;
5059 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
5060 $this->cond_reglement_id = 1;
5061 $this->cond_reglement_code =
'RECEP';
5063 $this->mode_reglement_id = 0;
5064 $this->mode_reglement_code =
'';
5066 $this->note_public =
'This is a comment (public)';
5067 $this->note_private =
'This is a comment (private)';
5068 $this->note =
'This is a comment (private)';
5070 $this->fk_user_author = $user->id;
5072 $this->multicurrency_tx = 1;
5073 $this->multicurrency_code = $conf->currency;
5075 $this->fk_incoterms = 0;
5076 $this->location_incoterms =
'';
5078 if (empty($option) || $option !=
'nolines') {
5082 while ($xnbp < $nbp) {
5084 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
5086 $line->subprice = 100;
5087 $line->tva_tx = 19.6;
5088 $line->localtax1_tx = 0;
5089 $line->localtax2_tx = 0;
5090 $line->remise_percent = 0;
5092 $prodid = mt_rand(1, $num_prods);
5093 $line->fk_product = $prodids[$prodid];
5095 $line->total_ht = -100;
5096 $line->total_ttc = -119.6;
5097 $line->total_tva = -19.6;
5098 $line->multicurrency_total_ht = -200;
5099 $line->multicurrency_total_ttc = -239.2;
5100 $line->multicurrency_total_tva = -39.2;
5101 } elseif ($xnbp == 2) {
5102 $line->subprice = -100;
5103 $line->total_ht = -100;
5104 $line->total_ttc = -119.6;
5105 $line->total_tva = -19.6;
5106 $line->remise_percent = 0;
5107 $line->multicurrency_total_ht = -200;
5108 $line->multicurrency_total_ttc = -239.2;
5109 $line->multicurrency_total_tva = -39.2;
5110 } elseif ($xnbp == 3) {
5111 $prodid = mt_rand(1, $num_prods);
5112 $line->fk_product = $prodids[$prodid];
5113 $line->total_ht = 50;
5114 $line->total_ttc = 59.8;
5115 $line->total_tva = 9.8;
5116 $line->multicurrency_total_ht = 100;
5117 $line->multicurrency_total_ttc = 119.6;
5118 $line->multicurrency_total_tva = 19.6;
5119 $line->remise_percent = 50;
5121 $prodid = mt_rand(1, $num_prods);
5122 $line->fk_product = $prodids[$prodid];
5123 $line->total_ht = 100;
5124 $line->total_ttc = 119.6;
5125 $line->total_tva = 19.6;
5126 $line->multicurrency_total_ht = 200;
5127 $line->multicurrency_total_ttc = 239.2;
5128 $line->multicurrency_total_tva = 39.2;
5129 $line->remise_percent = 0;
5132 $this->lines[$xnbp] = $line;
5135 $this->total_ht += $line->total_ht;
5136 $this->total_tva += $line->total_tva;
5137 $this->total_ttc += $line->total_ttc;
5139 $this->multicurrency_total_ht += $line->multicurrency_total_ht;
5140 $this->multicurrency_total_tva += $line->multicurrency_total_tva;
5141 $this->multicurrency_total_ttc += $line->multicurrency_total_ttc;
5145 $this->revenuestamp = 0;
5149 $line->desc = $langs->trans(
"Description").
" (offered line)";
5151 $line->subprice = 100;
5152 $line->tva_tx = 19.6;
5153 $line->localtax1_tx = 0;
5154 $line->localtax2_tx = 0;
5155 $line->remise_percent = 100;
5156 $line->total_ht = 0;
5157 $line->total_ttc = 0;
5158 $line->total_tva = 0;
5159 $line->multicurrency_total_ht = 0;
5160 $line->multicurrency_total_ttc = 0;
5161 $line->multicurrency_total_tva = 0;
5162 $prodid = mt_rand(1, $num_prods);
5163 $line->fk_product = $prodids[$prodid];
5165 $this->lines[$xnbp] = $line;
5651 global $conf, $langs, $user;
5657 $errorsMsg = array();
5659 $langs->load(
"bills");
5661 if (!isModEnabled(
'invoice')) {
5662 $this->output .= $langs->trans(
'ModuleNotEnabled', $langs->transnoentitiesnoconv(
"Facture"));
5665 if (!in_array($datetouse, array(
'duedate',
'invoicedate'))) {
5666 $this->output .=
'Bad value for parameter datetouse. Must be "duedate" or "invoicedate"';
5676 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
5677 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
5678 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
5679 $formmail =
new FormMail($this->db);
5684 $tmpinvoice =
new Facture($this->db);
5689 $sql =
"SELECT rowid as id FROM ".MAIN_DB_PREFIX.
"facture as f";
5690 if (!empty($paymentmode) && $paymentmode !=
'all') {
5691 $sql .=
", ".MAIN_DB_PREFIX.
"c_paiement as cp";
5693 $sql .=
" WHERE f.paye = 0";
5694 $sql .=
" AND f.fk_statut = ".self::STATUS_VALIDATED;
5695 if ($datetouse ==
'invoicedate') {
5696 $sql .=
" AND f.datef = '".$this->db->idate($tmpidate,
'gmt').
"'";
5698 $sql .=
" AND f.date_lim_reglement = '".$this->db->idate($tmpidate,
'gmt').
"'";
5700 $sql .=
" AND f.entity IN (".getEntity(
'facture', 0).
")";
5701 if (!empty($paymentmode) && $paymentmode !=
'all') {
5702 $sql .=
" AND f.fk_mode_reglement = cp.id AND cp.code = '".$this->db->escape($paymentmode).
"'";
5705 if ($datetouse ==
'invoicedate') {
5706 $sql .= $this->db->order(
"datef",
"ASC");
5708 $sql .= $this->db->order(
"date_lim_reglement",
"ASC");
5711 $resql = $this->db->query($sql);
5714 if ($datetouse ==
'invoicedate') {
5715 $this->output .= $langs->transnoentitiesnoconv(
"SearchValidatedInvoicesWithDate", $stmpidate);
5717 $this->output .= $langs->transnoentitiesnoconv(
"SearchUnpaidInvoicesWithDueDate", $stmpidate);
5719 if (!empty($paymentmode) && $paymentmode !=
'all') {
5720 $this->output .=
' ('.$langs->transnoentitiesnoconv(
"PaymentMode").
' '.$paymentmode.
')';
5722 $this->output .=
'<br>';
5725 while ($obj = $this->db->fetch_object($resql)) {
5728 $res = $tmpinvoice->fetch($obj->id);
5730 $tmpinvoice->fetch_thirdparty();
5732 $outputlangs =
new Translate(
'', $conf);
5733 if ($tmpinvoice->thirdparty->default_lang) {
5734 $outputlangs->setDefaultLang($tmpinvoice->thirdparty->default_lang);
5735 $outputlangs->loadLangs(array(
"main",
"bills"));
5737 $outputlangs = $langs;
5741 $arraymessage = $formmail->getEMailTemplate($this->db,
'facture_send', $user, $outputlangs, (is_numeric($template) ? $template : 0), 1, (is_numeric($template) ?
'' : $template));
5742 if (is_numeric($arraymessage) && $arraymessage <= 0) {
5743 $langs->load(
"errors");
5744 $this->output .= $langs->trans(
'ErrorFailedToFindEmailTemplate', $template);
5757 $sendTopic =
make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv(
'InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
5760 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
5766 if ($forcerecipient) {
5767 $to = array($forcerecipient);
5769 $res = $tmpinvoice->fetch_thirdparty();
5770 $recipient = $tmpinvoice->thirdparty;
5772 $tmparraycontact = $tmpinvoice->liste_contact(-1,
'external', 0,
'BILLING');
5773 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
5774 foreach ($tmparraycontact as $data_email) {
5775 if (!empty($data_email[
'email'])) {
5776 $to[] = $tmpinvoice->thirdparty->contact_get_property($data_email[
'id'],
'email');
5780 if (empty($to) && !empty($recipient->email)) {
5781 $to[] = $recipient->email;
5784 $errormesg =
"Failed to send remind to thirdparty id=".$tmpinvoice->socid.
". No email defined for invoice or customer.";
5788 $errormesg =
"Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
5795 if (!empty($arraymessage->email_from)) {
5796 $from = $arraymessage->email_from;
5799 $errormesg =
"Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
5803 if (!$error && !empty($to)) {
5806 $to = implode(
',', $to);
5807 if (!empty($arraymessage->email_to)) {
5808 $to = $to.
','.$arraymessage->email_to;
5814 $trackid =
'inv'.$tmpinvoice->id;
5815 $sendcontext =
'standard';
5818 if (!empty($arraymessage->email_tocc)) {
5819 $email_tocc = $arraymessage->email_tocc;
5823 if (!empty($arraymessage->email_tobcc)) {
5824 $email_tobcc = $arraymessage->email_tobcc;
5831 if ($arraymessage->joinfiles == 1 && !empty($tmpinvoice->last_main_doc)) {
5832 $joinFile[] = DOL_DATA_ROOT.
'/'.$tmpinvoice->last_main_doc;
5833 $joinFileName[] = basename($tmpinvoice->last_main_doc);
5834 $joinFileMime[] =
dol_mimetype(DOL_DATA_ROOT.
'/'.$tmpinvoice->last_main_doc);
5838 $cMailFile =
new CMailFile($sendTopic, $to, $from, $sendContent, $joinFile, $joinFileMime, $joinFileName, $email_tocc, $email_tobcc, 0, 1, $errors_to,
'', $trackid,
'', $sendcontext,
'');
5841 if ($cMailFile->sendfile()) {
5845 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
5850 $actioncomm->type_code =
'AC_OTH_AUTO';
5851 $actioncomm->socid = $tmpinvoice->thirdparty->id;
5852 $actioncomm->contact_id = 0;
5854 $actioncomm->code =
'AC_EMAIL';
5855 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateOK (nbdays='.$nbdays.
' paymentmode='.$paymentmode.
' template='.$template.
' datetouse='.$datetouse.
' forcerecipient='.$forcerecipient.
')';
5856 $actioncomm->note_private = $sendContent;
5857 $actioncomm->fk_project = $tmpinvoice->fk_project;
5858 $actioncomm->datep =
dol_now();
5859 $actioncomm->datef = $actioncomm->datep;
5860 $actioncomm->percentage = -1;
5861 $actioncomm->authorid = $user->id;
5862 $actioncomm->userownerid = $user->id;
5864 $actioncomm->email_msgid = $cMailFile->msgid;
5865 $actioncomm->email_subject = $sendTopic;
5866 $actioncomm->email_from = $from;
5867 $actioncomm->email_sender =
'';
5868 $actioncomm->email_to = $to;
5872 $actioncomm->errors_to = $errors_to;
5874 $actioncomm->elementtype =
'invoice';
5875 $actioncomm->fk_element = $tmpinvoice->id;
5879 $actioncomm->create($user);
5881 $errormesg = $cMailFile->error.
' : '.$to;
5885 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
5890 $actioncomm->type_code =
'AC_OTH_AUTO';
5891 $actioncomm->socid = $tmpinvoice->thirdparty->id;
5892 $actioncomm->contact_id = 0;
5894 $actioncomm->code =
'AC_EMAIL';
5895 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateKO';
5896 $actioncomm->note_private = $errormesg;
5897 $actioncomm->fk_project = $tmpinvoice->fk_project;
5898 $actioncomm->datep =
dol_now();
5899 $actioncomm->datef = $actioncomm->datep;
5900 $actioncomm->percentage = -1;
5901 $actioncomm->authorid = $user->id;
5902 $actioncomm->userownerid = $user->id;
5904 $actioncomm->email_msgid = $cMailFile->msgid;
5905 $actioncomm->email_from = $from;
5906 $actioncomm->email_sender =
'';
5907 $actioncomm->email_to = $to;
5911 $actioncomm->errors_to = $errors_to;
5915 $actioncomm->create($user);
5918 $this->db->commit();
5922 $errorsMsg[] = $errormesg;
5925 $errorsMsg[] =
'Failed to fetch record invoice with ID = '.$obj->id;
5935 $this->output .=
'Nb of emails sent : '.$nbMailSend;
5937 dol_syslog(__METHOD__.
" end - ".$this->output, LOG_INFO);
5941 $this->error =
'Nb of emails sent : '.$nbMailSend.
', '.(!empty($errorsMsg) ? implode(
', ', $errorsMsg) : $error);
5943 dol_syslog(__METHOD__.
" end - ".$this->error, LOG_INFO);