dolibarr  7.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@capnetworks.com>
4  * Copyright (C) 2013 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 {
36  var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
37  var $error = '';
38  var $nom = 'Cactus';
39  var $prefixinvoice='SI';
40  var $prefixcreditnote='SA';
41  var $prefixdeposit='SD';
42 
43 
49  function info()
50  {
51  global $langs;
52  $langs->load("bills");
53  return $langs->trans("CactusNumRefModelDesc1",$this->prefixinvoice,$this->prefixcreditnote,$this->prefixdeposit);
54  }
55 
56 
62  function getExample()
63  {
64  return $this->prefixinvoice."1301-0001";
65  }
66 
67 
73  function canBeActivated()
74  {
75  global $conf,$langs,$db;
76 
77  $langs->load("bills");
78 
79  // Check invoice num
80  $siyymm=''; $max='';
81 
82  $posindice=8;
83  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
84  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
85  $sql.= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
86  $sql.= " AND entity = ".$conf->entity;
87  $resql=$db->query($sql);
88  if ($resql)
89  {
90  $row = $db->fetch_row($resql);
91  if ($row) { $siyymm = substr($row[0],0,6); $max=$row[0]; }
92  }
93  if ($siyymm && ! preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i',$siyymm))
94  {
95  $langs->load("errors");
96  $this->error=$langs->trans('ErrorNumRefModel',$max);
97  return false;
98  }
99 
100  // Check credit note num
101  $siyymm='';
102 
103  $posindice=8;
104  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
105  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
106  $sql.= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
107  $sql.= " AND entity = ".$conf->entity;
108 
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->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i',$siyymm))
116  {
117  $this->error=$langs->trans('ErrorNumRefModel',$max);
118  return false;
119  }
120 
121  // Check deposit num
122  $siyymm='';
123 
124  $posindice=8;
125  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
126  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
127  $sql.= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
128  $sql.= " AND entity = ".$conf->entity;
129 
130  $resql=$db->query($sql);
131  if ($resql)
132  {
133  $row = $db->fetch_row($resql);
134  if ($row) { $siyymm = substr($row[0],0,6); $max=$row[0]; }
135  }
136  if ($siyymm && ! preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i',$siyymm))
137  {
138  $this->error=$langs->trans('ErrorNumRefModel',$max);
139  return false;
140  }
141  }
142 
151  function getNextValue($objsoc,$object,$mode='next')
152  {
153  global $db,$conf;
154 
155  if ($object->type == 2) $prefix=$this->prefixcreditnote;
156  else if ($facture->type == 3) $prefix=$this->prefixdeposit;
157  else $prefix=$this->prefixinvoice;
158 
159  // D'abord on recupere la valeur max
160  $posindice=8;
161  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
162  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
163  $sql.= " WHERE ref LIKE '".$prefix."____-%'";
164  $sql.= " AND entity = ".$conf->entity;
165 
166  $resql=$db->query($sql);
167  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
168  if ($resql)
169  {
170  $obj = $db->fetch_object($resql);
171  if ($obj) $max = intval($obj->max);
172  else $max=0;
173  }
174  else
175  {
176  return -1;
177  }
178 
179  if ($mode == 'last')
180  {
181  if ($max >= (pow(10, 4) - 1)) $num=$max; // If counter > 9999, we do not format on 4 chars, we take number as it is
182  else $num = sprintf("%04s",$max);
183 
184  $ref='';
185  $sql = "SELECT ref as ref";
186  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn";
187  $sql.= " WHERE ref LIKE '".$prefix."____-".$num."'";
188  $sql.= " AND entity = ".$conf->entity;
189 
190  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
191  $resql=$db->query($sql);
192  if ($resql)
193  {
194  $obj = $db->fetch_object($resql);
195  if ($obj) $ref = $obj->ref;
196  }
197  else dol_print_error($db);
198 
199  return $ref;
200  }
201  else if ($mode == 'next')
202  {
203  $date=$object->date; // This is invoice date (not creation date)
204  $yymm = strftime("%y%m",$date);
205 
206  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
207  else $num = sprintf("%04s",$max+1);
208 
209  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
210  return $prefix.$yymm."-".$num;
211  }
212  else dol_print_error('','Bad parameter for getNextValue');
213  }
214 
215 
224  function getNumRef($objsoc,$objforref,$mode='next')
225  {
226  return $this->getNextValue($objsoc,$objforref,$mode);
227  }
228 }
229 
getExample()
Returns a numbering example.
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.
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:1013
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 ...