88 global $conf, $langs, $db;
93 $posindice = strlen($this->prefix) + 6;
94 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
95 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport";
96 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefix).
"____-%'";
97 $sql .=
" AND entity = ".$conf->entity;
99 $resql = $db->query($sql);
101 $row = $db->fetch_row($resql);
103 $coyymm = substr($row[0], 0, 6);
107 if ($coyymm && !preg_match(
'/'.$this->prefix.
'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
108 $langs->load(
"errors");
109 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
127 if (!empty($conf->global->EXPENSEREPORT_USE_OLD_NUMBERING_RULE)) {
129 if ($object->fk_user_author > 0) {
130 $fuser =
new User($db);
131 $fuser->fetch($object->fk_user_author);
134 $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR)) ?
"-" : $conf->global->NDF_EXPLODE_CHAR;
135 $num_car = (empty($conf->global->NDF_NUM_CAR_REF)) ?
"5" : $conf->global->NDF_NUM_CAR_REF;
137 $sql =
'SELECT MAX(de.ref_number_int) as max';
138 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expensereport de';
140 $result = $db->query($sql);
142 if ($db->num_rows($result) > 0) {
143 $objp = $db->fetch_object($result);
144 $newref = $objp->max;
146 while (strlen($newref) < $num_car) {
147 $newref =
"0".$newref;
151 while (strlen($newref) < $num_car) {
152 $newref =
"0".$newref;
156 $ref_number_int = ($newref + 1) - 1;
161 if (!empty($conf->global->EXPENSE_REPORT_PREFIX)) {
162 $prefix = $conf->global->EXPENSE_REPORT_PREFIX;
164 $newref = str_replace(
' ',
'_', $user_author_infos).$expld_car.$prefix.$newref.$expld_car.dol_print_date($object->date_debut,
'%y%m%d');
166 $sqlbis =
'UPDATE '.MAIN_DB_PREFIX.
'expensereport SET ref_number_int = '.((int) $ref_number_int).
' WHERE rowid = '.((int) $object->id);
167 $resqlbis = $db->query($sqlbis);
173 dol_syslog(
"mod_expensereport_jade::getNextValue return ".$newref);
178 $posindice = strlen($this->prefix) + 6;
179 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
180 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport";
181 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefix).
"____-%'";
182 $sql .=
" AND entity = ".$conf->entity;
184 $resql = $db->query($sql);
186 $obj = $db->fetch_object($resql);
188 $max = intval($obj->max);
193 dol_syslog(
"mod_expensereport_jade::getNextValue", LOG_DEBUG);
197 $date = $object->date_valid;
199 $this->error =
'Date valid not defined';
203 $yymm = strftime(
"%y%m", $date);
205 if ($max >= (pow(10, 4) - 1)) {
208 $num = sprintf(
"%04s", $max + 1);
211 dol_syslog(
"mod_expensereport_jade::getNextValue return ".$this->prefix.$yymm.
"-".$num);
212 return $this->prefix.$yymm.
"-".$num;