dolibarr 25.0.0-alpha
mod_inventory_standard.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Jose MARTINEZ <jose.martinez@pichinov.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
23require_once DOL_DOCUMENT_ROOT.'/core/modules/inventory/modules_inventory.php';
24
25
30{
35 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
36
40 public $prefix = 'IV';
41
45 public $error = '';
46
50 public $name = 'standard';
51
55 public $position = 40;
56
57
64 public function info($langs)
65 {
66 global $langs;
67 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
68 }
69
70
76 public function getExample()
77 {
78 return $this->prefix."0501-0001";
79 }
80
81
89 public function canBeActivated($object)
90 {
91 global $conf, $langs, $db;
92
93 $coyymm = '';
94 $max = '';
95
96 $posindice = strlen($this->prefix) + 6;
97 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".((int) $posindice).") AS SIGNED)) as max";
98 $sql .= " FROM ".MAIN_DB_PREFIX."inventory";
99 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
100 $sql .= " AND entity = ".((int) $conf->entity);
101
102 $resql = $db->query($sql);
103 if ($resql) {
104 $row = $db->fetch_row($resql);
105 if ($row) {
106 $coyymm = substr($row[0], 0, 6);
107 $max = $row[0];
108 }
109 }
110 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
111 $langs->load("errors");
112 $this->error = $langs->trans('ErrorNumRefModel', $max);
113 return false;
114 }
115
116 return true;
117 }
118
125 public function getNextValue($object)
126 {
127 global $db, $conf;
128
129 // first we get the max value
130 $posindice = strlen($this->prefix) + 6;
131 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".((int) $posindice).") AS SIGNED)) as max";
132 $sql .= " FROM ".MAIN_DB_PREFIX."inventory";
133 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
134 $sql .= " AND entity = ".((int) $conf->entity);
135
136 $resql = $db->query($sql);
137 if ($resql) {
138 $obj = $db->fetch_object($resql);
139 if ($obj) {
140 $max = intval($obj->max);
141 } else {
142 $max = 0;
143 }
144 } else {
145 dol_syslog("mod_inventory_standard::getNextValue", LOG_DEBUG);
146 return -1;
147 }
148
149 $date = !empty($object->date_inventory) ? $object->date_inventory : (!empty($object->date_creation) ? $object->date_creation : dol_now());
150 $yymm = dol_print_date($date, "%y%m");
151
152 if ($max >= (pow(10, 4) - 1)) {
153 $num = $max + 1;
154 } else { // If counter > 9999, we do not format on 4 chars, we take number as it is
155 $num = sprintf("%04d", $max + 1);
156 }
157
158 dol_syslog("mod_inventory_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
159 return $this->prefix.$yymm."-".$num;
160 }
161}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class to manage numbering of Inventory.
Class to manage the Standard numbering rule for Inventory.
info($langs)
Return description of numbering module.
getNextValue($object)
Return next free value.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
getExample()
Return an example of numbering.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.