102 public function fetch($id, $ref =
'', $fk_bank = 0)
106 $sql =
'SELECT p.rowid, p.ref, p.entity, p.datep as dp, p.amount, p.statut, p.fk_bank, p.multicurrency_amount,';
107 $sql .=
' c.code as payment_code, c.libelle as payment_type,';
108 $sql .=
' p.num_paiement as num_payment, p.note, b.fk_account, p.fk_paiement';
109 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn as p';
110 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
111 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
112 $sql .=
' WHERE p.entity IN ('.getEntity(
'facture_fourn').
')';
114 $sql .=
' AND p.rowid = '.((int) $id);
116 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
117 } elseif ($fk_bank > 0) {
118 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
122 $resql = $this->db->query($sql);
124 $num = $this->db->num_rows($resql);
126 $obj = $this->db->fetch_object($resql);
128 $this->
id = $obj->rowid;
129 $this->
ref = $obj->ref;
130 $this->entity = $obj->entity;
131 $this->date = $this->db->jdate($obj->dp);
132 $this->datepaye = $this->db->jdate($obj->dp);
133 $this->num_payment = $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) {
199 if ($value_converted ===
false) {
201 $this->error = $langs->trans(
'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);
240 $totalamount = (float)
price2num($totalamount);
241 $totalamount_converted = (float)
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') {
251 $total = $totalamount;
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) ? (float) $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();
294 @phan-var-force array<string,float> $amount_ht
295 @phan-var-force array<string,float> $amount_tva
296 @phan-var-force array<string,float> $amount_ttc
297 @phan-var-force array<string,float> $multicurrency_amount_ht
298 @phan-var-force array<string,float> $multicurrency_amount_tva
299 @phan-var-force array<string,float> $multicurrency_amount_ttc
303 require_once DOL_DOCUMENT_ROOT .
'/core/class/discount.class.php';
305 $discount->fetch(
'', 0, $invoice->id);
306 if (empty($discount->id)) {
307 $discount->discount_type = 1;
308 $discount->description =
'(DEPOSIT)';
309 $discount->fk_soc = $invoice->socid;
310 $discount->socid = $invoice->socid;
311 $discount->fk_invoice_supplier_source = $invoice->id;
315 foreach ($invoice->lines as $line) {
316 if ($line->total_ht != 0) {
317 if (!array_key_exists($line->tva_tx, $amount_ht)) {
318 $amount_ht[$line->tva_tx] = 0.0;
319 $amount_tva[$line->tva_tx] = 0.0;
320 $amount_ttc[$line->tva_tx] = 0.0;
321 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
322 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
323 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
325 $amount_ht[$line->tva_tx] += $line->total_ht;
326 $amount_tva[$line->tva_tx] += $line->total_tva;
327 $amount_ttc[$line->tva_tx] += $line->total_ttc;
328 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
329 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
330 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
335 foreach ($amount_ht as $tva_tx => $xxx) {
336 $discount->amount_ht = abs($amount_ht[$tva_tx]);
337 $discount->amount_tva = abs($amount_tva[$tva_tx]);
338 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
339 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
340 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
341 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
342 $discount->tva_tx = abs((
float) $tva_tx);
344 $result = $discount->create($user);
360 $result = $invoice->setPaid($user,
'',
'');
362 $this->error = $invoice->error;
369 $hookmanager->initHooks(array(
'payment_supplierdao'));
370 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
371 $action =
'CLOSEPAIDSUPPLIERINVOICE';
372 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
374 $this->error = $hookmanager->error;
376 } elseif ($reshook == 0) {
377 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
385 $outputlangs = $langs;
387 $invoice->fetch_thirdparty();
388 $newlang = $invoice->thirdparty->default_lang;
390 if (!empty($newlang)) {
392 $outputlangs->setDefaultLang($newlang);
394 $ret = $invoice->fetch($facid);
395 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
402 $this->error = $this->db->lasterror();
406 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
412 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
419 $this->error = $this->db->lasterror();
423 $this->error =
"ErrorTotalIsNull";
424 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
428 if ($totalamount != 0 && $error == 0) {
429 $this->amount = $total;
430 $this->total = $total;
431 $this->multicurrency_amount = $mtotal;
433 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->amount.
', Total currency = '.$this->multicurrency_amount);
436 $this->db->rollback();
674 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
676 global $langs, $conf, $hookmanager;
678 if (!empty($conf->dol_no_mouse_hover)) {
686 if (preg_match(
'/^\((.*)\)$/i', $text, $reg)) {
688 if ($reg[1] ==
'paiement') {
691 $text = $langs->trans($reg[1]);
694 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
695 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$text;
696 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
697 if ($dateofpayment) {
698 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> '.
dol_print_date($dateofpayment,
'dayhour',
'tzuser');
701 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
705 if (empty($notooltip)) {
707 $label = $langs->trans(
"Payment");
708 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
710 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
711 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
713 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
716 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/fourn/paiement/card.php?id='.$this->
id.
'"';
717 $linkstart .= $linkclose.
'>';
720 $result .= $linkstart;
722 $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);
724 if ($withpicto != 2) {
725 $result .= $this->ref;
730 $hookmanager->initHooks(array($this->element .
'dao'));
731 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
732 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
734 $result = $hookmanager->resPrint;
736 $result .= $hookmanager->resPrint;