dolreceiptprinter.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <?php
  2. /*
  3. * Copyright (C) 2015-2018 Frédéric France <frederic.france@free.fr>
  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 <http://www.gnu.org/licenses/>.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/class/dolreceiptprinter.class.php
  21. * \brief Print receipt ticket on various ESC/POS printer
  22. */
  23. /*
  24. * Tags for ticket template
  25. *
  26. * <dol_align_left> Left align text
  27. * <dol_align_center> Center text
  28. * <dol_align_right> Right align text
  29. * <dol_use_font_a> Use font A of printer
  30. * <dol_use_font_b> Use font B of printer
  31. * <dol_use_font_c> Use font C of printer
  32. * <dol_bold> </dol_bold> Text Bold
  33. * <dol_double_height> </dol_double_height> Text double height
  34. * <dol_double_width> </dol_double_width> Text double width
  35. * <dol_underline> </dol_underline> Underline text
  36. * <dol_underline_2dots> </dol_underline_2dots> Underline with double line
  37. * <dol_emphasized> </dol_emphasized> Emphasized text
  38. * <dol_switch_colors> </dol_switch_colors> Print in white on black
  39. * <dol_print_barcode> Print barcode
  40. * <dol_print_barcode_customer_id> Print barcode customer id
  41. * <dol_set_print_width_57> Ticket print width of 57mm
  42. * <dol_cut_paper_full> Cut ticket completely
  43. * <dol_cut_paper_partial> Cut ticket partially
  44. * <dol_open_drawer> Open cash drawer
  45. * <dol_activate_buzzer> Activate buzzer
  46. *
  47. * Code which can be placed everywhere
  48. * <dol_print_qrcode> Print QR Code
  49. * <dol_print_date> Print date AAAA-MM-DD
  50. * <dol_print_date_time> Print date and time AAAA-MM-DD HH:MM:SS
  51. * <dol_print_year> Print Year
  52. * <dol_print_month_letters> Print month in letters (example : november)
  53. * <dol_print_month> Print month number
  54. * <dol_print_day> Print day number
  55. * <dol_print_day_letters> Print day number
  56. * <dol_print_table> Print table number (for restaurant, bar...)
  57. * <dol_print_cutlery> Print number of cutlery (for restaurant)
  58. * <dol_print_payment> Print payment method
  59. * <dol_print_logo> Print logo stored on printer. Example : <print_logo>32|32
  60. * <dol_print_logo_old> Print logo stored on printer. Must be followed by logo code. For old printers.
  61. * <dol_print_order_lines> Print order lines
  62. * <dol_print_order_tax> Print order total tax
  63. * <dol_print_order_local_tax> Print order local tax
  64. * <dol_print_order_total> Print order total
  65. * <dol_print_order_number> Print order number
  66. * <dol_print_order_number_unique> Print order number after validation
  67. * <dol_print_customer_firstname> Print customer firstname
  68. * <dol_print_customer_lastname> Print customer name
  69. * <dol_print_customer_mail> Print customer mail
  70. * <dol_print_customer_phone> Print customer phone
  71. * <dol_print_customer_mobile> Print customer mobile
  72. * <dol_print_customer_skype> Print customer skype
  73. * <dol_print_customer_tax_number> Print customer VAT number
  74. * <dol_print_customer_account_balance> Print customer account balance
  75. * <dol_print_vendor_lastname> Print vendor name
  76. * <dol_print_vendor_firstname> Print vendor firstname
  77. * <dol_print_vendor_mail> Print vendor mail
  78. * <dol_print_customer_points> Print customer points
  79. * <dol_print_order_points> Print number of points for this order
  80. *
  81. * Conditional code at line start (if�then Print)
  82. * <dol_print_if_customer> Print the line IF a customer is affected to the order
  83. * <dol_print_if_vendor> Print the line IF a vendor is affected to the order
  84. * <dol_print_if_happy_hour> Print the line IF Happy Hour
  85. * <dol_print_if_num_order_unique> Print the line IF order is validated
  86. * <dol_print_if_customer_points> Print the line IF customer points > 0
  87. * <dol_print_if_order_points> Print the line IF points of the order > 0
  88. * <dol_print_if_customer_tax_number> Print the line IF customer has vat number
  89. * <dol_print_if_customer_account_balance_positive> Print the line IF customer balance > 0
  90. *
  91. */
  92. require_once DOL_DOCUMENT_ROOT .'/includes/mike42/escpos-php/Escpos.php';
  93. /**
  94. * Class to manage Receipt Printers
  95. */
  96. class dolReceiptPrinter extends Escpos
  97. {
  98. const CONNECTOR_DUMMY = 1;
  99. const CONNECTOR_FILE_PRINT = 2;
  100. const CONNECTOR_NETWORK_PRINT = 3;
  101. const CONNECTOR_WINDOWS_PRINT = 4;
  102. //const CONNECTOR_JAVA = 5;
  103. /**
  104. * @var DoliDB Database handler.
  105. */
  106. public $db;
  107. var $tags;
  108. var $printer;
  109. var $template;
  110. /**
  111. * @var string Error code (or message)
  112. */
  113. public $error='';
  114. /**
  115. * @var string[] Error codes (or messages)
  116. */
  117. public $errors = array();
  118. /**
  119. * Constructor
  120. *
  121. * @param DoliDB $db database
  122. */
  123. function __construct($db)
  124. {
  125. $this->db=$db;
  126. $this->tags = array(
  127. 'dol_align_left',
  128. 'dol_align_center',
  129. 'dol_align_right',
  130. 'dol_use_font_a',
  131. 'dol_use_font_b',
  132. 'dol_use_font_c',
  133. 'dol_bold',
  134. '/dol_bold',
  135. 'dol_double_height',
  136. '/dol_double_height',
  137. 'dol_double_width',
  138. '/dol_double_width',
  139. 'dol_underline',
  140. '/dol_underline',
  141. 'dol_underline_2dots',
  142. '/dol_underline',
  143. 'dol_emphasized',
  144. '/dol_emphasized',
  145. 'dol_switch_colors',
  146. '/dol_switch_colors',
  147. 'dol_print_barcode',
  148. 'dol_print_barcode_customer_id',
  149. 'dol_set_print_width_57',
  150. 'dol_cut_paper_full',
  151. 'dol_cut_paper_partial',
  152. 'dol_open_drawer',
  153. 'dol_activate_buzzer',
  154. 'dol_print_qrcode',
  155. 'dol_print_date',
  156. 'dol_print_date_time',
  157. 'dol_print_year',
  158. 'dol_print_month_letters',
  159. 'dol_print_month',
  160. 'dol_print_day',
  161. 'dol_print_day_letters',
  162. 'dol_print_table',
  163. 'dol_print_cutlery',
  164. 'dol_print_payment',
  165. 'dol_print_logo',
  166. 'dol_print_logo_old',
  167. 'dol_print_order_lines',
  168. 'dol_print_order_tax',
  169. 'dol_print_order_local_tax',
  170. 'dol_print_order_total',
  171. 'dol_print_order_number',
  172. 'dol_print_order_number_unique',
  173. 'dol_print_customer_firstname',
  174. 'dol_print_customer_lastname',
  175. 'dol_print_customer_mail',
  176. 'dol_print_customer_phone',
  177. 'dol_print_customer_mobile',
  178. 'dol_print_customer_skype',
  179. 'dol_print_customer_tax_number',
  180. 'dol_print_customer_account_balance',
  181. 'dol_print_vendor_lastname',
  182. 'dol_print_vendor_firstname',
  183. 'dol_print_vendor_mail',
  184. 'dol_print_customer_points',
  185. 'dol_print_order_points',
  186. 'dol_print_if_customer',
  187. 'dol_print_if_vendor',
  188. 'dol_print_if_happy_hour',
  189. 'dol_print_if_num_order_unique',
  190. 'dol_print_if_customer_points',
  191. 'dol_print_if_order_points',
  192. 'dol_print_if_customer_tax_number',
  193. 'dol_print_if_customer_account_balance_positive',
  194. );
  195. }
  196. /**
  197. * list printers
  198. *
  199. * @return int 0 if OK; >0 if KO
  200. */
  201. function listPrinters()
  202. {
  203. global $conf;
  204. $error = 0;
  205. $line = 0;
  206. $obj = array();
  207. $sql = 'SELECT rowid, name, fk_type, fk_profile, parameter';
  208. $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt';
  209. $sql.= ' WHERE entity = '.$conf->entity;
  210. $resql = $this->db->query($sql);
  211. if ($resql) {
  212. $num = $this->db->num_rows($resql);
  213. while ($line < $num) {
  214. $row = $this->db->fetch_array($resql);
  215. switch ($row['fk_type']) {
  216. case 1:
  217. $row['fk_type_name'] = 'CONNECTOR_DUMMY';
  218. break;
  219. case 2:
  220. $row['fk_type_name'] = 'CONNECTOR_FILE_PRINT';
  221. break;
  222. case 3:
  223. $row['fk_type_name'] = 'CONNECTOR_NETWORK_PRINT';
  224. break;
  225. case 4:
  226. $row['fk_type_name'] = 'CONNECTOR_WINDOWS_PRINT';
  227. break;
  228. case 5:
  229. $row['fk_type_name'] = 'CONNECTOR_JAVA';
  230. break;
  231. default:
  232. $row['fk_type_name'] = 'CONNECTOR_UNKNOWN';
  233. break;
  234. }
  235. switch ($row['fk_profile']) {
  236. case 0:
  237. $row['fk_profile_name'] = 'PROFILE_DEFAULT';
  238. break;
  239. case 1:
  240. $row['fk_profile_name'] = 'PROFILE_SIMPLE';
  241. break;
  242. case 2:
  243. $row['fk_profile_name'] = 'PROFILE_EPOSTEP';
  244. break;
  245. case 3:
  246. $row['fk_profile_name'] = 'PROFILE_P822D';
  247. break;
  248. default:
  249. $row['fk_profile_name'] = 'PROFILE_STAR';
  250. break;
  251. }
  252. $obj[] = $row;
  253. $line++;
  254. }
  255. } else {
  256. $error++;
  257. $this->errors[] = $this->db->lasterror;
  258. }
  259. $this->listprinters = $obj;
  260. return $error;
  261. }
  262. /**
  263. * List printers templates
  264. *
  265. * @return int 0 if OK; >0 if KO
  266. */
  267. function listPrintersTemplates()
  268. {
  269. global $conf;
  270. $error = 0;
  271. $line = 0;
  272. $obj = array();
  273. $sql = 'SELECT rowid, name, template';
  274. $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt_template';
  275. $sql.= ' WHERE entity = '.$conf->entity;
  276. $resql = $this->db->query($sql);
  277. if ($resql) {
  278. $num = $this->db->num_rows($resql);
  279. while ($line < $num) {
  280. $obj[] = $this->db->fetch_array($resql);
  281. $line++;
  282. }
  283. } else {
  284. $error++;
  285. $this->errors[] = $this->db->lasterror;
  286. }
  287. $this->listprinterstemplates = $obj;
  288. return $error;
  289. }
  290. /**
  291. * Form to Select type printer
  292. *
  293. * @param string $selected Id printer type pre-selected
  294. * @param string $htmlname select html name
  295. * @return int 0 if OK; >0 if KO
  296. */
  297. function selectTypePrinter($selected='', $htmlname='printertypeid')
  298. {
  299. global $langs;
  300. $options = array(
  301. 1 => $langs->trans('CONNECTOR_DUMMY'),
  302. 2 => $langs->trans('CONNECTOR_FILE_PRINT'),
  303. 3 => $langs->trans('CONNECTOR_NETWORK_PRINT'),
  304. 4 => $langs->trans('CONNECTOR_WINDOWS_PRINT')
  305. );
  306. $this->resprint = Form::selectarray($htmlname, $options, $selected);
  307. return 0;
  308. }
  309. /**
  310. * Form to Select Profile printer
  311. *
  312. * @param string $selected Id printer profile pre-selected
  313. * @param string $htmlname select html name
  314. * @return int 0 if OK; >0 if KO
  315. */
  316. function selectProfilePrinter($selected='', $htmlname='printerprofileid')
  317. {
  318. global $langs;
  319. $options = array(
  320. 0 => $langs->trans('PROFILE_DEFAULT'),
  321. 1 => $langs->trans('PROFILE_SIMPLE'),
  322. 2 => $langs->trans('PROFILE_EPOSTEP'),
  323. 3 => $langs->trans('PROFILE_P822D'),
  324. 4 => $langs->trans('PROFILE_STAR')
  325. );
  326. $this->profileresprint = Form::selectarray($htmlname, $options, $selected);
  327. return 0;
  328. }
  329. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  330. /**
  331. * Function to Add a printer in db
  332. *
  333. * @param string $name Printer name
  334. * @param int $type Printer type
  335. * @param int $profile Printer profile
  336. * @param string $parameter Printer parameter
  337. * @return int 0 if OK; >0 if KO
  338. */
  339. function AddPrinter($name, $type, $profile, $parameter)
  340. {
  341. // phpcs:enable
  342. global $conf;
  343. $error = 0;
  344. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'printer_receipt';
  345. $sql.= ' (name, fk_type, fk_profile, parameter, entity)';
  346. $sql.= ' VALUES ("'.$this->db->escape($name).'", '.$type.', '.$profile.', "'.$this->db->escape($parameter).'", '.$conf->entity.')';
  347. $resql = $this->db->query($sql);
  348. if (! $resql) {
  349. $error++;
  350. $this->errors[] = $this->db->lasterror;
  351. }
  352. return $error;
  353. }
  354. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  355. /**
  356. * Function to Update a printer in db
  357. *
  358. * @param string $name Printer name
  359. * @param int $type Printer type
  360. * @param int $profile Printer profile
  361. * @param string $parameter Printer parameter
  362. * @param int $printerid Printer id
  363. * @return int 0 if OK; >0 if KO
  364. */
  365. function UpdatePrinter($name, $type, $profile, $parameter, $printerid)
  366. {
  367. // phpcs:enable
  368. global $conf;
  369. $error = 0;
  370. $sql = 'UPDATE '.MAIN_DB_PREFIX.'printer_receipt';
  371. $sql.= ' SET name="'.$this->db->escape($name).'"';
  372. $sql.= ', fk_type='.$type;
  373. $sql.= ', fk_profile='.$profile;
  374. $sql.= ', parameter="'.$this->db->escape($parameter).'"';
  375. $sql.= ' WHERE rowid='.$printerid;
  376. $resql = $this->db->query($sql);
  377. if (! $resql) {
  378. $error++;
  379. $this->errors[] = $this->db->lasterror;
  380. }
  381. return $error;
  382. }
  383. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  384. /**
  385. * Function to Delete a printer from db
  386. *
  387. * @param int $printerid Printer id
  388. * @return int 0 if OK; >0 if KO
  389. */
  390. function DeletePrinter($printerid)
  391. {
  392. // phpcs:enable
  393. global $conf;
  394. $error = 0;
  395. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'printer_receipt';
  396. $sql.= ' WHERE rowid='.$printerid;
  397. $resql = $this->db->query($sql);
  398. if (! $resql) {
  399. $error++;
  400. $this->errors[] = $this->db->lasterror;
  401. }
  402. return $error;
  403. }
  404. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  405. /**
  406. * Function to Update a printer template in db
  407. *
  408. * @param string $name Template name
  409. * @param int $template Template
  410. * @param int $templateid Template id
  411. * @return int 0 if OK; >0 if KO
  412. */
  413. function UpdateTemplate($name, $template, $templateid)
  414. {
  415. // phpcs:enable
  416. global $conf;
  417. $error = 0;
  418. $sql = 'UPDATE '.MAIN_DB_PREFIX.'printer_receipt_template';
  419. $sql.= ' SET name="'.$this->db->escape($name).'"';
  420. $sql.= ', template="'.$this->db->escape($template).'"';
  421. $sql.= ' WHERE rowid='.$templateid;
  422. $resql = $this->db->query($sql);
  423. if (! $resql) {
  424. $error++;
  425. $this->errors[] = $this->db->lasterror;
  426. }
  427. return $error;
  428. }
  429. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  430. /**
  431. * Function to Send Test page to Printer
  432. *
  433. * @param int $printerid Printer id
  434. * @return int 0 if OK; >0 if KO
  435. */
  436. function SendTestToPrinter($printerid)
  437. {
  438. // phpcs:enable
  439. global $conf;
  440. $error = 0;
  441. $img = new EscposImage(DOL_DOCUMENT_ROOT .'/theme/common/dolibarr_logo_bw.png');
  442. $ret = $this->InitPrinter($printerid);
  443. if ($ret>0) {
  444. setEventMessages($this->error, $this->errors, 'errors');
  445. } else {
  446. try {
  447. $this->printer->graphics($img);
  448. $this->printer->text("Hello World!\n");
  449. $testStr = "Testing 123";
  450. $this->printer->qrCode($testStr);
  451. $this->printer->text("Most simple example\n");
  452. $this->printer->feed();
  453. $this->printer->cut();
  454. //print '<pre>'.print_r($this->connector, true).'</pre>';
  455. $this->printer->close();
  456. } catch (Exception $e) {
  457. $this->errors[] = $e->getMessage();
  458. $error++;
  459. }
  460. }
  461. return $error;
  462. }
  463. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  464. /**
  465. * Function to Print Receipt Ticket
  466. *
  467. * @param object $object order or invoice object
  468. * @param int $templateid Template id
  469. * @param int $printerid Printer id
  470. * @return int 0 if OK; >0 if KO
  471. */
  472. function SendToPrinter($object, $templateid, $printerid)
  473. {
  474. // phpcs:enable
  475. global $conf;
  476. $error = 0;
  477. $ret = $this->loadTemplate($templateid);
  478. // tags a remplacer par leur valeur avant de parser
  479. $this->template = str_replace('<dol_print_num_order>', $object->id, $this->template);
  480. $this->template = str_replace('<dol_print_customer_firstname>', $object->customer_firstname, $this->template);
  481. $this->template = str_replace('<dol_print_customer_lastname>', $object->customer_lastname, $this->template);
  482. $this->template = str_replace('<dol_print_customer_mail>', $object->customer_mail, $this->template);
  483. $this->template = str_replace('<dol_print_customer_phone>', $object->customer_phone, $this->template);
  484. $this->template = str_replace('<dol_print_customer_mobile>', $object->customer_mobile, $this->template);
  485. $this->template = str_replace('<dol_print_customer_skype>', $object->customer_skype, $this->template);
  486. $this->template = str_replace('<dol_print_customer_tax_number>', $object->customer_tax_number, $this->template);
  487. $this->template = str_replace('<dol_print_customer_account_balance>', $object->customer_account_balance, $this->template);
  488. $this->template = str_replace('<dol_print_customer_points>', $object->customer_points, $this->template);
  489. $this->template = str_replace('<dol_print_order_points>', $object->order_points, $this->template);
  490. $this->template = str_replace('<dol_print_vendor_firstname>', $object->vendor_firstname, $this->template);
  491. $this->template = str_replace('<dol_print_vendor_lastname>', $object->vendor_lastname, $this->template);
  492. $this->template = str_replace('<dol_print_vendor_mail>', $object->vendor_mail, $this->template);
  493. $this->template = str_replace('<dol_print_date>', $object->date, $this->template);
  494. $this->template = str_replace('<dol_print_date_time>', $object->date_time, $this->template);
  495. $this->template = str_replace('<dol_print_year>', $object->date_time, $this->template);
  496. $this->template = str_replace('<dol_print_month_letters>', $object->date_time, $this->template);
  497. $this->template = str_replace('<dol_print_month>', $object->date_time, $this->template);
  498. $this->template = str_replace('<dol_print_day>', $object->date_time, $this->template);
  499. $this->template = str_replace('<dol_print_day_letters>', $object->date_time, $this->template);
  500. $this->template = str_replace('<dol_print_table>', $object->table, $this->template);
  501. $this->template = str_replace('<dol_print_cutlery>', $object->cutlery, $this->template);
  502. // parse template
  503. $p = xml_parser_create();
  504. xml_parse_into_struct($p, $this->template, $vals, $index);
  505. xml_parser_free($p);
  506. //print '<pre>'.print_r($index, true).'</pre>';
  507. //print '<pre>'.print_r($vals, true).'</pre>';
  508. // print ticket
  509. $level = 0;
  510. $html = '<table border="1" style="width:210px"><pre>';
  511. $ret = $this->InitPrinter($printerid);
  512. if ($ret>0) {
  513. setEventMessages($this->error, $this->errors, 'errors');
  514. }
  515. else
  516. {
  517. $nboflines = count($vals);
  518. for ($line=0; $line < $nboflines; $line++)
  519. {
  520. switch ($vals[$line]['tag']) {
  521. case 'DOL_ALIGN_CENTER':
  522. $this->printer->setJustification(Escpos::JUSTIFY_CENTER);
  523. $html.='<center>';
  524. $this->printer->text($vals[$line]['value']);
  525. break;
  526. case 'DOL_ALIGN_RIGHT':
  527. $this->printer->setJustification(Escpos::JUSTIFY_RIGHT);
  528. $html.='<right>';
  529. break;
  530. case 'DOL_ALIGN_LEFT':
  531. $this->printer->setJustification(Escpos::JUSTIFY_LEFT);
  532. $html.='<left>';
  533. break;
  534. case 'DOL_OPEN_DRAWER':
  535. $this->printer->pulse();
  536. $html.= ' &#991;'.nl2br($vals[$line]['value']);
  537. break;
  538. case 'DOL_ACTIVATE_BUZZER':
  539. //$this->printer->buzzer();
  540. $html.= ' &#x266b;'.nl2br($vals[$line]['value']);
  541. break;
  542. case 'DOL_PRINT_BARCODE':
  543. // $vals[$line]['value'] -> barcode($content, $type)
  544. $this->printer->barcode($object->barcode);
  545. break;
  546. case 'DOL_PRINT_BARCODE_CUSTOMER_ID':
  547. // $vals[$line]['value'] -> barcode($content, $type)
  548. $this->printer->barcode($object->customer_id);
  549. break;
  550. case 'DOL_PRINT_QRCODE':
  551. // $vals[$line]['value'] -> qrCode($content, $ec, $size, $model)
  552. $this->printer->qrcode($vals[$line]['value']);
  553. $html.='QRCODE: '.$vals[$line]['value'];
  554. break;
  555. case 'DOL_CUT_PAPER_FULL':
  556. $this->printer->cut(Escpos::CUT_FULL);
  557. $html.= ' &#9986;'.nl2br($vals[$line]['value']);
  558. break;
  559. case 'DOL_CUT_PAPER_PARTIAL':
  560. $this->printer->cut(Escpos::CUT_PARTIAL);
  561. $html.= ' &#9986;'.nl2br($vals[$line]['value']);
  562. break;
  563. case 'DOL_USE_FONT_A':
  564. $this->printer->setFont(Escpos::FONT_A);
  565. $this->printer->text($vals[$line]['value']);
  566. break;
  567. case 'DOL_USE_FONT_B':
  568. $this->printer->setFont(Escpos::FONT_B);
  569. $this->printer->text($vals[$line]['value']);
  570. break;
  571. case 'DOL_USE_FONT_C':
  572. $this->printer->setFont(Escpos::FONT_C);
  573. $this->printer->text($vals[$line]['value']);
  574. break;
  575. default:
  576. $this->printer->text($vals[$line]['value']);
  577. $html.= nl2br($vals[$line]['value']);
  578. $this->errors[] = 'UnknowTag: &lt;'.strtolower($vals[$line]['tag']).'&gt;';
  579. $error++;
  580. break;
  581. }
  582. }
  583. $html.= '</pre></table>';
  584. print $html;
  585. // Close and print
  586. // uncomment next line to see content sent to printer
  587. //print '<pre>'.print_r($this->connector, true).'</pre>';
  588. $this->printer->close();
  589. }
  590. return $error;
  591. }
  592. /**
  593. * Function to load Template
  594. *
  595. * @param int $templateid Template id
  596. * @return int 0 if OK; >0 if KO
  597. */
  598. function loadTemplate($templateid)
  599. {
  600. global $conf;
  601. $error = 0;
  602. $sql = 'SELECT template';
  603. $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt_template';
  604. $sql.= ' WHERE rowid='.$templateid;
  605. $sql.= ' AND entity = '.$conf->entity;
  606. $resql = $this->db->query($sql);
  607. if ($resql) {
  608. $obj = $this->db->fetch_array($resql);
  609. } else {
  610. $error++;
  611. $this->errors[] = $this->db->lasterror;
  612. }
  613. if (empty($obj)) {
  614. $error++;
  615. $this->errors[] = 'TemplateDontExist';
  616. } else {
  617. $this->template = $obj['0'];
  618. }
  619. return $error;
  620. }
  621. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  622. /**
  623. * Function Init Printer
  624. *
  625. * @param int $printerid Printer id
  626. * @return int 0 if OK; >0 if KO
  627. */
  628. function InitPrinter($printerid)
  629. {
  630. // phpcs:enable
  631. global $conf;
  632. $error=0;
  633. $sql = 'SELECT rowid, name, fk_type, fk_profile, parameter';
  634. $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt';
  635. $sql.= ' WHERE rowid = '.$printerid;
  636. $sql.= ' AND entity = '.$conf->entity;
  637. $resql = $this->db->query($sql);
  638. if ($resql) {
  639. $obj = $this->db->fetch_array($resql);
  640. } else {
  641. $error++;
  642. $this->errors[] = $this->db->lasterror;
  643. }
  644. if (empty($obj)) {
  645. $error++;
  646. $this->errors[] = 'PrinterDontExist';
  647. }
  648. if (! $error) {
  649. $parameter = $obj['parameter'];
  650. try {
  651. switch ($obj['fk_type']) {
  652. case 1:
  653. require_once DOL_DOCUMENT_ROOT .'/includes/mike42/escpos-php/src/DummyPrintConnector.php';
  654. $this->connector = new DummyPrintConnector();
  655. break;
  656. case 2:
  657. $this->connector = new FilePrintConnector($parameter);
  658. break;
  659. case 3:
  660. $parameters = explode(':', $parameter);
  661. $this->connector = new NetworkPrintConnector($parameters[0], $parameters[1]);
  662. break;
  663. case 4:
  664. $this->connector = new WindowsPrintConnector($parameter);
  665. break;
  666. default:
  667. $this->connector = 'CONNECTOR_UNKNOWN';
  668. break;
  669. }
  670. $this->printer = new Escpos($this->connector);
  671. } catch (Exception $e) {
  672. $this->errors[] = $e->getMessage();
  673. $error++;
  674. }
  675. }
  676. return $error;
  677. }
  678. }