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';
68 $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX;
78 public function info($langs)
81 $langs->load(
"bills");
82 return $langs->trans(
'MarsNumRefModelDesc1', $this->prefixinvoice, $this->prefixreplacement, $this->prefixdeposit, $this->prefixcreditnote);
92 return $this->prefixinvoice.
"0501-0001";
104 global $langs, $conf, $db;
106 $langs->load(
"bills");
112 $posindice = strlen($this->prefixinvoice) + 6;
113 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED) as max";
114 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
115 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixinvoice).
"____-%'";
116 $sql .=
" AND entity = ".$conf->entity;
118 $resql = $db->query($sql);
120 $row = $db->fetch_row($resql);
122 $fayymm = substr($row[0], 0, 6);
126 if ($fayymm && !preg_match(
'/'.$this->prefixinvoice.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
127 $langs->load(
"errors");
128 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
135 $posindice = strlen($this->prefixcreditnote) + 6;
136 $sql =
"SELECT MAX(SUBSTRING(ref FROM ".$posindice.
")) as max";
137 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
138 $sql .=
" WHERE ref LIKE '".$db->escape($this->prefixcreditnote).
"____-%'";
139 $sql .=
" AND entity = ".$conf->entity;
141 $resql = $db->query($sql);
143 $row = $db->fetch_row($resql);
145 $fayymm = substr($row[0], 0, 6);
149 if ($fayymm && !preg_match(
'/'.$this->prefixcreditnote.
'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
150 $this->error = $langs->trans(
'ErrorNumRefModel', $max);
169 $prefix = $this->prefixinvoice;
170 if ($invoice->type == 1) {
171 $prefix = $this->prefixreplacement;
172 } elseif ($invoice->type == 2) {
173 $prefix = $this->prefixcreditnote;
174 } elseif ($invoice->type == 3) {
175 $prefix = $this->prefixdeposit;
179 $posindice = strlen($prefix) + 6;
180 $sql =
"SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.
") AS SIGNED)) as max";
181 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
182 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-%'";
183 $sql .=
" AND entity IN (".getEntity(
'invoicenumber', 1, $invoice).
")";
185 $resql = $db->query($sql);
186 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
188 $obj = $db->fetch_object($resql);
190 $max = intval($obj->max);
198 if ($mode ==
'last') {
199 if ($max >= (pow(10, 4) - 1)) {
202 $num = sprintf(
"%04s", $max);
206 $sql =
"SELECT ref as ref";
207 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
208 $sql .=
" WHERE ref LIKE '".$db->escape($prefix).
"____-".$num.
"'";
209 $sql .=
" AND entity IN (".getEntity(
'invoicenumber', 1, $invoice).
")";
210 $sql .=
" ORDER BY ref DESC";
212 dol_syslog(get_class($this).
"::getNextValue", LOG_DEBUG);
213 $resql = $db->query($sql);
215 $obj = $db->fetch_object($resql);
224 } elseif ($mode ==
'next') {
225 $date = $invoice->date;
228 if ($max >= (pow(10, 4) - 1)) {
231 $num = sprintf(
"%04s", $max + 1);
234 dol_syslog(get_class($this).
"::getNextValue return ".$prefix.$yymm.
"-".$num);
235 return $prefix.$yymm.
"-".$num;
250 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.
info($langs)
Returns the description of the numbering model.
canBeActivated($object)
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.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.