76 if ((
float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code !=
'FR') {
77 $this->prefixinvoice =
'IN';
78 $this->prefixreplacement =
'IR';
79 $this->prefixdeposit =
'ID';
80 $this->prefixcreditnote =
'IC';
83 if (!empty($conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX)) {
84 $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX;
118 global $langs, $conf, $db;
120 $langs->load(
"bills");
126 $posindice = strlen($this->prefixinvoice) + 6;
127 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
128 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
129 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixinvoice).
"____-%'";
130 $sql .=
" AND entity = ".$conf->entity;
132 $resql = $db->query($sql);
134 $row = $db->fetch_row($resql);
136 $fayymm = substr($row[0], 0, 6);
140 if ($fayymm && !preg_match(
'/'.$this->prefixinvoice.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
141 $langs->load(
"errors");
142 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
149 $posindice = strlen($this->prefixcreditnote) + 6;
150 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
151 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
152 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixcreditnote).
"____-%'";
153 $sql .=
" AND entity = ".$conf->entity;
155 $resql = $db->query($sql);
157 $row = $db->fetch_row($resql);
159 $fayymm = substr($row[0], 0, 6);
163 if ($fayymm && !preg_match(
'/'.$this->prefixcreditnote.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
164 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
171 $posindice = strlen($this->prefixdeposit) + 6;
172 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
173 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
174 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixdeposit).
"____-%'";
175 $sql .=
" AND entity = ".$conf->entity;
177 $resql = $db->query($sql);
179 $row = $db->fetch_row($resql);
181 $fayymm = substr($row[0], 0, 6);
185 if ($fayymm && !preg_match(
'/'.$this->prefixdeposit.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
186 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
208 dol_syslog(get_class($this).
"::getNextValue mode=".$mode, LOG_DEBUG);
210 $prefix = $this->prefixinvoice;
211 if ($invoice->type == 2) {
212 $prefix = $this->prefixcreditnote;
213 } elseif ($invoice->type == 3) {
214 $prefix = $this->prefixdeposit;
218 $posindice = strlen($prefix) + 6;
219 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
220 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
221 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-%'";
222 $sql .=
" AND entity IN (".getEntity(
'invoicenumber', 1, $invoice).
")";
224 $resql = $db->query($sql);
226 $obj = $db->fetch_object($resql);
228 $max = intval($obj->max);
236 if ($mode ==
'last') {
237 if ($max >= (pow(10, 4) - 1)) {
240 $num = sprintf(
"%04s", $max);
244 $sql =
"SELECT ref as ref";
245 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
246 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-".$num.
"'";
247 $sql .=
" AND entity IN (".getEntity(
'invoicenumber', 1, $invoice).
")";
248 $sql .=
" ORDER BY ref DESC";
250 $resql = $db->query($sql);
252 $obj = $db->fetch_object($resql);
261 } elseif ($mode ==
'next') {
262 $date = $invoice->date;
263 $yymm = strftime(
"%y%m", $date);
265 if ($max >= (pow(10, 4) - 1)) {
268 $num = sprintf(
"%04s", $max + 1);
271 dol_syslog(get_class($this).
"::getNextValue return ".$prefix.$yymm.
"-".$num);
272 return $prefix.$yymm.
"-".$num;