dolibarr  20.0.0-beta
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  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
29 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
31 
32 
37 {
41  public $element = 'chequereceipt';
42 
46  public $table_element = 'bordereau_cheque';
47 
51  public $picto = 'payment';
52 
53  public $num;
54  public $intitule;
56  public $errno;
57 
58  public $type = 'CHQ'; // 'CHQ', 'TRA', ...
59 
60  public $amount;
61  public $date_bordereau;
62  public $account_id;
63  public $account_label;
64  public $author_id;
65  public $nbcheque;
66 
70  public $ref;
71 
72  const STATUS_DRAFT = 0;
73  const STATUS_VALIDATED = 1;
74 
75 
81  public function __construct($db)
82  {
83  $this->db = $db;
84  }
85 
93  public function fetch($id, $ref = '')
94  {
95  global $conf;
96 
97  $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,";
98  $sql .= " bc.date_bordereau as date_bordereau, bc.type,";
99  $sql .= " ba.label as account_label";
100  $sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque as bc";
101  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_account as ba ON bc.fk_bank_account = ba.rowid";
102  $sql .= " WHERE bc.entity = ".$conf->entity;
103  if ($id) {
104  $sql .= " AND bc.rowid = ".((int) $id);
105  }
106  if ($ref) {
107  $sql .= " AND bc.ref = '".$this->db->escape($ref)."'";
108  }
109 
110  dol_syslog("RemiseCheque::fetch", LOG_DEBUG);
111  $resql = $this->db->query($sql);
112  if ($resql) {
113  if ($obj = $this->db->fetch_object($resql)) {
114  $this->id = $obj->rowid;
115  $this->amount = $obj->amount;
116  $this->date_bordereau = $this->db->jdate($obj->date_bordereau);
117  $this->account_id = $obj->fk_bank_account;
118  $this->account_label = $obj->account_label;
119  $this->author_id = $obj->fk_user_author;
120  $this->nbcheque = $obj->nbcheque;
121  $this->statut = $obj->statut;
122  $this->ref_ext = $obj->ref_ext;
123  $this->type = $obj->type;
124 
125  if ($this->statut == 0) {
126  $this->ref = "(PROV".$this->id.")";
127  } else {
128  $this->ref = $obj->ref;
129  }
130  }
131  $this->db->free($resql);
132 
133  return 1;
134  } else {
135  $this->error = $this->db->lasterror();
136  return -1;
137  }
138  }
139 
149  public function create($user, $account_id, $limit, $toRemise)
150  {
151  global $conf;
152 
153  $this->errno = 0;
154  $this->id = 0;
155 
156  $now = dol_now();
157 
158  dol_syslog("RemiseCheque::Create start", LOG_DEBUG);
159 
160  // Clean parameters
161  if (empty($this->type)) {
162  $this->type = 'CHQ';
163  }
164 
165  $this->db->begin();
166 
167  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bordereau_cheque (";
168  $sql .= "datec";
169  $sql .= ", date_bordereau";
170  $sql .= ", fk_user_author";
171  $sql .= ", fk_bank_account";
172  $sql .= ", statut";
173  $sql .= ", amount";
174  $sql .= ", ref";
175  $sql .= ", entity";
176  $sql .= ", nbcheque";
177  $sql .= ", ref_ext";
178  $sql .= ", type";
179  $sql .= ") VALUES (";
180  $sql .= "'".$this->db->idate($now)."'";
181  $sql .= ", '".$this->db->idate($now)."'";
182  $sql .= ", ".((int) $user->id);
183  $sql .= ", ".((int) $account_id);
184  $sql .= ", 0";
185  $sql .= ", 0";
186  $sql .= ", 0";
187  $sql .= ", ".((int) $conf->entity);
188  $sql .= ", 0";
189  $sql .= ", ''";
190  $sql .= ", '".$this->db->escape($this->type)."'";
191  $sql .= ")";
192 
193  $resql = $this->db->query($sql);
194  if ($resql) {
195  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bordereau_cheque");
196  if ($this->id == 0) {
197  $this->errno = -1024;
198  dol_syslog("Remisecheque::Create Error read id ".$this->errno, LOG_ERR);
199  }
200 
201  if ($this->id > 0 && $this->errno == 0) {
202  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
203  $sql .= " SET ref = '(PROV".$this->id.")'";
204  $sql .= " WHERE rowid=".((int) $this->id);
205 
206  $resql = $this->db->query($sql);
207  if (!$resql) {
208  $this->errno = -1025;
209  dol_syslog("RemiseCheque::Create Error update ".$this->errno, LOG_ERR);
210  }
211  }
212 
213  $lines = array();
214 
215  if ($this->id > 0 && $this->errno == 0) {
216  $sql = "SELECT b.rowid";
217  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
218  $sql .= " WHERE b.fk_type = '".$this->db->escape($this->type)."'";
219  $sql .= " AND b.amount > 0";
220  $sql .= " AND b.fk_bordereau = 0";
221  $sql .= " AND b.fk_account = ".((int) $account_id);
222  if ($limit) {
223  $sql .= $this->db->plimit($limit);
224  }
225 
226  dol_syslog("RemiseCheque::Create", LOG_DEBUG);
227  $resql = $this->db->query($sql);
228  if ($resql) {
229  while ($row = $this->db->fetch_row($resql)) {
230  array_push($lines, $row[0]);
231  }
232  $this->db->free($resql);
233  } else {
234  $this->errno = -1026;
235  dol_syslog("RemiseCheque::Create Error ".$this->errno, LOG_ERR);
236  }
237  }
238 
239  if ($this->id > 0 && $this->errno == 0) {
240  foreach ($lines as $lineid) {
241  $checkremise = false;
242  foreach ($toRemise as $linetoremise) {
243  if ($linetoremise == $lineid) {
244  $checkremise = true;
245  }
246  }
247 
248  if ($checkremise) {
249  $sql = "UPDATE ".MAIN_DB_PREFIX."bank";
250  $sql .= " SET fk_bordereau = ".((int) $this->id);
251  $sql .= " WHERE rowid = ".((int) $lineid);
252 
253  $resql = $this->db->query($sql);
254  if (!$resql) {
255  $this->errno = -18;
256  dol_syslog("RemiseCheque::Create Error update bank ".$this->errno, LOG_ERR);
257  }
258  }
259  }
260  }
261 
262  if ($this->id > 0 && $this->errno == 0) {
263  if ($this->updateAmount() != 0) {
264  $this->errno = -1027;
265  dol_syslog("RemiseCheque::Create Error update amount ".$this->errno, LOG_ERR);
266  }
267  }
268  } else {
269  $this->errno = -1;
270  $this->error = $this->db->lasterror();
271  $this->errno = $this->db->lasterrno();
272  }
273 
274  if (!$this->errno && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_CHEQUE'))) {
275  $res = $this->validate($user);
276  //if ($res < 0) $error++;
277  }
278 
279  if (!$this->errno) {
280  $this->db->commit();
281  dol_syslog("RemiseCheque::Create end", LOG_DEBUG);
282  return $this->id;
283  } else {
284  $this->db->rollback();
285  dol_syslog("RemiseCheque::Create end", LOG_DEBUG);
286  return $this->errno;
287  }
288  }
289 
296  public function delete($user)
297  {
298  global $conf;
299 
300  $this->errno = 0;
301 
302  $this->db->begin();
303 
304  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bordereau_cheque";
305  $sql .= " WHERE rowid = ".((int) $this->id);
306  $sql .= " AND entity = ".$conf->entity;
307 
308  $resql = $this->db->query($sql);
309  if ($resql) {
310  $num = $this->db->affected_rows($resql);
311 
312  if ($num != 1) {
313  $this->errno = -2;
314  dol_syslog("Remisecheque::Delete Erreur Lecture ID ($this->errno)");
315  }
316 
317  if ($this->errno === 0) {
318  $sql = "UPDATE ".MAIN_DB_PREFIX."bank";
319  $sql .= " SET fk_bordereau = 0";
320  $sql .= " WHERE fk_bordereau = ".((int) $this->id);
321 
322  $resql = $this->db->query($sql);
323  if (!$resql) {
324  $this->errno = -1028;
325  dol_syslog("RemiseCheque::Delete ERREUR UPDATE ($this->errno)");
326  }
327  }
328  }
329 
330  if ($this->errno === 0) {
331  $this->db->commit();
332  } else {
333  $this->db->rollback();
334  dol_syslog("RemiseCheque::Delete ROLLBACK ($this->errno)");
335  }
336 
337  return $this->errno;
338  }
339 
346  public function validate($user)
347  {
348  global $langs, $conf;
349 
350  $this->errno = 0;
351 
352  $this->db->begin();
353 
354  $numref = $this->getNextNumRef();
355 
356  if ($this->errno == 0 && $numref) {
357  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
358  $sql .= " SET statut = 1, ref = '".$this->db->escape($numref)."'";
359  $sql .= " WHERE rowid = ".((int) $this->id);
360  $sql .= " AND entity = ".$conf->entity;
361  $sql .= " AND statut = 0";
362 
363  dol_syslog("RemiseCheque::Validate", LOG_DEBUG);
364  $resql = $this->db->query($sql);
365  if ($resql) {
366  $num = $this->db->affected_rows($resql);
367 
368  if ($num == 1) {
369  $this->ref = $numref;
370  $this->statut = 1;
371  } else {
372  $this->errno = -1029;
373  dol_syslog("Remisecheque::Validate Error ".$this->errno, LOG_ERR);
374  }
375  } else {
376  $this->errno = -1033;
377  dol_syslog("Remisecheque::Validate Error ".$this->errno, LOG_ERR);
378  }
379  }
380 
381  // Commit/Rollback
382  if ($this->errno == 0) {
383  $this->db->commit();
384  return 1;
385  } else {
386  $this->db->rollback();
387  dol_syslog("RemiseCheque::Validate ".$this->errno, LOG_ERR);
388  return $this->errno;
389  }
390  }
391 
399  public function getNextNumRef($mode = 'next')
400  {
401  global $conf, $db, $langs, $mysoc;
402  $langs->load("bills");
403 
404  // Clean parameters (if not defined or using deprecated value)
405  if (!getDolGlobalString('CHEQUERECEIPTS_ADDON')) {
406  $conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_mint';
407  } elseif (getDolGlobalString('CHEQUERECEIPTS_ADDON') == 'thyme') {
408  $conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_thyme';
409  } elseif (getDolGlobalString('CHEQUERECEIPTS_ADDON') == 'mint') {
410  $conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_mint';
411  }
412 
413  if (getDolGlobalString('CHEQUERECEIPTS_ADDON')) {
414  $mybool = false;
415 
416  $file = getDolGlobalString('CHEQUERECEIPTS_ADDON') . ".php";
417  $classname = getDolGlobalString('CHEQUERECEIPTS_ADDON');
418 
419  // Include file with class
420  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
421 
422  foreach ($dirmodels as $reldir) {
423  $dir = dol_buildpath($reldir."core/modules/cheque/");
424 
425  // Load file with numbering class (if found)
426  if (is_file($dir.$file) && is_readable($dir.$file)) {
427  $mybool = (include_once $dir.$file) || $mybool;
428  }
429  }
430 
431  // For compatibility
432  if (!$mybool) {
433  $file = getDolGlobalString('CHEQUERECEIPTS_ADDON') . ".php";
434  $classname = "mod_chequereceipt_" . getDolGlobalString('CHEQUERECEIPTS_ADDON');
435  $classname = preg_replace('/\-.*$/', '', $classname);
436  // Include file with class
437  foreach ($conf->file->dol_document_root as $dirroot) {
438  $dir = $dirroot."/core/modules/cheque/";
439 
440  // Load file with numbering class (if found)
441  if (is_file($dir.$file) && is_readable($dir.$file)) {
442  $mybool = (include_once $dir.$file) || $mybool;
443  }
444  }
445  }
446 
447  if (!$mybool) {
448  dol_print_error(null, "Failed to include file ".$file);
449  return '';
450  }
451 
452  $obj = new $classname();
453  '@phan-var-force CommonNumRefGenerator $obj';
454  $numref = "";
455  $numref = $obj->getNextValue($mysoc, $this);
456 
461  if ($mode != 'last' && !$numref) {
462  dol_print_error($db, "ChequeReceipts::getNextNumRef ".$obj->error);
463  return "";
464  }
465 
466  return $numref;
467  } else {
468  $langs->load("errors");
469  print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Bank"));
470  return "";
471  }
472  }
473 
474 
475  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
483  public function load_board($user, $type = 'CHQ')
484  {
485  // phpcs:enable
486  global $conf, $langs;
487 
488  if ($user->socid) {
489  return -1; // protection pour eviter appel par utilisateur externe
490  }
491 
492  $sql = "SELECT b.rowid, b.datev as datefin";
493  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
494  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
495  $sql .= " WHERE b.fk_account = ba.rowid";
496  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
497  $sql .= " AND b.fk_type = '".$this->db->escape($type)."'";
498  $sql .= " AND b.fk_bordereau = 0";
499  $sql .= " AND b.amount > 0";
500 
501  $resql = $this->db->query($sql);
502  if ($resql) {
503  $langs->load("banks");
504  $now = dol_now();
505 
506  $response = new WorkboardResponse();
507  $response->warning_delay = $conf->bank->cheque->warning_delay / 60 / 60 / 24;
508  $response->label = $langs->trans("BankChecksToReceipt");
509  $response->labelShort = $langs->trans("BankChecksToReceiptShort");
510  $response->url = DOL_URL_ROOT.'/compta/paiement/cheque/index.php?leftmenu=checks&amp;mainmenu=bank';
511  $response->img = img_object('', "payment");
512 
513  while ($obj = $this->db->fetch_object($resql)) {
514  $response->nbtodo++;
515 
516  if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->cheque->warning_delay)) {
517  $response->nbtodolate++;
518  }
519  }
520 
521  return $response;
522  } else {
523  dol_print_error($this->db);
524  $this->error = $this->db->error();
525  return -1;
526  }
527  }
528 
529 
536  public function loadStateBoard($type = 'CHQ')
537  {
538  global $user;
539 
540  if ($user->socid) {
541  return -1; // protection pour eviter appel par utilisateur externe
542  }
543 
544  $sql = "SELECT count(b.rowid) as nb";
545  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
546  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
547  $sql .= " WHERE b.fk_account = ba.rowid";
548  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
549  $sql .= " AND b.fk_type = '".$this->db->escape($type)."'";
550  $sql .= " AND b.amount > 0";
551 
552  $resql = $this->db->query($sql);
553  if ($resql) {
554  while ($obj = $this->db->fetch_object($resql)) {
555  $this->nb["cheques"] = $obj->nb;
556  }
557  $this->db->free($resql);
558  return 1;
559  } else {
560  dol_print_error($this->db);
561  $this->error = $this->db->error();
562  return -1;
563  }
564  }
565 
566 
574  public function generatePdf($model, $outputlangs)
575  {
576  global $langs, $conf;
577 
578  if (empty($model)) {
579  $model = 'blochet';
580  }
581 
582  dol_syslog("RemiseCheque::generatePdf model=".$model." id=".$this->id, LOG_DEBUG);
583 
584  $dir = DOL_DOCUMENT_ROOT."/core/modules/cheque/doc/";
585 
586  // Charge le modele
587  $file = "pdf_".$model.".class.php";
588  if (file_exists($dir.$file)) {
589  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
590  include_once $dir.$file;
591 
592  $classname = 'BordereauCheque'.ucfirst($model);
593  $docmodel = new $classname($this->db);
594  '@phan-var-force CommonDocGenerator $module';
595 
596  $sql = "SELECT b.banque, b.emetteur, b.amount, b.num_chq";
597  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
598  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
599  $sql .= ", ".MAIN_DB_PREFIX."bordereau_cheque as bc";
600  $sql .= " WHERE b.fk_account = ba.rowid";
601  $sql .= " AND b.fk_bordereau = bc.rowid";
602  $sql .= " AND bc.rowid = ".((int) $this->id);
603  $sql .= " AND bc.entity = ".$conf->entity;
604  $sql .= " ORDER BY b.dateo ASC, b.rowid ASC";
605 
606  dol_syslog("RemiseCheque::generatePdf", LOG_DEBUG);
607  $result = $this->db->query($sql);
608  if ($result) {
609  $i = 0;
610  while ($objp = $this->db->fetch_object($result)) {
611  $docmodel->lines[$i] = new stdClass();
612  $docmodel->lines[$i]->bank_chq = $objp->banque;
613  $docmodel->lines[$i]->emetteur_chq = $objp->emetteur;
614  $docmodel->lines[$i]->amount_chq = $objp->amount;
615  $docmodel->lines[$i]->num_chq = $objp->num_chq;
616  $i++;
617  }
618  }
619  $docmodel->nbcheque = $this->nbcheque;
620  $docmodel->ref = $this->ref;
621  $docmodel->amount = $this->amount;
622  $docmodel->date = $this->date_bordereau;
623 
624  $account = new Account($this->db);
625  $account->fetch($this->account_id);
626 
627  $docmodel->account = &$account;
628 
629  // We save charset_output to restore it because write_file can change it if needed for
630  // output format that does not support UTF8.
631  $sav_charset_output = $outputlangs->charset_output;
632 
633  $result = $docmodel->write_file($this, $conf->bank->dir_output.'/checkdeposits', $this->ref, $outputlangs);
634  if ($result > 0) {
635  //$outputlangs->charset_output=$sav_charset_output;
636  return 1;
637  } else {
638  //$outputlangs->charset_output=$sav_charset_output;
639  dol_syslog("Error");
640  dol_print_error($this->db, $docmodel->error);
641  return 0;
642  }
643  } else {
644  $this->error = $langs->trans("ErrorFileDoesNotExists", $dir.$file);
645  return -1;
646  }
647  }
648 
654  public function updateAmount()
655  {
656  global $conf;
657 
658  $this->errno = 0;
659 
660  $this->db->begin();
661  $total = 0;
662  $nb = 0;
663  $sql = "SELECT amount ";
664  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
665  $sql .= " WHERE fk_bordereau = ".((int) $this->id);
666 
667  $resql = $this->db->query($sql);
668  if ($resql) {
669  while ($row = $this->db->fetch_row($resql)) {
670  $total += $row[0];
671  $nb++;
672  }
673 
674  $this->db->free($resql);
675 
676  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
677  $sql .= " SET amount = ".price2num($total);
678  $sql .= ", nbcheque = ".((int) $nb);
679  $sql .= " WHERE rowid = ".((int) $this->id);
680  $sql .= " AND entity = ".((int) $conf->entity);
681 
682  $resql = $this->db->query($sql);
683  if (!$resql) {
684  $this->errno = -1030;
685  dol_syslog("RemiseCheque::updateAmount ERREUR UPDATE ($this->errno)");
686  }
687  } else {
688  $this->errno = -1031;
689  dol_syslog("RemiseCheque::updateAmount ERREUR SELECT ($this->errno)");
690  }
691 
692  if ($this->errno === 0) {
693  $this->db->commit();
694  } else {
695  $this->db->rollback();
696  dol_syslog("RemiseCheque::updateAmount ROLLBACK ($this->errno)");
697  }
698 
699  return $this->errno;
700  }
701 
708  public function removeCheck($account_id)
709  {
710  $this->errno = 0;
711 
712  if ($this->id > 0) {
713  $sql = "UPDATE ".MAIN_DB_PREFIX."bank";
714  $sql .= " SET fk_bordereau = 0";
715  $sql .= " WHERE rowid = ".((int) $account_id);
716  $sql .= " AND fk_bordereau = ".((int) $this->id);
717 
718  $resql = $this->db->query($sql);
719  if ($resql) {
720  $this->updateAmount();
721  } else {
722  $this->errno = -1032;
723  dol_syslog("RemiseCheque::removeCheck ERREUR UPDATE ($this->errno)");
724  }
725  }
726  return 0;
727  }
728 
737  public function rejectCheck($bank_id, $rejection_date)
738  {
739  global $db, $user;
740 
741  $payment = new Paiement($db);
742  $payment->fetch(0, 0, $bank_id);
743 
744  $bankline = new AccountLine($db);
745  $bankline->fetch($bank_id);
746 
747  /* Reconciliation is allowed because when check is returned, a new line is created onto bank transaction log.
748  if ($bankline->rappro)
749  {
750  $this->error='ActionRefusedLineAlreadyConciliated';
751  return -1;
752  }*/
753 
754  $this->db->begin();
755 
756  // Not reconciled, we can delete it
757  //$bankline->delete($user); // We delete
758 
759  $bankaccount = $payment->fk_account;
760 
761  // Get invoices list to reopen them
762  $sql = 'SELECT pf.fk_facture, pf.amount';
763  $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf';
764  $sql .= ' WHERE pf.fk_paiement = '.((int) $payment->id);
765 
766  $resql = $this->db->query($sql);
767  if ($resql) {
768  $rejectedPayment = new Paiement($this->db);
769  $rejectedPayment->amounts = array();
770  $rejectedPayment->datepaye = $rejection_date;
771  $rejectedPayment->paiementid = dol_getIdFromCode($this->db, 'CHQ', 'c_paiement', 'code', 'id', 1);
772  $rejectedPayment->num_payment = $payment->num_payment;
773 
774  while ($obj = $this->db->fetch_object($resql)) {
775  $invoice = new Facture($this->db);
776  $invoice->fetch($obj->fk_facture);
777  $invoice->setUnpaid($user);
778 
779  $rejectedPayment->amounts[$obj->fk_facture] = price2num($obj->amount) * -1;
780  }
781 
782  $result = $rejectedPayment->create($user);
783  if ($result > 0) {
784  // We created a negative payment, we also add the line as bank transaction
785  $result = $rejectedPayment->addPaymentToBank($user, 'payment', '(CheckRejected)', $bankaccount, '', '');
786  if ($result > 0) {
787  $result = $payment->reject();
788  if ($result > 0) {
789  $this->db->commit();
790  return $rejectedPayment->id;
791  } else {
792  $this->db->rollback();
793  return -1;
794  }
795  } else {
796  $this->error = $rejectedPayment->error;
797  $this->errors = $rejectedPayment->errors;
798  $this->db->rollback();
799  return -1;
800  }
801  } else {
802  $this->error = $rejectedPayment->error;
803  $this->errors = $rejectedPayment->errors;
804  $this->db->rollback();
805  return -1;
806  }
807  } else {
808  $this->error = $this->db->lasterror();
809  $this->db->rollback();
810  return -1;
811  }
812  }
813 
814  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
822  public function set_date($user, $date)
823  {
824  // phpcs:enable
825  if ($user->hasRight('banque', 'cheque')) {
826  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
827  $sql .= " SET date_bordereau = ".($date ? "'".$this->db->idate($date)."'" : 'null');
828  $sql .= " WHERE rowid = ".((int) $this->id);
829 
830  dol_syslog("RemiseCheque::set_date", LOG_DEBUG);
831  $resql = $this->db->query($sql);
832  if ($resql) {
833  $this->date_bordereau = $date;
834  return 1;
835  } else {
836  $this->error = $this->db->error();
837  return -1;
838  }
839  } else {
840  return -2;
841  }
842  }
843 
844  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
852  public function set_number($user, $ref)
853  {
854  // phpcs:enable
855  if ($user->hasRight('banque', 'cheque')) {
856  $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
857  $sql .= " SET ref = '".$this->db->escape($ref)."'";
858  $sql .= " WHERE rowid = ".((int) $this->id);
859 
860  dol_syslog("RemiseCheque::set_number", LOG_DEBUG);
861  $resql = $this->db->query($sql);
862  if ($resql) {
863  return 1;
864  } else {
865  $this->error = $this->db->error();
866  return -1;
867  }
868  } else {
869  return -2;
870  }
871  }
872 
881  public function initAsSpecimen($option = '')
882  {
883  global $user, $langs, $conf;
884 
885  $now = dol_now();
886  $arraynow = dol_getdate($now);
887  $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
888 
889  // Initialize parameters
890  $this->id = 0;
891  $this->ref = 'SPECIMEN';
892  $this->specimen = 1;
893  $this->date_bordereau = $nownotime;
894 
895  return 1;
896  }
897 
908  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
909  {
910  global $conf, $langs;
911 
912  $result = '';
913 
914  $label = '<u>'.$langs->trans("ShowCheckReceipt").'</u>';
915  $label .= '<br>';
916  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
917 
918  $url = DOL_URL_ROOT.'/compta/paiement/cheque/card.php?id='.$this->id;
919 
920  if ($option != 'nolink') {
921  // Add param to save lastsearch_values or not
922  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
923  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
924  $add_save_lastsearch_values = 1;
925  }
926  if ($add_save_lastsearch_values) {
927  $url .= '&save_lastsearch_values=1';
928  }
929  }
930 
931  $linkclose = '';
932  if (empty($notooltip)) {
933  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
934  $label = $langs->trans("ShowCheckReceipt");
935  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
936  }
937  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
938  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
939  } else {
940  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
941  }
942 
943  $linkstart = '<a href="'.$url.'"';
944  $linkstart .= $linkclose.'>';
945  $linkend = '</a>';
946 
947  $result .= $linkstart;
948  if ($withpicto) {
949  $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);
950  }
951  if ($withpicto != 2) {
952  $result .= $this->ref;
953  }
954  $result .= $linkend;
955 
956  return $result;
957  }
958 
965  public function getLibStatut($mode = 0)
966  {
967  return $this->LibStatut($this->statut, $mode);
968  }
969 
970  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
978  public function LibStatut($status, $mode = 0)
979  {
980  // phpcs:enable
981  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
982  global $langs;
983  $langs->load('compta');
984  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ToValidate');
985  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
986  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ToValidate');
987  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
988  }
989 
990  $statusType = 'status'.$status;
991  if ($status == self::STATUS_VALIDATED) {
992  $statusType = 'status4';
993  }
994 
995  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
996  }
997 
1005  public function getKanbanView($option = '', $arraydata = null)
1006  {
1007  global $langs;
1008 
1009  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1010 
1011  $return = '<div class="box-flex-item box-flex-grow-zero">';
1012  $return .= '<div class="info-box info-box-sm">';
1013  $return .= '<span class="info-box-icon bg-infobox-action">';
1014  $return .= img_picto('', $this->picto);
1015  $return .= '</span>';
1016  $return .= '<div class="info-box-content">';
1017  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
1018  if ($selected >= 0) {
1019  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1020  }
1021  if (property_exists($this, 'date_bordereau')) {
1022  $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>';
1023  }
1024  if (property_exists($this, 'nbcheque')) {
1025  $return .= '<br><span class="opacitymedium">'.$langs->trans("Cheque", '', '', '', '', 5).'</span> : <span class="info-box-label">'.$this->nbcheque.'</span>';
1026  }
1027  if (property_exists($this, 'account_id')) {
1028  $return .= ' | <span class="info-box-label">'.$this->account_id.'</span>';
1029  }
1030  if (method_exists($this, 'LibStatut')) {
1031  $return .= '<br><div style="display:inline-block" class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
1032  }
1033  if (property_exists($this, 'amount')) {
1034  $return .= ' | <div style="display:inline-block"><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="amount">'.price($this->amount).'</div>';
1035  }
1036  $return .= '</div>';
1037  $return .= '</div>';
1038  $return .= '</div>';
1039  return $return;
1040  }
1041 }
$object ref
Definition: info.php:79
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.
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.
loadStateBoard($type='CHQ')
Load indicators this->nb for the state board.
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('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('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') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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_now($mode='auto')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
td amount
Definition: global.inc.php:554
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:123