dolibarr 21.0.0-beta
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
31
32
37{
42 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
43
47 public $error = '';
48
54 public $nom = 'Cactus';
55
59 public $name = 'Cactus';
60
64 public $prefixinvoice = 'SI';
65
69 public $prefixcreditnote = 'SA';
70
74 public $prefixdeposit = 'SD';
75
76
83 public function info($langs)
84 {
85 global $langs;
86 $langs->load("bills");
87 return $langs->trans("CactusNumRefModelDesc1", $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
88 }
89
90
96 public function getExample()
97 {
98 return $this->prefixinvoice."1301-0001";
99 }
100
101
108 public function canBeActivated($object)
109 {
110 global $conf, $langs, $db;
111
112 $langs->load("bills");
113
114 // Check invoice num
115 $siyymm = '';
116 $max = '';
117
118 $posindice = strlen($this->prefixinvoice) + 6;
119 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
120 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
121 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
122 $sql .= " AND entity = ".$conf->entity;
123 $resql = $db->query($sql);
124 if ($resql) {
125 $row = $db->fetch_row($resql);
126 if ($row) {
127 $siyymm = substr($row[0], 0, 6);
128 $max = $row[0];
129 }
130 }
131 if ($siyymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
132 $langs->load("errors");
133 $this->error = $langs->trans('ErrorNumRefModel', $max);
134 return false;
135 }
136
137 // Check credit note num
138 $siyymm = '';
139
140 $posindice = strlen($this->prefixcreditnote) + 6;
141 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
142 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
143 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
144 $sql .= " AND entity = ".$conf->entity;
145
146 $resql = $db->query($sql);
147 if ($resql) {
148 $row = $db->fetch_row($resql);
149 if ($row) {
150 $siyymm = substr($row[0], 0, 6);
151 $max = $row[0];
152 }
153 }
154 if ($siyymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
155 $this->error = $langs->trans('ErrorNumRefModel', $max);
156 return false;
157 }
158
159 // Check deposit num
160 $siyymm = '';
161
162 $posindice = strlen($this->prefixdeposit) + 6;
163 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
164 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
165 $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
166 $sql .= " AND entity = ".$conf->entity;
167
168 $resql = $db->query($sql);
169 if ($resql) {
170 $row = $db->fetch_row($resql);
171 if ($row) {
172 $siyymm = substr($row[0], 0, 6);
173 $max = $row[0];
174 }
175 }
176 if ($siyymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
177 $this->error = $langs->trans('ErrorNumRefModel', $max);
178 return false;
179 }
180
181 return true;
182 }
183
192 public function getNextValue($objsoc, $object, $mode = 'next')
193 {
194 global $db, $conf;
195
196 $prefix = $this->prefixinvoice;
197 if ($object->type == 2) {
198 $prefix = $this->prefixcreditnote;
199 } elseif ($object->type == 3) {
200 $prefix = $this->prefixdeposit;
201 }
202
203 // First, we get the max value
204 $posindice = strlen($prefix) + 6;
205 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
206 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
207 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
208 $sql .= " AND entity = ".$conf->entity;
209
210 $resql = $db->query($sql);
211 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
212 if ($resql) {
213 $obj = $db->fetch_object($resql);
214 if ($obj) {
215 $max = intval($obj->max);
216 } else {
217 $max = 0;
218 }
219 } else {
220 return -1;
221 }
222
223 if ($mode == 'last') {
224 if ($max >= (pow(10, 4) - 1)) {
225 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
226 } else {
227 $num = sprintf("%04d", $max);
228 }
229
230 $ref = '';
231 $sql = "SELECT ref as ref";
232 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
233 $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
234 $sql .= " AND entity = ".$conf->entity;
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 = $object->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
266
276 public function getNumRef($objsoc, $objforref, $mode = 'next')
277 {
278 return $this->getNextValue($objsoc, $objforref, $mode);
279 }
280}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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_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...
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