dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
17  * or see https://www.gnu.org/
18  */
19 
25 require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
26 
32 {
37  public $version = 'dolibarr';
38 
43  public $prefixinvoice = 'FA';
44 
49  public $prefixcreditnote = 'AV';
50 
55  public $prefixdeposit = 'AC';
56 
60  public $error = '';
61 
62 
66  public function __construct()
67  {
68  if (!empty($conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX))
69  {
70  $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX;
71  }
72  }
73 
79  public function info()
80  {
81  global $langs;
82  $langs->load("bills");
83  return $langs->trans('TerreNumRefModelDesc1', $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
84  }
85 
91  public function getExample()
92  {
93  return $this->prefixinvoice."0501-0001";
94  }
95 
102  public function canBeActivated()
103  {
104  global $langs, $conf, $db;
105 
106  $langs->load("bills");
107 
108  // Check invoice num
109  $fayymm = ''; $max = '';
110 
111  $posindice = strlen($this->prefixinvoice) + 6;
112  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
113  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
114  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
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->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm))
124  {
125  $langs->load("errors");
126  $this->error = $langs->trans('ErrorNumRefModel', $max);
127  return false;
128  }
129 
130  // Check credit note num
131  $fayymm = '';
132 
133  $posindice = strlen($this->prefixcreditnote) + 6;
134  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
135  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
136  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
137  $sql .= " AND entity = ".$conf->entity;
138 
139  $resql = $db->query($sql);
140  if ($resql)
141  {
142  $row = $db->fetch_row($resql);
143  if ($row) { $fayymm = substr($row[0], 0, 6); $max = $row[0]; }
144  }
145  if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm))
146  {
147  $this->error = $langs->trans('ErrorNumRefModel', $max);
148  return false;
149  }
150 
151  // Check deposit num
152  $fayymm = '';
153 
154  $posindice = strlen($this->prefixdeposit) + 6;
155  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
156  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
157  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
158  $sql .= " AND entity = ".$conf->entity;
159 
160  $resql = $db->query($sql);
161  if ($resql)
162  {
163  $row = $db->fetch_row($resql);
164  if ($row) { $fayymm = substr($row[0], 0, 6); $max = $row[0]; }
165  }
166  if ($fayymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $fayymm))
167  {
168  $this->error = $langs->trans('ErrorNumRefModel', $max);
169  return false;
170  }
171 
172  return true;
173  }
174 
183  public function getNextValue($objsoc, $invoice, $mode = 'next')
184  {
185  global $db;
186 
187  dol_syslog(get_class($this)."::getNextValue mode=".$mode, LOG_DEBUG);
188 
189  $prefix = $this->prefixinvoice;
190  if ($invoice->type == 2) $prefix = $this->prefixcreditnote;
191  elseif ($invoice->type == 3) $prefix = $this->prefixdeposit;
192 
193  // First we get the max value
194  $posindice = strlen($prefix) + 6;
195  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
196  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
197  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
198  $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
199 
200  $resql = $db->query($sql);
201  if ($resql)
202  {
203  $obj = $db->fetch_object($resql);
204  if ($obj) $max = intval($obj->max);
205  else $max = 0;
206  } else {
207  return -1;
208  }
209 
210  if ($mode == 'last')
211  {
212  if ($max >= (pow(10, 4) - 1)) $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
213  else $num = sprintf("%04s", $max);
214 
215  $ref = '';
216  $sql = "SELECT ref as ref";
217  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
218  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
219  $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
220  $sql .= " ORDER BY ref DESC";
221 
222  $resql = $db->query($sql);
223  if ($resql)
224  {
225  $obj = $db->fetch_object($resql);
226  if ($obj) $ref = $obj->ref;
227  } else dol_print_error($db);
228 
229  return $ref;
230  } elseif ($mode == 'next')
231  {
232  $date = $invoice->date; // This is invoice date (not creation date)
233  $yymm = strftime("%y%m", $date);
234 
235  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
236  else $num = sprintf("%04s", $max + 1);
237 
238  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
239  return $prefix.$yymm."-".$num;
240  } else dol_print_error('', 'Bad parameter for getNextValue');
241  }
242 
251  public function getNumRef($objsoc, $objforref, $mode = 'next')
252  {
253  return $this->getNextValue($objsoc, $objforref, $mode);
254  }
255 }
info()
Returns the description of the numbering model.
Class of numbering module Terre for invoices.
canBeActivated()
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
getNumRef($objsoc, $objforref, $mode= 'next')
Return next free value.
Parent class of invoice reference numbering templates.
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...
__construct()
Constructor.
getNextValue($objsoc, $invoice, $mode= 'next')
Return next value not used or last value used.