dolibarr 22.0.5
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 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
51 public $prefixinvoice = 'FA';
52
53 public $prefixreplacement = 'FR';
54
55 public $prefixdeposit = 'AC';
56
57 public $prefixcreditnote = 'AV';
58
62 public $error = '';
63
64
68 public function __construct()
69 {
70 global $conf, $mysoc;
71
72 if ((float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code != 'FR') {
73 $this->prefixinvoice = 'IN'; // We use correct standard code "IN = Invoice"
74 $this->prefixreplacement = 'IR';
75 $this->prefixdeposit = 'ID';
76 $this->prefixcreditnote = 'IC';
77 }
78
79 if (getDolGlobalString('INVOICE_NUMBERING_MARS_FORCE_PREFIX')) {
80 $this->prefixinvoice = getDolGlobalString('INVOICE_NUMBERING_MARS_FORCE_PREFIX');
81 }
82 }
83
90 public function info($langs)
91 {
92 global $langs;
93 $langs->load("bills");
94 return $langs->trans('MarsNumRefModelDesc1', $this->prefixinvoice, $this->prefixreplacement, $this->prefixdeposit, $this->prefixcreditnote);
95 }
96
102 public function getExample()
103 {
104 return $this->prefixinvoice."0501-0001";
105 }
106
114 public function canBeActivated($object)
115 {
116 global $langs, $conf, $db;
117
118 $langs->load("bills");
119
120 // Check invoice num
121 $fayymm = '';
122 $max = '';
123
124 $posindice = strlen($this->prefixinvoice) + 6;
125 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
126 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
127 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
128 $sql .= " AND entity = ".$conf->entity;
129
130 $resql = $db->query($sql);
131 if ($resql) {
132 $row = $db->fetch_row($resql);
133 if ($row) {
134 $fayymm = substr($row[0], 0, 6);
135 $max = $row[0];
136 }
137 }
138 if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
139 $langs->load("errors");
140 $this->error = $langs->trans('ErrorNumRefModel', $max);
141 return false;
142 }
143
144 // Check credit note num
145 $fayymm = '';
146
147 $posindice = strlen($this->prefixcreditnote) + 6;
148 $sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
149 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
150 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
151 $sql .= " AND entity = ".$conf->entity;
152
153 $resql = $db->query($sql);
154 if ($resql) {
155 $row = $db->fetch_row($resql);
156 if ($row) {
157 $fayymm = substr($row[0], 0, 6);
158 $max = $row[0];
159 }
160 }
161 if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
162 $this->error = $langs->trans('ErrorNumRefModel', $max);
163 return false;
164 }
165
166 return true;
167 }
168
177 public function getNextValue($objsoc, $invoice, $mode = 'next')
178 {
179 global $db;
180
181 $prefix = $this->prefixinvoice;
182 if ($invoice->type == 1) {
183 $prefix = $this->prefixreplacement;
184 } elseif ($invoice->type == 2) {
185 $prefix = $this->prefixcreditnote;
186 } elseif ($invoice->type == 3) {
187 $prefix = $this->prefixdeposit;
188 }
189
190 // First we get the max value
191 $posindice = strlen($prefix) + 6;
192 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
193 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
194 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
195 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
196
197 $resql = $db->query($sql);
198 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
199 if ($resql) {
200 $obj = $db->fetch_object($resql);
201 if ($obj) {
202 $max = intval($obj->max);
203 } else {
204 $max = 0;
205 }
206 } else {
207 return -1;
208 }
209
210 if ($mode == 'last') {
211 if ($max >= (pow(10, 4) - 1)) {
212 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
213 } else {
214 $num = sprintf("%04d", $max);
215 }
216
217 $ref = '';
218 $sql = "SELECT ref as ref";
219 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
220 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
221 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
222 $sql .= " ORDER BY ref DESC";
223
224 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
225 $resql = $db->query($sql);
226 if ($resql) {
227 $obj = $db->fetch_object($resql);
228 if ($obj) {
229 $ref = $obj->ref;
230 }
231 } else {
232 dol_print_error($db);
233 }
234
235 return $ref;
236 } elseif ($mode == 'next') {
237 $date = $invoice->date; // This is invoice date (not creation date)
238 $yymm = dol_print_date($date, "%y%m");
239
240 if ($max >= (pow(10, 4) - 1)) {
241 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
242 } else {
243 $num = sprintf("%04d", $max + 1);
244 }
245
246 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
247 return $prefix.$yymm."-".$num;
248 } else {
249 dol_print_error(null, 'Bad parameter for getNextValue');
250 return -1;
251 }
252 }
253
263 public function getNumRef($objsoc, $objforref, $mode = 'next')
264 {
265 return $this->getNextValue($objsoc, $objforref, $mode);
266 }
267}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79