dolibarr 23.0.3
mod_expensereport_jade.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017 Maxime Kohlhaas <support@atm-consulting.fr>
3 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
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/expensereport/modules_expensereport.php';
27
32{
37 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
38
42 public $prefix = 'ER';
43
47 public $error = '';
48
54 public $nom = 'Jade';
55
59 public $name = 'Jade';
60
61
68 public function info($langs)
69 {
70 global $langs;
71 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
72 }
73
74
80 public function getExample()
81 {
82 return $this->prefix."0501-0001";
83 }
84
85
93 public function canBeActivated($object)
94 {
95 global $conf, $langs, $db;
96
97 $coyymm = '';
98 $max = '';
99
100 $posindice = strlen($this->prefix) + 6;
101 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
102 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport";
103 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
104 $sql .= " AND entity = ".$conf->entity;
105
106 $resql = $db->query($sql);
107 if ($resql) {
108 $row = $db->fetch_row($resql);
109 if ($row) {
110 $coyymm = substr($row[0], 0, 6);
111 $max = $row[0];
112 }
113 }
114 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
115 $langs->load("errors");
116 $this->error = $langs->trans('ErrorNumRefModel', $max);
117 return false;
118 }
119
120 return true;
121 }
122
129 public function getNextValue($object)
130 {
131 global $db, $conf;
132
133 // For backward compatibility and restore old behavior to get ref of expense report
134 if (getDolGlobalString('EXPENSEREPORT_USE_OLD_NUMBERING_RULE')) {
135 $fuser = null;
136 if ($object->fk_user_author > 0) {
137 $fuser = new User($db);
138 $fuser->fetch($object->fk_user_author);
139 }
140
141 $expld_car = (!getDolGlobalString('NDF_EXPLODE_CHAR')) ? "-" : $conf->global->NDF_EXPLODE_CHAR;
142 $num_car = (!getDolGlobalString('NDF_NUM_CAR_REF')) ? "5" : $conf->global->NDF_NUM_CAR_REF;
143
144 $sql = 'SELECT MAX(de.ref_number_int) as max';
145 $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport de';
146
147 $result = $db->query($sql);
148
149 if ($db->num_rows($result) > 0) {
150 $objp = $db->fetch_object($result);
151 $newref = $objp->max;
152 $newref++;
153 while (strlen($newref) < $num_car) {
154 $newref = "0".$newref;
155 }
156 } else {
157 $newref = 1;
158 while (strlen((string) $newref) < $num_car) {
159 $newref = "0".$newref;
160 }
161 }
162
163 $ref_number_int = (int) $newref;
164
165 $user_author_infos = dolGetFirstLastname($fuser->firstname, $fuser->lastname);
166
167 $prefix = "ER";
168 if (getDolGlobalString('EXPENSE_REPORT_PREFIX')) {
169 $prefix = getDolGlobalString('EXPENSE_REPORT_PREFIX');
170 }
171 $newref = str_replace(' ', '_', $user_author_infos).$expld_car.$prefix.$newref.$expld_car.dol_print_date($object->date_debut, '%y%m%d');
172
173 $sqlbis = 'UPDATE '.MAIN_DB_PREFIX.'expensereport SET ref_number_int = '.((int) $ref_number_int).' WHERE rowid = '.((int) $object->id);
174 $resqlbis = $db->query($sqlbis);
175 if (!$resqlbis) {
176 dol_print_error($db, $db->lasterror());
177 exit;
178 }
179
180 dol_syslog("mod_expensereport_jade::getNextValue return ".$newref);
181 return $newref;
182 }
183
184 // First we get the max value
185 $posindice = strlen($this->prefix) + 6;
186 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
187 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport";
188 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
189 $sql .= " AND entity = ".$conf->entity;
190
191 $resql = $db->query($sql);
192 if ($resql) {
193 $obj = $db->fetch_object($resql);
194 if ($obj) {
195 $max = intval($obj->max);
196 } else {
197 $max = 0;
198 }
199 } else {
200 dol_syslog("mod_expensereport_jade::getNextValue", LOG_DEBUG);
201 return 0;
202 }
203
204 $date = $object->date_valid; // $object->date does not exists
205 if (empty($date)) {
206 $this->error = 'Date valid not defined';
207 return 0;
208 }
209
210 $yymm = dol_print_date($date, "%y%m");
211
212 if ($max >= (pow(10, 4) - 1)) {
213 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
214 } else {
215 $num = sprintf("%04d", $max + 1);
216 }
217
218 dol_syslog("mod_expensereport_jade::getNextValue return ".$this->prefix.$yymm."-".$num);
219 return $this->prefix.$yymm."-".$num;
220 }
221}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class for numbering masks of expense reports.
Class to manage Dolibarr users.
Class to manage expensereport numbering rules Jade.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
getExample()
Returns an example of numbering.
info($langs)
Return description of numbering model.
getNextValue($object)
Return next free value.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
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.