32 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
34 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
44 public $element =
'payment_supplier';
49 public $table_element =
'paiementfourn';
54 public $picto =
'payment';
75 public $id_prelevement;
80 public $num_prelevement;
101 public function fetch($id, $ref =
'', $fk_bank =
'')
105 $sql =
'SELECT p.rowid, p.ref, p.entity, p.datep as dp, p.amount, p.statut, p.fk_bank, p.multicurrency_amount,';
106 $sql .=
' c.code as payment_code, c.libelle as payment_type,';
107 $sql .=
' p.num_paiement as num_payment, p.note, b.fk_account, p.fk_paiement';
108 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn as p';
109 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
110 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
111 $sql .=
' WHERE p.entity IN ('.getEntity(
'facture_fourn').
')';
113 $sql .=
' AND p.rowid = '.((int) $id);
115 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
116 } elseif ($fk_bank > 0) {
117 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
121 $resql = $this->db->query(
$sql);
123 $num = $this->db->num_rows($resql);
125 $obj = $this->db->fetch_object($resql);
127 $this->
id = $obj->rowid;
128 $this->
ref = $obj->ref;
129 $this->entity = $obj->entity;
130 $this->date = $this->db->jdate($obj->dp);
131 $this->datepaye = $this->db->jdate($obj->dp);
132 $this->num_payment = $obj->num_payment;
133 $this->numero = $obj->num_payment;
134 $this->bank_account = $obj->fk_account;
135 $this->fk_account = $obj->fk_account;
136 $this->bank_line = $obj->fk_bank;
137 $this->montant = $obj->amount;
138 $this->amount = $obj->amount;
139 $this->multicurrency_amount = $obj->multicurrency_amount;
140 $this->note = $obj->note;
141 $this->note_private = $obj->note;
142 $this->type_code = $obj->payment_code;
143 $this->type_label = $obj->payment_type;
144 $this->fk_paiement = $obj->fk_paiement;
145 $this->statut = $obj->statut;
151 $this->db->free($resql);
167 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
169 global $langs, $conf;
178 $totalamount_converted = 0;
179 $atleastonepaymentnotnull = 0;
181 if ($way ==
'dolibarr') {
182 $amounts = &$this->amounts;
183 $amounts_to_update = &$this->multicurrency_amounts;
185 $amounts = &$this->multicurrency_amounts;
186 $amounts_to_update = &$this->amounts;
189 $currencyofpayment =
'';
190 $currencytxofpayment =
'';
192 foreach ($amounts as $key => $value) {
197 $value_converted = Multicurrency::getAmountConversionFromInvoiceRate($key, $value ? $value : 0, $way,
'facture_fourn');
199 if ($value_converted ===
false) {
201 $this->error =
'FailedToFoundTheConversionRateForInvoice';
204 if (empty($currencyofpayment)) {
205 $currencyofpayment = $this->multicurrency_code[$key];
207 if ($currencyofpayment != $this->multicurrency_code[$key]) {
209 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
212 if (empty($currencytxofpayment)) {
213 $currencytxofpayment = $this->multicurrency_tx[$key];
216 $totalamount_converted += $value_converted;
217 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
220 $amounts[$key] = $newvalue;
221 $totalamount += $newvalue;
222 if (!empty($newvalue)) {
223 $atleastonepaymentnotnull++;
227 if (!empty($currencyofpayment)) {
229 $bankaccount =
new Account($this->db);
230 $bankaccount->fetch($this->fk_account);
231 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
232 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) {
233 $langs->load(
"errors");
234 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
241 $totalamount_converted =
price2num($totalamount_converted);
243 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
247 if ($totalamount <> 0) {
248 $ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
250 if ($way ==
'dolibarr') {
252 $mtotal = $totalamount_converted;
254 $total = $totalamount_converted;
255 $mtotal = $totalamount;
258 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn (';
259 $sql .=
'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
260 $sql .=
" VALUES ('".$this->db->escape($ref).
"', ".((int) $conf->entity).
", '".$this->db->idate($now).
"',";
261 $sql .=
" '".$this->db->idate($this->datepaye).
"', ".((
float)
$total).
", ".((
float) $mtotal).
", ".((int) $this->paiementid).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', ".((int) $user->id).
", 0)";
263 $resql = $this->db->query(
$sql);
265 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiementfourn');
268 foreach ($this->amounts as $key => $amount) {
270 if (is_numeric($amount) && $amount <> 0) {
272 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)';
273 $sql .=
" VALUES (".((int) $facid).
", ".((int) $this->
id).
", ".((
float) $amount).
', '.((
float) $this->multicurrency_amounts[$key]).
', '.($currencyofpayment ?
'"'.$this->db->escape($currencyofpayment).
'"' :
'NULL').
', '.(!empty($currencytxofpayment) ? (double) $currencytxofpayment : 1).
')';
274 $resql = $this->db->query(
$sql);
277 $invoice->fetch($facid);
280 if ($closepaidinvoices) {
281 $paiement = $invoice->getSommePaiement();
282 $creditnotes=$invoice->getSumCreditNotesUsed();
284 $deposits=$invoice->getSumDepositsUsed();
286 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
287 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
288 if ($remaintopay == 0) {
291 $amount_ht = $amount_tva = $amount_ttc = array();
292 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
295 require_once DOL_DOCUMENT_ROOT .
'/core/class/discount.class.php';
297 $discount->fetch(
'', 0, $invoice->id);
298 if (empty($discount->id)) {
299 $discount->discount_type = 1;
300 $discount->description =
'(DEPOSIT)';
301 $discount->fk_soc = $invoice->socid;
302 $discount->fk_invoice_supplier_source = $invoice->id;
306 foreach ($invoice->lines as $line) {
307 if ($line->total_ht != 0) {
308 $amount_ht[$line->tva_tx] += $line->total_ht;
309 $amount_tva[$line->tva_tx] += $line->total_tva;
310 $amount_ttc[$line->tva_tx] += $line->total_ttc;
311 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
312 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
313 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
318 foreach ($amount_ht as $tva_tx => $xxx) {
319 $discount->amount_ht = abs($amount_ht[$tva_tx]);
320 $discount->amount_tva = abs($amount_tva[$tva_tx]);
321 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
322 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
323 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
324 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
325 $discount->tva_tx = abs($tva_tx);
327 $result = $discount->create($user);
343 $result = $invoice->setPaid($user,
'',
'');
345 $this->error = $invoice->error;
352 $hookmanager->initHooks(array(
'payment_supplierdao'));
353 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
354 $action =
'CLOSEPAIDSUPPLIERINVOICE';
355 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
357 $this->error = $hookmanager->error;
359 } elseif ($reshook == 0) {
360 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
366 if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
368 $outputlangs = $langs;
370 $invoice->fetch_thirdparty();
371 $newlang = $invoice->thirdparty->default_lang;
373 if (!empty($newlang)) {
375 $outputlangs->setDefaultLang($newlang);
377 $ret = $invoice->fetch($facid);
378 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
385 $this->error = $this->db->lasterror();
389 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
395 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
402 $this->error = $this->db->lasterror();
406 $this->error =
"ErrorTotalIsNull";
407 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
411 if ($totalamount <> 0 && $error == 0) {
414 $this->multicurrency_amount = $mtotal;
416 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->amount.
', Total currency = '.$this->multicurrency_amount);
419 $this->db->rollback();
433 public function delete($notrigger = 0)
435 global $conf, $user, $langs;
437 $bank_line_id = $this->bank_line;
444 if (is_array($billsarray)) {
445 if (count($billsarray)) {
446 $this->error =
"ErrorCantDeletePaymentSharedWithPayedInvoice";
447 $this->db->rollback();
451 $this->db->rollback();
459 $accline->fetch($bank_line_id);
460 if ($accline->rappro) {
461 $this->error =
"ErrorCantDeletePaymentReconciliated";
462 $this->db->rollback();
468 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn';
469 $sql .=
' WHERE fk_paiementfourn = '.((int) $this->
id);
470 $resql = $this->db->query(
$sql);
472 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'paiementfourn';
473 $sql .=
" WHERE rowid = ".((int) $this->
id);
474 $result = $this->db->query(
$sql);
476 $this->error = $this->db->error();
477 $this->db->rollback();
484 $result = $accline->fetch($bank_line_id);
486 $result = $accline->delete($user);
489 $this->error = $accline->error;
490 $this->db->rollback();
497 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_DELETE', $user);
499 $this->db->rollback();
508 $this->error = $this->db->error;
509 $this->db->rollback();
522 $sql =
'SELECT c.rowid, datec, fk_user_author as fk_user_creat, tms';
523 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn as c';
524 $sql .=
' WHERE c.rowid = '.((int) $id);
526 $resql = $this->db->query(
$sql);
528 $num = $this->db->num_rows($resql);
530 $obj = $this->db->fetch_object($resql);
531 $this->
id = $obj->rowid;
533 if ($obj->fk_user_creat) {
534 $cuser =
new User($this->db);
535 $cuser->fetch($obj->fk_user_creat);
536 $this->user_creation = $cuser;
538 if ($obj->fk_user_modif) {
539 $muser =
new User($this->db);
540 $muser->fetch($obj->fk_user_modif);
541 $this->user_modification = $muser;
543 $this->date_creation = $this->db->jdate($obj->datec);
544 $this->date_modification = $this->db->jdate($obj->tms);
546 $this->db->free($resql);
560 $sql =
'SELECT fk_facturefourn';
561 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn as pf, '.MAIN_DB_PREFIX.
'facture_fourn as f';
562 $sql .=
' WHERE pf.fk_facturefourn = f.rowid AND fk_paiementfourn = '.((int) $this->
id);
564 $sql .=
" AND ".$filter;
567 dol_syslog(get_class($this).
'::getBillsArray', LOG_DEBUG);
568 $resql = $this->db->query(
$sql);
571 $num = $this->db->num_rows($resql);
572 $billsarray = array();
575 $obj = $this->db->fetch_object($resql);
576 $billsarray[$i] = $obj->fk_facturefourn;
582 $this->error = $this->db->error();
583 dol_syslog(get_class($this).
'::getBillsArray Error '.$this->error);
596 return $this->
LibStatut($this->statut, $mode);
612 $langs->load(
'compta');
662 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
664 global $langs, $conf, $hookmanager;
666 if (!empty($conf->dol_no_mouse_hover)) {
674 if (preg_match(
'/^\((.*)\)$/i', $text, $reg)) {
676 if ($reg[1] ==
'paiement') {
679 $text = $langs->trans($reg[1]);
682 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
683 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$text;
684 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
685 if ($dateofpayment) {
686 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> '.
dol_print_date($dateofpayment,
'dayhour',
'tzuser');
689 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
693 if (empty($notooltip)) {
694 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
695 $label = $langs->trans(
"Payment");
696 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
698 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
699 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
701 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
704 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/fourn/paiement/card.php?id='.$this->
id.
'"';
705 $linkstart .= $linkclose.
'>';
708 $result .= $linkstart;
710 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
712 if ($withpicto != 2) {
713 $result .= $this->ref;
718 $hookmanager->initHooks(array($this->element .
'dao'));
719 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
720 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
722 $result = $hookmanager->resPrint;
724 $result .= $hookmanager->resPrint;
739 global $user, $langs, $conf;
743 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
747 $this->
ref =
'SPECIMEN';
751 $this->datepaye = $nownotime;
764 global $conf, $db, $langs;
765 $langs->load(
"bills");
768 if (empty($conf->global->SUPPLIER_PAYMENT_ADDON)) {
769 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_bronan';
770 } elseif ($conf->global->SUPPLIER_PAYMENT_ADDON ==
'brodator') {
771 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_brodator';
772 } elseif ($conf->global->SUPPLIER_PAYMENT_ADDON ==
'bronan') {
773 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_bronan';
776 if (!empty($conf->global->SUPPLIER_PAYMENT_ADDON)) {
779 $file = $conf->global->SUPPLIER_PAYMENT_ADDON.
".php";
780 $classname = $conf->global->SUPPLIER_PAYMENT_ADDON;
783 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
785 foreach ($dirmodels as $reldir) {
786 $dir =
dol_buildpath($reldir.
"core/modules/supplier_payment/");
789 if (is_file($dir.$file) && is_readable($dir.$file)) {
790 $mybool |= include_once $dir.$file;
795 if ($mybool ===
false) {
796 $file = $conf->global->SUPPLIER_PAYMENT_ADDON.
".php";
797 $classname =
"mod_supplier_payment_".$conf->global->SUPPLIER_PAYMENT_ADDON;
798 $classname = preg_replace(
'/\-.*$/',
'', $classname);
800 foreach ($conf->file->dol_document_root as $dirroot) {
801 $dir = $dirroot.
"/core/modules/supplier_payment/";
804 if (is_file($dir.$file) && is_readable($dir.$file)) {
805 $mybool |= include_once $dir.$file;
810 if ($mybool ===
false) {
815 $obj =
new $classname();
817 $numref = $obj->getNextValue($soc, $this);
823 if ($mode !=
'last' && !$numref) {
830 $langs->load(
"errors");
831 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Supplier"));
847 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
849 global $conf, $user, $langs;
851 $langs->load(
"suppliers");
854 if (empty($modele)) {
855 if (!empty($conf->global->SUPPLIER_PAYMENT_ADDON_PDF)) {
856 $modele = $conf->global->SUPPLIER_PAYMENT_ADDON_PDF;
862 if (empty($modele)) {
865 $modelpath =
"core/modules/supplier_payment/doc/";
867 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
884 foreach ($this->multicurrency_amounts as $value) {
885 if (!empty($value)) {
906 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
908 if (empty($force_thirdparty_id)) {
910 if (!empty($billsarray)) {
912 if ($supplier_invoice->fetch($billsarray[0]) > 0) {
913 $force_thirdparty_id = $supplier_invoice->fk_soc;
918 return parent::fetch_thirdparty($force_thirdparty_id);