dolibarr  19.0.0-dev
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  return true;
169  }
170 
179  public function getNextValue($objsoc, $object, $mode = 'next')
180  {
181  global $db, $conf;
182 
183  $prefix = $this->prefixinvoice;
184  if ($object->type == 2) {
185  $prefix = $this->prefixcreditnote;
186  } elseif ($object->type == 3) {
187  $prefix = $this->prefixdeposit;
188  }
189 
190  // First, we get the max value
191  $posindice = strlen($prefix) + 6;
192  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
193  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
194  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
195  $sql .= " AND entity = ".$conf->entity;
196 
197  $resql = $db->query($sql);
198  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
199  if ($resql) {
200  $obj = $db->fetch_object($resql);
201  if ($obj) {
202  $max = intval($obj->max);
203  } else {
204  $max = 0;
205  }
206  } else {
207  return -1;
208  }
209 
210  if ($mode == 'last') {
211  if ($max >= (pow(10, 4) - 1)) {
212  $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
213  } else {
214  $num = sprintf("%04s", $max);
215  }
216 
217  $ref = '';
218  $sql = "SELECT ref as ref";
219  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
220  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
221  $sql .= " AND entity = ".$conf->entity;
222 
223  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
224  $resql = $db->query($sql);
225  if ($resql) {
226  $obj = $db->fetch_object($resql);
227  if ($obj) {
228  $ref = $obj->ref;
229  }
230  } else {
231  dol_print_error($db);
232  }
233 
234  return $ref;
235  } elseif ($mode == 'next') {
236  $date = $object->date; // This is invoice date (not creation date)
237  $yymm = strftime("%y%m", $date);
238 
239  if ($max >= (pow(10, 4) - 1)) {
240  $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
241  } else {
242  $num = sprintf("%04s", $max + 1);
243  }
244 
245  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
246  return $prefix.$yymm."-".$num;
247  } else {
248  dol_print_error('', 'Bad parameter for getNextValue');
249  return -1;
250  }
251  }
252 
253 
262  public function getNumRef($objsoc, $objforref, $mode = 'next')
263  {
264  return $this->getNextValue($objsoc, $objforref, $mode);
265  }
266 }
Parent Class of numbering models of suppliers invoices references.
Cactus Class of numbering models of suppliers invoices references.
getNextValue($objsoc, $object, $mode='next')
Return next value.
canBeActivated()
Tests if the numbers already in the database do not cause conflicts that would prevent this numbering...
getExample()
Returns a numbering example.
info()
Return description of numbering model.
getNumRef($objsoc, $objforref, $mode='next')
Return next free value.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.