dolibarr  7.0.0-beta
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@capnetworks.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 {
32  var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
33  var $prefixinvoice='FA';
34  var $prefixcreditnote='AV';
35  var $prefixdeposit='AC';
36  var $error='';
37 
38 
42  function __construct()
43  {
44  if (! empty($conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX))
45  {
46  $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX;
47  }
48  }
49 
55  function info()
56  {
57  global $langs;
58  $langs->load("bills");
59  return $langs->trans('TerreNumRefModelDesc1',$this->prefixinvoice,$this->prefixcreditnote,$this->prefixdeposit);
60  }
61 
67  function getExample()
68  {
69  return $this->prefixinvoice."0501-0001";
70  }
71 
78  function canBeActivated()
79  {
80  global $langs,$conf,$db;
81 
82  $langs->load("bills");
83 
84  // Check invoice num
85  $fayymm=''; $max='';
86 
87  $posindice=8;
88  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
89  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
90  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixinvoice)."____-%'";
91  $sql.= " AND entity = ".$conf->entity;
92 
93  $resql=$db->query($sql);
94  if ($resql)
95  {
96  $row = $db->fetch_row($resql);
97  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
98  }
99  if ($fayymm && ! preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i',$fayymm))
100  {
101  $langs->load("errors");
102  $this->error=$langs->trans('ErrorNumRefModel',$max);
103  return false;
104  }
105 
106  // Check credit note num
107  $fayymm='';
108 
109  $posindice=8;
110  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
111  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
112  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
113  $sql.= " AND entity = ".$conf->entity;
114 
115  $resql=$db->query($sql);
116  if ($resql)
117  {
118  $row = $db->fetch_row($resql);
119  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
120  }
121  if ($fayymm && ! preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i',$fayymm))
122  {
123  $this->error=$langs->trans('ErrorNumRefModel',$max);
124  return false;
125  }
126 
127  // Check deposit num
128  $fayymm='';
129 
130  $posindice=8;
131  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
132  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
133  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixdeposit)."____-%'";
134  $sql.= " AND entity = ".$conf->entity;
135 
136  $resql=$db->query($sql);
137  if ($resql)
138  {
139  $row = $db->fetch_row($resql);
140  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
141  }
142  if ($fayymm && ! preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i',$fayymm))
143  {
144  $this->error=$langs->trans('ErrorNumRefModel',$max);
145  return false;
146  }
147 
148  return true;
149  }
150 
159  function getNextValue($objsoc,$facture,$mode='next')
160  {
161  global $db;
162 
163  if ($facture->type == 2) $prefix=$this->prefixcreditnote;
164  else if ($facture->type == 3) $prefix=$this->prefixdeposit;
165  else $prefix=$this->prefixinvoice;
166 
167  // D'abord on recupere la valeur max
168  $posindice=8;
169  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
170  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
171  $sql.= " WHERE facnumber LIKE '".$prefix."____-%'";
172  $sql.= " AND entity IN (".getEntity('facture').")";
173 
174  $resql=$db->query($sql);
175  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
176  if ($resql)
177  {
178  $obj = $db->fetch_object($resql);
179  if ($obj) $max = intval($obj->max);
180  else $max=0;
181  }
182  else
183  {
184  return -1;
185  }
186 
187  if ($mode == 'last')
188  {
189  if ($max >= (pow(10, 4) - 1)) $num=$max; // If counter > 9999, we do not format on 4 chars, we take number as it is
190  else $num = sprintf("%04s",$max);
191 
192  $ref='';
193  $sql = "SELECT facnumber as ref";
194  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
195  $sql.= " WHERE facnumber LIKE '".$prefix."____-".$num."'";
196  $sql.= " AND entity IN (".getEntity('facture').")";
197 
198  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
199  $resql=$db->query($sql);
200  if ($resql)
201  {
202  $obj = $db->fetch_object($resql);
203  if ($obj) $ref = $obj->ref;
204  }
205  else dol_print_error($db);
206 
207  return $ref;
208  }
209  else if ($mode == 'next')
210  {
211  $date=$facture->date; // This is invoice date (not creation date)
212  $yymm = strftime("%y%m",$date);
213 
214  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
215  else $num = sprintf("%04s",$max+1);
216 
217  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
218  return $prefix.$yymm."-".$num;
219  }
220  else dol_print_error('','Bad parameter for getNextValue');
221  }
222 
231  function getNumRef($objsoc,$objforref,$mode='next')
232  {
233  return $this->getNextValue($objsoc,$objforref,$mode);
234  }
235 
236 }
237 
info()
Renvoi la description du modele de numerotation.
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.
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.
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
__construct()
Constructor.
getNextValue($objsoc, $facture, $mode='next')
Return next value not used or last value used.