dolibarr 24.0.0-beta
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
27require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
28
33{
37 public $name = 'Terre';
38
42 public $position = 40;
43
48 public $version = 'dolibarr_deprecated';
49
54 public $prefixinvoice = 'FA';
55
60 public $prefixreplacement = 'FA';
61
66 public $prefixcreditnote = 'AV';
67
72 public $prefixdeposit = 'AC';
73
77 public $error = '';
78
79
83 public function __construct()
84 {
85 global $mysoc;
86
87 if (((float) getDolGlobalString('MAIN_VERSION_LAST_INSTALL')) >= 16.0 && $mysoc->country_code != 'FR') {
88 $this->prefixinvoice = 'IN'; // We use correct standard code "IN = Invoice"
89 $this->prefixreplacement = 'IR';
90 $this->prefixdeposit = 'ID';
91 $this->prefixcreditnote = 'IC';
92 }
93
94 if (getDolGlobalString('INVOICE_NUMBERING_TERRE_FORCE_PREFIX')) {
95 $this->prefixinvoice = getDolGlobalString('INVOICE_NUMBERING_TERRE_FORCE_PREFIX');
96 }
97 }
98
105 public function info($langs)
106 {
107 global $langs;
108 $langs->load("bills");
109 return $langs->trans('TerreNumRefModelDesc1', $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
110 }
111
117 public function getExample()
118 {
119 return $this->prefixinvoice."0501-0001";
120 }
121
129 public function canBeActivated($object)
130 {
131 global $langs, $conf, $db;
132
133 $langs->load("bills");
134
135 // Check invoice num
136 $fayymm = '';
137 $max = '';
138
139 $posindice = strlen($this->prefixinvoice) + 6;
140 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
141 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
142 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
143 $sql .= " AND entity = ".$conf->entity;
144
145 $resql = $db->query($sql);
146 if ($resql) {
147 $row = $db->fetch_row($resql);
148 if ($row) {
149 $fayymm = substr($row[0], 0, 6);
150 $max = $row[0];
151 }
152 }
153 if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
154 $langs->load("errors");
155 $this->error = $langs->trans('ErrorNumRefModel', $max);
156 return false;
157 }
158
159 // Check credit note num
160 $fayymm = '';
161
162 $posindice = strlen($this->prefixcreditnote) + 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($this->prefixcreditnote)."____-%'";
166 $sql .= " AND entity = ".$conf->entity;
167
168 $resql = $db->query($sql);
169 if ($resql) {
170 $row = $db->fetch_row($resql);
171 if ($row) {
172 $fayymm = substr($row[0], 0, 6);
173 $max = $row[0];
174 }
175 }
176 if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
177 $this->error = $langs->trans('ErrorNumRefModel', $max);
178 return false;
179 }
180
181 // Check deposit num
182 $fayymm = '';
183
184 $posindice = strlen($this->prefixdeposit) + 6;
185 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
186 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
187 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
188 $sql .= " AND entity = ".$conf->entity;
189
190 $resql = $db->query($sql);
191 if ($resql) {
192 $row = $db->fetch_row($resql);
193 if ($row) {
194 $fayymm = substr($row[0], 0, 6);
195 $max = $row[0];
196 }
197 }
198 if ($fayymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
199 $this->error = $langs->trans('ErrorNumRefModel', $max);
200 return false;
201 }
202
203 return true;
204 }
205
217 public function getNextValue($objsoc, $invoice, $mode = 'next')
218 {
219 global $db;
220
221 dol_syslog(get_class($this)."::getNextValue mode=".$mode, LOG_DEBUG);
222
223 $prefix = $this->prefixinvoice;
224 if ($invoice->type == 2) {
225 $prefix = $this->prefixcreditnote;
226 } elseif ($invoice->type == 3) {
227 $prefix = $this->prefixdeposit;
228 }
229
230 // First we get the max value
231 $posindice = strlen($prefix) + 6;
232 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
233 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
234 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
235 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
236
237 $resql = $db->query($sql);
238 if ($resql) {
239 $obj = $db->fetch_object($resql);
240 if ($obj) {
241 $max = intval($obj->max);
242 } else {
243 $max = 0;
244 }
245 } else {
246 return -1;
247 }
248
249 if ($mode == 'last') {
250 if ($max >= (pow(10, 4) - 1)) {
251 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
252 } else {
253 $num = sprintf("%04d", $max);
254 }
255
256 $ref = '';
257 $sql = "SELECT ref as ref";
258 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
259 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
260 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
261 $sql .= " ORDER BY ref DESC";
262
263 $resql = $db->query($sql);
264 if ($resql) {
265 $obj = $db->fetch_object($resql);
266 if ($obj) {
267 $ref = $obj->ref;
268 }
269 } else {
271 }
272
273 return $ref;
274 } elseif ($mode == 'next') {
275 $date = $invoice->date; // This is invoice date (not creation date)
276 $yymm = dol_print_date($date, "%y%m");
277
278 if ($max >= (pow(10, 4) - 1)) {
279 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
280 } else {
281 $num = sprintf("%04d", $max + 1);
282 }
283
284 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
285 return $prefix.$yymm."-".$num;
286 } else {
287 dol_print_error(null, 'Bad parameter for getNextValue');
288 }
289
290 return 0;
291 }
292
302 public function getNumRef($objsoc, $objforref, $mode = 'next')
303 {
304 return $this->getNextValue($objsoc, $objforref, $mode);
305 }
306}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.