dolibarr 21.0.0-alpha
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 '@phan-var-force ModelePDFProductBatch $module';
134
135 if ($module->write_file($product_batch, $langs) > 0) {
136 header("Location: " . DOL_URL_ROOT . "/document.php?modulepart=product_batch&file=SPECIMEN.pdf");
137 return;
138 } else {
139 setEventMessages($module->error, $module->errors, 'errors');
140 dol_syslog($module->error, LOG_ERR);
141 }
142 } else {
143 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
144 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
145 }
146} elseif ($action == 'setdoc') {
147 // Set default model
148 if (dolibarr_set_const($db, "PRODUCT_BATCH_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
149 // La constante qui a ete lue en avant du nouveau set
150 // on passe donc par une variable pour avoir un affichage coherent
151 $conf->global->PRODUCT_BATCH_ADDON_PDF = $value;
152 }
153
154 // On active le modele
155 $ret = delDocumentModel($value, $type);
156 if ($ret > 0) {
157 $ret = addDocumentModel($value, $type, $label, $scandir);
158 }
159}
160
161/*
162 * View
163 */
164
165$form = new Form($db);
166
167$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
168
169llxHeader("", $langs->trans("ProductLotSetup"), '', '', 0, 0, '', '', '', 'mod-product page-admin_product_lot');
170
171$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
172print load_fiche_titre($langs->trans("ProductLotSetup"), $linkback, 'title_setup');
173
175
176print dol_get_fiche_head($head, 'settings', $langs->trans("Batch"), -1, 'lot');
177
178
179if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
180 // 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
181 // but the numbering module is not used.
182 // 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
183 // to the selected product.
184 print $langs->trans("NothingToSetup");
185} else {
186 /*
187 * Lot Numbering models
188 */
189
190 print load_fiche_titre($langs->trans("BatchLotNumberingModules"), '', '');
191
192 print '<table class="noborder centpercent">';
193 print '<tr class="liste_titre">';
194 print '<td>'.$langs->trans("Name").'</td>';
195 print '<td>'.$langs->trans("Description").'</td>';
196 print '<td class="nowrap">'.$langs->trans("Example").'</td>';
197 print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
198 print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
199 print '</tr>'."\n";
200
201 clearstatcache();
202
203 foreach ($dirmodels as $reldir) {
204 $dir = dol_buildpath($reldir."core/modules/product_batch/");
205
206 if (is_dir($dir)) {
207 $handle = opendir($dir);
208 if (is_resource($handle)) {
209 while (($file = readdir($handle)) !== false) {
210 if (substr($file, 0, 8) == 'mod_lot_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
211 $file = substr($file, 0, dol_strlen($file) - 4);
212
213 require_once $dir.$file.'.php';
214
215 $module = new $file($db);
216 '@phan-var-force ModeleNumRefBatch $module';
217
218 // Show modules according to features level
219 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
220 continue;
221 }
222 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
223 continue;
224 }
225
226 if ($module->isEnabled()) {
227 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
228 print $module->info($langs);
229 print '</td>';
230
231 // Show example of numbering model
232 print '<td class="nowrap">';
233 $tmp = $module->getExample();
234 if (preg_match('/^Error/', $tmp)) {
235 print '<div class="error">'.$langs->trans($tmp).'</div>';
236 } elseif ($tmp == 'NotConfigured') {
237 print $langs->trans($tmp);
238 } else {
239 print $tmp;
240 }
241 print '</td>'."\n";
242
243 print '<td class="center">';
244 if ($conf->global->PRODUCTBATCH_LOT_ADDON == $file) {
245 print img_picto($langs->trans("Activated"), 'switch_on');
246 } else {
247 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodlot&token='.newToken().'&value='.urlencode($file).'">';
248 print img_picto($langs->trans("Disabled"), 'switch_off');
249 print '</a>';
250 }
251 print '</td>';
252
253 $batch = new Productlot($db);
254 $batch->initAsSpecimen();
255
256 // Info
257 $htmltooltip = '';
258 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
259 $nextval = $module->getNextValue($mysoc, $batch);
260 if ((string) $nextval != $langs->trans("NotAvailable")) { // Keep " on nextval
261 $htmltooltip .= ''.$langs->trans("NextValue").': ';
262 if ($nextval) {
263 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
264 $nextval = $langs->trans($nextval);
265 }
266 $htmltooltip .= $nextval.'<br>';
267 } else {
268 $htmltooltip .= $langs->trans($module->error).'<br>';
269 }
270 }
271
272 print '<td class="center">';
273 print $form->textwithpicto('', $htmltooltip, 1, 0);
274 print '</td>';
275
276 print "</tr>\n";
277 }
278 }
279 }
280 closedir($handle);
281 }
282 }
283 }
284
285 print "</table><br>\n";
286
287
288 /*
289 * Serials Numbering models
290 */
291
292 print load_fiche_titre($langs->trans("BatchSerialNumberingModules"), '', '');
293
294 print '<table class="noborder centpercent">';
295 print '<tr class="liste_titre">';
296 print '<td>'.$langs->trans("Name").'</td>';
297 print '<td>'.$langs->trans("Description").'</td>';
298 print '<td class="nowrap">'.$langs->trans("Example").'</td>';
299 print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
300 print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
301 print '</tr>'."\n";
302
303 clearstatcache();
304
305 foreach ($dirmodels as $reldir) {
306 $dir = dol_buildpath($reldir."core/modules/product_batch/");
307
308 if (is_dir($dir)) {
309 $handle = opendir($dir);
310 if (is_resource($handle)) {
311 while (($file = readdir($handle)) !== false) {
312 if (substr($file, 0, 7) == 'mod_sn_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
313 $file = substr($file, 0, dol_strlen($file) - 4);
314
315 require_once $dir.$file.'.php';
316
317 $module = new $file($db);
318 '@phan-var-force ModeleNumRefBatch $module';
319
320 // Show modules according to features level
321 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
322 continue;
323 }
324 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
325 continue;
326 }
327
328 if ($module->isEnabled()) {
329 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
330 print $module->info($langs);
331 print '</td>';
332
333 // Show example of numbering model
334 print '<td class="nowrap">';
335 $tmp = $module->getExample();
336 if (preg_match('/^Error/', $tmp)) {
337 print '<div class="error">'.$langs->trans($tmp).'</div>';
338 } elseif ($tmp == 'NotConfigured') {
339 print $langs->trans($tmp);
340 } else {
341 print $tmp;
342 }
343 print '</td>'."\n";
344
345 print '<td class="center">';
346 if ($conf->global->PRODUCTBATCH_SN_ADDON == $file) {
347 print img_picto($langs->trans("Activated"), 'switch_on');
348 } else {
349 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodsn&token='.newToken().'&value='.urlencode($file).'">';
350 print img_picto($langs->trans("Disabled"), 'switch_off');
351 print '</a>';
352 }
353 print '</td>';
354
355 $batch = new Productlot($db);
356 $batch->initAsSpecimen();
357
358 // Info
359 $htmltooltip = '';
360 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
361 $nextval = $module->getNextValue($mysoc, $batch);
362 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
363 $htmltooltip .= ''.$langs->trans("NextValue").': ';
364 if ($nextval) {
365 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
366 $nextval = $langs->trans($nextval);
367 }
368 $htmltooltip .= $nextval.'<br>';
369 } else {
370 $htmltooltip .= $langs->trans($module->error).'<br>';
371 }
372 }
373
374 print '<td class="center">';
375 print $form->textwithpicto('', $htmltooltip, 1, 0);
376 print '</td>';
377
378 print "</tr>\n";
379 }
380 }
381 }
382 closedir($handle);
383 }
384 }
385 }
386
387 print "</table><br>\n";
388}
389
390// Module to build doc
391$def = array();
392$sql = "SELECT nom";
393$sql .= " FROM " . MAIN_DB_PREFIX . "document_model";
394$sql .= " WHERE type = '" . $db->escape($type) . "'";
395$sql .= " AND entity = " . $conf->entity;
396$resql = $db->query($sql);
397if ($resql) {
398 $i = 0;
399 $num_rows = $db->num_rows($resql);
400 while ($i < $num_rows) {
401 $array = $db->fetch_array($resql);
402 if (is_array($array)) {
403 array_push($def, $array[0]);
404 }
405 $i++;
406 }
407} else {
408 dol_print_error($db);
409}
410
411print '<br>';
412
413print load_fiche_titre($langs->trans("ProductBatchDocumentTemplates"), '', '');
414
415print '<div class="div-table-responsive-no-min">';
416print '<table class="noborder centpercent">';
417print '<tr class="liste_titre">';
418print '<td>' . $langs->trans("Name") . '</td>';
419print '<td>' . $langs->trans("Description") . '</td>';
420print '<td class="center" width="60">' . $langs->trans("Status") . "</td>\n";
421print '<td class="center" width="60">' . $langs->trans("Default") . "</td>\n";
422print '<td class="center"></td>';
423print '<td class="center" width="80">' . $langs->trans("Preview") . '</td>';
424print "</tr>\n";
425
426clearstatcache();
427
428$filelist = array();
429foreach ($dirmodels as $reldir) {
430 foreach (array('', '/doc') as $valdir) {
431 $dir = dol_buildpath($reldir . "core/modules/product_batch" . $valdir);
432 if (is_dir($dir)) {
433 $handle = opendir($dir);
434 if (is_resource($handle)) {
435 while (($file = readdir($handle)) !== false) {
436 $filelist[] = $file;
437 }
438 closedir($handle);
439 arsort($filelist);
440
441 foreach ($filelist as $file) {
442 if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
443 if (file_exists($dir . '/' . $file)) {
444 $name = substr($file, 4, dol_strlen($file) - 16);
445 $classname = substr($file, 0, dol_strlen($file) - 12);
446
447 require_once $dir . '/' . $file;
448 $module = new $classname($db);
449
450 $modulequalified = 1;
451 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
452 $modulequalified = 0;
453 }
454 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
455 $modulequalified = 0;
456 }
457
458 if ($modulequalified) {
459 print '<tr class="oddeven"><td width="100">';
460 print(empty($module->name) ? $name : $module->name);
461 print "</td><td>\n";
462 if (method_exists($module, 'info')) {
463 print $module->info($langs);
464 } else {
465 print $module->description;
466 }
467 print '</td>';
468
469 // Active
470 if (in_array($name, $def)) {
471 print '<td class="center">' . "\n";
472 print '<a href="' . $_SERVER["PHP_SELF"] . '?action=del&token=' . newToken() . '&value=' . urlencode($name) . '">';
473 print img_picto($langs->trans("Enabled"), 'switch_on');
474 print '</a>';
475 print '</td>';
476 } else {
477 print '<td class="center">' . "\n";
478 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>';
479 print "</td>";
480 }
481
482 // Default
483 print '<td class="center">';
484 if (getDolGlobalString('PRODUCT_BATCH_ADDON_PDF') == $name) {
485 print img_picto($langs->trans("Default"), 'on');
486 } else {
487 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>';
488 }
489 print '</td>';
490
491 // Info
492 $htmltooltip = '' . $langs->trans("Name") . ': ' . $module->name;
493 $htmltooltip .= '<br>' . $langs->trans("Type") . ': ' . ($module->type ? $module->type : $langs->trans("Unknown"));
494 if ($module->type == 'pdf') {
495 $htmltooltip .= '<br>' . $langs->trans("Width") . '/' . $langs->trans("Height") . ': ' . $module->page_largeur . '/' . $module->page_hauteur;
496 }
497 $htmltooltip .= '<br><br><u>' . $langs->trans("FeaturesSupported") . ':</u>';
498 $htmltooltip .= '<br>' . $langs->trans("Logo") . ': ' . yn($module->option_logo, 1, 1);
499 $htmltooltip .= '<br>' . $langs->trans("MultiLanguage") . ': ' . yn($module->option_multilang, 1, 1);
500
501
502 print '<td class="center">';
503 print $form->textwithpicto('', $htmltooltip, 1, 0);
504 print '</td>';
505
506 // Preview
507 print '<td class="center">';
508 if ($module->type == 'pdf') {
509 print '<a href="' . $_SERVER["PHP_SELF"] . '?action=specimen&module=' . $name . '">' . img_object($langs->trans("Preview"), 'contract') . '</a>';
510 } else {
511 print img_object($langs->trans("PreviewNotAvailable"), 'generic');
512 }
513 print '</td>';
514
515 print "</tr>\n";
516 }
517 }
518 }
519 }
520 }
521 }
522 }
523}
524
525print '</table>';
526print '</div>';
527
528// End of page
529llxFooter();
530$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($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:70
Class to manage generation of HTML components Only common components must be here.
Class with list of lots and properties.
llxFooter()
Footer empty.
Definition document.php:107
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)
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)
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.
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 a 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:140
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.