dolibarr  9.0.0
paiement.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
5  * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
6  * Copyright (C) 2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
7  * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
8  * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
9  * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
10  * Copyright (C) 2018 Thibault FOUCART <support@ptibogxiv.net>
11  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
32 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
33 require_once DOL_DOCUMENT_ROOT .'/multicurrency/class/multicurrency.class.php';
34 
35 
39 class Paiement extends CommonObject
40 {
44  public $element='payment';
45 
49  public $table_element='paiement';
50 
54  public $picto = 'payment';
55 
56  public $facid;
57  public $datepaye;
58 
63  public $total;
64 
69  public $montant;
70 
71  public $amount; // Total amount of payment
72  public $amounts=array(); // Array of amounts
73  public $multicurrency_amounts=array(); // Array of amounts
74  public $author;
75  public $paiementid; // Type de paiement. Stocke dans fk_paiement
76  // de llx_paiement qui est lie aux types de
77  //paiement de llx_c_paiement
78 
82  public $type_libelle;
83 
87  public $type_code;
88 
94  public $numero;
95 
101  public $num_paiement;
102 
106  public $num_payment;
107 
111  public $ext_payment_id;
112 
116  public $ext_payment_site;
117 
122  public $bank_account;
123 
127  public $fk_account;
128 
132  public $bank_line;
133 
134  // fk_paiement dans llx_paiement est l'id du type de paiement (7 pour CHQ, ...)
135  // fk_paiement dans llx_paiement_facture est le rowid du paiement
139  public $fk_paiement; // Type of payment
140 
141 
147  public function __construct($db)
148  {
149  $this->db = $db;
150  }
151 
160  public function fetch($id, $ref='', $fk_bank='')
161  {
162  $sql = 'SELECT p.rowid, p.ref, p.datep as dp, p.amount, p.statut, p.ext_payment_id, p.ext_payment_site, p.fk_bank,';
163  $sql.= ' c.code as type_code, c.libelle as type_libelle,';
164  $sql.= ' p.num_paiement as num_payment, p.note,';
165  $sql.= ' b.fk_account';
166  $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
167  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
168  $sql.= ' WHERE p.entity IN (' . getEntity('facture').')';
169  if ($id > 0)
170  $sql.= ' AND p.rowid = '.$id;
171  else if ($ref)
172  $sql.= " AND p.ref = '".$ref."'";
173  else if ($fk_bank)
174  $sql.= ' AND p.fk_bank = '.$fk_bank;
175 
176  $resql = $this->db->query($sql);
177  if ($resql)
178  {
179  if ($this->db->num_rows($resql))
180  {
181  $obj = $this->db->fetch_object($resql);
182  $this->id = $obj->rowid;
183  $this->ref = $obj->ref?$obj->ref:$obj->rowid;
184  $this->date = $this->db->jdate($obj->dp);
185  $this->datepaye = $this->db->jdate($obj->dp);
186  $this->numero = $obj->num_payment; // deprecated
187  $this->num_paiement = $obj->num_payment; // deprecated
188  $this->num_payment = $obj->num_payment;
189  $this->montant = $obj->amount; // deprecated
190  $this->amount = $obj->amount;
191  $this->note = $obj->note;
192  $this->type_libelle = $obj->type_libelle;
193  $this->type_code = $obj->type_code;
194  $this->statut = $obj->statut;
195  $this->ext_payment_id = $obj->ext_payment_id;
196  $this->ext_payment_site = $obj->ext_payment_site;
197 
198  $this->bank_account = $obj->fk_account; // deprecated
199  $this->fk_account = $obj->fk_account;
200  $this->bank_line = $obj->fk_bank;
201 
202  $this->db->free($resql);
203  return 1;
204  }
205  else
206  {
207  $this->db->free($resql);
208  return 0;
209  }
210  }
211  else
212  {
213  dol_print_error($this->db);
214  return -1;
215  }
216  }
217 
227  function create($user, $closepaidinvoices=0)
228  {
229  global $conf, $langs;
230 
231  $error = 0;
232  $way = $this->getWay();
233 
234  $now=dol_now();
235 
236  // Clean parameters
237  $totalamount = 0;
238  $totalamount_converted = 0;
239  $atleastonepaymentnotnull = 0;
240 
241  if ($way == 'dolibarr')
242  {
243  $amounts = &$this->amounts;
244  $amounts_to_update = &$this->multicurrency_amounts;
245  }
246  else
247  {
248  $amounts = &$this->multicurrency_amounts;
249  $amounts_to_update = &$this->amounts;
250  }
251 
252  foreach ($amounts as $key => $value) // How payment is dispatch
253  {
254  $value_converted = Multicurrency::getAmountConversionFromInvoiceRate($key, $value, $way);
255  $totalamount_converted += $value_converted;
256  $amounts_to_update[$key] = price2num($value_converted, 'MT');
257 
258  $newvalue = price2num($value,'MT');
259  $amounts[$key] = $newvalue;
260  $totalamount += $newvalue;
261  if (! empty($newvalue)) $atleastonepaymentnotnull++;
262  }
263 
264  $totalamount = price2num($totalamount);
265  $totalamount_converted = price2num($totalamount_converted);
266 
267  // Check parameters
268  if (empty($totalamount) && empty($atleastonepaymentnotnull)) // We accept negative amounts for withdraw reject but not empty arrays
269  {
270  $this->errors[]='TotalAmountEmpty';
271  $this->error='TotalAmountEmpty';
272  return -1;
273  }
274 
275  $this->db->begin();
276 
277  $this->ref = $this->getNextNumRef('');
278 
279  if ($way == 'dolibarr')
280  {
281  $total = $totalamount;
282  $mtotal = $totalamount_converted; // Maybe use price2num with MT for the converted value
283  }
284  else
285  {
286  $total = $totalamount_converted; // Maybe use price2num with MT for the converted value
287  $mtotal = $totalamount;
288  }
289  $note = ($this->note_public?$this->note_public:$this->note);
290 
291  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement (entity, ref, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, ext_payment_id, ext_payment_site, fk_user_creat)";
292  $sql.= " VALUES (".$conf->entity.", '".$this->db->escape($this->ref)."', '". $this->db->idate($now)."', '".$this->db->idate($this->datepaye)."', ".$total.", ".$mtotal.", ".$this->paiementid.", '".$this->db->escape($this->num_paiement)."', '".$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").", ".$user->id.")";
293 
294  dol_syslog(get_class($this)."::Create insert paiement", LOG_DEBUG);
295  $resql = $this->db->query($sql);
296  if ($resql)
297  {
298  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'paiement');
299 
300  // Insert links amount / invoices
301  foreach ($this->amounts as $key => $amount)
302  {
303  $facid = $key;
304  if (is_numeric($amount) && $amount <> 0)
305  {
306  $amount = price2num($amount);
307  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount)';
308  $sql .= ' VALUES ('.$facid.', '. $this->id.', \''.$amount.'\', \''.$this->multicurrency_amounts[$key].'\')';
309 
310  dol_syslog(get_class($this).'::Create Amount line '.$key.' insert paiement_facture', LOG_DEBUG);
311  $resql=$this->db->query($sql);
312  if ($resql)
313  {
314  $invoice=new Facture($this->db);
315  $invoice->fetch($facid);
316 
317  // If we want to closed payed invoices
318  if ($closepaidinvoices)
319  {
320  $paiement = $invoice->getSommePaiement();
321  $creditnotes=$invoice->getSumCreditNotesUsed();
322  $deposits=$invoice->getSumDepositsUsed();
323  $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
324  $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
325 
326  //var_dump($invoice->total_ttc.' - '.$paiement.' -'.$creditnotes.' - '.$deposits.' - '.$remaintopay);exit;
327 
328  /* Why this ? We can remove i think.
329  // If there is withdrawals request to do and not done yet on the invoice the payment is on, we wait before closing.
330  $mustwait=0;
331  $sqlrequest ="SELECT COUNT(rowid) FROM ".MAIN_DB_PREFIX."prelevement_facture_demande";
332  $sqlrequest.="WHERE fk_facture = ".$invoice->id." AND traite = 0";
333  ...
334 
335  $listofpayments=$invoice->getListOfPayments();
336  foreach($listofpayments as $paym)
337  {
338  // This payment on invoice $invoice might be the one we record or another one
339  if ($paym['type']=='PRE')
340  {
341  if (! empty($conf->prelevement->enabled))
342  {
343  // if not, $mustwait++; // This will disable automatic close on invoice to allow to process
344 
345  }
346  }
347  }
348  */
349 
350  //Invoice types that are eligible for changing status to paid
351  $affected_types = array(
352  Facture::TYPE_STANDARD,
353  Facture::TYPE_REPLACEMENT,
354  Facture::TYPE_CREDIT_NOTE,
355  Facture::TYPE_DEPOSIT,
356  Facture::TYPE_SITUATION
357  );
358 
359  if (!in_array($invoice->type, $affected_types)) dol_syslog("Invoice ".$facid." is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
360  else if ($remaintopay) dol_syslog("Remain to pay for invoice ".$facid." not null. We do nothing more.");
361  //else if ($mustwait) dol_syslog("There is ".$mustwait." differed payment to process, we do nothing more.");
362  else
363  {
364  // If invoice is a down payment, we also convert down payment to discount
365  if ($invoice->type == Facture::TYPE_DEPOSIT)
366  {
367  $amount_ht = $amount_tva = $amount_ttc = array();
368 
369  // Insert one discount by VAT rate category
370  $discount = new DiscountAbsolute($this->db);
371  $discount->fetch('',$invoice->id);
372  if (empty($discount->id)) { // If the invoice was not yet converted into a discount (this may have been done manually before we come here)
373 
374 
375  $discount->description = '(DEPOSIT)';
376  $discount->fk_soc = $invoice->socid;
377  $discount->fk_facture_source = $invoice->id;
378 
379  // Loop on each vat rate
380  $i = 0;
381  foreach ($invoice->lines as $line) {
382  if ($line->total_ht != 0) { // no need to create discount if amount is null
383  $amount_ht[$line->tva_tx] += $line->total_ht;
384  $amount_tva[$line->tva_tx] += $line->total_tva;
385  $amount_ttc[$line->tva_tx] += $line->total_ttc;
386  $i++;
387  }
388  }
389 
390  foreach ($amount_ht as $tva_tx => $xxx) {
391  $discount->amount_ht = abs($amount_ht[$tva_tx]);
392  $discount->amount_tva = abs($amount_tva[$tva_tx]);
393  $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
394  $discount->tva_tx = abs($tva_tx);
395 
396  $result = $discount->create($user);
397  if ($result < 0) {
398  $error++;
399  break;
400  }
401  }
402  }
403 
404  if ($error)
405  {
406  setEventMessages($discount->error, $discount->errors, 'errors');
407  $error++;
408  }
409  }
410 
411  // Set invoice to paid
412  if (! $error)
413  {
414  $result=$invoice->set_paid($user,'','');
415  if ($result<0)
416  {
417  $this->error=$invoice->error;
418  $error++;
419  }
420  }
421  }
422  }
423 
424  // Regenerate documents of invoices
425  if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
426  {
427  $outputlangs = $langs;
428  if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $invoice->thirdparty->default_lang;
429  if (! empty($newlang)) {
430  $outputlangs = new Translate("", $conf);
431  $outputlangs->setDefaultLang($newlang);
432  }
433  $ret = $invoice->fetch($facid); // Reload to get new records
434  $result = $invoice->generateDocument($invoice->modelpdf, $outputlangs);
435  if ($result < 0) {
436  setEventMessages($invoice->error, $invoice->errors, 'errors');
437  $error++;
438  }
439  }
440  }
441  else
442  {
443  $this->error=$this->db->lasterror();
444  $error++;
445  }
446  }
447  else
448  {
449  dol_syslog(get_class($this).'::Create Amount line '.$key.' not a number. We discard it.');
450  }
451  }
452 
453  if (! $error)
454  {
455  // Appel des triggers
456  $result=$this->call_trigger('PAYMENT_CUSTOMER_CREATE', $user);
457  if ($result < 0) { $error++; }
458  // Fin appel triggers
459  }
460  }
461  else
462  {
463  $this->error=$this->db->lasterror();
464  $error++;
465  }
466 
467  if (! $error)
468  {
469  $this->amount=$total;
470  $this->total=$total; // deprecated
471  $this->multicurrency_amount=$mtotal;
472  $this->db->commit();
473  return $this->id;
474  }
475  else
476  {
477  $this->db->rollback();
478  return -1;
479  }
480  }
481 
482 
491  function delete($notrigger=0)
492  {
493  global $conf, $user, $langs;
494 
495  $error=0;
496 
497  $bank_line_id = $this->bank_line;
498 
499  $this->db->begin();
500 
501  // Verifier si paiement porte pas sur une facture classee
502  // Si c'est le cas, on refuse la suppression
503  $billsarray=$this->getBillsArray('fk_statut > 1');
504  if (is_array($billsarray))
505  {
506  if (count($billsarray))
507  {
508  $this->error="ErrorDeletePaymentLinkedToAClosedInvoiceNotPossible";
509  $this->db->rollback();
510  return -1;
511  }
512  }
513  else
514  {
515  $this->db->rollback();
516  return -2;
517  }
518 
519  // Delete bank urls. If payment is on a conciliated line, return error.
520  if ($bank_line_id > 0)
521  {
522  $accline = new AccountLine($this->db);
523 
524  $result=$accline->fetch($bank_line_id);
525  if ($result == 0) $accline->rowid=$bank_line_id; // If not found, we set artificially rowid to allow delete of llx_bank_url
526 
527  // Delete bank account url lines linked to payment
528  $result=$accline->delete_urls($user);
529  if ($result < 0)
530  {
531  $this->error=$accline->error;
532  $this->db->rollback();
533  return -3;
534  }
535 
536  // Delete bank account lines linked to payment
537  $result=$accline->delete($user);
538  if ($result < 0)
539  {
540  $this->error=$accline->error;
541  $this->db->rollback();
542  return -4;
543  }
544  }
545 
546  if (! $notrigger)
547  {
548  // Call triggers
549  $result=$this->call_trigger('PAYMENT_CUSTOMER_DELETE', $user);
550  if ($result < 0)
551  {
552  $this->db->rollback();
553  return -1;
554  }
555  // End call triggers
556  }
557 
558  // Delete payment (into paiement_facture and paiement)
559  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement_facture';
560  $sql.= ' WHERE fk_paiement = '.$this->id;
561  dol_syslog($sql);
562  $result = $this->db->query($sql);
563  if ($result)
564  {
565  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement';
566  $sql.= ' WHERE rowid = '.$this->id;
567  dol_syslog($sql);
568  $result = $this->db->query($sql);
569  if (! $result)
570  {
571  $this->error=$this->db->lasterror();
572  $this->db->rollback();
573  return -3;
574  }
575 
576  $this->db->commit();
577  return 1;
578  }
579  else
580  {
581  $this->error=$this->db->error;
582  $this->db->rollback();
583  return -5;
584  }
585  }
586 
587 
601  function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque,$notrigger=0)
602  {
603  global $conf,$langs,$user;
604 
605  $error=0;
606  $bank_line_id=0;
607 
608  if (! empty($conf->banque->enabled))
609  {
610  if ($accountid <= 0)
611  {
612  $this->error='Bad value for parameter accountid='.$accountid;
613  dol_syslog(get_class($this).'::addPaymentToBank '.$this->error, LOG_ERR);
614  return -1;
615  }
616 
617  $this->db->begin();
618 
619  $this->fk_account=$accountid;
620 
621  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
622 
623  dol_syslog("$user->id,$mode,$label,$this->fk_account,$emetteur_nom,$emetteur_banque");
624 
625  $acc = new Account($this->db);
626  $result=$acc->fetch($this->fk_account);
627 
628  $totalamount=$this->amount;
629  if (empty($totalamount)) $totalamount=$this->total; // For backward compatibility
630 
631  // if dolibarr currency != bank currency then we received an amount in customer currency (currently I don't manage the case : my currency is USD, the customer currency is EUR and he paid me in GBP. Seems no sense for me)
632  if (!empty($conf->multicurrency->enabled) && $conf->currency != $acc->currency_code) $totalamount=$this->multicurrency_amount;
633 
634  if ($mode == 'payment_supplier') $totalamount=-$totalamount;
635 
636  // Insert payment into llx_bank
637  $bank_line_id = $acc->addline(
638  $this->datepaye,
639  $this->paiementid, // Payment mode id or code ("CHQ or VIR for example")
640  $label,
641  $totalamount, // Sign must be positive when we receive money (customer payment), negative when you give money (supplier invoice or credit note)
642  $this->num_paiement,
643  '',
644  $user,
645  $emetteur_nom,
646  $emetteur_banque
647  );
648 
649  // Mise a jour fk_bank dans llx_paiement
650  // On connait ainsi le paiement qui a genere l'ecriture bancaire
651  if ($bank_line_id > 0)
652  {
653  $result=$this->update_fk_bank($bank_line_id);
654  if ($result <= 0)
655  {
656  $error++;
657  dol_print_error($this->db);
658  }
659 
660  // Add link 'payment', 'payment_supplier' in bank_url between payment and bank transaction
661  if ( ! $error)
662  {
663  $url='';
664  if ($mode == 'payment') $url=DOL_URL_ROOT.'/compta/paiement/card.php?id=';
665  if ($mode == 'payment_supplier') $url=DOL_URL_ROOT.'/fourn/paiement/card.php?id=';
666  if ($url)
667  {
668  $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
669  if ($result <= 0)
670  {
671  $error++;
672  dol_print_error($this->db);
673  }
674  }
675  }
676 
677  // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
678  //if (! $error && $label != '(WithdrawalPayment)')
679  if (! $error)
680  {
681  $linkaddedforthirdparty=array();
682  foreach ($this->amounts as $key => $value) // We should have invoices always for same third party but we loop in case of.
683  {
684  if ($mode == 'payment')
685  {
686  $fac = new Facture($this->db);
687  $fac->fetch($key);
688  $fac->fetch_thirdparty();
689  if (! in_array($fac->thirdparty->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
690  {
691  $result=$acc->add_url_line(
692  $bank_line_id,
693  $fac->thirdparty->id,
694  DOL_URL_ROOT.'/comm/card.php?socid=',
695  $fac->thirdparty->name,
696  'company'
697  );
698  if ($result <= 0) dol_syslog(get_class($this).'::addPaymentToBank '.$this->db->lasterror());
699  $linkaddedforthirdparty[$fac->thirdparty->id]=$fac->thirdparty->id; // Mark as done for this thirdparty
700  }
701  }
702  if ($mode == 'payment_supplier')
703  {
704  $fac = new FactureFournisseur($this->db);
705  $fac->fetch($key);
706  $fac->fetch_thirdparty();
707  if (! in_array($fac->thirdparty->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
708  {
709  $result=$acc->add_url_line(
710  $bank_line_id,
711  $fac->thirdparty->id,
712  DOL_URL_ROOT.'/fourn/card.php?socid=',
713  $fac->thirdparty->name,
714  'company'
715  );
716  if ($result <= 0) dol_syslog(get_class($this).'::addPaymentToBank '.$this->db->lasterror());
717  $linkaddedforthirdparty[$fac->thirdparty->id]=$fac->thirdparty->id; // Mark as done for this thirdparty
718  }
719  }
720  }
721  }
722 
723  // Add link 'WithdrawalPayment' in bank_url
724  if (! $error && $label == '(WithdrawalPayment)')
725  {
726  $result=$acc->add_url_line(
727  $bank_line_id,
728  $this->id_prelevement,
729  DOL_URL_ROOT.'/compta/prelevement/card.php?id=',
730  $this->num_paiement,
731  'withdraw'
732  );
733  }
734 
735  if (! $error && ! $notrigger)
736  {
737  // Appel des triggers
738  $result=$this->call_trigger('PAYMENT_ADD_TO_BANK', $user);
739  if ($result < 0) { $error++; }
740  // Fin appel triggers
741  }
742  }
743  else
744  {
745  $this->error=$acc->error;
746  $error++;
747  }
748 
749  if (! $error)
750  {
751  $this->db->commit();
752  }
753  else
754  {
755  $this->db->rollback();
756  }
757  }
758 
759  if (! $error)
760  {
761  return $bank_line_id;
762  }
763  else
764  {
765  return -1;
766  }
767  }
768 
769 
770  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
777  function update_fk_bank($id_bank)
778  {
779  // phpcs:enable
780  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' set fk_bank = '.$id_bank;
781  $sql.= ' WHERE rowid = '.$this->id;
782 
783  dol_syslog(get_class($this).'::update_fk_bank', LOG_DEBUG);
784  $result = $this->db->query($sql);
785  if ($result)
786  {
787  return 1;
788  }
789  else
790  {
791  $this->error=$this->db->lasterror();
792  dol_syslog(get_class($this).'::update_fk_bank '.$this->error);
793  return -1;
794  }
795  }
796 
797  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
804  function update_date($date)
805  {
806  // phpcs:enable
807  $error=0;
808 
809  if (!empty($date) && $this->statut != 1)
810  {
811  $this->db->begin();
812 
813  dol_syslog(get_class($this)."::update_date with date = ".$date, LOG_DEBUG);
814 
815  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
816  $sql.= " SET datep = '".$this->db->idate($date)."'";
817  $sql.= " WHERE rowid = ".$this->id;
818 
819  $result = $this->db->query($sql);
820  if (! $result)
821  {
822  $error++;
823  $this->error='Error -1 '.$this->db->error();
824  }
825 
826  $type = $this->element;
827 
828  $sql = "UPDATE ".MAIN_DB_PREFIX.'bank';
829  $sql.= " SET dateo = '".$this->db->idate($date)."', datev = '".$this->db->idate($date)."'";
830  $sql.= " WHERE rowid IN (SELECT fk_bank FROM ".MAIN_DB_PREFIX."bank_url WHERE type = '".$type."' AND url_id = ".$this->id.")";
831  $sql.= " AND rappro = 0";
832 
833  $result = $this->db->query($sql);
834  if (! $result)
835  {
836  $error++;
837  $this->error='Error -1 '.$this->db->error();
838  }
839 
840  if (! $error)
841  {
842 
843  }
844 
845  if (! $error)
846  {
847  $this->datepaye = $date;
848  $this->date = $date;
849 
850  $this->db->commit();
851  return 0;
852  }
853  else
854  {
855  $this->db->rollback();
856  return -2;
857  }
858  }
859  return -1; //no date given or already validated
860  }
861 
862  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
869  function update_num($num)
870  {
871  // phpcs:enable
872  if(!empty($num) && $this->statut!=1)
873  {
874  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
875  $sql.= " SET num_paiement = '".$this->db->escape($num)."'";
876  $sql.= " WHERE rowid = ".$this->id;
877 
878  dol_syslog(get_class($this)."::update_num", LOG_DEBUG);
879  $result = $this->db->query($sql);
880  if ($result)
881  {
882  $this->numero = $this->db->escape($num);
883  return 0;
884  }
885  else
886  {
887  $this->error='Error -1 '.$this->db->error();
888  return -2;
889  }
890  }
891  return -1; //no num given or already validated
892  }
893 
900  function valide(User $user=null)
901  {
902  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 1 WHERE rowid = '.$this->id;
903 
904  dol_syslog(get_class($this).'::valide', LOG_DEBUG);
905  $result = $this->db->query($sql);
906  if ($result)
907  {
908  return 1;
909  }
910  else
911  {
912  $this->error=$this->db->lasterror();
913  dol_syslog(get_class($this).'::valide '.$this->error);
914  return -1;
915  }
916  }
917 
924  function reject(User $user=null)
925  {
926  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 2 WHERE rowid = '.$this->id;
927 
928  dol_syslog(get_class($this).'::reject', LOG_DEBUG);
929  $result = $this->db->query($sql);
930  if ($result)
931  {
932  return 1;
933  }
934  else
935  {
936  $this->error=$this->db->lasterror();
937  dol_syslog(get_class($this).'::reject '.$this->error);
938  return -1;
939  }
940  }
941 
948  function info($id)
949  {
950  $sql = 'SELECT p.rowid, p.datec, p.fk_user_creat, p.fk_user_modif, p.tms';
951  $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p';
952  $sql.= ' WHERE p.rowid = '.$id;
953 
954  dol_syslog(get_class($this).'::info', LOG_DEBUG);
955  $result = $this->db->query($sql);
956 
957  if ($result)
958  {
959  if ($this->db->num_rows($result))
960  {
961  $obj = $this->db->fetch_object($result);
962  $this->id = $obj->rowid;
963  if ($obj->fk_user_creat)
964  {
965  $cuser = new User($this->db);
966  $cuser->fetch($obj->fk_user_creat);
967  $this->user_creation = $cuser;
968  }
969  if ($obj->fk_user_modif)
970  {
971  $muser = new User($this->db);
972  $muser->fetch($obj->fk_user_modif);
973  $this->user_modification = $muser;
974  }
975  $this->date_creation = $this->db->jdate($obj->datec);
976  $this->date_modification = $this->db->jdate($obj->tms);
977  }
978  $this->db->free($result);
979  }
980  else
981  {
982  dol_print_error($this->db);
983  }
984  }
985 
992  function getBillsArray($filter='')
993  {
994  $sql = 'SELECT fk_facture';
995  $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'facture as f';
996  $sql.= ' WHERE pf.fk_facture = f.rowid AND fk_paiement = '.$this->id;
997  if ($filter) $sql.= ' AND '.$filter;
998  $resql = $this->db->query($sql);
999  if ($resql)
1000  {
1001  $i=0;
1002  $num=$this->db->num_rows($resql);
1003  $billsarray=array();
1004 
1005  while ($i < $num)
1006  {
1007  $obj = $this->db->fetch_object($resql);
1008  $billsarray[$i]=$obj->fk_facture;
1009  $i++;
1010  }
1011 
1012  return $billsarray;
1013  }
1014  else
1015  {
1016  $this->error=$this->db->error();
1017  dol_syslog(get_class($this).'::getBillsArray Error '.$this->error.' -', LOG_DEBUG);
1018  return -1;
1019  }
1020  }
1021 
1030  function getNextNumRef($soc,$mode='next')
1031  {
1032  global $conf, $db, $langs;
1033  $langs->load("bills");
1034 
1035  // Clean parameters (if not defined or using deprecated value)
1036  if (empty($conf->global->PAYMENT_ADDON)) $conf->global->PAYMENT_ADDON='mod_payment_cicada';
1037  else if ($conf->global->PAYMENT_ADDON=='ant') $conf->global->PAYMENT_ADDON='mod_payment_ant';
1038  else if ($conf->global->PAYMENT_ADDON=='cicada') $conf->global->PAYMENT_ADDON='mod_payment_cicada';
1039 
1040  if (! empty($conf->global->PAYMENT_ADDON))
1041  {
1042  $mybool=false;
1043 
1044  $file = $conf->global->PAYMENT_ADDON.".php";
1045  $classname = $conf->global->PAYMENT_ADDON;
1046 
1047  // Include file with class
1048  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1049 
1050  foreach ($dirmodels as $reldir) {
1051 
1052  $dir = dol_buildpath($reldir."core/modules/payment/");
1053 
1054  // Load file with numbering class (if found)
1055  if (is_file($dir.$file) && is_readable($dir.$file))
1056  {
1057  $mybool |= include_once $dir . $file;
1058  }
1059  }
1060 
1061  // For compatibility
1062  if (! $mybool)
1063  {
1064  $file = $conf->global->PAYMENT_ADDON.".php";
1065  $classname = "mod_payment_".$conf->global->PAYMENT_ADDON;
1066  $classname = preg_replace('/\-.*$/','',$classname);
1067  // Include file with class
1068  foreach ($conf->file->dol_document_root as $dirroot)
1069  {
1070  $dir = $dirroot."/core/modules/payment/";
1071 
1072  // Load file with numbering class (if found)
1073  if (is_file($dir.$file) && is_readable($dir.$file)) {
1074  $mybool |= include_once $dir . $file;
1075  }
1076  }
1077  }
1078 
1079  if (! $mybool)
1080  {
1081  dol_print_error('',"Failed to include file ".$file);
1082  return '';
1083  }
1084 
1085  $obj = new $classname();
1086  $numref = "";
1087  $numref = $obj->getNextValue($soc,$this);
1088 
1093  if ($mode != 'last' && !$numref) {
1094  dol_print_error($db,"Payment::getNextNumRef ".$obj->error);
1095  return "";
1096  }
1097 
1098  return $numref;
1099  }
1100  else
1101  {
1102  $langs->load("errors");
1103  print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete");
1104  return "";
1105  }
1106  }
1107 
1113  function getWay()
1114  {
1115  global $conf;
1116 
1117  $way = 'dolibarr';
1118  if (!empty($conf->multicurrency->enabled))
1119  {
1120  foreach ($this->multicurrency_amounts as $value)
1121  {
1122  if (!empty($value)) // one value found then payment is in invoice currency
1123  {
1124  $way = 'customer';
1125  break;
1126  }
1127  }
1128  }
1129 
1130  return $way;
1131  }
1132 
1141  function initAsSpecimen($option='')
1142  {
1143  global $user,$langs,$conf;
1144 
1145  $now=dol_now();
1146  $arraynow=dol_getdate($now);
1147  $nownotime=dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1148 
1149  // Initialize parameters
1150  $this->id=0;
1151  $this->ref = 'SPECIMEN';
1152  $this->specimen=1;
1153  $this->facid = 1;
1154  $this->datepaye = $nownotime;
1155  }
1156 
1157 
1167  function getNomUrl($withpicto=0, $option='', $mode='withlistofinvoices', $notooltip=0)
1168  {
1169  global $conf, $langs;
1170 
1171  if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
1172 
1173  $result='';
1174  $label = '<u>'.$langs->trans("ShowPayment").'</u><br>';
1175  $label.= '<strong>'.$langs->trans("Ref").':</strong> '.$this->ref;
1176  $label.= '<br><strong>'.$langs->trans("Date").':</strong> '.dol_print_date($this->date, 'dayhour');
1177  if ($mode == 'withlistofinvoices')
1178  {
1179  $arraybill = $this->getBillsArray();
1180  if (is_array($arraybill) && count($arraybill) > 0)
1181  {
1182  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1183  $facturestatic=new Facture($this->db);
1184  foreach ($arraybill as $billid)
1185  {
1186  $facturestatic->fetch($billid);
1187  $label .='<br> '.$facturestatic->getNomUrl(1).' '.$facturestatic->getLibStatut(2,1);
1188  }
1189  }
1190  }
1191 
1192  $linkclose='';
1193  if (empty($notooltip))
1194  {
1195  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
1196  {
1197  $label=$langs->trans("ShowMyObject");
1198  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
1199  }
1200  $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
1201  $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
1202  }
1203  else $linkclose = ($morecss?' class="'.$morecss.'"':'');
1204 
1205  $url = DOL_URL_ROOT.'/compta/paiement/card.php?id='.$this->id;
1206 
1207  $linkstart = '<a href="'.$url.'"';
1208  $linkstart.=$linkclose.'>';
1209  $linkend='</a>';
1210 
1211  $result .= $linkstart;
1212  if ($withpicto) $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);
1213  if ($withpicto && $withpicto != 2) $result.= ($this->ref?$this->ref:$this->id);
1214  $result .= $linkend;
1215 
1216  return $result;
1217  }
1218 
1225  function getLibStatut($mode=0)
1226  {
1227  return $this->LibStatut($this->statut,$mode);
1228  }
1229 
1230  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1238  function LibStatut($status,$mode=0)
1239  {
1240  // phpcs:enable
1241  global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
1242 
1243  $langs->load('compta');
1244  /*if ($mode == 0)
1245  {
1246  if ($status == 0) return $langs->trans('ToValidate');
1247  if ($status == 1) return $langs->trans('Validated');
1248  }
1249  if ($mode == 1)
1250  {
1251  if ($status == 0) return $langs->trans('ToValidate');
1252  if ($status == 1) return $langs->trans('Validated');
1253  }
1254  if ($mode == 2)
1255  {
1256  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
1257  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
1258  }
1259  if ($mode == 3)
1260  {
1261  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
1262  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
1263  }
1264  if ($mode == 4)
1265  {
1266  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
1267  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
1268  }
1269  if ($mode == 5)
1270  {
1271  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
1272  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
1273  }
1274  if ($mode == 6)
1275  {
1276  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
1277  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
1278  }*/
1279  return '';
1280  }
1281 
1282  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1289  function fetch_thirdparty($force_thirdparty_id=0)
1290  {
1291  // phpcs:enable
1292  include_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
1293 
1294  if (empty($force_thirdparty_id))
1295  {
1296  $billsarray = $this->getBillsArray(); // From payment, the fk_soc isn't available, we should load the first supplier invoice to get him
1297  if (!empty($billsarray))
1298  {
1299  $invoice = new Facture($this->db);
1300  if ($invoice->fetch($billsarray[0]) > 0)
1301  {
1302  $force_thirdparty_id = $invoice->fk_soc;
1303  }
1304  }
1305  }
1306 
1307  return parent::fetch_thirdparty($force_thirdparty_id);
1308  }
1309 }
print
Draft customers invoices.
Definition: index.php:91
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
fetch($id, $ref='', $fk_bank='')
Load payment from database.
create($user, $closepaidinvoices=0)
Create payment of invoices into database.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
if(! empty($search_group)) natural_search(array("g.nom" g note
Definition: list.php:123
Class to manage Dolibarr users.
Definition: user.class.php:41
__construct($db)
Constructor.
getBillsArray($filter='')
Retourne la liste des factures sur lesquels porte le paiement.
Class to manage bank transaction lines.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
reject(User $user=null)
Reject payment.
Class to manage suppliers invoices.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getNextNumRef($soc, $mode='next')
Return next reference of customer invoice not already used (or last reference) according to numbering...
Class to manage bank accounts.
info($id)
Information sur l&#39;objet.
update_fk_bank($id_bank)
Mise a jour du lien entre le paiement et la ligne generee dans llx_bank.
update_date($date)
Updates the payment date.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id into this->thirdparty.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
initAsSpecimen($option='')
Initialise an instance with random values.
getNomUrl($withpicto=0, $option='', $mode='withlistofinvoices', $notooltip=0)
Return clicable name (with picto eventually)
Class to manage payments of customer invoices.
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;une facture (brouillon, validee, abandonnee, payee) ...
valide(User $user=null)
Validate payment.
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
update_num($num)
Updates the payment number.
getWay()
get the right way of payment
addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque, $notrigger=0)
Add a record into bank for payment + links between this bank record and sources of payment...
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
Class to manage invoices.
dol_getdate($timestamp, $fast=false)
Return an array with locale date info.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)