101 public function fetch($id, $ref =
'', $fk_bank = 0)
105 $sql =
'SELECT p.rowid, p.ref, p.entity, p.datep as dp, p.amount, p.statut, p.fk_bank, p.multicurrency_amount,';
106 $sql .=
' c.code as payment_code, c.libelle as payment_type,';
107 $sql .=
' p.num_paiement as num_payment, p.note, b.fk_account, p.fk_paiement';
108 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn as p';
109 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
110 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
111 $sql .=
' WHERE p.entity IN ('.getEntity(
'facture_fourn').
')';
113 $sql .=
' AND p.rowid = '.((int) $id);
115 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
116 } elseif ($fk_bank > 0) {
117 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
121 $resql = $this->db->query($sql);
123 $num = $this->db->num_rows($resql);
125 $obj = $this->db->fetch_object($resql);
127 $this->
id = $obj->rowid;
128 $this->
ref = $obj->ref;
129 $this->entity = $obj->entity;
130 $this->date = $this->db->jdate($obj->dp);
131 $this->datepaye = $this->db->jdate($obj->dp);
132 $this->num_payment = $obj->num_payment;
133 $this->bank_account = $obj->fk_account;
134 $this->fk_account = $obj->fk_account;
135 $this->bank_line = $obj->fk_bank;
136 $this->montant = $obj->amount;
137 $this->amount = $obj->amount;
138 $this->multicurrency_amount = $obj->multicurrency_amount;
139 $this->note = $obj->note;
140 $this->note_private = $obj->note;
141 $this->type_code = $obj->payment_code;
142 $this->type_label = $obj->payment_type;
143 $this->fk_paiement = $obj->fk_paiement;
144 $this->
statut = $obj->statut;
150 $this->db->free($resql);
166 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
168 global $langs, $conf;
177 $totalamount_converted = 0;
178 $atleastonepaymentnotnull = 0;
180 if ($way ==
'dolibarr') {
181 $amounts = &$this->amounts;
182 $amounts_to_update = &$this->multicurrency_amounts;
184 $amounts = &$this->multicurrency_amounts;
185 $amounts_to_update = &$this->amounts;
188 $currencyofpayment =
'';
189 $currencytxofpayment =
'';
191 foreach ($amounts as $key => $value) {
198 if ($value_converted ===
false) {
200 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
203 if (empty($currencyofpayment)) {
204 $currencyofpayment = $this->multicurrency_code[$key];
206 if ($currencyofpayment != $this->multicurrency_code[$key]) {
208 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
211 if (empty($currencytxofpayment)) {
212 $currencytxofpayment = $this->multicurrency_tx[$key];
215 $totalamount_converted += $value_converted;
216 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
219 $amounts[$key] = $newvalue;
220 $totalamount += $newvalue;
221 if (!empty($newvalue)) {
222 $atleastonepaymentnotnull++;
226 if (!empty($currencyofpayment)) {
228 $bankaccount =
new Account($this->db);
229 $bankaccount->fetch($this->fk_account);
230 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
231 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) {
232 $langs->load(
"errors");
233 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
240 $totalamount_converted =
price2num($totalamount_converted);
242 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
246 if ($totalamount != 0) {
247 $ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
249 if ($way ==
'dolibarr') {
251 $mtotal = $totalamount_converted;
253 $total = $totalamount_converted;
254 $mtotal = $totalamount;
257 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn (';
258 $sql .=
'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
259 $sql .=
" VALUES ('".$this->db->escape($ref).
"', ".((int) $conf->entity).
", '".$this->db->idate($now).
"',";
260 $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)";
262 $resql = $this->db->query($sql);
264 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiementfourn');
267 foreach ($this->amounts as $key => $amount) {
269 if (is_numeric($amount) && $amount != 0) {
271 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)';
272 $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).
')';
273 $resql = $this->db->query($sql);
276 $invoice->fetch($facid);
279 if ($closepaidinvoices) {
280 $paiement = $invoice->getSommePaiement();
281 $creditnotes=$invoice->getSumCreditNotesUsed();
283 $deposits=$invoice->getSumDepositsUsed();
285 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
286 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
287 if ($remaintopay == 0) {
290 $amount_ht = $amount_tva = $amount_ttc = array();
291 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
294 require_once DOL_DOCUMENT_ROOT .
'/core/class/discount.class.php';
296 $discount->fetch(
'', 0, $invoice->id);
297 if (empty($discount->id)) {
298 $discount->discount_type = 1;
299 $discount->description =
'(DEPOSIT)';
300 $discount->fk_soc = $invoice->socid;
301 $discount->socid = $invoice->socid;
302 $discount->fk_invoice_supplier_source = $invoice->id;
306 foreach ($invoice->lines as $line) {
307 if ($line->total_ht != 0) {
308 $amount_ht[$line->tva_tx] += $line->total_ht;
309 $amount_tva[$line->tva_tx] += $line->total_tva;
310 $amount_ttc[$line->tva_tx] += $line->total_ttc;
311 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
312 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
313 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
318 foreach ($amount_ht as $tva_tx => $xxx) {
319 $discount->amount_ht = abs($amount_ht[$tva_tx]);
320 $discount->amount_tva = abs($amount_tva[$tva_tx]);
321 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
322 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
323 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
324 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
325 $discount->tva_tx = abs($tva_tx);
327 $result = $discount->create($user);
343 $result = $invoice->setPaid($user,
'',
'');
345 $this->error = $invoice->error;
352 $hookmanager->initHooks(array(
'payment_supplierdao'));
353 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
354 $action =
'CLOSEPAIDSUPPLIERINVOICE';
355 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
357 $this->error = $hookmanager->error;
359 } elseif ($reshook == 0) {
360 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
368 $outputlangs = $langs;
370 $invoice->fetch_thirdparty();
371 $newlang = $invoice->thirdparty->default_lang;
373 if (!empty($newlang)) {
375 $outputlangs->setDefaultLang($newlang);
377 $ret = $invoice->fetch($facid);
378 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
385 $this->error = $this->db->lasterror();
389 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
395 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
402 $this->error = $this->db->lasterror();
406 $this->error =
"ErrorTotalIsNull";
407 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
411 if ($totalamount != 0 && $error == 0) {
414 $this->multicurrency_amount = $mtotal;
416 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->amount.
', Total currency = '.$this->multicurrency_amount);
419 $this->db->rollback();
655 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
657 global $langs, $conf, $hookmanager;
659 if (!empty($conf->dol_no_mouse_hover)) {
667 if (preg_match(
'/^\((.*)\)$/i', $text, $reg)) {
669 if ($reg[1] ==
'paiement') {
672 $text = $langs->trans($reg[1]);
675 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
676 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$text;
677 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
678 if ($dateofpayment) {
679 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> '.
dol_print_date($dateofpayment,
'dayhour',
'tzuser');
682 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
686 if (empty($notooltip)) {
688 $label = $langs->trans(
"Payment");
689 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
691 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
692 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
694 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
697 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/fourn/paiement/card.php?id='.$this->
id.
'"';
698 $linkstart .= $linkclose.
'>';
701 $result .= $linkstart;
703 $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);
705 if ($withpicto != 2) {
706 $result .= $this->ref;
711 $hookmanager->initHooks(array($this->element .
'dao'));
712 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
713 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
715 $result = $hookmanager->resPrint;
717 $result .= $hookmanager->resPrint;
755 global $conf, $db, $langs;
756 $langs->load(
"bills");
760 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_bronan';
762 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_brodator';
764 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_bronan';
771 $classname = $conf->global->SUPPLIER_PAYMENT_ADDON;
774 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
776 foreach ($dirmodels as $reldir) {
777 $dir =
dol_buildpath($reldir.
"core/modules/supplier_payment/");
780 if (is_file($dir.$file) && is_readable($dir.$file)) {
781 $mybool |= include_once $dir.$file;
786 if ($mybool ===
false) {
789 $classname = preg_replace(
'/\-.*$/',
'', $classname);
791 foreach ($conf->file->dol_document_root as $dirroot) {
792 $dir = $dirroot.
"/core/modules/supplier_payment/";
795 if (is_file($dir.$file) && is_readable($dir.$file)) {
796 $mybool |= include_once $dir.$file;
801 if ($mybool ===
false) {
806 $obj =
new $classname();
808 $numref = $obj->getNextValue($soc, $this);
814 if ($mode !=
'last' && !$numref) {
821 $langs->load(
"errors");
822 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Supplier"));