PageRenderTime 35ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/barcode/printsheet.php

http://github.com/Dolibarr/dolibarr
PHP | 443 lines | 319 code | 66 blank | 58 comment | 60 complexity | 7ab60286c77f402ea0395dde8330b9a3 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT
  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 <https://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. if (!empty($_POST['mode']) && $_POST['mode'] === 'label') { // Page is called to build a PDF and output, we must ne renew the token.
  25. if (!defined('NOTOKENRENEWAL')) {
  26. define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  27. }
  28. }
  29. require '../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/modules/printsheet/modules_labels.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array('admin', 'members', 'errors'));
  36. // Choice of print year or current year.
  37. $now = dol_now();
  38. $year = dol_print_date($now, '%Y');
  39. $month = dol_print_date($now, '%m');
  40. $day = dol_print_date($now, '%d');
  41. $forbarcode = GETPOST('forbarcode', 'alphanohtml');
  42. $fk_barcode_type = GETPOST('fk_barcode_type', 'int');
  43. $mode = GETPOST('mode', 'aZ09');
  44. $modellabel = GETPOST("modellabel", 'aZ09'); // Doc template to use
  45. $numberofsticker = GETPOST('numberofsticker', 'int');
  46. $mesg = '';
  47. $action = GETPOST('action', 'aZ09');
  48. $producttmp = new Product($db);
  49. $thirdpartytmp = new Societe($db);
  50. /*
  51. * Actions
  52. */
  53. if (GETPOST('submitproduct') && GETPOST('submitproduct')) {
  54. $action = ''; // We reset because we don't want to build doc
  55. if (GETPOST('productid', 'int') > 0) {
  56. $result = $producttmp->fetch(GETPOST('productid', 'int'));
  57. if ($result < 0) {
  58. setEventMessage($producttmp->error, 'errors');
  59. }
  60. $forbarcode = $producttmp->barcode;
  61. $fk_barcode_type = $producttmp->barcode_type;
  62. if (empty($fk_barcode_type) && !empty($conf->global->PRODUIT_DEFAULT_BARCODE_TYPE)) {
  63. $fk_barcode_type = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE;
  64. }
  65. if (empty($forbarcode) || empty($fk_barcode_type)) {
  66. setEventMessages($langs->trans("DefinitionOfBarCodeForProductNotComplete", $producttmp->getNomUrl()), null, 'warnings');
  67. }
  68. }
  69. }
  70. if (GETPOST('submitthirdparty') && GETPOST('submitthirdparty')) {
  71. $action = ''; // We reset because we don't want to build doc
  72. if (GETPOST('socid', 'int') > 0) {
  73. $thirdpartytmp->fetch(GETPOST('socid', 'int'));
  74. $forbarcode = $thirdpartytmp->barcode;
  75. $fk_barcode_type = $thirdpartytmp->barcode_type_code;
  76. if (empty($fk_barcode_type) && !empty($conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY)) {
  77. $fk_barcode_type = $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY;
  78. }
  79. if (empty($forbarcode) || empty($fk_barcode_type)) {
  80. setEventMessages($langs->trans("DefinitionOfBarCodeForThirdpartyNotComplete", $thirdpartytmp->getNomUrl()), null, 'warnings');
  81. }
  82. }
  83. }
  84. if ($action == 'builddoc') {
  85. $result = 0; $error = 0;
  86. if (empty($forbarcode)) { // barcode value
  87. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BarcodeValue")), null, 'errors');
  88. $error++;
  89. }
  90. if (empty($fk_barcode_type)) { // barcode type = barcode encoding
  91. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BarcodeType")), null, 'errors');
  92. $error++;
  93. }
  94. if (!$error) {
  95. // Get encoder (barcode_type_coder) from barcode type id (barcode_type)
  96. $stdobject = new GenericObject($db);
  97. $stdobject->barcode_type = $fk_barcode_type;
  98. $result = $stdobject->fetch_barcode();
  99. if ($result <= 0) {
  100. $error++;
  101. setEventMessages('Failed to get bar code type information '.$stdobject->error, $stdobject->errors, 'errors');
  102. }
  103. }
  104. if (!$error) {
  105. $code = $forbarcode;
  106. $generator = $stdobject->barcode_type_coder; // coder (loaded by fetch_barcode). Engine.
  107. $encoding = strtoupper($stdobject->barcode_type_code); // code (loaded by fetch_barcode). Example 'ean', 'isbn', ...
  108. $diroutput = $conf->barcode->dir_temp;
  109. dol_mkdir($diroutput);
  110. // Generate barcode
  111. $dirbarcode = array_merge(array("/core/modules/barcode/doc/"), $conf->modules_parts['barcode']);
  112. foreach ($dirbarcode as $reldir) {
  113. $dir = dol_buildpath($reldir, 0);
  114. $newdir = dol_osencode($dir);
  115. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
  116. if (!is_dir($newdir)) {
  117. continue;
  118. }
  119. $result = @include_once $newdir.$generator.'.modules.php';
  120. if ($result) {
  121. break;
  122. }
  123. }
  124. // Load barcode class for generating barcode image
  125. $classname = "mod".ucfirst($generator);
  126. $module = new $classname($db);
  127. if ($generator != 'tcpdfbarcode') {
  128. // May be phpbarcode
  129. $template = 'standardlabel';
  130. $is2d = false;
  131. if ($module->encodingIsSupported($encoding)) {
  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. $error++;
  138. setEventMessages('Failed to generate image file of barcode for code='.$code.' encoding='.$encoding.' file='.basename($barcodeimage), null, 'errors');
  139. setEventMessages($module->error, null, 'errors');
  140. }
  141. } else {
  142. $error++;
  143. 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');
  144. }
  145. } else {
  146. $template = 'tcpdflabel';
  147. $encoding = $module->getTcpdfEncodingType($encoding); //convert to TCPDF compatible encoding types
  148. $is2d = $module->is2d;
  149. }
  150. }
  151. if (!$error) {
  152. // List of values to scan for a replacement
  153. $substitutionarray = array(
  154. '%LOGIN%' => $user->login,
  155. '%COMPANY%' => $mysoc->name,
  156. '%ADDRESS%' => $mysoc->address,
  157. '%ZIP%' => $mysoc->zip,
  158. '%TOWN%' => $mysoc->town,
  159. '%COUNTRY%' => $mysoc->country,
  160. '%COUNTRY_CODE%' => $mysoc->country_code,
  161. '%EMAIL%' => $mysoc->email,
  162. '%YEAR%' => $year,
  163. '%MONTH%' => $month,
  164. '%DAY%' => $day,
  165. '%DOL_MAIN_URL_ROOT%' => DOL_MAIN_URL_ROOT,
  166. '%SERVER%' => "http://".$_SERVER["SERVER_NAME"]."/",
  167. );
  168. complete_substitutions_array($substitutionarray, $langs);
  169. // For labels
  170. if ($mode == 'label') {
  171. $txtforsticker = "%PHOTO%"; // Photo will be barcode image, %BARCODE% posible when using TCPDF generator
  172. $textleft = make_substitutions((empty($conf->global->BARCODE_LABEL_LEFT_TEXT) ? $txtforsticker : $conf->global->BARCODE_LABEL_LEFT_TEXT), $substitutionarray);
  173. $textheader = make_substitutions((empty($conf->global->BARCODE_LABEL_HEADER_TEXT) ? '' : $conf->global->BARCODE_LABEL_HEADER_TEXT), $substitutionarray);
  174. $textfooter = make_substitutions((empty($conf->global->BARCODE_LABEL_FOOTER_TEXT) ? '' : $conf->global->BARCODE_LABEL_FOOTER_TEXT), $substitutionarray);
  175. $textright = make_substitutions((empty($conf->global->BARCODE_LABEL_RIGHT_TEXT) ? '' : $conf->global->BARCODE_LABEL_RIGHT_TEXT), $substitutionarray);
  176. $forceimgscalewidth = (empty($conf->global->BARCODE_FORCEIMGSCALEWIDTH) ? 1 : $conf->global->BARCODE_FORCEIMGSCALEWIDTH);
  177. $forceimgscaleheight = (empty($conf->global->BARCODE_FORCEIMGSCALEHEIGHT) ? 1 : $conf->global->BARCODE_FORCEIMGSCALEHEIGHT);
  178. for ($i = 0; $i < $numberofsticker; $i++) {
  179. $arrayofrecords[] = array(
  180. 'textleft'=>$textleft,
  181. 'textheader'=>$textheader,
  182. 'textfooter'=>$textfooter,
  183. 'textright'=>$textright,
  184. 'code'=>$code,
  185. 'encoding'=>$encoding,
  186. 'is2d'=>$is2d,
  187. 'photo'=>$barcodeimage // Photo must be a file that exists with format supported by TCPDF
  188. );
  189. }
  190. }
  191. $i++;
  192. $mesg = '';
  193. // Build and output PDF
  194. if ($mode == 'label') {
  195. if (!count($arrayofrecords)) {
  196. $mesg = $langs->trans("ErrorRecordNotFound");
  197. }
  198. if (empty($modellabel) || $modellabel == '-1') {
  199. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
  200. }
  201. $outfile = $langs->trans("BarCode").'_sheets_'.dol_print_date(dol_now(), 'dayhourlog').'.pdf';
  202. if (!$mesg) {
  203. $outputlangs = $langs;
  204. // This generates and send PDF to output
  205. // TODO Move
  206. $result = doc_label_pdf_create($db, $arrayofrecords, $modellabel, $outputlangs, $diroutput, $template, dol_sanitizeFileName($outfile));
  207. }
  208. }
  209. if ($result <= 0 || $mesg) {
  210. if (empty($mesg)) {
  211. $mesg = 'Error '.$result;
  212. }
  213. setEventMessages($mesg, null, 'errors');
  214. } else {
  215. $db->close();
  216. exit;
  217. }
  218. }
  219. }
  220. /*
  221. * View
  222. */
  223. if (empty($conf->barcode->enabled)) {
  224. accessforbidden();
  225. }
  226. $form = new Form($db);
  227. llxHeader('', $langs->trans("BarCodePrintsheet"));
  228. print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'barcode');
  229. print '<br>';
  230. print '<span class="opacitymedium">'.$langs->trans("PageToGenerateBarCodeSheets", $langs->transnoentitiesnoconv("BuildPageToPrint")).'</span><br>';
  231. print '<br>';
  232. dol_htmloutput_errors($mesg);
  233. //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
  234. //print '<br>';
  235. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'; // The target is for brothers that open the file instead of downloading it
  236. print '<input type="hidden" name="mode" value="label">';
  237. print '<input type="hidden" name="action" value="builddoc">';
  238. print '<input type="hidden" name="token" value="'.currentToken().'">'; // The page will not renew the token but force download of a file, so we must use here currentToken
  239. print '<div class="tagtable">';
  240. // Sheet format
  241. print ' <div class="tagtr">';
  242. print ' <div class="tagtd">';
  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. $labeltoshow = $_Avery_Labels[$codecards]['name'];
  249. //$labeltoshow.=' ('.$_Avery_Labels[$row['code']]['paper-size'].')';
  250. $arrayoflabels[$codecards] = $labeltoshow;
  251. }
  252. asort($arrayoflabels);
  253. print $form->selectarray('modellabel', $arrayoflabels, (GETPOST('modellabel') ?GETPOST('modellabel') : $conf->global->ADHERENT_ETIQUETTE_TYPE), 1, 0, 0, '', 0, 0, 0, '', '', 1);
  254. print '</div></div>';
  255. // Number of stickers to print
  256. print ' <div class="tagtr">';
  257. print ' <div class="tagtd">';
  258. print $langs->trans("NumberOfStickers").' &nbsp; ';
  259. print '</div><div class="tagtd maxwidthonsmartphone" style="overflow: hidden; white-space: nowrap;">';
  260. print '<input size="4" type="text" name="numberofsticker" value="'.(GETPOST('numberofsticker') ?GETPOST('numberofsticker', 'int') : 10).'">';
  261. print '</div></div>';
  262. print '</div>';
  263. print '<br>';
  264. // Add javascript to make choice dynamic
  265. print '<script type="text/javascript" language="javascript">
  266. jQuery(document).ready(function() {
  267. function init_selectors()
  268. {
  269. if (jQuery("#fillmanually:checked").val() == "fillmanually")
  270. {
  271. jQuery("#submitproduct").prop("disabled", true);
  272. jQuery("#submitthirdparty").prop("disabled", true);
  273. jQuery("#search_productid").prop("disabled", true);
  274. jQuery("#socid").prop("disabled", true);
  275. jQuery(".showforproductselector").hide();
  276. jQuery(".showforthirdpartyselector").hide();
  277. }
  278. if (jQuery("#fillfromproduct:checked").val() == "fillfromproduct")
  279. {
  280. jQuery("#submitproduct").removeAttr("disabled");
  281. jQuery("#submitthirdparty").prop("disabled", true);
  282. jQuery("#search_productid").removeAttr("disabled");
  283. jQuery("#socid").prop("disabled", true);
  284. jQuery(".showforproductselector").show();
  285. jQuery(".showforthirdpartyselector").hide();
  286. }
  287. if (jQuery("#fillfromthirdparty:checked").val() == "fillfromthirdparty")
  288. {
  289. jQuery("#submitproduct").prop("disabled", true);
  290. jQuery("#submitthirdparty").removeAttr("disabled");
  291. jQuery("#search_productid").prop("disabled", true);
  292. jQuery("#socid").removeAttr("disabled");
  293. jQuery(".showforproductselector").hide();
  294. jQuery(".showforthirdpartyselector").show();
  295. }
  296. }
  297. init_selectors();
  298. jQuery(".radiobarcodeselect").click(function() {
  299. init_selectors();
  300. });
  301. function init_gendoc_button()
  302. {
  303. if (jQuery("#select_fk_barcode_type").val() > 0 && jQuery("#forbarcode").val())
  304. {
  305. jQuery("#submitformbarcodegen").removeAttr("disabled");
  306. }
  307. else
  308. {
  309. jQuery("#submitformbarcodegen").prop("disabled", true);
  310. }
  311. }
  312. init_gendoc_button();
  313. jQuery("#select_fk_barcode_type").change(function() {
  314. init_gendoc_button();
  315. });
  316. jQuery("#forbarcode").keyup(function() {
  317. init_gendoc_button()
  318. });
  319. });
  320. </script>';
  321. // Checkbox to select from free text
  322. print '<input id="fillmanually" type="radio" '.((!GETPOST("selectorforbarcode") || GETPOST("selectorforbarcode") == 'fillmanually') ? 'checked ' : '').'name="selectorforbarcode" value="fillmanually" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueManually").' &nbsp; ';
  323. print '<br>';
  324. if (!empty($user->rights->produit->lire) || !empty($user->rights->service->lire)) {
  325. print '<input id="fillfromproduct" type="radio" '.((GETPOST("selectorforbarcode") == 'fillfromproduct') ? 'checked ' : '').'name="selectorforbarcode" value="fillfromproduct" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueFromProduct").' &nbsp; ';
  326. print '<br>';
  327. print '<div class="showforproductselector">';
  328. $form->select_produits(GETPOST('productid', 'int'), 'productid', '', '', 0, -1, 2, '', 0, array(), 0, '1', 0, 'minwidth400imp', 1);
  329. print ' &nbsp; <input type="submit" class="button" id="submitproduct" name="submitproduct" value="'.(dol_escape_htmltag($langs->trans("GetBarCode"))).'">';
  330. print '</div>';
  331. }
  332. if (!empty($user->rights->societe->lire)) {
  333. print '<input id="fillfromthirdparty" type="radio" '.((GETPOST("selectorforbarcode") == 'fillfromthirdparty') ? 'checked ' : '').'name="selectorforbarcode" value="fillfromthirdparty" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueFromThirdParty").' &nbsp; ';
  334. print '<br>';
  335. print '<div class="showforthirdpartyselector">';
  336. print $form->select_company(GETPOST('socid', 'int'), 'socid', '', 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300');
  337. print ' &nbsp; <input type="submit" id="submitthirdparty" name="submitthirdparty" class="button showforthirdpartyselector" value="'.(dol_escape_htmltag($langs->trans("GetBarCode"))).'">';
  338. print '</div>';
  339. }
  340. print '<br>';
  341. if ($producttmp->id > 0) {
  342. print $langs->trans("BarCodeDataForProduct", '').' '.$producttmp->getNomUrl(1).'<br>';
  343. }
  344. if ($thirdpartytmp->id > 0) {
  345. print $langs->trans("BarCodeDataForThirdparty", '').' '.$thirdpartytmp->getNomUrl(1).'<br>';
  346. }
  347. print '<div class="tagtable">';
  348. // Barcode type
  349. print ' <div class="tagtr">';
  350. print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  351. print $langs->trans("BarcodeType").' &nbsp; ';
  352. print '</div><div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  353. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formbarcode.class.php';
  354. $formbarcode = new FormBarCode($db);
  355. print $formbarcode->selectBarcodeType($fk_barcode_type, 'fk_barcode_type', 1);
  356. print '</div></div>';
  357. // Barcode value
  358. print ' <div class="tagtr">';
  359. print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  360. print $langs->trans("BarcodeValue").' &nbsp; ';
  361. print '</div><div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
  362. print '<input size="16" type="text" name="forbarcode" id="forbarcode" value="'.$forbarcode.'">';
  363. print '</div></div>';
  364. /*
  365. $barcodestickersmask=GETPOST('barcodestickersmask');
  366. print '<br>'.$langs->trans("BarcodeStickersMask").':<br>';
  367. print '<textarea cols="40" type="text" name="barcodestickersmask" value="'.GETPOST('barcodestickersmask').'">'.$barcodestickersmask.'</textarea>';
  368. print '<br>';
  369. */
  370. print '</div>';
  371. print '<br><input type="submit" class="button" id="submitformbarcodegen" '.((GETPOST("selectorforbarcode") && GETPOST("selectorforbarcode")) ? '' : 'disabled ').'value="'.$langs->trans("BuildPageToPrint").'">';
  372. print '</form>';
  373. print '<br>';
  374. // End of page
  375. llxFooter();
  376. $db->close();