31 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
43 public $element =
'payment_supplier';
48 public $table_element =
'paiementfourn';
53 public $picto =
'payment';
74 public $id_prelevement;
79 public $num_prelevement;
100 public function fetch($id, $ref =
'', $fk_bank =
'')
104 $sql =
'SELECT p.rowid, p.ref, p.entity, p.datep as dp, p.amount, p.statut, p.fk_bank, p.multicurrency_amount,';
105 $sql .=
' c.code as payment_code, c.libelle as payment_type,';
106 $sql .=
' p.num_paiement as num_payment, p.note, b.fk_account, p.fk_paiement';
107 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn as p';
108 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
109 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON p.fk_bank = b.rowid';
110 $sql .=
' WHERE p.entity IN ('.getEntity(
'facture_fourn').
')';
112 $sql .=
' AND p.rowid = '.((int) $id);
114 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
115 } elseif ($fk_bank > 0) {
116 $sql .=
' AND p.fk_bank = '.((int) $fk_bank);
124 $obj = $this->
db->fetch_object(
$resql);
126 $this->
id = $obj->rowid;
127 $this->
ref = $obj->ref;
128 $this->entity = $obj->entity;
129 $this->date = $this->
db->jdate($obj->dp);
130 $this->datepaye = $this->
db->jdate($obj->dp);
131 $this->num_payment = $obj->num_payment;
132 $this->numero = $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;
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 =
'';
190 foreach ($amounts as $key => $value) {
195 $value_converted = Multicurrency::getAmountConversionFromInvoiceRate($key, $value ? $value : 0, $way,
'facture_fourn');
197 if ($value_converted ===
false) {
199 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
202 if (empty($currencyofpayment)) {
203 $currencyofpayment = $this->multicurrency_code[$key];
205 if ($currencyofpayment != $this->multicurrency_code[$key]) {
207 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
211 $totalamount_converted += $value_converted;
212 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
215 $amounts[$key] = $newvalue;
216 $totalamount += $newvalue;
217 if (!empty($newvalue)) {
218 $atleastonepaymentnotnull++;
222 if (!empty($currencyofpayment)) {
225 $bankaccount->fetch($this->fk_account);
226 $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code;
227 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) {
228 $langs->load(
"errors");
229 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
236 $totalamount_converted =
price2num($totalamount_converted);
238 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
242 if ($totalamount <> 0) {
243 $ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
245 if ($way ==
'dolibarr') {
247 $mtotal = $totalamount_converted;
249 $total = $totalamount_converted;
250 $mtotal = $totalamount;
253 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn (';
254 $sql .=
'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
255 $sql .=
" VALUES ('".$this->db->escape($ref).
"', ".((int) $conf->entity).
", '".$this->
db->idate($now).
"',";
256 $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)";
260 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'paiementfourn');
263 foreach ($this->amounts as $key => $amount) {
265 if (is_numeric($amount) && $amount <> 0) {
267 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount)';
268 $sql .=
" VALUES (".((int) $facid).
", ".((int) $this->
id).
", ".((
float) $amount).
', '.((
float) $this->multicurrency_amounts[$key]).
')';
272 $invoice->fetch($facid);
275 if ($closepaidinvoices) {
276 $paiement = $invoice->getSommePaiement();
277 $creditnotes=$invoice->getSumCreditNotesUsed();
279 $deposits=$invoice->getSumDepositsUsed();
281 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
282 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
283 if ($remaintopay == 0) {
286 $amount_ht = $amount_tva = $amount_ttc = array();
287 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
290 require_once DOL_DOCUMENT_ROOT .
'/core/class/discount.class.php';
292 $discount->fetch(
'', 0, $invoice->id);
293 if (empty($discount->id)) {
294 $discount->discount_type = 1;
295 $discount->description =
'(DEPOSIT)';
296 $discount->fk_soc = $invoice->socid;
297 $discount->fk_invoice_supplier_source = $invoice->id;
301 foreach ($invoice->lines as $line) {
302 if ($line->total_ht != 0) {
303 $amount_ht[$line->tva_tx] += $line->total_ht;
304 $amount_tva[$line->tva_tx] += $line->total_tva;
305 $amount_ttc[$line->tva_tx] += $line->total_ttc;
306 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
307 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
308 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
313 foreach ($amount_ht as $tva_tx => $xxx) {
314 $discount->amount_ht = abs($amount_ht[$tva_tx]);
315 $discount->amount_tva = abs($amount_tva[$tva_tx]);
316 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
317 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
318 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
319 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
320 $discount->tva_tx = abs($tva_tx);
322 $result = $discount->create($user);
338 $result = $invoice->setPaid($user,
'',
'');
340 $this->error = $invoice->error;
347 $hookmanager->initHooks(array(
'payment_supplierdao'));
348 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
349 $action =
'CLOSEPAIDSUPPLIERINVOICE';
350 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
352 $this->error = $hookmanager->error;
354 } elseif ($reshook == 0) {
355 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
361 if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
363 $outputlangs = $langs;
365 $newlang = $invoice->thirdparty->default_lang;
367 if (!empty($newlang)) {
369 $outputlangs->setDefaultLang($newlang);
371 $ret = $invoice->fetch($facid);
372 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
379 $this->error = $this->
db->lasterror();
383 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
389 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
396 $this->error = $this->
db->lasterror();
400 $this->error =
"ErrorTotalIsNull";
401 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
405 if ($totalamount <> 0 && $error == 0) {
408 $this->multicurrency_amount = $mtotal;
410 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->amount.
', Total currency = '.$this->multicurrency_amount);
413 $this->
db->rollback();
427 public function delete($notrigger = 0)
429 global $conf, $user, $langs;
431 $bank_line_id = $this->bank_line;
438 if (is_array($billsarray)) {
439 if (count($billsarray)) {
440 $this->error =
"ErrorCantDeletePaymentSharedWithPayedInvoice";
441 $this->
db->rollback();
445 $this->
db->rollback();
453 $accline->fetch($bank_line_id);
454 if ($accline->rappro) {
455 $this->error =
"ErrorCantDeletePaymentReconciliated";
456 $this->
db->rollback();
462 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn';
463 $sql .=
' WHERE fk_paiementfourn = '.((int) $this->
id);
466 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'paiementfourn';
467 $sql .=
" WHERE rowid = ".((int) $this->
id);
468 $result = $this->
db->query($sql);
470 $this->error = $this->
db->error();
471 $this->
db->rollback();
478 $result = $accline->fetch($bank_line_id);
480 $result = $accline->delete($user);
483 $this->error = $accline->error;
484 $this->
db->rollback();
491 $result = $this->
call_trigger(
'PAYMENT_SUPPLIER_DELETE', $user);
493 $this->
db->rollback();
502 $this->error = $this->
db->error;
503 $this->
db->rollback();
516 $sql =
'SELECT c.rowid, datec, fk_user_author as fk_user_creat, tms';
517 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn as c';
518 $sql .=
' WHERE c.rowid = '.((int) $id);
524 $obj = $this->
db->fetch_object(
$resql);
525 $this->
id = $obj->rowid;
527 if ($obj->fk_user_creat) {
528 $cuser =
new User($this->
db);
529 $cuser->fetch($obj->fk_user_creat);
530 $this->user_creation = $cuser;
532 if ($obj->fk_user_modif) {
533 $muser =
new User($this->
db);
534 $muser->fetch($obj->fk_user_modif);
535 $this->user_modification = $muser;
537 $this->date_creation = $this->
db->jdate($obj->datec);
538 $this->date_modification = $this->
db->jdate($obj->tms);
554 $sql =
'SELECT fk_facturefourn';
555 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn as pf, '.MAIN_DB_PREFIX.
'facture_fourn as f';
556 $sql .=
' WHERE pf.fk_facturefourn = f.rowid AND fk_paiementfourn = '.((int) $this->
id);
558 $sql .=
" AND ".$filter;
561 dol_syslog(get_class($this).
'::getBillsArray', LOG_DEBUG);
566 $billsarray = array();
569 $obj = $this->
db->fetch_object(
$resql);
570 $billsarray[$i] = $obj->fk_facturefourn;
576 $this->error = $this->
db->error();
577 dol_syslog(get_class($this).
'::getBillsArray Error '.$this->error);
590 return $this->
LibStatut($this->statut, $mode);
606 $langs->load(
'compta');
656 public function getNomUrl($withpicto = 0, $option =
'', $mode =
'withlistofinvoices', $notooltip = 0, $morecss =
'')
658 global $langs, $conf, $hookmanager;
660 if (!empty($conf->dol_no_mouse_hover)) {
668 if (preg_match(
'/^\((.*)\)$/i', $text, $reg)) {
670 if ($reg[1] ==
'paiement') {
673 $text = $langs->trans($reg[1]);
676 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Payment").
'</u><br>';
677 $label .=
'<strong>'.$langs->trans(
"Ref").
':</strong> '.$text;
678 $dateofpayment = ($this->datepaye ? $this->datepaye : $this->date);
679 if ($dateofpayment) {
680 $label .=
'<br><strong>'.$langs->trans(
"Date").
':</strong> '.
dol_print_date($dateofpayment,
'dayhour',
'tzuser');
683 $label .=
'<br><strong>'.$langs->trans(
"Amount").
':</strong> '.
price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
687 if (empty($notooltip)) {
688 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
689 $label = $langs->trans(
"Payment");
690 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
692 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
693 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
695 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
698 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/fourn/paiement/card.php?id='.$this->
id.
'"';
699 $linkstart .= $linkclose.
'>';
702 $result .= $linkstart;
704 $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);
706 if ($withpicto != 2) {
707 $result .= $this->ref;
712 $hookmanager->initHooks(array($this->element .
'dao'));
713 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
714 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
716 $result = $hookmanager->resPrint;
718 $result .= $hookmanager->resPrint;
733 global $user, $langs, $conf;
737 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
741 $this->
ref =
'SPECIMEN';
745 $this->datepaye = $nownotime;
758 global $conf, $db, $langs;
759 $langs->load(
"bills");
762 if (empty($conf->global->SUPPLIER_PAYMENT_ADDON)) {
763 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_bronan';
764 } elseif ($conf->global->SUPPLIER_PAYMENT_ADDON ==
'brodator') {
765 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_brodator';
766 } elseif ($conf->global->SUPPLIER_PAYMENT_ADDON ==
'bronan') {
767 $conf->global->SUPPLIER_PAYMENT_ADDON =
'mod_supplier_payment_bronan';
770 if (!empty($conf->global->SUPPLIER_PAYMENT_ADDON)) {
773 $file = $conf->global->SUPPLIER_PAYMENT_ADDON.
".php";
774 $classname = $conf->global->SUPPLIER_PAYMENT_ADDON;
777 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
779 foreach ($dirmodels as $reldir) {
780 $dir =
dol_buildpath($reldir.
"core/modules/supplier_payment/");
783 if (is_file($dir.$file) && is_readable($dir.$file)) {
784 $mybool |= include_once $dir.$file;
789 if ($mybool ===
false) {
790 $file = $conf->global->SUPPLIER_PAYMENT_ADDON.
".php";
791 $classname =
"mod_supplier_payment_".$conf->global->SUPPLIER_PAYMENT_ADDON;
792 $classname = preg_replace(
'/\-.*$/',
'', $classname);
794 foreach ($conf->file->dol_document_root as $dirroot) {
795 $dir = $dirroot.
"/core/modules/supplier_payment/";
798 if (is_file($dir.$file) && is_readable($dir.$file)) {
799 $mybool |= include_once $dir.$file;
804 if ($mybool ===
false) {
809 $obj =
new $classname();
811 $numref = $obj->getNextValue($soc, $this);
817 if ($mode !=
'last' && !$numref) {
824 $langs->load(
"errors");
825 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Supplier"));
841 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
843 global $conf, $user, $langs;
845 $langs->load(
"suppliers");
848 if (empty($modele)) {
849 if (!empty($conf->global->SUPPLIER_PAYMENT_ADDON_PDF)) {
850 $modele = $conf->global->SUPPLIER_PAYMENT_ADDON_PDF;
856 if (empty($modele)) {
859 $modelpath =
"core/modules/supplier_payment/doc/";
861 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
878 foreach ($this->multicurrency_amounts as $value) {
879 if (!empty($value)) {
900 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
902 if (empty($force_thirdparty_id)) {
904 if (!empty($billsarray)) {
906 if ($supplier_invoice->fetch($billsarray[0]) > 0) {
907 $force_thirdparty_id = $supplier_invoice->fk_soc;
912 return parent::fetch_thirdparty($force_thirdparty_id);
Class to manage bank accounts.
Class to manage bank transaction lines.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage absolute discounts.
Class to manage suppliers invoices.
const TYPE_DEPOSIT
Deposit invoice.
Class to manage payments for supplier invoices.
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
getNextNumRef($soc, $mode='next')
Return next reference of supplier invoice not already used (or last reference) according to numbering...
initAsSpecimen($option='')
Initialise an instance with random values.
getLibStatut($mode=0)
Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
__construct($db)
Constructor.
info($id)
Information on object.
create($user, $closepaidinvoices=0, $thirdparty=null)
Create payment in database.
getBillsArray($filter='')
Return list of supplier invoices the payment point to.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template model.
getNomUrl($withpicto=0, $option='', $mode='withlistofinvoices', $notooltip=0, $morecss='')
Return clicable name (with picto eventually)
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id into this->thirdparty.
getWay()
get the right way of payment
fetch($id, $ref='', $fk_bank='')
Load payment object.
Class to manage payments of customer invoices.
Class to manage translations.
Class to manage Dolibarr users.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
div float
Buy price without taxes.
$conf db
API class for accounts.