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 *
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
24// Load Dolibarr environment
25require '../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/stock.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php';
37
38// Translations
39$langs->loadLangs(array("admin", "errors", "stocks"));
40
41// Access control
42if (!$user->admin) {
44}
45
46// Parameters
47$action = GETPOST('action', 'aZ09');
48$value = GETPOST('value', 'alpha');
49$label = GETPOST('label', 'alpha');
50$scandir = GETPOST('scan_dir', 'alpha');
51
52$error = 0;
53
54
55/*
56 * Actions
57 */
58
59if ($action == 'updateMask') {
60 $maskconstinventory = GETPOST('maskconstinventory', 'aZ09');
61 $maskinventory = GETPOST('maskInventory', 'alpha');
62
63 if ($maskconstinventory && preg_match('/_MASK$/', $maskconstinventory)) {
64 $res = dolibarr_set_const($db, $maskconstinventory, $maskinventory, 'chaine', 0, '', $conf->entity);
65 if ($res <= 0) {
66 $error++;
67 }
68 }
69
70 if (!$error) {
71 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
72 } else {
73 setEventMessages($langs->trans("Error"), null, 'errors');
74 }
75} elseif ($action == 'specimen') {
76 $modele = GETPOST('module', 'alpha');
77
78 $tmpobject = new Inventory($db);
79 $tmpobject->initAsSpecimen();
80
81 // Search template files
82 $file = '';
83 $classname = '';
84 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
85 foreach ($dirmodels as $reldir) {
86 $file = dol_buildpath($reldir."core/modules/inventory/doc/pdf_".$modele.".modules.php", 0);
87 if (file_exists($file)) {
88 $classname = "pdf_".$modele;
89 break;
90 }
91 }
92
93 if ($classname !== '') {
94 require_once $file;
95
96 $module = new $classname($db);
97 '@phan-var-force ModelePDFInventory $module';
100 if ($module->write_file($tmpobject, $langs) > 0) {
101 header("Location: ".DOL_URL_ROOT."/document.php?modulepart=inventory&file=SPECIMEN.pdf");
102 return;
103 } else {
104 setEventMessages($module->error, null, 'errors');
105 dol_syslog($module->error, LOG_ERR);
106 }
107 } else {
108 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
109 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
110 }
111} elseif ($action == 'set') { // Activate a document model
112 $ret = addDocumentModel($value, 'inventory', $label, $scandir);
113} elseif ($action == 'del') {
114 $ret = delDocumentModel($value, 'inventory');
115 if ($ret > 0) {
116 if (getDolGlobalString('INVENTORY_ADDON_PDF') == "$value") {
117 dolibarr_del_const($db, 'INVENTORY_ADDON_PDF', $conf->entity);
118 }
119 }
120} elseif ($action == 'setdoc') { // Set default document model
121 if (dolibarr_set_const($db, 'INVENTORY_ADDON_PDF', $value, 'chaine', 0, '', $conf->entity)) {
122 // The constant that was read before the new set
123 // We therefore requires a variable to have a coherent view
124 $conf->global->INVENTORY_ADDON_PDF = $value;
125 }
126
127 // Activate the model
128 $ret = delDocumentModel($value, 'inventory');
129 if ($ret > 0) {
130 $ret = addDocumentModel($value, 'inventory', $label, $scandir);
131 }
132} elseif ($action == 'setmod') {
133 // The default value ('') means: free reference typed by the user
134 dolibarr_set_const($db, 'INVENTORY_ADDON', $value, 'chaine', 0, '', $conf->entity);
135}
136
137
138/*
139 * View
140 */
141
142$form = new Form($db);
143
144$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
145
146$page_name = "StockSetup";
147llxHeader('', $langs->trans($page_name), '', '', 0, 0, '', '', '', 'mod-admin page-inventory');
148
149// Subheader
150$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>';
151
152print load_fiche_titre($langs->trans($page_name), $linkback, 'title_setup');
153
154// Configuration header
156print dol_get_fiche_head($head, 'inventory', $langs->trans("Inventory"), -1, 'stock');
157
158// Numbering models
159print load_fiche_titre($langs->trans("NumberingModules", $langs->transnoentities("Inventory")), '', '');
160
161print '<div class="div-table-responsive-no-min">';
162print '<table class="noborder centpercent">';
163print '<tr class="liste_titre">';
164print '<td>'.$langs->trans("Name").'</td>';
165print '<td>'.$langs->trans("Description").'</td>';
166print '<td class="nowrap">'.$langs->trans("Example").'</td>';
167print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
168print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
169print '</tr>'."\n";
170
171clearstatcache();
172
173foreach ($dirmodels as $reldir) {
174 $dir = dol_buildpath($reldir."core/modules/inventory");
175
176 if (is_dir($dir)) {
177 $handle = opendir($dir);
178 if (is_resource($handle)) {
179 while (($file = readdir($handle)) !== false) {
180 if (strpos($file, 'mod_inventory_') === 0 && substr($file, dol_strlen($file) - 3, 3) == 'php') {
181 $file = substr($file, 0, dol_strlen($file) - 4);
182
183 require_once $dir.'/'.$file.'.php';
184
185 $module = new $file($db);
186
187 '@phan-var-force ModeleNumRefInventory $module';
188
189 // Show modules according to features level
190 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
191 continue;
192 }
193 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
194 continue;
195 }
196
197 if ($module->isEnabled()) {
198 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
199 print $module->info($langs);
200 print '</td>';
201
202 // Show example of numbering model
203 print '<td class="nowrap">';
204 $tmp = $module->getExample();
205 if (preg_match('/^Error/', $tmp)) {
206 print '<div class="error">'.$langs->trans($tmp).'</div>';
207 } elseif ($tmp == 'NotConfigured') {
208 print $langs->trans($tmp);
209 } else {
210 print $tmp;
211 }
212 print '</td>'."\n";
213
214 print '<td class="center">';
215 if (getDolGlobalString('INVENTORY_ADDON') == $file) {
216 print img_picto($langs->trans("Activated"), 'switch_on');
217 } else {
218 print '<a href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
219 print img_picto($langs->trans("Disabled"), 'switch_off');
220 print '</a>';
221 }
222 print '</td>';
223
224 $mytmpinstance = new Inventory($db);
225 $mytmpinstance->initAsSpecimen();
226
227 // Info
228 $htmltooltip = '';
229 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
230
231 $nextval = $module->getNextValue($mytmpinstance);
232 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
233 $htmltooltip .= ''.$langs->trans("NextValue").': ';
234 if ($nextval) {
235 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
236 $nextval = $langs->trans($nextval);
237 }
238 $htmltooltip .= $nextval.'<br>';
239 } else {
240 $htmltooltip .= $langs->trans("InventoryRefTypedManually").'<br>';
241 }
242 }
243
244 print '<td class="center">';
245 print $form->textwithpicto('', $htmltooltip, 1, 'info');
246 print '</td>';
247
248 print "</tr>\n";
249 }
250 }
251 }
252 closedir($handle);
253 }
254 }
255}
256print "</table></div><br>\n";
257
258// Document templates generators
259print load_fiche_titre($langs->trans("DocumentModules", $langs->transnoentities("Inventory")), '', '');
260
261// Load array def with activated templates
262$def = array();
263$sql = "SELECT nom";
264$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
265$sql .= " WHERE type = 'inventory'";
266$sql .= " AND entity = ".((int) $conf->entity);
267$resql = $db->query($sql);
268if ($resql) {
269 $i = 0;
270 $num_rows = $db->num_rows($resql);
271 while ($i < $num_rows) {
272 $array = $db->fetch_array($resql);
273 if (is_array($array)) {
274 array_push($def, $array[0]);
275 }
276 $i++;
277 }
278} else {
280}
281
282print '<div class="div-table-responsive">';
283print '<table class="noborder centpercent">'."\n";
284print '<tr class="liste_titre">'."\n";
285print '<td>'.$langs->trans("Name").'</td>';
286print '<td>'.$langs->trans("Description").'</td>';
287print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
288print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
289print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
290print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
291print "</tr>\n";
292
293clearstatcache();
294
295foreach ($dirmodels as $reldir) {
296 foreach (array('', '/doc') as $valdir) {
297 $realpath = $reldir."core/modules/inventory".$valdir;
298 $dir = dol_buildpath($realpath);
299
300 if (is_dir($dir)) {
301 $handle = opendir($dir);
302 if (is_resource($handle)) {
303 $filelist = array();
304 while (($file = readdir($handle)) !== false) {
305 $filelist[] = $file;
306 }
307 closedir($handle);
308 arsort($filelist);
309
310 foreach ($filelist as $file) {
311 if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
312 if (file_exists($dir.'/'.$file)) {
313 $name = substr($file, 4, dol_strlen($file) - 16);
314 $classname = substr($file, 0, dol_strlen($file) - 12);
315
316 require_once $dir.'/'.$file;
317 $module = new $classname($db);
318
319 '@phan-var-force ModelePDFInventory $module';
320
321 $modulequalified = 1;
322 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
323 $modulequalified = 0;
324 }
325 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
326 $modulequalified = 0;
327 }
328
329 if ($modulequalified) {
330 print '<tr class="oddeven"><td>';
331 print(empty($module->name) ? $name : $module->name);
332 print "</td><td>\n";
333 if (method_exists($module, 'info')) {
334 print $module->info($langs); // @phan-suppress-current-line PhanUndeclaredMethod
335 } else {
336 print $module->description;
337 }
338 print '</td>';
339
340 // Active
341 if (in_array($name, $def)) {
342 print '<td class="center">'."\n";
343 print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&value='.urlencode($name).'&token='.newToken().'">';
344 print img_picto($langs->trans("Enabled"), 'switch_on');
345 print '</a>';
346 print '</td>';
347 } else {
348 print '<td class="center">'."\n";
349 print '<a href="'.$_SERVER["PHP_SELF"].'?action=set&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'&token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
350 print "</td>";
351 }
352
353 // Default
354 print '<td class="center">';
355 if (getDolGlobalString('INVENTORY_ADDON_PDF') == $name) {
356 print img_picto($langs->trans("Default"), 'on');
357 } else {
358 print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdoc&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
359 }
360 print '</td>';
361
362 // Info
363 $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
364 $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
365 if ($module->type == 'pdf') {
366 $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
367 }
368 $htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
369
370 $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
371 $htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
372 $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
373
374 print '<td class="center">';
375 print $form->textwithpicto('', $htmltooltip, 1, 'info');
376 print '</td>';
377
378 // Preview
379 print '<td class="center">';
380 if ($module->type == 'pdf') {
381 print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'&token='.newToken().'">'.img_object($langs->trans("Preview"), 'generic').'</a>';
382 } else {
383 print img_object($langs->transnoentitiesnoconv("PreviewNotAvailable"), 'generic');
384 }
385 print '</td>';
386
387 print "</tr>\n";
388 }
389 }
390 }
391 }
392 }
393 }
394 }
395}
396
397print '</table></div>';
398
399// Page end
400print dol_get_fiche_end();
401
402llxFooter();
403$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.
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_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_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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
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:519
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:717
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
yn($yesno, $format=1, $color=0)
Return yes or no in current language.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133
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