1<?php
2/* Copyright (C) 2021		Christophe Battarel  <christophe.battarel@altairis.fr>
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
18/**
19 *  \file	   htdocs/product/admin/product_lot.php
20 *  \ingroup	produit
21 *  \brief	  Setup page of product lot module
22 */
23
24require '../../main.inc.php';
25require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
26require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
28
29// Load translation files required by the page
30$langs->loadLangs(array("admin", "products", "productbatch"));
31
32// Security check
33if (!$user->admin || (empty($conf->productbatch->enabled)))
34	accessforbidden();
35
36$action = GETPOST('action', 'alpha');
37$value = GETPOST('value', 'alpha');
38
39/*
40 * Actions
41 */
42
43include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
44
45if ($action == 'updateMaskLot') {
46	$maskconstbatch = GETPOST('maskconstLot', 'alpha');
47	$maskbatch = GETPOST('maskLot', 'alpha');
48
49	if ($maskconstbatch) $res = dolibarr_set_const($db, $maskconstbatch, $maskbatch, 'chaine', 0, '', $conf->entity);
50
51	if (!$res > 0) $error++;
52
53	if (!$error) {
54		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
55	} else {
56		setEventMessages($langs->trans("Error"), null, 'errors');
57	}
58} elseif ($action == 'updateMaskSN') {
59	$maskconstbatch = GETPOST('maskconstSN', 'alpha');
60	$maskbatch = GETPOST('maskSN', 'alpha');
61
62	if ($maskconstbatch) $res = dolibarr_set_const($db, $maskconstbatch, $maskbatch, 'chaine', 0, '', $conf->entity);
63
64	if (!$res > 0) $error++;
65
66	if (!$error) {
67		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
68	} else {
69		setEventMessages($langs->trans("Error"), null, 'errors');
70	}
71} elseif ($action == 'setmodlot') {
72	dolibarr_set_const($db, "PRODUCTBATCH_LOT_ADDON", $value, 'chaine', 0, '', $conf->entity);
73} elseif ($action == 'setmodsn') {
74	dolibarr_set_const($db, "PRODUCTBATCH_SN_ADDON", $value, 'chaine', 0, '', $conf->entity);
75} elseif ($action == 'setmaskslot') {
76	dolibarr_set_const($db, "PRODUCTBATCH_LOT_USE_PRODUCT_MASKS", $value, 'bool', 0, '', $conf->entity);
77	if ($value == '1' && $conf->global->PRODUCTBATCH_LOT_ADDONS !== 'mod_lot_advanced') {
78		dolibarr_set_const($db, "PRODUCTBATCH_LOT_ADDON", 'mod_lot_advanced', 'chaine', 0, '', $conf->entity);
79	}
80} elseif ($action == 'setmaskssn') {
81	dolibarr_set_const($db, "PRODUCTBATCH_SN_USE_PRODUCT_MASKS", $value, 'bool', 0, '', $conf->entity);
82	if ($value == '1' && $conf->global->PRODUCTBATCH_SN_ADDONS !== 'mod_sn_advanced') {
83		dolibarr_set_const($db, "PRODUCTBATCH_SN_ADDON", 'mod_sn_advanced', 'chaine', 0, '', $conf->entity);
84	}
85}
86
87/*
88 * View
89 */
90
91$form = new Form($db);
92
93$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
94
95llxHeader("", $langs->trans("ProductLotSetup"));
96
97$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
98print load_fiche_titre($langs->trans("ProductLotSetup"), $linkback, 'title_setup');
99
100$head = product_lot_admin_prepare_head();
101
102print dol_get_fiche_head($head, 'settings', $langs->trans("Batch"), -1, 'lot');
103
104
105if ($conf->global->MAIN_FEATURES_LEVEL < 2) {
106	// The feature to define the numbering module of lot or serial is no enabled bcause it is not used anywhere in Dolibarr code: You can set it
107	// but the numbering module is not used.
108	// 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
109	// to the selected product.
110	print $langs->trans("NothingToSetup");
111} else {
112	/*
113	 * Lot Numbering models
114	 */
115
116	print load_fiche_titre($langs->trans("BatchLotNumberingModules"), '', '');
117
118	print '<table class="noborder centpercent">';
119	print '<tr class="liste_titre">';
120	print '<td>'.$langs->trans("Name").'</td>';
121	print '<td>'.$langs->trans("Description").'</td>';
122	print '<td class="nowrap">'.$langs->trans("Example").'</td>';
123	print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
124	print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
125	print '</tr>'."\n";
126
127	clearstatcache();
128
129	foreach ($dirmodels as $reldir) {
130		$dir = dol_buildpath($reldir."core/modules/product_batch/");
131
132		if (is_dir($dir)) {
133			$handle = opendir($dir);
134			if (is_resource($handle)) {
135				while (($file = readdir($handle)) !== false) {
136					if (substr($file, 0, 8) == 'mod_lot_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
137						$file = substr($file, 0, dol_strlen($file) - 4);
138
139						require_once $dir.$file.'.php';
140
141						$module = new $file($db);
142
143						// Show modules according to features level
144						if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
145						if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
146
147						if ($module->isEnabled()) {
148							print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
149							print $module->info();
150							print '</td>';
151
152							// Show example of numbering model
153							print '<td class="nowrap">';
154							$tmp = $module->getExample();
155							if (preg_match('/^Error/', $tmp)) print '<div class="error">'.$langs->trans($tmp).'</div>';
156							elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
157							else print $tmp;
158							print '</td>'."\n";
159
160							print '<td class="center">';
161							if ($conf->global->PRODUCTBATCH_LOT_ADDON == $file) {
162								print img_picto($langs->trans("Activated"), 'switch_on');
163							} else {
164								print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodlot&amp;value='.$file.'">';
165								print img_picto($langs->trans("Disabled"), 'switch_off');
166								print '</a>';
167							}
168							print '</td>';
169
170							$batch = new Productlot($db);
171							$batch->initAsSpecimen();
172
173							// Info
174							$htmltooltip = '';
175							$htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
176							$nextval = $module->getNextValue($mysoc, $batch);
177							if ("$nextval" != $langs->trans("NotAvailable")) {  // Keep " on nextval
178								$htmltooltip .= ''.$langs->trans("NextValue").': ';
179								if ($nextval) {
180									if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured')
181										$nextval = $langs->trans($nextval);
182									$htmltooltip .= $nextval.'<br>';
183								} else {
184									$htmltooltip .= $langs->trans($module->error).'<br>';
185								}
186							}
187
188							print '<td class="center">';
189							print $form->textwithpicto('', $htmltooltip, 1, 0);
190							print '</td>';
191
192							print "</tr>\n";
193						}
194					}
195				}
196				closedir($handle);
197			}
198		}
199	}
200
201	print "</table><br>\n";
202
203
204	/*
205	 * Serials Numbering models
206	 */
207
208	print load_fiche_titre($langs->trans("BatchSerialNumberingModules"), '', '');
209
210	print '<table class="noborder centpercent">';
211	print '<tr class="liste_titre">';
212	print '<td>'.$langs->trans("Name").'</td>';
213	print '<td>'.$langs->trans("Description").'</td>';
214	print '<td class="nowrap">'.$langs->trans("Example").'</td>';
215	print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
216	print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
217	print '</tr>'."\n";
218
219	clearstatcache();
220
221	foreach ($dirmodels as $reldir) {
222		$dir = dol_buildpath($reldir."core/modules/product_batch/");
223
224		if (is_dir($dir)) {
225			$handle = opendir($dir);
226			if (is_resource($handle)) {
227				while (($file = readdir($handle)) !== false) {
228					if (substr($file, 0, 7) == 'mod_sn_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
229						$file = substr($file, 0, dol_strlen($file) - 4);
230
231						require_once $dir.$file.'.php';
232
233						$module = new $file($db);
234
235						// Show modules according to features level
236						if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
237						if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
238
239						if ($module->isEnabled()) {
240							print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
241							print $module->info();
242							print '</td>';
243
244							// Show example of numbering model
245							print '<td class="nowrap">';
246							$tmp = $module->getExample();
247							if (preg_match('/^Error/', $tmp)) print '<div class="error">'.$langs->trans($tmp).'</div>';
248							elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
249							else print $tmp;
250							print '</td>'."\n";
251
252							print '<td class="center">';
253							if ($conf->global->PRODUCTBATCH_SN_ADDON == $file) {
254								print img_picto($langs->trans("Activated"), 'switch_on');
255							} else {
256								print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodsn&amp;value='.$file.'">';
257								print img_picto($langs->trans("Disabled"), 'switch_off');
258								print '</a>';
259							}
260							print '</td>';
261
262							$batch = new Productlot($db);
263							$batch->initAsSpecimen();
264
265							// Info
266							$htmltooltip = '';
267							$htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
268							$nextval = $module->getNextValue($mysoc, $batch);
269							if ("$nextval" != $langs->trans("NotAvailable")) {  // Keep " on nextval
270								$htmltooltip .= ''.$langs->trans("NextValue").': ';
271								if ($nextval) {
272									if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured')
273										$nextval = $langs->trans($nextval);
274									$htmltooltip .= $nextval.'<br>';
275								} else {
276									$htmltooltip .= $langs->trans($module->error).'<br>';
277								}
278							}
279
280							print '<td class="center">';
281							print $form->textwithpicto('', $htmltooltip, 1, 0);
282							print '</td>';
283
284							print "</tr>\n";
285						}
286					}
287				}
288				closedir($handle);
289			}
290		}
291	}
292
293	print "</table><br>\n";
294}
295
296// End of page
297llxFooter();
298$db->close();
299