codeinit.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /* Copyright (C) 2014-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/barcode/codeinit.php
  19. * \ingroup member
  20. * \brief Page to make mass init of barcode
  21. */
  22. require '../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  24. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  25. $langs->load("admin");
  26. $langs->load("members");
  27. $langs->load("errors");
  28. $langs->load("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. $eraseallbarcode=GETPOST('eraseallbarcode');
  37. $action=GETPOST('action');
  38. $producttmp=new Product($db);
  39. $thirdpartytmp=new Societe($db);
  40. $modBarCodeProduct='';
  41. $maxperinit=1000;
  42. /*
  43. * Actions
  44. */
  45. // Define barcode template for products
  46. if (! empty($conf->global->BARCODE_PRODUCT_ADDON_NUM))
  47. {
  48. $dirbarcodenum=array_merge(array('/core/modules/barcode/'),$conf->modules_parts['barcode']);
  49. foreach ($dirbarcodenum as $dirroot)
  50. {
  51. $dir = dol_buildpath($dirroot,0);
  52. $handle = @opendir($dir);
  53. if (is_resource($handle))
  54. {
  55. while (($file = readdir($handle))!==false)
  56. {
  57. if (preg_match('/^mod_barcode_product_.*php$/', $file))
  58. {
  59. $file = substr($file, 0, dol_strlen($file)-4);
  60. try {
  61. dol_include_once($dirroot.$file.'.php');
  62. }
  63. catch(Exception $e)
  64. {
  65. dol_syslog($e->getMessage(), LOG_ERR);
  66. }
  67. $modBarCodeProduct = new $file();
  68. break;
  69. }
  70. }
  71. closedir($handle);
  72. }
  73. }
  74. }
  75. if ($action == 'initbarcodeproducts')
  76. {
  77. if (! is_object($modBarCodeProduct))
  78. {
  79. $error++;
  80. setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
  81. }
  82. if (! $error)
  83. {
  84. $productstatic=new Product($db);
  85. $db->begin();
  86. $nbok=0;
  87. if (! empty($eraseallbarcode))
  88. {
  89. $sql ="UPDATE ".MAIN_DB_PREFIX."product";
  90. $sql.=" SET barcode = NULL";
  91. $resql=$db->query($sql);
  92. if ($resql)
  93. {
  94. setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
  95. }
  96. else
  97. {
  98. $error++;
  99. dol_print_error($db);
  100. }
  101. }
  102. else
  103. {
  104. $sql ="SELECT rowid, ref, fk_product_type";
  105. $sql.=" FROM ".MAIN_DB_PREFIX."product";
  106. $sql.=" WHERE barcode IS NULL or barcode = ''";
  107. $sql.=$db->order("datec","ASC");
  108. $sql.=$db->plimit($maxperinit);
  109. dol_syslog("codeinit", LOG_DEBUG);
  110. $resql=$db->query($sql);
  111. if ($resql)
  112. {
  113. $num=$db->num_rows($resql);
  114. $i=0; $nbok=$nbtry=0;
  115. while ($i < min($num,$maxperinit))
  116. {
  117. $obj=$db->fetch_object($resql);
  118. if ($obj)
  119. {
  120. $productstatic->id=$obj->rowid;
  121. $productstatic->ref=$obj->ref;
  122. $productstatic->type=$obj->fk_product_type;
  123. $nextvalue=$modBarCodeProduct->getNextValue($productstatic,'');
  124. //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
  125. $result=$productstatic->setValueFrom('barcode', $nextvalue);
  126. $nbtry++;
  127. if ($result > 0) $nbok++;
  128. }
  129. $i++;
  130. }
  131. }
  132. else
  133. {
  134. $error++;
  135. dol_print_error($db);
  136. }
  137. if (! $error)
  138. {
  139. setEventMessages($langs->trans("RecordsModified",$nbok), null, 'mesgs');
  140. }
  141. }
  142. if (! $error)
  143. {
  144. //$db->rollback();
  145. $db->commit();
  146. }
  147. else
  148. {
  149. $db->rollback();
  150. }
  151. }
  152. $action='';
  153. }
  154. /*
  155. * View
  156. */
  157. if (!$user->admin) accessforbidden();
  158. if (empty($conf->barcode->enabled)) accessforbidden();
  159. $form=new Form($db);
  160. llxHeader('',$langs->trans("MassBarcodeInit"));
  161. print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
  162. print '<br>';
  163. print $langs->trans("MassBarcodeInitDesc").'<br>';
  164. print '<br>';
  165. //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
  166. //print '<br>';
  167. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  168. print '<input type="hidden" name="mode" value="label">';
  169. print '<input type="hidden" name="action" value="initbarcodeproducts">';
  170. print '<br>';
  171. // For thirdparty
  172. if ($conf->societe->enabled)
  173. {
  174. $nbno=$nbtotal=0;
  175. print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"),'','object_company');
  176. print '<br>'."\n";
  177. $sql="SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
  178. $resql=$db->query($sql);
  179. if ($resql)
  180. {
  181. $obj=$db->fetch_object($resql);
  182. $nbno=$obj->nb;
  183. }
  184. else dol_print_error($db);
  185. $sql="SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
  186. $resql=$db->query($sql);
  187. if ($resql)
  188. {
  189. $obj=$db->fetch_object($resql);
  190. $nbtotal=$obj->nb;
  191. }
  192. else dol_print_error($db);
  193. print $langs->trans("CurrentlyNWithoutBarCode", $nbno, $nbtotal, $langs->transnoentitiesnoconv("ThirdParties")).'<br>'."\n";
  194. print '<br><input class="button" type="submit" id="submitformbarcodethirdpartygen" '.((GETPOST("selectorforbarcode") && GETPOST("selectorforbarcode"))?'':'disabled ').'value="'.$langs->trans("InitEmptyBarCode",$nbno).'"';
  195. print ' title="'.dol_escape_htmltag($langs->trans("FeatureNotYetAvailable")).'" disabled';
  196. print '>';
  197. print '<br><br><br><br>';
  198. }
  199. // For products
  200. if ($conf->product->enabled || $conf->product->service)
  201. {
  202. // Example 1 : Adding jquery code
  203. print '<script type="text/javascript" language="javascript">
  204. function confirm_erase() {
  205. return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
  206. }
  207. </script>';
  208. $nbno=$nbtotal=0;
  209. print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"),'','object_product');
  210. print '<br>'."\n";
  211. $sql ="SELECT count(rowid) as nb, fk_product_type, datec";
  212. $sql.=" FROM ".MAIN_DB_PREFIX."product";
  213. $sql.=" WHERE barcode IS NULL OR barcode = ''";
  214. $sql.=" GROUP BY fk_product_type, datec";
  215. $sql.=" ORDER BY datec";
  216. $resql=$db->query($sql);
  217. if ($resql)
  218. {
  219. $num=$db->num_rows($resql);
  220. $i=0;
  221. while($i < $num)
  222. {
  223. $obj=$db->fetch_object($resql);
  224. $nbno+=$obj->nb;
  225. $i++;
  226. }
  227. }
  228. else dol_print_error($db);
  229. $sql="SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
  230. $resql=$db->query($sql);
  231. if ($resql)
  232. {
  233. $obj=$db->fetch_object($resql);
  234. $nbtotal=$obj->nb;
  235. }
  236. else dol_print_error($db);
  237. print $langs->trans("CurrentlyNWithoutBarCode", $nbno, $nbtotal, $langs->transnoentitiesnoconv("ProductsOrServices")).'<br>'."\n";
  238. if (is_object($modBarCodeProduct))
  239. {
  240. print $langs->trans("BarCodeNumberManager").": ";
  241. $objproduct=new Product($db);
  242. print '<b>'.(isset($modBarCodeProduct->name)?$modBarCodeProduct->name:$modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
  243. $disabled=0;
  244. }
  245. else
  246. {
  247. $disabled=1;
  248. $titleno=$langs->trans("NoBarcodeNumberingTemplateDefined");
  249. print '<font class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined").'</font> (<a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>)<br>';
  250. }
  251. if (empty($nbno))
  252. {
  253. $disabled1=1;
  254. }
  255. print '<br>';
  256. //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
  257. $moretags1=(($disabled||$disabled1)?' disabled title="'.dol_escape_htmltag($titleno).'"':'');
  258. print '<input class="button" type="submit" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode",min($maxperinit,$nbno)).'"'.$moretags1.'>';
  259. $moretags2=(($nbno == $nbtotal)?' disabled':'');
  260. print ' &nbsp; ';
  261. print '<input class="button" type="submit" name="eraseallbarcode" id="eraseallbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretags2.' onClick="return confirm_erase();">';
  262. print '<br><br><br><br>';
  263. }
  264. print '</form>';
  265. print '<br>';
  266. llxFooter();
  267. $db->close();