codeinit.php 8.9 KB

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