179 public function fetch($id, $ref =
'', $fk_bank =
'')
181 $sql =
'SELECT p.rowid, p.ref, p.ref_ext, p.datep as dp, p.amount, p.statut, p.ext_payment_id, p.ext_payment_site, p.fk_bank, p.multicurrency_amount,';
182 $sql .=
' c.code as type_code, c.libelle as type_label,';
183 $sql .=
' p.num_paiement as num_payment, p.note,';
184 $sql .=
' b.fk_account';
185 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement as p LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
186 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
187 $sql .=
' WHERE p.entity IN ('.getEntity(
'invoice').
')';
189 $sql .=
' AND p.rowid = '.((int) $id);
191 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
192 } elseif ($fk_bank) {
193 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
196 $resql = $this->db->query($sql);
198 if ($this->db->num_rows($resql)) {
199 $obj = $this->db->fetch_object($resql);
201 $this->
id = $obj->rowid;
202 $this->
ref = $obj->ref ? $obj->ref : $obj->rowid;
203 $this->ref_ext = $obj->ref_ext;
204 $this->date = $this->db->jdate($obj->dp);
205 $this->datepaye = $this->db->jdate($obj->dp);
206 $this->num_payment = $obj->num_payment;
207 $this->montant = $obj->amount;
208 $this->amount = $obj->amount;
209 $this->multicurrency_amount = $obj->multicurrency_amount;
210 $this->note = $obj->note;
211 $this->note_private = $obj->note;
212 $this->type_label = $obj->type_label;
213 $this->type_code = $obj->type_code;
214 $this->
statut = $obj->statut;
215 $this->ext_payment_id = $obj->ext_payment_id;
216 $this->ext_payment_site = $obj->ext_payment_site;
218 $this->bank_account = $obj->fk_account;
219 $this->fk_account = $obj->fk_account;
220 $this->bank_line = $obj->fk_bank;
222 $this->db->free($resql);
225 $this->db->free($resql);
244 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
246 global $conf, $langs;
255 $totalamount_converted = 0;
256 $atleastonepaymentnotnull = 0;
258 if ($way ==
'dolibarr') {
259 $amounts = &$this->amounts;
260 $amounts_to_update = &$this->multicurrency_amounts;
262 $amounts = &$this->multicurrency_amounts;
263 $amounts_to_update = &$this->amounts;
266 $currencyofpayment =
'';
267 $currencytxofpayment =
'';
269 foreach ($amounts as $key => $value) {
276 if ($value_converted ===
false) {
278 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
281 if (empty($currencyofpayment)) {
282 $currencyofpayment = isset($this->multicurrency_code[$key]) ? $this->multicurrency_code[$key] :
"";
283 } elseif ($currencyofpayment != $this->multicurrency_code[$key]) {
285 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
288 if (empty($currencytxofpayment)) {
289 $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] :
"";
292 $totalamount_converted += $value_converted;
293 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
296 $amounts[$key] = $newvalue;
297 $totalamount += $newvalue;
298 if (!empty($newvalue)) {
299 $atleastonepaymentnotnull++;
303 if (empty($currencyofpayment)) {
304 $currencyofpayment = $conf->currency;
307 if (!empty($currencyofpayment)) {
309 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
310 $bankaccount =
new Account($this->db);
311 $bankaccount->fetch($this->fk_account);
312 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
313 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) {
314 $langs->load(
"errors");
315 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
322 $totalamount_converted =
price2num($totalamount_converted);
325 if (empty($totalamount) && empty($atleastonepaymentnotnull)) {
326 $this->errors[] =
'TotalAmountEmpty';
327 $this->error = $langs->trans(
'TotalAmountEmpty');
331 dol_syslog(get_class($this).
"::create insert paiement", LOG_DEBUG);
335 $this->
ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
337 if (empty($this->ref_ext)) {
341 if ($way ==
'dolibarr') {
343 $mtotal = $totalamount_converted;
345 $total = $totalamount_converted;
346 $mtotal = $totalamount;
349 $num_payment = $this->num_payment;
352 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement (entity, ref, ref_ext, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, ext_payment_id, ext_payment_site, fk_user_creat, pos_change)";
353 $sql .=
" VALUES (".((int) $conf->entity).
", '".$this->db->escape($this->
ref).
"', '".$this->db->escape($this->ref_ext).
"', '".$this->db->idate($now).
"', '".$this->db->idate($this->datepaye).
"', ".((float)
$total).
", ".((float) $mtotal).
", ".((int) $this->paiementid).
", ";
354 $sql .=
"'".$this->db->escape($num_payment).
"', '".$this->db->escape(
$note).
"', ".($this->ext_payment_id ?
"'".$this->db->escape($this->ext_payment_id).
"'" :
"null").
", ".($this->ext_payment_site ?
"'".$this->db->escape($this->ext_payment_site).
"'" :
"null").
", ".((
int) $user->id).
", ".((
float) $this->pos_change).
")";
356 $resql = $this->db->query($sql);
358 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiement');
361 foreach ($this->amounts as $key => $amount) {
363 if (is_numeric($amount) && $amount <> 0) {
365 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)";
366 $sql .=
" VALUES (".((int) $facid).
", ".((int) $this->
id).
", ".((float) $amount).
", ".((float) $this->multicurrency_amounts[$key]).
", ".($currencyofpayment ?
"'".$this->db->escape($currencyofpayment).
"'" :
'NULL').
", ".(!empty($this->multicurrency_tx) ? (double) $currencytxofpayment : 1).
")";
368 dol_syslog(get_class($this).
'::create Amount line '.$key.
' insert paiement_facture', LOG_DEBUG);
369 $resql = $this->db->query($sql);
371 $invoice =
new Facture($this->db);
372 $invoice->fetch($facid);
375 if ($closepaidinvoices) {
376 $paiement = $invoice->getSommePaiement();
377 $creditnotes = $invoice->getSumCreditNotesUsed();
378 $deposits = $invoice->getSumDepositsUsed();
379 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
380 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
385 $affected_types = array(
393 if (!in_array($invoice->type, $affected_types)) {
394 dol_syslog(
"Invoice ".$facid.
" is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
395 } elseif ($remaintopay) {
398 $hookmanager->initHooks(array(
'paymentdao'));
399 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
400 $action =
'CLOSEPAIDINVOICE';
401 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
403 $this->errors[] = $hookmanager->error;
404 $this->error = $hookmanager->error;
406 } elseif ($reshook == 0) {
407 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
413 $amount_ht = $amount_tva = $amount_ttc = array();
414 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
418 $discount->fetch(
'', $invoice->id);
419 if (empty($discount->id)) {
420 $discount->description =
'(DEPOSIT)';
421 $discount->fk_soc = $invoice->socid;
422 $discount->fk_facture_source = $invoice->id;
426 foreach ($invoice->lines as $line) {
427 if ($line->total_ht != 0) {
428 $amount_ht[$line->tva_tx] += $line->total_ht;
429 $amount_tva[$line->tva_tx] += $line->total_tva;
430 $amount_ttc[$line->tva_tx] += $line->total_ttc;
431 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
432 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
433 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
438 foreach ($amount_ht as $tva_tx => $xxx) {
439 $discount->amount_ht = abs($amount_ht[$tva_tx]);
440 $discount->amount_tva = abs($amount_tva[$tva_tx]);
441 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
442 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
443 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
444 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
445 $discount->tva_tx = abs($tva_tx);
447 $result = $discount->create($user);
456 $this->error = $discount->error;
457 $this->errors = $discount->errors;
464 $result = $invoice->setPaid($user,
'',
'');
466 $this->error = $invoice->error;
467 $this->errors = $invoice->errors;
475 if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
476 dol_syslog(get_class($this).
'::create Regenerate the document after inserting payment for thirdparty default_lang='.(is_object($invoice->thirdparty) ? $invoice->thirdparty->default_lang :
'null'), LOG_DEBUG);
479 $outputlangs = $langs;
481 $invoice->fetch_thirdparty();
482 $newlang = $invoice->thirdparty->default_lang;
484 if (!empty($newlang)) {
486 $outputlangs->setDefaultLang($newlang);
489 $hidedetails = !empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0;
490 $hidedesc = !empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0;
491 $hideref = !empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0;
493 $ret = $invoice->fetch($facid);
495 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
498 $this->error = $invoice->error;
499 $this->errors = $invoice->errors;
504 $this->error = $this->db->lasterror();
508 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
514 $result = $this->
call_trigger(
'PAYMENT_CUSTOMER_CREATE', $user);
521 $this->error = $this->db->lasterror();
528 $this->multicurrency_amount = $mtotal;
532 $this->db->rollback();
646 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger = 0, $accountancycode =
'')
648 global $conf, $langs, $user;
653 if (isModEnabled(
"banque")) {
654 if ($accountid <= 0) {
655 $this->error =
'Bad value for parameter accountid='.$accountid;
656 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->error, LOG_ERR);
660 $this->fk_account = $accountid;
662 dol_syslog(
"addPaymentToBank ".$user->id.
", ".$mode.
", ".$label.
", ".$this->fk_account.
", ".$emetteur_nom.
", ".$emetteur_banque);
664 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
666 $result = $acc->fetch($this->fk_account);
668 $this->error = $acc->error;
669 $this->errors = $acc->errors;
676 $totalamount = $this->amount;
677 $totalamount_main_currency =
null;
678 if (empty($totalamount)) {
683 if (isModEnabled(
'multicurrency') && $conf->currency != $acc->currency_code) {
684 $totalamount = $this->multicurrency_amount;
685 $totalamount_main_currency = $this->amount;
688 if ($mode ==
'payment_supplier') {
689 $totalamount = -$totalamount;
690 if (isset($totalamount_main_currency)) {
691 $totalamount_main_currency = -$totalamount_main_currency;
696 $bank_line_id = $acc->addline(
698 $this->paiementcode ? $this->paiementcode : $this->paiementid,
709 $totalamount_main_currency
714 if ($bank_line_id > 0) {
724 if ($mode ==
'payment') {
725 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id=';
727 if ($mode ==
'payment_supplier') {
728 $url = DOL_URL_ROOT.
'/fourn/paiement/card.php?id=';
731 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
742 $linkaddedforthirdparty = array();
743 foreach ($this->amounts as $key => $value) {
744 if ($mode ==
'payment') {
747 $fac->fetch_thirdparty();
748 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
749 $result = $acc->add_url_line(
751 $fac->thirdparty->id,
752 DOL_URL_ROOT.
'/comm/card.php?socid=',
753 $fac->thirdparty->name,
757 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
759 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
762 if ($mode ==
'payment_supplier') {
765 $fac->fetch_thirdparty();
766 if (!in_array($fac->thirdparty->id, $linkaddedforthirdparty)) {
767 $result = $acc->add_url_line(
769 $fac->thirdparty->id,
770 DOL_URL_ROOT.
'/fourn/card.php?socid=',
771 $fac->thirdparty->name,
775 dol_syslog(get_class($this).
'::addPaymentToBank '.$this->db->lasterror());
777 $linkaddedforthirdparty[$fac->thirdparty->id] = $fac->thirdparty->id;
784 if (!$error && $label ==
'(WithdrawalPayment)') {
785 $result = $acc->add_url_line(
787 $this->id_prelevement,
788 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
795 if (!$error && $label ==
'(InvoiceRefused)') {
796 $result=$acc->add_url_line(
798 $this->id_prelevement,
799 DOL_URL_ROOT.
'/compta/prelevement/card.php?id=',
800 $this->num_prelevement,
805 if (!$error && !$notrigger) {
807 $result = $this->
call_trigger(
'PAYMENT_ADD_TO_BANK', $user);
814 $this->error = $acc->error;
815 $this->errors = $acc->errors;
822 $this->db->rollback();
827 return $bank_line_id;
1111 global $conf, $db, $langs;
1112 $langs->load(
"bills");
1115 if (empty($conf->global->PAYMENT_ADDON)) {
1116 $conf->global->PAYMENT_ADDON =
'mod_payment_cicada';
1117 } elseif ($conf->global->PAYMENT_ADDON ==
'ant') {
1118 $conf->global->PAYMENT_ADDON =
'mod_payment_ant';
1119 } elseif ($conf->global->PAYMENT_ADDON ==
'cicada') {
1120 $conf->global->PAYMENT_ADDON =
'mod_payment_cicada';
1123 if (!empty($conf->global->PAYMENT_ADDON)) {
1126 $file = $conf->global->PAYMENT_ADDON.
".php";
1127 $classname = $conf->global->PAYMENT_ADDON;
1130 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
1132 foreach ($dirmodels as $reldir) {
1136 if (is_file($dir.$file) && is_readable($dir.$file)) {
1137 $mybool |= include_once $dir.$file;
1143 $file = $conf->global->PAYMENT_ADDON.
".php";
1144 $classname =
"mod_payment_".$conf->global->PAYMENT_ADDON;
1145 $classname = preg_replace(
'/\-.*$/',
'', $classname);
1147 foreach ($conf->file->dol_document_root as $dirroot) {
1148 $dir = $dirroot.
"/core/modules/payment/";
1151 if (is_file($dir.$file) && is_readable($dir.$file)) {
1152 $mybool |= include_once $dir.$file;
1162 $obj =
new $classname();
1164 $numref = $obj->getNextValue($soc, $this);
1170 if ($mode !=
'last' && !$numref) {
1177 $langs->load(
"errors");
1178 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Invoice"));
1240 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
1242 global $conf, $langs, $hookmanager;
1244 if (!empty($conf->dol_no_mouse_hover)) {
1250 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
1251 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$this->ref;
1252 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
1253 if ($dateofpayment) {
1254 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> ';
1256 if ($tmparray[
'seconds'] == 0 && $tmparray[
'minutes'] == 0 && ($tmparray[
'hours'] == 0 || $tmparray[
'hours'] == 12)) {
1262 if ($this->amount) {
1263 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
1265 if ($mode ==
'withlistofinvoices') {
1267 if (is_array($arraybill) && count($arraybill) > 0) {
1268 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1269 $facturestatic =
new Facture($this->db);
1270 foreach ($arraybill as $billid) {
1271 $facturestatic->fetch($billid);
1272 $label .=
'<br> '.$facturestatic->getNomUrl(1,
'', 0, 0,
'', 1).
' '.$facturestatic->getLibStatut(2, -1);
1278 if (empty($notooltip)) {
1279 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1280 $label = $langs->trans(
"Payment");
1281 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1283 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1284 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1286 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1289 $url = DOL_URL_ROOT.
'/compta/paiement/card.php?id='.$this->id;
1291 $linkstart =
'<a href="'.$url.
'"';
1292 $linkstart .= $linkclose.
'>';
1295 $result .= $linkstart;
1297 $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);
1299 if ($withpicto && $withpicto != 2) {
1300 $result .= ($this->
ref ? $this->
ref : $this->id);
1302 $result .= $linkend;
1304 $hookmanager->initHooks(array($this->element .
'dao'));
1305 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1306 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1308 $result = $hookmanager->resPrint;
1310 $result .= $hookmanager->resPrint;