dolibarr  9.0.0
mod_facture_terre.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-2015 Regis Houssin <regis.houssin@inodbox.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * or see http://www.gnu.org/
18  */
19 
25 require_once DOL_DOCUMENT_ROOT .'/core/modules/facture/modules_facture.php';
26 
31 {
36  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
37 
38  public $prefixinvoice='FA';
39 
40  public $prefixcreditnote='AV';
41 
42  public $prefixdeposit='AC';
43 
47  public $error='';
48 
49 
53  function __construct()
54  {
55  if (! empty($conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX))
56  {
57  $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX;
58  }
59  }
60 
66  function info()
67  {
68  global $langs;
69  $langs->load("bills");
70  return $langs->trans('TerreNumRefModelDesc1',$this->prefixinvoice,$this->prefixcreditnote,$this->prefixdeposit);
71  }
72 
78  function getExample()
79  {
80  return $this->prefixinvoice."0501-0001";
81  }
82 
89  function canBeActivated()
90  {
91  global $langs,$conf,$db;
92 
93  $langs->load("bills");
94 
95  // Check invoice num
96  $fayymm=''; $max='';
97 
98  $posindice=8;
99  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
100  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
101  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixinvoice)."____-%'";
102  $sql.= " AND entity = ".$conf->entity;
103 
104  $resql=$db->query($sql);
105  if ($resql)
106  {
107  $row = $db->fetch_row($resql);
108  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
109  }
110  if ($fayymm && ! preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i',$fayymm))
111  {
112  $langs->load("errors");
113  $this->error=$langs->trans('ErrorNumRefModel',$max);
114  return false;
115  }
116 
117  // Check credit note num
118  $fayymm='';
119 
120  $posindice=8;
121  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
122  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
123  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
124  $sql.= " AND entity = ".$conf->entity;
125 
126  $resql=$db->query($sql);
127  if ($resql)
128  {
129  $row = $db->fetch_row($resql);
130  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
131  }
132  if ($fayymm && ! preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i',$fayymm))
133  {
134  $this->error=$langs->trans('ErrorNumRefModel',$max);
135  return false;
136  }
137 
138  // Check deposit num
139  $fayymm='';
140 
141  $posindice=8;
142  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
143  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
144  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixdeposit)."____-%'";
145  $sql.= " AND entity = ".$conf->entity;
146 
147  $resql=$db->query($sql);
148  if ($resql)
149  {
150  $row = $db->fetch_row($resql);
151  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
152  }
153  if ($fayymm && ! preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i',$fayymm))
154  {
155  $this->error=$langs->trans('ErrorNumRefModel',$max);
156  return false;
157  }
158 
159  return true;
160  }
161 
170  function getNextValue($objsoc, $invoice, $mode='next')
171  {
172  global $db;
173 
174  if ($invoice->type == 2) $prefix=$this->prefixcreditnote;
175  else if ($invoice->type == 3) $prefix=$this->prefixdeposit;
176  else $prefix=$this->prefixinvoice;
177 
178  // D'abord on recupere la valeur max
179  $posindice=8;
180  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
181  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
182  $sql.= " WHERE facnumber LIKE '".$prefix."____-%'";
183  $sql.= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
184 
185  $resql=$db->query($sql);
186  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
187  if ($resql)
188  {
189  $obj = $db->fetch_object($resql);
190  if ($obj) $max = intval($obj->max);
191  else $max=0;
192  }
193  else
194  {
195  return -1;
196  }
197 
198  if ($mode == 'last')
199  {
200  if ($max >= (pow(10, 4) - 1)) $num=$max; // If counter > 9999, we do not format on 4 chars, we take number as it is
201  else $num = sprintf("%04s",$max);
202 
203  $ref='';
204  $sql = "SELECT facnumber as ref";
205  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
206  $sql.= " WHERE facnumber LIKE '".$prefix."____-".$num."'";
207  $sql.= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
208  $sql.= " ORDER BY ref DESC";
209 
210  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
211  $resql=$db->query($sql);
212  if ($resql)
213  {
214  $obj = $db->fetch_object($resql);
215  if ($obj) $ref = $obj->ref;
216  }
217  else dol_print_error($db);
218 
219  return $ref;
220  }
221  else if ($mode == 'next')
222  {
223  $date=$invoice->date; // This is invoice date (not creation date)
224  $yymm = strftime("%y%m",$date);
225 
226  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
227  else $num = sprintf("%04s",$max+1);
228 
229  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
230  return $prefix.$yymm."-".$num;
231  }
232  else dol_print_error('','Bad parameter for getNextValue');
233  }
234 
243  function getNumRef($objsoc,$objforref,$mode='next')
244  {
245  return $this->getNextValue($objsoc,$objforref,$mode);
246  }
247 }
248 
info()
Renvoi la description du modele de numerotation.
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
Classe du modele de numerotation de reference de facture Terre.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
getNumRef($objsoc, $objforref, $mode='next')
Return next free value.
$version
Dolibarr version of the loaded document string.
canBeActivated()
Test si les numeros deja en vigueur dans la base ne provoquent pas de de conflits qui empechera cette...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Classe mere des modeles de numerotation des references de facture.
getExample()
Renvoi un exemple de numerotation.
__construct()
Constructor.
getNextValue($objsoc, $invoice, $mode='next')
Return next value not used or last value used.