dolibarr  20.0.0-beta
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  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  * or see https://www.gnu.org/
22  */
23 
30 require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
31 
32 
37 {
42  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
43 
47  public $error = '';
48 
54  public $nom = 'Cactus';
55 
59  public $name = 'Cactus';
60 
61  public $prefixinvoice = 'SI';
62 
63  public $prefixcreditnote = 'SA';
64 
65  public $prefixdeposit = 'SD';
66 
67 
74  public function info($langs)
75  {
76  global $langs;
77  $langs->load("bills");
78  return $langs->trans("CactusNumRefModelDesc1", $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
79  }
80 
81 
87  public function getExample()
88  {
89  return $this->prefixinvoice."1301-0001";
90  }
91 
92 
99  public function canBeActivated($object)
100  {
101  global $conf, $langs, $db;
102 
103  $langs->load("bills");
104 
105  // Check invoice num
106  $siyymm = '';
107  $max = '';
108 
109  $posindice = strlen($this->prefixinvoice) + 6;
110  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
111  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
112  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
113  $sql .= " AND entity = ".$conf->entity;
114  $resql = $db->query($sql);
115  if ($resql) {
116  $row = $db->fetch_row($resql);
117  if ($row) {
118  $siyymm = substr($row[0], 0, 6);
119  $max = $row[0];
120  }
121  }
122  if ($siyymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
123  $langs->load("errors");
124  $this->error = $langs->trans('ErrorNumRefModel', $max);
125  return false;
126  }
127 
128  // Check credit note num
129  $siyymm = '';
130 
131  $posindice = strlen($this->prefixcreditnote) + 6;
132  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
133  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
134  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
135  $sql .= " AND entity = ".$conf->entity;
136 
137  $resql = $db->query($sql);
138  if ($resql) {
139  $row = $db->fetch_row($resql);
140  if ($row) {
141  $siyymm = substr($row[0], 0, 6);
142  $max = $row[0];
143  }
144  }
145  if ($siyymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
146  $this->error = $langs->trans('ErrorNumRefModel', $max);
147  return false;
148  }
149 
150  // Check deposit num
151  $siyymm = '';
152 
153  $posindice = strlen($this->prefixdeposit) + 6;
154  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
155  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
156  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
157  $sql .= " AND entity = ".$conf->entity;
158 
159  $resql = $db->query($sql);
160  if ($resql) {
161  $row = $db->fetch_row($resql);
162  if ($row) {
163  $siyymm = substr($row[0], 0, 6);
164  $max = $row[0];
165  }
166  }
167  if ($siyymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
168  $this->error = $langs->trans('ErrorNumRefModel', $max);
169  return false;
170  }
171 
172  return true;
173  }
174 
183  public function getNextValue($objsoc, $object, $mode = 'next')
184  {
185  global $db, $conf;
186 
187  $prefix = $this->prefixinvoice;
188  if ($object->type == 2) {
189  $prefix = $this->prefixcreditnote;
190  } elseif ($object->type == 3) {
191  $prefix = $this->prefixdeposit;
192  }
193 
194  // First, we get the max value
195  $posindice = strlen($prefix) + 6;
196  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
197  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
198  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
199  $sql .= " AND entity = ".$conf->entity;
200 
201  $resql = $db->query($sql);
202  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
203  if ($resql) {
204  $obj = $db->fetch_object($resql);
205  if ($obj) {
206  $max = intval($obj->max);
207  } else {
208  $max = 0;
209  }
210  } else {
211  return -1;
212  }
213 
214  if ($mode == 'last') {
215  if ($max >= (pow(10, 4) - 1)) {
216  $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
217  } else {
218  $num = sprintf("%04d", $max);
219  }
220 
221  $ref = '';
222  $sql = "SELECT ref as ref";
223  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
224  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
225  $sql .= " AND entity = ".$conf->entity;
226 
227  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
228  $resql = $db->query($sql);
229  if ($resql) {
230  $obj = $db->fetch_object($resql);
231  if ($obj) {
232  $ref = $obj->ref;
233  }
234  } else {
235  dol_print_error($db);
236  }
237 
238  return $ref;
239  } elseif ($mode == 'next') {
240  $date = $object->date; // This is invoice date (not creation date)
241  $yymm = dol_print_date($date, "%y%m");
242 
243  if ($max >= (pow(10, 4) - 1)) {
244  $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
245  } else {
246  $num = sprintf("%04d", $max + 1);
247  }
248 
249  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
250  return $prefix.$yymm."-".$num;
251  } else {
252  dol_print_error(null, 'Bad parameter for getNextValue');
253  return -1;
254  }
255  }
256 
257 
267  public function getNumRef($objsoc, $objforref, $mode = 'next')
268  {
269  return $this->getNextValue($objsoc, $objforref, $mode);
270  }
271 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
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($object)
Tests if the numbers already in the database do not cause conflicts that would prevent this numbering...
getExample()
Returns a numbering example.
info($langs)
Return description of numbering model.
getNumRef($objsoc, $objforref, $mode='next')
Return next free value.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('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') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $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.