27 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
39 public $element =
'chequereceipt';
44 public $table_element =
'bordereau_cheque';
49 public $picto =
'payment';
57 public $date_bordereau;
59 public $account_label;
68 const STATUS_DRAFT = 0;
69 const STATUS_VALIDATED = 1;
81 $this->previous_id = 0;
91 public function fetch($id, $ref =
'')
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";
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;
102 $sql .=
" AND bc.rowid = ".((int) $id);
105 $sql .=
" AND bc.ref = '".$this->db->escape($ref).
"'";
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;
122 if ($this->statut == 0) {
123 $this->
ref =
"(PROV".$this->id.
")";
125 $this->
ref = $obj->ref;
132 $this->error = $this->
db->lasterror();
146 public function create($user, $account_id, $limit, $toRemise)
155 dol_syslog(
"RemiseCheque::Create start", LOG_DEBUG);
159 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"bordereau_cheque (";
161 $sql .=
", date_bordereau";
162 $sql .=
", fk_user_author";
163 $sql .=
", fk_bank_account";
168 $sql .=
", nbcheque";
170 $sql .=
") VALUES (";
171 $sql .=
"'".$this->db->idate($now).
"'";
172 $sql .=
", '".$this->db->idate($now).
"'";
173 $sql .=
", ".((int) $user->id);
174 $sql .=
", ".((int) $account_id);
178 $sql .=
", ".((int) $conf->entity);
185 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"bordereau_cheque");
186 if ($this->
id == 0) {
187 $this->errno = -1024;
188 dol_syslog(
"Remisecheque::Create Error read id ".$this->errno, LOG_ERR);
191 if ($this->
id > 0 && $this->errno == 0) {
192 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bordereau_cheque";
193 $sql .=
" SET ref='(PROV".$this->id.
")'";
194 $sql .=
" WHERE rowid=".((int) $this->
id).
"";
198 $this->errno = -1025;
199 dol_syslog(
"RemiseCheque::Create Error update ".$this->errno, LOG_ERR);
205 if ($this->
id > 0 && $this->errno == 0) {
206 $sql =
"SELECT b.rowid";
207 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
208 $sql .=
" WHERE b.fk_type = 'CHQ'";
209 $sql .=
" AND b.amount > 0";
210 $sql .=
" AND b.fk_bordereau = 0";
211 $sql .=
" AND b.fk_account = ".((int) $account_id);
213 $sql .= $this->
db->plimit($limit);
216 dol_syslog(
"RemiseCheque::Create", LOG_DEBUG);
219 while ($row = $this->
db->fetch_row(
$resql)) {
220 array_push($lines, $row[0]);
224 $this->errno = -1026;
225 dol_syslog(
"RemiseCheque::Create Error ".$this->errno, LOG_ERR);
229 if ($this->
id > 0 && $this->errno == 0) {
230 foreach ($lines as $lineid) {
231 $checkremise =
false;
232 foreach ($toRemise as $linetoremise) {
233 if ($linetoremise == $lineid) {
239 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bank";
240 $sql .=
" SET fk_bordereau = ".((int) $this->
id);
241 $sql .=
" WHERE rowid = ".((int) $lineid);
246 dol_syslog(
"RemiseCheque::Create Error update bank ".$this->errno, LOG_ERR);
252 if ($this->
id > 0 && $this->errno == 0) {
254 $this->errno = -1027;
255 dol_syslog(
"RemiseCheque::Create Error update amount ".$this->errno, LOG_ERR);
260 $this->error = $this->
db->lasterror();
261 $this->errno = $this->
db->lasterrno();
271 dol_syslog(
"RemiseCheque::Create end", LOG_DEBUG);
274 $this->
db->rollback();
275 dol_syslog(
"RemiseCheque::Create end", LOG_DEBUG);
286 public function delete($user =
'')
293 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"bordereau_cheque";
294 $sql .=
" WHERE rowid = ".((int) $this->
id);
295 $sql .=
" AND entity = ".$conf->entity;
299 $num = $this->
db->affected_rows(
$resql);
303 dol_syslog(
"Remisecheque::Delete Erreur Lecture ID ($this->errno)");
306 if ($this->errno === 0) {
307 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bank";
308 $sql .=
" SET fk_bordereau = 0";
309 $sql .=
" WHERE fk_bordereau = ".((int) $this->
id);
313 $this->errno = -1028;
314 dol_syslog(
"RemiseCheque::Delete ERREUR UPDATE ($this->errno)");
319 if ($this->errno === 0) {
322 $this->
db->rollback();
323 dol_syslog(
"RemiseCheque::Delete ROLLBACK ($this->errno)");
337 global $langs, $conf;
345 if ($this->errno == 0 && $numref) {
346 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bordereau_cheque";
347 $sql .=
" SET statut = 1, ref = '".$this->db->escape($numref).
"'";
348 $sql .=
" WHERE rowid = ".((int) $this->
id);
349 $sql .=
" AND entity = ".$conf->entity;
350 $sql .=
" AND statut = 0";
352 dol_syslog(
"RemiseCheque::Validate", LOG_DEBUG);
355 $num = $this->
db->affected_rows(
$resql);
358 $this->
ref = $numref;
361 $this->errno = -1029;
362 dol_syslog(
"Remisecheque::Validate Error ".$this->errno, LOG_ERR);
365 $this->errno = -1033;
366 dol_syslog(
"Remisecheque::Validate Error ".$this->errno, LOG_ERR);
371 if ($this->errno == 0) {
375 $this->
db->rollback();
376 dol_syslog(
"RemiseCheque::Validate ".$this->errno, LOG_ERR);
390 global $conf, $db, $langs, $mysoc;
391 $langs->load(
"bills");
394 if (empty($conf->global->CHEQUERECEIPTS_ADDON)) {
395 $conf->global->CHEQUERECEIPTS_ADDON =
'mod_chequereceipt_mint';
396 } elseif ($conf->global->CHEQUERECEIPTS_ADDON ==
'thyme') {
397 $conf->global->CHEQUERECEIPTS_ADDON =
'mod_chequereceipt_thyme';
398 } elseif ($conf->global->CHEQUERECEIPTS_ADDON ==
'mint') {
399 $conf->global->CHEQUERECEIPTS_ADDON =
'mod_chequereceipt_mint';
402 if (!empty($conf->global->CHEQUERECEIPTS_ADDON)) {
405 $file = $conf->global->CHEQUERECEIPTS_ADDON.
".php";
406 $classname = $conf->global->CHEQUERECEIPTS_ADDON;
409 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
411 foreach ($dirmodels as $reldir) {
415 if (is_file($dir.$file) && is_readable($dir.$file)) {
416 $mybool |= include_once $dir.$file;
422 $file = $conf->global->CHEQUERECEIPTS_ADDON.
".php";
423 $classname =
"mod_chequereceipt_".$conf->global->CHEQUERECEIPTS_ADDON;
424 $classname = preg_replace(
'/\-.*$/',
'', $classname);
426 foreach ($conf->file->dol_document_root as $dirroot) {
427 $dir = $dirroot.
"/core/modules/cheque/";
430 if (is_file($dir.$file) && is_readable($dir.$file)) {
431 $mybool |= include_once $dir.$file;
441 $obj =
new $classname();
443 $numref = $obj->getNextValue($mysoc, $this);
449 if ($mode !=
'last' && !$numref) {
456 $langs->load(
"errors");
457 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Bank"));
473 global $conf, $langs;
479 $sql =
"SELECT b.rowid, b.datev as datefin";
480 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
481 $sql .=
", ".MAIN_DB_PREFIX.
"bank_account as ba";
482 $sql .=
" WHERE b.fk_account = ba.rowid";
483 $sql .=
" AND ba.entity IN (".getEntity(
'bank_account').
")";
484 $sql .=
" AND b.fk_type = 'CHQ'";
485 $sql .=
" AND b.fk_bordereau = 0";
486 $sql .=
" AND b.amount > 0";
490 $langs->load(
"banks");
494 $response->warning_delay = $conf->bank->cheque->warning_delay / 60 / 60 / 24;
495 $response->label = $langs->trans(
"BankChecksToReceipt");
496 $response->labelShort = $langs->trans(
"BankChecksToReceiptShort");
497 $response->url = DOL_URL_ROOT.
'/compta/paiement/cheque/index.php?leftmenu=checks&mainmenu=bank';
500 while ($obj = $this->
db->fetch_object(
$resql)) {
503 if ($this->
db->jdate($obj->datefin) < ($now - $conf->bank->cheque->warning_delay)) {
504 $response->nbtodolate++;
511 $this->error = $this->
db->error();
532 $sql =
"SELECT count(b.rowid) as nb";
533 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
534 $sql .=
", ".MAIN_DB_PREFIX.
"bank_account as ba";
535 $sql .=
" WHERE b.fk_account = ba.rowid";
536 $sql .=
" AND ba.entity IN (".getEntity(
'bank_account').
")";
537 $sql .=
" AND b.fk_type = 'CHQ'";
538 $sql .=
" AND b.amount > 0";
542 while ($obj = $this->
db->fetch_object(
$resql)) {
543 $this->nb[
"cheques"] = $obj->nb;
549 $this->error = $this->
db->error();
564 global $langs, $conf;
570 dol_syslog(
"RemiseCheque::generatePdf model=".$model.
" id=".$this->id, LOG_DEBUG);
572 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/cheque/doc/";
575 $file =
"pdf_".$model.
".class.php";
576 if (file_exists($dir.$file)) {
577 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
578 include_once $dir.$file;
580 $classname =
'BordereauCheque'.ucfirst($model);
581 $docmodel =
new $classname($this->
db);
583 $sql =
"SELECT b.banque, b.emetteur, b.amount, b.num_chq";
584 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
585 $sql .=
", ".MAIN_DB_PREFIX.
"bank_account as ba";
586 $sql .=
", ".MAIN_DB_PREFIX.
"bordereau_cheque as bc";
587 $sql .=
" WHERE b.fk_account = ba.rowid";
588 $sql .=
" AND b.fk_bordereau = bc.rowid";
589 $sql .=
" AND bc.rowid = ".((int) $this->
id);
590 $sql .=
" AND bc.entity = ".$conf->entity;
591 $sql .=
" ORDER BY b.dateo ASC, b.rowid ASC";
593 dol_syslog(
"RemiseCheque::generatePdf", LOG_DEBUG);
594 $result = $this->
db->query($sql);
597 while ($objp = $this->
db->fetch_object($result)) {
598 $docmodel->lines[$i] =
new stdClass();
599 $docmodel->lines[$i]->bank_chq = $objp->banque;
600 $docmodel->lines[$i]->emetteur_chq = $objp->emetteur;
601 $docmodel->lines[$i]->amount_chq = $objp->amount;
602 $docmodel->lines[$i]->num_chq = $objp->num_chq;
606 $docmodel->nbcheque = $this->nbcheque;
607 $docmodel->ref = $this->ref;
608 $docmodel->amount = $this->amount;
609 $docmodel->date = $this->date_bordereau;
612 $account->fetch($this->account_id);
614 $docmodel->account = &$account;
618 $sav_charseSupprimert_output = $outputlangs->charset_output;
620 $result = $docmodel->write_file($this, $conf->bank->dir_output.
'/checkdeposits', $this->ref, $outputlangs);
631 $this->error = $langs->trans(
"ErrorFileDoesNotExists", $dir.$file);
650 $sql =
"SELECT amount ";
651 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank";
652 $sql .=
" WHERE fk_bordereau = ".((int) $this->
id);
656 while ($row = $this->
db->fetch_row(
$resql)) {
663 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bordereau_cheque";
664 $sql .=
" SET amount = ".price2num($total);
665 $sql .=
", nbcheque = ".((int) $nb);
666 $sql .=
" WHERE rowid = ".((int) $this->
id);
667 $sql .=
" AND entity = ".$conf->entity;
671 $this->errno = -1030;
672 dol_syslog(
"RemiseCheque::updateAmount ERREUR UPDATE ($this->errno)");
675 $this->errno = -1031;
676 dol_syslog(
"RemiseCheque::updateAmount ERREUR SELECT ($this->errno)");
679 if ($this->errno === 0) {
682 $this->
db->rollback();
683 dol_syslog(
"RemiseCheque::updateAmount ROLLBACK ($this->errno)");
700 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bank";
701 $sql .=
" SET fk_bordereau = 0";
702 $sql .=
" WHERE rowid = ".((int) $account_id);
703 $sql .=
" AND fk_bordereau = ".((int) $this->
id);
709 $this->errno = -1032;
710 dol_syslog(
"RemiseCheque::removeCheck ERREUR UPDATE ($this->errno)");
729 $payment->fetch(0, 0, $bank_id);
732 $bankline->fetch($bank_id);
746 $bankaccount = $payment->fk_account;
749 $sql =
'SELECT pf.fk_facture, pf.amount';
750 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf';
751 $sql .=
' WHERE pf.fk_paiement = '.((int) $payment->id);
756 $rejectedPayment->amounts = array();
757 $rejectedPayment->datepaye = $rejection_date;
758 $rejectedPayment->paiementid =
dol_getIdFromCode($this->
db,
'CHQ',
'c_paiement',
'code',
'id', 1);
759 $rejectedPayment->num_payment = $payment->num_payment;
761 while ($obj = $this->
db->fetch_object(
$resql)) {
763 $invoice->fetch($obj->fk_facture);
764 $invoice->setUnpaid($user);
766 $rejectedPayment->amounts[$obj->fk_facture] =
price2num($obj->amount) * -1;
769 $result = $rejectedPayment->create($user);
772 $result = $rejectedPayment->addPaymentToBank($user,
'payment',
'(CheckRejected)', $bankaccount,
'',
'');
774 $result = $payment->reject();
777 return $rejectedPayment->id;
779 $this->
db->rollback();
783 $this->error = $rejectedPayment->error;
784 $this->errors = $rejectedPayment->errors;
785 $this->
db->rollback();
789 $this->error = $rejectedPayment->error;
790 $this->errors = $rejectedPayment->errors;
791 $this->
db->rollback();
795 $this->error = $this->
db->lasterror();
796 $this->
db->rollback();
814 $sql =
"SELECT MAX(rowid)";
815 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bordereau_cheque";
816 $sql .=
" WHERE rowid < ".$this->id;
817 $sql .=
" AND entity = ".$conf->entity;
819 $result = $this->
db->query($sql);
821 $this->errno = -1035;
823 $row = $this->
db->fetch_row($result);
824 $this->previous_id = $row[0];
826 $sql =
"SELECT MIN(rowid)";
827 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bordereau_cheque";
828 $sql .=
" WHERE rowid > ".$this->id;
829 $sql .=
" AND entity = ".$conf->entity;
831 $result = $this->
db->query($sql);
833 $this->errno = -1035;
835 $row = $this->
db->fetch_row($result);
836 $this->next_id = $row[0];
853 if ($user->rights->banque->cheque) {
854 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bordereau_cheque";
855 $sql .=
" SET date_bordereau = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
856 $sql .=
" WHERE rowid = ".((int) $this->
id);
858 dol_syslog(
"RemiseCheque::set_date", LOG_DEBUG);
861 $this->date_bordereau = $date;
864 $this->error = $this->
db->error();
883 if ($user->rights->banque->cheque) {
884 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bordereau_cheque";
885 $sql .=
" SET ref = '".$this->db->escape($ref).
"'";
886 $sql .=
" WHERE rowid = ".((int) $this->
id);
888 dol_syslog(
"RemiseCheque::set_number", LOG_DEBUG);
893 $this->error = $this->
db->error();
911 global $user, $langs, $conf;
915 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
919 $this->
ref =
'SPECIMEN';
921 $this->date_bordereau = $nownotime;
934 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
936 global $conf, $langs;
940 $label =
'<u>'.$langs->trans(
"ShowCheckReceipt").
'</u>';
942 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
944 $url = DOL_URL_ROOT.
'/compta/paiement/cheque/card.php?id='.$this->id;
946 if ($option !=
'nolink') {
948 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
949 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
950 $add_save_lastsearch_values = 1;
952 if ($add_save_lastsearch_values) {
953 $url .=
'&save_lastsearch_values=1';
958 if (empty($notooltip)) {
959 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
960 $label = $langs->trans(
"ShowCheckReceipt");
961 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
963 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
964 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
966 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
969 $linkstart =
'<a href="'.$url.
'"';
970 $linkstart .= $linkclose.
'>';
973 $result .= $linkstart;
975 $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);
977 if ($withpicto != 2) {
978 $result .= $this->ref;
993 return $this->
LibStatut($this->statut, $mode);
1007 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1009 $langs->load(
'compta');
1010 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ToValidate');
1011 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'Validated');
1012 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ToValidate');
1013 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'Validated');
1016 $statusType =
'status'.$status;
1017 if ($status == self::STATUS_VALIDATED) {
1018 $statusType =
'status4';
1021 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);