101 global $conf, $langs, $db;
103 $langs->load(
"bills");
109 $posindice = strlen($this->prefixinvoice) + 6;
110 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
111 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
112 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixinvoice).
"____-%'";
113 $sql .=
" AND entity = ".$conf->entity;
114 $resql = $db->query($sql);
116 $row = $db->fetch_row($resql);
118 $siyymm = substr($row[0], 0, 6);
122 if ($siyymm && !preg_match(
'/'.$this->prefixinvoice.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
123 $langs->load(
"errors");
124 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
131 $posindice = strlen($this->prefixcreditnote) + 6;
132 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
133 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
134 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixcreditnote).
"____-%'";
135 $sql .=
" AND entity = ".$conf->entity;
137 $resql = $db->query($sql);
139 $row = $db->fetch_row($resql);
141 $siyymm = substr($row[0], 0, 6);
145 if ($siyymm && !preg_match(
'/'.$this->prefixcreditnote.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
146 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
153 $posindice = strlen($this->prefixdeposit) + 6;
154 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
155 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
156 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixdeposit).
"____-%'";
157 $sql .=
" AND entity = ".$conf->entity;
159 $resql = $db->query($sql);
161 $row = $db->fetch_row($resql);
163 $siyymm = substr($row[0], 0, 6);
167 if ($siyymm && !preg_match(
'/'.$this->prefixdeposit.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
168 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
187 $prefix = $this->prefixinvoice;
189 $prefix = $this->prefixcreditnote;
190 } elseif (
$object->type == 3) {
191 $prefix = $this->prefixdeposit;
195 $posindice = strlen($prefix) + 6;
196 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
197 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
198 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-%'";
199 $sql .=
" AND entity = ".$conf->entity;
201 $resql = $db->query($sql);
202 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
204 $obj = $db->fetch_object($resql);
206 $max = intval($obj->max);
214 if ($mode ==
'last') {
215 if ($max >= (pow(10, 4) - 1)) {
218 $num = sprintf(
"%04d", $max);
222 $sql =
"SELECT ref as ref";
223 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
224 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-".$num.
"'";
225 $sql .=
" AND entity = ".$conf->entity;
227 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
228 $resql = $db->query($sql);
230 $obj = $db->fetch_object($resql);
239 } elseif ($mode ==
'next') {
243 if ($max >= (pow(10, 4) - 1)) {
246 $num = sprintf(
"%04d", $max + 1);
249 dol_syslog(get_class($this).
"::getNextValue return ".$prefix.$yymm.
"-".$num);
250 return $prefix.$yymm.
"-".$num;