90 global $conf, $langs, $db;
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;
101 $resql = $db->query($sql);
103 $row = $db->fetch_row($resql);
105 $coyymm = substr($row[0], 0, 6);
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);
131 if ($object->fk_user_author > 0) {
132 $fuser =
new User($db);
133 $fuser->fetch($object->fk_user_author);
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;
139 $sql =
'SELECT MAX(de.ref_number_int) as max';
140 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expensereport de';
142 $result = $db->query($sql);
144 if ($db->num_rows($result) > 0) {
145 $objp = $db->fetch_object($result);
146 $newref = $objp->max;
148 while (strlen($newref) < $num_car) {
149 $newref =
"0".$newref;
153 while (strlen($newref) < $num_car) {
154 $newref =
"0".$newref;
158 $ref_number_int = ($newref + 1) - 1;
164 $prefix = $conf->global->EXPENSE_REPORT_PREFIX;
166 $newref = str_replace(
' ',
'_', $user_author_infos).$expld_car.$prefix.$newref.$expld_car.dol_print_date($object->date_debut,
'%y%m%d');
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);
175 dol_syslog(
"mod_expensereport_jade::getNextValue return ".$newref);
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;
186 $resql = $db->query($sql);
188 $obj = $db->fetch_object($resql);
190 $max = intval($obj->max);
195 dol_syslog(
"mod_expensereport_jade::getNextValue", LOG_DEBUG);
199 $date = $object->date_valid;
201 $this->error =
'Date valid not defined';
207 if ($max >= (pow(10, 4) - 1)) {
210 $num = sprintf(
"%04s", $max + 1);
213 dol_syslog(
"mod_expensereport_jade::getNextValue return ".$this->prefix.$yymm.
"-".$num);
214 return $this->prefix.$yymm.
"-".$num;