codeinit.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  26. // Load translation files required by the page
  27. $langs->loadLangs(array('admin', 'members', 'errors', 'other'));
  28. // Choice of print year or current year.
  29. $now = dol_now();
  30. $year = dol_print_date($now, '%Y');
  31. $month = dol_print_date($now, '%m');
  32. $day = dol_print_date($now, '%d');
  33. $forbarcode = GETPOST('forbarcode');
  34. $fk_barcode_type = GETPOST('fk_barcode_type');
  35. $eraseallbarcode = GETPOST('eraseallbarcode');
  36. $action = GETPOST('action', 'aZ09');
  37. $producttmp = new Product($db);
  38. $thirdpartytmp = new Societe($db);
  39. $modBarCodeProduct = '';
  40. $maxperinit = empty($conf->global->BARCODE_INIT_MAX) ? 1000 : $conf->global->BARCODE_INIT_MAX;
  41. /*
  42. * Actions
  43. */
  44. // Define barcode template for products
  45. if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
  46. $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
  47. foreach ($dirbarcodenum as $dirroot) {
  48. $dir = dol_buildpath($dirroot, 0);
  49. $handle = @opendir($dir);
  50. if (is_resource($handle)) {
  51. while (($file = readdir($handle)) !== false) {
  52. if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
  53. $file = substr($file, 0, dol_strlen($file) - 4);
  54. if ($file == $conf->global->BARCODE_PRODUCT_ADDON_NUM) {
  55. try {
  56. dol_include_once($dirroot.$file.'.php');
  57. } catch (Exception $e) {
  58. dol_syslog($e->getMessage(), LOG_ERR);
  59. }
  60. $modBarCodeProduct = new $file();
  61. break;
  62. }
  63. }
  64. }
  65. closedir($handle);
  66. }
  67. }
  68. }
  69. if ($action == 'initbarcodeproducts') {
  70. if (!is_object($modBarCodeProduct)) {
  71. $error++;
  72. setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
  73. }
  74. if (!$error) {
  75. $productstatic = new Product($db);
  76. $db->begin();
  77. $nbok = 0;
  78. if (!empty($eraseallbarcode)) {
  79. $sql = "UPDATE ".MAIN_DB_PREFIX."product";
  80. $sql .= " SET barcode = NULL";
  81. $resql = $db->query($sql);
  82. if ($resql) {
  83. setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
  84. } else {
  85. $error++;
  86. dol_print_error($db);
  87. }
  88. } else {
  89. $sql = "SELECT rowid, ref, fk_product_type";
  90. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  91. $sql .= " WHERE barcode IS NULL or barcode = ''";
  92. $sql .= $db->order("datec", "ASC");
  93. $sql .= $db->plimit($maxperinit);
  94. dol_syslog("codeinit", LOG_DEBUG);
  95. $resql = $db->query($sql);
  96. if ($resql) {
  97. $num = $db->num_rows($resql);
  98. $i = 0; $nbok = $nbtry = 0;
  99. while ($i < min($num, $maxperinit)) {
  100. $obj = $db->fetch_object($resql);
  101. if ($obj) {
  102. $productstatic->id = $obj->rowid;
  103. $productstatic->ref = $obj->ref;
  104. $productstatic->type = $obj->fk_product_type;
  105. $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
  106. //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
  107. $result = $productstatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'PRODUCT_MODIFY');
  108. $nbtry++;
  109. if ($result > 0) {
  110. $nbok++;
  111. }
  112. }
  113. $i++;
  114. }
  115. } else {
  116. $error++;
  117. dol_print_error($db);
  118. }
  119. if (!$error) {
  120. setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
  121. }
  122. }
  123. if (!$error) {
  124. //$db->rollback();
  125. $db->commit();
  126. } else {
  127. $db->rollback();
  128. }
  129. }
  130. $action = '';
  131. }
  132. /*
  133. * View
  134. */
  135. if (!$user->admin) {
  136. accessforbidden();
  137. }
  138. if (empty($conf->barcode->enabled)) {
  139. accessforbidden();
  140. }
  141. $form = new Form($db);
  142. llxHeader('', $langs->trans("MassBarcodeInit"));
  143. print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
  144. print '<br>';
  145. print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
  146. print '<br>';
  147. //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
  148. //print '<br>';
  149. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  150. print '<input type="hidden" name="mode" value="label">';
  151. print '<input type="hidden" name="action" value="initbarcodeproducts">';
  152. print '<input type="hidden" name="token" value="'.newToken().'">';
  153. print '<br>';
  154. // For thirdparty
  155. if (isModEnabled('societe')) {
  156. $nbno = $nbtotal = 0;
  157. print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
  158. print '<br>'."\n";
  159. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
  160. $resql = $db->query($sql);
  161. if ($resql) {
  162. $obj = $db->fetch_object($resql);
  163. $nbno = $obj->nb;
  164. } else {
  165. dol_print_error($db);
  166. }
  167. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
  168. $resql = $db->query($sql);
  169. if ($resql) {
  170. $obj = $db->fetch_object($resql);
  171. $nbtotal = $obj->nb;
  172. } else {
  173. dol_print_error($db);
  174. }
  175. print $langs->trans("CurrentlyNWithoutBarCode", $nbno, $nbtotal, $langs->transnoentitiesnoconv("ThirdParties")).'<br>'."\n";
  176. print '<br><input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" '.((GETPOST("selectorforbarcode") && GETPOST("selectorforbarcode")) ? '' : 'disabled ').'value="'.$langs->trans("InitEmptyBarCode", $nbno).'"';
  177. print ' title="'.dol_escape_htmltag($langs->trans("FeatureNotYetAvailable")).'" disabled';
  178. print '>';
  179. print '<br><br><br><br>';
  180. }
  181. // For products
  182. if ($conf->product->enabled || $conf->product->service) {
  183. // Example 1 : Adding jquery code
  184. print '<script type="text/javascript">
  185. function confirm_erase() {
  186. return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
  187. }
  188. </script>';
  189. $nbno = $nbtotal = 0;
  190. print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
  191. print '<br>'."\n";
  192. $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
  193. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  194. $sql .= " WHERE barcode IS NULL OR barcode = ''";
  195. $sql .= " GROUP BY fk_product_type, datec";
  196. $sql .= " ORDER BY datec";
  197. $resql = $db->query($sql);
  198. if ($resql) {
  199. $num = $db->num_rows($resql);
  200. $i = 0;
  201. while ($i < $num) {
  202. $obj = $db->fetch_object($resql);
  203. $nbno += $obj->nb;
  204. $i++;
  205. }
  206. } else {
  207. dol_print_error($db);
  208. }
  209. $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
  210. $resql = $db->query($sql);
  211. if ($resql) {
  212. $obj = $db->fetch_object($resql);
  213. $nbtotal = $obj->nb;
  214. } else {
  215. dol_print_error($db);
  216. }
  217. print $langs->trans("CurrentlyNWithoutBarCode", $nbno, $nbtotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
  218. if (is_object($modBarCodeProduct)) {
  219. print $langs->trans("BarCodeNumberManager").": ";
  220. $objproduct = new Product($db);
  221. print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
  222. $disabled = 0;
  223. print '<br>';
  224. } else {
  225. $disabled = 1;
  226. $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
  227. print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
  228. print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
  229. print '</div>';
  230. }
  231. if (empty($nbno)) {
  232. $disabled1 = 1;
  233. }
  234. //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
  235. $moretags1 = (($disabled || $disabled1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
  236. print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbno)).'"'.$moretags1.'>';
  237. $moretags2 = (($nbno == $nbtotal) ? ' disabled' : '');
  238. print ' &nbsp; ';
  239. print '<input type="submit" class="button butActionDelete" name="eraseallbarcode" id="eraseallbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretags2.' onClick="return confirm_erase();">';
  240. print '<br><br><br><br>';
  241. }
  242. print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
  243. print '<br>'."\n";
  244. print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
  245. print '</form>';
  246. print '<br>';
  247. // End of page
  248. llxFooter();
  249. $db->close();