dolreceiptprinter.class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. /*
  3. * Copyright (C) 2015 Frederic 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. var $db;
  104. var $tags;
  105. var $printer;
  106. var $template;
  107. var $error;
  108. var $errors;
  109. /**
  110. * Constructor
  111. *
  112. * @param DoliDB $db database
  113. */
  114. function __construct($db)
  115. {
  116. $this->db=$db;
  117. $this->tags = array(
  118. 'dol_align_left',
  119. 'dol_align_center',
  120. 'dol_align_right',
  121. 'dol_use_font_a',
  122. 'dol_use_font_b',
  123. 'dol_use_font_c',
  124. 'dol_bold',
  125. '/dol_bold',
  126. 'dol_double_height',
  127. '/dol_double_height',
  128. 'dol_double_width',
  129. '/dol_double_width',
  130. 'dol_underline',
  131. '/dol_underline',
  132. 'dol_underline_2dots',
  133. '/dol_underline',
  134. 'dol_emphasized',
  135. '/dol_emphasized',
  136. 'dol_switch_colors',
  137. '/dol_switch_colors',
  138. 'dol_print_barcode',
  139. 'dol_print_barcode_customer_id',
  140. 'dol_set_print_width_57',
  141. 'dol_cut_paper_full',
  142. 'dol_cut_paper_partial',
  143. 'dol_open_drawer',
  144. 'dol_activate_buzzer',
  145. 'dol_print_qrcode',
  146. 'dol_print_date',
  147. 'dol_print_date_time',
  148. 'dol_print_year',
  149. 'dol_print_month_letters',
  150. 'dol_print_month',
  151. 'dol_print_day',
  152. 'dol_print_day_letters',
  153. 'dol_print_table',
  154. 'dol_print_cutlery',
  155. 'dol_print_payment',
  156. 'dol_print_logo',
  157. 'dol_print_logo_old',
  158. 'dol_print_order_lines',
  159. 'dol_print_order_tax',
  160. 'dol_print_order_local_tax',
  161. 'dol_print_order_total',
  162. 'dol_print_order_number',
  163. 'dol_print_order_number_unique',
  164. 'dol_print_customer_firstname',
  165. 'dol_print_customer_lastname',
  166. 'dol_print_customer_mail',
  167. 'dol_print_customer_phone',
  168. 'dol_print_customer_mobile',
  169. 'dol_print_customer_skype',
  170. 'dol_print_customer_tax_number',
  171. 'dol_print_customer_account_balance',
  172. 'dol_print_vendor_lastname',
  173. 'dol_print_vendor_firstname',
  174. 'dol_print_vendor_mail',
  175. 'dol_print_customer_points',
  176. 'dol_print_order_points',
  177. 'dol_print_if_customer',
  178. 'dol_print_if_vendor',
  179. 'dol_print_if_happy_hour',
  180. 'dol_print_if_num_order_unique',
  181. 'dol_print_if_customer_points',
  182. 'dol_print_if_order_points',
  183. 'dol_print_if_customer_tax_number',
  184. 'dol_print_if_customer_account_balance_positive',
  185. );
  186. }
  187. /**
  188. * list printers
  189. *
  190. * @return int 0 if OK; >0 if KO
  191. */
  192. function listPrinters()
  193. {
  194. global $conf;
  195. $error = 0;
  196. $line = 0;
  197. $sql = 'SELECT rowid, name, fk_type, fk_profile, parameter';
  198. $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt';
  199. $sql.= ' WHERE entity = '.$conf->entity;
  200. $resql = $this->db->query($sql);
  201. if ($resql) {
  202. $num = $this->db->num_rows($resql);
  203. while ($line < $num) {
  204. $row = $this->db->fetch_array($resql);
  205. switch ($row['fk_type']) {
  206. case 1:
  207. $row['fk_type_name'] = 'CONNECTOR_DUMMY';
  208. break;
  209. case 2:
  210. $row['fk_type_name'] = 'CONNECTOR_FILE_PRINT';
  211. break;
  212. case 3:
  213. $row['fk_type_name'] = 'CONNECTOR_NETWORK_PRINT';
  214. break;
  215. case 4:
  216. $row['fk_type_name'] = 'CONNECTOR_WINDOWS_PRINT';
  217. break;
  218. case 5:
  219. $row['fk_type_name'] = 'CONNECTOR_JAVA';
  220. break;
  221. default:
  222. $row['fk_type_name'] = 'CONNECTOR_UNKNOWN';
  223. break;
  224. }
  225. switch ($row['fk_profile']) {
  226. case 0:
  227. $row['fk_profile_name'] = 'PROFILE_DEFAULT';
  228. break;
  229. case 1:
  230. $row['fk_profile_name'] = 'PROFILE_SIMPLE';
  231. break;
  232. case 2:
  233. $row['fk_profile_name'] = 'PROFILE_EPOSTEP';
  234. break;
  235. case 3:
  236. $row['fk_profile_name'] = 'PROFILE_P822D';
  237. break;
  238. default:
  239. $row['fk_profile_name'] = 'PROFILE_STAR';
  240. break;
  241. }
  242. $obj[] = $row;
  243. $line++;
  244. }
  245. } else {
  246. $error++;
  247. $this->errors[] = $this->db->lasterror;
  248. }
  249. $this->listprinters = $obj;
  250. return $error;
  251. }
  252. /**
  253. * List printers templates
  254. *
  255. * @return int 0 if OK; >0 if KO
  256. */
  257. function listPrintersTemplates()
  258. {
  259. global $conf;
  260. $error = 0;
  261. $line = 0;
  262. $sql = 'SELECT rowid, name, template';
  263. $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt_template';
  264. $sql.= ' WHERE entity = '.$conf->entity;
  265. $resql = $this->db->query($sql);
  266. if ($resql) {
  267. $num = $this->db->num_rows($resql);
  268. while ($line < $num) {
  269. $obj[] = $this->db->fetch_array($resql);
  270. $line++;
  271. }
  272. } else {
  273. $error++;
  274. $this->errors[] = $this->db->lasterror;
  275. }
  276. $this->listprinterstemplates = $obj;
  277. return $error;
  278. }
  279. /**
  280. * Form to Select type printer
  281. *
  282. * @param string $selected Id printer type pre-selected
  283. * @param string $htmlname select html name
  284. * @return int 0 if OK; >0 if KO
  285. */
  286. function selectTypePrinter($selected='', $htmlname='printertypeid')
  287. {
  288. global $langs;
  289. $options = array(
  290. 1 => $langs->trans('CONNECTOR_DUMMY'),
  291. 2 => $langs->trans('CONNECTOR_FILE_PRINT'),
  292. 3 => $langs->trans('CONNECTOR_NETWORK_PRINT'),
  293. 4 => $langs->trans('CONNECTOR_WINDOWS_PRINT')
  294. );
  295. $this->resprint = Form::selectarray($htmlname, $options, $selected);
  296. return 0;
  297. }
  298. /**
  299. * Form to Select Profile printer
  300. *
  301. * @param string $selected Id printer profile pre-selected
  302. * @param string $htmlname select html name
  303. * @return int 0 if OK; >0 if KO
  304. */
  305. function selectProfilePrinter($selected='', $htmlname='printerprofileid')
  306. {
  307. global $langs;
  308. $options = array(
  309. 0 => $langs->trans('PROFILE_DEFAULT'),
  310. 1 => $langs->trans('PROFILE_SIMPLE'),
  311. 2 => $langs->trans('PROFILE_EPOSTEP'),
  312. 3 => $langs->trans('PROFILE_P822D'),
  313. 4 => $langs->trans('PROFILE_STAR')
  314. );
  315. $this->profileresprint = Form::selectarray($htmlname, $options, $selected);
  316. return 0;
  317. }
  318. /**
  319. * Function to Add a printer in db
  320. *
  321. * @param string $name Printer name
  322. * @param int $type Printer type
  323. * @param int $profile Printer profile
  324. * @param string $parameter Printer parameter
  325. * @return int 0 if OK; >0 if KO
  326. */
  327. function AddPrinter($name, $type, $profile, $parameter)
  328. {
  329. global $conf;
  330. $error = 0;
  331. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'printer_receipt';
  332. $sql.= ' (name, fk_type, fk_profile, parameter, entity)';
  333. $sql.= ' VALUES ("'.$this->db->escape($name).'", '.$type.', '.$profile.', "'.$this->db->escape($parameter).'", '.$conf->entity.')';
  334. $resql = $this->db->query($sql);
  335. if (! $resql) {
  336. $error++;
  337. $this->errors[] = $this->db->lasterror;
  338. }
  339. return $error;
  340. }
  341. /**
  342. * Function to Update a printer in db
  343. *
  344. * @param string $name Printer name
  345. * @param int $type Printer type
  346. * @param int $profile Printer profile
  347. * @param string $parameter Printer parameter
  348. * @param int $printerid Printer id
  349. * @return int 0 if OK; >0 if KO
  350. */
  351. function UpdatePrinter($name, $type, $profile, $parameter, $printerid)
  352. {
  353. global $conf;
  354. $error = 0;
  355. $sql = 'UPDATE '.MAIN_DB_PREFIX.'printer_receipt';
  356. $sql.= ' SET name="'.$this->db->escape($name).'"';
  357. $sql.= ', fk_type='.$type;
  358. $sql.= ', fk_profile='.$profile;
  359. $sql.= ', parameter="'.$this->db->escape($parameter).'"';
  360. $sql.= ' WHERE rowid='.$printerid;
  361. $resql = $this->db->query($sql);
  362. if (! $resql) {
  363. $error++;
  364. $this->errors[] = $this->db->lasterror;
  365. }
  366. return $error;
  367. }
  368. /**
  369. * Function to Delete a printer from db
  370. *
  371. * @param int $printerid Printer id
  372. * @return int 0 if OK; >0 if KO
  373. */
  374. function DeletePrinter($printerid)
  375. {
  376. global $conf;
  377. $error = 0;
  378. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'printer_receipt';
  379. $sql.= ' WHERE rowid='.$printerid;
  380. $resql = $this->db->query($sql);
  381. if (! $resql) {
  382. $error++;
  383. $this->errors[] = $this->db->lasterror;
  384. }
  385. return $error;
  386. }
  387. /**
  388. * Function to Update a printer template in db
  389. *
  390. * @param string $name Template name
  391. * @param int $template Template
  392. * @param int $templateid Template id
  393. * @return int 0 if OK; >0 if KO
  394. */
  395. function UpdateTemplate($name, $template, $templateid)
  396. {
  397. global $conf;
  398. $error = 0;
  399. $sql = 'UPDATE '.MAIN_DB_PREFIX.'printer_receipt_template';
  400. $sql.= ' SET name="'.$this->db->escape($name).'"';
  401. $sql.= ', template="'.$this->db->escape($template).'"';
  402. $sql.= ' WHERE rowid='.$templateid;
  403. $resql = $this->db->query($sql);
  404. if (! $resql) {
  405. $error++;
  406. $this->errors[] = $this->db->lasterror;
  407. }
  408. return $error;
  409. }
  410. /**
  411. * Function to Send Test page to Printer
  412. *
  413. * @param int $printerid Printer id
  414. * @return int 0 if OK; >0 if KO
  415. */
  416. function SendTestToPrinter($printerid)
  417. {
  418. global $conf;
  419. $error = 0;
  420. $img = new EscposImage(DOL_DOCUMENT_ROOT .'/theme/common/dolibarr_logo_bw.png');
  421. $ret = $this->InitPrinter($printerid);
  422. if ($ret>0) {
  423. setEventMessages($this->error, $this->errors, 'errors');
  424. } else {
  425. try {
  426. $this->printer->graphics($img);
  427. $this->printer->text("Hello World!\n");
  428. $testStr = "Testing 123";
  429. $this->printer->qrCode($testStr);
  430. $this->printer->text("Most simple example\n");
  431. $this->printer->feed();
  432. $this->printer->cut();
  433. //print '<pre>'.print_r($this->connector, true).'</pre>';
  434. $this->printer->close();
  435. } catch (Exception $e) {
  436. $this->errors[] = $e->getMessage();
  437. $error++;
  438. }
  439. }
  440. return $error;
  441. }
  442. /**
  443. * Function to Print Receipt Ticket
  444. *
  445. * @param object $object order or invoice object
  446. * @param int $templateid Template id
  447. * @param int $printerid Printer id
  448. * @return int 0 if OK; >0 if KO
  449. */
  450. function SendToPrinter($object, $templateid, $printerid)
  451. {
  452. global $conf;
  453. $error = 0;
  454. $ret = $this->loadTemplate($templateid);
  455. // tags a remplacer par leur valeur avant de parser
  456. $this->template = str_replace('<dol_print_num_order>', $object->id, $this->template);
  457. $this->template = str_replace('<dol_print_customer_firstname>', $object->customer_firstname, $this->template);
  458. $this->template = str_replace('<dol_print_customer_lastname>', $object->customer_lastname, $this->template);
  459. $this->template = str_replace('<dol_print_customer_mail>', $object->customer_mail, $this->template);
  460. $this->template = str_replace('<dol_print_customer_phone>', $object->customer_phone, $this->template);
  461. $this->template = str_replace('<dol_print_customer_mobile>', $object->customer_mobile, $this->template);
  462. $this->template = str_replace('<dol_print_customer_skype>', $object->customer_skype, $this->template);
  463. $this->template = str_replace('<dol_print_customer_tax_number>', $object->customer_tax_number, $this->template);
  464. $this->template = str_replace('<dol_print_customer_account_balance>', $object->customer_account_balance, $this->template);
  465. $this->template = str_replace('<dol_print_customer_points>', $object->customer_points, $this->template);
  466. $this->template = str_replace('<dol_print_order_points>', $object->order_points, $this->template);
  467. $this->template = str_replace('<dol_print_vendor_firstname>', $object->vendor_firstname, $this->template);
  468. $this->template = str_replace('<dol_print_vendor_lastname>', $object->vendor_lastname, $this->template);
  469. $this->template = str_replace('<dol_print_vendor_mail>', $object->vendor_mail, $this->template);
  470. $this->template = str_replace('<dol_print_date>', $object->date, $this->template);
  471. $this->template = str_replace('<dol_print_date_time>', $object->date_time, $this->template);
  472. $this->template = str_replace('<dol_print_year>', $object->date_time, $this->template);
  473. $this->template = str_replace('<dol_print_month_letters>', $object->date_time, $this->template);
  474. $this->template = str_replace('<dol_print_month>', $object->date_time, $this->template);
  475. $this->template = str_replace('<dol_print_day>', $object->date_time, $this->template);
  476. $this->template = str_replace('<dol_print_day_letters>', $object->date_time, $this->template);
  477. $this->template = str_replace('<dol_print_table>', $object->table, $this->template);
  478. $this->template = str_replace('<dol_print_cutlery>', $object->cutlery, $this->template);
  479. // parse template
  480. $p = xml_parser_create();
  481. xml_parse_into_struct($p, $this->template, $vals, $index);
  482. xml_parser_free($p);
  483. //print '<pre>'.print_r($index, true).'</pre>';
  484. //print '<pre>'.print_r($vals, true).'</pre>';
  485. // print ticket
  486. $level = 0;
  487. $html = '<table border="1" style="width:210px"><pre>';
  488. $ret = $this->InitPrinter($printerid);
  489. if ($ret>0) {
  490. setEventMessages($this->error, $this->errors, 'errors');
  491. }
  492. else
  493. {
  494. $nboflines = count($vals);
  495. for ($line=0; $line < $nboflines; $line++)
  496. {
  497. switch ($vals[$line]['tag']) {
  498. case 'DOL_ALIGN_CENTER':
  499. $this->printer->setJustification(Escpos::JUSTIFY_CENTER);
  500. $html.='<center>';
  501. $this->printer->text($vals[$line]['value']);
  502. break;
  503. case 'DOL_ALIGN_RIGHT':
  504. $this->printer->setJustification(Escpos::JUSTIFY_RIGHT);
  505. $html.='<right>';
  506. break;
  507. case 'DOL_ALIGN_LEFT':
  508. $this->printer->setJustification(Escpos::JUSTIFY_LEFT);
  509. $html.='<left>';
  510. break;
  511. case 'DOL_OPEN_DRAWER':
  512. $this->printer->pulse();
  513. $html.= ' &#991;'.nl2br($vals[$line]['value']);
  514. break;
  515. case 'DOL_ACTIVATE_BUZZER':
  516. //$this->printer->buzzer();
  517. $html.= ' &#x266b;'.nl2br($vals[$line]['value']);
  518. break;
  519. case 'DOL_PRINT_BARCODE':
  520. // $vals[$line]['value'] -> barcode($content, $type)
  521. $this->printer->barcode($object->barcode);
  522. break;
  523. case 'DOL_PRINT_BARCODE_CUSTOMER_ID':
  524. // $vals[$line]['value'] -> barcode($content, $type)
  525. $this->printer->barcode($object->customer_id);
  526. break;
  527. case 'DOL_PRINT_QRCODE':
  528. // $vals[$line]['value'] -> qrCode($content, $ec, $size, $model)
  529. $this->printer->qrcode($vals[$line]['value']);
  530. $html.='QRCODE: '.$vals[$line]['value'];
  531. break;
  532. case 'DOL_CUT_PAPER_FULL':
  533. $this->printer->cut(Escpos::CUT_FULL);
  534. $html.= ' &#9986;'.nl2br($vals[$line]['value']);
  535. break;
  536. case 'DOL_CUT_PAPER_PARTIAL':
  537. $this->printer->cut(Escpos::CUT_PARTIAL);
  538. $html.= ' &#9986;'.nl2br($vals[$line]['value']);
  539. break;
  540. case 'DOL_USE_FONT_A':
  541. $this->printer->setFont(Escpos::FONT_A);
  542. $this->printer->text($vals[$line]['value']);
  543. break;
  544. case 'DOL_USE_FONT_B':
  545. $this->printer->setFont(Escpos::FONT_B);
  546. $this->printer->text($vals[$line]['value']);
  547. break;
  548. case 'DOL_USE_FONT_C':
  549. $this->printer->setFont(Escpos::FONT_C);
  550. $this->printer->text($vals[$line]['value']);
  551. break;
  552. default:
  553. $this->printer->text($vals[$line]['value']);
  554. $html.= nl2br($vals[$line]['value']);
  555. $this->errors[] = 'UnknowTag: &lt;'.strtolower($vals[$line]['tag']).'&gt;';
  556. $error++;
  557. break;
  558. }
  559. }
  560. $html.= '</pre></table>';
  561. print $html;
  562. // Close and print
  563. // uncomment next line to see content sent to printer
  564. //print '<pre>'.print_r($this->connector, true).'</pre>';
  565. $this->printer->close();
  566. }
  567. return $error;
  568. }
  569. /**
  570. * Function to load Template
  571. *
  572. * @param int $templateid Template id
  573. * @return int 0 if OK; >0 if KO
  574. */
  575. function loadTemplate($templateid)
  576. {
  577. global $conf;
  578. $error = 0;
  579. $sql = 'SELECT template';
  580. $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt_template';
  581. $sql.= ' WHERE rowid='.$templateid;
  582. $sql.= ' AND entity = '.$conf->entity;
  583. $resql = $this->db->query($sql);
  584. if ($resql) {
  585. $obj = $this->db->fetch_array($resql);
  586. } else {
  587. $error++;
  588. $this->errors[] = $this->db->lasterror;
  589. }
  590. if (empty($obj)) {
  591. $error++;
  592. $this->errors[] = 'TemplateDontExist';
  593. } else {
  594. $this->template = $obj['0'];
  595. }
  596. return $error;
  597. }
  598. /**
  599. * Function Init Printer
  600. *
  601. * @param int $printerid Printer id
  602. * @return int 0 if OK; >0 if KO
  603. */
  604. function InitPrinter($printerid)
  605. {
  606. global $conf;
  607. $error=0;
  608. $sql = 'SELECT rowid, name, fk_type, fk_profile, parameter';
  609. $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt';
  610. $sql.= ' WHERE rowid = '.$printerid;
  611. $sql.= ' AND entity = '.$conf->entity;
  612. $resql = $this->db->query($sql);
  613. if ($resql) {
  614. $obj = $this->db->fetch_array($resql);
  615. } else {
  616. $error++;
  617. $this->errors[] = $this->db->lasterror;
  618. }
  619. if (empty($obj)) {
  620. $error++;
  621. $this->errors[] = 'PrinterDontExist';
  622. }
  623. if (! $error) {
  624. $parameter = $obj['parameter'];
  625. try {
  626. switch ($obj['fk_type']) {
  627. case 1:
  628. require_once DOL_DOCUMENT_ROOT .'/includes/mike42/escpos-php/src/DummyPrintConnector.php';
  629. $this->connector = new DummyPrintConnector();
  630. break;
  631. case 2:
  632. $this->connector = new FilePrintConnector($parameter);
  633. break;
  634. case 3:
  635. $parameters = explode(':', $parameter);
  636. $this->connector = new NetworkPrintConnector($parameters[0], $parameters[1]);
  637. break;
  638. case 4:
  639. $this->connector = new WindowsPrintConnector($parameter);
  640. break;
  641. default:
  642. $this->connector = 'CONNECTOR_UNKNOWN';
  643. break;
  644. }
  645. $this->printer = new Escpos($this->connector);
  646. } catch (Exception $e) {
  647. $this->errors[] = $e->getMessage();
  648. $error++;
  649. }
  650. }
  651. return $error;
  652. }
  653. }