product_lot.php 17 KB

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