printsheet.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
  4. * Copyright (C) 2006-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/barcode/printsheet.php
  21. * \ingroup member
  22. * \brief Page to print sheets with barcodes using the document templates into core/modules/printsheets
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/modules/printsheet/modules_labels.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
  29. $langs->load("admin");
  30. $langs->load("members");
  31. $langs->load("errors");
  32. // Choice of print year or current year.
  33. $now = dol_now();
  34. $year=dol_print_date($now,'%Y');
  35. $month=dol_print_date($now,'%m');
  36. $day=dol_print_date($now,'%d');
  37. $forbarcode=GETPOST('forbarcode');
  38. $fk_barcode_type=GETPOST('fk_barcode_type');
  39. $mode=GETPOST('mode');
  40. $modellabel=GETPOST("modellabel"); // Doc template to use
  41. $numberofsticker=GETPOST('numberofsticker','int');
  42. $mesg='';
  43. $action=GETPOST('action','aZ09');
  44. $producttmp=new Product($db);
  45. $thirdpartytmp=new Societe($db);
  46. /*
  47. * Actions
  48. */
  49. if (GETPOST('submitproduct') && GETPOST('submitproduct'))
  50. {
  51. $action=''; // We reset because we don't want to build doc
  52. if (GETPOST('productid') > 0)
  53. {
  54. $producttmp->fetch(GETPOST('productid'));
  55. $forbarcode=$producttmp->barcode;
  56. $fk_barcode_type=$producttmp->barcode_type;
  57. if (empty($fk_barcode_type) && ! empty($conf->global->PRODUIT_DEFAULT_BARCODE_TYPE)) $fk_barcode_type = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE;
  58. if (empty($forbarcode) || empty($fk_barcode_type))
  59. {
  60. setEventMessages($langs->trans("DefinitionOfBarCodeForProductNotComplete",$producttmp->getNomUrl()), null, 'warnings');
  61. }
  62. }
  63. }
  64. if (GETPOST('submitthirdparty') && GETPOST('submitthirdparty'))
  65. {
  66. $action=''; // We reset because we don't want to build doc
  67. if (GETPOST('socid') > 0)
  68. {
  69. $thirdpartytmp->fetch(GETPOST('socid'));
  70. $forbarcode=$thirdpartytmp->barcode;
  71. $fk_barcode_type=$thirdpartytmp->barcode_type_code;
  72. if (empty($fk_barcode_type) && ! empty($conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY)) $fk_barcode_type = $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY;
  73. if (empty($forbarcode) || empty($fk_barcode_type))
  74. {
  75. setEventMessages($langs->trans("DefinitionOfBarCodeForThirdpartyNotComplete",$thirdpartytmp->getNomUrl()), null, 'warnings');
  76. }
  77. }
  78. }
  79. if ($action == 'builddoc')
  80. {
  81. $result=0; $error=0;
  82. if (empty($forbarcode)) // barcode value
  83. {
  84. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BarcodeValue")), null, 'errors');
  85. $error++;
  86. }
  87. if (empty($fk_barcode_type)) // barcode type = barcode encoding
  88. {
  89. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BarcodeType")), null, 'errors');
  90. $error++;
  91. }
  92. if (! $error)
  93. {
  94. // Get encoder (barcode_type_coder) from barcode type id (barcode_type)
  95. $stdobject=new GenericObject($db);
  96. $stdobject->barcode_type=$fk_barcode_type;
  97. $result=$stdobject->fetch_barcode();
  98. if ($result <= 0)
  99. {
  100. $error++;
  101. setEventMessages('Failed to get bar code type information '.$stdobject->error, $stdobject->errors, 'errors');
  102. }
  103. }
  104. if (! $error)
  105. {
  106. $code=$forbarcode;
  107. $generator=$stdobject->barcode_type_coder; // coder (loaded by fetch_barcode). Engine.
  108. $encoding=strtoupper($stdobject->barcode_type_code); // code (loaded by fetch_barcode). Example 'ean', 'isbn', ...
  109. $diroutput=$conf->barcode->dir_temp;
  110. dol_mkdir($diroutput);
  111. // Generate barcode
  112. $dirbarcode=array_merge(array("/core/modules/barcode/doc/"),$conf->modules_parts['barcode']);
  113. foreach($dirbarcode as $reldir)
  114. {
  115. $dir=dol_buildpath($reldir,0);
  116. $newdir=dol_osencode($dir);
  117. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
  118. if (! is_dir($newdir)) continue;
  119. $result=@include_once $newdir.$generator.'.modules.php';
  120. if ($result) break;
  121. }
  122. // Load barcode class for generating barcode image
  123. $classname = "mod".ucfirst($generator);
  124. $module = new $classname($db);
  125. if ($generator != 'tcpdfbarcode')
  126. {
  127. // May be phpbarcode
  128. $template = 'standardlabel';
  129. $is2d = false;
  130. if ($module->encodingIsSupported($encoding))
  131. {
  132. $barcodeimage=$conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
  133. dol_delete_file($barcodeimage);
  134. // File is created with full name $barcodeimage = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
  135. $result=$module->writeBarCode($code,$encoding,'Y',4,1);
  136. if ($result <= 0 || ! dol_is_file($barcodeimage))
  137. {
  138. $error++;
  139. setEventMessages('Failed to generate image file of barcode for code='.$code.' encoding='.$encoding.' file='.basename($barcodeimage), null, 'errors');
  140. setEventMessages($module->error, null, 'errors');
  141. }
  142. }
  143. else
  144. {
  145. $error++;
  146. setEventMessages("Error, encoding ".$encoding." is not supported by encoder ".$generator.'. You must choose another barcode type or install a barcode generation engine that support '.$encoding, null, 'errors');
  147. }
  148. } else {
  149. $template = 'tcpdflabel';
  150. $encoding = $module->getTcpdfEncodingType($encoding); //convert to TCPDF compatible encoding types
  151. $is2d = $module->is2d;
  152. }
  153. }
  154. if (! $error)
  155. {
  156. // List of values to scan for a replacement
  157. $substitutionarray = array (
  158. '%LOGIN%'=>$user->login,
  159. '%COMPANY%'=>$mysoc->name,
  160. '%ADDRESS%'=>$mysoc->address,
  161. '%ZIP%'=>$mysoc->zip,
  162. '%TOWN%'=>$mysoc->town,
  163. '%COUNTRY%'=>$mysoc->country,
  164. '%COUNTRY_CODE%'=>$mysoc->country_code,
  165. '%EMAIL%'=>$mysoc->email,
  166. '%YEAR%'=>$year,
  167. '%MONTH%'=>$month,
  168. '%DAY%'=>$day,
  169. '%DOL_MAIN_URL_ROOT%'=>DOL_MAIN_URL_ROOT,
  170. '%SERVER%'=>"http://".$_SERVER["SERVER_NAME"]."/"
  171. );
  172. complete_substitutions_array($substitutionarray, $langs);
  173. // For labels
  174. if ($mode == 'label')
  175. {
  176. $txtforsticker="%PHOTO%"; // Photo will be barcode image, %BARCODE% posible when using TCPDF generator
  177. $textleft=make_substitutions((empty($conf->global->BARCODE_LABEL_LEFT_TEXT)?$txtforsticker:$conf->global->BARCODE_LABEL_LEFT_TEXT), $substitutionarray);
  178. $textheader=make_substitutions((empty($conf->global->BARCODE_LABEL_HEADER_TEXT)?'':$conf->global->BARCODE_LABEL_HEADER_TEXT), $substitutionarray);
  179. $textfooter=make_substitutions((empty($conf->global->BARCODE_LABEL_FOOTER_TEXT)?'':$conf->global->BARCODE_LABEL_FOOTER_TEXT), $substitutionarray);
  180. $textright=make_substitutions((empty($conf->global->BARCODE_LABEL_RIGHT_TEXT)?'':$conf->global->BARCODE_LABEL_RIGHT_TEXT), $substitutionarray);
  181. $forceimgscalewidth=(empty($conf->global->BARCODE_FORCEIMGSCALEWIDTH)?1:$conf->global->BARCODE_FORCEIMGSCALEWIDTH);
  182. $forceimgscaleheight=(empty($conf->global->BARCODE_FORCEIMGSCALEHEIGHT)?1:$conf->global->BARCODE_FORCEIMGSCALEHEIGHT);
  183. for ($i=0; $i < $numberofsticker; $i++)
  184. {
  185. $arrayofrecords[]=array(
  186. 'textleft'=>$textleft,
  187. 'textheader'=>$textheader,
  188. 'textfooter'=>$textfooter,
  189. 'textright'=>$textright,
  190. 'code'=>$code,
  191. 'encoding'=>$encoding,
  192. 'is2d'=>$is2d,
  193. 'photo'=>$barcodeimage // Photo must be a file that exists with format supported by TCPDF
  194. );
  195. }
  196. }
  197. $i++;
  198. $mesg='';
  199. // Build and output PDF
  200. if ($mode == 'label')
  201. {
  202. if (! count($arrayofrecords))
  203. {
  204. $mesg=$langs->trans("ErrorRecordNotFound");
  205. }
  206. if (empty($modellabel) || $modellabel == '-1')
  207. {
  208. $mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
  209. }
  210. if (! $mesg) $result=doc_label_pdf_create($db, $arrayofrecords, $modellabel, $outputlangs, $diroutput, $template, 'tmp_barcode_sheet.pdf');
  211. }
  212. if ($result <= 0)
  213. {
  214. dol_print_error('',$result);
  215. }
  216. if (! $mesg)
  217. {
  218. $db->close();
  219. exit;
  220. }
  221. }
  222. }
  223. /*
  224. * View
  225. */
  226. if (empty($conf->barcode->enabled)) accessforbidden();
  227. $form=new Form($db);
  228. llxHeader('',$langs->trans("BarCodePrintsheet"));
  229. print load_fiche_titre($langs->trans("BarCodePrintsheet"));
  230. print '<br>';
  231. print $langs->trans("PageToGenerateBarCodeSheets",$langs->transnoentitiesnoconv("BuildPageToPrint")).'<br>';
  232. print '<br>';
  233. dol_htmloutput_errors($mesg);
  234. //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
  235. //print '<br>';
  236. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  237. print '<input type="hidden" name="mode" value="label">';
  238. print '<input type="hidden" name="action" value="builddoc">';
  239. print '<div class="tagtable">';
  240. // Sheet format
  241. print ' <div class="tagtr">';
  242. print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  243. print $langs->trans("DescADHERENT_ETIQUETTE_TYPE").' &nbsp; ';
  244. print '</div><div class="tagtd maxwidthonsmartphone" style="overflow: hidden; white-space: nowrap;">';
  245. // List of possible labels (defined into $_Avery_Labels variable set into core/lib/format_cards.lib.php)
  246. $arrayoflabels=array();
  247. foreach(array_keys($_Avery_Labels) as $codecards)
  248. {
  249. $labeltoshow=$_Avery_Labels[$codecards]['name'];
  250. //$labeltoshow.=' ('.$_Avery_Labels[$row['code']]['paper-size'].')';
  251. $arrayoflabels[$codecards]=$labeltoshow;
  252. $arrayoflabels[$codecards]=$_Avery_Labels[$codecards]['name'];
  253. }
  254. print $form->selectarray('modellabel',$arrayoflabels,(GETPOST('modellabel')?GETPOST('modellabel'):$conf->global->ADHERENT_ETIQUETTE_TYPE),1,0,0);
  255. print '</div></div>';
  256. // Number of stickers to print
  257. print ' <div class="tagtr">';
  258. print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  259. print $langs->trans("NumberOfStickers").' &nbsp; ';
  260. print '</div><div class="tagtd maxwidthonsmartphone" style="overflow: hidden; white-space: nowrap;">';
  261. print '<input size="4" type="text" name="numberofsticker" value="'.(GETPOST('numberofsticker')?GETPOST('numberofsticker','int'):10).'">';
  262. print '</div></div>';
  263. print '</div>';
  264. print '<br>';
  265. // Add javascript to make choice dynamic
  266. print '<script type="text/javascript" language="javascript">
  267. jQuery(document).ready(function() {
  268. function init_selectors()
  269. {
  270. if (jQuery("#fillmanually:checked").val() == "fillmanually")
  271. {
  272. jQuery("#submitproduct").prop("disabled", true);
  273. jQuery("#submitthirdparty").prop("disabled", true);
  274. jQuery("#search_productid").prop("disabled", true);
  275. jQuery("#socid").prop("disabled", true);
  276. jQuery(".showforproductselector").hide();
  277. jQuery(".showforthirdpartyselector").hide();
  278. }
  279. if (jQuery("#fillfromproduct:checked").val() == "fillfromproduct")
  280. {
  281. jQuery("#submitproduct").removeAttr("disabled");
  282. jQuery("#submitthirdparty").prop("disabled", true);
  283. jQuery("#search_productid").removeAttr("disabled");
  284. jQuery("#socid").prop("disabled", true);
  285. jQuery(".showforproductselector").show();
  286. jQuery(".showforthirdpartyselector").hide();
  287. }
  288. if (jQuery("#fillfromthirdparty:checked").val() == "fillfromthirdparty")
  289. {
  290. jQuery("#submitproduct").prop("disabled", true);
  291. jQuery("#submitthirdparty").removeAttr("disabled");
  292. jQuery("#search_productid").prop("disabled", true);
  293. jQuery("#socid").removeAttr("disabled");
  294. jQuery(".showforproductselector").hide();
  295. jQuery(".showforthirdpartyselector").show();
  296. }
  297. }
  298. init_selectors();
  299. jQuery(".radiobarcodeselect").click(function() {
  300. init_selectors();
  301. });
  302. function init_gendoc_button()
  303. {
  304. if (jQuery("#select_fk_barcode_type").val() > 0 && jQuery("#forbarcode").val())
  305. {
  306. jQuery("#submitformbarcodegen").removeAttr("disabled");
  307. }
  308. else
  309. {
  310. jQuery("#submitformbarcodegen").prop("disabled", true);
  311. }
  312. }
  313. init_gendoc_button();
  314. jQuery("#select_fk_barcode_type").change(function() {
  315. init_gendoc_button();
  316. });
  317. jQuery("#forbarcode").keyup(function() {
  318. init_gendoc_button()
  319. });
  320. });
  321. </script>';
  322. // Checkbox to select from free text
  323. print '<input id="fillmanually" type="radio" '.((! GETPOST("selectorforbarcode") || GETPOST("selectorforbarcode")=='fillmanually')?'checked ':'').'name="selectorforbarcode" value="fillmanually" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueManually").' &nbsp; ';
  324. print '<br>';
  325. if (! empty($user->rights->produit->lire) || ! empty($user->rights->service->lire))
  326. {
  327. print '<input id="fillfromproduct" type="radio" '.((GETPOST("selectorforbarcode")=='fillfromproduct')?'checked ':'').'name="selectorforbarcode" value="fillfromproduct" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueFromProduct").' &nbsp; ';
  328. print '<br>';
  329. print '<div class="showforproductselector">';
  330. $form->select_produits(GETPOST('productid'), 'productid', '');
  331. print ' &nbsp; <input type="submit" id="submitproduct" name="submitproduct" class="button" value="'.(dol_escape_htmltag($langs->trans("GetBarCode"))).'">';
  332. print '</div>';
  333. }
  334. if (! empty($user->rights->societe->lire))
  335. {
  336. print '<input id="fillfromthirdparty" type="radio" '.((GETPOST("selectorforbarcode")=='fillfromthirdparty')?'checked ':'').'name="selectorforbarcode" value="fillfromthirdparty" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueFromThirdParty").' &nbsp; ';
  337. print '<br>';
  338. print '<div class="showforthirdpartyselector">';
  339. print $form->select_company(GETPOST('socid'), 'socid', '', 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300');
  340. print ' &nbsp; <input type="submit" id="submitthirdparty" name="submitthirdparty" class="button showforthirdpartyselector" value="'.(dol_escape_htmltag($langs->trans("GetBarCode"))).'">';
  341. print '</div>';
  342. }
  343. print '<br>';
  344. if ($producttmp->id > 0)
  345. {
  346. print $langs->trans("BarCodeDataForProduct",'').' '.$producttmp->getNomUrl(1).'<br>';
  347. }
  348. if ($thirdpartytmp->id > 0)
  349. {
  350. print $langs->trans("BarCodeDataForThirdparty",'').' '.$thirdpartytmp->getNomUrl(1).'<br>';
  351. }
  352. print '<div class="tagtable">';
  353. // Barcode type
  354. print ' <div class="tagtr">';
  355. print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  356. print $langs->trans("BarcodeType").' &nbsp; ';
  357. print '</div><div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  358. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formbarcode.class.php';
  359. $formbarcode = new FormBarCode($db);
  360. $formbarcode->select_barcode_type($fk_barcode_type, 'fk_barcode_type', 1);
  361. print '</div></div>';
  362. // Barcode value
  363. print ' <div class="tagtr">';
  364. print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  365. print $langs->trans("BarcodeValue").' &nbsp; ';
  366. print '</div><div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  367. print '<input size="16" type="text" name="forbarcode" id="forbarcode" value="'.$forbarcode.'">';
  368. print '</div></div>';
  369. /*
  370. $barcodestickersmask=GETPOST('barcodestickersmask');
  371. print '<br>'.$langs->trans("BarcodeStickersMask").':<br>';
  372. print '<textarea cols="40" type="text" name="barcodestickersmask" value="'.GETPOST('barcodestickersmask').'">'.$barcodestickersmask.'</textarea>';
  373. print '<br>';
  374. */
  375. print '</div>';
  376. print '<br><input class="button" type="submit" id="submitformbarcodegen" '.((GETPOST("selectorforbarcode") && GETPOST("selectorforbarcode"))?'':'disabled ').'value="'.$langs->trans("BuildPageToPrint").'">';
  377. print '</form>';
  378. print '<br>';
  379. llxFooter();
  380. $db->close();