dolibarr 19.0.3
mod_facture_fournisseur_cactus.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-2009 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2013-2018 Philippe Grand <philippe.grand@atoo-net.com>
5 * Copyright (C) 2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
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
28require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
29
30
35{
40 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
41
45 public $error = '';
46
52 public $nom = 'Cactus';
53
57 public $name = 'Cactus';
58
59 public $prefixinvoice = 'SI';
60
61 public $prefixcreditnote = 'SA';
62
63 public $prefixdeposit = 'SD';
64
65
72 public function info($langs)
73 {
74 global $langs;
75 $langs->load("bills");
76 return $langs->trans("CactusNumRefModelDesc1", $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
77 }
78
79
85 public function getExample()
86 {
87 return $this->prefixinvoice."1301-0001";
88 }
89
90
97 public function canBeActivated($object)
98 {
99 global $conf, $langs, $db;
100
101 $langs->load("bills");
102
103 // Check invoice num
104 $siyymm = '';
105 $max = '';
106
107 $posindice = strlen($this->prefixinvoice) + 6;
108 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
109 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
110 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
111 $sql .= " AND entity = ".$conf->entity;
112 $resql = $db->query($sql);
113 if ($resql) {
114 $row = $db->fetch_row($resql);
115 if ($row) {
116 $siyymm = substr($row[0], 0, 6);
117 $max = $row[0];
118 }
119 }
120 if ($siyymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
121 $langs->load("errors");
122 $this->error = $langs->trans('ErrorNumRefModel', $max);
123 return false;
124 }
125
126 // Check credit note num
127 $siyymm = '';
128
129 $posindice = strlen($this->prefixcreditnote) + 6;
130 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
131 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
132 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
133 $sql .= " AND entity = ".$conf->entity;
134
135 $resql = $db->query($sql);
136 if ($resql) {
137 $row = $db->fetch_row($resql);
138 if ($row) {
139 $siyymm = substr($row[0], 0, 6);
140 $max = $row[0];
141 }
142 }
143 if ($siyymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
144 $this->error = $langs->trans('ErrorNumRefModel', $max);
145 return false;
146 }
147
148 // Check deposit num
149 $siyymm = '';
150
151 $posindice = strlen($this->prefixdeposit) + 6;
152 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
153 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
154 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
155 $sql .= " AND entity = ".$conf->entity;
156
157 $resql = $db->query($sql);
158 if ($resql) {
159 $row = $db->fetch_row($resql);
160 if ($row) {
161 $siyymm = substr($row[0], 0, 6);
162 $max = $row[0];
163 }
164 }
165 if ($siyymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
166 $this->error = $langs->trans('ErrorNumRefModel', $max);
167 return false;
168 }
169
170 return true;
171 }
172
181 public function getNextValue($objsoc, $object, $mode = 'next')
182 {
183 global $db, $conf;
184
185 $prefix = $this->prefixinvoice;
186 if ($object->type == 2) {
187 $prefix = $this->prefixcreditnote;
188 } elseif ($object->type == 3) {
189 $prefix = $this->prefixdeposit;
190 }
191
192 // First, we get the max value
193 $posindice = strlen($prefix) + 6;
194 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
195 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
196 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
197 $sql .= " AND entity = ".$conf->entity;
198
199 $resql = $db->query($sql);
200 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
201 if ($resql) {
202 $obj = $db->fetch_object($resql);
203 if ($obj) {
204 $max = intval($obj->max);
205 } else {
206 $max = 0;
207 }
208 } else {
209 return -1;
210 }
211
212 if ($mode == 'last') {
213 if ($max >= (pow(10, 4) - 1)) {
214 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
215 } else {
216 $num = sprintf("%04s", $max);
217 }
218
219 $ref = '';
220 $sql = "SELECT ref as ref";
221 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
222 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
223 $sql .= " AND entity = ".$conf->entity;
224
225 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
226 $resql = $db->query($sql);
227 if ($resql) {
228 $obj = $db->fetch_object($resql);
229 if ($obj) {
230 $ref = $obj->ref;
231 }
232 } else {
233 dol_print_error($db);
234 }
235
236 return $ref;
237 } elseif ($mode == 'next') {
238 $date = $object->date; // This is invoice date (not creation date)
239 $yymm = dol_print_date($date, "%y%m");
240
241 if ($max >= (pow(10, 4) - 1)) {
242 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
243 } else {
244 $num = sprintf("%04s", $max + 1);
245 }
246
247 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
248 return $prefix.$yymm."-".$num;
249 } else {
250 dol_print_error('', 'Bad parameter for getNextValue');
251 return -1;
252 }
253 }
254
255
264 public function getNumRef($objsoc, $objforref, $mode = 'next')
265 {
266 return $this->getNextValue($objsoc, $objforref, $mode);
267 }
268}
Parent Class of numbering models of suppliers invoices references.
Cactus Class of numbering models of suppliers invoices references.
getNextValue($objsoc, $object, $mode='next')
Return next value.
canBeActivated($object)
Tests if the numbers already in the database do not cause conflicts that would prevent this numbering...
info($langs)
Return description of numbering model.
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).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.