dolibarr  9.0.0
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@zendsi.com>
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 <http://www.gnu.org/licenses/>.
19  * or see http://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  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  function getExample()
85  {
86  return $this->prefixinvoice."1301-0001";
87  }
88 
89 
95  function canBeActivated()
96  {
97  global $conf,$langs,$db;
98 
99  $langs->load("bills");
100 
101  // Check invoice num
102  $siyymm=''; $max='';
103 
104  $posindice=8;
105  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
106  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
107  $sql.= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
108  $sql.= " AND entity = ".$conf->entity;
109  $resql=$db->query($sql);
110  if ($resql)
111  {
112  $row = $db->fetch_row($resql);
113  if ($row) { $siyymm = substr($row[0],0,6); $max=$row[0]; }
114  }
115  if ($siyymm && ! preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i',$siyymm))
116  {
117  $langs->load("errors");
118  $this->error=$langs->trans('ErrorNumRefModel',$max);
119  return false;
120  }
121 
122  // Check credit note num
123  $siyymm='';
124 
125  $posindice=8;
126  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
127  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
128  $sql.= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
129  $sql.= " AND entity = ".$conf->entity;
130 
131  $resql=$db->query($sql);
132  if ($resql)
133  {
134  $row = $db->fetch_row($resql);
135  if ($row) { $siyymm = substr($row[0],0,6); $max=$row[0]; }
136  }
137  if ($siyymm && ! preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i',$siyymm))
138  {
139  $this->error=$langs->trans('ErrorNumRefModel',$max);
140  return false;
141  }
142 
143  // Check deposit num
144  $siyymm='';
145 
146  $posindice=8;
147  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
148  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
149  $sql.= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
150  $sql.= " AND entity = ".$conf->entity;
151 
152  $resql=$db->query($sql);
153  if ($resql)
154  {
155  $row = $db->fetch_row($resql);
156  if ($row) { $siyymm = substr($row[0],0,6); $max=$row[0]; }
157  }
158  if ($siyymm && ! preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i',$siyymm))
159  {
160  $this->error=$langs->trans('ErrorNumRefModel',$max);
161  return false;
162  }
163  }
164 
173  function getNextValue($objsoc,$object,$mode='next')
174  {
175  global $db,$conf;
176 
177  if ($object->type == 2) $prefix=$this->prefixcreditnote;
178  else if ($facture->type == 3) $prefix=$this->prefixdeposit;
179  else $prefix=$this->prefixinvoice;
180 
181  // D'abord on recupere la valeur max
182  $posindice=8;
183  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
184  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
185  $sql.= " WHERE ref LIKE '".$prefix."____-%'";
186  $sql.= " AND entity = ".$conf->entity;
187 
188  $resql=$db->query($sql);
189  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
190  if ($resql)
191  {
192  $obj = $db->fetch_object($resql);
193  if ($obj) $max = intval($obj->max);
194  else $max=0;
195  }
196  else
197  {
198  return -1;
199  }
200 
201  if ($mode == 'last')
202  {
203  if ($max >= (pow(10, 4) - 1)) $num=$max; // If counter > 9999, we do not format on 4 chars, we take number as it is
204  else $num = sprintf("%04s",$max);
205 
206  $ref='';
207  $sql = "SELECT ref as ref";
208  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
209  $sql.= " WHERE ref LIKE '".$prefix."____-".$num."'";
210  $sql.= " AND entity = ".$conf->entity;
211 
212  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
213  $resql=$db->query($sql);
214  if ($resql)
215  {
216  $obj = $db->fetch_object($resql);
217  if ($obj) $ref = $obj->ref;
218  }
219  else dol_print_error($db);
220 
221  return $ref;
222  }
223  else if ($mode == 'next')
224  {
225  $date=$object->date; // This is invoice date (not creation date)
226  $yymm = strftime("%y%m",$date);
227 
228  if ($max >= (pow(10, 4) - 1)) $num=$max+1; // If counter > 9999, we do not format on 4 chars, we take number as it is
229  else $num = sprintf("%04s",$max+1);
230 
231  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
232  return $prefix.$yymm."-".$num;
233  }
234  else dol_print_error('','Bad parameter for getNextValue');
235  }
236 
237 
246  function getNumRef($objsoc,$objforref,$mode='next')
247  {
248  return $this->getNextValue($objsoc,$objforref,$mode);
249  }
250 }
251 
getExample()
Returns a numbering example.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
info()
Return description of numbering model.
getNumRef($objsoc, $objforref, $mode='next')
Return next free value.
Parent Class of numbering models of suppliers invoices references.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getNextValue($objsoc, $object, $mode='next')
Return next value.
$version
Dolibarr version of the loaded document string.
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 ...