codeinit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 = !getDolGlobalString('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 (getDolGlobalString('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 .= " AND entity IN (".getEntity('societe').")";
  92. $sql .= " SET barcode = NULL";
  93. $resql = $db->query($sql);
  94. if ($resql) {
  95. setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
  96. } else {
  97. $error++;
  98. dol_print_error($db);
  99. }
  100. } else {
  101. $sql = "SELECT rowid";
  102. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  103. $sql .= " WHERE barcode IS NULL or barcode = ''";
  104. $sql .= " AND entity IN (".getEntity('societe').")";
  105. $sql .= $db->order("datec", "ASC");
  106. $sql .= $db->plimit($maxperinit);
  107. dol_syslog("codeinit", LOG_DEBUG);
  108. $resql = $db->query($sql);
  109. if ($resql) {
  110. $num = $db->num_rows($resql);
  111. $i = 0;
  112. $nbok = $nbtry = 0;
  113. while ($i < min($num, $maxperinit)) {
  114. $obj = $db->fetch_object($resql);
  115. if ($obj) {
  116. $thirdpartystatic->id = $obj->rowid;
  117. $nextvalue = $modBarCodeThirdparty->getNextValue($thirdpartystatic, '');
  118. $result = $thirdpartystatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'THIRDPARTY_MODIFY');
  119. $nbtry++;
  120. if ($result > 0) {
  121. $nbok++;
  122. }
  123. }
  124. $i++;
  125. }
  126. } else {
  127. $error++;
  128. dol_print_error($db);
  129. }
  130. if (!$error) {
  131. setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
  132. }
  133. }
  134. if (!$error) {
  135. //$db->rollback();
  136. $db->commit();
  137. } else {
  138. $db->rollback();
  139. }
  140. }
  141. $action = '';
  142. }
  143. // Define barcode template for products
  144. if (getDolGlobalString('BARCODE_PRODUCT_ADDON_NUM')) {
  145. $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
  146. foreach ($dirbarcodenum as $dirroot) {
  147. $dir = dol_buildpath($dirroot, 0);
  148. $handle = @opendir($dir);
  149. if (is_resource($handle)) {
  150. while (($file = readdir($handle)) !== false) {
  151. if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
  152. $file = substr($file, 0, dol_strlen($file) - 4);
  153. if ($file == $conf->global->BARCODE_PRODUCT_ADDON_NUM) {
  154. try {
  155. dol_include_once($dirroot.$file.'.php');
  156. } catch (Exception $e) {
  157. dol_syslog($e->getMessage(), LOG_ERR);
  158. }
  159. $modBarCodeProduct = new $file();
  160. break;
  161. }
  162. }
  163. }
  164. closedir($handle);
  165. }
  166. }
  167. }
  168. if ($action == 'initbarcodeproducts') {
  169. if (!is_object($modBarCodeProduct)) {
  170. $error++;
  171. setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
  172. }
  173. if (!$error) {
  174. $productstatic = new Product($db);
  175. $db->begin();
  176. $nbok = 0;
  177. if (!empty($eraseallproductbarcode)) {
  178. $sql = "UPDATE ".MAIN_DB_PREFIX."product";
  179. $sql .= " SET barcode = NULL";
  180. $sql .= " WHERE entity IN (".getEntity('product').")";
  181. $resql = $db->query($sql);
  182. if ($resql) {
  183. setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
  184. } else {
  185. $error++;
  186. dol_print_error($db);
  187. }
  188. } else {
  189. $sql = "SELECT rowid, ref, fk_product_type";
  190. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  191. $sql .= " WHERE barcode IS NULL or barcode = ''";
  192. $sql .= " AND entity IN (".getEntity('product').")";
  193. $sql .= $db->order("datec", "ASC");
  194. $sql .= $db->plimit($maxperinit);
  195. dol_syslog("codeinit", LOG_DEBUG);
  196. $resql = $db->query($sql);
  197. if ($resql) {
  198. $num = $db->num_rows($resql);
  199. $i = 0;
  200. $nbok = $nbtry = 0;
  201. while ($i < min($num, $maxperinit)) {
  202. $obj = $db->fetch_object($resql);
  203. if ($obj) {
  204. $productstatic->id = $obj->rowid;
  205. $productstatic->ref = $obj->ref;
  206. $productstatic->type = $obj->fk_product_type;
  207. $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
  208. //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
  209. $result = $productstatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'PRODUCT_MODIFY');
  210. $nbtry++;
  211. if ($result > 0) {
  212. $nbok++;
  213. }
  214. }
  215. $i++;
  216. }
  217. } else {
  218. $error++;
  219. dol_print_error($db);
  220. }
  221. if (!$error) {
  222. setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
  223. }
  224. }
  225. if (!$error) {
  226. //$db->rollback();
  227. $db->commit();
  228. } else {
  229. $db->rollback();
  230. }
  231. }
  232. $action = '';
  233. }
  234. /*
  235. * View
  236. */
  237. $form = new Form($db);
  238. llxHeader('', $langs->trans("MassBarcodeInit"));
  239. print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
  240. print '<br>';
  241. print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
  242. print '<br>';
  243. //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
  244. //print '<br>';
  245. print '<br>';
  246. // Example 1 : Adding jquery code
  247. print '<script type="text/javascript">
  248. function confirm_erase() {
  249. return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
  250. }
  251. </script>';
  252. // For thirdparty
  253. if (isModEnabled('societe')) {
  254. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  255. print '<input type="hidden" name="mode" value="label">';
  256. print '<input type="hidden" name="action" value="initbarcodethirdparties">';
  257. print '<input type="hidden" name="token" value="'.newToken().'">';
  258. $nbthirdpartyno = $nbthirdpartytotal = 0;
  259. print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
  260. print '<br>'."\n";
  261. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
  262. $resql = $db->query($sql);
  263. if ($resql) {
  264. $obj = $db->fetch_object($resql);
  265. $nbthirdpartyno = $obj->nb;
  266. } else {
  267. dol_print_error($db);
  268. }
  269. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
  270. $sql .= " WHERE entity IN (".getEntity('societe').")";
  271. $resql = $db->query($sql);
  272. if ($resql) {
  273. $obj = $db->fetch_object($resql);
  274. $nbthirdpartytotal = $obj->nb;
  275. } else {
  276. dol_print_error($db);
  277. }
  278. print $langs->trans("CurrentlyNWithoutBarCode", $nbthirdpartyno, $nbthirdpartytotal, $langs->transnoentitiesnoconv("ThirdParties"))."\n";
  279. $disabledthirdparty = $disabledthirdparty1 = 0;
  280. if (is_object($modBarCodeThirdparty)) {
  281. print '<br>'.$langs->trans("BarCodeNumberManager").": ";
  282. $objthirdparty = new Societe($db);
  283. print '<b>'.(isset($modBarCodeThirdparty->name) ? $modBarCodeThirdparty->name : $modBarCodeThirdparty->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeThirdparty->getNextValue($objthirdparty).'</b><br>';
  284. $disabledthirdparty = 0;
  285. print '<br>';
  286. } else {
  287. $disabledthirdparty = 1;
  288. $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
  289. print '<div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
  290. print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
  291. print '</div>';
  292. }
  293. if (empty($nbthirdpartyno)) {
  294. $disabledthirdparty1 = 1;
  295. }
  296. $moretagsthirdparty1 = (($disabledthirdparty || $disabledthirdparty1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
  297. print '<br><input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" value="'.$langs->trans("InitEmptyBarCode", $nbthirdpartyno).'"'.$moretagsthirdparty1.'>';
  298. $moretagsthirdparty2 = (($nbthirdpartyno == $nbthirdpartytotal) ? ' disabled' : '');
  299. print ' &nbsp; ';
  300. print '<input type="submit" class="button butActionDelete" name="eraseallthirdpartybarcode" id="eraseallthirdpartybarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsthirdparty2.' onClick="return confirm_erase();">';
  301. print '<br><br><br><br>';
  302. print '</form>';
  303. }
  304. // For products
  305. if (isModEnabled('product') || isModEnabled('service')) {
  306. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  307. print '<input type="hidden" name="mode" value="label">';
  308. print '<input type="hidden" name="action" value="initbarcodeproducts">';
  309. print '<input type="hidden" name="token" value="'.newToken().'">';
  310. $nbproductno = $nbproducttotal = 0;
  311. print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
  312. print '<br>'."\n";
  313. $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
  314. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  315. $sql .= " WHERE barcode IS NULL OR barcode = ''";
  316. $sql .= " AND entity IN (".getEntity('product').")";
  317. $sql .= " GROUP BY fk_product_type, datec";
  318. $sql .= " ORDER BY datec";
  319. $resql = $db->query($sql);
  320. if ($resql) {
  321. $num = $db->num_rows($resql);
  322. $i = 0;
  323. while ($i < $num) {
  324. $obj = $db->fetch_object($resql);
  325. $nbproductno += $obj->nb;
  326. $i++;
  327. }
  328. } else {
  329. dol_print_error($db);
  330. }
  331. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
  332. $sql .= " WHERE entity IN (".getEntity('product').")";
  333. $resql = $db->query($sql);
  334. if ($resql) {
  335. $obj = $db->fetch_object($resql);
  336. $nbproducttotal = $obj->nb;
  337. } else {
  338. dol_print_error($db);
  339. }
  340. print $langs->trans("CurrentlyNWithoutBarCode", $nbproductno, $nbproducttotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
  341. $disabledproduct = $disabledproduct1 = 0;
  342. if (is_object($modBarCodeProduct)) {
  343. print '<br>'.$langs->trans("BarCodeNumberManager").": ";
  344. $objproduct = new Product($db);
  345. print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
  346. $disabledproduct = 0;
  347. print '<br>';
  348. } else {
  349. $disabledproduct = 1;
  350. $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
  351. print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
  352. print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
  353. print '</div>';
  354. }
  355. if (empty($nbproductno)) {
  356. $disabledproduct1 = 1;
  357. }
  358. //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
  359. $moretagsproduct1 = (($disabledproduct || $disabledproduct1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
  360. print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbproductno)).'"'.$moretagsproduct1.'>';
  361. $moretagsproduct2 = (($nbproductno == $nbproducttotal) ? ' disabled' : '');
  362. print ' &nbsp; ';
  363. print '<input type="submit" class="button butActionDelete" name="eraseallproductbarcode" id="eraseallproductbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsproduct2.' onClick="return confirm_erase();">';
  364. print '<br><br><br><br>';
  365. print '</form>';
  366. }
  367. print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
  368. print '<br>'."\n";
  369. print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
  370. print '<br>';
  371. // End of page
  372. llxFooter();
  373. $db->close();