dolibarr  16.0.1
mod_facture_fournisseur_cactus.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2013-2018 Philippe Grand <philippe.grand@atoo-net.com>
5  * Copyright (C) 2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  * or see https://www.gnu.org/
20  */
21 
28 require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
29 
30 
35 {
40  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
41 
45  public $error = '';
46 
52  public $nom = 'Cactus';
53 
57  public $name = 'Cactus';
58 
59  public $prefixinvoice = 'SI';
60 
61  public $prefixcreditnote = 'SA';
62 
63  public $prefixdeposit = 'SD';
64 
65 
71  public function info()
72  {
73  global $langs;
74  $langs->load("bills");
75  return $langs->trans("CactusNumRefModelDesc1", $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
76  }
77 
78 
84  public function getExample()
85  {
86  return $this->prefixinvoice."1301-0001";
87  }
88 
89 
95  public function canBeActivated()
96  {
97  global $conf, $langs, $db;
98 
99  $langs->load("bills");
100 
101  // Check invoice num
102  $siyymm = '';
103  $max = '';
104 
105  $posindice = strlen($this->prefixinvoice) + 6;
106  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
107  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
108  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
109  $sql .= " AND entity = ".$conf->entity;
110  $resql = $db->query($sql);
111  if ($resql) {
112  $row = $db->fetch_row($resql);
113  if ($row) {
114  $siyymm = substr($row[0], 0, 6);
115  $max = $row[0];
116  }
117  }
118  if ($siyymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
119  $langs->load("errors");
120  $this->error = $langs->trans('ErrorNumRefModel', $max);
121  return false;
122  }
123 
124  // Check credit note num
125  $siyymm = '';
126 
127  $posindice = strlen($this->prefixcreditnote) + 6;
128  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
129  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
130  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
131  $sql .= " AND entity = ".$conf->entity;
132 
133  $resql = $db->query($sql);
134  if ($resql) {
135  $row = $db->fetch_row($resql);
136  if ($row) {
137  $siyymm = substr($row[0], 0, 6);
138  $max = $row[0];
139  }
140  }
141  if ($siyymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
142  $this->error = $langs->trans('ErrorNumRefModel', $max);
143  return false;
144  }
145 
146  // Check deposit num
147  $siyymm = '';
148 
149  $posindice = strlen($this->prefixdeposit) + 6;
150  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
151  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
152  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
153  $sql .= " AND entity = ".$conf->entity;
154 
155  $resql = $db->query($sql);
156  if ($resql) {
157  $row = $db->fetch_row($resql);
158  if ($row) {
159  $siyymm = substr($row[0], 0, 6);
160  $max = $row[0];
161  }
162  }
163  if ($siyymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
164  $this->error = $langs->trans('ErrorNumRefModel', $max);
165  return false;
166  }
167  }
168 
177  public function getNextValue($objsoc, $object, $mode = 'next')
178  {
179  global $db, $conf;
180 
181  $prefix = $this->prefixinvoice;
182  if ($object->type == 2) {
183  $prefix = $this->prefixcreditnote;
184  } elseif ($object->type == 3) {
185  $prefix = $this->prefixdeposit;
186  }
187 
188  // First, we get the max value
189  $posindice = strlen($prefix) + 6;
190  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
191  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
192  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
193  $sql .= " AND entity = ".$conf->entity;
194 
195  $resql = $db->query($sql);
196  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
197  if ($resql) {
198  $obj = $db->fetch_object($resql);
199  if ($obj) {
200  $max = intval($obj->max);
201  } else {
202  $max = 0;
203  }
204  } else {
205  return -1;
206  }
207 
208  if ($mode == 'last') {
209  if ($max >= (pow(10, 4) - 1)) {
210  $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
211  } else {
212  $num = sprintf("%04s", $max);
213  }
214 
215  $ref = '';
216  $sql = "SELECT ref as ref";
217  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
218  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
219  $sql .= " AND entity = ".$conf->entity;
220 
221  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
222  $resql = $db->query($sql);
223  if ($resql) {
224  $obj = $db->fetch_object($resql);
225  if ($obj) {
226  $ref = $obj->ref;
227  }
228  } else {
229  dol_print_error($db);
230  }
231 
232  return $ref;
233  } elseif ($mode == 'next') {
234  $date = $object->date; // This is invoice date (not creation date)
235  $yymm = strftime("%y%m", $date);
236 
237  if ($max >= (pow(10, 4) - 1)) {
238  $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
239  } else {
240  $num = sprintf("%04s", $max + 1);
241  }
242 
243  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
244  return $prefix.$yymm."-".$num;
245  } else {
246  dol_print_error('', 'Bad parameter for getNextValue');
247  }
248  }
249 
250 
259  public function getNumRef($objsoc, $objforref, $mode = 'next')
260  {
261  return $this->getNextValue($objsoc, $objforref, $mode);
262  }
263 }
getNumRef($objsoc, $objforref, $mode= 'next')
Return next free value.
getExample()
Returns a numbering example.
info()
Return description of numbering model.
Parent Class of numbering models of suppliers invoices references.
getNextValue($objsoc, $object, $mode= 'next')
Return next value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
if(isModEnabled('facture')&&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur')&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)&&$user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice')&&$user->rights->supplier_invoice->lire)) if(isModEnabled('don')&&!empty($user->rights->don->lire)) if(isModEnabled('tax')&&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture')&&isModEnabled('commande')&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Cactus Class of numbering models of suppliers invoices references.
canBeActivated()
Tests if the numbers already in force in the database do not cause conflicts that would prevent this ...