dolibarr 20.0.2
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 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 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
39 public $prefix = 'ER';
40
44 public $error = '';
45
51 public $nom = 'Jade';
52
56 public $name = 'Jade';
57
58
65 public function info($langs)
66 {
67 global $langs;
68 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
69 }
70
71
77 public function getExample()
78 {
79 return $this->prefix."0501-0001";
80 }
81
82
90 public function canBeActivated($object)
91 {
92 global $conf, $langs, $db;
93
94 $coyymm = '';
95 $max = '';
96
97 $posindice = strlen($this->prefix) + 6;
98 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
99 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport";
100 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
101 $sql .= " AND entity = ".$conf->entity;
102
103 $resql = $db->query($sql);
104 if ($resql) {
105 $row = $db->fetch_row($resql);
106 if ($row) {
107 $coyymm = substr($row[0], 0, 6);
108 $max = $row[0];
109 }
110 }
111 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
112 $langs->load("errors");
113 $this->error = $langs->trans('ErrorNumRefModel', $max);
114 return false;
115 }
116
117 return true;
118 }
119
126 public function getNextValue($object)
127 {
128 global $db, $conf;
129
130 // For backward compatibility and restore old behavior to get ref of expense report
131 if (getDolGlobalString('EXPENSEREPORT_USE_OLD_NUMBERING_RULE')) {
132 $fuser = null;
133 if ($object->fk_user_author > 0) {
134 $fuser = new User($db);
135 $fuser->fetch($object->fk_user_author);
136 }
137
138 $expld_car = (!getDolGlobalString('NDF_EXPLODE_CHAR')) ? "-" : $conf->global->NDF_EXPLODE_CHAR;
139 $num_car = (!getDolGlobalString('NDF_NUM_CAR_REF')) ? "5" : $conf->global->NDF_NUM_CAR_REF;
140
141 $sql = 'SELECT MAX(de.ref_number_int) as max';
142 $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport de';
143
144 $result = $db->query($sql);
145
146 if ($db->num_rows($result) > 0) {
147 $objp = $db->fetch_object($result);
148 $newref = $objp->max;
149 $newref++;
150 while (strlen($newref) < $num_car) {
151 $newref = "0".$newref;
152 }
153 } else {
154 $newref = 1;
155 while (strlen((string) $newref) < $num_car) {
156 $newref = "0".$newref;
157 }
158 }
159
160 $ref_number_int = (int) $newref;
161
162 $user_author_infos = dolGetFirstLastname($fuser->firstname, $fuser->lastname);
163
164 $prefix = "ER";
165 if (getDolGlobalString('EXPENSE_REPORT_PREFIX')) {
166 $prefix = getDolGlobalString('EXPENSE_REPORT_PREFIX');
167 }
168 $newref = str_replace(' ', '_', $user_author_infos).$expld_car.$prefix.$newref.$expld_car.dol_print_date($object->date_debut, '%y%m%d');
169
170 $sqlbis = 'UPDATE '.MAIN_DB_PREFIX.'expensereport SET ref_number_int = '.((int) $ref_number_int).' WHERE rowid = '.((int) $object->id);
171 $resqlbis = $db->query($sqlbis);
172 if (!$resqlbis) {
173 dol_print_error($resqlbis);
174 exit;
175 }
176
177 dol_syslog("mod_expensereport_jade::getNextValue return ".$newref);
178 return $newref;
179 }
180
181 // First we get the max value
182 $posindice = strlen($this->prefix) + 6;
183 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
184 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport";
185 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
186 $sql .= " AND entity = ".$conf->entity;
187
188 $resql = $db->query($sql);
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 dol_syslog("mod_expensereport_jade::getNextValue", LOG_DEBUG);
198 return 0;
199 }
200
201 $date = $object->date_valid; // $object->date does not exists
202 if (empty($date)) {
203 $this->error = 'Date valid not defined';
204 return 0;
205 }
206
207 $yymm = dol_print_date($date, "%y%m");
208
209 if ($max >= (pow(10, 4) - 1)) {
210 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
211 } else {
212 $num = sprintf("%04d", $max + 1);
213 }
214
215 dol_syslog("mod_expensereport_jade::getNextValue return ".$this->prefix.$yymm."-".$num);
216 return $this->prefix.$yymm."-".$num;
217 }
218}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.
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).
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
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.