105 public function fetch($id, $ref =
'', $fk_bank = 0)
109 $sql =
'SELECT p.rowid, p.ref, p.entity, p.datep as dp, p.amount, p.statut, p.fk_bank, p.multicurrency_amount,';
110 $sql .=
' c.code as payment_code, c.libelle as payment_type,';
111 $sql .=
' p.num_paiement as num_payment, p.note, b.fk_account, p.fk_paiement';
112 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn as p';
113 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
114 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
115 $sql .=
' WHERE p.entity IN ('.getEntity(
'facture_fourn').
')';
117 $sql .=
' AND p.rowid = '.((int) $id);
119 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
120 } elseif ($fk_bank > 0) {
121 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
125 $resql = $this->db->query($sql);
127 $num = $this->db->num_rows($resql);
129 $obj = $this->db->fetch_object($resql);
131 $this->
id = $obj->rowid;
132 $this->
ref = $obj->ref;
133 $this->entity = $obj->entity;
134 $this->
date = $this->db->jdate($obj->dp);
135 $this->datepaye = $this->db->jdate($obj->dp);
136 $this->num_payment = $obj->num_payment;
137 $this->bank_account = $obj->fk_account;
138 $this->fk_account = $obj->fk_account;
139 $this->bank_line = $obj->fk_bank;
140 $this->montant = $obj->amount;
141 $this->
amount = $obj->amount;
142 $this->multicurrency_amount = $obj->multicurrency_amount;
143 $this->note = $obj->note;
144 $this->note_private = $obj->note;
145 $this->type_code = $obj->payment_code;
146 $this->type_label = $obj->payment_type;
147 $this->fk_paiement = $obj->fk_paiement;
148 $this->statut = $obj->statut;
154 $this->db->free($resql);
170 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
172 global $langs, $conf;
181 $totalamount_converted = 0;
182 $atleastonepaymentnotnull = 0;
184 if ($way ==
'dolibarr') {
185 $amounts = &$this->amounts;
186 $amounts_to_update = &$this->multicurrency_amounts;
188 $amounts = &$this->multicurrency_amounts;
189 $amounts_to_update = &$this->amounts;
192 $currencyofpayment =
'';
193 $currencytxofpayment =
'';
195 foreach ($amounts as $key => $value) {
202 if ($value_converted ===
false) {
204 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
207 if (empty($currencyofpayment)) {
208 $currencyofpayment = $this->multicurrency_code[$key];
210 if ($currencyofpayment != $this->multicurrency_code[$key]) {
212 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
215 if (empty($currencytxofpayment)) {
216 $currencytxofpayment = $this->multicurrency_tx[$key];
219 $totalamount_converted += $value_converted;
220 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
223 $amounts[$key] = $newvalue;
224 $totalamount += $newvalue;
225 if (!empty($newvalue)) {
226 $atleastonepaymentnotnull++;
230 if (!empty($currencyofpayment)) {
232 $bankaccount =
new Account($this->db);
233 $bankaccount->fetch($this->fk_account);
234 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
235 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) {
236 $langs->load(
"errors");
237 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
243 $totalamount = (float)
price2num($totalamount);
244 $totalamount_converted = (float)
price2num($totalamount_converted);
248 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
252 if ($totalamount != 0) {
253 $ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
255 if ($way ==
'dolibarr') {
256 $total = $totalamount;
257 $mtotal = $totalamount_converted;
259 $total = $totalamount_converted;
260 $mtotal = $totalamount;
263 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn (';
264 $sql .=
'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
265 $sql .=
" VALUES ('".$this->db->escape($ref).
"', ".((int) $conf->entity).
", '".$this->db->idate($now).
"',";
266 $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)";
268 $resql = $this->db->query($sql);
270 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiementfourn');
273 foreach ($this->amounts as $key => $amount) {
275 if (is_numeric($amount) && $amount != 0) {
277 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)';
278 $sql .=
" VALUES (".((int) $facid).
", ".((int) $this->
id).
", ".((float) $amount).
', '.((float) $this->multicurrency_amounts[$key]).
', '.($currencyofpayment ?
"'".$this->db->escape($currencyofpayment).
"'" :
'NULL').
', '.(!empty($currencytxofpayment) ? (float) $currencytxofpayment : 1).
')';
279 $resql = $this->db->query($sql);
282 $invoice->fetch($facid);
285 if ($closepaidinvoices) {
286 $paiement = $invoice->getSommePaiement();
287 $creditnotes = $invoice->getSumCreditNotesUsed();
289 $deposits = $invoice->getSumDepositsUsed();
291 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
292 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
293 if ($remaintopay == 0) {
296 $amount_ht = $amount_tva = $amount_ttc = array();
297 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
299 @phan-var-force array<string,float> $amount_ht
300 @phan-var-force array<string,float> $amount_tva
301 @phan-var-force array<string,float> $amount_ttc
302 @phan-var-force array<string,float> $multicurrency_amount_ht
303 @phan-var-force array<string,float> $multicurrency_amount_tva
304 @phan-var-force array<string,float> $multicurrency_amount_ttc
308 require_once DOL_DOCUMENT_ROOT .
'/core/class/discount.class.php';
310 $discount->fetch(0, 0, $invoice->id);
311 if (empty($discount->id)) {
312 $discount->discount_type = 1;
313 $discount->description =
'(DEPOSIT)';
314 $discount->fk_soc = $invoice->socid;
315 $discount->socid = $invoice->socid;
316 $discount->fk_invoice_supplier_source = $invoice->id;
320 foreach ($invoice->lines as $line) {
321 if ($line->total_ht != 0) {
322 if (!array_key_exists($line->tva_tx, $amount_ht)) {
323 $amount_ht[$line->tva_tx] = 0.0;
324 $amount_tva[$line->tva_tx] = 0.0;
325 $amount_ttc[$line->tva_tx] = 0.0;
326 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
327 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
328 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
330 $amount_ht[$line->tva_tx] += $line->total_ht;
331 $amount_tva[$line->tva_tx] += $line->total_tva;
332 $amount_ttc[$line->tva_tx] += $line->total_ttc;
333 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
334 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
335 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
340 foreach ($amount_ht as $tva_tx => $xxx) {
341 $discount->amount_ht = abs($amount_ht[$tva_tx]);
342 $discount->amount_tva = abs($amount_tva[$tva_tx]);
343 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
344 $discount->total_ht = abs($amount_ht[$tva_tx]);
345 $discount->total_tva = abs($amount_tva[$tva_tx]);
346 $discount->total_ttc = abs($amount_ttc[$tva_tx]);
347 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
348 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
349 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
350 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]);
351 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]);
352 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
353 $discount->tva_tx = abs((
float) $tva_tx);
355 $result = $discount->create($user);
371 $result = $invoice->setPaid($user,
'',
'');
373 $this->error = $invoice->error;
380 $hookmanager->initHooks(array(
'payment_supplierdao'));
381 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
382 $action =
'CLOSEPAIDSUPPLIERINVOICE';
383 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
385 $this->error = $hookmanager->error;
387 } elseif ($reshook == 0) {
388 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
396 $outputlangs = $langs;
398 $invoice->fetch_thirdparty();
399 $newlang = $invoice->thirdparty->default_lang;
401 if (!empty($newlang)) {
403 $outputlangs->setDefaultLang($newlang);
405 $ret = $invoice->fetch($facid);
406 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
413 $this->error = $this->db->lasterror();
417 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
423 $result = $this->call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
430 $this->error = $this->db->lasterror();
434 $this->error =
"ErrorTotalIsNull";
435 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
439 if ($totalamount != 0 && $error == 0) {
441 $this->total = $total;
442 $this->multicurrency_amount = $mtotal;
444 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->
amount.
', Total currency = '.$this->multicurrency_amount);
447 $this->db->rollback();
685 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
687 global $langs, $conf, $hookmanager;
689 if (!empty($conf->dol_no_mouse_hover)) {
697 if (preg_match(
'/^\((.*)\)$/i', $text, $reg)) {
699 if ($reg[1] ==
'paiement') {
702 $text = $langs->trans($reg[1]);
705 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
706 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$text;
707 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
708 if ($dateofpayment) {
709 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> '.
dol_print_date($dateofpayment,
'dayhour',
'tzuser');
712 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->
amount, 0, $langs, 1, -1, -1, $conf->currency);
716 if (empty($notooltip)) {
718 $label = $langs->trans(
"Payment");
719 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
721 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
722 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
724 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
727 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/fourn/paiement/card.php?id='.$this->
id.
'"';
728 $linkstart .= $linkclose.
'>';
731 $result .= $linkstart;
733 $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);
735 if ($withpicto != 2) {
736 $result .= $this->ref;
741 $hookmanager->initHooks(array($this->element .
'dao'));
742 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
743 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
745 $result = $hookmanager->resPrint;
747 $result .= $hookmanager->resPrint;