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", "errors", "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 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
111 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
112 }
113} elseif ($action == 'set') { // Activate a document model
114 $ret = addDocumentModel($value, 'inventory', $label, $scandir);
115} elseif ($action == 'del') {
116 $ret = delDocumentModel($value, 'inventory');
117 if ($ret > 0) {
118 if (getDolGlobalString('INVENTORY_ADDON_PDF') == "$value") {
119 dolibarr_del_const($db, 'INVENTORY_ADDON_PDF', $conf->entity);
120 }
121 }
122} elseif ($action == 'setdoc') { // Set default document model
123 if (dolibarr_set_const($db, 'INVENTORY_ADDON_PDF', $value, 'chaine', 0, '', $conf->entity)) {
124 // The constant that was read before the new set
125 // We therefore requires a variable to have a coherent view
126 $conf->global->INVENTORY_ADDON_PDF = $value;
127 }
128
129 // Activate the model
130 $ret = delDocumentModel($value, 'inventory');
131 if ($ret > 0) {
132 $ret = addDocumentModel($value, 'inventory', $label, $scandir);
133 }
134} elseif ($action == 'setmod') {
135 // The default value ('') means: free reference typed by the user
136 dolibarr_set_const($db, 'INVENTORY_ADDON', $value, 'chaine', 0, '', $conf->entity);
137} elseif ($action == 'setstartmode') {
138 $startmode = GETPOST('INVENTORY_DEFAULT_START_MODE', 'aZ09');
139 if (!in_array($startmode, array(Inventory::START_MODE_CURRENT, Inventory::START_MODE_NONE, Inventory::START_MODE_ZERO))) {
141 }
142 if (dolibarr_set_const($db, 'INVENTORY_DEFAULT_START_MODE', $startmode, 'chaine', 0, '', $conf->entity) > 0) {
143 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
144 } else {
145 setEventMessages($langs->trans("Error"), null, 'errors');
146 }
147}
148
149
150/*
151 * View
152 */
153
154$form = new Form($db);
155
156$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
157
158$page_name = "StockSetup";
159llxHeader('', $langs->trans($page_name), '', '', 0, 0, '', '', '', 'mod-admin page-inventory');
160
161// Subheader
162$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>';
163
164print load_fiche_titre($langs->trans($page_name), $linkback, 'title_setup');
165
166// Configuration header
168print dol_get_fiche_head($head, 'inventory', $langs->trans("Inventory"), -1, 'stock');
169
170// Options
171print load_fiche_titre($langs->trans("Options"), '', '');
172
173print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
174print '<input type="hidden" name="token" value="'.newToken().'">';
175print '<input type="hidden" name="action" value="setstartmode">';
176print '<div class="div-table-responsive-no-min">';
177print '<table class="noborder centpercent">';
178print '<tr class="liste_titre">';
179print '<td>'.$langs->trans("Parameter").'</td>';
180print '<td class="right">'.$langs->trans("Value").'</td>';
181print '</tr>';
182print '<tr class="oddeven">';
183print '<td>'.$form->textwithpicto($langs->trans("InventoryStartMode"), $langs->trans("InventoryStartModeHelp")).'</td>';
184print '<td class="right">';
185$startmodes = array(
186 Inventory::START_MODE_CURRENT => $langs->trans("InventoryStartModeCurrent"),
187 Inventory::START_MODE_NONE => $langs->trans("InventoryStartModeNone"),
188 Inventory::START_MODE_ZERO => $langs->trans("InventoryStartModeZero"),
189);
190$currentstartmode = getDolGlobalString('INVENTORY_DEFAULT_START_MODE', Inventory::START_MODE_CURRENT);
191if (!array_key_exists($currentstartmode, $startmodes)) {
192 $currentstartmode = Inventory::START_MODE_CURRENT;
193}
194print $form->selectarray('INVENTORY_DEFAULT_START_MODE', $startmodes, $currentstartmode, 0, 0, 0, '', 0, 0, 0, '', 'minwidth200');
195print ' <input type="submit" class="button small" value="'.$langs->trans("Modify").'">';
196print '</td>';
197print '</tr>';
198print '</table>';
199print '</div>';
200print '</form>';
201print '<br>';
202
203// Numbering models
204print load_fiche_titre($langs->trans("NumberingModules", $langs->transnoentities("Inventory")), '', '');
205
206print '<div class="div-table-responsive-no-min">';
207print '<table class="noborder centpercent">';
208print '<tr class="liste_titre">';
209print '<td>'.$langs->trans("Name").'</td>';
210print '<td>'.$langs->trans("Description").'</td>';
211print '<td class="nowrap">'.$langs->trans("Example").'</td>';
212print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
213print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
214print '</tr>'."\n";
215
216clearstatcache();
217
218foreach ($dirmodels as $reldir) {
219 $dir = dol_buildpath($reldir."core/modules/inventory");
220
221 if (is_dir($dir)) {
222 $handle = opendir($dir);
223 if (is_resource($handle)) {
224 while (($file = readdir($handle)) !== false) {
225 if (strpos($file, 'mod_inventory_') === 0 && dol_substr($file, dol_strlen($file) - 3, 3) == 'php') {
226 $file = dol_substr($file, 0, dol_strlen($file) - 4);
227
228 require_once $dir.'/'.$file.'.php';
229
230 $module = new $file($db);
231
232 '@phan-var-force ModeleNumRefInventory $module';
233
234 // Show modules according to features level
235 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
236 continue;
237 }
238 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
239 continue;
240 }
241
242 if ($module->isEnabled()) {
243 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
244 print $module->info($langs);
245 print '</td>';
246
247 // Show example of numbering model
248 print '<td class="nowrap">';
249 $tmp = $module->getExample();
250 if (preg_match('/^Error/', $tmp)) {
251 print '<div class="error">'.$langs->trans($tmp).'</div>';
252 } elseif ($tmp == 'NotConfigured') {
253 print $langs->trans($tmp);
254 } else {
255 print $tmp;
256 }
257 print '</td>'."\n";
258
259 print '<td class="center">';
260 if (getDolGlobalString('INVENTORY_ADDON') == $file) {
261 print img_picto($langs->trans("Activated"), 'switch_on');
262 } else {
263 print '<a href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
264 print img_picto($langs->trans("Disabled"), 'switch_off');
265 print '</a>';
266 }
267 print '</td>';
268
269 $mytmpinstance = new Inventory($db);
270 $mytmpinstance->initAsSpecimen();
271
272 // Info
273 $htmltooltip = '';
274 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
275
276 $nextval = $module->getNextValue($mytmpinstance);
277 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
278 $htmltooltip .= ''.$langs->trans("NextValue").': ';
279 if ($nextval) {
280 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
281 $nextval = $langs->trans($nextval);
282 }
283 $htmltooltip .= $nextval.'<br>';
284 } else {
285 $htmltooltip .= $langs->trans("InventoryRefTypedManually").'<br>';
286 }
287 }
288
289 print '<td class="center">';
290 print $form->textwithpicto('', $htmltooltip, 1, 'info');
291 print '</td>';
292
293 print "</tr>\n";
294 }
295 }
296 }
297 closedir($handle);
298 }
299 }
300}
301print "</table></div><br>\n";
302
303// Document templates generators
304printDocumentModelList('inventory', 'inventory', 'INVENTORY_ADDON_PDF', $langs->trans("DocumentModules", $langs->transnoentities("Inventory")), array(
305 'Logo' => 'option_logo',
306 'MultiLanguage' => 'option_multilang',
307));
308
309// Page end
310print dol_get_fiche_end();
311
312llxFooter();
313$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
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
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_strlen($string, $stringencoding='UTF-8')
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:540
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:738
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