26 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonincoterm.class.php';
97 public $totaldeposits;
98 public $totalcreditnotes;
101 public $sumpayed_multicurrency;
103 public $sumdeposit_multicurrency;
104 public $sumcreditnote;
105 public $sumcreditnote_multicurrency;
123 $remaintopay =
price2num($this->total_ttc - $alreadypaid,
'MT');
124 if ($this->statut == self::STATUS_CLOSED && $this->close_code ==
'discount_vat') {
139 $table =
'paiement_facture';
140 $field =
'fk_facture';
141 if ($this->element ==
'facture_fourn' || $this->element ==
'invoice_supplier') {
142 $table =
'paiementfourn_facturefourn';
143 $field =
'fk_facturefourn';
146 $sql =
"SELECT sum(amount) as amount, sum(multicurrency_amount) as multicurrency_amount";
147 $sql .=
" FROM ".$this->db->prefix().$table;
148 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
150 dol_syslog(get_class($this).
"::getSommePaiement", LOG_DEBUG);
153 $obj = $this->
db->fetch_object(
$resql);
156 if ($multicurrency) {
157 $this->sumpayed_multicurrency = $obj->multicurrency_amount;
158 return $obj->multicurrency_amount;
160 $this->sumpayed = $obj->amount;
164 $this->error = $this->
db->lasterror();
184 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
187 $result = $discountstatic->getSumDepositsUsed($this, $multicurrency);
190 if ($multicurrency) {
191 $this->sumdeposit_multicurrency = $result;
193 $this->sumdeposit = $result;
198 $this->error = $discountstatic->error;
211 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
214 $result = $discountstatic->getSumCreditNotesUsed($this, $multicurrency);
216 if ($multicurrency) {
217 $this->sumcreditnote_multicurrency = $result;
219 $this->sumcreditnote = $result;
224 $this->error = $discountstatic->error;
237 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
240 $result = $discountstatic->getSumFromThisCreditNotesNotUsed($this, $multicurrency);
244 $this->error = $discountstatic->error;
258 $sql =
"SELECT rowid";
259 $sql .=
" FROM ".$this->db->prefix().$this->table_element;
260 $sql .=
" WHERE fk_facture_source = ".((int) $this->
id);
261 $sql .=
" AND type = 2";
267 $row = $this->
db->fetch_row(
$resql);
268 $idarray[] = $row[0];
285 $sql =
"SELECT rowid";
286 $sql .=
" FROM ".$this->db->prefix().$this->table_element;
287 $sql .=
" WHERE fk_facture_source = ".((int) $this->
id);
288 $sql .=
" AND type < 2";
289 if ($option ==
'validated') {
290 $sql .=
' AND fk_statut = 1';
297 $sql .=
" ORDER BY fk_statut DESC";
301 $obj = $this->
db->fetch_object(
$resql);
324 $table =
'paiement_facture';
325 $table2 =
'paiement';
326 $field =
'fk_facture';
327 $field2 =
'fk_paiement';
328 $field3 =
', p.ref_ext';
329 $sharedentity =
'facture';
330 if ($this->element ==
'facture_fourn' || $this->element ==
'invoice_supplier') {
331 $table =
'paiementfourn_facturefourn';
332 $table2 =
'paiementfourn';
333 $field =
'fk_facturefourn';
334 $field2 =
'fk_paiementfourn';
336 $sharedentity =
'facture_fourn';
339 $sql =
"SELECT p.ref, pf.amount, pf.multicurrency_amount, p.fk_paiement, p.datep, p.num_paiement as num, t.code".$field3;
340 $sql .=
" FROM ".$this->db->prefix().$table.
" as pf, ".$this->
db->prefix().$table2.
" as p, ".$this->
db->prefix().
"c_paiement as t";
341 $sql .=
" WHERE pf.".$field.
" = ".((int) $this->
id);
342 $sql .=
" AND pf.".$field2.
" = p.rowid";
343 $sql .=
' AND p.fk_paiement = t.id';
344 $sql .=
' AND p.entity IN ('.getEntity($sharedentity).
')';
346 $sql .=
" AND t.code='PRE'";
349 dol_syslog(get_class($this).
"::getListOfPayments", LOG_DEBUG);
355 $obj = $this->
db->fetch_object(
$resql);
356 $tmp = array(
'amount'=>$obj->amount,
'type'=>$obj->code,
'date'=>$obj->datep,
'num'=>$obj->num,
'ref'=>$obj->ref);
357 if (!empty($field3)) {
358 $tmp[
'ref_ext'] = $obj->ref_ext;
367 if ($this->element ==
'facture' || $this->element ==
'invoice') {
368 $sql =
"SELECT rc.amount_ttc as amount, rc.multicurrency_amount_ttc as multicurrency_amount, rc.datec as date, f.ref as ref, rc.description as type";
369 $sql .=
' FROM '.$this->db->prefix().
'societe_remise_except as rc, '.$this->
db->prefix().
'facture as f';
370 $sql .=
' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.((int) $this->
id);
371 $sql .=
' AND (f.type = 2 OR f.type = 0 OR f.type = 3)';
372 } elseif ($this->element ==
'facture_fourn' || $this->element ==
'invoice_supplier') {
373 $sql =
"SELECT rc.amount_ttc as amount, rc.multicurrency_amount_ttc as multicurrency_amount, rc.datec as date, f.ref as ref, rc.description as type";
374 $sql .=
' FROM '.$this->db->prefix().
'societe_remise_except as rc, '.$this->
db->prefix().
'facture_fourn as f';
375 $sql .=
' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.((int) $this->
id);
376 $sql .=
' AND (f.type = 2 OR f.type = 0 OR f.type = 3)';
385 $obj = $this->
db->fetch_object(
$resql);
386 if ($multicurrency) {
387 $retarray[] = array(
'amount'=>$obj->multicurrency_amount,
'type'=>$obj->type,
'date'=>$obj->date,
'num'=>
'0',
'ref'=>$obj->ref);
389 $retarray[] = array(
'amount'=>$obj->amount,
'type'=>$obj->type,
'date'=>$obj->date,
'num'=>
'',
'ref'=>$obj->ref);
394 $this->error = $this->
db->lasterror();
403 $this->error = $this->
db->lasterror();
430 $tmppart = substr($this->
ref, 1, 4);
432 if ($this->statut == self::STATUS_DRAFT && $tmppart ===
'PROV') {
436 if (!empty($conf->global->INVOICE_CAN_NEVER_BE_REMOVED)) {
441 if ($tmppart !==
'PROV') {
443 if ($ventilExportCompta != 0) {
448 if ($this->element !=
'invoice_supplier') {
449 if (empty($this->thirdparty)) {
452 $maxref = $this->getNextNumRef($this->thirdparty,
'last');
456 if (empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $maxref !=
'' && $maxref != $this->ref) {
463 if ($this->situation_cycle_ref && method_exists($this,
'is_last_in_cycle')) {
464 $last = $this->is_last_in_cycle();
473 if (empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $this->getSommePaiement() > 0) {
487 $alreadydispatched = 0;
489 $type =
'customer_invoice';
490 if ($this->element ==
'invoice_supplier') {
491 $type =
'supplier_invoice';
494 $sql =
" SELECT COUNT(ab.rowid) as nb FROM ".$this->db->prefix().
"accounting_bookkeeping as ab WHERE ab.doc_type='".$this->
db->escape($type).
"' AND ab.fk_doc = ".((int) $this->
id);
497 $obj = $this->
db->fetch_object(
$resql);
499 $alreadydispatched = $obj->nb;
502 $this->error = $this->
db->lasterror();
506 if ($alreadydispatched) {
522 return $langs->trans(
"InvoiceStandard");
524 return $langs->trans(
"InvoiceReplacement");
526 return $langs->trans(
"InvoiceAvoir");
528 return $langs->trans(
"InvoiceDeposit");
530 return $langs->trans(
"InvoiceProForma");
532 return $langs->trans(
"InvoiceSituation");
534 return $langs->trans(
"Unknown");
546 return $this->
LibStatut($this->paye, $this->statut, $mode, $alreadypaid, $this->
type);
560 public function LibStatut($paye, $status, $mode = 0, $alreadypaid = -1, $type = -1)
564 $langs->load(
'bills');
570 $statusType =
'status0';
574 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusDraft');
575 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusDraft');
576 } elseif (($status == 3 || $status == 2) && $alreadypaid <= 0) {
578 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusCanceled');
579 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusCanceled');
581 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusClosedUnpaid');
582 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusClosedUnpaid');
584 $statusType =
'status5';
585 } elseif (($status == 3 || $status == 2) && $alreadypaid > 0) {
586 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusClosedPaidPartially');
587 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusClosedPaidPartially');
588 $statusType =
'status9';
589 } elseif ($alreadypaid == 0) {
590 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusNotPaid');
591 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusNotPaid');
592 $statusType =
'status1';
594 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusStarted');
595 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusStarted');
596 $statusType =
'status3';
599 $statusType =
'status6';
601 if ($type == self::TYPE_CREDIT_NOTE) {
602 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusPaidBackOrConverted');
603 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusPaidBackOrConverted');
604 } elseif ($type == self::TYPE_DEPOSIT) {
605 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusConverted');
606 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusConverted');
608 $labelStatus = $langs->transnoentitiesnoconv(
'BillStatusPaid');
609 $labelStatusShort = $langs->transnoentitiesnoconv(
'Bill'.$prefix.
'StatusPaid');
613 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
627 if (!$cond_reglement) {
628 $cond_reglement = $this->cond_reglement_code;
630 if (!$cond_reglement) {
631 $cond_reglement = $this->cond_reglement_id;
638 $sqltemp =
"SELECT c.type_cdr, c.nbjour, c.decalage";
639 $sqltemp .=
" FROM ".$this->db->prefix().
"c_payment_term as c";
640 if (is_numeric($cond_reglement)) {
641 $sqltemp .=
" WHERE c.rowid=".((int) $cond_reglement);
643 $sqltemp .=
" WHERE c.entity IN (".getEntity(
'c_payment_term').
")";
644 $sqltemp .=
" AND c.code = '".$this->db->escape($cond_reglement).
"'";
647 dol_syslog(get_class($this).
'::calculate_date_lim_reglement', LOG_DEBUG);
648 $resqltemp = $this->
db->query($sqltemp);
650 if ($this->
db->num_rows($resqltemp)) {
651 $obj = $this->
db->fetch_object($resqltemp);
652 $cdr_nbjour = $obj->nbjour;
653 $cdr_type = $obj->type_cdr;
654 $cdr_decalage = $obj->decalage;
657 $this->error = $this->
db->error();
660 $this->
db->free($resqltemp);
665 if ($cdr_type == 0) {
666 $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
668 $datelim += ($cdr_decalage * 3600 * 24);
669 } elseif ($cdr_type == 1) {
671 $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
673 $mois = date(
'm', $datelim);
674 $annee = date(
'Y', $datelim);
682 $datelim =
dol_mktime(12, 0, 0, $mois, 1, $annee);
683 $datelim -= (3600 * 24);
685 $datelim += ($cdr_decalage * 3600 * 24);
686 } elseif ($cdr_type == 2 && !empty($cdr_decalage)) {
688 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
689 $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
691 $date_piece =
dol_mktime(0, 0, 0, date(
'm', $datelim), date(
'd', $datelim), date(
'Y', $datelim));
692 $date_lim_current =
dol_mktime(0, 0, 0, date(
'm', $datelim), $cdr_decalage, date(
'Y', $datelim));
695 $diff = $date_piece - $date_lim_current;
698 $datelim = $date_lim_current;
700 $datelim = $date_lim_next;
703 return 'Bad value for type_cdr in database for record cond_reglement = '.$cond_reglement;
727 dol_syslog(get_class($this).
"::demande_prelevement", LOG_DEBUG);
729 if ($this->statut > self::STATUS_DRAFT && $this->paye == 0) {
730 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
732 $bac->fetch(0, $this->socid);
734 $sql =
"SELECT count(*)";
735 $sql .=
" FROM ".$this->db->prefix().
"prelevement_facture_demande";
736 if ($type ==
'bank-transfer') {
737 $sql .=
" WHERE fk_facture_fourn = ".((int) $this->
id);
739 $sql .=
" WHERE fk_facture = ".((int) $this->
id);
741 $sql .=
" AND ext_payment_id IS NULL";
742 $sql .=
" AND traite = 0";
744 dol_syslog(get_class($this).
"::demande_prelevement", LOG_DEBUG);
747 $row = $this->
db->fetch_row(
$resql);
760 if (empty($amount)) {
761 $amount =
price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
764 if (is_numeric($amount) && $amount != 0) {
765 $sql =
'INSERT INTO '.$this->db->prefix().
'prelevement_facture_demande(';
766 if ($type ==
'bank-transfer') {
767 $sql .=
'fk_facture_fourn, ';
769 $sql .=
'fk_facture, ';
771 $sql .=
' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, entity)';
772 $sql .=
" VALUES (".((int) $this->
id);
773 $sql .=
", ".((float)
price2num($amount));
774 $sql .=
", '".$this->db->idate($now).
"'";
775 $sql .=
", ".((int) $fuser->id);
776 $sql .=
", '".$this->db->escape($bac->code_banque).
"'";
777 $sql .=
", '".$this->db->escape($bac->code_guichet).
"'";
778 $sql .=
", '".$this->db->escape($bac->number).
"'";
779 $sql .=
", '".$this->db->escape($bac->cle_rib).
"'";
780 $sql .=
", '".$this->db->escape($sourcetype).
"'";
781 $sql .=
", ".((int) $conf->entity);
784 dol_syslog(get_class($this).
"::demande_prelevement", LOG_DEBUG);
787 $this->error = $this->
db->lasterror();
788 dol_syslog(get_class($this).
'::demandeprelevement Erreur');
792 $this->error =
'WithdrawRequestErrorNilAmount';
793 dol_syslog(get_class($this).
'::demandeprelevement WithdrawRequestErrorNilAmount');
799 $payment_mode_id =
dol_getIdFromCode($this->
db, ($type ==
'bank-transfer' ?
'VIR' :
'PRE'),
'c_paiement',
'code',
'id', 1);
800 if ($payment_mode_id > 0) {
810 $this->error =
"A request already exists";
811 dol_syslog(get_class($this).
'::demandeprelevement Impossible de creer une demande, demande deja en cours');
815 $this->error = $this->
db->error();
816 dol_syslog(get_class($this).
'::demandeprelevement Erreur -2');
820 $this->error =
"Status of invoice does not allow this";
821 dol_syslog(get_class($this).
"::demandeprelevement ".$this->error.
" $this->statut, $this->paye, $this->mode_reglement_id");
837 $sql =
'DELETE FROM '.$this->db->prefix().
'prelevement_facture_demande';
838 $sql .=
' WHERE rowid = '.((int) $did);
839 $sql .=
' AND traite = 0';
840 if ($this->
db->query($sql)) {
843 $this->error = $this->
db->lasterror();
844 dol_syslog(get_class($this).
'::demande_prelevement_delete Error '.$this->error);
857 global $conf, $mysoc;
860 $tmplang->setDefaultLang(
'en_US');
861 $tmplang->load(
"main");
866 $pricewithtaxstring =
price2num($this->total_ttc, 2, 1);
867 $pricetaxstring =
price2num($this->total_tva, 2, 1);
894 $s = pack(
'C1', 1).pack(
'C1', strlen($mysoc->name)).$mysoc->name;
895 $s .= pack(
'C1', 2).pack(
'C1', strlen($mysoc->tva_intra)).$mysoc->tva_intra;
896 $s .= pack(
'C1', 3).pack(
'C1', strlen($datestring)).$datestring;
897 $s .= pack(
'C1', 4).pack(
'C1', strlen($pricewithtaxstring)).$pricewithtaxstring;
898 $s .= pack(
'C1', 5).pack(
'C1', strlen($pricetaxstring)).$pricetaxstring;
904 $s = base64_encode($s);
917 global $conf, $mysoc;
920 $tmplang->setDefaultLang(
'en_US');
921 $tmplang->load(
"main");
923 $pricewithtaxstring =
price2num($this->total_ttc, 2, 1);
924 $pricetaxstring =
price2num($this->total_tva, 2, 1);
926 $complementaryinfo =
'';
940 $complementaryinfo =
'//S1/10/'.str_replace(
'/',
'', $this->
ref).
'/11/'.$datestring;
941 if ($this->ref_client) {
942 $complementaryinfo .=
'/20/'.$this->ref_client;
944 if ($this->thirdparty->vat_number) {
945 $complementaryinfo .=
'/30/'.$this->thirdparty->vat_number;
948 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
956 if ($this->fk_account > 0) {
958 $bankaccount->fetch($this->fk_account);
959 $s .= $bankaccount->iban.
"\n";
963 if ($bankaccount->id > 0 &&
getDolGlobalString(
'PDF_SWISS_QRCODE_USE_OWNER_OF_ACCOUNT_AS_CREDITOR')) {
967 $s .=
dol_trunc($bankaccount->proprio, 70,
'right',
'UTF-8', 1).
"\n";
968 $addresslinearray = explode(
"\n", $bankaccount->owner_address);
969 $s .=
dol_trunc(empty($addresslinearray[1]) ?
'' : $addresslinearray[1], 70,
'right',
'UTF-8', 1).
"\n";
970 $s .=
dol_trunc(empty($addresslinearray[2]) ?
'' : $addresslinearray[2], 70,
'right',
'UTF-8', 1).
"\n";
976 $s .=
dol_trunc($mysoc->name, 70,
'right',
'UTF-8', 1).
"\n";
977 $addresslinearray = explode(
"\n", $mysoc->address);
978 $s .=
dol_trunc(empty($addresslinearray[1]) ?
'' : $addresslinearray[1], 70,
'right',
'UTF-8', 1).
"\n";
979 $s .=
dol_trunc(empty($addresslinearray[2]) ?
'' : $addresslinearray[2], 70,
'right',
'UTF-8', 1).
"\n";
980 $s .=
dol_trunc($mysoc->zip, 16,
'right',
'UTF-8', 1).
"\n";
981 $s .=
dol_trunc($mysoc->town, 35,
'right',
'UTF-8', 1).
"\n";
982 $s .=
dol_trunc($mysoc->country_code, 2,
'right',
'UTF-8', 1).
"\n";
993 $s .=
price($pricewithtaxstring, 0,
'none', 0, 0, 2).
"\n";
994 $s .= ($this->multicurrency_code ? $this->multicurrency_code : $conf->currency).
"\n";
997 $s .=
dol_trunc($this->thirdparty->name, 70,
'right',
'UTF-8', 1).
"\n";
998 $addresslinearray = explode(
"\n", $this->thirdparty->address);
999 $s .=
dol_trunc(empty($addresslinearray[1]) ?
'' : $addresslinearray[1], 70,
'right',
'UTF-8', 1).
"\n";
1000 $s .=
dol_trunc(empty($addresslinearray[2]) ?
'' : $addresslinearray[2], 70,
'right',
'UTF-8', 1).
"\n";
1001 $s .=
dol_trunc($this->thirdparty->zip, 16,
'right',
'UTF-8', 1).
"\n";
1002 $s .=
dol_trunc($this->thirdparty->town, 35,
'right',
'UTF-8', 1).
"\n";
1003 $s .=
dol_trunc($this->thirdparty->country_code, 2,
'right',
'UTF-8', 1).
"\n";
1008 if ($complementaryinfo) {
1009 $s .= $complementaryinfo.
"\n";
1015 if ($complementaryinfo) {
1016 $s .= $complementaryinfo.
"\n";
1026 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
1054 public $product_type = 0;
1060 public $product_ref;
1066 public $product_label;
1072 public $product_desc;
1103 public $vat_src_code;
1115 public $localtax1_tx;
1121 public $localtax2_tx;
1127 public $localtax1_type;
1133 public $localtax2_type;
1139 public $remise_percent;
1164 public $total_localtax1;
1170 public $total_localtax2;
1178 public $date_start_fill;
1179 public $date_end_fill;
1181 public $buy_price_ht;
1194 public $info_bits = 0;
1196 public $special_code = 0;
1198 public $fk_multicurrency;
1199 public $multicurrency_code;
1200 public $multicurrency_subprice;
1201 public $multicurrency_total_ht;
1202 public $multicurrency_total_tva;
1203 public $multicurrency_total_ttc;
1205 public $fk_user_author;
1206 public $fk_user_modif;
1208 public $fk_accounting_account;