dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
18  * or see https://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  public 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  public 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  public function getExample()
82  {
83  return $this->prefixinvoice."0501-0001";
84  }
85 
92  public function canBeActivated()
93  {
94  global $langs, $conf, $db;
95 
96  $langs->load("bills");
97 
98  // Check invoice num
99  $fayymm = ''; $max = '';
100 
101  $posindice = strlen($this->prefixinvoice) + 6;
102  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
103  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
104  $sql .= " WHERE ref 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 = strlen($this->prefixcreditnote) + 6;
124  $sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
125  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
126  $sql .= " WHERE ref 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  public function getNextValue($objsoc, $invoice, $mode = 'next')
153  {
154  global $db;
155 
156  $prefix = $this->prefixinvoice;
157  if ($invoice->type == 1) $prefix = $this->prefixreplacement;
158  elseif ($invoice->type == 2) $prefix = $this->prefixcreditnote;
159  elseif ($invoice->type == 3) $prefix = $this->prefixdeposit;
160 
161  // First we get the max value
162  $posindice = strlen($prefix) + 6;
163  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
164  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
165  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
166  $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
167 
168  $resql = $db->query($sql);
169  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
170  if ($resql)
171  {
172  $obj = $db->fetch_object($resql);
173  if ($obj) $max = intval($obj->max);
174  else $max = 0;
175  } else {
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";
187  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
188  $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
189  $sql .= " ORDER BY ref DESC";
190 
191  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
192  $resql = $db->query($sql);
193  if ($resql)
194  {
195  $obj = $db->fetch_object($resql);
196  if ($obj) $ref = $obj->ref;
197  } else dol_print_error($db);
198 
199  return $ref;
200  } elseif ($mode == 'next')
201  {
202  $date = $invoice->date; // This is invoice date (not creation date)
203  $yymm = strftime("%y%m", $date);
204 
205  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
206  else $num = sprintf("%04s", $max + 1);
207 
208  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
209  return $prefix.$yymm."-".$num;
210  } else dol_print_error('', 'Bad parameter for getNextValue');
211  }
212 
221  public function getNumRef($objsoc, $objforref, $mode = 'next')
222  {
223  return $this->getNextValue($objsoc, $objforref, $mode);
224  }
225 }
getNumRef($objsoc, $objforref, $mode= 'next')
Return next free value.
Class to manage invoice numbering rules Mars.
getNextValue($objsoc, $invoice, $mode= 'next')
Return next value not used or last value used.
__construct()
Constructor.
Parent class of invoice reference numbering templates.
info()
Returns the description of the numbering model.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getExample()
Return an example of numbering.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
canBeActivated()
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...