dolibarr 19.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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
26require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
27
32{
37 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
38
39 public $prefixinvoice = 'FA';
40
41 public $prefixreplacement = 'FR';
42
43 public $prefixdeposit = 'AC';
44
45 public $prefixcreditnote = 'AV';
46
50 public $error = '';
51
52
56 public function __construct()
57 {
58 global $conf, $mysoc;
59
60 if ((float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code != 'FR') {
61 $this->prefixinvoice = 'IN'; // We use correct standard code "IN = Invoice"
62 $this->prefixreplacement = 'IR';
63 $this->prefixdeposit = 'ID';
64 $this->prefixcreditnote = 'IC';
65 }
66
67 if (getDolGlobalString('INVOICE_NUMBERING_MARS_FORCE_PREFIX')) {
68 $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX;
69 }
70 }
71
78 public function info($langs)
79 {
80 global $langs;
81 $langs->load("bills");
82 return $langs->trans('MarsNumRefModelDesc1', $this->prefixinvoice, $this->prefixreplacement, $this->prefixdeposit, $this->prefixcreditnote);
83 }
84
90 public function getExample()
91 {
92 return $this->prefixinvoice."0501-0001";
93 }
94
102 public function canBeActivated($object)
103 {
104 global $langs, $conf, $db;
105
106 $langs->load("bills");
107
108 // Check invoice num
109 $fayymm = '';
110 $max = '';
111
112 $posindice = strlen($this->prefixinvoice) + 6;
113 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
114 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
115 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
116 $sql .= " AND entity = ".$conf->entity;
117
118 $resql = $db->query($sql);
119 if ($resql) {
120 $row = $db->fetch_row($resql);
121 if ($row) {
122 $fayymm = substr($row[0], 0, 6);
123 $max = $row[0];
124 }
125 }
126 if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
127 $langs->load("errors");
128 $this->error = $langs->trans('ErrorNumRefModel', $max);
129 return false;
130 }
131
132 // Check credit note num
133 $fayymm = '';
134
135 $posindice = strlen($this->prefixcreditnote) + 6;
136 $sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
137 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
138 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
139 $sql .= " AND entity = ".$conf->entity;
140
141 $resql = $db->query($sql);
142 if ($resql) {
143 $row = $db->fetch_row($resql);
144 if ($row) {
145 $fayymm = substr($row[0], 0, 6);
146 $max = $row[0];
147 }
148 }
149 if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
150 $this->error = $langs->trans('ErrorNumRefModel', $max);
151 return false;
152 }
153
154 return true;
155 }
156
165 public function getNextValue($objsoc, $invoice, $mode = 'next')
166 {
167 global $db;
168
169 $prefix = $this->prefixinvoice;
170 if ($invoice->type == 1) {
171 $prefix = $this->prefixreplacement;
172 } elseif ($invoice->type == 2) {
173 $prefix = $this->prefixcreditnote;
174 } elseif ($invoice->type == 3) {
175 $prefix = $this->prefixdeposit;
176 }
177
178 // First we get the max value
179 $posindice = strlen($prefix) + 6;
180 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
181 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
182 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
183 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
184
185 $resql = $db->query($sql);
186 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
187 if ($resql) {
188 $obj = $db->fetch_object($resql);
189 if ($obj) {
190 $max = intval($obj->max);
191 } else {
192 $max = 0;
193 }
194 } else {
195 return -1;
196 }
197
198 if ($mode == 'last') {
199 if ($max >= (pow(10, 4) - 1)) {
200 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
201 } else {
202 $num = sprintf("%04s", $max);
203 }
204
205 $ref = '';
206 $sql = "SELECT ref as ref";
207 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
208 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
209 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
210 $sql .= " ORDER BY ref DESC";
211
212 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
213 $resql = $db->query($sql);
214 if ($resql) {
215 $obj = $db->fetch_object($resql);
216 if ($obj) {
217 $ref = $obj->ref;
218 }
219 } else {
220 dol_print_error($db);
221 }
222
223 return $ref;
224 } elseif ($mode == 'next') {
225 $date = $invoice->date; // This is invoice date (not creation date)
226 $yymm = dol_print_date($date, "%y%m");
227
228 if ($max >= (pow(10, 4) - 1)) {
229 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
230 } else {
231 $num = sprintf("%04s", $max + 1);
232 }
233
234 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
235 return $prefix.$yymm."-".$num;
236 } else {
237 dol_print_error('', 'Bad parameter for getNextValue');
238 return -1;
239 }
240 }
241
250 public function getNumRef($objsoc, $objforref, $mode = 'next')
251 {
252 return $this->getNextValue($objsoc, $objforref, $mode);
253 }
254}
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_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.