120 global $langs, $conf, $db;
122 $langs->load(
"bills");
128 $posindice = strlen($this->prefixinvoice) + 6;
129 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
130 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
131 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixinvoice).
"____-%'";
132 $sql .=
" AND entity = ".$conf->entity;
134 $resql = $db->query($sql);
136 $row = $db->fetch_row($resql);
138 $fayymm = substr($row[0], 0, 6);
142 if ($fayymm && !preg_match(
'/'.$this->prefixinvoice.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
143 $langs->load(
"errors");
144 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
151 $posindice = strlen($this->prefixcreditnote) + 6;
152 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
153 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
154 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixcreditnote).
"____-%'";
155 $sql .=
" AND entity = ".$conf->entity;
157 $resql = $db->query($sql);
159 $row = $db->fetch_row($resql);
161 $fayymm = substr($row[0], 0, 6);
165 if ($fayymm && !preg_match(
'/'.$this->prefixcreditnote.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
166 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
173 $posindice = strlen($this->prefixdeposit) + 6;
174 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
175 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
176 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixdeposit).
"____-%'";
177 $sql .=
" AND entity = ".$conf->entity;
179 $resql = $db->query($sql);
181 $row = $db->fetch_row($resql);
183 $fayymm = substr($row[0], 0, 6);
187 if ($fayymm && !preg_match(
'/'.$this->prefixdeposit.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
188 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
210 dol_syslog(get_class($this).
"::getNextValue mode=".$mode, LOG_DEBUG);
212 $prefix = $this->prefixinvoice;
213 if ($invoice->type == 2) {
214 $prefix = $this->prefixcreditnote;
215 } elseif ($invoice->type == 3) {
216 $prefix = $this->prefixdeposit;
220 $posindice = strlen($prefix) + 6;
221 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
222 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
223 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-%'";
224 $sql .=
" AND entity IN (".getEntity(
'invoicenumber', 1, $invoice).
")";
226 $resql = $db->query($sql);
228 $obj = $db->fetch_object($resql);
230 $max = intval($obj->max);
238 if ($mode ==
'last') {
239 if ($max >= (pow(10, 4) - 1)) {
242 $num = sprintf(
"%04s", $max);
246 $sql =
"SELECT ref as ref";
247 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
248 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-".$num.
"'";
249 $sql .=
" AND entity IN (".getEntity(
'invoicenumber', 1, $invoice).
")";
250 $sql .=
" ORDER BY ref DESC";
252 $resql = $db->query($sql);
254 $obj = $db->fetch_object($resql);
263 } elseif ($mode ==
'next') {
264 $date = $invoice->date;
265 $yymm = strftime(
"%y%m", $date);
267 if ($max >= (pow(10, 4) - 1)) {
270 $num = sprintf(
"%04s", $max + 1);
273 dol_syslog(get_class($this).
"::getNextValue return ".$prefix.$yymm.
"-".$num);
274 return $prefix.$yymm.
"-".$num;