product_lot.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. * \file htdocs/product/admin/product_lot.php
  19. * \ingroup produit
  20. * \brief Setup page of product lot module
  21. */
  22. // Load Dolibarr environment
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  27. // Load translation files required by the page
  28. $langs->loadLangs(array("admin", "products", "productbatch"));
  29. // Security check
  30. if (!$user->admin || (empty($conf->productbatch->enabled)))
  31. accessforbidden();
  32. $action = GETPOST('action', 'alpha');
  33. $value = GETPOST('value', 'alpha');
  34. $label = GETPOST('label', 'alpha');
  35. $scandir = GETPOST('scan_dir', 'alpha');
  36. $type = 'product_batch';
  37. $error = 0;
  38. /*
  39. * Actions
  40. */
  41. include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
  42. if ($action == 'updateMaskLot') {
  43. $maskconstbatch = GETPOST('maskconstLot', 'aZ09');
  44. $maskbatch = GETPOST('maskLot', 'alpha');
  45. if ($maskconstbatch && preg_match('/_MASK$/', $maskconstbatch)) {
  46. $res = dolibarr_set_const($db, $maskconstbatch, $maskbatch, 'chaine', 0, '', $conf->entity);
  47. if ($res <= 0) $error++;
  48. }
  49. if (!$error) {
  50. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  51. } else {
  52. setEventMessages($langs->trans("Error"), null, 'errors');
  53. }
  54. } elseif ($action == 'updateMaskSN') {
  55. $maskconstbatch = GETPOST('maskconstSN', 'aZ09');
  56. $maskbatch = GETPOST('maskSN', 'alpha');
  57. if ($maskconstbatch && preg_match('/_MASK$/', $maskconstbatch)) {
  58. $res = dolibarr_set_const($db, $maskconstbatch, $maskbatch, 'chaine', 0, '', $conf->entity);
  59. if ($res <= 0) $error++;
  60. }
  61. if (!$error) {
  62. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  63. } else {
  64. setEventMessages($langs->trans("Error"), null, 'errors');
  65. }
  66. } elseif ($action == 'setmodlot') {
  67. dolibarr_set_const($db, "PRODUCTBATCH_LOT_ADDON", $value, 'chaine', 0, '', $conf->entity);
  68. } elseif ($action == 'setmodsn') {
  69. dolibarr_set_const($db, "PRODUCTBATCH_SN_ADDON", $value, 'chaine', 0, '', $conf->entity);
  70. } elseif ($action == 'setmaskslot') {
  71. dolibarr_set_const($db, "PRODUCTBATCH_LOT_USE_PRODUCT_MASKS", $value, 'bool', 0, '', $conf->entity);
  72. if ($value == '1' && $conf->global->PRODUCTBATCH_LOT_ADDONS !== 'mod_lot_advanced') {
  73. dolibarr_set_const($db, "PRODUCTBATCH_LOT_ADDON", 'mod_lot_advanced', 'chaine', 0, '', $conf->entity);
  74. }
  75. } elseif ($action == 'setmaskssn') {
  76. dolibarr_set_const($db, "PRODUCTBATCH_SN_USE_PRODUCT_MASKS", $value, 'bool', 0, '', $conf->entity);
  77. if ($value == '1' && $conf->global->PRODUCTBATCH_SN_ADDONS !== 'mod_sn_advanced') {
  78. dolibarr_set_const($db, "PRODUCTBATCH_SN_ADDON", 'mod_sn_advanced', 'chaine', 0, '', $conf->entity);
  79. }
  80. } elseif ($action == 'set') {
  81. // Activate a model
  82. $ret = addDocumentModel($value, $type, $label, $scandir);
  83. } elseif ($action == 'del') {
  84. $ret = delDocumentModel($value, $type);
  85. if ($ret > 0) {
  86. if ($conf->global->FACTURE_ADDON_PDF == "$value") {
  87. dolibarr_del_const($db, 'FACTURE_ADDON_PDF', $conf->entity);
  88. }
  89. }
  90. } elseif ($action == 'specimen') {
  91. $modele = GETPOST('module', 'alpha');
  92. $product_batch = new Productlot($db);
  93. $product_batch->initAsSpecimen();
  94. // Search template files
  95. $file = '';
  96. $classname = '';
  97. $filefound = 0;
  98. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  99. foreach ($dirmodels as $reldir) {
  100. $file = dol_buildpath($reldir . "core/modules/product_batch/doc/pdf_" . $modele . ".modules.php", 0);
  101. if (file_exists($file)) {
  102. $filefound = 1;
  103. $classname = "pdf_" . $modele;
  104. break;
  105. }
  106. }
  107. if ($filefound) {
  108. require_once $file;
  109. $module = new $classname($db);
  110. if ($module->write_file($product_batch, $langs) > 0) {
  111. header("Location: " . DOL_URL_ROOT . "/document.php?modulepart=product_batch&file=SPECIMEN.pdf");
  112. return;
  113. } else {
  114. setEventMessages($module->error, $module->errors, 'errors');
  115. dol_syslog($module->error, LOG_ERR);
  116. }
  117. } else {
  118. setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
  119. dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
  120. }
  121. } elseif ($action == 'setdoc') {
  122. // Set default model
  123. if (dolibarr_set_const($db, "PRODUCT_BATCH_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
  124. // La constante qui a ete lue en avant du nouveau set
  125. // on passe donc par une variable pour avoir un affichage coherent
  126. $conf->global->PRODUCT_BATCH_ADDON_PDF = $value;
  127. }
  128. // On active le modele
  129. $ret = delDocumentModel($value, $type);
  130. if ($ret > 0) {
  131. $ret = addDocumentModel($value, $type, $label, $scandir);
  132. }
  133. }
  134. /*
  135. * View
  136. */
  137. $form = new Form($db);
  138. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  139. llxHeader("", $langs->trans("ProductLotSetup"));
  140. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  141. print load_fiche_titre($langs->trans("ProductLotSetup"), $linkback, 'title_setup');
  142. $head = product_lot_admin_prepare_head();
  143. print dol_get_fiche_head($head, 'settings', $langs->trans("Batch"), -1, 'lot');
  144. if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
  145. // 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
  146. // but the numbering module is not used.
  147. // 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
  148. // to the selected product.
  149. print $langs->trans("NothingToSetup");
  150. } else {
  151. /*
  152. * Lot Numbering models
  153. */
  154. print load_fiche_titre($langs->trans("BatchLotNumberingModules"), '', '');
  155. print '<table class="noborder centpercent">';
  156. print '<tr class="liste_titre">';
  157. print '<td>'.$langs->trans("Name").'</td>';
  158. print '<td>'.$langs->trans("Description").'</td>';
  159. print '<td class="nowrap">'.$langs->trans("Example").'</td>';
  160. print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
  161. print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
  162. print '</tr>'."\n";
  163. clearstatcache();
  164. foreach ($dirmodels as $reldir) {
  165. $dir = dol_buildpath($reldir."core/modules/product_batch/");
  166. if (is_dir($dir)) {
  167. $handle = opendir($dir);
  168. if (is_resource($handle)) {
  169. while (($file = readdir($handle)) !== false) {
  170. if (substr($file, 0, 8) == 'mod_lot_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
  171. $file = substr($file, 0, dol_strlen($file) - 4);
  172. require_once $dir.$file.'.php';
  173. $module = new $file($db);
  174. // Show modules according to features level
  175. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
  176. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
  177. if ($module->isEnabled()) {
  178. print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
  179. print $module->info($langs);
  180. print '</td>';
  181. // Show example of numbering model
  182. print '<td class="nowrap">';
  183. $tmp = $module->getExample();
  184. if (preg_match('/^Error/', $tmp)) print '<div class="error">'.$langs->trans($tmp).'</div>';
  185. elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
  186. else print $tmp;
  187. print '</td>'."\n";
  188. print '<td class="center">';
  189. if ($conf->global->PRODUCTBATCH_LOT_ADDON == $file) {
  190. print img_picto($langs->trans("Activated"), 'switch_on');
  191. } else {
  192. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodlot&token='.newToken().'&value='.urlencode($file).'">';
  193. print img_picto($langs->trans("Disabled"), 'switch_off');
  194. print '</a>';
  195. }
  196. print '</td>';
  197. $batch = new Productlot($db);
  198. $batch->initAsSpecimen();
  199. // Info
  200. $htmltooltip = '';
  201. $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
  202. $nextval = $module->getNextValue($mysoc, $batch);
  203. if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
  204. $htmltooltip .= ''.$langs->trans("NextValue").': ';
  205. if ($nextval) {
  206. if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured')
  207. $nextval = $langs->trans($nextval);
  208. $htmltooltip .= $nextval.'<br>';
  209. } else {
  210. $htmltooltip .= $langs->trans($module->error).'<br>';
  211. }
  212. }
  213. print '<td class="center">';
  214. print $form->textwithpicto('', $htmltooltip, 1, 0);
  215. print '</td>';
  216. print "</tr>\n";
  217. }
  218. }
  219. }
  220. closedir($handle);
  221. }
  222. }
  223. }
  224. print "</table><br>\n";
  225. /*
  226. * Serials Numbering models
  227. */
  228. print load_fiche_titre($langs->trans("BatchSerialNumberingModules"), '', '');
  229. print '<table class="noborder centpercent">';
  230. print '<tr class="liste_titre">';
  231. print '<td>'.$langs->trans("Name").'</td>';
  232. print '<td>'.$langs->trans("Description").'</td>';
  233. print '<td class="nowrap">'.$langs->trans("Example").'</td>';
  234. print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
  235. print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
  236. print '</tr>'."\n";
  237. clearstatcache();
  238. foreach ($dirmodels as $reldir) {
  239. $dir = dol_buildpath($reldir."core/modules/product_batch/");
  240. if (is_dir($dir)) {
  241. $handle = opendir($dir);
  242. if (is_resource($handle)) {
  243. while (($file = readdir($handle)) !== false) {
  244. if (substr($file, 0, 7) == 'mod_sn_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
  245. $file = substr($file, 0, dol_strlen($file) - 4);
  246. require_once $dir.$file.'.php';
  247. $module = new $file($db);
  248. // Show modules according to features level
  249. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
  250. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
  251. if ($module->isEnabled()) {
  252. print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
  253. print $module->info($langs);
  254. print '</td>';
  255. // Show example of numbering model
  256. print '<td class="nowrap">';
  257. $tmp = $module->getExample();
  258. if (preg_match('/^Error/', $tmp)) print '<div class="error">'.$langs->trans($tmp).'</div>';
  259. elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
  260. else print $tmp;
  261. print '</td>'."\n";
  262. print '<td class="center">';
  263. if ($conf->global->PRODUCTBATCH_SN_ADDON == $file) {
  264. print img_picto($langs->trans("Activated"), 'switch_on');
  265. } else {
  266. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodsn&token='.newToken().'&value='.urlencode($file).'">';
  267. print img_picto($langs->trans("Disabled"), 'switch_off');
  268. print '</a>';
  269. }
  270. print '</td>';
  271. $batch = new Productlot($db);
  272. $batch->initAsSpecimen();
  273. // Info
  274. $htmltooltip = '';
  275. $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
  276. $nextval = $module->getNextValue($mysoc, $batch);
  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. $htmltooltip .= $nextval.'<br>';
  283. } else {
  284. $htmltooltip .= $langs->trans($module->error).'<br>';
  285. }
  286. }
  287. print '<td class="center">';
  288. print $form->textwithpicto('', $htmltooltip, 1, 0);
  289. print '</td>';
  290. print "</tr>\n";
  291. }
  292. }
  293. }
  294. closedir($handle);
  295. }
  296. }
  297. }
  298. print "</table><br>\n";
  299. }
  300. // Module to build doc
  301. $def = array();
  302. $sql = "SELECT nom";
  303. $sql .= " FROM " . MAIN_DB_PREFIX . "document_model";
  304. $sql .= " WHERE type = '" . $db->escape($type) . "'";
  305. $sql .= " AND entity = " . $conf->entity;
  306. $resql = $db->query($sql);
  307. if ($resql) {
  308. $i = 0;
  309. $num_rows = $db->num_rows($resql);
  310. while ($i < $num_rows) {
  311. $array = $db->fetch_array($resql);
  312. array_push($def, $array[0]);
  313. $i++;
  314. }
  315. } else {
  316. dol_print_error($db);
  317. }
  318. print '<br>';
  319. print load_fiche_titre($langs->trans("ProductBatchDocumentTemplates"), '', '');
  320. print '<div class="div-table-responsive-no-min">';
  321. print '<table class="noborder centpercent">';
  322. print '<tr class="liste_titre">';
  323. print '<td>' . $langs->trans("Name") . '</td>';
  324. print '<td>' . $langs->trans("Description") . '</td>';
  325. print '<td class="center" width="60">' . $langs->trans("Status") . "</td>\n";
  326. print '<td class="center" width="60">' . $langs->trans("Default") . "</td>\n";
  327. print '<td class="center"></td>';
  328. print '<td class="center" width="80">' . $langs->trans("Preview") . '</td>';
  329. print "</tr>\n";
  330. clearstatcache();
  331. foreach ($dirmodels as $reldir) {
  332. foreach (array('', '/doc') as $valdir) {
  333. $dir = dol_buildpath($reldir . "core/modules/product_batch" . $valdir);
  334. if (is_dir($dir)) {
  335. $handle = opendir($dir);
  336. if (is_resource($handle)) {
  337. while (($file = readdir($handle)) !== false) {
  338. $filelist[] = $file;
  339. }
  340. closedir($handle);
  341. arsort($filelist);
  342. foreach ($filelist as $file) {
  343. if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
  344. if (file_exists($dir . '/' . $file)) {
  345. $name = substr($file, 4, dol_strlen($file) - 16);
  346. $classname = substr($file, 0, dol_strlen($file) - 12);
  347. require_once $dir . '/' . $file;
  348. $module = new $classname($db);
  349. $modulequalified = 1;
  350. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  351. $modulequalified = 0;
  352. }
  353. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  354. $modulequalified = 0;
  355. }
  356. if ($modulequalified) {
  357. print '<tr class="oddeven"><td width="100">';
  358. print (empty($module->name) ? $name : $module->name);
  359. print "</td><td>\n";
  360. if (method_exists($module, 'info')) {
  361. print $module->info($langs);
  362. } else {
  363. print $module->description;
  364. }
  365. print '</td>';
  366. // Active
  367. if (in_array($name, $def)) {
  368. print '<td class="center">' . "\n";
  369. print '<a href="' . $_SERVER["PHP_SELF"] . '?action=del&token=' . newToken() . '&value=' . urlencode($name) . '">';
  370. print img_picto($langs->trans("Enabled"), 'switch_on');
  371. print '</a>';
  372. print '</td>';
  373. } else {
  374. print '<td class="center">' . "\n";
  375. 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>';
  376. print "</td>";
  377. }
  378. // Defaut
  379. print '<td class="center">';
  380. if (getDolGlobalString('PRODUCT_BATCH_ADDON_PDF') == $name) {
  381. print img_picto($langs->trans("Default"), 'on');
  382. } else {
  383. 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>';
  384. }
  385. print '</td>';
  386. // Info
  387. $htmltooltip = '' . $langs->trans("Name") . ': ' . $module->name;
  388. $htmltooltip .= '<br>' . $langs->trans("Type") . ': ' . ($module->type ? $module->type : $langs->trans("Unknown"));
  389. if ($module->type == 'pdf') {
  390. $htmltooltip .= '<br>' . $langs->trans("Width") . '/' . $langs->trans("Height") . ': ' . $module->page_largeur . '/' . $module->page_hauteur;
  391. }
  392. $htmltooltip .= '<br><br><u>' . $langs->trans("FeaturesSupported") . ':</u>';
  393. $htmltooltip .= '<br>' . $langs->trans("Logo") . ': ' . yn($module->option_logo, 1, 1);
  394. $htmltooltip .= '<br>' . $langs->trans("MultiLanguage") . ': ' . yn($module->option_multilang, 1, 1);
  395. print '<td class="center">';
  396. print $form->textwithpicto('', $htmltooltip, 1, 0);
  397. print '</td>';
  398. // Preview
  399. print '<td class="center">';
  400. if ($module->type == 'pdf') {
  401. print '<a href="' . $_SERVER["PHP_SELF"] . '?action=specimen&module=' . $name . '">' . img_object($langs->trans("Preview"), 'contract') . '</a>';
  402. } else {
  403. print img_object($langs->trans("PreviewNotAvailable"), 'generic');
  404. }
  405. print '</td>';
  406. print "</tr>\n";
  407. }
  408. }
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. print '</table>';
  416. print '</div>';
  417. // End of page
  418. llxFooter();
  419. $db->close();