dolibarr 20.0.0
product_lot.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021 Christophe Battarel <christophe.battarel@altairis.fr>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25// Load Dolibarr environment
26require '../../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
30
31// Load translation files required by the page
32$langs->loadLangs(array("admin", "products", "productbatch"));
33
34// Security check
35if (!$user->admin || (empty($conf->productbatch->enabled))) {
37}
38
39$action = GETPOST('action', 'alpha');
40$value = GETPOST('value', 'alpha');
41$label = GETPOST('label', 'alpha');
42$scandir = GETPOST('scan_dir', 'alpha');
43$type = 'product_batch';
44
45$error = 0;
46
47
48/*
49 * Actions
50 */
51
52include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
53
54if ($action == 'updateMaskLot') {
55 $maskconstbatch = GETPOST('maskconstLot', 'aZ09');
56 $maskbatch = GETPOST('maskLot', 'alpha');
57
58 if ($maskconstbatch && preg_match('/_MASK$/', $maskconstbatch)) {
59 $res = dolibarr_set_const($db, $maskconstbatch, $maskbatch, 'chaine', 0, '', $conf->entity);
60 if ($res <= 0) {
61 $error++;
62 }
63 }
64
65 if (!$error) {
66 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
67 } else {
68 setEventMessages($langs->trans("Error"), null, 'errors');
69 }
70} elseif ($action == 'updateMaskSN') {
71 $maskconstbatch = GETPOST('maskconstSN', 'aZ09');
72 $maskbatch = GETPOST('maskSN', 'alpha');
73
74 if ($maskconstbatch && preg_match('/_MASK$/', $maskconstbatch)) {
75 $res = dolibarr_set_const($db, $maskconstbatch, $maskbatch, 'chaine', 0, '', $conf->entity);
76 if ($res <= 0) {
77 $error++;
78 }
79 }
80
81 if (!$error) {
82 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
83 } else {
84 setEventMessages($langs->trans("Error"), null, 'errors');
85 }
86} elseif ($action == 'setmodlot') {
87 dolibarr_set_const($db, "PRODUCTBATCH_LOT_ADDON", $value, 'chaine', 0, '', $conf->entity);
88} elseif ($action == 'setmodsn') {
89 dolibarr_set_const($db, "PRODUCTBATCH_SN_ADDON", $value, 'chaine', 0, '', $conf->entity);
90} elseif ($action == 'setmaskslot') {
91 dolibarr_set_const($db, "PRODUCTBATCH_LOT_USE_PRODUCT_MASKS", $value, 'bool', 0, '', $conf->entity);
92 if ($value == '1' && getDolGlobalString('PRODUCTBATCH_LOT_ADDONS') !== 'mod_lot_advanced') {
93 dolibarr_set_const($db, "PRODUCTBATCH_LOT_ADDON", 'mod_lot_advanced', 'chaine', 0, '', $conf->entity);
94 }
95} elseif ($action == 'setmaskssn') {
96 dolibarr_set_const($db, "PRODUCTBATCH_SN_USE_PRODUCT_MASKS", $value, 'bool', 0, '', $conf->entity);
97 if ($value == '1' && getDolGlobalString('PRODUCTBATCH_SN_ADDONS') !== 'mod_sn_advanced') {
98 dolibarr_set_const($db, "PRODUCTBATCH_SN_ADDON", 'mod_sn_advanced', 'chaine', 0, '', $conf->entity);
99 }
100} elseif ($action == 'set') {
101 // Activate a model
102 $ret = addDocumentModel($value, $type, $label, $scandir);
103} elseif ($action == 'del') {
104 $ret = delDocumentModel($value, $type);
105 if ($ret > 0) {
106 if ($conf->global->FACTURE_ADDON_PDF == "$value") {
107 dolibarr_del_const($db, 'FACTURE_ADDON_PDF', $conf->entity);
108 }
109 }
110} elseif ($action == 'specimen') {
111 $modele = GETPOST('module', 'alpha');
112
113 $product_batch = new Productlot($db);
114 $product_batch->initAsSpecimen();
115
116 // Search template files
117 $file = '';
118 $classname = '';
119 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
120 foreach ($dirmodels as $reldir) {
121 $file = dol_buildpath($reldir . "core/modules/product_batch/doc/pdf_" . $modele . ".modules.php", 0);
122 if (file_exists($file)) {
123 $classname = "pdf_" . $modele;
124 break;
125 }
126 }
127
128 if ($classname !== '') {
129 require_once $file;
130
131 $module = new $classname($db);
132
133 if ($module->write_file($product_batch, $langs) > 0) {
134 header("Location: " . DOL_URL_ROOT . "/document.php?modulepart=product_batch&file=SPECIMEN.pdf");
135 return;
136 } else {
137 setEventMessages($module->error, $module->errors, 'errors');
138 dol_syslog($module->error, LOG_ERR);
139 }
140 } else {
141 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
142 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
143 }
144} elseif ($action == 'setdoc') {
145 // Set default model
146 if (dolibarr_set_const($db, "PRODUCT_BATCH_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
147 // La constante qui a ete lue en avant du nouveau set
148 // on passe donc par une variable pour avoir un affichage coherent
149 $conf->global->PRODUCT_BATCH_ADDON_PDF = $value;
150 }
151
152 // On active le modele
153 $ret = delDocumentModel($value, $type);
154 if ($ret > 0) {
155 $ret = addDocumentModel($value, $type, $label, $scandir);
156 }
157}
158
159/*
160 * View
161 */
162
163$form = new Form($db);
164
165$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
166
167llxHeader("", $langs->trans("ProductLotSetup"), '', '', 0, 0, '', '', '', 'mod-product page-admin_product_lot');
168
169$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
170print load_fiche_titre($langs->trans("ProductLotSetup"), $linkback, 'title_setup');
171
173
174print dol_get_fiche_head($head, 'settings', $langs->trans("Batch"), -1, 'lot');
175
176
177if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
178 // The feature to define the numbering module of lot or serial is no enabled because it is not used anywhere in Dolibarr code: You can set it
179 // but the numbering module is not used.
180 // TODO Use it on lot creation page, when you create a lot and when the lot number is kept empty to define the lot according
181 // to the selected product.
182 print $langs->trans("NothingToSetup");
183} else {
184 /*
185 * Lot Numbering models
186 */
187
188 print load_fiche_titre($langs->trans("BatchLotNumberingModules"), '', '');
189
190 print '<table class="noborder centpercent">';
191 print '<tr class="liste_titre">';
192 print '<td>'.$langs->trans("Name").'</td>';
193 print '<td>'.$langs->trans("Description").'</td>';
194 print '<td class="nowrap">'.$langs->trans("Example").'</td>';
195 print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
196 print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
197 print '</tr>'."\n";
198
199 clearstatcache();
200
201 foreach ($dirmodels as $reldir) {
202 $dir = dol_buildpath($reldir."core/modules/product_batch/");
203
204 if (is_dir($dir)) {
205 $handle = opendir($dir);
206 if (is_resource($handle)) {
207 while (($file = readdir($handle)) !== false) {
208 if (substr($file, 0, 8) == 'mod_lot_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
209 $file = substr($file, 0, dol_strlen($file) - 4);
210
211 require_once $dir.$file.'.php';
212
213 $module = new $file($db);
214
215 // Show modules according to features level
216 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
217 continue;
218 }
219 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
220 continue;
221 }
222
223 if ($module->isEnabled()) {
224 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
225 print $module->info($langs);
226 print '</td>';
227
228 // Show example of numbering model
229 print '<td class="nowrap">';
230 $tmp = $module->getExample();
231 if (preg_match('/^Error/', $tmp)) {
232 print '<div class="error">'.$langs->trans($tmp).'</div>';
233 } elseif ($tmp == 'NotConfigured') {
234 print $langs->trans($tmp);
235 } else {
236 print $tmp;
237 }
238 print '</td>'."\n";
239
240 print '<td class="center">';
241 if ($conf->global->PRODUCTBATCH_LOT_ADDON == $file) {
242 print img_picto($langs->trans("Activated"), 'switch_on');
243 } else {
244 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodlot&token='.newToken().'&value='.urlencode($file).'">';
245 print img_picto($langs->trans("Disabled"), 'switch_off');
246 print '</a>';
247 }
248 print '</td>';
249
250 $batch = new Productlot($db);
251 $batch->initAsSpecimen();
252
253 // Info
254 $htmltooltip = '';
255 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
256 $nextval = $module->getNextValue($mysoc, $batch);
257 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
258 $htmltooltip .= ''.$langs->trans("NextValue").': ';
259 if ($nextval) {
260 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
261 $nextval = $langs->trans($nextval);
262 }
263 $htmltooltip .= $nextval.'<br>';
264 } else {
265 $htmltooltip .= $langs->trans($module->error).'<br>';
266 }
267 }
268
269 print '<td class="center">';
270 print $form->textwithpicto('', $htmltooltip, 1, 0);
271 print '</td>';
272
273 print "</tr>\n";
274 }
275 }
276 }
277 closedir($handle);
278 }
279 }
280 }
281
282 print "</table><br>\n";
283
284
285 /*
286 * Serials Numbering models
287 */
288
289 print load_fiche_titre($langs->trans("BatchSerialNumberingModules"), '', '');
290
291 print '<table class="noborder centpercent">';
292 print '<tr class="liste_titre">';
293 print '<td>'.$langs->trans("Name").'</td>';
294 print '<td>'.$langs->trans("Description").'</td>';
295 print '<td class="nowrap">'.$langs->trans("Example").'</td>';
296 print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
297 print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
298 print '</tr>'."\n";
299
300 clearstatcache();
301
302 foreach ($dirmodels as $reldir) {
303 $dir = dol_buildpath($reldir."core/modules/product_batch/");
304
305 if (is_dir($dir)) {
306 $handle = opendir($dir);
307 if (is_resource($handle)) {
308 while (($file = readdir($handle)) !== false) {
309 if (substr($file, 0, 7) == 'mod_sn_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
310 $file = substr($file, 0, dol_strlen($file) - 4);
311
312 require_once $dir.$file.'.php';
313
314 $module = new $file($db);
315
316 // Show modules according to features level
317 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
318 continue;
319 }
320 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
321 continue;
322 }
323
324 if ($module->isEnabled()) {
325 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
326 print $module->info($langs);
327 print '</td>';
328
329 // Show example of numbering model
330 print '<td class="nowrap">';
331 $tmp = $module->getExample();
332 if (preg_match('/^Error/', $tmp)) {
333 print '<div class="error">'.$langs->trans($tmp).'</div>';
334 } elseif ($tmp == 'NotConfigured') {
335 print $langs->trans($tmp);
336 } else {
337 print $tmp;
338 }
339 print '</td>'."\n";
340
341 print '<td class="center">';
342 if ($conf->global->PRODUCTBATCH_SN_ADDON == $file) {
343 print img_picto($langs->trans("Activated"), 'switch_on');
344 } else {
345 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodsn&token='.newToken().'&value='.urlencode($file).'">';
346 print img_picto($langs->trans("Disabled"), 'switch_off');
347 print '</a>';
348 }
349 print '</td>';
350
351 $batch = new Productlot($db);
352 $batch->initAsSpecimen();
353
354 // Info
355 $htmltooltip = '';
356 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
357 $nextval = $module->getNextValue($mysoc, $batch);
358 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
359 $htmltooltip .= ''.$langs->trans("NextValue").': ';
360 if ($nextval) {
361 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
362 $nextval = $langs->trans($nextval);
363 }
364 $htmltooltip .= $nextval.'<br>';
365 } else {
366 $htmltooltip .= $langs->trans($module->error).'<br>';
367 }
368 }
369
370 print '<td class="center">';
371 print $form->textwithpicto('', $htmltooltip, 1, 0);
372 print '</td>';
373
374 print "</tr>\n";
375 }
376 }
377 }
378 closedir($handle);
379 }
380 }
381 }
382
383 print "</table><br>\n";
384}
385
386// Module to build doc
387$def = array();
388$sql = "SELECT nom";
389$sql .= " FROM " . MAIN_DB_PREFIX . "document_model";
390$sql .= " WHERE type = '" . $db->escape($type) . "'";
391$sql .= " AND entity = " . $conf->entity;
392$resql = $db->query($sql);
393if ($resql) {
394 $i = 0;
395 $num_rows = $db->num_rows($resql);
396 while ($i < $num_rows) {
397 $array = $db->fetch_array($resql);
398 if (is_array($array)) {
399 array_push($def, $array[0]);
400 }
401 $i++;
402 }
403} else {
404 dol_print_error($db);
405}
406
407print '<br>';
408
409print load_fiche_titre($langs->trans("ProductBatchDocumentTemplates"), '', '');
410
411print '<div class="div-table-responsive-no-min">';
412print '<table class="noborder centpercent">';
413print '<tr class="liste_titre">';
414print '<td>' . $langs->trans("Name") . '</td>';
415print '<td>' . $langs->trans("Description") . '</td>';
416print '<td class="center" width="60">' . $langs->trans("Status") . "</td>\n";
417print '<td class="center" width="60">' . $langs->trans("Default") . "</td>\n";
418print '<td class="center"></td>';
419print '<td class="center" width="80">' . $langs->trans("Preview") . '</td>';
420print "</tr>\n";
421
422clearstatcache();
423
424$filelist = array();
425foreach ($dirmodels as $reldir) {
426 foreach (array('', '/doc') as $valdir) {
427 $dir = dol_buildpath($reldir . "core/modules/product_batch" . $valdir);
428 if (is_dir($dir)) {
429 $handle = opendir($dir);
430 if (is_resource($handle)) {
431 while (($file = readdir($handle)) !== false) {
432 $filelist[] = $file;
433 }
434 closedir($handle);
435 arsort($filelist);
436
437 foreach ($filelist as $file) {
438 if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
439 if (file_exists($dir . '/' . $file)) {
440 $name = substr($file, 4, dol_strlen($file) - 16);
441 $classname = substr($file, 0, dol_strlen($file) - 12);
442
443 require_once $dir . '/' . $file;
444 $module = new $classname($db);
445
446 $modulequalified = 1;
447 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
448 $modulequalified = 0;
449 }
450 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
451 $modulequalified = 0;
452 }
453
454 if ($modulequalified) {
455 print '<tr class="oddeven"><td width="100">';
456 print(empty($module->name) ? $name : $module->name);
457 print "</td><td>\n";
458 if (method_exists($module, 'info')) {
459 print $module->info($langs);
460 } else {
461 print $module->description;
462 }
463 print '</td>';
464
465 // Active
466 if (in_array($name, $def)) {
467 print '<td class="center">' . "\n";
468 print '<a href="' . $_SERVER["PHP_SELF"] . '?action=del&token=' . newToken() . '&value=' . urlencode($name) . '">';
469 print img_picto($langs->trans("Enabled"), 'switch_on');
470 print '</a>';
471 print '</td>';
472 } else {
473 print '<td class="center">' . "\n";
474 print '<a href="' . $_SERVER["PHP_SELF"] . '?action=set&token=' . newToken() . '&value=' . urlencode($name) . '&scan_dir=' . urlencode($module->scandir) . '&label=' . urlencode($module->name) . '">' . img_picto($langs->trans("Disabled"), 'switch_off') . '</a>';
475 print "</td>";
476 }
477
478 // Default
479 print '<td class="center">';
480 if (getDolGlobalString('PRODUCT_BATCH_ADDON_PDF') == $name) {
481 print img_picto($langs->trans("Default"), 'on');
482 } else {
483 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>';
484 }
485 print '</td>';
486
487 // Info
488 $htmltooltip = '' . $langs->trans("Name") . ': ' . $module->name;
489 $htmltooltip .= '<br>' . $langs->trans("Type") . ': ' . ($module->type ? $module->type : $langs->trans("Unknown"));
490 if ($module->type == 'pdf') {
491 $htmltooltip .= '<br>' . $langs->trans("Width") . '/' . $langs->trans("Height") . ': ' . $module->page_largeur . '/' . $module->page_hauteur;
492 }
493 $htmltooltip .= '<br><br><u>' . $langs->trans("FeaturesSupported") . ':</u>';
494 $htmltooltip .= '<br>' . $langs->trans("Logo") . ': ' . yn($module->option_logo, 1, 1);
495 $htmltooltip .= '<br>' . $langs->trans("MultiLanguage") . ': ' . yn($module->option_multilang, 1, 1);
496
497
498 print '<td class="center">';
499 print $form->textwithpicto('', $htmltooltip, 1, 0);
500 print '</td>';
501
502 // Preview
503 print '<td class="center">';
504 if ($module->type == 'pdf') {
505 print '<a href="' . $_SERVER["PHP_SELF"] . '?action=specimen&module=' . $name . '">' . img_object($langs->trans("Preview"), 'contract') . '</a>';
506 } else {
507 print img_object($langs->trans("PreviewNotAvailable"), 'generic');
508 }
509 print '</td>';
510
511 print "</tr>\n";
512 }
513 }
514 }
515 }
516 }
517 }
518 }
519}
520
521print '</table>';
522print '</div>';
523
524// End of page
525llxFooter();
526$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.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage generation of HTML components Only common components must be here.
Class with list of lots and properties.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
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)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
product_lot_admin_prepare_head()
Return array head with list of tabs to view object information.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:142
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.