dolibarr 19.0.4
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
25require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
26
32{
37 public $version = 'dolibarr';
38
43 public $prefixinvoice = 'FA';
44
49 public $prefixreplacement = 'FA';
50
55 public $prefixcreditnote = 'AV';
56
61 public $prefixdeposit = 'AC';
62
66 public $error = '';
67
68
72 public function __construct()
73 {
74 global $conf, $mysoc;
75
76 if (((float) getDolGlobalString('MAIN_VERSION_LAST_INSTALL')) >= 16.0 && $mysoc->country_code != 'FR') {
77 $this->prefixinvoice = 'IN'; // We use correct standard code "IN = Invoice"
78 $this->prefixreplacement = 'IR';
79 $this->prefixdeposit = 'ID';
80 $this->prefixcreditnote = 'IC';
81 }
82
83 if (getDolGlobalString('INVOICE_NUMBERING_TERRE_FORCE_PREFIX')) {
84 $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX;
85 }
86 }
87
94 public function info($langs)
95 {
96 global $langs;
97 $langs->load("bills");
98 return $langs->trans('TerreNumRefModelDesc1', $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
99 }
100
106 public function getExample()
107 {
108 return $this->prefixinvoice."0501-0001";
109 }
110
118 public function canBeActivated($object)
119 {
120 global $langs, $conf, $db;
121
122 $langs->load("bills");
123
124 // Check invoice num
125 $fayymm = '';
126 $max = '';
127
128 $posindice = strlen($this->prefixinvoice) + 6;
129 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
130 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
131 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
132 $sql .= " AND entity = ".$conf->entity;
133
134 $resql = $db->query($sql);
135 if ($resql) {
136 $row = $db->fetch_row($resql);
137 if ($row) {
138 $fayymm = substr($row[0], 0, 6);
139 $max = $row[0];
140 }
141 }
142 if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
143 $langs->load("errors");
144 $this->error = $langs->trans('ErrorNumRefModel', $max);
145 return false;
146 }
147
148 // Check credit note num
149 $fayymm = '';
150
151 $posindice = strlen($this->prefixcreditnote) + 6;
152 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
153 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
154 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
155 $sql .= " AND entity = ".$conf->entity;
156
157 $resql = $db->query($sql);
158 if ($resql) {
159 $row = $db->fetch_row($resql);
160 if ($row) {
161 $fayymm = substr($row[0], 0, 6);
162 $max = $row[0];
163 }
164 }
165 if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
166 $this->error = $langs->trans('ErrorNumRefModel', $max);
167 return false;
168 }
169
170 // Check deposit num
171 $fayymm = '';
172
173 $posindice = strlen($this->prefixdeposit) + 6;
174 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
175 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
176 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
177 $sql .= " AND entity = ".$conf->entity;
178
179 $resql = $db->query($sql);
180 if ($resql) {
181 $row = $db->fetch_row($resql);
182 if ($row) {
183 $fayymm = substr($row[0], 0, 6);
184 $max = $row[0];
185 }
186 }
187 if ($fayymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
188 $this->error = $langs->trans('ErrorNumRefModel', $max);
189 return false;
190 }
191
192 return true;
193 }
194
206 public function getNextValue($objsoc, $invoice, $mode = 'next')
207 {
208 global $db;
209
210 dol_syslog(get_class($this)."::getNextValue mode=".$mode, LOG_DEBUG);
211
212 $prefix = $this->prefixinvoice;
213 if ($invoice->type == 2) {
214 $prefix = $this->prefixcreditnote;
215 } elseif ($invoice->type == 3) {
216 $prefix = $this->prefixdeposit;
217 }
218
219 // First we get the max value
220 $posindice = strlen($prefix) + 6;
221 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
222 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
223 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
224 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
225
226 $resql = $db->query($sql);
227 if ($resql) {
228 $obj = $db->fetch_object($resql);
229 if ($obj) {
230 $max = intval($obj->max);
231 } else {
232 $max = 0;
233 }
234 } else {
235 return -1;
236 }
237
238 if ($mode == 'last') {
239 if ($max >= (pow(10, 4) - 1)) {
240 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
241 } else {
242 $num = sprintf("%04s", $max);
243 }
244
245 $ref = '';
246 $sql = "SELECT ref as ref";
247 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
248 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
249 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
250 $sql .= " ORDER BY ref DESC";
251
252 $resql = $db->query($sql);
253 if ($resql) {
254 $obj = $db->fetch_object($resql);
255 if ($obj) {
256 $ref = $obj->ref;
257 }
258 } else {
259 dol_print_error($db);
260 }
261
262 return $ref;
263 } elseif ($mode == 'next') {
264 $date = $invoice->date; // This is invoice date (not creation date)
265 $yymm = strftime("%y%m", $date);
266
267 if ($max >= (pow(10, 4) - 1)) {
268 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
269 } else {
270 $num = sprintf("%04s", $max + 1);
271 }
272
273 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
274 return $prefix.$yymm."-".$num;
275 } else {
276 dol_print_error('', 'Bad parameter for getNextValue');
277 }
278
279 return 0;
280 }
281
290 public function getNumRef($objsoc, $objforref, $mode = 'next')
291 {
292 return $this->getNextValue($objsoc, $objforref, $mode);
293 }
294}
Parent class of invoice reference numbering templates.
Class of numbering module Terre for invoices.
getNextValue($objsoc, $invoice, $mode='next')
Return next value not used or last value used.
info($langs)
Returns the description of the numbering model.
getNumRef($objsoc, $objforref, $mode='next')
Return next free value.
getExample()
Return an example of numbering.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
__construct()
Constructor.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.