99 global $conf, $langs, $db;
101 $langs->load(
"bills");
107 $posindice = strlen($this->prefixinvoice) + 6;
108 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
109 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
110 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixinvoice).
"____-%'";
111 $sql .=
" AND entity = ".$conf->entity;
112 $resql = $db->query($sql);
114 $row = $db->fetch_row($resql);
116 $siyymm = substr($row[0], 0, 6);
120 if ($siyymm && !preg_match(
'/'.$this->prefixinvoice.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
121 $langs->load(
"errors");
122 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
129 $posindice = strlen($this->prefixcreditnote) + 6;
130 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
131 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
132 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixcreditnote).
"____-%'";
133 $sql .=
" AND entity = ".$conf->entity;
135 $resql = $db->query($sql);
137 $row = $db->fetch_row($resql);
139 $siyymm = substr($row[0], 0, 6);
143 if ($siyymm && !preg_match(
'/'.$this->prefixcreditnote.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
144 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
151 $posindice = strlen($this->prefixdeposit) + 6;
152 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
153 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
154 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixdeposit).
"____-%'";
155 $sql .=
" AND entity = ".$conf->entity;
157 $resql = $db->query($sql);
159 $row = $db->fetch_row($resql);
161 $siyymm = substr($row[0], 0, 6);
165 if ($siyymm && !preg_match(
'/'.$this->prefixdeposit.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
166 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
185 $prefix = $this->prefixinvoice;
186 if ($object->type == 2) {
187 $prefix = $this->prefixcreditnote;
188 } elseif ($object->type == 3) {
189 $prefix = $this->prefixdeposit;
193 $posindice = strlen($prefix) + 6;
194 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
195 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
196 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-%'";
197 $sql .=
" AND entity = ".$conf->entity;
199 $resql = $db->query($sql);
200 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
202 $obj = $db->fetch_object($resql);
204 $max = intval($obj->max);
212 if ($mode ==
'last') {
213 if ($max >= (pow(10, 4) - 1)) {
216 $num = sprintf(
"%04s", $max);
220 $sql =
"SELECT ref as ref";
221 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
222 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-".$num.
"'";
223 $sql .=
" AND entity = ".$conf->entity;
225 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
226 $resql = $db->query($sql);
228 $obj = $db->fetch_object($resql);
237 } elseif ($mode ==
'next') {
238 $date = $object->date;
241 if ($max >= (pow(10, 4) - 1)) {
244 $num = sprintf(
"%04s", $max + 1);
247 dol_syslog(get_class($this).
"::getNextValue return ".$prefix.$yymm.
"-".$num);
248 return $prefix.$yymm.
"-".$num;