26require_once DOL_DOCUMENT_ROOT.
'/core/modules/facture/modules_facture.php';
37 public $version =
'dolibarr';
39 public $prefixinvoice =
'FA';
41 public $prefixreplacement =
'FR';
43 public $prefixdeposit =
'AC';
45 public $prefixcreditnote =
'AV';
60 if ((
float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code !=
'FR') {
61 $this->prefixinvoice =
'IN';
62 $this->prefixreplacement =
'IR';
63 $this->prefixdeposit =
'ID';
64 $this->prefixcreditnote =
'IC';
67 if (!empty($conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX)) {
68 $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX;
80 $langs->load(
"bills");
81 return $langs->trans(
'MarsNumRefModelDesc1', $this->prefixinvoice, $this->prefixreplacement, $this->prefixdeposit, $this->prefixcreditnote);
91 return $this->prefixinvoice.
"0501-0001";
102 global $langs, $conf, $db;
104 $langs->load(
"bills");
110 $posindice = strlen($this->prefixinvoice) + 6;
111 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED) as max";
112 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
113 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixinvoice).
"____-%'";
114 $sql .=
" AND entity = ".$conf->entity;
116 $resql = $db->query($sql);
118 $row = $db->fetch_row($resql);
120 $fayymm = substr($row[0], 0, 6);
124 if ($fayymm && !preg_match(
'/'.$this->prefixinvoice.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
125 $langs->load(
"errors");
126 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
133 $posindice = strlen($this->prefixcreditnote) + 6;
134 $sql =
"SELECT MAX(SUBSTRING(ref FROM ".$posindice.
")) as max";
135 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
136 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixcreditnote).
"____-%'";
137 $sql .=
" AND entity = ".$conf->entity;
139 $resql = $db->query($sql);
141 $row = $db->fetch_row($resql);
143 $fayymm = substr($row[0], 0, 6);
147 if ($fayymm && !preg_match(
'/'.$this->prefixcreditnote.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
148 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
167 $prefix = $this->prefixinvoice;
168 if ($invoice->type == 1) {
169 $prefix = $this->prefixreplacement;
170 } elseif ($invoice->type == 2) {
171 $prefix = $this->prefixcreditnote;
172 } elseif ($invoice->type == 3) {
173 $prefix = $this->prefixdeposit;
177 $posindice = strlen($prefix) + 6;
178 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
179 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
180 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-%'";
181 $sql .=
" AND entity IN (".getEntity(
'invoicenumber', 1, $invoice).
")";
183 $resql = $db->query($sql);
184 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
186 $obj = $db->fetch_object($resql);
188 $max = intval($obj->max);
196 if ($mode ==
'last') {
197 if ($max >= (pow(10, 4) - 1)) {
200 $num = sprintf(
"%04s", $max);
204 $sql =
"SELECT ref as ref";
205 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
206 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-".$num.
"'";
207 $sql .=
" AND entity IN (".getEntity(
'invoicenumber', 1, $invoice).
")";
208 $sql .=
" ORDER BY ref DESC";
210 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
211 $resql = $db->query($sql);
213 $obj = $db->fetch_object($resql);
222 } elseif ($mode ==
'next') {
223 $date = $invoice->date;
224 $yymm = strftime(
"%y%m", $date);
226 if ($max >= (pow(10, 4) - 1)) {
229 $num = sprintf(
"%04s", $max + 1);
232 dol_syslog(get_class($this).
"::getNextValue return ".$prefix.$yymm.
"-".$num);
233 return $prefix.$yymm.
"-".$num;
248 public function getNumRef($objsoc, $objforref, $mode =
'next')
Parent class of invoice reference numbering templates.
Class to manage invoice numbering rules Mars.
getExample()
Return an example of numbering.
__construct()
Constructor.
getNextValue($objsoc, $invoice, $mode='next')
Return next value not used or last value used.
canBeActivated()
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
getNumRef($objsoc, $objforref, $mode='next')
Return next free value.
info()
Returns the description of the numbering model.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.