103 public function fetch($id, $ref =
'', $fk_bank = 0)
107 $sql =
'SELECT p.rowid, p.ref, p.entity, p.datep as dp, p.amount, p.statut, p.fk_bank, p.multicurrency_amount,';
108 $sql .=
' c.code as payment_code, c.libelle as payment_type,';
109 $sql .=
' p.num_paiement as num_payment, p.note, b.fk_account, p.fk_paiement';
110 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn as p';
111 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
112 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
113 $sql .=
' WHERE p.entity IN ('.getEntity(
'facture_fourn').
')';
115 $sql .=
' AND p.rowid = '.((int) $id);
117 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
118 } elseif ($fk_bank > 0) {
119 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
123 $resql = $this->db->query($sql);
125 $num = $this->db->num_rows($resql);
127 $obj = $this->db->fetch_object($resql);
129 $this->
id = $obj->rowid;
130 $this->
ref = $obj->ref;
131 $this->entity = $obj->entity;
132 $this->date = $this->db->jdate($obj->dp);
133 $this->datepaye = $this->db->jdate($obj->dp);
134 $this->num_payment = $obj->num_payment;
135 $this->bank_account = $obj->fk_account;
136 $this->fk_account = $obj->fk_account;
137 $this->bank_line = $obj->fk_bank;
138 $this->montant = $obj->amount;
139 $this->amount = $obj->amount;
140 $this->multicurrency_amount = $obj->multicurrency_amount;
141 $this->note = $obj->note;
142 $this->note_private = $obj->note;
143 $this->type_code = $obj->payment_code;
144 $this->type_label = $obj->payment_type;
145 $this->fk_paiement = $obj->fk_paiement;
146 $this->
statut = $obj->statut;
152 $this->db->free($resql);
168 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
170 global $langs, $conf;
179 $totalamount_converted = 0;
180 $atleastonepaymentnotnull = 0;
182 if ($way ==
'dolibarr') {
183 $amounts = &$this->amounts;
184 $amounts_to_update = &$this->multicurrency_amounts;
186 $amounts = &$this->multicurrency_amounts;
187 $amounts_to_update = &$this->amounts;
190 $currencyofpayment =
'';
191 $currencytxofpayment =
'';
193 foreach ($amounts as $key => $value) {
200 if ($value_converted ===
false) {
202 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
205 if (empty($currencyofpayment)) {
206 $currencyofpayment = $this->multicurrency_code[$key];
208 if ($currencyofpayment != $this->multicurrency_code[$key]) {
210 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
213 if (empty($currencytxofpayment)) {
214 $currencytxofpayment = $this->multicurrency_tx[$key];
217 $totalamount_converted += $value_converted;
218 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
221 $amounts[$key] = $newvalue;
222 $totalamount += $newvalue;
223 if (!empty($newvalue)) {
224 $atleastonepaymentnotnull++;
228 if (!empty($currencyofpayment)) {
230 $bankaccount =
new Account($this->db);
231 $bankaccount->fetch($this->fk_account);
232 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
233 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) {
234 $langs->load(
"errors");
235 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
241 $totalamount = (float)
price2num($totalamount);
242 $totalamount_converted = (float)
price2num($totalamount_converted);
244 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
248 if ($totalamount != 0) {
249 $ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
251 if ($way ==
'dolibarr') {
253 $mtotal = $totalamount_converted;
255 $total = $totalamount_converted;
256 $mtotal = $totalamount;
259 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn (';
260 $sql .=
'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
261 $sql .=
" VALUES ('".$this->db->escape($ref).
"', ".((int) $conf->entity).
", '".$this->db->idate($now).
"',";
262 $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)";
264 $resql = $this->db->query($sql);
266 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiementfourn');
269 foreach ($this->amounts as $key => $amount) {
271 if (is_numeric($amount) && $amount != 0) {
273 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)';
274 $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).
')';
275 $resql = $this->db->query($sql);
278 $invoice->fetch($facid);
281 if ($closepaidinvoices) {
282 $paiement = $invoice->getSommePaiement();
283 $creditnotes = $invoice->getSumCreditNotesUsed();
285 $deposits = $invoice->getSumDepositsUsed();
287 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
288 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
289 if ($remaintopay == 0) {
292 $amount_ht = $amount_tva = $amount_ttc = array();
293 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
296 require_once DOL_DOCUMENT_ROOT .
'/core/class/discount.class.php';
298 $discount->fetch(
'', 0, $invoice->id);
299 if (empty($discount->id)) {
300 $discount->discount_type = 1;
301 $discount->description =
'(DEPOSIT)';
302 $discount->fk_soc = $invoice->socid;
303 $discount->socid = $invoice->socid;
304 $discount->fk_invoice_supplier_source = $invoice->id;
308 foreach ($invoice->lines as $line) {
309 if ($line->total_ht != 0) {
310 $amount_ht[$line->tva_tx] += $line->total_ht;
311 $amount_tva[$line->tva_tx] += $line->total_tva;
312 $amount_ttc[$line->tva_tx] += $line->total_ttc;
313 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
314 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
315 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
320 foreach ($amount_ht as $tva_tx => $xxx) {
321 $discount->amount_ht = abs($amount_ht[$tva_tx]);
322 $discount->amount_tva = abs($amount_tva[$tva_tx]);
323 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
324 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
325 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
326 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
327 $discount->tva_tx = abs($tva_tx);
329 $result = $discount->create($user);
345 $result = $invoice->setPaid($user,
'',
'');
347 $this->error = $invoice->error;
354 $hookmanager->initHooks(array(
'payment_supplierdao'));
355 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
356 $action =
'CLOSEPAIDSUPPLIERINVOICE';
357 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
359 $this->error = $hookmanager->error;
361 } elseif ($reshook == 0) {
362 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
370 $outputlangs = $langs;
372 $invoice->fetch_thirdparty();
373 $newlang = $invoice->thirdparty->default_lang;
375 if (!empty($newlang)) {
377 $outputlangs->setDefaultLang($newlang);
379 $ret = $invoice->fetch($facid);
380 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
387 $this->error = $this->db->lasterror();
391 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
397 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
404 $this->error = $this->db->lasterror();
408 $this->error =
"ErrorTotalIsNull";
409 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
413 if ($totalamount != 0 && $error == 0) {
416 $this->multicurrency_amount = $mtotal;
418 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->amount.
', Total currency = '.$this->multicurrency_amount);
421 $this->db->rollback();
659 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
661 global $langs, $conf, $hookmanager;
663 if (!empty($conf->dol_no_mouse_hover)) {
671 if (preg_match(
'/^\((.*)\)$/i', $text, $reg)) {
673 if ($reg[1] ==
'paiement') {
676 $text = $langs->trans($reg[1]);
679 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
680 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$text;
681 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
682 if ($dateofpayment) {
683 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> '.
dol_print_date($dateofpayment,
'dayhour',
'tzuser');
686 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
690 if (empty($notooltip)) {
692 $label = $langs->trans(
"Payment");
693 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
695 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
696 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
698 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
701 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/fourn/paiement/card.php?id='.$this->
id.
'"';
702 $linkstart .= $linkclose.
'>';
705 $result .= $linkstart;
707 $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);
709 if ($withpicto != 2) {
710 $result .= $this->ref;
715 $hookmanager->initHooks(array($this->element .
'dao'));
716 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
717 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
719 $result = $hookmanager->resPrint;
721 $result .= $hookmanager->resPrint;