dolibarr/htdocs/compta/paiement/cheque/class/remisecheque.class.php

1024 lines
28 KiB
PHP
Raw Normal View History

2006-12-19 14:56:05 +01:00
<?php
2008-10-28 21:05:23 +01:00
/* Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
2011-09-03 02:27:02 +02:00
* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
2018-10-27 14:43:12 +02:00
* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
2016-04-03 11:56:31 +02:00
* Copyright (C) 2011-2016 Juanjo Menent <jmenent@2byte.es>
2015-02-15 15:07:24 +01:00
* Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
2006-12-19 14:56:05 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2006-12-19 14:56:05 +01:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2006-12-19 14:56:05 +01:00
*/
/**
2010-06-05 17:45:53 +02:00
* \file htdocs/compta/paiement/cheque/class/remisecheque.class.php
2008-10-28 21:05:23 +01:00
* \ingroup compta
* \brief File with class to manage cheque delivery receipts
2008-10-28 21:05:23 +01:00
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
2015-06-26 18:50:31 +02:00
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
2006-12-19 14:56:05 +01:00
/**
* Class to manage cheque delivery receipts
2008-10-28 21:05:23 +01:00
*/
class RemiseCheque extends CommonObject
2006-12-19 14:56:05 +01:00
{
2018-08-23 17:07:27 +02:00
/**
* @var string ID to identify managed object
*/
public $element = 'chequereceipt';
2018-09-02 20:37:12 +02:00
2018-08-22 18:34:50 +02:00
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element = 'bordereau_cheque';
2018-09-02 20:37:12 +02:00
2018-09-05 10:31:12 +02:00
/**
* @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
*/
2017-03-02 01:40:53 +01:00
public $picto = 'payment';
2017-06-09 09:25:15 +02:00
2018-09-05 10:31:12 +02:00
public $num;
public $intitule;
//! Numero d'erreur Plage 1024-1279
2018-09-05 10:31:12 +02:00
public $errno;
2006-12-19 14:56:05 +01:00
2015-09-06 19:24:17 +02:00
public $amount;
public $date_bordereau;
public $account_id;
public $account_label;
public $author_id;
public $nbcheque;
2018-08-31 18:47:25 +02:00
/**
* @var string Ref
*/
2016-04-03 11:56:31 +02:00
public $ref;
2019-11-07 16:40:59 +01:00
const STATUS_DRAFT = 0;
const STATUS_VALIDATED = 1;
/**
2011-09-11 20:35:38 +02:00
* Constructor
2011-09-03 02:27:02 +02:00
*
2012-07-29 15:28:39 +02:00
* @param DoliDB $db Database handler
2008-10-28 21:05:23 +01:00
*/
public function __construct($db)
{
$this->db = $db;
2008-10-28 21:05:23 +01:00
$this->next_id = 0;
$this->previous_id = 0;
}
2006-12-19 14:56:05 +01:00
2007-11-09 19:21:48 +01:00
/**
2011-09-03 02:27:02 +02:00
* Load record
*
* @param int $id Id record
* @param string $ref Ref record
* @return int <0 if KO, > 0 if OK
2008-12-02 00:09:52 +01:00
*/
public function fetch($id, $ref = '')
{
global $conf;
2016-04-03 11:56:31 +02:00
$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";
$sql .= ", bc.date_bordereau as date_bordereau";
$sql .= ", ba.label as account_label";
$sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque as bc";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_account as ba ON bc.fk_bank_account = ba.rowid";
$sql .= " WHERE bc.entity = ".$conf->entity;
2021-02-23 21:09:01 +01:00
if ($id) {
$sql .= " AND bc.rowid = ".((int) $id);
}
if ($ref) {
$sql .= " AND bc.ref = '".$this->db->escape($ref)."'";
}
2012-03-18 19:23:01 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog("RemiseCheque::fetch", LOG_DEBUG);
2007-11-09 19:21:48 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
if ($obj = $this->db->fetch_object($resql)) {
2007-11-09 19:21:48 +01:00
$this->id = $obj->rowid;
$this->amount = $obj->amount;
2010-05-08 21:31:43 +02:00
$this->date_bordereau = $this->db->jdate($obj->date_bordereau);
2007-11-09 19:21:48 +01:00
$this->account_id = $obj->fk_bank_account;
$this->account_label = $obj->account_label;
$this->author_id = $obj->fk_user_author;
$this->nbcheque = $obj->nbcheque;
$this->statut = $obj->statut;
2013-07-19 08:54:11 +02:00
$this->ref_ext = $obj->ref_ext;
2007-11-09 19:21:48 +01:00
2021-02-23 21:09:01 +01:00
if ($this->statut == 0) {
$this->ref = "(PROV".$this->id.")";
2020-05-21 15:05:19 +02:00
} else {
$this->ref = $obj->ref;
2007-11-09 19:21:48 +01:00
}
}
$this->db->free($resql);
return 1;
2020-05-21 15:05:19 +02:00
} else {
$this->error = $this->db->lasterror();
2007-11-09 19:21:48 +01:00
return -1;
}
}
2006-12-19 14:56:05 +01:00
/**
* Create a receipt to send cheques
2011-09-03 02:27:02 +02:00
*
2012-03-18 19:23:01 +01:00
* @param User $user User making creation
* @param int $account_id Bank account for cheque receipt
2016-04-03 11:56:31 +02:00
* @param int $limit Limit ref of cheque to this
2012-03-18 19:23:01 +01:00
* @param array $toRemise array with cheques to remise
* @return int <0 if KO, >0 if OK
2008-12-02 00:09:52 +01:00
*/
public function create($user, $account_id, $limit, $toRemise)
{
global $conf;
$this->errno = 0;
$this->id = 0;
$now = dol_now();
2018-10-19 14:59:00 +02:00
dol_syslog("RemiseCheque::Create start", LOG_DEBUG);
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."bordereau_cheque (";
$sql .= "datec";
$sql .= ", date_bordereau";
$sql .= ", fk_user_author";
$sql .= ", fk_bank_account";
$sql .= ", statut";
$sql .= ", amount";
$sql .= ", ref";
$sql .= ", entity";
$sql .= ", nbcheque";
$sql .= ", ref_ext";
$sql .= ") VALUES (";
$sql .= "'".$this->db->idate($now)."'";
$sql .= ", '".$this->db->idate($now)."'";
2021-09-03 21:25:17 +02:00
$sql .= ", ".((int) $user->id);
2021-04-24 21:15:09 +02:00
$sql .= ", ".((int) $account_id);
$sql .= ", 0";
$sql .= ", 0";
$sql .= ", 0";
2021-09-03 21:25:17 +02:00
$sql .= ", ".((int) $conf->entity);
$sql .= ", 0";
$sql .= ", ''";
$sql .= ")";
2008-10-28 21:05:23 +01:00
2006-12-19 14:56:05 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bordereau_cheque");
2021-02-23 21:09:01 +01:00
if ($this->id == 0) {
$this->errno = -1024;
dol_syslog("Remisecheque::Create Error read id ".$this->errno, LOG_ERR);
}
2021-02-23 21:09:01 +01:00
if ($this->id > 0 && $this->errno == 0) {
$sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
$sql .= " SET ref='(PROV".$this->id.")'";
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid=".((int) $this->id)."";
2008-10-28 21:05:23 +01:00
2007-11-09 19:21:48 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if (!$resql) {
$this->errno = -1025;
dol_syslog("RemiseCheque::Create Error update ".$this->errno, LOG_ERR);
2008-10-28 21:05:23 +01:00
}
}
2022-05-25 08:54:40 +02:00
$lines = array();
2021-02-23 21:09:01 +01:00
if ($this->id > 0 && $this->errno == 0) {
$sql = "SELECT b.rowid";
$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
$sql .= " WHERE b.fk_type = 'CHQ'";
$sql .= " AND b.amount > 0";
$sql .= " AND b.fk_bordereau = 0";
2020-09-19 23:30:29 +02:00
$sql .= " AND b.fk_account = ".((int) $account_id);
2021-02-23 21:09:01 +01:00
if ($limit) {
$sql .= $this->db->plimit($limit);
}
2014-06-12 11:31:53 +02:00
dol_syslog("RemiseCheque::Create", LOG_DEBUG);
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
while ($row = $this->db->fetch_row($resql)) {
array_push($lines, $row[0]);
}
$this->db->free($resql);
2020-05-21 15:05:19 +02:00
} else {
$this->errno = -1026;
dol_syslog("RemiseCheque::Create Error ".$this->errno, LOG_ERR);
}
}
2021-02-23 21:09:01 +01:00
if ($this->id > 0 && $this->errno == 0) {
foreach ($lines as $lineid) {
$checkremise = false;
2021-02-23 21:09:01 +01:00
foreach ($toRemise as $linetoremise) {
if ($linetoremise == $lineid) {
$checkremise = true;
}
}
2021-02-23 21:09:01 +01:00
if ($checkremise) {
$sql = "UPDATE ".MAIN_DB_PREFIX."bank";
2021-03-30 03:37:54 +02:00
$sql .= " SET fk_bordereau = ".((int) $this->id);
$sql .= " WHERE rowid = ".((int) $lineid);
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if (!$resql) {
$this->errno = -18;
dol_syslog("RemiseCheque::Create Error update bank ".$this->errno, LOG_ERR);
}
}
}
}
2021-02-23 21:09:01 +01:00
if ($this->id > 0 && $this->errno == 0) {
if ($this->updateAmount() <> 0) {
$this->errno = -1027;
dol_syslog("RemiseCheque::Create Error update amount ".$this->errno, LOG_ERR);
}
}
2020-05-21 15:05:19 +02:00
} else {
$this->errno = -1;
$this->error = $this->db->lasterror();
$this->errno = $this->db->lasterrno();
}
2008-10-28 21:05:23 +01:00
2021-02-23 21:09:01 +01:00
if (!$this->errno && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS)) {
$res = $this->validate($user);
//if ($res < 0) $error++;
}
2021-02-23 21:09:01 +01:00
if (!$this->errno) {
$this->db->commit();
dol_syslog("RemiseCheque::Create end", LOG_DEBUG);
return $this->id;
} else {
$this->db->rollback();
dol_syslog("RemiseCheque::Create end", LOG_DEBUG);
return $this->errno;
}
}
2006-12-19 14:56:05 +01:00
2008-10-28 21:05:23 +01:00
/**
2010-07-22 01:45:08 +02:00
* Supprime la remise en base
2011-09-03 02:27:02 +02:00
*
2012-03-18 19:23:01 +01:00
* @param User $user Utilisateur qui effectue l'operation
* @return int
2008-10-28 21:05:23 +01:00
*/
public function delete($user = '')
{
global $conf;
2008-10-28 21:05:23 +01:00
$this->errno = 0;
$this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."bordereau_cheque";
$sql .= " WHERE rowid = ".((int) $this->id);
$sql .= " AND entity = ".$conf->entity;
2008-10-28 21:05:23 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
2008-10-28 21:05:23 +01:00
$num = $this->db->affected_rows($resql);
if ($num <> 1) {
$this->errno = -2;
dol_syslog("Remisecheque::Delete Erreur Lecture ID ($this->errno)");
}
2006-12-19 16:01:34 +01:00
if ($this->errno === 0) {
$sql = "UPDATE ".MAIN_DB_PREFIX."bank";
$sql .= " SET fk_bordereau = 0";
2021-08-27 16:33:03 +02:00
$sql .= " WHERE fk_bordereau = ".((int) $this->id);
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if (!$resql) {
$this->errno = -1028;
dol_syslog("RemiseCheque::Delete ERREUR UPDATE ($this->errno)");
}
}
2008-10-28 21:05:23 +01:00
}
2021-02-23 21:09:01 +01:00
if ($this->errno === 0) {
2008-10-28 21:05:23 +01:00
$this->db->commit();
2020-05-21 15:05:19 +02:00
} else {
2008-10-28 21:05:23 +01:00
$this->db->rollback();
dol_syslog("RemiseCheque::Delete ROLLBACK ($this->errno)");
2008-10-28 21:05:23 +01:00
}
return $this->errno;
}
/**
* Validate a receipt
2011-09-03 02:27:02 +02:00
*
2012-03-18 19:23:01 +01:00
* @param User $user User
* @return int <0 if KO, >0 if OK
2008-10-28 21:05:23 +01:00
*/
public function validate($user)
{
global $langs, $conf;
2009-03-09 22:59:29 +01:00
2008-10-28 21:05:23 +01:00
$this->errno = 0;
2008-11-28 19:42:11 +01:00
2008-10-28 21:05:23 +01:00
$this->db->begin();
2017-06-09 09:25:15 +02:00
2016-04-03 12:14:51 +02:00
$numref = $this->getNextNumRef();
2009-03-09 22:59:29 +01:00
2021-02-23 21:09:01 +01:00
if ($this->errno == 0 && $numref) {
2008-10-28 21:05:23 +01:00
$sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
2020-09-19 20:11:04 +02:00
$sql .= " SET statut = 1, ref = '".$this->db->escape($numref)."'";
$sql .= " WHERE rowid = ".((int) $this->id);
$sql .= " AND entity = ".$conf->entity;
$sql .= " AND statut = 0";
2008-10-28 21:05:23 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog("RemiseCheque::Validate", LOG_DEBUG);
2008-10-28 21:05:23 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
2008-10-28 21:05:23 +01:00
$num = $this->db->affected_rows($resql);
2021-02-23 21:09:01 +01:00
if ($num == 1) {
$this->ref = $numref;
2008-10-28 21:05:23 +01:00
$this->statut = 1;
2020-05-21 15:05:19 +02:00
} else {
2008-10-28 21:05:23 +01:00
$this->errno = -1029;
dol_syslog("Remisecheque::Validate Error ".$this->errno, LOG_ERR);
2008-10-28 21:05:23 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2008-10-28 21:05:23 +01:00
$this->errno = -1033;
dol_syslog("Remisecheque::Validate Error ".$this->errno, LOG_ERR);
2008-10-28 21:05:23 +01:00
}
}
2008-11-28 19:42:11 +01:00
// Commit/Rollback
2021-02-23 21:09:01 +01:00
if ($this->errno == 0) {
2008-10-28 21:05:23 +01:00
$this->db->commit();
return 1;
2020-05-21 15:05:19 +02:00
} else {
2008-10-28 21:05:23 +01:00
$this->db->rollback();
dol_syslog("RemiseCheque::Validate ".$this->errno, LOG_ERR);
return $this->errno;
2008-10-28 21:05:23 +01:00
}
}
2016-04-03 11:56:31 +02:00
/**
* Return next reference of cheque receipts not already used (or last reference)
* according to numbering module defined into constant FACTURE_ADDON
*
* @param string $mode 'next' for next value or 'last' for last value
* @return string free ref or last ref
*/
public function getNextNumRef($mode = 'next')
{
2016-04-03 11:56:31 +02:00
global $conf, $db, $langs, $mysoc;
$langs->load("bills");
// Clean parameters (if not defined or using deprecated value)
2021-02-23 21:09:01 +01:00
if (empty($conf->global->CHEQUERECEIPTS_ADDON)) {
$conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_mint';
} elseif ($conf->global->CHEQUERECEIPTS_ADDON == 'thyme') {
$conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_thyme';
} elseif ($conf->global->CHEQUERECEIPTS_ADDON == 'mint') {
$conf->global->CHEQUERECEIPTS_ADDON = 'mod_chequereceipt_mint';
}
2016-04-03 11:56:31 +02:00
2021-02-23 21:09:01 +01:00
if (!empty($conf->global->CHEQUERECEIPTS_ADDON)) {
$mybool = false;
2016-04-03 11:56:31 +02:00
$file = $conf->global->CHEQUERECEIPTS_ADDON.".php";
$classname = $conf->global->CHEQUERECEIPTS_ADDON;
// Include file with class
$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
foreach ($dirmodels as $reldir) {
$dir = dol_buildpath($reldir."core/modules/cheque/");
// Load file with numbering class (if found)
2021-02-23 21:09:01 +01:00
if (is_file($dir.$file) && is_readable($dir.$file)) {
$mybool |= include_once $dir.$file;
2016-04-03 11:56:31 +02:00
}
}
// For compatibility
2021-02-23 21:09:01 +01:00
if (!$mybool) {
2016-04-03 11:56:31 +02:00
$file = $conf->global->CHEQUERECEIPTS_ADDON.".php";
$classname = "mod_chequereceipt_".$conf->global->CHEQUERECEIPTS_ADDON;
$classname = preg_replace('/\-.*$/', '', $classname);
2016-04-03 11:56:31 +02:00
// Include file with class
2021-02-23 21:09:01 +01:00
foreach ($conf->file->dol_document_root as $dirroot) {
2016-04-03 11:56:31 +02:00
$dir = $dirroot."/core/modules/cheque/";
// Load file with numbering class (if found)
if (is_file($dir.$file) && is_readable($dir.$file)) {
$mybool |= include_once $dir.$file;
2016-04-03 11:56:31 +02:00
}
}
}
2021-02-23 21:09:01 +01:00
if (!$mybool) {
dol_print_error('', "Failed to include file ".$file);
2016-04-03 11:56:31 +02:00
return '';
}
$obj = new $classname();
$numref = "";
$numref = $obj->getNextValue($mysoc, $this);
2016-04-03 11:56:31 +02:00
/**
* $numref can be empty in case we ask for the last value because if there is no invoice created with the
* set up mask.
*/
if ($mode != 'last' && !$numref) {
dol_print_error($db, "ChequeReceipts::getNextNumRef ".$obj->error);
2016-04-03 11:56:31 +02:00
return "";
}
return $numref;
2020-05-21 15:05:19 +02:00
} else {
2016-04-03 11:56:31 +02:00
$langs->load("errors");
2019-08-01 12:23:02 +02:00
print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Bank"));
2016-04-03 11:56:31 +02:00
return "";
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-28 21:05:23 +01:00
/**
* Load indicators for dashboard (this->nbtodo and this->nbtodolate)
*
* @param User $user Objet user
* @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
2008-10-28 21:05:23 +01:00
*/
public function load_board($user)
{
// phpcs:enable
2015-02-15 15:01:28 +01:00
global $conf, $langs;
2021-02-23 21:09:01 +01:00
if ($user->socid) {
return -1; // protection pour eviter appel par utilisateur externe
}
$sql = "SELECT b.rowid, b.datev as datefin";
$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
$sql .= " WHERE b.fk_account = ba.rowid";
$sql .= " AND ba.entity IN (".getEntity('bank_account').")";
$sql .= " AND b.fk_type = 'CHQ'";
$sql .= " AND b.fk_bordereau = 0";
$sql .= " AND b.amount > 0";
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
2015-02-15 15:01:28 +01:00
$langs->load("banks");
$now = dol_now();
$response = new WorkboardResponse();
$response->warning_delay = $conf->bank->cheque->warning_delay / 60 / 60 / 24;
$response->label = $langs->trans("BankChecksToReceipt");
$response->labelShort = $langs->trans("BankChecksToReceiptShort");
$response->url = DOL_URL_ROOT.'/compta/paiement/cheque/index.php?leftmenu=checks&amp;mainmenu=bank';
$response->img = img_object('', "payment");
2021-02-23 21:09:01 +01:00
while ($obj = $this->db->fetch_object($resql)) {
2015-02-15 15:01:28 +01:00
$response->nbtodo++;
2015-02-15 15:01:28 +01:00
if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->cheque->warning_delay)) {
$response->nbtodolate++;
}
2008-10-28 21:05:23 +01:00
}
2015-02-15 15:01:28 +01:00
return $response;
2020-05-21 15:05:19 +02:00
} else {
dol_print_error($this->db);
$this->error = $this->db->error();
2008-10-28 21:05:23 +01:00
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Charge indicateurs this->nb de tableau de bord
*
* @return int <0 if ko, >0 if ok
*/
public function load_state_board()
{
// phpcs:enable
global $user;
2021-02-23 21:09:01 +01:00
if ($user->socid) {
return -1; // protection pour eviter appel par utilisateur externe
}
$sql = "SELECT count(b.rowid) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
$sql .= " WHERE b.fk_account = ba.rowid";
$sql .= " AND ba.entity IN (".getEntity('bank_account').")";
$sql .= " AND b.fk_type = 'CHQ'";
$sql .= " AND b.amount > 0";
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
while ($obj = $this->db->fetch_object($resql)) {
$this->nb["cheques"] = $obj->nb;
}
$this->db->free($resql);
return 1;
2020-05-21 15:05:19 +02:00
} else {
dol_print_error($this->db);
$this->error = $this->db->error();
return -1;
}
}
2008-10-28 21:05:23 +01:00
/**
* Build document
2011-12-05 18:36:54 +01:00
*
2012-03-18 19:23:01 +01:00
* @param string $model Model name
* @param Translate $outputlangs Object langs
2012-03-18 19:23:01 +01:00
* @return int <0 if KO, >0 if OK
2008-10-28 21:05:23 +01:00
*/
public function generatePdf($model, $outputlangs)
{
global $langs, $conf;
2009-03-09 22:59:29 +01:00
2021-02-23 21:09:01 +01:00
if (empty($model)) {
$model = 'blochet';
}
2009-03-09 22:59:29 +01:00
dol_syslog("RemiseCheque::generatePdf model=".$model." id=".$this->id, LOG_DEBUG);
2008-12-02 00:51:52 +01:00
$dir = DOL_DOCUMENT_ROOT."/core/modules/cheque/doc/";
2008-12-02 00:51:52 +01:00
// Charge le modele
$file = "pdf_".$model.".class.php";
2021-02-23 21:09:01 +01:00
if (file_exists($dir.$file)) {
include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
2018-07-26 11:57:25 +02:00
include_once $dir.$file;
2008-12-02 00:51:52 +01:00
$classname = 'BordereauCheque'.ucfirst($model);
2011-12-05 18:36:54 +01:00
$docmodel = new $classname($this->db);
2008-12-02 00:51:52 +01:00
$sql = "SELECT b.banque, b.emetteur, b.amount, b.num_chq";
$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
$sql .= ", ".MAIN_DB_PREFIX."bordereau_cheque as bc";
$sql .= " WHERE b.fk_account = ba.rowid";
$sql .= " AND b.fk_bordereau = bc.rowid";
$sql .= " AND bc.rowid = ".((int) $this->id);
$sql .= " AND bc.entity = ".$conf->entity;
$sql .= " ORDER BY b.dateo ASC, b.rowid ASC";
2008-12-02 00:51:52 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog("RemiseCheque::generatePdf", LOG_DEBUG);
2008-12-02 00:51:52 +01:00
$result = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($result) {
2008-12-02 00:51:52 +01:00
$i = 0;
2021-02-23 21:09:01 +01:00
while ($objp = $this->db->fetch_object($result)) {
2013-01-19 14:32:37 +01:00
$docmodel->lines[$i] = new stdClass();
$docmodel->lines[$i]->bank_chq = $objp->banque;
$docmodel->lines[$i]->emetteur_chq = $objp->emetteur;
$docmodel->lines[$i]->amount_chq = $objp->amount;
$docmodel->lines[$i]->num_chq = $objp->num_chq;
2008-12-02 00:51:52 +01:00
$i++;
}
2008-10-28 21:05:23 +01:00
}
$docmodel->nbcheque = $this->nbcheque;
2016-04-03 11:56:31 +02:00
$docmodel->ref = $this->ref;
$docmodel->amount = $this->amount;
$docmodel->date = $this->date_bordereau;
2008-10-28 21:05:23 +01:00
2008-12-02 00:51:52 +01:00
$account = new Account($this->db);
$account->fetch($this->account_id);
2008-10-28 21:05:23 +01:00
$docmodel->account = &$account;
2008-10-28 21:05:23 +01:00
2008-12-02 00:51:52 +01:00
// We save charset_output to restore it because write_file can change it if needed for
// output format that does not support UTF8.
$sav_charseSupprimert_output = $outputlangs->charset_output;
2022-03-07 13:21:32 +01:00
$result = $docmodel->write_file($this, $conf->bank->dir_output.'/checkdeposits', $this->ref, $outputlangs);
2021-02-23 21:09:01 +01:00
if ($result > 0) {
2011-12-05 18:36:54 +01:00
//$outputlangs->charset_output=$sav_charset_output;
2008-12-02 00:51:52 +01:00
return 1;
2020-05-21 15:05:19 +02:00
} else {
2011-12-05 18:36:54 +01:00
//$outputlangs->charset_output=$sav_charset_output;
dol_syslog("Error");
dol_print_error($this->db, $docmodel->error);
2008-12-02 00:51:52 +01:00
return 0;
}
2020-05-21 15:05:19 +02:00
} else {
$this->error = $langs->trans("ErrorFileDoesNotExists", $dir.$file);
2008-12-02 00:51:52 +01:00
return -1;
2008-10-28 21:05:23 +01:00
}
}
2008-10-28 21:05:23 +01:00
/**
2012-03-18 19:23:01 +01:00
* Mets a jour le montant total
*
* @return int 0 en cas de succes
2008-10-28 21:05:23 +01:00
*/
public function updateAmount()
{
global $conf;
2008-10-28 21:05:23 +01:00
$this->errno = 0;
2020-09-19 20:11:04 +02:00
2008-10-28 21:05:23 +01:00
$this->db->begin();
$total = 0;
$nb = 0;
$sql = "SELECT amount ";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
2021-08-27 16:33:03 +02:00
$sql .= " WHERE fk_bordereau = ".((int) $this->id);
2008-10-28 21:05:23 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
while ($row = $this->db->fetch_row($resql)) {
$total += $row[0];
$nb++;
}
2008-10-28 21:05:23 +01:00
$this->db->free($resql);
2008-10-28 21:05:23 +01:00
$sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
2020-09-19 20:11:04 +02:00
$sql .= " SET amount = ".price2num($total);
$sql .= ", nbcheque = ".((int) $nb);
$sql .= " WHERE rowid = ".((int) $this->id);
$sql .= " AND entity = ".$conf->entity;
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if (!$resql) {
$this->errno = -1030;
dol_syslog("RemiseCheque::updateAmount ERREUR UPDATE ($this->errno)");
}
2020-05-21 15:05:19 +02:00
} else {
2008-10-28 21:05:23 +01:00
$this->errno = -1031;
2010-07-21 19:39:17 +02:00
dol_syslog("RemiseCheque::updateAmount ERREUR SELECT ($this->errno)");
2008-10-28 21:05:23 +01:00
}
2021-02-23 21:09:01 +01:00
if ($this->errno === 0) {
2008-10-28 21:05:23 +01:00
$this->db->commit();
2020-05-21 15:05:19 +02:00
} else {
2008-10-28 21:05:23 +01:00
$this->db->rollback();
2010-07-21 19:39:17 +02:00
dol_syslog("RemiseCheque::updateAmount ROLLBACK ($this->errno)");
2008-10-28 21:05:23 +01:00
}
return $this->errno;
}
2008-10-28 21:05:23 +01:00
/**
2012-03-18 19:23:01 +01:00
* Insere la remise en base
*
* @param int $account_id Compte bancaire concerne
* @return int
2008-10-28 21:05:23 +01:00
*/
public function removeCheck($account_id)
{
2008-10-28 21:05:23 +01:00
$this->errno = 0;
2021-02-23 21:09:01 +01:00
if ($this->id > 0) {
2008-10-28 21:05:23 +01:00
$sql = "UPDATE ".MAIN_DB_PREFIX."bank";
$sql .= " SET fk_bordereau = 0";
2020-09-19 20:11:04 +02:00
$sql .= " WHERE rowid = ".((int) $account_id);
$sql .= " AND fk_bordereau = ".((int) $this->id);
2008-10-28 21:05:23 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
2015-06-26 18:50:31 +02:00
$this->updateAmount();
2020-05-21 15:05:19 +02:00
} else {
2015-06-26 18:50:31 +02:00
$this->errno = -1032;
dol_syslog("RemiseCheque::removeCheck ERREUR UPDATE ($this->errno)");
}
2008-10-28 21:05:23 +01:00
}
return 0;
}
2015-07-04 04:34:14 +02:00
2015-06-26 18:50:31 +02:00
/**
* Check return management
* Reopen linked invoices and create a new negative payment.
2015-06-26 18:50:31 +02:00
*
* @param int $bank_id Id of bank transaction line concerned
2019-04-08 16:04:15 +02:00
* @param integer $rejection_date Date to use on the negative payment
2017-06-09 09:25:15 +02:00
* @return int Id of negative payment line created
2015-06-26 18:50:31 +02:00
*/
public function rejectCheck($bank_id, $rejection_date)
{
2015-06-26 18:50:31 +02:00
global $db, $user;
2015-07-04 04:34:14 +02:00
2015-06-26 18:50:31 +02:00
$payment = new Paiement($db);
$payment->fetch(0, 0, $bank_id);
2015-07-04 04:34:14 +02:00
$bankline = new AccountLine($db);
$bankline->fetch($bank_id);
2017-06-09 09:25:15 +02:00
/* Reconciliation is allowed because when check is returned, a new line is created onto bank transaction log.
if ($bankline->rappro)
{
2021-02-23 21:09:01 +01:00
$this->error='ActionRefusedLineAlreadyConciliated';
return -1;
}*/
2017-06-09 09:25:15 +02:00
$this->db->begin();
2017-06-09 09:25:15 +02:00
// Not reconciled, we can delete it
2017-06-09 09:25:15 +02:00
//$bankline->delete($user); // We delete
2015-06-26 18:50:31 +02:00
$bankaccount = $payment->fk_account;
2015-07-04 04:34:14 +02:00
// Get invoices list to reopen them
2015-06-26 18:50:31 +02:00
$sql = 'SELECT pf.fk_facture, pf.amount';
$sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf';
$sql .= ' WHERE pf.fk_paiement = '.((int) $payment->id);
2015-07-04 04:34:14 +02:00
2020-09-19 23:30:29 +02:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
2020-09-19 23:30:29 +02:00
$rejectedPayment = new Paiement($this->db);
2015-06-26 18:50:31 +02:00
$rejectedPayment->amounts = array();
$rejectedPayment->datepaye = $rejection_date;
$rejectedPayment->paiementid = dol_getIdFromCode($this->db, 'CHQ', 'c_paiement', 'code', 'id', 1);
$rejectedPayment->num_payment = $payment->num_payment;
2015-07-04 04:34:14 +02:00
2021-02-23 21:09:01 +01:00
while ($obj = $this->db->fetch_object($resql)) {
2020-09-19 23:30:29 +02:00
$invoice = new Facture($this->db);
2015-06-26 18:50:31 +02:00
$invoice->fetch($obj->fk_facture);
2021-02-09 09:40:07 +01:00
$invoice->setUnpaid($user);
2015-07-04 04:34:14 +02:00
2015-06-26 18:50:31 +02:00
$rejectedPayment->amounts[$obj->fk_facture] = price2num($obj->amount) * -1;
}
2015-07-04 04:34:14 +02:00
$result = $rejectedPayment->create($user);
2021-02-23 21:09:01 +01:00
if ($result > 0) {
// We created a negative payment, we also add the line as bank transaction
$result = $rejectedPayment->addPaymentToBank($user, 'payment', '(CheckRejected)', $bankaccount, '', '');
2021-02-23 21:09:01 +01:00
if ($result > 0) {
$result = $payment->reject();
2021-02-23 21:09:01 +01:00
if ($result > 0) {
$this->db->commit();
return $rejectedPayment->id;
2020-05-21 15:05:19 +02:00
} else {
$this->db->rollback();
return -1;
}
2020-05-21 15:05:19 +02:00
} else {
$this->error = $rejectedPayment->error;
$this->errors = $rejectedPayment->errors;
$this->db->rollback();
2015-06-26 18:50:31 +02:00
return -1;
}
2020-05-21 15:05:19 +02:00
} else {
$this->error = $rejectedPayment->error;
$this->errors = $rejectedPayment->errors;
$this->db->rollback();
2015-06-26 18:50:31 +02:00
return -1;
}
2020-05-21 15:05:19 +02:00
} else {
$this->error = $this->db->lasterror();
$this->db->rollback();
2015-06-26 18:50:31 +02:00
return -1;
}
}
2015-07-04 04:34:14 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-28 21:05:23 +01:00
/**
2012-03-18 19:23:01 +01:00
* Charge les proprietes ref_previous et ref_next
*
2018-08-13 17:26:32 +02:00
* @return int <0 if KO, 0 if OK
2008-10-28 21:05:23 +01:00
*/
public function load_previous_next_id()
{
// phpcs:enable
global $conf;
2008-10-28 21:05:23 +01:00
$this->errno = 0;
$sql = "SELECT MAX(rowid)";
$sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque";
$sql .= " WHERE rowid < ".$this->id;
$sql .= " AND entity = ".$conf->entity;
2008-10-28 21:05:23 +01:00
$result = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if (!$result) {
2008-10-28 21:05:23 +01:00
$this->errno = -1035;
}
$row = $this->db->fetch_row($result);
$this->previous_id = $row[0];
$sql = "SELECT MIN(rowid)";
$sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque";
$sql .= " WHERE rowid > ".$this->id;
$sql .= " AND entity = ".$conf->entity;
$result = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if (!$result) {
2008-10-28 21:05:23 +01:00
$this->errno = -1035;
}
$row = $this->db->fetch_row($result);
$this->next_id = $row[0];
return $this->errno;
}
2006-12-22 12:34:36 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Set the creation date
*
* @param User $user Object user
* @param int $date Date creation
* @return int <0 if KO, >0 if OK
*/
public function set_date($user, $date)
{
// phpcs:enable
2021-02-23 21:09:01 +01:00
if ($user->rights->banque->cheque) {
$sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
$sql .= " SET date_bordereau = ".($date ? "'".$this->db->idate($date)."'" : 'null');
$sql .= " WHERE rowid = ".((int) $this->id);
dol_syslog("RemiseCheque::set_date", LOG_DEBUG);
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
$this->date_bordereau = $date;
return 1;
} else {
$this->error = $this->db->error();
return -1;
}
} else {
return -2;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
2016-04-03 11:56:31 +02:00
* Set the ref of bordereau
*
* @param User $user Object user
2016-04-03 11:56:31 +02:00
* @param int $ref ref of bordereau
* @return int <0 if KO, >0 if OK
*/
public function set_number($user, $ref)
{
// phpcs:enable
2021-02-23 21:09:01 +01:00
if ($user->rights->banque->cheque) {
$sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
$sql .= " SET ref = '".$this->db->escape($ref)."'";
$sql .= " WHERE rowid = ".((int) $this->id);
2015-07-04 04:34:14 +02:00
2014-07-02 21:29:07 +02:00
dol_syslog("RemiseCheque::set_number", LOG_DEBUG);
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
return 1;
2020-05-21 15:05:19 +02:00
} else {
$this->error = $this->db->error();
return -1;
}
2020-05-21 15:05:19 +02:00
} else {
return -2;
}
}
2016-04-03 11:56:31 +02:00
/**
* Initialise an instance with random values.
* Used to build previews or test instances.
* id must be 0 if object instance is a specimen.
*
* @param string $option ''=Create a specimen invoice with lines, 'nolines'=No lines
* @return void
*/
public function initAsSpecimen($option = '')
{
global $user, $langs, $conf;
2016-04-03 11:56:31 +02:00
$now = dol_now();
$arraynow = dol_getdate($now);
$nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
2016-04-03 11:56:31 +02:00
// Initialize parameters
$this->id = 0;
2016-04-03 11:56:31 +02:00
$this->ref = 'SPECIMEN';
$this->specimen = 1;
2016-04-03 11:56:31 +02:00
$this->date_bordereau = $nownotime;
}
2016-04-03 11:56:31 +02:00
/**
2018-03-01 11:51:13 +01:00
* Return clicable name (with picto eventually)
2012-03-18 19:23:01 +01:00
*
2018-03-01 11:51:13 +01:00
* @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
* @param string $option Sur quoi pointe le lien
* @param int $notooltip 1=Disable tooltip
* @param string $morecss Add more css on link
* @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
2018-03-01 11:51:13 +01:00
* @return string Chaine avec URL
*/
public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
{
2018-03-01 11:51:13 +01:00
global $conf, $langs;
2008-10-28 21:05:23 +01:00
$result = '';
2018-03-01 11:51:13 +01:00
$label = '<u>'.$langs->trans("ShowCheckReceipt").'</u>';
$label .= '<br>';
$label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
2018-03-01 11:51:13 +01:00
$url = DOL_URL_ROOT.'/compta/paiement/cheque/card.php?id='.$this->id;
2021-02-23 21:09:01 +01:00
if ($option != 'nolink') {
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2021-02-23 21:09:01 +01:00
if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
$add_save_lastsearch_values = 1;
}
if ($add_save_lastsearch_values) {
$url .= '&save_lastsearch_values=1';
}
}
$linkclose = '';
2021-02-23 21:09:01 +01:00
if (empty($notooltip)) {
if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
$label = $langs->trans("ShowCheckReceipt");
$linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
}
$linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
$linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
2021-02-23 21:09:01 +01:00
} else {
$linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
}
2018-03-01 11:51:13 +01:00
$linkstart = '<a href="'.$url.'"';
$linkstart .= $linkclose.'>';
$linkend = '</a>';
2018-03-01 11:51:13 +01:00
$result .= $linkstart;
2021-02-23 21:09:01 +01:00
if ($withpicto) {
$result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
}
if ($withpicto != 2) {
$result .= $this->ref;
}
2018-03-01 11:51:13 +01:00
$result .= $linkend;
return $result;
}
/**
2012-03-18 19:23:01 +01:00
* Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
*
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @return string Libelle
2008-10-28 21:05:23 +01:00
*/
public function getLibStatut($mode = 0)
{
return $this->LibStatut($this->statut, $mode);
}
2008-10-28 21:05:23 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
2012-03-18 19:23:01 +01:00
* Return label of a status
*
2019-11-07 16:40:59 +01:00
* @param int $status Id status
2017-03-02 01:40:53 +01:00
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto, 6=Long label + picto
2012-03-18 19:23:01 +01:00
* @return string Libelle du statut
2008-10-28 21:05:23 +01:00
*/
public function LibStatut($status, $mode = 0)
{
// phpcs:enable
2021-02-23 21:09:01 +01:00
if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
global $langs;
$langs->load('compta');
2021-10-16 19:37:57 +02:00
$this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ToValidate');
$this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
$this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ToValidate');
$this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
}
$statusType = 'status'.$status;
2021-02-23 21:09:01 +01:00
if ($status == self::STATUS_VALIDATED) {
$statusType = 'status4';
}
return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
}
2006-12-19 14:56:05 +01:00
}