barcode.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. /* Copyright (C) 2003-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011-2013 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/admin/barcode.php
  22. * \ingroup barcode
  23. * \brief Page to setup barcode module
  24. */
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formbarcode.class.php';
  28. // Load translation files required by the page
  29. $langs->load("admin");
  30. if (!$user->admin) {
  31. accessforbidden();
  32. }
  33. $action = GETPOST('action', 'aZ09');
  34. $modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
  35. /*
  36. * Actions
  37. */
  38. include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
  39. if ($action == 'setbarcodeproducton') {
  40. $barcodenumberingmodule = GETPOST('value', 'alpha');
  41. $res = dolibarr_set_const($db, "BARCODE_PRODUCT_ADDON_NUM", $barcodenumberingmodule, 'chaine', 0, '', $conf->entity);
  42. if ($barcodenumberingmodule == 'mod_barcode_product_standard' && empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) {
  43. $res = dolibarr_set_const($db, "BARCODE_STANDARD_PRODUCT_MASK", '020{000000000}', 'chaine', 0, '', $conf->entity);
  44. }
  45. } elseif ($action == 'setbarcodeproductoff') {
  46. $res = dolibarr_del_const($db, "BARCODE_PRODUCT_ADDON_NUM", $conf->entity);
  47. }
  48. if ($action == 'setbarcodethirdpartyon') {
  49. $barcodenumberingmodule = GETPOST('value', 'alpha');
  50. $res = dolibarr_set_const($db, "BARCODE_THIRDPARTY_ADDON_NUM", $barcodenumberingmodule, 'chaine', 0, '', $conf->entity);
  51. if ($barcodenumberingmodule == 'mod_barcode_thirdparty_standard' && empty($conf->global->BARCODE_STANDARD_THIRDPARTY_MASK)) {
  52. $res = dolibarr_set_const($db, "BARCODE_STANDARD_THIRDPARTY_MASK", '020{000000000}', 'chaine', 0, '', $conf->entity);
  53. }
  54. } elseif ($action == 'setbarcodethirdpartyoff') {
  55. $res = dolibarr_del_const($db, "BARCODE_THIRDPARTY_ADDON_NUM", $conf->entity);
  56. }
  57. if ($action == 'setcoder') {
  58. $coder = GETPOST('coder', 'alpha');
  59. $code_id = GETPOST('code_id', 'int');
  60. $sqlp = "UPDATE ".MAIN_DB_PREFIX."c_barcode_type";
  61. $sqlp .= " SET coder = '".$db->escape($coder)."'";
  62. $sqlp .= " WHERE rowid = ".((int) $code_id);
  63. $sqlp .= " AND entity = ".$conf->entity;
  64. $resql = $db->query($sqlp);
  65. if (!$resql) {
  66. dol_print_error($db);
  67. }
  68. } elseif ($action == 'update') {
  69. $location = GETPOST('GENBARCODE_LOCATION', 'alpha');
  70. $res = dolibarr_set_const($db, "GENBARCODE_LOCATION", $location, 'chaine', 0, '', $conf->entity);
  71. $coder_id = GETPOST('PRODUIT_DEFAULT_BARCODE_TYPE', 'alpha');
  72. $res = dolibarr_set_const($db, "PRODUIT_DEFAULT_BARCODE_TYPE", $coder_id, 'chaine', 0, '', $conf->entity);
  73. $coder_id = GETPOST('GENBARCODE_BARCODETYPE_THIRDPARTY', 'alpha');
  74. $res = dolibarr_set_const($db, "GENBARCODE_BARCODETYPE_THIRDPARTY", $coder_id, 'chaine', 0, '', $conf->entity);
  75. if ($res > 0) {
  76. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  77. } else {
  78. setEventMessages($langs->trans("Error"), null, 'errors');
  79. }
  80. } elseif ($action == 'updateengine') {
  81. $sql = "SELECT rowid, coder";
  82. $sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
  83. $sql .= " WHERE entity = ".$conf->entity;
  84. $sql .= " ORDER BY code";
  85. $resql = $db->query($sql);
  86. if ($resql) {
  87. $num = $db->num_rows($resql);
  88. $i = 0;
  89. while ($i < $num) {
  90. $obj = $db->fetch_object($resql);
  91. if (GETPOST('coder'.$obj->rowid, 'alpha')) {
  92. $coder = GETPOST('coder'.$obj->rowid, 'alpha');
  93. $code_id = $obj->rowid;
  94. $sqlp = "UPDATE ".MAIN_DB_PREFIX."c_barcode_type";
  95. $sqlp .= " SET coder = '".$db->escape($coder)."'";
  96. $sqlp .= " WHERE rowid = ".((int) $code_id);
  97. $sqlp .= " AND entity = ".$conf->entity;
  98. $upsql = $db->query($sqlp);
  99. if (!$upsql) {
  100. dol_print_error($db);
  101. }
  102. }
  103. $i++;
  104. }
  105. }
  106. }
  107. /*
  108. * View
  109. */
  110. $form = new Form($db);
  111. $formbarcode = new FormBarCode($db);
  112. $help_url = 'EN:Module_Barcode|FR:Module_Codes_Barre|ES:Módulo Código de barra|DE:Modul_Barcode';
  113. llxHeader('', $langs->trans("BarcodeSetup"), $help_url);
  114. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  115. print load_fiche_titre($langs->trans("BarcodeSetup"), $linkback, 'title_setup');
  116. // Detect bar codes modules
  117. $barcodelist = array();
  118. clearstatcache();
  119. // Scan list of all barcode included provided by external modules
  120. $dirbarcode = array_merge(array("/core/modules/barcode/doc/"), $conf->modules_parts['barcode']);
  121. foreach ($dirbarcode as $reldir) {
  122. $dir = dol_buildpath($reldir);
  123. $newdir = dol_osencode($dir);
  124. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
  125. if (!is_dir($newdir)) {
  126. continue;
  127. }
  128. $handle = @opendir($newdir);
  129. if (is_resource($handle)) {
  130. while (($file = readdir($handle)) !== false) {
  131. if (substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS') {
  132. if (is_readable($newdir.$file)) {
  133. if (preg_match('/(.*)\.modules\.php$/i', $file, $reg)) {
  134. $filebis = $reg[1];
  135. // Loading encoding class
  136. require_once $newdir.$file;
  137. $classname = "mod".ucfirst($filebis);
  138. $module = new $classname($db);
  139. // Show modules according to features level
  140. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  141. continue;
  142. }
  143. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  144. continue;
  145. }
  146. if ($module->isEnabled()) {
  147. $barcodelist[$filebis] = $module->info();
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. // Select barcode numbering module
  156. if (isModEnabled('product')) {
  157. print load_fiche_titre($langs->trans("BarCodeNumberManager")." (".$langs->trans("Product").")", '', '');
  158. print '<div class="div-table-responsive-no-min">';
  159. print '<table class="noborder centpercent">';
  160. print '<tr class="liste_titre">';
  161. print '<td width="140">'.$langs->trans("Name").'</td>';
  162. print '<td>'.$langs->trans("Description").'</td>';
  163. print '<td>'.$langs->trans("Example").'</td>';
  164. print '<td class="center" width="80">'.$langs->trans("Status").'</td>';
  165. print '<td class="center" width="60">'.$langs->trans("ShortInfo").'</td>';
  166. print "</tr>\n";
  167. $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
  168. foreach ($dirbarcodenum as $dirroot) {
  169. $dir = dol_buildpath($dirroot, 0);
  170. $handle = @opendir($dir);
  171. if (is_resource($handle)) {
  172. while (($file = readdir($handle)) !== false) {
  173. if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
  174. $file = substr($file, 0, dol_strlen($file) - 4);
  175. try {
  176. dol_include_once($dirroot.$file.'.php');
  177. } catch (Exception $e) {
  178. dol_syslog($e->getMessage(), LOG_ERR);
  179. }
  180. $modBarCode = new $file();
  181. print '<tr class="oddeven">';
  182. print '<td>'.(isset($modBarCode->name) ? $modBarCode->name : $modBarCode->nom)."</td><td>\n";
  183. print $modBarCode->info($langs);
  184. print '</td>';
  185. print '<td class="nowrap">'.$modBarCode->getExample($langs)."</td>\n";
  186. if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM) && $conf->global->BARCODE_PRODUCT_ADDON_NUM == "$file") {
  187. print '<td class="center"><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setbarcodeproductoff&token='.newToken().'&amp;value='.urlencode($file).'">';
  188. print img_picto($langs->trans("Activated"), 'switch_on');
  189. print '</a></td>';
  190. } else {
  191. print '<td class="center"><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setbarcodeproducton&token='.newToken().'&amp;value='.urlencode($file).'">';
  192. print img_picto($langs->trans("Disabled"), 'switch_off');
  193. print '</a></td>';
  194. }
  195. print '<td class="center">';
  196. $s = $modBarCode->getToolTip($langs, null, -1);
  197. print $form->textwithpicto('', $s, 1);
  198. print '</td>';
  199. print "</tr>\n";
  200. }
  201. }
  202. closedir($handle);
  203. }
  204. }
  205. print "</table>\n";
  206. print '</div>';
  207. }
  208. // Select barcode numbering module
  209. if (isModEnabled('societe')) {
  210. print load_fiche_titre($langs->trans("BarCodeNumberManager")." (".$langs->trans("ThirdParty").")", '', '');
  211. print '<div class="div-table-responsive-no-min">';
  212. print '<table class="noborder centpercent">';
  213. print '<tr class="liste_titre">';
  214. print '<td width="140">'.$langs->trans("Name").'</td>';
  215. print '<td>'.$langs->trans("Description").'</td>';
  216. print '<td>'.$langs->trans("Example").'</td>';
  217. print '<td class="center" width="80">'.$langs->trans("Status").'</td>';
  218. print '<td class="center" width="60">'.$langs->trans("ShortInfo").'</td>';
  219. print "</tr>\n";
  220. $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
  221. foreach ($dirbarcodenum as $dirroot) {
  222. $dir = dol_buildpath($dirroot, 0);
  223. $handle = @opendir($dir);
  224. if (is_resource($handle)) {
  225. while (($file = readdir($handle)) !== false) {
  226. if (preg_match('/^mod_barcode_thirdparty_.*php$/', $file)) {
  227. $file = substr($file, 0, dol_strlen($file) - 4);
  228. try {
  229. dol_include_once($dirroot.$file.'.php');
  230. } catch (Exception $e) {
  231. dol_syslog($e->getMessage(), LOG_ERR);
  232. }
  233. $modBarCode = new $file();
  234. print '<tr class="oddeven">';
  235. print '<td>'.(isset($modBarCode->name) ? $modBarCode->name : $modBarCode->nom)."</td><td>\n";
  236. print $modBarCode->info($langs);
  237. print '</td>';
  238. print '<td class="nowrap">'.$modBarCode->getExample($langs)."</td>\n";
  239. if (!empty($conf->global->BARCODE_THIRDPARTY_ADDON_NUM) && $conf->global->BARCODE_THIRDPARTY_ADDON_NUM == "$file") {
  240. print '<td class="center"><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setbarcodethirdpartyoff&token='.newToken().'&amp;value='.urlencode($file).'">';
  241. print img_picto($langs->trans("Activated"), 'switch_on');
  242. print '</a></td>';
  243. } else {
  244. print '<td class="center"><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setbarcodethirdpartyon&token='.newToken().'&amp;value='.urlencode($file).'">';
  245. print img_picto($langs->trans("Disabled"), 'switch_off');
  246. print '</a></td>';
  247. }
  248. print '<td class="center">';
  249. $s = $modBarCode->getToolTip($langs, null, -1);
  250. print $form->textwithpicto('', $s, 1);
  251. print '</td>';
  252. print "</tr>\n";
  253. }
  254. }
  255. closedir($handle);
  256. }
  257. }
  258. print "</table>\n";
  259. print '</div>';
  260. }
  261. /*
  262. * CHOIX ENCODAGE
  263. */
  264. print '<br>';
  265. print load_fiche_titre($langs->trans("BarcodeEncodeModule"), '', '');
  266. if (empty($conf->use_javascript_ajax)) {
  267. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" id="form_engine">';
  268. print '<input type="hidden" name="token" value="'.newToken().'">';
  269. print '<input type="hidden" name="action" value="updateengine">';
  270. }
  271. print '<div class="div-table-responsive-no-min">';
  272. print '<table class="noborder centpercent">';
  273. print '<tr class="liste_titre">';
  274. print '<td>'.$langs->trans("Name").'</td>';
  275. print '<td>'.$langs->trans("Description").'</td>';
  276. print '<td width="200" class="center">'.$langs->trans("Example").'</td>';
  277. print '<td class="center" width="60">'.$langs->trans("CodeBarGenerator").'</td>';
  278. print "</tr>\n";
  279. $sql = "SELECT rowid, code as encoding, libelle as label, coder, example";
  280. $sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
  281. $sql .= " WHERE entity = ".$conf->entity;
  282. $sql .= " ORDER BY code";
  283. dol_syslog("admin/barcode.php", LOG_DEBUG);
  284. $resql = $db->query($sql);
  285. if ($resql) {
  286. $num = $db->num_rows($resql);
  287. $i = 0;
  288. while ($i < $num) {
  289. $obj = $db->fetch_object($resql);
  290. print '<tr class="oddeven">';
  291. print '<td width="100">';
  292. print dol_escape_htmltag($obj->label);
  293. print "</td><td>\n";
  294. print $langs->trans('BarcodeDesc'.$obj->encoding);
  295. //print "L'EAN se compose de 8 caracteres, 7 chiffres plus une cle de controle.<br>";
  296. //print "L'utilisation des symbologies EAN8 impose la souscription et l'abonnement aupres d'organisme tel que GENCOD.<br>";
  297. //print "Codes numeriques utilises exclusivement a l'identification des produits susceptibles d'etre vendus au grand public.";
  298. print '</td>';
  299. // Show example
  300. print '<td class="center">';
  301. if ($obj->coder && $obj->coder != -1) {
  302. $result = 0;
  303. foreach ($dirbarcode as $reldir) {
  304. $dir = dol_buildpath($reldir, 0);
  305. $newdir = dol_osencode($dir);
  306. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
  307. if (!is_dir($newdir)) {
  308. continue;
  309. }
  310. $result = @include_once $newdir.$obj->coder.'.modules.php';
  311. if ($result) {
  312. break;
  313. }
  314. }
  315. if ($result) {
  316. $classname = "mod".ucfirst($obj->coder);
  317. if (class_exists($classname)) {
  318. $module = new $classname($db);
  319. if ($module->encodingIsSupported($obj->encoding)) {
  320. // Build barcode on disk (not used, this is done to make debug easier)
  321. $result = $module->writeBarCode($obj->example, $obj->encoding, 'Y');
  322. // Generate on the fly and output barcode with generator
  323. $url = DOL_URL_ROOT.'/viewimage.php?modulepart=barcode&amp;generator='.urlencode($obj->coder).'&amp;code='.urlencode($obj->example).'&amp;encoding='.urlencode($obj->encoding);
  324. //print $url;
  325. print '<img src="'.$url.'" title="'.$obj->example.'" border="0">';
  326. } else {
  327. print $langs->trans("FormatNotSupportedByGenerator");
  328. }
  329. } else {
  330. print 'ErrorClassNotFoundInModule '.$classname.' '.$obj->coder;
  331. }
  332. }
  333. } else {
  334. print '<span class="opacitymedium">'.$langs->trans("ChooseABarCode").'</span>';
  335. }
  336. print '</td>';
  337. print '<td class="center">';
  338. print $formbarcode->setBarcodeEncoder($obj->coder, $barcodelist, $obj->rowid, 'form'.$i);
  339. print "</td></tr>\n";
  340. $i++;
  341. }
  342. }
  343. print "</table>\n";
  344. print '</div>';
  345. if (empty($conf->use_javascript_ajax)) {
  346. print $form->buttonsSaveCancel("Save", '');
  347. }
  348. print "<br>";
  349. /*
  350. * Other options
  351. */
  352. print load_fiche_titre($langs->trans("OtherOptions"), '', '');
  353. print "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";
  354. print '<input type="hidden" name="token" value="'.newToken().'">';
  355. print "<input type=\"hidden\" name=\"action\" value=\"update\">";
  356. print '<div class="div-table-responsive-no-min">';
  357. print '<table class="noborder centpercent">';
  358. print '<tr class="liste_titre">';
  359. print '<td>'.$langs->trans("Parameter").'</td>';
  360. print '<td width="60" class="center">'.$langs->trans("Value").'</td>';
  361. print '<td>&nbsp;</td>';
  362. print '</tr>';
  363. // Chemin du binaire genbarcode sous linux
  364. if (!isset($_SERVER['WINDIR'])) {
  365. print '<tr class="oddeven">';
  366. print '<td>'.$langs->trans("GenbarcodeLocation").'</td>';
  367. print '<td width="60" class="center">';
  368. print '<input type="text" size="40" name="GENBARCODE_LOCATION" value="'.getDolGlobalString('GENBARCODE_LOCATION').'">';
  369. if (!empty($conf->global->GENBARCODE_LOCATION) && !@file_exists($conf->global->GENBARCODE_LOCATION)) {
  370. $langs->load("errors");
  371. print '<br><span class="error">'.$langs->trans("ErrorFileNotFound", $conf->global->GENBARCODE_LOCATION).'</span>';
  372. }
  373. print '</td>';
  374. print '<td>&nbsp;</td>';
  375. print '</tr>';
  376. }
  377. // Module products
  378. if (isModEnabled('product')) {
  379. print '<tr class="oddeven">';
  380. print '<td>'.$langs->trans("SetDefaultBarcodeTypeProducts").'</td>';
  381. print '<td width="60" class="right">';
  382. print $formbarcode->selectBarcodeType($conf->global->PRODUIT_DEFAULT_BARCODE_TYPE, "PRODUIT_DEFAULT_BARCODE_TYPE", 1);
  383. print '</td>';
  384. print '<td>&nbsp;</td>';
  385. print '</tr>';
  386. }
  387. // Module thirdparty
  388. if (isModEnabled('societe')) {
  389. print '<tr class="oddeven">';
  390. print '<td>'.$langs->trans("SetDefaultBarcodeTypeThirdParties").'</td>';
  391. print '<td width="60" class="right">';
  392. print $formbarcode->selectBarcodeType($conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY, "GENBARCODE_BARCODETYPE_THIRDPARTY", 1);
  393. print '</td>';
  394. print '<td>&nbsp;</td>';
  395. print '</tr>';
  396. }
  397. print "</table>\n";
  398. print '</div>';
  399. print '<div class="tabsAction">';
  400. print '<input type="submit" class="button" name="submit_GENBARCODE_BARCODETYPE_THIRDPARTY" value="'.$langs->trans("Modify").'">';
  401. print "</div>";
  402. print '</form>';
  403. print '<br>';
  404. // End of page
  405. llxFooter();
  406. $db->close();