dolibarr 25.0.0-alpha
inventory.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Jose Martinez <jose.martinez@pichinov.com>
3 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2026 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/stock.lib.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
38require_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php';
39
40// Translations
41$langs->loadLangs(array("admin", "stocks"));
42
43// Access control
44if (!$user->admin) {
46}
47
48// Parameters
49$action = GETPOST('action', 'aZ09');
50$value = GETPOST('value', 'alpha');
51$label = GETPOST('label', 'alpha');
52$scandir = GETPOST('scan_dir', 'alpha');
53
54$error = 0;
55
56
57/*
58 * Actions
59 */
60
61if ($action == 'updateMask') {
62 $maskconstinventory = GETPOST('maskconstinventory', 'aZ09');
63 $maskinventory = GETPOST('maskInventory', 'alpha');
64
65 if ($maskconstinventory && preg_match('/_MASK$/', $maskconstinventory)) {
66 $res = dolibarr_set_const($db, $maskconstinventory, $maskinventory, 'chaine', 0, '', $conf->entity);
67 if ($res <= 0) {
68 $error++;
69 }
70 }
71
72 if (!$error) {
73 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
74 } else {
75 setEventMessages($langs->trans("Error"), null, 'errors');
76 }
77} elseif ($action == 'specimen') {
78 $modele = GETPOST('module', 'alpha');
79
80 $tmpobject = new Inventory($db);
81 $tmpobject->initAsSpecimen();
82
83 // Search template files
84 $file = '';
85 $classname = '';
86 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
87 foreach ($dirmodels as $reldir) {
88 $file = dol_buildpath($reldir."core/modules/inventory/doc/pdf_".$modele.".modules.php", 0);
89 if (file_exists($file)) {
90 $classname = "pdf_".$modele;
91 break;
92 }
93 }
94
95 if ($classname !== '') {
96 require_once $file;
97
98 $module = new $classname($db);
99 '@phan-var-force ModelePDFInventory $module';
102 if ($module->write_file($tmpobject, $langs) > 0) {
103 header("Location: ".DOL_URL_ROOT."/document.php?modulepart=inventory&file=SPECIMEN.pdf");
104 return;
105 } else {
106 setEventMessages($module->error, null, 'errors');
107 dol_syslog($module->error, LOG_ERR);
108 }
109 } else {
110 $langs->load('errors');
111 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
112 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
113 }
114} elseif ($action == 'set') { // Activate a document model
115 $ret = addDocumentModel($value, 'inventory', $label, $scandir);
116} elseif ($action == 'del') {
117 $ret = delDocumentModel($value, 'inventory');
118 if ($ret > 0) {
119 if (getDolGlobalString('INVENTORY_ADDON_PDF') == "$value") {
120 dolibarr_del_const($db, 'INVENTORY_ADDON_PDF', $conf->entity);
121 }
122 }
123} elseif ($action == 'setdoc') { // Set default document model
124 if (dolibarr_set_const($db, 'INVENTORY_ADDON_PDF', $value, 'chaine', 0, '', $conf->entity)) {
125 // The constant that was read before the new set
126 // We therefore requires a variable to have a coherent view
127 $conf->global->INVENTORY_ADDON_PDF = $value;
128 }
129
130 // Activate the model
131 $ret = delDocumentModel($value, 'inventory');
132 if ($ret > 0) {
133 $ret = addDocumentModel($value, 'inventory', $label, $scandir);
134 }
135} elseif ($action == 'setmod') {
136 // The default value ('') means: free reference typed by the user
137 dolibarr_set_const($db, 'INVENTORY_ADDON', $value, 'chaine', 0, '', $conf->entity);
138} elseif ($action == 'setstartmode') {
139 $startmode = GETPOST('INVENTORY_DEFAULT_START_MODE', 'aZ09');
140 if (!in_array($startmode, array(Inventory::START_MODE_CURRENT, Inventory::START_MODE_NONE, Inventory::START_MODE_ZERO))) {
142 }
143 if (dolibarr_set_const($db, 'INVENTORY_DEFAULT_START_MODE', $startmode, 'chaine', 0, '', $conf->entity) > 0) {
144 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
145 } else {
146 setEventMessages($langs->trans("Error"), null, 'errors');
147 }
148}
149
150
151/*
152 * View
153 */
154
155$form = new Form($db);
156
157$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
158
159$page_name = "StockSetup";
160llxHeader('', $langs->trans($page_name), '', '', 0, 0, '', '', '', 'mod-admin page-inventory');
161
162// Subheader
163$linkback = '<a href="'.dolBuildUrl(DOL_URL_ROOT.'/admin/modules.php', ['restore_lastsearch_values' => 1]).'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
164
165print load_fiche_titre($langs->trans($page_name), $linkback, 'title_setup');
166
167// Configuration header
169print dol_get_fiche_head($head, 'inventory', $langs->trans("Inventory"), -1, 'stock');
170
171// Options
172print load_fiche_titre($langs->trans("Options"), '', '');
173
174print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
175print '<input type="hidden" name="token" value="'.newToken().'">';
176print '<input type="hidden" name="action" value="setstartmode">';
177print '<div class="div-table-responsive-no-min">';
178print '<table class="noborder centpercent">';
179print '<tr class="liste_titre">';
180print '<td>'.$langs->trans("Parameter").'</td>';
181print '<td class="right">'.$langs->trans("Value").'</td>';
182print '</tr>';
183print '<tr class="oddeven">';
184print '<td>'.$form->textwithpicto($langs->trans("InventoryStartMode"), $langs->trans("InventoryStartModeHelp")).'</td>';
185print '<td class="right">';
186$startmodes = array(
187 Inventory::START_MODE_CURRENT => $langs->trans("InventoryStartModeCurrent"),
188 Inventory::START_MODE_NONE => $langs->trans("InventoryStartModeNone"),
189 Inventory::START_MODE_ZERO => $langs->trans("InventoryStartModeZero"),
190);
191$currentstartmode = getDolGlobalString('INVENTORY_DEFAULT_START_MODE', Inventory::START_MODE_CURRENT);
192if (!array_key_exists($currentstartmode, $startmodes)) {
193 $currentstartmode = Inventory::START_MODE_CURRENT;
194}
195print $form->selectarray('INVENTORY_DEFAULT_START_MODE', $startmodes, $currentstartmode, 0, 0, 0, '', 0, 0, 0, '', 'minwidth200');
196print ' <input type="submit" class="button small" value="'.$langs->trans("Modify").'">';
197print '</td>';
198print '</tr>';
199print '</table>';
200print '</div>';
201print '</form>';
202print '<br>';
203
204// Numbering models
205print load_fiche_titre($langs->trans("NumberingModules", $langs->transnoentities("Inventory")), '', '');
206
207print '<div class="div-table-responsive-no-min">';
208print '<table class="noborder centpercent">';
209print '<tr class="liste_titre">';
210print '<td>'.$langs->trans("Name").'</td>';
211print '<td>'.$langs->trans("Description").'</td>';
212print '<td class="nowrap">'.$langs->trans("Example").'</td>';
213print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
214print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
215print '</tr>'."\n";
216
217clearstatcache();
218
219foreach ($dirmodels as $reldir) {
220 $dir = dol_buildpath($reldir."core/modules/inventory");
221
222 if (is_dir($dir)) {
223 $handle = opendir($dir);
224 if (is_resource($handle)) {
225 while (($file = readdir($handle)) !== false) {
226 if (strpos($file, 'mod_inventory_') === 0 && dol_substr($file, dol_strlen($file) - 3, 3) == 'php') {
227 $file = dol_substr($file, 0, dol_strlen($file) - 4);
228
229 require_once $dir.'/'.$file.'.php';
230
231 $module = new $file($db);
232
233 '@phan-var-force ModeleNumRefInventory $module';
234
235 // Show modules according to features level
236 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
237 continue;
238 }
239 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
240 continue;
241 }
242
243 if ($module->isEnabled()) {
244 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
245 print $module->info($langs);
246 print '</td>';
247
248 // Show example of numbering model
249 print '<td class="nowrap">';
250 $tmp = $module->getExample();
251 if (preg_match('/^Error/', $tmp)) {
252 $langs->load('errors');
253 print '<div class="error">'.$langs->trans($tmp).'</div>';
254 } elseif ($tmp == 'NotConfigured') {
255 print $langs->trans($tmp);
256 } else {
257 print $tmp;
258 }
259 print '</td>'."\n";
260
261 print '<td class="center">';
262 if (getDolGlobalString('INVENTORY_ADDON') == $file) {
263 print img_picto($langs->trans("Activated"), 'switch_on');
264 } else {
265 print '<a href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
266 print img_picto($langs->trans("Disabled"), 'switch_off');
267 print '</a>';
268 }
269 print '</td>';
270
271 $mytmpinstance = new Inventory($db);
272 $mytmpinstance->initAsSpecimen();
273
274 // Info
275 $htmltooltip = '';
276 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
277
278 $nextval = $module->getNextValue($mytmpinstance);
279 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
280 $htmltooltip .= ''.$langs->trans("NextValue").': ';
281 if ($nextval) {
282 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
283 $langs->load('errors');
284 $nextval = $langs->trans($nextval);
285 }
286 $htmltooltip .= $nextval.'<br>';
287 } else {
288 $htmltooltip .= $langs->trans("InventoryRefTypedManually").'<br>';
289 }
290 }
291
292 print '<td class="center">';
293 print $form->textwithpicto('', $htmltooltip, 1, 'info');
294 print '</td>';
295
296 print "</tr>\n";
297 }
298 }
299 }
300 closedir($handle);
301 }
302 }
303}
304print "</table></div><br>\n";
305
306// Document templates generators
307printDocumentModelList('inventory', 'inventory', 'INVENTORY_ADDON_PDF', $langs->trans("DocumentModules", $langs->transnoentities("Inventory")), array(
308 'Logo' => 'option_logo',
309 'MultiLanguage' => 'option_multilang',
310));
311
312// Page end
313print dol_get_fiche_end();
314
315llxFooter();
316$db->close();
addDocumentModel($name, $type, $label='', $description='')
Add document model used by doc generator.
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
delDocumentModel($name, $type)
Delete document model used by doc generator.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage generation of HTML components Only common components must be here.
Class for Inventory.
const START_MODE_CURRENT
How the lines of the inventory are initialized when it is started.
if(! $sortfield) if(! $sortorder) $module
Definition list.php:193
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
Definition html.lib.php:542
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:740
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
stock_admin_prepare_head()
Return array head with list of tabs to view object information.
Definition stock.lib.php:92