dolibarr  7.0.0-beta
mod_facture_mars.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  * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * or see http://www.gnu.org/
19  */
20 
26 require_once DOL_DOCUMENT_ROOT .'/core/modules/facture/modules_facture.php';
27 
32 {
33  var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
34  var $prefixinvoice='FA';
35  var $prefixreplacement='FR';
36  var $prefixdeposit='AC';
37  var $prefixcreditnote='AV';
38  var $error='';
39 
40 
44  function __construct()
45  {
46  if (! empty($conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX))
47  {
48  $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX;
49  }
50  }
51 
57  function info()
58  {
59  global $langs;
60  $langs->load("bills");
61  return $langs->trans('MarsNumRefModelDesc1',$this->prefixinvoice,$this->prefixreplacement,$this->prefixdeposit,$this->prefixcreditnote);
62  }
63 
69  function getExample()
70  {
71  return $this->prefixinvoice."0501-0001";
72  }
73 
80  function canBeActivated()
81  {
82  global $langs,$conf,$db;
83 
84  $langs->load("bills");
85 
86  // Check invoice num
87  $fayymm=''; $max='';
88 
89  $posindice=8;
90  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
91  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
92  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixinvoice)."____-%'";
93  $sql.= " AND entity = ".$conf->entity;
94 
95  $resql=$db->query($sql);
96  if ($resql)
97  {
98  $row = $db->fetch_row($resql);
99  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
100  }
101  if ($fayymm && ! preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i',$fayymm))
102  {
103  $langs->load("errors");
104  $this->error=$langs->trans('ErrorNumRefModel',$max);
105  return false;
106  }
107 
108  // Check credit note num
109  $fayymm='';
110 
111  $posindice=8;
112  $sql = "SELECT MAX(SUBSTRING(facnumber FROM ".$posindice.")) as max"; // This is standard SQL
113  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
114  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
115  $sql.= " AND entity = ".$conf->entity;
116 
117  $resql=$db->query($sql);
118  if ($resql)
119  {
120  $row = $db->fetch_row($resql);
121  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
122  }
123  if ($fayymm && ! preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i',$fayymm))
124  {
125  $this->error=$langs->trans('ErrorNumRefModel',$max);
126  return false;
127  }
128 
129  return true;
130  }
131 
140  function getNextValue($objsoc,$facture,$mode='next')
141  {
142  global $db;
143 
144  $prefix=$this->prefixinvoice;
145 
146  if ($facture->type == 1) $prefix=$this->prefixreplacement;
147  else if ($facture->type == 2) $prefix=$this->prefixcreditnote;
148  else if ($facture->type == 3) $prefix=$this->prefixdeposit;
149  else $prefix=$this->prefixinvoice;
150 
151  // D'abord on recupere la valeur max
152  $posindice=8;
153  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
154  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
155  $sql.= " WHERE facnumber LIKE '".$prefix."____-%'";
156  $sql.= " AND entity IN (".getEntity('facture').")";
157 
158  $resql=$db->query($sql);
159  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
160  if ($resql)
161  {
162  $obj = $db->fetch_object($resql);
163  if ($obj) $max = intval($obj->max);
164  else $max=0;
165  }
166  else
167  {
168  return -1;
169  }
170 
171  if ($mode == 'last')
172  {
173  if ($max >= (pow(10, 4) - 1)) $num=$max; // If counter > 9999, we do not format on 4 chars, we take number as it is
174  else $num = sprintf("%04s",$max);
175 
176  $ref='';
177  $sql = "SELECT facnumber as ref";
178  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
179  $sql.= " WHERE facnumber LIKE '".$prefix."____-".$num."'";
180  $sql.= " AND entity IN (".getEntity('facture').")";
181 
182  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
183  $resql=$db->query($sql);
184  if ($resql)
185  {
186  $obj = $db->fetch_object($resql);
187  if ($obj) $ref = $obj->ref;
188  }
189  else dol_print_error($db);
190 
191  return $ref;
192  }
193  else if ($mode == 'next')
194  {
195  $date=$facture->date; // This is invoice date (not creation date)
196  $yymm = strftime("%y%m",$date);
197 
198  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
199  else $num = sprintf("%04s",$max+1);
200 
201  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
202  return $prefix.$yymm."-".$num;
203  }
204  else dol_print_error('','Bad parameter for getNextValue');
205  }
206 
215  function getNumRef($objsoc,$objforref,$mode='next')
216  {
217  return $this->getNextValue($objsoc,$objforref,$mode);
218  }
219 
220 }
221 
Classe du modele de numerotation de reference de facture Mars.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
__construct()
Constructor.
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.
info()
Renvoi la description du modele de numerotation.
getExample()
Renvoi un exemple de numerotation.
getNextValue($objsoc, $facture, $mode='next')
Return next value not used or last value used.
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
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...