172 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
174 global $langs,
$conf;
183 $totalamount_converted = 0;
184 $atleastonepaymentnotnull = 0;
186 if ($way ==
'dolibarr') {
187 $amounts = &$this->amounts;
188 $amounts_to_update = &$this->multicurrency_amounts;
190 $amounts = &$this->multicurrency_amounts;
191 $amounts_to_update = &$this->amounts;
194 $currencyofpayment =
'';
195 $currencytxofpayment =
'';
197 foreach ($amounts as $key => $value) {
204 if ($value_converted ===
false) {
206 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
210 $invoice_multicurrency_code = $this->multicurrency_code[$key] ??
'';
211 $invoice_multicurrency_tx = $this->multicurrency_tx[$key] ??
'';
212 if (empty($invoice_multicurrency_code) || empty($invoice_multicurrency_tx)) {
214 if ($tmparray !==
false) {
215 if (empty($invoice_multicurrency_code)) {
216 $invoice_multicurrency_code = $tmparray[
'invoice_multicurrency_code'];
218 if (empty($invoice_multicurrency_tx)) {
219 $invoice_multicurrency_tx = $tmparray[
'invoice_multicurrency_tx'];
224 if (empty($currencyofpayment)) {
225 $currencyofpayment = $invoice_multicurrency_code;
227 if ($currencyofpayment != $invoice_multicurrency_code) {
229 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
232 if (empty($currencytxofpayment)) {
233 $currencytxofpayment = $invoice_multicurrency_tx;
236 $totalamount_converted += $value_converted;
237 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
240 $amounts[$key] = $newvalue;
241 $totalamount += $newvalue;
242 if (!empty($newvalue)) {
243 $atleastonepaymentnotnull++;
247 if (!empty($currencyofpayment)) {
249 $bankaccount =
new Account($this->db);
250 $bankaccount->fetch($this->fk_account);
251 $bankcurrencycode = empty($bankaccount->currency_code) ?
$conf->currency : $bankaccount->currency_code;
252 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment !=
$conf->currency && $bankcurrencycode !=
$conf->currency) {
253 $langs->load(
"errors");
254 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
260 $totalamount = (float)
price2num($totalamount);
261 $totalamount_converted = (float)
price2num($totalamount_converted);
265 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
269 if ($totalamount != 0) {
270 $ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
272 if ($way ==
'dolibarr') {
273 $total = $totalamount;
274 $mtotal = $totalamount_converted;
276 $total = $totalamount_converted;
277 $mtotal = $totalamount;
280 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn (';
281 $sql .=
'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
282 $sql .=
" VALUES ('".$this->db->escape($ref).
"', ".((int)
$conf->entity).
", '".$this->db->idate($now).
"',";
283 $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)";
285 $resql = $this->db->query($sql);
287 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiementfourn');
290 foreach ($this->amounts as $key => $amount) {
292 if (is_numeric($amount) && $amount != 0) {
294 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)';
295 $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).
')';
296 $resql = $this->db->query($sql);
299 $invoice->fetch($facid);
302 if ($closepaidinvoices) {
303 $paiement = $invoice->getSommePaiement();
304 $creditnotes = $invoice->getSumCreditNotesUsed();
306 $deposits = $invoice->getSumDepositsUsed();
308 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
309 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
310 if ($remaintopay == 0) {
313 $amount_ht = $amount_tva = $amount_ttc = array();
314 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
316 @phan-var-force array<string,float> $amount_ht
317 @phan-var-force array<string,float> $amount_tva
318 @phan-var-force array<string,float> $amount_ttc
319 @phan-var-force array<string,float> $multicurrency_amount_ht
320 @phan-var-force array<string,float> $multicurrency_amount_tva
321 @phan-var-force array<string,float> $multicurrency_amount_ttc
325 require_once DOL_DOCUMENT_ROOT .
'/core/class/discount.class.php';
327 $discount->fetch(0, 0, $invoice->id);
328 if (empty($discount->id)) {
329 $discount->discount_type = 1;
330 $discount->description =
'(DEPOSIT)';
331 $discount->fk_soc = $invoice->socid;
332 $discount->socid = $invoice->socid;
333 $discount->fk_invoice_supplier_source = $invoice->id;
334 $discount->multicurrency_code = $invoice->multicurrency_code;
335 $discount->multicurrency_tx = $invoice->multicurrency_tx;
339 foreach ($invoice->lines as $line) {
340 if ($line->total_ht != 0) {
341 if (!array_key_exists($line->tva_tx, $amount_ht)) {
342 $amount_ht[$line->tva_tx] = 0.0;
343 $amount_tva[$line->tva_tx] = 0.0;
344 $amount_ttc[$line->tva_tx] = 0.0;
345 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
346 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
347 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
349 $amount_ht[$line->tva_tx] += $line->total_ht;
350 $amount_tva[$line->tva_tx] += $line->total_tva;
351 $amount_ttc[$line->tva_tx] += $line->total_ttc;
352 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
353 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
354 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
359 foreach ($amount_ht as $tva_tx => $xxx) {
360 $discount->amount_ht = abs($amount_ht[$tva_tx]);
361 $discount->amount_tva = abs($amount_tva[$tva_tx]);
362 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
364 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]);
365 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]);
366 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
368 $discount->multicurrency_amount_ht = $discount->multicurrency_total_ht;
369 $discount->multicurrency_amount_tva = $discount->multicurrency_total_tva;
370 $discount->multicurrency_amount_ttc = $discount->multicurrency_total_ttc;
371 $discount->tva_tx = abs((
float) $tva_tx);
373 $result = $discount->create($user);
389 $result = $invoice->setPaid($user,
'',
'');
391 $this->error = $invoice->error;
398 $hookmanager->initHooks(array(
'payment_supplierdao'));
399 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
400 $action =
'CLOSEPAIDSUPPLIERINVOICE';
401 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
403 $this->error = $hookmanager->error;
405 } elseif ($reshook == 0) {
406 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
414 $outputlangs = $langs;
416 $invoice->fetch_thirdparty();
417 $newlang = $invoice->thirdparty->default_lang;
419 if (!empty($newlang)) {
421 $outputlangs->setDefaultLang($newlang);
423 $ret = $invoice->fetch($facid);
424 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
431 $this->error = $this->db->lasterror();
435 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
441 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
448 $this->error = $this->db->lasterror();
452 $this->error =
"ErrorTotalIsNull";
453 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
457 if ($totalamount != 0 && $error == 0) {
458 $this->amount = $total;
459 $this->total = $total;
460 $this->multicurrency_amount = $mtotal;
462 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->amount.
', Total currency = '.$this->multicurrency_amount);
465 $this->db->rollback();
703 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
705 global $langs,
$conf, $hookmanager;
707 if (!empty(
$conf->dol_no_mouse_hover)) {
715 if (preg_match(
'/^\((.*)\)$/i', $text, $reg)) {
717 if ($reg[1] ==
'paiement') {
720 $text = $langs->trans($reg[1]);
723 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
724 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$text;
725 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
726 if ($dateofpayment) {
727 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> '.
dol_print_date($dateofpayment,
'dayhour',
'tzuser');
730 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1,
$conf->currency);
734 if (empty($notooltip)) {
736 $label = $langs->trans(
"Payment");
737 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
739 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
740 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
742 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
745 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/fourn/paiement/card.php?id='.$this->
id.
'"';
746 $linkstart .= $linkclose.
'>';
749 $result .= $linkstart;
751 $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);
753 if ($withpicto != 2) {
754 $result .= $this->ref;
759 $hookmanager->initHooks(array($this->element .
'dao'));
760 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
761 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
763 $result = $hookmanager->resPrint;
765 $result .= $hookmanager->resPrint;