493 public function create(
User $user, $notrigger = 0, $forceduedate = 0)
495 global $langs, $conf, $mysoc, $hookmanager;
499 if (empty($this->
type)) {
503 $this->ref_client = trim($this->ref_client);
505 $this->note = (isset($this->note) ? trim($this->note) : trim($this->note_private));
506 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note_private));
507 $this->note_public = trim($this->note_public);
508 if (!$this->cond_reglement_id) {
509 $this->cond_reglement_id = 0;
511 if (!$this->mode_reglement_id) {
512 $this->mode_reglement_id = 0;
514 $this->brouillon = 1;
518 if (!empty($this->multicurrency_code)) {
520 if (empty($this->multicurrency_tx)) {
528 $this->fk_multicurrency = 0;
530 if (empty($this->fk_multicurrency)) {
531 $this->multicurrency_code = $conf->currency;
532 $this->fk_multicurrency = 0;
533 $this->multicurrency_tx = 1;
536 dol_syslog(get_class($this).
"::create user=".$user->id.
" date=".$this->date);
539 if (empty($this->date)) {
540 $this->error =
"Try to create an invoice with an empty parameter (date)";
541 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
545 $result = $soc->fetch($this->socid);
547 $this->error =
"Failed to fetch company: ".$soc->error;
548 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
553 $this->date_creation = $now;
557 $originaldatewhen =
null;
558 $nextdatewhen =
null;
559 $previousdaynextdatewhen =
null;
562 if ($this->fac_rec > 0) {
563 $this->fk_fac_rec_source = $this->fac_rec;
565 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
567 $result = $_facrec->fetch($this->fac_rec);
568 $result = $_facrec->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
571 $originaldatewhen = $_facrec->date_when;
572 $nextdatewhen =
null; $previousdaynextdatewhen =
null;
573 if ($originaldatewhen) {
574 $nextdatewhen =
dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency);
578 if (!empty($_facrec->frequency)) {
579 $this->socid = $_facrec->socid;
581 $this->entity = $_facrec->entity;
584 $this->fk_project =
GETPOST(
'projectid',
'int') > 0 ? ((int)
GETPOST(
'projectid',
'int')) : $_facrec->fk_project;
585 $this->note_public = GETPOSTISSET(
'note_public') ?
GETPOST(
'note_public',
'restricthtml') : $_facrec->note_public;
586 $this->note_private = GETPOSTISSET(
'note_private') ?
GETPOST(
'note_private',
'restricthtml') : $_facrec->note_private;
587 $this->model_pdf = GETPOSTISSET(
'model') ?
GETPOST(
'model',
'alpha') : $_facrec->model_pdf;
588 $this->cond_reglement_id =
GETPOST(
'cond_reglement_id',
'int') > 0 ? ((int)
GETPOST(
'cond_reglement_id',
'int')) : $_facrec->cond_reglement_id;
589 $this->mode_reglement_id =
GETPOST(
'mode_reglement_id',
'int') > 0 ? ((int)
GETPOST(
'mode_reglement_id',
'int')) : $_facrec->mode_reglement_id;
590 $this->fk_account =
GETPOST(
'fk_account') > 0 ? ((int)
GETPOST(
'fk_account')) : $_facrec->fk_account;
593 $this->total_ht = $_facrec->total_ht;
594 $this->total_ttc = $_facrec->total_ttc;
597 $this->remise_absolue = $_facrec->remise_absolue;
598 $this->remise_percent = $_facrec->remise_percent;
599 $this->fk_incoterms = $_facrec->fk_incoterms;
600 $this->location_incoterms = $_facrec->location_incoterms;
606 $this->ref_client = trim($this->ref_client);
607 $this->ref_customer = trim($this->ref_customer);
608 $this->note_public = trim($this->note_public);
609 $this->note_private = trim($this->note_private);
610 $this->note_private =
dol_concatdesc($this->note_private, $langs->trans(
"GeneratedFromRecurringInvoice", $_facrec->ref));
612 $this->array_options = $_facrec->array_options;
614 if (!$this->mode_reglement_id) {
615 $this->mode_reglement_id = 0;
617 $this->brouillon = 1;
621 $this->linked_objects = $_facrec->linkedObjectsIds;
626 if ($_facrec->frequency > 0) {
627 dol_syslog(
"This is a recurring invoice so we set date_last_gen and next date_when");
628 if (empty($_facrec->date_when)) {
629 $_facrec->date_when = $now;
631 $next_date = $_facrec->getNextDate();
632 $result = $_facrec->setValueFrom(
'date_last_gen', $now,
'',
null,
'date',
'', $user,
'');
634 $result = $_facrec->setNextDate($next_date, 1);
638 $outputlangs = $langs;
641 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && empty($newlang) && isset($this->thirdparty->default_lang)) {
642 $newlang = $this->thirdparty->default_lang;
644 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && empty($newlang) && isset($this->default_lang)) {
645 $newlang = $this->default_lang;
647 if (!empty($newlang)) {
649 $outputlangs->setDefaultLang($newlang);
655 $substitutionarray[
'__INVOICE_MONTH__'] =
dol_print_date($this->date,
'%m');
658 $substitutionarray[
'__INVOICE_MONTH_TEXT__'] =
dol_print_date($this->date,
'%B');
661 $substitutionarray[
'__INVOICE_YEAR__'] =
dol_print_date($this->date,
'%Y');
664 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] = (isset($originaldatewhen) ?
dol_print_date($originaldatewhen,
'dayhour') :
'');
665 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] = (isset($nextdatewhen) ?
dol_print_date($nextdatewhen,
'dayhour') :
'');
666 $substitutionarray[
'__INVOICE_PREVIOUS_DATE_NEXT_INVOICE_AFTER_GEN__'] = (isset($previousdaynextdatewhen) ?
dol_print_date($previousdaynextdatewhen,
'dayhour') :
'');
667 $substitutionarray[
'__INVOICE_COUNTER_CURRENT__'] = $_facrec->nb_gen_done;
668 $substitutionarray[
'__INVOICE_COUNTER_MAX__'] = $_facrec->nb_gen_max;
679 if (empty($forceduedate)) {
685 $this->date_lim_reglement = $duedate;
687 $this->date_lim_reglement = $forceduedate;
691 $socid = $this->socid;
693 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture (";
700 $sql .=
", remise_absolue";
701 $sql .=
", remise_percent";
703 $sql .=
", date_pointoftax";
704 $sql .=
", note_private";
705 $sql .=
", note_public";
706 $sql .=
", ref_client";
707 $sql .=
", fk_account";
708 $sql .=
", module_source, pos_source, fk_fac_rec_source, fk_facture_source, fk_user_author, fk_projet";
709 $sql .=
", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf";
710 $sql .=
", situation_cycle_ref, situation_counter, situation_final";
711 $sql .=
", fk_incoterms, location_incoterms";
712 $sql .=
", fk_multicurrency";
713 $sql .=
", multicurrency_code";
714 $sql .=
", multicurrency_tx";
715 $sql .=
", retained_warranty";
716 $sql .=
", retained_warranty_date_limit";
717 $sql .=
", retained_warranty_fk_cond_reglement";
721 $sql .=
", ".setEntity($this);
722 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
723 $sql .=
", '".$this->db->escape($this->
type).
"'";
724 $sql .=
", ".((int) $socid);
725 $sql .=
", '".$this->db->idate($this->date_creation).
"'";
726 $sql .=
", ".($this->remise_absolue > 0 ? $this->remise_absolue :
'NULL');
727 $sql .=
", ".($this->remise_percent > 0 ? $this->remise_percent :
'NULL');
728 $sql .=
", '".$this->db->idate($this->date).
"'";
729 $sql .=
", ".(empty($this->date_pointoftax) ?
"null" :
"'".$this->db->idate($this->date_pointoftax).
"'");
730 $sql .=
", ".($this->note_private ?
"'".$this->db->escape($this->note_private).
"'" :
"null");
731 $sql .=
", ".($this->note_public ?
"'".$this->db->escape($this->note_public).
"'" :
"null");
732 $sql .=
", ".($this->ref_customer ?
"'".$this->db->escape($this->ref_customer).
"'" : ($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null"));
733 $sql .=
", ".($this->fk_account > 0 ? $this->fk_account :
'NULL');
734 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
735 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
736 $sql .=
", ".($this->fk_fac_rec_source ?
"'".$this->db->escape($this->fk_fac_rec_source).
"'" :
"null");
737 $sql .=
", ".($this->fk_facture_source ?
"'".$this->db->escape($this->fk_facture_source).
"'" :
"null");
738 $sql .=
", ".($user->id > 0 ? (int) $user->id :
"null");
739 $sql .=
", ".($this->fk_project ? $this->fk_project :
"null");
740 $sql .=
", ".((int) $this->cond_reglement_id);
741 $sql .=
", ".((int) $this->mode_reglement_id);
742 $sql .=
", '".$this->db->idate($this->date_lim_reglement).
"'";
743 $sql .=
", ".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null");
744 $sql .=
", ".($this->situation_cycle_ref ?
"'".$this->db->escape($this->situation_cycle_ref).
"'" :
"null");
745 $sql .=
", ".($this->situation_counter ?
"'".$this->db->escape($this->situation_counter).
"'" :
"null");
746 $sql .=
", ".($this->situation_final ? $this->situation_final : 0);
747 $sql .=
", ".(int) $this->fk_incoterms;
748 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
749 $sql .=
", ".(int) $this->fk_multicurrency;
750 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
751 $sql .=
", ".(double) $this->multicurrency_tx;
752 $sql .=
", ".(empty($this->retained_warranty) ?
"0" : $this->db->escape($this->retained_warranty));
753 $sql .=
", ".(!empty($this->retained_warranty_date_limit) ?
"'".$this->db->idate($this->retained_warranty_date_limit).
"'" :
'NULL');
754 $sql .=
", ".(int) $this->retained_warranty_fk_cond_reglement;
757 $resql = $this->db->query($sql);
759 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture');
762 $this->
ref =
'(PROV'.$this->id.
')';
763 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"facture SET ref='".$this->db->escape($this->
ref).
"' WHERE rowid=".((int) $this->
id);
765 $resql = $this->db->query($sql);
770 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
771 $this->linked_objects = $this->linkedObjectsIds;
775 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
776 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
777 if (is_array($tmp_origin_id)) {
778 foreach ($tmp_origin_id as $origin_id) {
781 $this->error = $this->db->lasterror();
787 $origin_id = $tmp_origin_id;
790 $this->error = $this->db->lasterror();
798 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
799 $originforcontact = $this->origin;
800 $originidforcontact = $this->origin_id;
801 if ($originforcontact ==
'shipping') {
802 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
804 $exp->fetch($this->origin_id);
805 $exp->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
806 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
807 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
808 $originforcontact =
'commande';
809 if (is_object($value)) {
810 $originidforcontact = $value->id;
812 $originidforcontact = $value;
819 $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";
820 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
822 $resqlcontact = $this->db->query($sqlcontact);
824 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
826 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
836 if (!$error && empty($this->fac_rec) && count($this->lines) && is_object($this->lines[0])) {
839 dol_syslog(
"There is ".count($this->lines).
" lines that are invoice lines objects");
840 foreach ($this->lines as $i => $val) {
841 $newinvoiceline = $this->lines[$i];
843 $newinvoiceline->context = $this->context;
845 $newinvoiceline->fk_facture = $this->id;
847 $newinvoiceline->origin = $this->lines[$i]->element;
848 $newinvoiceline->origin_id = $this->lines[$i]->id;
851 if ($this->lines[$i]->date_start_fill == 1 && $originaldatewhen) {
852 $newinvoiceline->date_start = $originaldatewhen;
854 if ($this->lines[$i]->date_end_fill == 1 && $previousdaynextdatewhen) {
855 $newinvoiceline->date_end = $previousdaynextdatewhen;
860 if (($newinvoiceline->product_type != 9 && empty($newinvoiceline->fk_parent_line)) || $newinvoiceline->product_type == 9) {
865 $vatrate = $newinvoiceline->tva_tx;
866 if ($newinvoiceline->vat_src_code && ! preg_match(
'/\(.*\)/', $vatrate)) $vatrate.=
' ('.$newinvoiceline->vat_src_code.
')';
868 $newinvoiceline->fk_parent_line = $fk_parent_line;
872 $discount->fetch($newinvoiceline->fk_remise_except);
874 $discountId = $soc->set_remise_except($discount->amount_ht, $user, $discount->description, $discount->tva_tx);
875 $newinvoiceline->fk_remise_except = $discountId;
879 $newinvoiceline->desc,
880 $newinvoiceline->subprice,
881 $newinvoiceline->qty,
883 $newinvoiceline->localtax1_tx,
884 $newinvoiceline->localtax2_tx,
885 $newinvoiceline->fk_product,
886 $newinvoiceline->remise_percent,
887 $newinvoiceline->date_start,
888 $newinvoiceline->date_end,
889 $newinvoiceline->fk_code_ventilation,
890 $newinvoiceline->info_bits,
891 $newinvoiceline->fk_remise_except,
894 $newinvoiceline->product_type,
895 $newinvoiceline->rang,
896 $newinvoiceline->special_code,
897 $newinvoiceline->element,
900 $newinvoiceline->fk_fournprice,
901 $newinvoiceline->pa_ht,
902 $newinvoiceline->label,
903 $newinvoiceline->array_options,
904 $newinvoiceline->situation_percent,
905 $newinvoiceline->fk_prev_id,
906 $newinvoiceline->fk_unit,
907 $newinvoiceline->multicurrency_subprice,
908 $newinvoiceline->ref_ext,
913 if ($result > 0 && $newinvoiceline->product_type == 9) {
914 $fk_parent_line = $result;
918 $this->error = $newinvoiceline->error;
919 $this->errors = $newinvoiceline->errors;
924 } elseif (!$error && empty($this->fac_rec)) {
927 dol_syslog(
"There is ".count($this->lines).
" lines that are array lines");
929 foreach ($this->lines as $i => $val) {
930 $line = $this->lines[$i];
934 if (!is_object($line)) {
935 $line = (object) $line;
940 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
945 $vatrate = $line->tva_tx;
946 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) {
947 $vatrate .=
' ('.$line->vat_src_code.
')';
950 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
951 $originid = $line->origin_id;
952 $origintype = $line->origin;
954 $originid = $line->id;
955 $origintype = $this->element;
959 if (empty($line->ref_ext)) {
971 $line->remise_percent,
974 $line->fk_code_ventilation,
976 $line->fk_remise_except,
985 $line->fk_fournprice,
988 $line->array_options,
989 $line->situation_percent,
992 $line->multicurrency_subprice,
997 $this->error = $this->db->lasterror();
999 $this->db->rollback();
1004 if ($result > 0 && $line->product_type == 9) {
1005 $fk_parent_line = $result;
1014 if (!$error && $this->fac_rec > 0) {
1015 foreach ($_facrec->lines as $i => $val) {
1016 if ($_facrec->lines[$i]->fk_product) {
1017 $prod =
new Product($this->db);
1018 $res = $prod->fetch($_facrec->lines[$i]->fk_product);
1029 $tva_tx = $_facrec->lines[$i]->tva_tx.($_facrec->lines[$i]->vat_src_code ?
'('.$_facrec->lines[$i]->vat_src_code.
')' :
'');
1030 $tva_npr = $_facrec->lines[$i]->info_bits;
1031 if (empty($tva_tx)) {
1034 $localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
1035 $localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
1037 $fk_product_fournisseur_price = empty($_facrec->lines[$i]->fk_product_fournisseur_price) ? null : $_facrec->lines[$i]->fk_product_fournisseur_price;
1038 $buyprice = empty($_facrec->lines[$i]->buyprice) ? 0 : $_facrec->lines[$i]->buyprice;
1041 if (!$buyprice && $_facrec->lines[$i]->fk_product > 0) {
1042 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
1044 $producttmp->fetch($_facrec->lines[$i]->fk_product);
1049 if ($conf->global->MARGIN_TYPE ==
'costprice' && !empty($producttmp->cost_price)) {
1050 $buyprice = $producttmp->cost_price;
1051 } elseif (isModEnabled(
'stock') && ($conf->global->MARGIN_TYPE ==
'costprice' || $conf->global->MARGIN_TYPE ==
'pmp') && !empty($producttmp->pmp)) {
1052 $buyprice = $producttmp->pmp;
1054 if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0) {
1055 if ($producttmp->product_fourn_price_id > 0) {
1056 $buyprice =
price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent / 100) + $producttmp->fourn_remise,
'MU');
1062 $result_insert = $this->
addline(
1063 $_facrec->lines[$i]->desc,
1064 $_facrec->lines[$i]->subprice,
1065 $_facrec->lines[$i]->qty,
1069 $_facrec->lines[$i]->fk_product,
1070 $_facrec->lines[$i]->remise_percent,
1071 ($_facrec->lines[$i]->date_start_fill == 1 && $originaldatewhen) ? $originaldatewhen :
'',
1072 ($_facrec->lines[$i]->date_end_fill == 1 && $previousdaynextdatewhen) ? $previousdaynextdatewhen :
'',
1078 $_facrec->lines[$i]->product_type,
1079 $_facrec->lines[$i]->rang,
1080 $_facrec->lines[$i]->special_code,
1084 $fk_product_fournisseur_price,
1086 $_facrec->lines[$i]->label,
1087 empty($_facrec->lines[$i]->array_options) ?
null:$_facrec->lines[$i]->array_options,
1090 $_facrec->lines[$i]->fk_unit,
1091 $_facrec->lines[$i]->multicurrency_subprice,
1092 $_facrec->lines[$i]->ref_ext,
1096 if ($result_insert < 0) {
1098 $this->error = $this->db->error();
1117 if (!$error && !$notrigger) {
1127 $this->db->commit();
1130 $this->db->rollback();
1134 $this->error = $langs->trans(
'FailedToUpdatePrice');
1135 $this->db->rollback();
1139 dol_syslog(get_class($this).
"::create error ".$this->error, LOG_ERR);
1140 $this->db->rollback();
1144 $this->error = $this->db->error();
1145 $this->db->rollback();
1395 global $conf, $hookmanager;
1403 $num = count($object->lines);
1404 for ($i = 0; $i < $num; $i++) {
1407 $line->libelle = $object->lines[$i]->libelle;
1408 $line->label = $object->lines[$i]->label;
1409 $line->desc = $object->lines[$i]->desc;
1410 $line->subprice = $object->lines[$i]->subprice;
1411 $line->total_ht = $object->lines[$i]->total_ht;
1412 $line->total_tva = $object->lines[$i]->total_tva;
1413 $line->total_localtax1 = $object->lines[$i]->total_localtax1;
1414 $line->total_localtax2 = $object->lines[$i]->total_localtax2;
1415 $line->total_ttc = $object->lines[$i]->total_ttc;
1416 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1417 $line->tva_tx = $object->lines[$i]->tva_tx;
1418 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1419 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1420 $line->qty = $object->lines[$i]->qty;
1421 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1422 $line->remise_percent = $object->lines[$i]->remise_percent;
1423 $line->fk_product = $object->lines[$i]->fk_product;
1424 $line->info_bits = $object->lines[$i]->info_bits;
1425 $line->product_type = $object->lines[$i]->product_type;
1426 $line->rang = $object->lines[$i]->rang;
1427 $line->special_code = $object->lines[$i]->special_code;
1428 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1429 $line->fk_unit = $object->lines[$i]->fk_unit;
1430 $line->date_start = $object->lines[$i]->date_start;
1431 $line->date_end = $object->lines[$i]->date_end;
1434 $line->fk_multicurrency = $object->lines[$i]->fk_multicurrency;
1435 $line->multicurrency_code = $object->lines[$i]->multicurrency_code;
1436 $line->multicurrency_subprice = $object->lines[$i]->multicurrency_subprice;
1437 $line->multicurrency_total_ht = $object->lines[$i]->multicurrency_total_ht;
1438 $line->multicurrency_total_tva = $object->lines[$i]->multicurrency_total_tva;
1439 $line->multicurrency_total_ttc = $object->lines[$i]->multicurrency_total_ttc;
1441 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1442 $marginInfos =
getMarginInfos($object->lines[$i]->subprice, $object->lines[$i]->remise_percent, $object->lines[$i]->tva_tx, $object->lines[$i]->localtax1_tx, $object->lines[$i]->localtax2_tx, $object->lines[$i]->fk_fournprice, $object->lines[$i]->pa_ht);
1443 $line->pa_ht = $marginInfos[0];
1446 $object->lines[$i]->fetch_optionals();
1447 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1448 $line->array_options[$options_key] = $value;
1451 $this->lines[$i] = $line;
1454 $this->socid = $object->socid;
1455 $this->fk_project = $object->fk_project;
1456 $this->fk_account = $object->fk_account;
1457 $this->cond_reglement_id = $object->cond_reglement_id;
1458 $this->mode_reglement_id = $object->mode_reglement_id;
1459 $this->availability_id = $object->availability_id;
1460 $this->demand_reason_id = $object->demand_reason_id;
1461 $this->delivery_date = (empty($object->delivery_date) ? $object->date_livraison : $object->delivery_date);
1462 $this->date_livraison = $object->delivery_date;
1463 $this->fk_delivery_address = $object->fk_delivery_address;
1464 $this->contact_id = $object->contact_id;
1465 $this->ref_client = $object->ref_client;
1467 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1468 $this->note_private = $object->note_private;
1469 $this->note_public = $object->note_public;
1472 $this->module_source = $object->module_source;
1473 $this->pos_source = $object->pos_source;
1475 $this->origin = $object->element;
1476 $this->origin_id = $object->id;
1478 $this->fk_user_author = $user->id;
1481 $object->fetch_optionals();
1482 foreach ($object->array_options as $options_key => $value) {
1483 $this->array_options[$options_key] = $value;
1487 $this->linked_objects[$this->origin] = $this->origin_id;
1488 if (!empty($object->other_linked_objects) && is_array($object->other_linked_objects)) {
1489 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1492 $ret = $this->
create($user);
1496 $hookmanager->initHooks(array(
'invoicedao'));
1498 $parameters = array(
'objFrom'=>$object);
1500 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1526 global $conf, $hookmanager;
1534 $use_all_lines = empty($lines);
1535 $num = count($object->lines);
1536 for ($i = 0; $i < $num; $i++) {
1537 if (!$use_all_lines && !in_array($object->lines[$i]->id, $lines)) {
1543 $line->libelle = $object->lines[$i]->libelle;
1544 $line->label = $object->lines[$i]->label;
1545 $line->desc = $object->lines[$i]->desc;
1546 $line->subprice = $object->lines[$i]->subprice;
1547 $line->total_ht = $object->lines[$i]->total_ht;
1548 $line->total_tva = $object->lines[$i]->total_tva;
1549 $line->total_localtax1 = $object->lines[$i]->total_localtax1;
1550 $line->total_localtax2 = $object->lines[$i]->total_localtax2;
1551 $line->total_ttc = $object->lines[$i]->total_ttc;
1552 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1553 $line->tva_tx = $object->lines[$i]->tva_tx;
1554 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1555 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1556 $line->qty = $object->lines[$i]->qty;
1557 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1558 $line->remise_percent = $object->lines[$i]->remise_percent;
1559 $line->fk_product = $object->lines[$i]->fk_product;
1560 $line->info_bits = $object->lines[$i]->info_bits;
1561 $line->product_type = $object->lines[$i]->product_type;
1562 $line->rang = $object->lines[$i]->rang;
1563 $line->special_code = $object->lines[$i]->special_code;
1564 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1565 $line->fk_unit = $object->lines[$i]->fk_unit;
1566 $line->date_start = $object->lines[$i]->date_start;
1567 $line->date_end = $object->lines[$i]->date_end;
1570 $line->fk_multicurrency = $object->lines[$i]->fk_multicurrency;
1571 $line->multicurrency_code = $object->lines[$i]->multicurrency_code;
1572 $line->multicurrency_subprice = $object->lines[$i]->multicurrency_subprice;
1573 $line->multicurrency_total_ht = $object->lines[$i]->multicurrency_total_ht;
1574 $line->multicurrency_total_tva = $object->lines[$i]->multicurrency_total_tva;
1575 $line->multicurrency_total_ttc = $object->lines[$i]->multicurrency_total_ttc;
1577 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1578 $marginInfos =
getMarginInfos($object->lines[$i]->subprice, $object->lines[$i]->remise_percent, $object->lines[$i]->tva_tx, $object->lines[$i]->localtax1_tx, $object->lines[$i]->localtax2_tx, $object->lines[$i]->fk_fournprice, $object->lines[$i]->pa_ht);
1579 $line->pa_ht = $marginInfos[0];
1582 $object->lines[$i]->fetch_optionals();
1583 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1584 $line->array_options[$options_key] = $value;
1587 $this->lines[$i] = $line;
1590 $this->socid = $object->socid;
1591 $this->fk_project = $object->fk_project;
1592 $this->fk_account = $object->fk_account;
1593 $this->cond_reglement_id = $object->cond_reglement_id;
1594 $this->mode_reglement_id = $object->mode_reglement_id;
1595 $this->availability_id = $object->availability_id;
1596 $this->demand_reason_id = $object->demand_reason_id;
1597 $this->delivery_date = (empty($object->delivery_date) ? $object->date_livraison : $object->delivery_date);
1598 $this->date_livraison = $object->delivery_date;
1599 $this->fk_delivery_address = $object->fk_delivery_address;
1600 $this->contact_id = $object->contact_id;
1601 $this->ref_client = $object->ref_client;
1603 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1604 $this->note_private = $object->note_private;
1605 $this->note_public = $object->note_public;
1608 $this->module_source = $object->module_source;
1609 $this->pos_source = $object->pos_source;
1611 $this->origin = $object->element;
1612 $this->origin_id = $object->id;
1614 $this->fk_user_author = $user->id;
1617 $object->fetch_optionals();
1618 foreach ($object->array_options as $options_key => $value) {
1619 $this->array_options[$options_key] = $value;
1623 $this->linked_objects[$this->origin] = $this->origin_id;
1624 if (!empty($object->other_linked_objects) && is_array($object->other_linked_objects)) {
1625 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1628 $ret = $this->
create($user);
1632 $hookmanager->initHooks(array(
'invoicedao'));
1634 $parameters = array(
'objFrom'=>$object);
1636 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1666 global $conf, $langs, $hookmanager, $action;
1668 if (! in_array($origin->element, array(
'propal',
'commande'))) {
1669 $origin->error =
'ErrorCanOnlyAutomaticallyGenerateADepositFromProposalOrOrder';
1674 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'DateInvoice'));
1678 require_once DOL_DOCUMENT_ROOT .
'/core/lib/date.lib.php';
1680 if ($date > (
dol_get_last_hour(
dol_now(
'tzuserrel')) + (empty($conf->global->INVOICE_MAX_FUTURE_DELAY) ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
1681 $origin->error =
'ErrorDateIsInFuture';
1685 if ($payment_terms_id <= 0) {
1686 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'PaymentConditionsShort'));
1690 $payment_conditions_deposit_percent =
getDictionaryValue(
'c_payment_term',
'deposit_percent', $origin->cond_reglement_id);
1692 if (empty($payment_conditions_deposit_percent)) {
1693 $origin->error =
'ErrorPaymentConditionsNotEligibleToDepositCreation';
1697 if (empty($origin->deposit_percent)) {
1698 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'DepositPercent'));
1702 $deposit =
new self($origin->db);
1703 $deposit->socid = $origin->socid;
1705 $deposit->fk_project = $origin->fk_project;
1706 $deposit->ref_client = $origin->ref_client;
1707 $deposit->date = $date;
1708 $deposit->mode_reglement_id = $origin->mode_reglement_id;
1709 $deposit->cond_reglement_id = $payment_terms_id;
1710 $deposit->availability_id = $origin->availability_id;
1711 $deposit->demand_reason_id = $origin->demand_reason_id;
1712 $deposit->fk_account = $origin->fk_account;
1713 $deposit->fk_incoterms = $origin->fk_incoterms;
1714 $deposit->location_incoterms = $origin->location_incoterms;
1715 $deposit->fk_multicurrency = $origin->fk_multicurrency;
1716 $deposit->multicurrency_code = $origin->multicurrency_code;
1717 $deposit->multicurrency_tx = $origin->multicurrency_tx;
1718 $deposit->module_source = $origin->module_source;
1719 $deposit->pos_source = $origin->pos_source;
1720 $deposit->model_pdf =
'crabe';
1722 $modelByTypeConfName =
'FACTURE_ADDON_PDF_' . $deposit->type;
1724 if (!empty($conf->global->$modelByTypeConfName)) {
1725 $deposit->model_pdf = $conf->global->$modelByTypeConfName;
1726 } elseif (!empty($conf->global->FACTURE_ADDON_PDF)) {
1727 $deposit->model_pdf = $conf->global->FACTURE_ADDON_PDF;
1730 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1731 $deposit->note_private = $origin->note_private;
1732 $deposit->note_public = $origin->note_public;
1735 $deposit->origin = $origin->element;
1736 $deposit->origin_id = $origin->id;
1738 $origin->fetch_optionals();
1740 foreach ($origin->array_options as $extrakey => $value) {
1741 $deposit->array_options[$extrakey] = $value;
1744 $deposit->linked_objects[$deposit->origin] = $deposit->origin_id;
1746 foreach ($overrideFields as $key => $value) {
1747 $deposit->$key = $value;
1750 $deposit->context[
'createdepositfromorigin'] =
'createdepositfromorigin';
1752 $origin->db->begin();
1755 $createReturn = $deposit->create($user, $notrigger);
1757 if ($createReturn <= 0) {
1758 $origin->db->rollback();
1759 $origin->error = $deposit->error;
1760 $origin->errors = $deposit->errors;
1764 $amount_ttc_diff = 0;
1765 $amountdeposit = array();
1766 $descriptions = array();
1768 if (!empty($conf->global->MAIN_DEPOSIT_MULTI_TVA)) {
1769 $amount = $origin->total_ttc * ($origin->deposit_percent / 100);
1771 $TTotalByTva = array();
1772 foreach ($origin->lines as &$line) {
1773 if (!empty($line->special_code)) {
1776 $TTotalByTva[$line->tva_tx] += $line->total_ttc;
1777 $descriptions[$line->tva_tx] .=
'<li>' . (!empty($line->product_ref) ? $line->product_ref .
' - ' :
'');
1778 $descriptions[$line->tva_tx] .= (!empty($line->product_label) ? $line->product_label .
' - ' :
'');
1779 $descriptions[$line->tva_tx] .= $langs->trans(
'Qty') .
' : ' . $line->qty;
1780 $descriptions[$line->tva_tx] .=
' - ' . $langs->trans(
'TotalHT') .
' : ' .
price($line->total_ht) .
'</li>';
1783 foreach ($TTotalByTva as $tva => &$total) {
1784 $coef = $total / $origin->total_ttc;
1785 $am = $amount * $coef;
1786 $amount_ttc_diff += $am;
1787 $amountdeposit[$tva] += $am / (1 + $tva / 100);
1791 $lines = $origin->lines;
1792 $numlines = count($lines);
1793 for ($i = 0; $i < $numlines; $i++) {
1794 if (empty($lines[$i]->qty)) {
1797 if (!empty($lines[$i]->special_code)) {
1801 $totalamount += $lines[$i]->total_ht;
1802 $tva_tx = $lines[$i]->tva_tx;
1803 $amountdeposit[$tva_tx] += ($lines[$i]->total_ht * $origin->deposit_percent) / 100;
1804 $descriptions[$tva_tx] .=
'<li>' . (!empty($lines[$i]->product_ref) ? $lines[$i]->product_ref .
' - ' :
'');
1805 $descriptions[$tva_tx] .= (!empty($lines[$i]->product_label) ? $lines[$i]->product_label .
' - ' :
'');
1806 $descriptions[$tva_tx] .= $langs->trans(
'Qty') .
' : ' . $lines[$i]->qty;
1807 $descriptions[$tva_tx] .=
' - ' . $langs->trans(
'TotalHT') .
' : ' .
price($lines[$i]->total_ht) .
'</li>';
1810 if ($totalamount == 0) {
1811 $amountdeposit[0] = 0;
1814 $amount_ttc_diff = $amountdeposit[0];
1817 foreach ($amountdeposit as $tva => $amount) {
1818 if (empty($amount)) {
1822 $descline =
'(DEPOSIT) ('. $origin->deposit_percent .
'%) - '.$origin->ref;
1825 if (!empty($conf->global->INVOICE_DEPOSIT_VARIABLE_MODE_DETAIL_LINES_IN_DESCRIPTION) && !empty($descriptions[$tva])) {
1826 $descline .=
'<ul>' . $descriptions[$tva] .
'</ul>';
1829 $addlineResult = $deposit->addline(
1836 (empty($conf->global->INVOICE_PRODUCTID_DEPOSIT) ? 0 : $conf->global->INVOICE_PRODUCTID_DEPOSIT),
1856 if ($addlineResult < 0) {
1857 $origin->db->rollback();
1858 $origin->error = $deposit->error;
1859 $origin->errors = $deposit->errors;
1864 $diff = $deposit->total_ttc - $amount_ttc_diff;
1866 if (!empty($conf->global->MAIN_DEPOSIT_MULTI_TVA) && $diff != 0) {
1867 $deposit->fetch_lines();
1868 $subprice_diff = $deposit->lines[0]->subprice - $diff / (1 + $deposit->lines[0]->tva_tx / 100);
1870 $updatelineResult = $deposit->updateline(
1871 $deposit->lines[0]->id,
1872 $deposit->lines[0]->desc,
1874 $deposit->lines[0]->qty,
1875 $deposit->lines[0]->remise_percent,
1876 $deposit->lines[0]->date_start,
1877 $deposit->lines[0]->date_end,
1878 $deposit->lines[0]->tva_tx,
1882 $deposit->lines[0]->info_bits,
1883 $deposit->lines[0]->product_type,
1887 $deposit->lines[0]->pa_ht,
1888 $deposit->lines[0]->label,
1894 if ($updatelineResult < 0) {
1895 $origin->db->rollback();
1896 $origin->error = $deposit->error;
1897 $origin->errors = $deposit->errors;
1902 $hookmanager->initHooks(array(
'invoicedao'));
1904 $parameters = array(
'objFrom' => $origin);
1905 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $deposit, $action);
1908 $origin->db->rollback();
1909 $origin->error = $hookmanager->error;
1910 $origin->errors = $hookmanager->errors;
1914 if (!empty($autoValidateDeposit)) {
1915 $validateReturn = $deposit->validate($user,
'', 0, $notrigger);
1917 if ($validateReturn < 0) {
1918 $origin->db->rollback();
1919 $origin->error = $deposit->error;
1920 $origin->errors = $deposit->errors;
1925 unset($deposit->context[
'createdepositfromorigin']);
1927 $origin->db->commit();
2148 public function fetch($rowid, $ref =
'', $ref_ext =
'', $notused =
'', $fetch_situation =
false)
2150 if (empty($rowid) && empty($ref) && empty($ref_ext)) {
2154 $sql =
'SELECT f.rowid, f.entity, f.ref, f.ref_client, f.ref_ext, f.type, f.fk_soc';
2155 $sql .=
', f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc, f.revenuestamp';
2156 $sql .=
', f.remise_percent, f.remise_absolue, f.remise';
2157 $sql .=
', f.datef as df, f.date_pointoftax';
2158 $sql .=
', f.date_lim_reglement as dlr';
2159 $sql .=
', f.datec as datec';
2160 $sql .=
', f.date_valid as datev';
2161 $sql .=
', f.tms as datem';
2162 $sql .=
', f.note_private, f.note_public, f.fk_statut, 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';
2163 $sql .=
', f.fk_facture_source, f.fk_fac_rec_source';
2164 $sql .=
', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project, f.extraparams';
2165 $sql .=
', f.situation_cycle_ref, f.situation_counter, f.situation_final';
2166 $sql .=
', f.fk_account';
2167 $sql .=
", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
2168 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
2169 $sql .=
', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
2170 $sql .=
', f.fk_incoterms, f.location_incoterms';
2171 $sql .=
', f.module_source, f.pos_source';
2172 $sql .=
", i.libelle as label_incoterms";
2173 $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";
2174 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
2175 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
2176 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON f.fk_mode_reglement = p.id';
2177 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON f.fk_incoterms = i.rowid';
2180 $sql .=
" WHERE f.rowid = ".((int) $rowid);
2182 $sql .=
' WHERE f.entity IN ('.getEntity(
'invoice').
')';
2184 $sql .=
" AND f.ref = '".$this->db->escape($ref).
"'";
2187 $sql .=
" AND f.ref_ext = '".$this->db->escape($ref_ext).
"'";
2191 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
2192 $resql = $this->db->query($sql);
2194 if ($this->db->num_rows($resql)) {
2195 $obj = $this->db->fetch_object($resql);
2197 $this->
id = $obj->rowid;
2198 $this->entity = $obj->entity;
2200 $this->
ref = $obj->ref;
2201 $this->ref_client = $obj->ref_client;
2202 $this->ref_customer = $obj->ref_client;
2203 $this->ref_ext = $obj->ref_ext;
2204 $this->
type = $obj->type;
2205 $this->date = $this->db->jdate($obj->df);
2206 $this->date_pointoftax = $this->db->jdate($obj->date_pointoftax);
2207 $this->date_creation = $this->db->jdate($obj->datec);
2208 $this->date_validation = $this->db->jdate($obj->datev);
2209 $this->date_modification = $this->db->jdate($obj->datem);
2210 $this->datem = $this->db->jdate($obj->datem);
2211 $this->remise_percent = $obj->remise_percent;
2212 $this->remise_absolue = $obj->remise_absolue;
2213 $this->total_ht = $obj->total_ht;
2214 $this->total_tva = $obj->total_tva;
2215 $this->total_localtax1 = $obj->localtax1;
2216 $this->total_localtax2 = $obj->localtax2;
2217 $this->total_ttc = $obj->total_ttc;
2218 $this->revenuestamp = $obj->revenuestamp;
2219 $this->paye = $obj->paye;
2220 $this->close_code = $obj->close_code;
2221 $this->close_note = $obj->close_note;
2223 $this->socid = $obj->fk_soc;
2224 $this->thirdparty =
null;
2226 $this->fk_project = $obj->fk_project;
2227 $this->project =
null;
2229 $this->
statut = $obj->fk_statut;
2230 $this->status = $obj->fk_statut;
2232 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
2233 $this->mode_reglement_id = $obj->fk_mode_reglement;
2234 $this->mode_reglement_code = $obj->mode_reglement_code;
2235 $this->mode_reglement = $obj->mode_reglement_libelle;
2236 $this->cond_reglement_id = $obj->fk_cond_reglement;
2237 $this->cond_reglement_code = $obj->cond_reglement_code;
2238 $this->cond_reglement = $obj->cond_reglement_libelle;
2239 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
2240 $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account :
null;
2241 $this->fk_facture_source = $obj->fk_facture_source;
2242 $this->fk_fac_rec_source = $obj->fk_fac_rec_source;
2243 $this->note = $obj->note_private;
2244 $this->note_private = $obj->note_private;
2245 $this->note_public = $obj->note_public;
2246 $this->user_author = $obj->fk_user_author;
2247 $this->user_valid = $obj->fk_user_valid;
2248 $this->user_modification = $obj->fk_user_modif;
2249 $this->fk_user_author = $obj->fk_user_author;
2250 $this->fk_user_valid = $obj->fk_user_valid;
2251 $this->fk_user_modif = $obj->fk_user_modif;
2252 $this->model_pdf = $obj->model_pdf;
2253 $this->modelpdf = $obj->model_pdf;
2254 $this->last_main_doc = $obj->last_main_doc;
2255 $this->situation_cycle_ref = $obj->situation_cycle_ref;
2256 $this->situation_counter = $obj->situation_counter;
2257 $this->situation_final = $obj->situation_final;
2258 $this->retained_warranty = $obj->retained_warranty;
2259 $this->retained_warranty_date_limit = $this->db->jdate($obj->retained_warranty_date_limit);
2260 $this->retained_warranty_fk_cond_reglement = $obj->retained_warranty_fk_cond_reglement;
2262 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
2265 $this->fk_incoterms = $obj->fk_incoterms;
2266 $this->location_incoterms = $obj->location_incoterms;
2267 $this->label_incoterms = $obj->label_incoterms;
2269 $this->module_source = $obj->module_source;
2270 $this->pos_source = $obj->pos_source;
2273 $this->fk_multicurrency = $obj->fk_multicurrency;
2274 $this->multicurrency_code = $obj->multicurrency_code;
2275 $this->multicurrency_tx = $obj->multicurrency_tx;
2276 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
2277 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
2278 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
2280 if (($this->
type == self::TYPE_SITUATION || ($this->
type == self::TYPE_CREDIT_NOTE && $this->situation_cycle_ref > 0)) && $fetch_situation) {
2284 if ($this->status == self::STATUS_DRAFT) {
2285 $this->brouillon = 1;
2293 $this->lines = array();
2297 $this->error = $this->db->error();
2301 $this->db->free($resql);
2305 $this->error =
'Invoice with id='.$rowid.
' or ref='.$ref.
' or ref_ext='.$ref_ext.
' not found';
2307 dol_syslog(__METHOD__.$this->error, LOG_WARNING);
2311 $this->error = $this->db->lasterror();
2326 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2329 global $langs, $conf;
2331 $this->lines = array();
2333 $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,';
2334 $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,';
2335 $sql .=
' l.situation_percent, l.fk_prev_id,';
2336 $sql .=
' l.rang, l.special_code,';
2337 $sql .=
' l.date_start as date_start, l.date_end as date_end,';
2338 $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,';
2339 $sql .=
' l.fk_unit,';
2340 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2341 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2342 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet as l';
2343 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
2344 $sql .=
' WHERE l.fk_facture = '.((int) $this->
id);
2345 $sql .=
' ORDER BY l.rang, l.rowid';
2347 dol_syslog(get_class($this).
'::fetch_lines', LOG_DEBUG);
2348 $result = $this->db->query($sql);
2350 $num = $this->db->num_rows($result);
2353 $objp = $this->db->fetch_object($result);
2356 $line->id = $objp->rowid;
2357 $line->rowid = $objp->rowid;
2358 $line->fk_facture = $objp->fk_facture;
2359 $line->label = $objp->custom_label;
2360 $line->desc = $objp->description;
2361 $line->description = $objp->description;
2362 $line->product_type = $objp->product_type;
2363 $line->ref = $objp->product_ref;
2364 $line->product_ref = $objp->product_ref;
2365 $line->libelle = $objp->product_label;
2366 $line->product_label = $objp->product_label;
2367 $line->product_desc = $objp->product_desc;
2368 $line->fk_product_type = $objp->fk_product_type;
2369 $line->qty = $objp->qty;
2370 $line->subprice = $objp->subprice;
2371 $line->ref_ext = $objp->ref_ext;
2373 $line->vat_src_code = $objp->vat_src_code;
2374 $line->tva_tx = $objp->tva_tx;
2375 $line->localtax1_tx = $objp->localtax1_tx;
2376 $line->localtax2_tx = $objp->localtax2_tx;
2377 $line->localtax1_type = $objp->localtax1_type;
2378 $line->localtax2_type = $objp->localtax2_type;
2379 $line->remise_percent = $objp->remise_percent;
2380 $line->fk_remise_except = $objp->fk_remise_except;
2381 $line->fk_product = $objp->fk_product;
2382 $line->date_start = $this->db->jdate($objp->date_start);
2383 $line->date_end = $this->db->jdate($objp->date_end);
2384 $line->info_bits = $objp->info_bits;
2385 $line->total_ht = $objp->total_ht;
2386 $line->total_tva = $objp->total_tva;
2387 $line->total_localtax1 = $objp->total_localtax1;
2388 $line->total_localtax2 = $objp->total_localtax2;
2389 $line->total_ttc = $objp->total_ttc;
2390 $line->code_ventilation = $objp->fk_code_ventilation;
2391 $line->fk_fournprice = $objp->fk_fournprice;
2392 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2393 $line->pa_ht = $marginInfos[0];
2394 $line->marge_tx = $marginInfos[1];
2395 $line->marque_tx = $marginInfos[2];
2396 $line->rang = $objp->rang;
2397 $line->special_code = $objp->special_code;
2398 $line->fk_parent_line = $objp->fk_parent_line;
2399 $line->situation_percent = $objp->situation_percent;
2400 $line->fk_prev_id = $objp->fk_prev_id;
2401 $line->fk_unit = $objp->fk_unit;
2404 $line->fk_accounting_account = $objp->fk_code_ventilation;
2407 $line->fk_multicurrency = $objp->fk_multicurrency;
2408 $line->multicurrency_code = $objp->multicurrency_code;
2409 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2410 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2411 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2412 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2414 $line->fetch_optionals();
2417 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2418 $tmpproduct =
new Product($this->db);
2419 $tmpproduct->fetch($objp->fk_product);
2420 $tmpproduct->getMultiLangs();
2422 $line->multilangs = $tmpproduct->multilangs;
2425 $this->lines[$i] = $line;
2429 $this->db->free($result);
2432 $this->error = $this->db->error();
2486 if (empty($this->
type)) {
2489 if (isset($this->
ref)) {
2490 $this->
ref = trim($this->
ref);
2492 if (isset($this->ref_ext)) {
2493 $this->ref_ext = trim($this->ref_ext);
2495 if (isset($this->ref_client)) {
2496 $this->ref_client = trim($this->ref_client);
2498 if (isset($this->increment)) {
2499 $this->increment = trim($this->increment);
2501 if (isset($this->close_code)) {
2502 $this->close_code = trim($this->close_code);
2504 if (isset($this->close_note)) {
2505 $this->close_note = trim($this->close_note);
2507 if (isset($this->note) || isset($this->note_private)) {
2508 $this->note = (isset($this->note) ? trim($this->note) : trim($this->note_private));
2510 if (isset($this->note) || isset($this->note_private)) {
2511 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
2513 if (isset($this->note_public)) {
2514 $this->note_public = trim($this->note_public);
2516 if (isset($this->model_pdf)) {
2517 $this->model_pdf = trim($this->model_pdf);
2519 if (isset($this->import_key)) {
2520 $this->import_key = trim($this->import_key);
2522 if (isset($this->retained_warranty)) {
2523 $this->retained_warranty = floatval($this->retained_warranty);
2531 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture SET";
2532 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
2533 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
2534 $sql .=
" type=".(isset($this->
type) ? $this->db->escape($this->
type) :
"null").
",";
2535 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
2536 $sql .=
" increment=".(isset($this->increment) ?
"'".$this->db->escape($this->increment).
"'" :
"null").
",";
2537 $sql .=
" fk_soc=".(isset($this->socid) ? $this->db->escape($this->socid) :
"null").
",";
2538 $sql .=
" datec=".(strval($this->date_creation) !=
'' ?
"'".$this->db->idate($this->date_creation).
"'" :
'null').
",";
2539 $sql .=
" datef=".(strval($this->date) !=
'' ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
2540 $sql .=
" date_pointoftax=".(strval($this->date_pointoftax) !=
'' ?
"'".$this->db->idate($this->date_pointoftax).
"'" :
'null').
",";
2541 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
2542 $sql .=
" paye=".(isset($this->paye) ? $this->db->escape($this->paye) : 0).
",";
2543 $sql .=
" remise_percent=".(isset($this->remise_percent) ? $this->db->escape($this->remise_percent) :
"null").
",";
2544 $sql .=
" remise_absolue=".(isset($this->remise_absolue) ? $this->db->escape($this->remise_absolue) :
"null").
",";
2545 $sql .=
" close_code=".(isset($this->close_code) ?
"'".$this->db->escape($this->close_code).
"'" :
"null").
",";
2546 $sql .=
" close_note=".(isset($this->close_note) ?
"'".$this->db->escape($this->close_note).
"'" :
"null").
",";
2547 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
2548 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
2549 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
2550 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
2551 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
2552 $sql .=
" revenuestamp=".((isset($this->revenuestamp) && $this->revenuestamp !=
'') ? $this->db->escape($this->revenuestamp) :
"null").
",";
2553 $sql .=
" fk_statut=".(isset($this->
statut) ? $this->db->escape($this->
statut) :
"null").
",";
2554 $sql .=
" fk_user_author=".(isset($this->user_author) ? $this->db->escape($this->user_author) :
"null").
",";
2555 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? $this->db->escape($this->fk_user_valid) :
"null").
",";
2556 $sql .=
" fk_facture_source=".(isset($this->fk_facture_source) ? $this->db->escape($this->fk_facture_source) :
"null").
",";
2557 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->db->escape($this->fk_project) :
"null").
",";
2558 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->db->escape($this->cond_reglement_id) :
"null").
",";
2559 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->db->escape($this->mode_reglement_id) :
"null").
",";
2560 $sql .=
" date_lim_reglement=".(strval($this->date_lim_reglement) !=
'' ?
"'".$this->db->idate($this->date_lim_reglement).
"'" :
'null').
",";
2561 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
2562 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
2563 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
2564 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
",";
2565 $sql .=
" situation_cycle_ref=".(empty($this->situation_cycle_ref) ?
"null" : $this->db->escape($this->situation_cycle_ref)).
",";
2566 $sql .=
" situation_counter=".(empty($this->situation_counter) ?
"null" : $this->db->escape($this->situation_counter)).
",";
2567 $sql .=
" situation_final=".(empty($this->situation_final) ?
"0" : $this->db->escape($this->situation_final)).
",";
2568 $sql .=
" retained_warranty=".(empty($this->retained_warranty) ?
"0" : $this->db->escape($this->retained_warranty)).
",";
2569 $sql .=
" retained_warranty_date_limit=".(strval($this->retained_warranty_date_limit) !=
'' ?
"'".$this->db->idate($this->retained_warranty_date_limit).
"'" :
'null').
",";
2570 $sql .=
" retained_warranty_fk_cond_reglement=".(isset($this->retained_warranty_fk_cond_reglement) ?intval($this->retained_warranty_fk_cond_reglement) :
"null");
2571 $sql .=
" WHERE rowid=".((int) $this->
id);
2575 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
2576 $resql = $this->db->query($sql);
2579 $this->errors[] =
"Error ".$this->db->lasterror();
2589 if (!$error && !$notrigger) {
2600 foreach ($this->errors as $errmsg) {
2601 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
2602 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2604 $this->db->rollback();
2607 $this->db->commit();
3191 public function validate($user, $force_number =
'', $idwarehouse = 0, $notrigger = 0, $batch_rule = 0)
3193 global $conf, $langs, $mysoc;
3194 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3196 $productStatic =
null;
3197 $warehouseStatic =
null;
3198 if ($batch_rule > 0) {
3199 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
3200 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
3201 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
3202 $productStatic =
new Product($this->db);
3203 $warehouseStatic =
new Entrepot($this->db);
3210 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number.
', idwarehouse='.$idwarehouse);
3217 if ($this->
statut != self::STATUS_DRAFT) {
3218 dol_syslog(get_class($this).
"::validate status is not draft. operation canceled.", LOG_WARNING);
3221 if (count($this->lines) <= 0) {
3222 $langs->load(
"errors");
3223 $this->error = $langs->trans(
"ErrorObjectMustHaveLinesToBeValidated", $this->
ref);
3226 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->facture->creer))
3227 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->facture->invoice_advance->validate))) {
3228 $this->error =
'Permission denied';
3229 dol_syslog(get_class($this).
"::validate ".$this->error.
' MAIN_USE_ADVANCED_PERMS='.$conf->global->MAIN_USE_ADVANCED_PERMS, LOG_ERR);
3232 if ((preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) &&
3233 !empty($conf->global->FAC_FORCE_DATE_VALIDATION)
3238 if (!empty($conf->global-> INVOICE_CHECK_POSTERIOR_DATE)) {
3240 if (!$last_of_type[0]) {
3241 $this->error = $langs->transnoentities(
"ErrorInvoiceIsNotLastOfSameType", $this->
ref,
dol_print_date($this->date,
'day'),
dol_print_date($last_of_type[1],
'day'));
3247 if (!empty($this->thirdparty) && is_object($this->thirdparty)) {
3248 $array_to_check = array(
'IDPROF1',
'IDPROF2',
'IDPROF3',
'IDPROF4',
'IDPROF5',
'IDPROF6',
'EMAIL',
'ACCOUNTANCY_CODE_CUSTOMER');
3249 foreach ($array_to_check as $key) {
3250 $keymin = strtolower($key);
3251 if (!property_exists($this->thirdparty, $keymin)) {
3254 $vallabel = $this->thirdparty->$keymin;
3256 $i = (int) preg_replace(
'/[^0-9]/',
'', $key);
3258 if ($this->thirdparty->isACompany()) {
3260 if ($mysoc->country_id > 0 && $this->thirdparty->country_id == $mysoc->country_id) {
3261 $idprof_mandatory =
'SOCIETE_'.$key.
'_INVOICE_MANDATORY';
3262 if (!$vallabel && !empty($conf->global->$idprof_mandatory)) {
3263 $langs->load(
"errors");
3264 $this->error = $langs->trans(
'ErrorProdIdIsMandatory', $langs->transcountry(
'ProfId'.$i, $this->thirdparty->country_code)).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
3265 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3271 if ($key ==
'EMAIL') {
3273 if (!empty($conf->global->SOCIETE_EMAIL_INVOICE_MANDATORY) && !isValidEMail($this->thirdparty->email)) {
3274 $langs->load(
"errors");
3275 $this->error = $langs->trans(
"ErrorBadEMail", $this->thirdparty->email).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
3276 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3280 if ($key ==
'ACCOUNTANCY_CODE_CUSTOMER') {
3282 if (!empty($conf->global->SOCIETE_ACCOUNTANCY_CODE_CUSTOMER_INVOICE_MANDATORY) && empty($this->thirdparty->code_compta)) {
3283 $langs->load(
"errors");
3284 $this->error = $langs->trans(
"ErrorAccountancyCodeCustomerIsMandatory", $this->thirdparty->name).
' ('.$langs->trans(
"ForbiddenBySetupRules").
')';
3285 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3294 $array_to_check = array(
'REF_CLIENT'=>
'RefCustomer');
3295 foreach ($array_to_check as $key => $val) {
3296 $keymin = strtolower($key);
3297 $vallabel = $this->$keymin;
3300 $keymandatory =
'INVOICE_'.$key.
'_MANDATORY_FOR_VALIDATION';
3301 if (!$vallabel && !empty($conf->global->$keymandatory)) {
3302 $langs->load(
"errors");
3304 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv($val)),
null,
'errors');
3311 if ($this->
type == self::TYPE_REPLACEMENT) {
3313 if ($this->fk_facture_source <= 0) {
3314 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"InvoiceReplacement"));
3315 $this->db->rollback();
3320 $facreplaced =
new Facture($this->db);
3321 $result = $facreplaced->fetch($this->fk_facture_source);
3323 $this->error = $langs->trans(
"ErrorBadInvoice");
3324 $this->db->rollback();
3329 $idreplacement = $facreplaced->getIdReplacingInvoice(
'validated');
3330 if ($idreplacement && $idreplacement != $this->
id) {
3331 $facreplacement =
new Facture($this->db);
3332 $facreplacement->fetch($idreplacement);
3333 $this->error = $langs->trans(
"ErrorInvoiceAlreadyReplaced", $facreplaced->ref, $facreplacement->ref);
3334 $this->db->rollback();
3338 $result = $facreplaced->setCanceled($user, self::CLOSECODE_REPLACED,
'');
3340 $this->error = $facreplaced->error;
3341 $this->db->rollback();
3347 if ($force_number) {
3348 $num = $force_number;
3349 } elseif (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
3361 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
3362 $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).
"'";
3363 if (!empty($conf->global->FAC_FORCE_DATE_VALIDATION)) {
3364 $sql .=
", datef='".$this->db->idate($this->date).
"'";
3365 $sql .=
", date_lim_reglement='".$this->db->idate($this->date_lim_reglement).
"'";
3367 $sql .=
" WHERE rowid = ".((int) $this->
id);
3369 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
3370 $resql = $this->db->query($sql);
3377 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref))) {
3383 $result = $this->thirdparty->set_as_client();
3386 if ($this->
type != self::TYPE_DEPOSIT && $result >= 0 && isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_BILL) && $idwarehouse > 0) {
3387 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
3388 $langs->load(
"agenda");
3391 $cpt = count($this->lines);
3392 for ($i = 0; $i < $cpt; $i++) {
3393 if ($this->lines[$i]->fk_product > 0) {
3395 $mouvP->origin = &$this;
3396 $mouvP->setOrigin($this->element, $this->
id);
3398 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3399 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
3402 $this->error = $mouvP->error;
3405 $is_batch_line =
false;
3406 if ($batch_rule > 0) {
3407 $productStatic->fetch($this->lines[$i]->fk_product);
3408 if ($productStatic->hasbatch()) {
3409 $is_batch_line =
true;
3410 $product_qty_remain = $this->lines[$i]->qty;
3416 $sortfield =
'pl.sellby,pl.eatby,pb.qty,pl.rowid';
3417 $sortorder =
'ASC,ASC,ASC,ASC';
3420 $resBatchList = $productbatch->findAllForProduct($productStatic->id, $idwarehouse, (
getDolGlobalInt(
'STOCK_ALLOW_NEGATIVE_TRANSFER') ?
null : 0), $sortfield, $sortorder);
3421 if (!is_array($resBatchList)) {
3423 $this->error = $this->db->lasterror();
3427 $batchList = $resBatchList;
3428 if (empty($batchList)) {
3430 $langs->load(
'errors');
3431 $warehouseStatic->fetch($idwarehouse);
3432 $this->error = $langs->trans(
'ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref);
3433 dol_syslog(__METHOD__.
' Error: '.$langs->transnoentitiesnoconv(
'ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR);
3436 foreach ($batchList as $batch) {
3437 if ($batch->qty <= 0) {
3442 if ($batch->qty >= $product_qty_remain) {
3443 $product_batch_qty = $product_qty_remain;
3446 $product_batch_qty = $batch->qty;
3448 $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_batch_qty, $this->lines[$i]->subprice, $langs->trans(
'InvoiceValidatedInDolibarr', $num),
'',
'',
'', $batch->batch);
3451 $this->error = $mouvP->error;
3452 $this->errors = $mouvP->errors;
3456 $product_qty_remain -= $product_batch_qty;
3458 if ($product_qty_remain <= 0) {
3463 if (!$error && $product_qty_remain > 0) {
3466 $batch = $batchList[0];
3467 $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_qty_remain, $this->lines[$i]->subprice, $langs->trans(
'InvoiceValidatedInDolibarr', $num),
'',
'',
'', $batch->batch);
3470 $this->error = $mouvP->error;
3471 $this->errors = $mouvP->errors;
3475 $langs->load(
'errors');
3476 $warehouseStatic->fetch($idwarehouse);
3477 $this->error = $langs->trans(
'ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref);
3478 dol_syslog(__METHOD__.
' Error: '.$langs->transnoentitiesnoconv(
'ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR);
3485 if (!$is_batch_line) {
3486 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
3489 $this->error = $mouvP->error;
3490 $this->errors = $mouvP->errors;
3503 if (!$error && $this->
type == self::TYPE_CREDIT_NOTE && $this->fk_facture_source > 0) {
3504 $invoice_situation =
new Facture($this->db);
3505 $result = $invoice_situation->fetch($this->fk_facture_source);
3506 if ($result > 0 && $invoice_situation->type == self::TYPE_SITUATION && $invoice_situation->situation_final == 1) {
3507 $invoice_situation->situation_final = 0;
3509 $result = $invoice_situation->setFinal($user, 1);
3512 $this->error = $invoice_situation->error;
3513 $this->errors = $invoice_situation->errors;
3519 if (!$error && !$notrigger) {
3529 $this->oldref = $this->ref;
3532 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
3534 $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).
"'";
3535 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'facture/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
3536 $resql = $this->db->query($sql);
3539 $this->error = $this->db->lasterror();
3541 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filepath = 'facture/".$this->db->escape($this->newref).
"'";
3542 $sql .=
" WHERE filepath = 'facture/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
3543 $resql = $this->db->query($sql);
3545 $error++; $this->error = $this->db->lasterror();
3551 $dirsource = $conf->facture->dir_output.
'/'.$oldref;
3552 $dirdest = $conf->facture->dir_output.
'/'.$newref;
3553 if (!$error && file_exists($dirsource)) {
3554 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
3556 if (@rename($dirsource, $dirdest)) {
3559 $listoffiles =
dol_dir_list($conf->facture->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
3560 foreach ($listoffiles as $fileentry) {
3561 $dirsource = $fileentry[
'name'];
3562 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
3563 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
3564 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
3565 @rename($dirsource, $dirdest);
3583 $this->brouillon = 0;
3584 $this->date_validation = $now;
3587 if (!empty($conf->global->INVOICE_USE_SITUATION)) {
3589 $nboflines = count($this->lines);
3590 while (($i < $nboflines) && $final) {
3591 $final = ($this->lines[$i]->situation_percent == 100);
3595 if (empty($final)) {
3596 $this->situation_final = 0;
3598 $this->situation_final = 1;
3609 $this->db->commit();
3612 $this->db->rollback();
3816 $fk_remise_except =
'',
3817 $price_base_type =
'HT',
3824 $fk_parent_line = 0,
3825 $fk_fournprice =
null,
3829 $situation_percent = 100,
3834 $noupdateafterinsertline = 0
3838 dol_syslog(__METHOD__.
": using line label is deprecated", LOG_WARNING);
3842 global $mysoc, $conf, $langs;
3844 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, ventil=$ventil, 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);
3846 if ($this->
statut == self::STATUS_DRAFT) {
3847 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3856 if (empty($info_bits)) {
3862 if (empty($ventil)) {
3865 if (empty($txtva)) {
3868 if (empty($txlocaltax1)) {
3871 if (empty($txlocaltax2)) {
3874 if (empty($fk_parent_line) || $fk_parent_line < 0) {
3875 $fk_parent_line = 0;
3877 if (empty($fk_prev_id)) {
3878 $fk_prev_id =
'null';
3880 if (!isset($situation_percent) || $situation_percent > 100 || (
string) $situation_percent ==
'') {
3881 $situation_percent = 100;
3883 if (empty($ref_ext)) {
3890 $pu_ht_devise =
price2num($pu_ht_devise);
3893 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3899 if ($price_base_type ==
'HT') {
3910 if ($date_start && $date_end && $date_start > $date_end) {
3911 $langs->load(
"errors");
3912 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3918 $product_type = $type;
3919 if (!empty($fk_product) && $fk_product > 0) {
3920 $product =
new Product($this->db);
3921 $result = $product->fetch($fk_product);
3922 $product_type = $product->type;
3924 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_INVOICE) && $product_type == 0 && $product->stock_reel < $qty) {
3925 $langs->load(
"errors");
3926 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnInvoice', $product->ref);
3927 $this->db->rollback();
3937 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3938 $vat_src_code = $reg[1];
3939 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3947 $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);
3949 $total_ht = $tabprice[0];
3950 $total_tva = $tabprice[1];
3951 $total_ttc = $tabprice[2];
3952 $total_localtax1 = $tabprice[9];
3953 $total_localtax2 = $tabprice[10];
3954 $pu_ht = $tabprice[3];
3957 $multicurrency_total_ht = $tabprice[16];
3958 $multicurrency_total_tva = $tabprice[17];
3959 $multicurrency_total_ttc = $tabprice[18];
3960 $pu_ht_devise = $tabprice[19];
3964 if ($ranktouse == -1) {
3965 $rangmax = $this->
line_max($fk_parent_line);
3966 $ranktouse = $rangmax + 1;
3972 $this->line->context = $this->context;
3974 $this->line->fk_facture = $this->id;
3975 $this->line->label = $label;
3976 $this->line->desc = $desc;
3977 $this->line->ref_ext = $ref_ext;
3979 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs($qty) : $qty);
3980 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
3982 $this->line->vat_src_code = $vat_src_code;
3983 $this->line->tva_tx = $txtva;
3984 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
3985 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
3986 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3987 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3989 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ht) : $total_ht);
3990 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ttc) : $total_ttc);
3991 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_tva) : $total_tva);
3992 $this->line->total_localtax1 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax1) : $total_localtax1);
3993 $this->line->total_localtax2 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax2) : $total_localtax2);
3995 $this->line->fk_product = $fk_product;
3996 $this->line->product_type = $product_type;
3998 $this->line->date_start = $date_start;
3999 $this->line->date_end = $date_end;
4000 $this->line->ventil = $ventil;
4001 $this->line->rang = $ranktouse;
4002 $this->line->info_bits = $info_bits;
4003 $this->line->fk_remise_except = $fk_remise_except;
4005 $this->line->special_code = $special_code;
4006 $this->line->fk_parent_line = $fk_parent_line;
4007 $this->line->origin = $origin;
4008 $this->line->origin_id = $origin_id;
4009 $this->line->situation_percent = $situation_percent;
4010 $this->line->fk_prev_id = $fk_prev_id;
4011 $this->line->fk_unit = $fk_unit;
4014 $this->line->fk_fournprice = $fk_fournprice;
4015 $this->line->pa_ht = $pa_ht;
4018 $this->line->fk_multicurrency = $this->fk_multicurrency;
4019 $this->line->multicurrency_code = $this->multicurrency_code;
4020 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht_devise) : $pu_ht_devise);
4022 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ht) : $multicurrency_total_ht);
4023 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_tva) : $multicurrency_total_tva);
4024 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
4026 if (is_array($array_options) && count($array_options) > 0) {
4027 $this->line->array_options = $array_options;
4030 $result = $this->line->insert();
4033 if (!empty($fk_parent_line)) {
4035 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
4036 $linecount = count($this->lines);
4037 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
4043 if (empty($noupdateafterinsertline)) {
4048 $this->db->commit();
4049 return $this->line->id;
4051 $this->error = $this->db->lasterror();
4052 $this->db->rollback();
4056 $this->error = $this->line->error;
4057 $this->errors = $this->line->errors;
4058 $this->db->rollback();
4062 $this->errors[]=
'status of invoice must be Draft to allow use of ->addline()';
4063 dol_syslog(get_class($this).
"::addline status of invoice must be Draft to allow use of ->addline()", LOG_ERR);
4099 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 = 0, $situation_percent = 100, $fk_unit =
null, $pu_ht_devise = 0, $notrigger = 0, $ref_ext =
'', $rang = 0)
4101 global $conf, $user;
4104 dol_syslog(__METHOD__.
": using line label is deprecated", LOG_WARNING);
4107 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
4109 global $mysoc, $langs;
4111 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);
4113 if ($this->
statut == self::STATUS_DRAFT) {
4116 if (!$this->error) {
4117 $this->error = $langs->trans(
'invoiceLineProgressError');
4123 if ($date_start && $date_end && $date_start > $date_end) {
4124 $langs->load(
"errors");
4125 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
4135 if (empty($fk_parent_line) || $fk_parent_line < 0) {
4136 $fk_parent_line = 0;
4138 if (empty($special_code) || $special_code == 3) {
4141 if (!isset($situation_percent) || $situation_percent > 100 || (
string) $situation_percent ==
'') {
4142 $situation_percent = 100;
4144 if (empty($ref_ext)) {
4151 $pu_ht_devise =
price2num($pu_ht_devise);
4153 if (!preg_match(
'/\((.*)\)/', $txtva)) {
4173 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
4174 $vat_src_code = $reg[1];
4175 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
4178 $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);
4180 $total_ht = $tabprice[0];
4181 $total_tva = $tabprice[1];
4182 $total_ttc = $tabprice[2];
4183 $total_localtax1 = $tabprice[9];
4184 $total_localtax2 = $tabprice[10];
4185 $pu_ht = $tabprice[3];
4186 $pu_tva = $tabprice[4];
4187 $pu_ttc = $tabprice[5];
4190 $multicurrency_total_ht = $tabprice[16];
4191 $multicurrency_total_tva = $tabprice[17];
4192 $multicurrency_total_ttc = $tabprice[18];
4193 $pu_ht_devise = $tabprice[19];
4200 $price = ($pu - $remise);
4206 $line->fetch($rowid);
4207 $line->fetch_optionals();
4209 if (!empty($line->fk_product)) {
4210 $product =
new Product($this->db);
4211 $result = $product->fetch($line->fk_product);
4212 $product_type = $product->type;
4214 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_INVOICE) && $product_type == 0 && $product->stock_reel < $qty) {
4215 $langs->load(
"errors");
4216 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnInvoice', $product->ref);
4217 $this->db->rollback();
4222 $staticline = clone $line;
4224 $line->oldline = $staticline;
4225 $this->line = $line;
4226 $this->line->context = $this->context;
4227 $this->line->rang = $rang;
4230 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
4231 $rangmax = $this->
line_max($fk_parent_line);
4232 $this->line->rang = $rangmax + 1;
4235 $this->line->id = $rowid;
4236 $this->line->rowid = $rowid;
4237 $this->line->label = $label;
4238 $this->line->desc = $desc;
4239 $this->line->ref_ext = $ref_ext;
4240 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ?abs($qty) : $qty);
4242 $this->line->vat_src_code = $vat_src_code;
4243 $this->line->tva_tx = $txtva;
4244 $this->line->localtax1_tx = $txlocaltax1;
4245 $this->line->localtax2_tx = $txlocaltax2;
4246 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
4247 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
4250 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ?-abs($pu_ht) : $pu_ht);
4251 $this->line->date_start = $date_start;
4252 $this->line->date_end = $date_end;
4253 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ht) : $total_ht);
4254 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_tva) : $total_tva);
4255 $this->line->total_localtax1 = $total_localtax1;
4256 $this->line->total_localtax2 = $total_localtax2;
4257 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ttc) : $total_ttc);
4258 $this->line->info_bits = $info_bits;
4259 $this->line->special_code = $special_code;
4260 $this->line->product_type = $type;
4261 $this->line->fk_parent_line = $fk_parent_line;
4262 $this->line->skip_update_total = $skip_update_total;
4263 $this->line->situation_percent = $situation_percent;
4264 $this->line->fk_unit = $fk_unit;
4266 $this->line->fk_fournprice = $fk_fournprice;
4267 $this->line->pa_ht = $pa_ht;
4270 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ?-abs($pu_ht_devise) : $pu_ht_devise);
4271 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_ht) : $multicurrency_total_ht);
4272 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_tva) : $multicurrency_total_tva);
4273 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
4275 if (is_array($array_options) && count($array_options) > 0) {
4277 foreach ($array_options as $key => $value) {
4278 $this->line->array_options[$key] = $array_options[$key];
4282 $result = $this->line->update($user, $notrigger);
4285 if (!empty($fk_parent_line)) {
4291 $this->db->commit();
4294 $this->error = $this->line->error;
4295 $this->db->rollback();
4299 $this->error =
"Invoice statut makes operation forbidden";
5039 global $conf, $langs, $user;
5043 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
5048 $sql =
"SELECT rowid";
5049 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
5050 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
5051 $sql .= $this->db->plimit(100);
5053 $resql = $this->db->query($sql);
5055 $num_prods = $this->db->num_rows($resql);
5057 while ($i < $num_prods) {
5059 $row = $this->db->fetch_row($resql);
5060 $prodids[$i] = $row[0];
5064 if (empty($num_prods)) {
5071 $this->
ref =
'SPECIMEN';
5072 $this->specimen = 1;
5074 $this->date = $nownotime;
5075 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
5076 $this->cond_reglement_id = 1;
5077 $this->cond_reglement_code =
'RECEP';
5079 $this->mode_reglement_id = 0;
5080 $this->mode_reglement_code =
'';
5082 $this->note_public =
'This is a comment (public)';
5083 $this->note_private =
'This is a comment (private)';
5084 $this->note =
'This is a comment (private)';
5086 $this->fk_user_author = $user->id;
5088 $this->multicurrency_tx = 1;
5089 $this->multicurrency_code = $conf->currency;
5091 $this->fk_incoterms = 0;
5092 $this->location_incoterms =
'';
5094 if (empty($option) || $option !=
'nolines') {
5098 while ($xnbp < $nbp) {
5100 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
5102 $line->subprice = 100;
5103 $line->tva_tx = 19.6;
5104 $line->localtax1_tx = 0;
5105 $line->localtax2_tx = 0;
5106 $line->remise_percent = 0;
5108 $prodid = mt_rand(1, $num_prods);
5109 $line->fk_product = $prodids[$prodid];
5111 $line->total_ht = -100;
5112 $line->total_ttc = -119.6;
5113 $line->total_tva = -19.6;
5114 $line->multicurrency_total_ht = -200;
5115 $line->multicurrency_total_ttc = -239.2;
5116 $line->multicurrency_total_tva = -39.2;
5117 } elseif ($xnbp == 2) {
5118 $line->subprice = -100;
5119 $line->total_ht = -100;
5120 $line->total_ttc = -119.6;
5121 $line->total_tva = -19.6;
5122 $line->remise_percent = 0;
5123 $line->multicurrency_total_ht = -200;
5124 $line->multicurrency_total_ttc = -239.2;
5125 $line->multicurrency_total_tva = -39.2;
5126 } elseif ($xnbp == 3) {
5127 $prodid = mt_rand(1, $num_prods);
5128 $line->fk_product = $prodids[$prodid];
5129 $line->total_ht = 50;
5130 $line->total_ttc = 59.8;
5131 $line->total_tva = 9.8;
5132 $line->multicurrency_total_ht = 100;
5133 $line->multicurrency_total_ttc = 119.6;
5134 $line->multicurrency_total_tva = 19.6;
5135 $line->remise_percent = 50;
5138 $prodid = mt_rand(1, $num_prods);
5139 $line->fk_product = $prodids[$prodid];
5140 $line->total_ht = 100;
5141 $line->total_ttc = 119.6;
5142 $line->total_tva = 19.6;
5143 $line->multicurrency_total_ht = 200;
5144 $line->multicurrency_total_ttc = 239.2;
5145 $line->multicurrency_total_tva = 39.2;
5146 $line->remise_percent = 0;
5149 $this->lines[$xnbp] = $line;
5152 $this->total_ht += $line->total_ht;
5153 $this->total_tva += $line->total_tva;
5154 $this->total_ttc += $line->total_ttc;
5156 $this->multicurrency_total_ht += $line->multicurrency_total_ht;
5157 $this->multicurrency_total_tva += $line->multicurrency_total_tva;
5158 $this->multicurrency_total_ttc += $line->multicurrency_total_ttc;
5162 $this->revenuestamp = 0;
5166 $line->desc = $langs->trans(
"Description").
" (offered line)";
5168 $line->subprice = 100;
5169 $line->tva_tx = 19.6;
5170 $line->localtax1_tx = 0;
5171 $line->localtax2_tx = 0;
5172 $line->remise_percent = 100;
5173 $line->total_ht = 0;
5174 $line->total_ttc = 0;
5175 $line->total_tva = 0;
5176 $line->multicurrency_total_ht = 0;
5177 $line->multicurrency_total_ttc = 0;
5178 $line->multicurrency_total_tva = 0;
5179 $prodid = mt_rand(1, $num_prods);
5180 $line->fk_product = $prodids[$prodid];
5182 $this->lines[$xnbp] = $line;
5666 global $conf, $langs, $user;
5672 $errorsMsg = array();
5674 $langs->load(
"bills");
5676 if (!isModEnabled(
'facture')) {
5677 $this->output .= $langs->trans(
'ModuleNotEnabled', $langs->transnoentitiesnoconv(
"Facture"));
5680 if (!in_array($datetouse, array(
'duedate',
'invoicedate'))) {
5681 $this->output .=
'Bad value for parameter datetouse. Must be "duedate" or "invoicedate"';
5691 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
5692 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
5693 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
5694 $formmail =
new FormMail($this->db);
5699 $tmpinvoice =
new Facture($this->db);
5704 $sql =
"SELECT rowid as id FROM ".MAIN_DB_PREFIX.
"facture as f";
5705 if (!empty($paymentmode) && $paymentmode !=
'all') {
5706 $sql .=
", ".MAIN_DB_PREFIX.
"c_paiement as cp";
5708 $sql .=
" WHERE f.paye = 0";
5709 $sql .=
" AND f.fk_statut = ".self::STATUS_VALIDATED;
5710 if ($datetouse ==
'invoicedate') {
5711 $sql .=
" AND f.datef = '".$this->db->idate($tmpidate,
'gmt').
"'";
5713 $sql .=
" AND f.date_lim_reglement = '".$this->db->idate($tmpidate,
'gmt').
"'";
5715 $sql .=
" AND f.entity IN (".getEntity(
'facture', 0).
")";
5716 if (!empty($paymentmode) && $paymentmode !=
'all') {
5717 $sql .=
" AND f.fk_mode_reglement = cp.id AND cp.code = '".$this->db->escape($paymentmode).
"'";
5720 if ($datetouse ==
'invoicedate') {
5721 $sql .= $this->db->order(
"datef",
"ASC");
5723 $sql .= $this->db->order(
"date_lim_reglement",
"ASC");
5726 $resql = $this->db->query($sql);
5729 if ($datetouse ==
'invoicedate') {
5730 $this->output .= $langs->transnoentitiesnoconv(
"SearchValidatedInvoicesWithDate", $stmpidate);
5732 $this->output .= $langs->transnoentitiesnoconv(
"SearchUnpaidInvoicesWithDueDate", $stmpidate);
5734 if (!empty($paymentmode) && $paymentmode !=
'all') {
5735 $this->output .=
' ('.$langs->transnoentitiesnoconv(
"PaymentMode").
' '.$paymentmode.
')';
5737 $this->output .=
'<br>';
5740 while ($obj = $this->db->fetch_object($resql)) {
5743 $res = $tmpinvoice->fetch($obj->id);
5745 $tmpinvoice->fetch_thirdparty();
5747 $outputlangs =
new Translate(
'', $conf);
5748 if ($tmpinvoice->thirdparty->default_lang) {
5749 $outputlangs->setDefaultLang($tmpinvoice->thirdparty->default_lang);
5750 $outputlangs->loadLangs(array(
"main",
"bills"));
5752 $outputlangs = $langs;
5756 $arraymessage = $formmail->getEMailTemplate($this->db,
'facture_send', $user, $outputlangs, (is_numeric($template) ? $template : 0), 1, (is_numeric($template) ?
'' : $template));
5757 if (is_numeric($arraymessage) && $arraymessage <= 0) {
5758 $langs->load(
"errors");
5759 $this->output .= $langs->trans(
'ErrorFailedToFindEmailTemplate', $template);
5772 $sendTopic =
make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv(
'InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
5775 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
5781 if ($forcerecipient) {
5782 $to = array($forcerecipient);
5784 $res = $tmpinvoice->fetch_thirdparty();
5785 $recipient = $tmpinvoice->thirdparty;
5787 $tmparraycontact = $tmpinvoice->liste_contact(-1,
'external', 0,
'BILLING');
5788 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
5789 foreach ($tmparraycontact as $data_email) {
5790 if (!empty($data_email[
'email'])) {
5791 $to[] = $tmpinvoice->thirdparty->contact_get_property($data_email[
'id'],
'email');
5795 if (empty($to) && !empty($recipient->email)) {
5796 $to[] = $recipient->email;
5799 $errormesg =
"Failed to send remind to thirdparty id=".$tmpinvoice->socid.
". No email defined for invoice or customer.";
5803 $errormesg =
"Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
5810 if (!empty($arraymessage->email_from)) {
5811 $from = $arraymessage->email_from;
5814 $errormesg =
"Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
5818 if (!$error && !empty($to)) {
5821 $to = implode(
',', $to);
5822 if (!empty($arraymessage->email_to)) {
5823 $to = $to.
','.$arraymessage->email_to;
5827 $errors_to = $conf->global->MAIN_MAIL_ERRORS_TO;
5829 $trackid =
'inv'.$tmpinvoice->id;
5830 $sendcontext =
'standard';
5833 if (!empty($arraymessage->email_tocc)) {
5834 $email_tocc = $arraymessage->email_tocc;
5838 if (!empty($arraymessage->email_tobcc)) {
5839 $email_tobcc = $arraymessage->email_tobcc;
5846 if ($arraymessage->joinfiles == 1 && !empty($tmpinvoice->last_main_doc)) {
5847 $joinFile[] = DOL_DATA_ROOT.$tmpinvoice->last_main_doc;
5848 $joinFileName[] = basename($tmpinvoice->last_main_doc);
5849 $joinFileMime[] =
dol_mimetype(DOL_DATA_ROOT.$tmpinvoice->last_main_doc);
5853 $cMailFile =
new CMailFile($sendTopic, $to, $from, $sendContent, $joinFile, $joinFileMime, $joinFileName, $email_tocc, $email_tobcc, 0, 1, $errors_to,
'', $trackid,
'', $sendcontext,
'');
5856 if ($cMailFile->sendfile()) {
5860 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
5865 $actioncomm->type_code =
'AC_OTH_AUTO';
5866 $actioncomm->socid = $tmpinvoice->thirdparty->id;
5867 $actioncomm->contact_id = 0;
5869 $actioncomm->code =
'AC_EMAIL';
5870 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateOK (nbdays='.$nbdays.
' paymentmode='.$paymentmode.
' template='.$template.
' datetouse='.$datetouse.
' forcerecipient='.$forcerecipient.
')';
5871 $actioncomm->note_private = $sendContent;
5872 $actioncomm->fk_project = $tmpinvoice->fk_project;
5873 $actioncomm->datep =
dol_now();
5874 $actioncomm->datef = $actioncomm->datep;
5875 $actioncomm->percentage = -1;
5876 $actioncomm->authorid = $user->id;
5877 $actioncomm->userownerid = $user->id;
5879 $actioncomm->email_msgid = $cMailFile->msgid;
5880 $actioncomm->email_subject = $sendTopic;
5881 $actioncomm->email_from = $from;
5882 $actioncomm->email_sender =
'';
5883 $actioncomm->email_to = $to;
5887 $actioncomm->errors_to = $errors_to;
5889 $actioncomm->elementtype =
'invoice';
5890 $actioncomm->fk_element = $tmpinvoice->id;
5894 $actioncomm->create($user);
5896 $errormesg = $cMailFile->error.
' : '.$to;
5900 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
5905 $actioncomm->type_code =
'AC_OTH_AUTO';
5906 $actioncomm->socid = $tmpinvoice->thirdparty->id;
5907 $actioncomm->contact_id = 0;
5909 $actioncomm->code =
'AC_EMAIL';
5910 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateKO';
5911 $actioncomm->note_private = $errormesg;
5912 $actioncomm->fk_project = $tmpinvoice->fk_project;
5913 $actioncomm->datep =
dol_now();
5914 $actioncomm->datef = $actioncomm->datep;
5915 $actioncomm->percentage = -1;
5916 $actioncomm->authorid = $user->id;
5917 $actioncomm->userownerid = $user->id;
5919 $actioncomm->email_msgid = $cMailFile->msgid;
5920 $actioncomm->email_from = $from;
5921 $actioncomm->email_sender =
'';
5922 $actioncomm->email_to = $to;
5926 $actioncomm->errors_to = $errors_to;
5930 $actioncomm->create($user);
5933 $this->db->commit();
5937 $errorsMsg[] = $errormesg;
5940 $errorsMsg[] =
'Failed to fetch record invoice with ID = '.$obj->id;
5950 $this->output .=
'Nb of emails sent : '.$nbMailSend;
5952 dol_syslog(__METHOD__.
" end - ".$this->output, LOG_INFO);
5956 $this->error =
'Nb of emails sent : '.$nbMailSend.
', '.(!empty($errorsMsg)) ? join(
', ', $errorsMsg) : $error;
5958 dol_syslog(__METHOD__.
" end - ".$this->error, LOG_INFO);