dolibarr 21.0.0-alpha
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{
39 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
40
41 public $prefixinvoice = 'FA';
42
43 public $prefixreplacement = 'FR';
44
45 public $prefixdeposit = 'AC';
46
47 public $prefixcreditnote = 'AV';
48
52 public $error = '';
53
54
58 public function __construct()
59 {
60 global $conf, $mysoc;
61
62 if ((float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code != 'FR') {
63 $this->prefixinvoice = 'IN'; // We use correct standard code "IN = Invoice"
64 $this->prefixreplacement = 'IR';
65 $this->prefixdeposit = 'ID';
66 $this->prefixcreditnote = 'IC';
67 }
68
69 if (getDolGlobalString('INVOICE_NUMBERING_MARS_FORCE_PREFIX')) {
70 $this->prefixinvoice = getDolGlobalString('INVOICE_NUMBERING_MARS_FORCE_PREFIX');
71 }
72 }
73
80 public function info($langs)
81 {
82 global $langs;
83 $langs->load("bills");
84 return $langs->trans('MarsNumRefModelDesc1', $this->prefixinvoice, $this->prefixreplacement, $this->prefixdeposit, $this->prefixcreditnote);
85 }
86
92 public function getExample()
93 {
94 return $this->prefixinvoice."0501-0001";
95 }
96
104 public function canBeActivated($object)
105 {
106 global $langs, $conf, $db;
107
108 $langs->load("bills");
109
110 // Check invoice num
111 $fayymm = '';
112 $max = '';
113
114 $posindice = strlen($this->prefixinvoice) + 6;
115 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
116 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
117 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
118 $sql .= " AND entity = ".$conf->entity;
119
120 $resql = $db->query($sql);
121 if ($resql) {
122 $row = $db->fetch_row($resql);
123 if ($row) {
124 $fayymm = substr($row[0], 0, 6);
125 $max = $row[0];
126 }
127 }
128 if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
129 $langs->load("errors");
130 $this->error = $langs->trans('ErrorNumRefModel', $max);
131 return false;
132 }
133
134 // Check credit note num
135 $fayymm = '';
136
137 $posindice = strlen($this->prefixcreditnote) + 6;
138 $sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
139 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
140 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
141 $sql .= " AND entity = ".$conf->entity;
142
143 $resql = $db->query($sql);
144 if ($resql) {
145 $row = $db->fetch_row($resql);
146 if ($row) {
147 $fayymm = substr($row[0], 0, 6);
148 $max = $row[0];
149 }
150 }
151 if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
152 $this->error = $langs->trans('ErrorNumRefModel', $max);
153 return false;
154 }
155
156 return true;
157 }
158
167 public function getNextValue($objsoc, $invoice, $mode = 'next')
168 {
169 global $db;
170
171 $prefix = $this->prefixinvoice;
172 if ($invoice->type == 1) {
173 $prefix = $this->prefixreplacement;
174 } elseif ($invoice->type == 2) {
175 $prefix = $this->prefixcreditnote;
176 } elseif ($invoice->type == 3) {
177 $prefix = $this->prefixdeposit;
178 }
179
180 // First we get the max value
181 $posindice = strlen($prefix) + 6;
182 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
183 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
184 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
185 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
186
187 $resql = $db->query($sql);
188 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
189 if ($resql) {
190 $obj = $db->fetch_object($resql);
191 if ($obj) {
192 $max = intval($obj->max);
193 } else {
194 $max = 0;
195 }
196 } else {
197 return -1;
198 }
199
200 if ($mode == 'last') {
201 if ($max >= (pow(10, 4) - 1)) {
202 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
203 } else {
204 $num = sprintf("%04d", $max);
205 }
206
207 $ref = '';
208 $sql = "SELECT ref as ref";
209 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
210 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
211 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
212 $sql .= " ORDER BY ref DESC";
213
214 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
215 $resql = $db->query($sql);
216 if ($resql) {
217 $obj = $db->fetch_object($resql);
218 if ($obj) {
219 $ref = $obj->ref;
220 }
221 } else {
222 dol_print_error($db);
223 }
224
225 return $ref;
226 } elseif ($mode == 'next') {
227 $date = $invoice->date; // This is invoice date (not creation date)
228 $yymm = dol_print_date($date, "%y%m");
229
230 if ($max >= (pow(10, 4) - 1)) {
231 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
232 } else {
233 $num = sprintf("%04d", $max + 1);
234 }
235
236 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
237 return $prefix.$yymm."-".$num;
238 } else {
239 dol_print_error(null, 'Bad parameter for getNextValue');
240 return -1;
241 }
242 }
243
253 public function getNumRef($objsoc, $objforref, $mode = 'next')
254 {
255 return $this->getNextValue($objsoc, $objforref, $mode);
256 }
257}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.