dolibarr  19.0.0-dev
remisecheque.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2011-2016 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2015 Marcos GarcĂ­a <marcosgdf@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
27 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
29 
30 
35 {
39  public $element = 'chequereceipt';
40 
44  public $table_element = 'bordereau_cheque';
45 
49  public $picto = 'payment';
50 
51  public $num;
52  public $intitule;
54  public $errno;
55 
56  public $type = 'CHQ'; // 'CHQ', 'TRA', ...
57 
58  public $amount;
59  public $date_bordereau;
60  public $account_id;
61  public $account_label;
62  public $author_id;
63  public $nbcheque;
64 
68  public $ref;
69 
70  const STATUS_DRAFT = 0;
71  const STATUS_VALIDATED = 1;
72 
73 
79  public function __construct($db)
80  {
81  $this->db = $db;
82  }
83 
91  public function fetch($id, $ref = '')
92  {
93  global $conf;
94 
95  $sql = "SELECT bc.rowid, bc.datec, bc.fk_user_author, bc.fk_bank_account, bc.amount, bc.ref, bc.statut, bc.nbcheque, bc.ref_ext,";
96  $sql .= " bc.date_bordereau as date_bordereau, bc.type,";
97  $sql .= " ba.label as account_label";
98  $sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque as bc";
99  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_account as ba ON bc.fk_bank_account = ba.rowid";
100  $sql .= " WHERE bc.entity = ".$conf->entity;
101  if ($id) {
102  $sql .= " AND bc.rowid = ".((int) $id);
103  }
104  if ($ref) {
105  $sql .= " AND bc.ref = '".$this->db->escape($ref)."'";
106  }
107 
108  dol_syslog("RemiseCheque::fetch", LOG_DEBUG);
109  $resql = $this->db->query($sql);
110  if ($resql) {
111  if ($obj = $this->db->fetch_object($resql)) {
112  $this->id = $obj->rowid;
113  $this->amount = $obj->amount;
114  $this->date_bordereau = $this->db->jdate($obj->date_bordereau);
115  $this->account_id = $obj->fk_bank_account;
116  $this->account_label = $obj->account_label;
117  $this->author_id = $obj->fk_user_author;
118  $this->nbcheque = $obj->nbcheque;
119  $this->statut = $obj->statut;
120  $this->ref_ext = $obj->ref_ext;
121  $this->type = $obj->type;
122 
123  if ($this->statut == 0) {
124  $this->ref = "(PROV".$this->id.")";
125  } else {
126  $this->ref = $obj->ref;
127  }
128  }
129  $this->db->free($resql);
130 
131  return 1;
132  } else {
133  $this->error = $this->db->lasterror();
134  return -1;
135  }
136  }
137 
147  public function create($user, $account_id, $limit, $toRemise)
148  {
149  global $conf;
150 
151  $this->errno = 0;
152  $this->id = 0;
153 
154  $now = dol_now();
155 
156  dol_syslog("RemiseCheque::Create start", LOG_DEBUG);
157 
158  // Clean parameters
159  if (empty($this->type)) {
160  $this->type = 'CHQ';
161  }
162 
163  $this->db->begin();
164 
165  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bordereau_cheque (";
166  $sql .= "datec";
167  $sql .= ", date_bordereau";
168  $sql .= ", fk_user_author";
169  $sql .= ", fk_bank_account";
170  $sql .= ", statut";
171  $sql .= ", amount";
172  $sql .= ", ref";
173  $sql .= ", entity";
174  $sql .= ", nbcheque";
175  $sql .= ", ref_ext";
176  $sql .= ", type";
177  $sql .= ") VALUES (";
178  $sql .= "'".$this->db->idate($now)."'";
179  $sql .= ", '".$this->db->idate($now)."'";
180  $sql .= ", ".((int) $user->id);
181  $sql .= ", ".((int) $account_id);
182  $sql .= ", 0";
183  $sql .= ", 0";
184  $sql .= ", 0";
185  $sql .= ", ".((int) $conf->entity);
186  $sql .= ", 0";
187  $sql .= ", ''";
188  $sql .= ", '".$this->db->escape($this->type)."'";
189  $sql .= ")";
190 
191  $resql = $this->db->query($sql);
192  if ($resql) {
193  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bordereau_cheque");
194  if ($this->id == 0) {
195  $this->errno = -1024;
196  dol_syslog("Remisecheque::Create Error read id ".$this->errno, LOG_ERR);
197  }
198 
199  if ($this->id > 0 && $this->errno == 0) {
200  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
201  $sql .= " SET ref = '(PROV".$this->id.")'";
202  $sql .= " WHERE rowid=".((int) $this->id);
203 
204  $resql = $this->db->query($sql);
205  if (!$resql) {
206  $this->errno = -1025;
207  dol_syslog("RemiseCheque::Create Error update ".$this->errno, LOG_ERR);
208  }
209  }
210 
211  $lines = array();
212 
213  if ($this->id > 0 && $this->errno == 0) {
214  $sql = "SELECT b.rowid";
215  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
216  $sql .= " WHERE b.fk_type = '".$this->db->escape($this->type)."'";
217  $sql .= " AND b.amount > 0";
218  $sql .= " AND b.fk_bordereau = 0";
219  $sql .= " AND b.fk_account = ".((int) $account_id);
220  if ($limit) {
221  $sql .= $this->db->plimit($limit);
222  }
223 
224  dol_syslog("RemiseCheque::Create", LOG_DEBUG);
225  $resql = $this->db->query($sql);
226  if ($resql) {
227  while ($row = $this->db->fetch_row($resql)) {
228  array_push($lines, $row[0]);
229  }
230  $this->db->free($resql);
231  } else {
232  $this->errno = -1026;
233  dol_syslog("RemiseCheque::Create Error ".$this->errno, LOG_ERR);
234  }
235  }
236 
237  if ($this->id > 0 && $this->errno == 0) {
238  foreach ($lines as $lineid) {
239  $checkremise = false;
240  foreach ($toRemise as $linetoremise) {
241  if ($linetoremise == $lineid) {
242  $checkremise = true;
243  }
244  }
245 
246  if ($checkremise) {
247  $sql = "UPDATE ".MAIN_DB_PREFIX."bank";
248  $sql .= " SET fk_bordereau = ".((int) $this->id);
249  $sql .= " WHERE rowid = ".((int) $lineid);
250 
251  $resql = $this->db->query($sql);
252  if (!$resql) {
253  $this->errno = -18;
254  dol_syslog("RemiseCheque::Create Error update bank ".$this->errno, LOG_ERR);
255  }
256  }
257  }
258  }
259 
260  if ($this->id > 0 && $this->errno == 0) {
261  if ($this->updateAmount() <> 0) {
262  $this->errno = -1027;
263  dol_syslog("RemiseCheque::Create Error update amount ".$this->errno, LOG_ERR);
264  }
265  }
266  } else {
267  $this->errno = -1;
268  $this->error = $this->db->lasterror();
269  $this->errno = $this->db->lasterrno();
270  }
271 
272  if (!$this->errno && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_CHEQUE'))) {
273  $res = $this->validate($user);
274  //if ($res < 0) $error++;
275  }
276 
277  if (!$this->errno) {
278  $this->db->commit();
279  dol_syslog("RemiseCheque::Create end", LOG_DEBUG);
280  return $this->id;
281  } else {
282  $this->db->rollback();
283  dol_syslog("RemiseCheque::Create end", LOG_DEBUG);
284  return $this->errno;
285  }
286  }
287 
294  public function delete($user = '')
295  {
296  global $conf;
297 
298  $this->errno = 0;
299  $this->db->begin();
300 
301  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bordereau_cheque";
302  $sql .= " WHERE rowid = ".((int) $this->id);
303  $sql .= " AND entity = ".$conf->entity;
304 
305  $resql = $this->db->query($sql);
306  if ($resql) {
307  $num = $this->db->affected_rows($resql);
308 
309  if ($num <> 1) {
310  $this->errno = -2;
311  dol_syslog("Remisecheque::Delete Erreur Lecture ID ($this->errno)");
312  }
313 
314  if ($this->errno === 0) {
315  $sql = "UPDATE ".MAIN_DB_PREFIX."bank";
316  $sql .= " SET fk_bordereau = 0";
317  $sql .= " WHERE fk_bordereau = ".((int) $this->id);
318 
319  $resql = $this->db->query($sql);
320  if (!$resql) {
321  $this->errno = -1028;
322  dol_syslog("RemiseCheque::Delete ERREUR UPDATE ($this->errno)");
323  }
324  }
325  }
326 
327  if ($this->errno === 0) {
328  $this->db->commit();
329  } else {
330  $this->db->rollback();
331  dol_syslog("RemiseCheque::Delete ROLLBACK ($this->errno)");
332  }
333 
334  return $this->errno;
335  }
336 
343  public function validate($user)
344  {
345  global $langs, $conf;
346 
347  $this->errno = 0;
348 
349  $this->db->begin();
350 
351  $numref = $this->getNextNumRef();
352 
353  if ($this->errno == 0 && $numref) {
354  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
355  $sql .= " SET statut = 1, ref = '".$this->db->escape($numref)."'";
356  $sql .= " WHERE rowid = ".((int) $this->id);
357  $sql .= " AND entity = ".$conf->entity;
358  $sql .= " AND statut = 0";
359 
360  dol_syslog("RemiseCheque::Validate", LOG_DEBUG);
361  $resql = $this->db->query($sql);
362  if ($resql) {
363  $num = $this->db->affected_rows($resql);
364 
365  if ($num == 1) {
366  $this->ref = $numref;
367  $this->statut = 1;
368  } else {
369  $this->errno = -1029;
370  dol_syslog("Remisecheque::Validate Error ".$this->errno, LOG_ERR);
371  }
372  } else {
373  $this->errno = -1033;
374  dol_syslog("Remisecheque::Validate Error ".$this->errno, LOG_ERR);
375  }
376  }
377 
378  // Commit/Rollback
379  if ($this->errno == 0) {
380  $this->db->commit();
381  return 1;
382  } else {
383  $this->db->rollback();
384  dol_syslog("RemiseCheque::Validate ".$this->errno, LOG_ERR);
385  return $this->errno;
386  }
387  }
388 
396  public function getNextNumRef($mode = 'next')
397  {
398  global $conf, $db, $langs, $mysoc;
399  $langs->load("bills");
400 
401  // Clean parameters (if not defined or using deprecated value)
402  if (empty($conf->global->CHEQUERECEIPTS_ADDON)) {
403  $conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_mint';
404  } elseif ($conf->global->CHEQUERECEIPTS_ADDON == 'thyme') {
405  $conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_thyme';
406  } elseif ($conf->global->CHEQUERECEIPTS_ADDON == 'mint') {
407  $conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_mint';
408  }
409 
410  if (!empty($conf->global->CHEQUERECEIPTS_ADDON)) {
411  $mybool = false;
412 
413  $file = $conf->global->CHEQUERECEIPTS_ADDON.".php";
414  $classname = $conf->global->CHEQUERECEIPTS_ADDON;
415 
416  // Include file with class
417  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
418 
419  foreach ($dirmodels as $reldir) {
420  $dir = dol_buildpath($reldir."core/modules/cheque/");
421 
422  // Load file with numbering class (if found)
423  if (is_file($dir.$file) && is_readable($dir.$file)) {
424  $mybool |= include_once $dir.$file;
425  }
426  }
427 
428  // For compatibility
429  if (!$mybool) {
430  $file = $conf->global->CHEQUERECEIPTS_ADDON.".php";
431  $classname = "mod_chequereceipt_".$conf->global->CHEQUERECEIPTS_ADDON;
432  $classname = preg_replace('/\-.*$/', '', $classname);
433  // Include file with class
434  foreach ($conf->file->dol_document_root as $dirroot) {
435  $dir = $dirroot."/core/modules/cheque/";
436 
437  // Load file with numbering class (if found)
438  if (is_file($dir.$file) && is_readable($dir.$file)) {
439  $mybool |= include_once $dir.$file;
440  }
441  }
442  }
443 
444  if (!$mybool) {
445  dol_print_error('', "Failed to include file ".$file);
446  return '';
447  }
448 
449  $obj = new $classname();
450  $numref = "";
451  $numref = $obj->getNextValue($mysoc, $this);
452 
457  if ($mode != 'last' && !$numref) {
458  dol_print_error($db, "ChequeReceipts::getNextNumRef ".$obj->error);
459  return "";
460  }
461 
462  return $numref;
463  } else {
464  $langs->load("errors");
465  print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Bank"));
466  return "";
467  }
468  }
469 
470 
471  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
479  public function load_board($user, $type = 'CHQ')
480  {
481  // phpcs:enable
482  global $conf, $langs;
483 
484  if ($user->socid) {
485  return -1; // protection pour eviter appel par utilisateur externe
486  }
487 
488  $sql = "SELECT b.rowid, b.datev as datefin";
489  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
490  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
491  $sql .= " WHERE b.fk_account = ba.rowid";
492  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
493  $sql .= " AND b.fk_type = '".$this->db->escape($type)."'";
494  $sql .= " AND b.fk_bordereau = 0";
495  $sql .= " AND b.amount > 0";
496 
497  $resql = $this->db->query($sql);
498  if ($resql) {
499  $langs->load("banks");
500  $now = dol_now();
501 
502  $response = new WorkboardResponse();
503  $response->warning_delay = $conf->bank->cheque->warning_delay / 60 / 60 / 24;
504  $response->label = $langs->trans("BankChecksToReceipt");
505  $response->labelShort = $langs->trans("BankChecksToReceiptShort");
506  $response->url = DOL_URL_ROOT.'/compta/paiement/cheque/index.php?leftmenu=checks&amp;mainmenu=bank';
507  $response->img = img_object('', "payment");
508 
509  while ($obj = $this->db->fetch_object($resql)) {
510  $response->nbtodo++;
511 
512  if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->cheque->warning_delay)) {
513  $response->nbtodolate++;
514  }
515  }
516 
517  return $response;
518  } else {
519  dol_print_error($this->db);
520  $this->error = $this->db->error();
521  return -1;
522  }
523  }
524 
525 
526  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
533  public function load_state_board($type = 'CHQ')
534  {
535  // phpcs:enable
536  global $user;
537 
538  if ($user->socid) {
539  return -1; // protection pour eviter appel par utilisateur externe
540  }
541 
542  $sql = "SELECT count(b.rowid) as nb";
543  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
544  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
545  $sql .= " WHERE b.fk_account = ba.rowid";
546  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
547  $sql .= " AND b.fk_type = '".$this->db->escape($type)."'";
548  $sql .= " AND b.amount > 0";
549 
550  $resql = $this->db->query($sql);
551  if ($resql) {
552  while ($obj = $this->db->fetch_object($resql)) {
553  $this->nb["cheques"] = $obj->nb;
554  }
555  $this->db->free($resql);
556  return 1;
557  } else {
558  dol_print_error($this->db);
559  $this->error = $this->db->error();
560  return -1;
561  }
562  }
563 
564 
572  public function generatePdf($model, $outputlangs)
573  {
574  global $langs, $conf;
575 
576  if (empty($model)) {
577  $model = 'blochet';
578  }
579 
580  dol_syslog("RemiseCheque::generatePdf model=".$model." id=".$this->id, LOG_DEBUG);
581 
582  $dir = DOL_DOCUMENT_ROOT."/core/modules/cheque/doc/";
583 
584  // Charge le modele
585  $file = "pdf_".$model.".class.php";
586  if (file_exists($dir.$file)) {
587  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
588  include_once $dir.$file;
589 
590  $classname = 'BordereauCheque'.ucfirst($model);
591  $docmodel = new $classname($this->db);
592 
593  $sql = "SELECT b.banque, b.emetteur, b.amount, b.num_chq";
594  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
595  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
596  $sql .= ", ".MAIN_DB_PREFIX."bordereau_cheque as bc";
597  $sql .= " WHERE b.fk_account = ba.rowid";
598  $sql .= " AND b.fk_bordereau = bc.rowid";
599  $sql .= " AND bc.rowid = ".((int) $this->id);
600  $sql .= " AND bc.entity = ".$conf->entity;
601  $sql .= " ORDER BY b.dateo ASC, b.rowid ASC";
602 
603  dol_syslog("RemiseCheque::generatePdf", LOG_DEBUG);
604  $result = $this->db->query($sql);
605  if ($result) {
606  $i = 0;
607  while ($objp = $this->db->fetch_object($result)) {
608  $docmodel->lines[$i] = new stdClass();
609  $docmodel->lines[$i]->bank_chq = $objp->banque;
610  $docmodel->lines[$i]->emetteur_chq = $objp->emetteur;
611  $docmodel->lines[$i]->amount_chq = $objp->amount;
612  $docmodel->lines[$i]->num_chq = $objp->num_chq;
613  $i++;
614  }
615  }
616  $docmodel->nbcheque = $this->nbcheque;
617  $docmodel->ref = $this->ref;
618  $docmodel->amount = $this->amount;
619  $docmodel->date = $this->date_bordereau;
620 
621  $account = new Account($this->db);
622  $account->fetch($this->account_id);
623 
624  $docmodel->account = &$account;
625 
626  // We save charset_output to restore it because write_file can change it if needed for
627  // output format that does not support UTF8.
628  $sav_charset_output = $outputlangs->charset_output;
629 
630  $result = $docmodel->write_file($this, $conf->bank->dir_output.'/checkdeposits', $this->ref, $outputlangs);
631  if ($result > 0) {
632  //$outputlangs->charset_output=$sav_charset_output;
633  return 1;
634  } else {
635  //$outputlangs->charset_output=$sav_charset_output;
636  dol_syslog("Error");
637  dol_print_error($this->db, $docmodel->error);
638  return 0;
639  }
640  } else {
641  $this->error = $langs->trans("ErrorFileDoesNotExists", $dir.$file);
642  return -1;
643  }
644  }
645 
651  public function updateAmount()
652  {
653  global $conf;
654 
655  $this->errno = 0;
656 
657  $this->db->begin();
658  $total = 0;
659  $nb = 0;
660  $sql = "SELECT amount ";
661  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
662  $sql .= " WHERE fk_bordereau = ".((int) $this->id);
663 
664  $resql = $this->db->query($sql);
665  if ($resql) {
666  while ($row = $this->db->fetch_row($resql)) {
667  $total += $row[0];
668  $nb++;
669  }
670 
671  $this->db->free($resql);
672 
673  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
674  $sql .= " SET amount = ".price2num($total);
675  $sql .= ", nbcheque = ".((int) $nb);
676  $sql .= " WHERE rowid = ".((int) $this->id);
677  $sql .= " AND entity = ".((int) $conf->entity);
678 
679  $resql = $this->db->query($sql);
680  if (!$resql) {
681  $this->errno = -1030;
682  dol_syslog("RemiseCheque::updateAmount ERREUR UPDATE ($this->errno)");
683  }
684  } else {
685  $this->errno = -1031;
686  dol_syslog("RemiseCheque::updateAmount ERREUR SELECT ($this->errno)");
687  }
688 
689  if ($this->errno === 0) {
690  $this->db->commit();
691  } else {
692  $this->db->rollback();
693  dol_syslog("RemiseCheque::updateAmount ROLLBACK ($this->errno)");
694  }
695 
696  return $this->errno;
697  }
698 
705  public function removeCheck($account_id)
706  {
707  $this->errno = 0;
708 
709  if ($this->id > 0) {
710  $sql = "UPDATE ".MAIN_DB_PREFIX."bank";
711  $sql .= " SET fk_bordereau = 0";
712  $sql .= " WHERE rowid = ".((int) $account_id);
713  $sql .= " AND fk_bordereau = ".((int) $this->id);
714 
715  $resql = $this->db->query($sql);
716  if ($resql) {
717  $this->updateAmount();
718  } else {
719  $this->errno = -1032;
720  dol_syslog("RemiseCheque::removeCheck ERREUR UPDATE ($this->errno)");
721  }
722  }
723  return 0;
724  }
725 
734  public function rejectCheck($bank_id, $rejection_date)
735  {
736  global $db, $user;
737 
738  $payment = new Paiement($db);
739  $payment->fetch(0, 0, $bank_id);
740 
741  $bankline = new AccountLine($db);
742  $bankline->fetch($bank_id);
743 
744  /* Reconciliation is allowed because when check is returned, a new line is created onto bank transaction log.
745  if ($bankline->rappro)
746  {
747  $this->error='ActionRefusedLineAlreadyConciliated';
748  return -1;
749  }*/
750 
751  $this->db->begin();
752 
753  // Not reconciled, we can delete it
754  //$bankline->delete($user); // We delete
755 
756  $bankaccount = $payment->fk_account;
757 
758  // Get invoices list to reopen them
759  $sql = 'SELECT pf.fk_facture, pf.amount';
760  $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf';
761  $sql .= ' WHERE pf.fk_paiement = '.((int) $payment->id);
762 
763  $resql = $this->db->query($sql);
764  if ($resql) {
765  $rejectedPayment = new Paiement($this->db);
766  $rejectedPayment->amounts = array();
767  $rejectedPayment->datepaye = $rejection_date;
768  $rejectedPayment->paiementid = dol_getIdFromCode($this->db, 'CHQ', 'c_paiement', 'code', 'id', 1);
769  $rejectedPayment->num_payment = $payment->num_payment;
770 
771  while ($obj = $this->db->fetch_object($resql)) {
772  $invoice = new Facture($this->db);
773  $invoice->fetch($obj->fk_facture);
774  $invoice->setUnpaid($user);
775 
776  $rejectedPayment->amounts[$obj->fk_facture] = price2num($obj->amount) * -1;
777  }
778 
779  $result = $rejectedPayment->create($user);
780  if ($result > 0) {
781  // We created a negative payment, we also add the line as bank transaction
782  $result = $rejectedPayment->addPaymentToBank($user, 'payment', '(CheckRejected)', $bankaccount, '', '');
783  if ($result > 0) {
784  $result = $payment->reject();
785  if ($result > 0) {
786  $this->db->commit();
787  return $rejectedPayment->id;
788  } else {
789  $this->db->rollback();
790  return -1;
791  }
792  } else {
793  $this->error = $rejectedPayment->error;
794  $this->errors = $rejectedPayment->errors;
795  $this->db->rollback();
796  return -1;
797  }
798  } else {
799  $this->error = $rejectedPayment->error;
800  $this->errors = $rejectedPayment->errors;
801  $this->db->rollback();
802  return -1;
803  }
804  } else {
805  $this->error = $this->db->lasterror();
806  $this->db->rollback();
807  return -1;
808  }
809  }
810 
811  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
819  public function set_date($user, $date)
820  {
821  // phpcs:enable
822  if ($user->rights->banque->cheque) {
823  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
824  $sql .= " SET date_bordereau = ".($date ? "'".$this->db->idate($date)."'" : 'null');
825  $sql .= " WHERE rowid = ".((int) $this->id);
826 
827  dol_syslog("RemiseCheque::set_date", LOG_DEBUG);
828  $resql = $this->db->query($sql);
829  if ($resql) {
830  $this->date_bordereau = $date;
831  return 1;
832  } else {
833  $this->error = $this->db->error();
834  return -1;
835  }
836  } else {
837  return -2;
838  }
839  }
840 
841  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
849  public function set_number($user, $ref)
850  {
851  // phpcs:enable
852  if ($user->rights->banque->cheque) {
853  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
854  $sql .= " SET ref = '".$this->db->escape($ref)."'";
855  $sql .= " WHERE rowid = ".((int) $this->id);
856 
857  dol_syslog("RemiseCheque::set_number", LOG_DEBUG);
858  $resql = $this->db->query($sql);
859  if ($resql) {
860  return 1;
861  } else {
862  $this->error = $this->db->error();
863  return -1;
864  }
865  } else {
866  return -2;
867  }
868  }
869 
878  public function initAsSpecimen($option = '')
879  {
880  global $user, $langs, $conf;
881 
882  $now = dol_now();
883  $arraynow = dol_getdate($now);
884  $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
885 
886  // Initialize parameters
887  $this->id = 0;
888  $this->ref = 'SPECIMEN';
889  $this->specimen = 1;
890  $this->date_bordereau = $nownotime;
891  }
892 
903  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
904  {
905  global $conf, $langs;
906 
907  $result = '';
908 
909  $label = '<u>'.$langs->trans("ShowCheckReceipt").'</u>';
910  $label .= '<br>';
911  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
912 
913  $url = DOL_URL_ROOT.'/compta/paiement/cheque/card.php?id='.$this->id;
914 
915  if ($option != 'nolink') {
916  // Add param to save lastsearch_values or not
917  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
918  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
919  $add_save_lastsearch_values = 1;
920  }
921  if ($add_save_lastsearch_values) {
922  $url .= '&save_lastsearch_values=1';
923  }
924  }
925 
926  $linkclose = '';
927  if (empty($notooltip)) {
928  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
929  $label = $langs->trans("ShowCheckReceipt");
930  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
931  }
932  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
933  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
934  } else {
935  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
936  }
937 
938  $linkstart = '<a href="'.$url.'"';
939  $linkstart .= $linkclose.'>';
940  $linkend = '</a>';
941 
942  $result .= $linkstart;
943  if ($withpicto) {
944  $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);
945  }
946  if ($withpicto != 2) {
947  $result .= $this->ref;
948  }
949  $result .= $linkend;
950 
951  return $result;
952  }
953 
960  public function getLibStatut($mode = 0)
961  {
962  return $this->LibStatut($this->statut, $mode);
963  }
964 
965  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
973  public function LibStatut($status, $mode = 0)
974  {
975  // phpcs:enable
976  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
977  global $langs;
978  $langs->load('compta');
979  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ToValidate');
980  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
981  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ToValidate');
982  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
983  }
984 
985  $statusType = 'status'.$status;
986  if ($status == self::STATUS_VALIDATED) {
987  $statusType = 'status4';
988  }
989 
990  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
991  }
992 
1000  public function getKanbanView($option = '', $arraydata = null)
1001  {
1002  global $langs;
1003 
1004  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1005 
1006  $return = '<div class="box-flex-item box-flex-grow-zero">';
1007  $return .= '<div class="info-box info-box-sm">';
1008  $return .= '<span class="info-box-icon bg-infobox-action">';
1009  $return .= img_picto('', $this->picto);
1010  $return .= '</span>';
1011  $return .= '<div class="info-box-content">';
1012  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
1013  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1014 
1015  if (property_exists($this, 'date_bordereau')) {
1016  $return .= '<br><span class="opacitymedium">'.$langs->trans("DateCreation").'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->date_bordereau), 'day').'</span>';
1017  }
1018  if (property_exists($this, 'nbcheque')) {
1019  $return .= '<br><span class="opacitymedium">'.$langs->trans("Cheque", '', '', '', '', 5).'</span> : <span class="info-box-label">'.$this->nbcheque.'</span>';
1020  }
1021  if (property_exists($this, 'account_id')) {
1022  $return .= ' | <span class="info-box-label">'.$this->account_id.'</span>';
1023  }
1024  if (method_exists($this, 'LibStatut')) {
1025  $return .= '<br><div style="display:inline-block" class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
1026  }
1027  if (property_exists($this, 'amount')) {
1028  $return .= ' | <div style="display:inline-block"><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="amount">'.price($this->amount).'</div>';
1029  }
1030  $return .= '</div>';
1031  $return .= '</div>';
1032  $return .= '</div>';
1033  return $return;
1034  }
1035 }
$object ref
Definition: info.php:78
Class to manage bank accounts.
Class to manage bank transaction lines.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage invoices.
Class to manage payments of customer invoices.
Class to manage cheque delivery receipts.
removeCheck($account_id)
Insere la remise en base.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
$errno
Numero d'erreur Plage 1024-1279.
load_state_board($type='CHQ')
Charge indicateurs this->nb de tableau de bord.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
generatePdf($model, $outputlangs)
Build document.
LibStatut($status, $mode=0)
Return the label of a given status.
__construct($db)
Constructor.
getLibStatut($mode=0)
Return the label of the status.
set_date($user, $date)
Set the creation date.
rejectCheck($bank_id, $rejection_date)
Check return management Reopen linked invoices and create a new negative payment.
validate($user)
Validate a receipt.
set_number($user, $ref)
Set the ref of bordereau.
load_board($user, $type='CHQ')
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
getNextNumRef($mode='next')
Return next reference of cheque receipts not already used (or last reference) according to numbering ...
fetch($id, $ref='')
Load record.
create($user, $account_id, $limit, $toRemise)
Create a receipt to send cheques.
updateAmount()
Mets a jour le montant total.
initAsSpecimen($option='')
Initialise an instance with random values.
if(isModEnabled('facture') && $user->hasRight('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') && $user->hasRight('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)) $sql
Social contributions to pay.
Definition: index.php:746
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)
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.
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_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120