97 global $conf, $langs, $db;
99 $langs->load(
"bills");
105 $posindice = strlen($this->prefixinvoice) + 6;
106 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
107 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
108 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixinvoice).
"____-%'";
109 $sql .=
" AND entity = ".$conf->entity;
110 $resql = $db->query($sql);
112 $row = $db->fetch_row($resql);
114 $siyymm = substr($row[0], 0, 6);
118 if ($siyymm && !preg_match(
'/'.$this->prefixinvoice.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
119 $langs->load(
"errors");
120 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
127 $posindice = strlen($this->prefixcreditnote) + 6;
128 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
129 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
130 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixcreditnote).
"____-%'";
131 $sql .=
" AND entity = ".$conf->entity;
133 $resql = $db->query($sql);
135 $row = $db->fetch_row($resql);
137 $siyymm = substr($row[0], 0, 6);
141 if ($siyymm && !preg_match(
'/'.$this->prefixcreditnote.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
142 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
149 $posindice = strlen($this->prefixdeposit) + 6;
150 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
151 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
152 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixdeposit).
"____-%'";
153 $sql .=
" AND entity = ".$conf->entity;
155 $resql = $db->query($sql);
157 $row = $db->fetch_row($resql);
159 $siyymm = substr($row[0], 0, 6);
163 if ($siyymm && !preg_match(
'/'.$this->prefixdeposit.
'[0-9][0-9][0-9][0-9]/i', $siyymm)) {
164 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
183 $prefix = $this->prefixinvoice;
184 if ($object->type == 2) {
185 $prefix = $this->prefixcreditnote;
186 } elseif ($object->type == 3) {
187 $prefix = $this->prefixdeposit;
191 $posindice = strlen($prefix) + 6;
192 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
193 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
194 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-%'";
195 $sql .=
" AND entity = ".$conf->entity;
197 $resql = $db->query($sql);
198 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
200 $obj = $db->fetch_object($resql);
202 $max = intval($obj->max);
210 if ($mode ==
'last') {
211 if ($max >= (pow(10, 4) - 1)) {
214 $num = sprintf(
"%04s", $max);
218 $sql =
"SELECT ref as ref";
219 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn";
220 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-".$num.
"'";
221 $sql .=
" AND entity = ".$conf->entity;
223 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
224 $resql = $db->query($sql);
226 $obj = $db->fetch_object($resql);
235 } elseif ($mode ==
'next') {
236 $date = $object->date;
237 $yymm = strftime(
"%y%m", $date);
239 if ($max >= (pow(10, 4) - 1)) {
242 $num = sprintf(
"%04s", $max + 1);
245 dol_syslog(get_class($this).
"::getNextValue return ".$prefix.$yymm.
"-".$num);
246 return $prefix.$yymm.
"-".$num;