dolibarr 19.0.4
mod_expensereport_jade.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017 Maxime Kohlhaas <support@atm-consulting.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 * or see https://www.gnu.org/
17 */
18
24require_once DOL_DOCUMENT_ROOT.'/core/modules/expensereport/modules_expensereport.php';
25
30{
35 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
36
37 public $prefix = 'ER';
38
42 public $error = '';
43
49 public $nom = 'Jade';
50
54 public $name = 'Jade';
55
56
63 public function info($langs)
64 {
65 global $langs;
66 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
67 }
68
69
75 public function getExample()
76 {
77 return $this->prefix."0501-0001";
78 }
79
80
88 public function canBeActivated($object)
89 {
90 global $conf, $langs, $db;
91
92 $coyymm = '';
93 $max = '';
94
95 $posindice = strlen($this->prefix) + 6;
96 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
97 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport";
98 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
99 $sql .= " AND entity = ".$conf->entity;
100
101 $resql = $db->query($sql);
102 if ($resql) {
103 $row = $db->fetch_row($resql);
104 if ($row) {
105 $coyymm = substr($row[0], 0, 6);
106 $max = $row[0];
107 }
108 }
109 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
110 $langs->load("errors");
111 $this->error = $langs->trans('ErrorNumRefModel', $max);
112 return false;
113 }
114
115 return true;
116 }
117
124 public function getNextValue($object)
125 {
126 global $db, $conf;
127
128 // For backward compatibility and restore old behavior to get ref of expense report
129 if (getDolGlobalString('EXPENSEREPORT_USE_OLD_NUMBERING_RULE')) {
130 $fuser = null;
131 if ($object->fk_user_author > 0) {
132 $fuser = new User($db);
133 $fuser->fetch($object->fk_user_author);
134 }
135
136 $expld_car = (!getDolGlobalString('NDF_EXPLODE_CHAR')) ? "-" : $conf->global->NDF_EXPLODE_CHAR;
137 $num_car = (!getDolGlobalString('NDF_NUM_CAR_REF')) ? "5" : $conf->global->NDF_NUM_CAR_REF;
138
139 $sql = 'SELECT MAX(de.ref_number_int) as max';
140 $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport de';
141
142 $result = $db->query($sql);
143
144 if ($db->num_rows($result) > 0) {
145 $objp = $db->fetch_object($result);
146 $newref = $objp->max;
147 $newref++;
148 while (strlen($newref) < $num_car) {
149 $newref = "0".$newref;
150 }
151 } else {
152 $newref = 1;
153 while (strlen($newref) < $num_car) {
154 $newref = "0".$newref;
155 }
156 }
157
158 $ref_number_int = ($newref + 1) - 1;
159
160 $user_author_infos = dolGetFirstLastname($fuser->firstname, $fuser->lastname);
161
162 $prefix = "ER";
163 if (getDolGlobalString('EXPENSE_REPORT_PREFIX')) {
164 $prefix = $conf->global->EXPENSE_REPORT_PREFIX;
165 }
166 $newref = str_replace(' ', '_', $user_author_infos).$expld_car.$prefix.$newref.$expld_car.dol_print_date($object->date_debut, '%y%m%d');
167
168 $sqlbis = 'UPDATE '.MAIN_DB_PREFIX.'expensereport SET ref_number_int = '.((int) $ref_number_int).' WHERE rowid = '.((int) $object->id);
169 $resqlbis = $db->query($sqlbis);
170 if (!$resqlbis) {
171 dol_print_error($resqlbis);
172 exit;
173 }
174
175 dol_syslog("mod_expensereport_jade::getNextValue return ".$newref);
176 return $newref;
177 }
178
179 // First we get the max value
180 $posindice = strlen($this->prefix) + 6;
181 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
182 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport";
183 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
184 $sql .= " AND entity = ".$conf->entity;
185
186 $resql = $db->query($sql);
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 dol_syslog("mod_expensereport_jade::getNextValue", LOG_DEBUG);
196 return 0;
197 }
198
199 $date = $object->date_valid; // $object->date does not exists
200 if (empty($date)) {
201 $this->error = 'Date valid not defined';
202 return 0;
203 }
204
205 $yymm = dol_print_date($date, "%y%m");
206
207 if ($max >= (pow(10, 4) - 1)) {
208 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
209 } else {
210 $num = sprintf("%04s", $max + 1);
211 }
212
213 dol_syslog("mod_expensereport_jade::getNextValue return ".$this->prefix.$yymm."-".$num);
214 return $this->prefix.$yymm."-".$num;
215 }
216}
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_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).
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
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.