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->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
345 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
346 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
347 $discount->tva_tx = abs((
float) $tva_tx);
349 $result = $discount->create($user);
365 $result = $invoice->setPaid($user,
'',
'');
367 $this->error = $invoice->error;
374 $hookmanager->initHooks(array(
'payment_supplierdao'));
375 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
376 $action =
'CLOSEPAIDSUPPLIERINVOICE';
377 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
379 $this->error = $hookmanager->error;
381 } elseif ($reshook == 0) {
382 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
390 $outputlangs = $langs;
392 $invoice->fetch_thirdparty();
393 $newlang = $invoice->thirdparty->default_lang;
395 if (!empty($newlang)) {
397 $outputlangs->setDefaultLang($newlang);
399 $ret = $invoice->fetch($facid);
400 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
407 $this->error = $this->db->lasterror();
411 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
417 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
424 $this->error = $this->db->lasterror();
428 $this->error =
"ErrorTotalIsNull";
429 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
433 if ($totalamount != 0 && $error == 0) {
434 $this->amount = $total;
435 $this->total = $total;
436 $this->multicurrency_amount = $mtotal;
438 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->amount.
', Total currency = '.$this->multicurrency_amount);
441 $this->db->rollback();
679 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
681 global $langs,
$conf, $hookmanager;
683 if (!empty(
$conf->dol_no_mouse_hover)) {
691 if (preg_match(
'/^\((.*)\)$/i', $text, $reg)) {
693 if ($reg[1] ==
'paiement') {
696 $text = $langs->trans($reg[1]);
699 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
700 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$text;
701 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
702 if ($dateofpayment) {
703 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> '.
dol_print_date($dateofpayment,
'dayhour',
'tzuser');
706 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1,
$conf->currency);
710 if (empty($notooltip)) {
712 $label = $langs->trans(
"Payment");
713 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
715 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
716 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
718 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
721 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/fourn/paiement/card.php?id='.$this->
id.
'"';
722 $linkstart .= $linkclose.
'>';
725 $result .= $linkstart;
727 $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);
729 if ($withpicto != 2) {
730 $result .= $this->ref;
735 $hookmanager->initHooks(array($this->element .
'dao'));
736 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
737 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
739 $result = $hookmanager->resPrint;
741 $result .= $hookmanager->resPrint;