dolibarr 23.0.3
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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
28require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
29
34{
38 public $name = 'Mars';
39
43 public $position = 30;
44
49 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
50
54 public $prefixinvoice = 'FA';
55
59 public $prefixreplacement = 'FR';
60
64 public $prefixdeposit = 'AC';
65
69 public $prefixcreditnote = 'AV';
70
74 public $error = '';
75
76
80 public function __construct()
81 {
82 global $conf, $mysoc;
83
84 if ((float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code != 'FR') {
85 $this->prefixinvoice = 'IN'; // We use correct standard code "IN = Invoice"
86 $this->prefixreplacement = 'IR';
87 $this->prefixdeposit = 'ID';
88 $this->prefixcreditnote = 'IC';
89 }
90
91 if (getDolGlobalString('INVOICE_NUMBERING_MARS_FORCE_PREFIX')) {
92 $this->prefixinvoice = getDolGlobalString('INVOICE_NUMBERING_MARS_FORCE_PREFIX');
93 }
94 }
95
102 public function info($langs)
103 {
104 global $langs;
105 $langs->load("bills");
106 return $langs->trans('MarsNumRefModelDesc1', $this->prefixinvoice, $this->prefixreplacement, $this->prefixdeposit, $this->prefixcreditnote);
107 }
108
114 public function getExample()
115 {
116 return $this->prefixinvoice."0501-0001";
117 }
118
126 public function canBeActivated($object)
127 {
128 global $langs, $conf, $db;
129
130 $langs->load("bills");
131
132 // Check invoice num
133 $fayymm = '';
134 $max = '';
135
136 $posindice = strlen($this->prefixinvoice) + 6;
137 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
138 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
139 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
140 $sql .= " AND entity = ".$conf->entity;
141
142 $resql = $db->query($sql);
143 if ($resql) {
144 $row = $db->fetch_row($resql);
145 if ($row) {
146 $fayymm = substr($row[0], 0, 6);
147 $max = $row[0];
148 }
149 }
150 if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
151 $langs->load("errors");
152 $this->error = $langs->trans('ErrorNumRefModel', $max);
153 return false;
154 }
155
156 // Check credit note num
157 $fayymm = '';
158
159 $posindice = strlen($this->prefixcreditnote) + 6;
160 $sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
161 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
162 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
163 $sql .= " AND entity = ".$conf->entity;
164
165 $resql = $db->query($sql);
166 if ($resql) {
167 $row = $db->fetch_row($resql);
168 if ($row) {
169 $fayymm = substr($row[0], 0, 6);
170 $max = $row[0];
171 }
172 }
173 if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
174 $this->error = $langs->trans('ErrorNumRefModel', $max);
175 return false;
176 }
177
178 return true;
179 }
180
189 public function getNextValue($objsoc, $invoice, $mode = 'next')
190 {
191 global $db;
192
193 $prefix = $this->prefixinvoice;
194 if ($invoice->type == 1) {
195 $prefix = $this->prefixreplacement;
196 } elseif ($invoice->type == 2) {
197 $prefix = $this->prefixcreditnote;
198 } elseif ($invoice->type == 3) {
199 $prefix = $this->prefixdeposit;
200 }
201
202 // First we get the max value
203 $posindice = strlen($prefix) + 6;
204 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
205 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
206 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
207 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
208
209 $resql = $db->query($sql);
210 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
211 if ($resql) {
212 $obj = $db->fetch_object($resql);
213 if ($obj) {
214 $max = intval($obj->max);
215 } else {
216 $max = 0;
217 }
218 } else {
219 return -1;
220 }
221
222 if ($mode == 'last') {
223 if ($max >= (pow(10, 4) - 1)) {
224 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
225 } else {
226 $num = sprintf("%04d", $max);
227 }
228
229 $ref = '';
230 $sql = "SELECT ref as ref";
231 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
232 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
233 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
234 $sql .= " ORDER BY ref DESC";
235
236 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
237 $resql = $db->query($sql);
238 if ($resql) {
239 $obj = $db->fetch_object($resql);
240 if ($obj) {
241 $ref = $obj->ref;
242 }
243 } else {
244 dol_print_error($db);
245 }
246
247 return $ref;
248 } elseif ($mode == 'next') {
249 $date = $invoice->date; // This is invoice date (not creation date)
250 $yymm = dol_print_date($date, "%y%m");
251
252 if ($max >= (pow(10, 4) - 1)) {
253 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
254 } else {
255 $num = sprintf("%04d", $max + 1);
256 }
257
258 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
259 return $prefix.$yymm."-".$num;
260 } else {
261 dol_print_error(null, 'Bad parameter for getNextValue');
262 return -1;
263 }
264 }
265
275 public function getNumRef($objsoc, $objforref, $mode = 'next')
276 {
277 return $this->getNextValue($objsoc, $objforref, $mode);
278 }
279}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class of invoice reference numbering templates.
Class to manage invoice numbering rules Mars.
getExample()
Return an example of numbering.
__construct()
Constructor.
getNextValue($objsoc, $invoice, $mode='next')
Return next value not used or last value used.
info($langs)
Returns the description of the numbering model.
canBeActivated($object)
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.
global $mysoc
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.