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';
59 public $date_bordereau;
61 public $account_label;
70 const STATUS_DRAFT = 0;
71 const STATUS_VALIDATED = 1;
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, 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;
102 $sql .=
" AND bc.rowid = ".((int) $id);
105 $sql .=
" AND bc.ref = '".$this->db->escape($ref).
"'";
109 $resql = $this->db->query(
$sql);
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;
123 if ($this->statut == 0) {
124 $this->
ref =
"(PROV".$this->id.
")";
126 $this->
ref = $obj->ref;
129 $this->db->free($resql);
133 $this->error = $this->db->lasterror();
147 public function create($user, $account_id, $limit, $toRemise)
156 dol_syslog(
"RemiseCheque::Create start", LOG_DEBUG);
159 if (empty($this->
type)) {
165 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"bordereau_cheque (";
167 $sql .=
", date_bordereau";
168 $sql .=
", fk_user_author";
169 $sql .=
", fk_bank_account";
174 $sql .=
", nbcheque";
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);
185 $sql .=
", ".((int) $conf->entity);
188 $sql .=
", '".$this->db->escape($this->
type).
"'";
191 $resql = $this->db->query(
$sql);
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);
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);
204 $resql = $this->db->query(
$sql);
206 $this->errno = -1025;
207 dol_syslog(
"RemiseCheque::Create Error update ".$this->errno, LOG_ERR);
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);
221 $sql .= $this->db->plimit($limit);
224 dol_syslog(
"RemiseCheque::Create", LOG_DEBUG);
225 $resql = $this->db->query(
$sql);
227 while ($row = $this->db->fetch_row($resql)) {
228 array_push($lines, $row[0]);
230 $this->db->free($resql);
232 $this->errno = -1026;
233 dol_syslog(
"RemiseCheque::Create Error ".$this->errno, LOG_ERR);
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) {
247 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"bank";
248 $sql .=
" SET fk_bordereau = ".((int) $this->
id);
249 $sql .=
" WHERE rowid = ".((int) $lineid);
251 $resql = $this->db->query(
$sql);
254 dol_syslog(
"RemiseCheque::Create Error update bank ".$this->errno, LOG_ERR);
260 if ($this->
id > 0 && $this->errno == 0) {
262 $this->errno = -1027;
263 dol_syslog(
"RemiseCheque::Create Error update amount ".$this->errno, LOG_ERR);
268 $this->error = $this->db->lasterror();
269 $this->errno = $this->db->lasterrno();
279 dol_syslog(
"RemiseCheque::Create end", LOG_DEBUG);
282 $this->db->rollback();
283 dol_syslog(
"RemiseCheque::Create end", LOG_DEBUG);
294 public function delete($user =
'')
301 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"bordereau_cheque";
302 $sql .=
" WHERE rowid = ".((int) $this->
id);
303 $sql .=
" AND entity = ".$conf->entity;
305 $resql = $this->db->query(
$sql);
307 $num = $this->db->affected_rows($resql);
311 dol_syslog(
"Remisecheque::Delete Erreur Lecture ID ($this->errno)");
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);
319 $resql = $this->db->query(
$sql);
321 $this->errno = -1028;
322 dol_syslog(
"RemiseCheque::Delete ERREUR UPDATE ($this->errno)");
327 if ($this->errno === 0) {
330 $this->db->rollback();
331 dol_syslog(
"RemiseCheque::Delete ROLLBACK ($this->errno)");
345 global $langs, $conf;
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";
360 dol_syslog(
"RemiseCheque::Validate", LOG_DEBUG);
361 $resql = $this->db->query(
$sql);
363 $num = $this->db->affected_rows($resql);
366 $this->
ref = $numref;
369 $this->errno = -1029;
370 dol_syslog(
"Remisecheque::Validate Error ".$this->errno, LOG_ERR);
373 $this->errno = -1033;
374 dol_syslog(
"Remisecheque::Validate Error ".$this->errno, LOG_ERR);
379 if ($this->errno == 0) {
383 $this->db->rollback();
384 dol_syslog(
"RemiseCheque::Validate ".$this->errno, LOG_ERR);
398 global $conf, $db, $langs, $mysoc;
399 $langs->load(
"bills");
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';
410 if (!empty($conf->global->CHEQUERECEIPTS_ADDON)) {
413 $file = $conf->global->CHEQUERECEIPTS_ADDON.
".php";
414 $classname = $conf->global->CHEQUERECEIPTS_ADDON;
417 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
419 foreach ($dirmodels as $reldir) {
423 if (is_file($dir.$file) && is_readable($dir.$file)) {
424 $mybool |= include_once $dir.$file;
430 $file = $conf->global->CHEQUERECEIPTS_ADDON.
".php";
431 $classname =
"mod_chequereceipt_".$conf->global->CHEQUERECEIPTS_ADDON;
432 $classname = preg_replace(
'/\-.*$/',
'', $classname);
434 foreach ($conf->file->dol_document_root as $dirroot) {
435 $dir = $dirroot.
"/core/modules/cheque/";
438 if (is_file($dir.$file) && is_readable($dir.$file)) {
439 $mybool |= include_once $dir.$file;
449 $obj =
new $classname();
451 $numref = $obj->getNextValue($mysoc, $this);
457 if ($mode !=
'last' && !$numref) {
464 $langs->load(
"errors");
465 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Bank"));
482 global $conf, $langs;
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";
497 $resql = $this->db->query(
$sql);
499 $langs->load(
"banks");
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&mainmenu=bank';
509 while ($obj = $this->db->fetch_object($resql)) {
512 if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->cheque->warning_delay)) {
513 $response->nbtodolate++;
520 $this->error = $this->db->error();
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";
550 $resql = $this->db->query(
$sql);
552 while ($obj = $this->db->fetch_object($resql)) {
553 $this->nb[
"cheques"] = $obj->nb;
555 $this->db->free($resql);
559 $this->error = $this->db->error();
574 global $langs, $conf;
580 dol_syslog(
"RemiseCheque::generatePdf model=".$model.
" id=".$this->id, LOG_DEBUG);
582 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/cheque/doc/";
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;
590 $classname =
'BordereauCheque'.ucfirst($model);
591 $docmodel =
new $classname($this->db);
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";
603 dol_syslog(
"RemiseCheque::generatePdf", LOG_DEBUG);
604 $result = $this->db->query(
$sql);
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;
616 $docmodel->nbcheque = $this->nbcheque;
617 $docmodel->ref = $this->ref;
618 $docmodel->amount = $this->amount;
619 $docmodel->date = $this->date_bordereau;
621 $account =
new Account($this->db);
622 $account->fetch($this->account_id);
624 $docmodel->account = &$account;
628 $sav_charset_output = $outputlangs->charset_output;
630 $result = $docmodel->write_file($this, $conf->bank->dir_output.
'/checkdeposits', $this->ref, $outputlangs);
641 $this->error = $langs->trans(
"ErrorFileDoesNotExists", $dir.$file);
660 $sql =
"SELECT amount ";
661 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank";
662 $sql .=
" WHERE fk_bordereau = ".((int) $this->
id);
664 $resql = $this->db->query(
$sql);
666 while ($row = $this->db->fetch_row($resql)) {
671 $this->db->free($resql);
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);
679 $resql = $this->db->query(
$sql);
681 $this->errno = -1030;
682 dol_syslog(
"RemiseCheque::updateAmount ERREUR UPDATE ($this->errno)");
685 $this->errno = -1031;
686 dol_syslog(
"RemiseCheque::updateAmount ERREUR SELECT ($this->errno)");
689 if ($this->errno === 0) {
692 $this->db->rollback();
693 dol_syslog(
"RemiseCheque::updateAmount ROLLBACK ($this->errno)");
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);
715 $resql = $this->db->query(
$sql);
719 $this->errno = -1032;
720 dol_syslog(
"RemiseCheque::removeCheck ERREUR UPDATE ($this->errno)");
739 $payment->fetch(0, 0, $bank_id);
742 $bankline->fetch($bank_id);
756 $bankaccount = $payment->fk_account;
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);
763 $resql = $this->db->query(
$sql);
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;
771 while ($obj = $this->db->fetch_object($resql)) {
772 $invoice =
new Facture($this->db);
773 $invoice->fetch($obj->fk_facture);
774 $invoice->setUnpaid($user);
776 $rejectedPayment->amounts[$obj->fk_facture] =
price2num($obj->amount) * -1;
779 $result = $rejectedPayment->create($user);
782 $result = $rejectedPayment->addPaymentToBank($user,
'payment',
'(CheckRejected)', $bankaccount,
'',
'');
784 $result = $payment->reject();
787 return $rejectedPayment->id;
789 $this->db->rollback();
793 $this->error = $rejectedPayment->error;
794 $this->errors = $rejectedPayment->errors;
795 $this->db->rollback();
799 $this->error = $rejectedPayment->error;
800 $this->errors = $rejectedPayment->errors;
801 $this->db->rollback();
805 $this->error = $this->db->lasterror();
806 $this->db->rollback();
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);
827 dol_syslog(
"RemiseCheque::set_date", LOG_DEBUG);
828 $resql = $this->db->query(
$sql);
830 $this->date_bordereau = $date;
833 $this->error = $this->db->error();
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);
857 dol_syslog(
"RemiseCheque::set_number", LOG_DEBUG);
858 $resql = $this->db->query(
$sql);
862 $this->error = $this->db->error();
880 global $user, $langs, $conf;
884 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
888 $this->
ref =
'SPECIMEN';
890 $this->date_bordereau = $nownotime;
903 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
905 global $conf, $langs;
909 $label =
'<u>'.$langs->trans(
"ShowCheckReceipt").
'</u>';
911 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
913 $url = DOL_URL_ROOT.
'/compta/paiement/cheque/card.php?id='.$this->id;
915 if ($option !=
'nolink') {
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;
921 if ($add_save_lastsearch_values) {
922 $url .=
'&save_lastsearch_values=1';
927 if (empty($notooltip)) {
928 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
929 $label = $langs->trans(
"ShowCheckReceipt");
930 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
932 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
933 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
935 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
938 $linkstart =
'<a href="'.$url.
'"';
939 $linkstart .= $linkclose.
'>';
942 $result .= $linkstart;
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);
946 if ($withpicto != 2) {
947 $result .= $this->ref;
962 return $this->
LibStatut($this->statut, $mode);
976 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
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');
985 $statusType =
'status'.$status;
986 if ($status == self::STATUS_VALIDATED) {
987 $statusType =
'status4';
990 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1004 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
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">';
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"' :
'').
'>';
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>';
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>';
1021 if (property_exists($this,
'account_id')) {
1022 $return .=
' | <span class="info-box-label">'.$this->account_id.
'</span>';
1024 if (method_exists($this,
'LibStatut')) {
1025 $return .=
'<br><div style="display:inline-block" class="info-box-status margintoponly">'.$this->getLibStatut(3).
'</div>';
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>';
1030 $return .=
'</div>';
1031 $return .=
'</div>';
1032 $return .=
'</div>';