codeinit.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /* Copyright (C) 2014-2022 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
  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. /**
  19. * \file htdocs/barcode/codeinit.php
  20. * \ingroup member
  21. * \brief Page to make mass init of barcode
  22. */
  23. // Load Dolibarr environment
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  27. // Load translation files required by the page
  28. $langs->loadLangs(array('admin', 'members', 'errors', 'other'));
  29. // Choice of print year or current year.
  30. $now = dol_now();
  31. $year = dol_print_date($now, '%Y');
  32. $month = dol_print_date($now, '%m');
  33. $day = dol_print_date($now, '%d');
  34. $forbarcode = GETPOST('forbarcode');
  35. $fk_barcode_type = GETPOST('fk_barcode_type');
  36. $eraseallproductbarcode = GETPOST('eraseallproductbarcode');
  37. $eraseallthirdpartybarcode = GETPOST('eraseallthirdpartybarcode');
  38. $action = GETPOST('action', 'aZ09');
  39. $producttmp = new Product($db);
  40. $thirdpartytmp = new Societe($db);
  41. $modBarCodeProduct = '';
  42. $modBarCodeThirdparty = '';
  43. $maxperinit = empty($conf->global->BARCODE_INIT_MAX) ? 1000 : $conf->global->BARCODE_INIT_MAX;
  44. // Security check (enable the most restrictive one)
  45. //if ($user->socid > 0) accessforbidden();
  46. //if ($user->socid > 0) $socid = $user->socid;
  47. if (!isModEnabled('barcode')) {
  48. accessforbidden('Module not enabled');
  49. }
  50. //restrictedArea($user, 'barcode');
  51. if (empty($user->admin)) {
  52. accessforbidden('Must be admin');
  53. }
  54. /*
  55. * Actions
  56. */
  57. // Define barcode template for third-party
  58. if (!empty($conf->global->BARCODE_THIRDPARTY_ADDON_NUM)) {
  59. $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
  60. foreach ($dirbarcodenum as $dirroot) {
  61. $dir = dol_buildpath($dirroot, 0);
  62. $handle = @opendir($dir);
  63. if (is_resource($handle)) {
  64. while (($file = readdir($handle)) !== false) {
  65. if (preg_match('/^mod_barcode_thirdparty_.*php$/', $file)) {
  66. $file = substr($file, 0, dol_strlen($file) - 4);
  67. try {
  68. dol_include_once($dirroot.$file.'.php');
  69. } catch (Exception $e) {
  70. dol_syslog($e->getMessage(), LOG_ERR);
  71. }
  72. $modBarCodeThirdparty = new $file();
  73. break;
  74. }
  75. }
  76. closedir($handle);
  77. }
  78. }
  79. }
  80. if ($action == 'initbarcodethirdparties') {
  81. if (!is_object($modBarCodeThirdparty)) {
  82. $error++;
  83. setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
  84. }
  85. if (!$error) {
  86. $thirdpartystatic = new Societe($db);
  87. $db->begin();
  88. $nbok = 0;
  89. if (!empty($eraseallthirdpartybarcode)) {
  90. $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
  91. $sql .= " SET barcode = NULL";
  92. $resql = $db->query($sql);
  93. if ($resql) {
  94. setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
  95. } else {
  96. $error++;
  97. dol_print_error($db);
  98. }
  99. } else {
  100. $sql = "SELECT rowid";
  101. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  102. $sql .= " WHERE barcode IS NULL or barcode = ''";
  103. $sql .= $db->order("datec", "ASC");
  104. $sql .= $db->plimit($maxperinit);
  105. dol_syslog("codeinit", LOG_DEBUG);
  106. $resql = $db->query($sql);
  107. if ($resql) {
  108. $num = $db->num_rows($resql);
  109. $i = 0; $nbok = $nbtry = 0;
  110. while ($i < min($num, $maxperinit)) {
  111. $obj = $db->fetch_object($resql);
  112. if ($obj) {
  113. $thirdpartystatic->id = $obj->rowid;
  114. $nextvalue = $modBarCodeThirdparty->getNextValue($thirdpartystatic, '');
  115. $result = $thirdpartystatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'THIRDPARTY_MODIFY');
  116. $nbtry++;
  117. if ($result > 0) {
  118. $nbok++;
  119. }
  120. }
  121. $i++;
  122. }
  123. } else {
  124. $error++;
  125. dol_print_error($db);
  126. }
  127. if (!$error) {
  128. setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
  129. }
  130. }
  131. if (!$error) {
  132. //$db->rollback();
  133. $db->commit();
  134. } else {
  135. $db->rollback();
  136. }
  137. }
  138. $action = '';
  139. }
  140. // Define barcode template for products
  141. if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
  142. $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
  143. foreach ($dirbarcodenum as $dirroot) {
  144. $dir = dol_buildpath($dirroot, 0);
  145. $handle = @opendir($dir);
  146. if (is_resource($handle)) {
  147. while (($file = readdir($handle)) !== false) {
  148. if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
  149. $file = substr($file, 0, dol_strlen($file) - 4);
  150. if ($file == $conf->global->BARCODE_PRODUCT_ADDON_NUM) {
  151. try {
  152. dol_include_once($dirroot.$file.'.php');
  153. } catch (Exception $e) {
  154. dol_syslog($e->getMessage(), LOG_ERR);
  155. }
  156. $modBarCodeProduct = new $file();
  157. break;
  158. }
  159. }
  160. }
  161. closedir($handle);
  162. }
  163. }
  164. }
  165. if ($action == 'initbarcodeproducts') {
  166. if (!is_object($modBarCodeProduct)) {
  167. $error++;
  168. setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
  169. }
  170. if (!$error) {
  171. $productstatic = new Product($db);
  172. $db->begin();
  173. $nbok = 0;
  174. if (!empty($eraseallproductbarcode)) {
  175. $sql = "UPDATE ".MAIN_DB_PREFIX."product";
  176. $sql .= " SET barcode = NULL";
  177. $resql = $db->query($sql);
  178. if ($resql) {
  179. setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
  180. } else {
  181. $error++;
  182. dol_print_error($db);
  183. }
  184. } else {
  185. $sql = "SELECT rowid, ref, fk_product_type";
  186. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  187. $sql .= " WHERE barcode IS NULL or barcode = ''";
  188. $sql .= $db->order("datec", "ASC");
  189. $sql .= $db->plimit($maxperinit);
  190. dol_syslog("codeinit", LOG_DEBUG);
  191. $resql = $db->query($sql);
  192. if ($resql) {
  193. $num = $db->num_rows($resql);
  194. $i = 0; $nbok = $nbtry = 0;
  195. while ($i < min($num, $maxperinit)) {
  196. $obj = $db->fetch_object($resql);
  197. if ($obj) {
  198. $productstatic->id = $obj->rowid;
  199. $productstatic->ref = $obj->ref;
  200. $productstatic->type = $obj->fk_product_type;
  201. $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
  202. //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
  203. $result = $productstatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'PRODUCT_MODIFY');
  204. $nbtry++;
  205. if ($result > 0) {
  206. $nbok++;
  207. }
  208. }
  209. $i++;
  210. }
  211. } else {
  212. $error++;
  213. dol_print_error($db);
  214. }
  215. if (!$error) {
  216. setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
  217. }
  218. }
  219. if (!$error) {
  220. //$db->rollback();
  221. $db->commit();
  222. } else {
  223. $db->rollback();
  224. }
  225. }
  226. $action = '';
  227. }
  228. /*
  229. * View
  230. */
  231. $form = new Form($db);
  232. llxHeader('', $langs->trans("MassBarcodeInit"));
  233. print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
  234. print '<br>';
  235. print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
  236. print '<br>';
  237. //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
  238. //print '<br>';
  239. print '<br>';
  240. // Example 1 : Adding jquery code
  241. print '<script type="text/javascript">
  242. function confirm_erase() {
  243. return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
  244. }
  245. </script>';
  246. // For thirdparty
  247. if (isModEnabled('societe')) {
  248. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  249. print '<input type="hidden" name="mode" value="label">';
  250. print '<input type="hidden" name="action" value="initbarcodethirdparties">';
  251. print '<input type="hidden" name="token" value="'.newToken().'">';
  252. $nbthirdpartyno = $nbthirdpartytotal = 0;
  253. print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
  254. print '<br>'."\n";
  255. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
  256. $resql = $db->query($sql);
  257. if ($resql) {
  258. $obj = $db->fetch_object($resql);
  259. $nbthirdpartyno = $obj->nb;
  260. } else {
  261. dol_print_error($db);
  262. }
  263. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
  264. $resql = $db->query($sql);
  265. if ($resql) {
  266. $obj = $db->fetch_object($resql);
  267. $nbthirdpartytotal = $obj->nb;
  268. } else {
  269. dol_print_error($db);
  270. }
  271. print $langs->trans("CurrentlyNWithoutBarCode", $nbthirdpartyno, $nbthirdpartytotal, $langs->transnoentitiesnoconv("ThirdParties"))."\n";
  272. $disabledthirdparty = $disabledthirdparty1 = 0;
  273. if (is_object($modBarCodeThirdparty)) {
  274. print '<br>'.$langs->trans("BarCodeNumberManager").": ";
  275. $objthirdparty = new Societe($db);
  276. print '<b>'.(isset($modBarCodeThirdparty->name) ? $modBarCodeThirdparty->name : $modBarCodeThirdparty->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeThirdparty->getNextValue($objthirdparty).'</b><br>';
  277. $disabledthirdparty = 0;
  278. print '<br>';
  279. } else {
  280. $disabledthirdparty = 1;
  281. $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
  282. print '<div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
  283. print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
  284. print '</div>';
  285. }
  286. if (empty($nbthirdpartyno)) {
  287. $disabledthirdparty1 = 1;
  288. }
  289. $moretagsthirdparty1 = (($disabledthirdparty || $disabledthirdparty1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
  290. print '<br><input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" value="'.$langs->trans("InitEmptyBarCode", $nbthirdpartyno).'"'.$moretagsthirdparty1.'>';
  291. $moretagsthirdparty2 = (($nbthirdpartyno == $nbthirdpartytotal) ? ' disabled' : '');
  292. print ' &nbsp; ';
  293. print '<input type="submit" class="button butActionDelete" name="eraseallthirdpartybarcode" id="eraseallthirdpartybarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsthirdparty2.' onClick="return confirm_erase();">';
  294. print '<br><br><br><br>';
  295. print '</form>';
  296. }
  297. // For products
  298. if (isModEnabled('product') || isModEnabled('service')) {
  299. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  300. print '<input type="hidden" name="mode" value="label">';
  301. print '<input type="hidden" name="action" value="initbarcodeproducts">';
  302. print '<input type="hidden" name="token" value="'.newToken().'">';
  303. $nbproductno = $nbproducttotal = 0;
  304. print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
  305. print '<br>'."\n";
  306. $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
  307. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  308. $sql .= " WHERE barcode IS NULL OR barcode = ''";
  309. $sql .= " GROUP BY fk_product_type, datec";
  310. $sql .= " ORDER BY datec";
  311. $resql = $db->query($sql);
  312. if ($resql) {
  313. $num = $db->num_rows($resql);
  314. $i = 0;
  315. while ($i < $num) {
  316. $obj = $db->fetch_object($resql);
  317. $nbproductno += $obj->nb;
  318. $i++;
  319. }
  320. } else {
  321. dol_print_error($db);
  322. }
  323. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
  324. $resql = $db->query($sql);
  325. if ($resql) {
  326. $obj = $db->fetch_object($resql);
  327. $nbproducttotal = $obj->nb;
  328. } else {
  329. dol_print_error($db);
  330. }
  331. print $langs->trans("CurrentlyNWithoutBarCode", $nbproductno, $nbproducttotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
  332. $disabledproduct = $disabledproduct1 = 0;
  333. if (is_object($modBarCodeProduct)) {
  334. print '<br>'.$langs->trans("BarCodeNumberManager").": ";
  335. $objproduct = new Product($db);
  336. print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
  337. $disabledproduct = 0;
  338. print '<br>';
  339. } else {
  340. $disabledproduct = 1;
  341. $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
  342. print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
  343. print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
  344. print '</div>';
  345. }
  346. if (empty($nbproductno)) {
  347. $disabledproduct1 = 1;
  348. }
  349. //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
  350. $moretagsproduct1 = (($disabledproduct || $disabledproduct1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
  351. print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbproductno)).'"'.$moretagsproduct1.'>';
  352. $moretagsproduct2 = (($nbproductno == $nbproducttotal) ? ' disabled' : '');
  353. print ' &nbsp; ';
  354. print '<input type="submit" class="button butActionDelete" name="eraseallproductbarcode" id="eraseallproductbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsproduct2.' onClick="return confirm_erase();">';
  355. print '<br><br><br><br>';
  356. print '</form>';
  357. }
  358. print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
  359. print '<br>'."\n";
  360. print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
  361. print '<br>';
  362. // End of page
  363. llxFooter();
  364. $db->close();