dolibarr  9.0.0
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-2018 Regis Houssin <regis.houssin@inodbox.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 {
37  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
38 
39  public $prefixinvoice='FA';
40 
41  public $prefixreplacement='FR';
42 
43  public $prefixdeposit='AC';
44 
45  public $prefixcreditnote='AV';
46 
50  public $error='';
51 
52 
56  function __construct()
57  {
58  if (! empty($conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX))
59  {
60  $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX;
61  }
62  }
63 
69  function info()
70  {
71  global $langs;
72  $langs->load("bills");
73  return $langs->trans('MarsNumRefModelDesc1',$this->prefixinvoice,$this->prefixreplacement,$this->prefixdeposit,$this->prefixcreditnote);
74  }
75 
81  function getExample()
82  {
83  return $this->prefixinvoice."0501-0001";
84  }
85 
92  function canBeActivated()
93  {
94  global $langs,$conf,$db;
95 
96  $langs->load("bills");
97 
98  // Check invoice num
99  $fayymm=''; $max='';
100 
101  $posindice=8;
102  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
103  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
104  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixinvoice)."____-%'";
105  $sql.= " AND entity = ".$conf->entity;
106 
107  $resql=$db->query($sql);
108  if ($resql)
109  {
110  $row = $db->fetch_row($resql);
111  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
112  }
113  if ($fayymm && ! preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i',$fayymm))
114  {
115  $langs->load("errors");
116  $this->error=$langs->trans('ErrorNumRefModel',$max);
117  return false;
118  }
119 
120  // Check credit note num
121  $fayymm='';
122 
123  $posindice=8;
124  $sql = "SELECT MAX(SUBSTRING(facnumber FROM ".$posindice.")) as max"; // This is standard SQL
125  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
126  $sql.= " WHERE facnumber LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
127  $sql.= " AND entity = ".$conf->entity;
128 
129  $resql=$db->query($sql);
130  if ($resql)
131  {
132  $row = $db->fetch_row($resql);
133  if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
134  }
135  if ($fayymm && ! preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i',$fayymm))
136  {
137  $this->error=$langs->trans('ErrorNumRefModel',$max);
138  return false;
139  }
140 
141  return true;
142  }
143 
152  function getNextValue($objsoc, $invoice, $mode='next')
153  {
154  global $db;
155 
156  $prefix=$this->prefixinvoice;
157 
158  if ($invoice->type == 1) $prefix=$this->prefixreplacement;
159  else if ($invoice->type == 2) $prefix=$this->prefixcreditnote;
160  else if ($invoice->type == 3) $prefix=$this->prefixdeposit;
161  else $prefix=$this->prefixinvoice;
162 
163  // D'abord on recupere la valeur max
164  $posindice=8;
165  $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
166  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
167  $sql.= " WHERE facnumber LIKE '".$prefix."____-%'";
168  $sql.= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
169 
170  $resql=$db->query($sql);
171  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
172  if ($resql)
173  {
174  $obj = $db->fetch_object($resql);
175  if ($obj) $max = intval($obj->max);
176  else $max=0;
177  }
178  else
179  {
180  return -1;
181  }
182 
183  if ($mode == 'last')
184  {
185  if ($max >= (pow(10, 4) - 1)) $num=$max; // If counter > 9999, we do not format on 4 chars, we take number as it is
186  else $num = sprintf("%04s",$max);
187 
188  $ref='';
189  $sql = "SELECT facnumber as ref";
190  $sql.= " FROM ".MAIN_DB_PREFIX."facture";
191  $sql.= " WHERE facnumber LIKE '".$prefix."____-".$num."'";
192  $sql.= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
193  $sql.= " ORDER BY ref DESC";
194 
195  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
196  $resql=$db->query($sql);
197  if ($resql)
198  {
199  $obj = $db->fetch_object($resql);
200  if ($obj) $ref = $obj->ref;
201  }
202  else dol_print_error($db);
203 
204  return $ref;
205  }
206  else if ($mode == 'next')
207  {
208  $date=$invoice->date; // This is invoice date (not creation date)
209  $yymm = strftime("%y%m",$date);
210 
211  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
212  else $num = sprintf("%04s",$max+1);
213 
214  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
215  return $prefix.$yymm."-".$num;
216  }
217  else dol_print_error('','Bad parameter for getNextValue');
218  }
219 
228  function getNumRef($objsoc,$objforref,$mode='next')
229  {
230  return $this->getNextValue($objsoc,$objforref,$mode);
231  }
232 }
233 
$version
Dolibarr version of the loaded document string.
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 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.
getNextValue($objsoc, $invoice, $mode='next')
Return next value not used or last value used.
Classe mere des modeles de numerotation des references de facture.
info()
Renvoi la description du modele de numerotation.
getExample()
Renvoi un exemple de numerotation.
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...