dolreceiptprinter.class.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. <?php
  2. /* Copyright (C) 2015-2021 Frédéric France <frederic.france@netlogic.fr>
  3. * Copyright (C) 2020 Andreu Bisquerra <jove@bisquerra.com>
  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 <https://www.gnu.org/licenses/>.
  17. * or see https://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} Text Bold
  33. * {dol_bold_disabled} Disable Text Bold
  34. * {dol_double_height} Text double height
  35. * {dol_double_width} Text double width
  36. * {dol_default_height_width} Text default height and width
  37. * {dol_underline} Underline text
  38. * {dol_underline_disabled} Disable underline text
  39. * {dol_cut_paper_full} Cut ticket completely
  40. * {dol_cut_paper_partial} Cut ticket partially
  41. * {dol_open_drawer} Open cash drawer
  42. * {dol_beep} Activate buzzer
  43. * {dol_print_barcode} Print barcode
  44. * {dol_print_logo} Print logo stored on printer. Example : <print_logo>32|32
  45. * {dol_print_logo_old} Print logo stored on printer. Must be followed by logo code. For old printers.
  46. * {dol_print_object_lines} Print object lines
  47. * {dol_print_object_tax} Print object total tax
  48. * {dol_print_object_local_tax} Print object local tax
  49. * {dol_print_object_total} Print object total
  50. * {dol_print_order_lines} Print order lines for Printer
  51. * {dol_print_object_lines_with_notes} Print object lines with notes
  52. * {dol_print_payment} Print payment method
  53. *
  54. * Code which can be placed everywhere
  55. * <dol_value_date> Replaced by date AAAA-MM-DD
  56. * <dol_value_date_time> Replaced by date and time AAAA-MM-DD HH:MM:SS
  57. * <dol_value_year> Replaced by Year
  58. * <dol_value_month_letters> Replaced by month in letters (example : november)
  59. * <dol_value_month> Replaced by month number
  60. * <dol_value_day> Replaced by day number
  61. * <dol_value_day_letters> Replaced by day number
  62. * <dol_object_id> Replaced by object id
  63. * <dol_object_ref> Replaced by object ref
  64. * <dol_value_customer_firstname> Replaced by customer firstname
  65. * <dol_value_customer_lastname> Replaced by customer name
  66. * <dol_value_customer_mail> Replaced by customer mail
  67. * <dol_value_customer_phone> Replaced by customer phone
  68. * <dol_value_customer_mobile> Replaced by customer mobile
  69. * <dol_value_customer_skype> Replaced by customer skype
  70. * <dol_value_customer_tax_number> Replaced by customer VAT number
  71. * <dol_value_customer_account_balance> Replaced by customer account balance
  72. * <dol_value_mysoc_name> Replaced by mysoc name
  73. * <dol_value_mysoc_address> Replaced by mysoc address
  74. * <dol_value_mysoc_zip> Replaced by mysoc zip
  75. * <dol_value_mysoc_town> Replaced by mysoc town
  76. * <dol_value_mysoc_country> Replaced by mysoc country
  77. * <dol_value_mysoc_idprof1> Replaced by mysoc idprof1
  78. * <dol_value_mysoc_idprof2> Replaced by mysoc idprof2
  79. * <dol_value_mysoc_idprof3> Replaced by mysoc idprof3
  80. * <dol_value_mysoc_idprof4> Replaced by mysoc idprof4
  81. * <dol_value_mysoc_idprof5> Replaced by mysoc idprof5
  82. * <dol_value_mysoc_idprof6> Replaced by mysoc idprof6
  83. * <dol_value_vendor_lastname> Replaced by vendor name
  84. * <dol_value_vendor_firstname> Replaced by vendor firstname
  85. * <dol_value_vendor_mail> Replaced by vendor mail
  86. * <dol_value_customer_points> Replaced by customer points
  87. * <dol_value_object_points> Replaced by number of points for this object
  88. *
  89. * Conditional code at line start (if then Print)
  90. * <dol_print_if_customer> Print the line IF a customer is affected to the object
  91. * <dol_print_if_vendor> Print the line IF a vendor is affected to the object
  92. * <dol_print_if_happy_hour> Print the line IF Happy Hour
  93. * <dol_print_if_num_object_unique> Print the line IF object is validated
  94. * <dol_print_if_customer_points> Print the line IF customer points > 0
  95. * <dol_print_if_object_points> Print the line IF points of the object > 0
  96. * <dol_print_if_customer_tax_number> Print the line IF customer has vat number
  97. * <dol_print_if_customer_account_balance_positive> Print the line IF customer balance > 0
  98. *
  99. */
  100. require_once DOL_DOCUMENT_ROOT.'/includes/mike42/escpos-php/autoload.php';
  101. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  102. use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
  103. use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
  104. use Mike42\Escpos\PrintConnectors\CupsPrintConnector;
  105. use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
  106. use Mike42\Escpos\CapabilityProfile;
  107. use Mike42\Escpos\Printer;
  108. use Mike42\Escpos\EscposImage;
  109. /**
  110. * Class to manage Receipt Printers
  111. */
  112. class dolReceiptPrinter extends Printer
  113. {
  114. const CONNECTOR_DUMMY = 1;
  115. const CONNECTOR_FILE_PRINT = 2;
  116. const CONNECTOR_NETWORK_PRINT = 3;
  117. const CONNECTOR_WINDOWS_PRINT = 4;
  118. const CONNECTOR_CUPS_PRINT = 5;
  119. /**
  120. * @var DoliDB Database handler.
  121. */
  122. public $db;
  123. /*
  124. * @var string[] array of tags
  125. */
  126. public $tags;
  127. public $printer;
  128. public $template;
  129. /**
  130. * Number of order printer
  131. * @var int
  132. */
  133. public $orderprinter;
  134. /**
  135. * @var string Error code (or message)
  136. */
  137. public $error = '';
  138. /**
  139. * @var string[] Error codes (or messages)
  140. */
  141. public $errors = array();
  142. /**
  143. * Constructor
  144. *
  145. * @param DoliDB $db database
  146. */
  147. public function __construct($db)
  148. {
  149. $this->db = $db;
  150. $this->tags = array(
  151. 'dol_line_feed' => 'DOL_LINE_FEED',
  152. 'dol_line_feed_reverse' => 'DOL_LINE_FEED_REVERSE',
  153. 'dol_align_left' => 'DOL_ALIGN_LEFT',
  154. 'dol_align_center' => 'DOL_ALIGN_CENTER',
  155. 'dol_align_right' => 'DOL_ALIGN_RIGHT',
  156. 'dol_use_font_a' => 'DOL_USE_FONT_A',
  157. 'dol_use_font_b' => 'DOL_USE_FONT_B',
  158. 'dol_use_font_c' => 'DOL_USE_FONT_C',
  159. 'dol_bold' => 'DOL_BOLD',
  160. 'dol_bold_disabled' => 'DOL_BOLD_DISABLED',
  161. 'dol_double_height' => 'DOL_DOUBLE_HEIGHT',
  162. 'dol_double_width' => 'DOL_DOUBLE_WIDTH',
  163. 'dol_default_height_width' => 'DOL_DEFAULT_HEIGHT_WIDTH',
  164. 'dol_underline' => 'DOL_UNDERLINE',
  165. 'dol_underline_disabled' => 'DOL_UNDERLINE_DISABLED',
  166. 'dol_cut_paper_full' => 'DOL_CUT_PAPER_FULL',
  167. 'dol_cut_paper_partial' => 'DOL_CUT_PAPER_PARTIAL',
  168. 'dol_open_drawer' => 'DOL_OPEN_DRAWER',
  169. 'dol_beep' => 'DOL_BEEP',
  170. 'dol_print_text' => 'DOL_PRINT_TEXT',
  171. 'dol_print_barcode' => 'DOL_PRINT_BARCODE',
  172. 'dol_value_date' => 'DateInvoice',
  173. 'dol_value_date_time' => 'DateInvoiceWithTime',
  174. 'dol_value_year' => 'YearInvoice',
  175. 'dol_value_month_letters' => 'DOL_VALUE_MONTH_LETTERS',
  176. 'dol_value_month' => 'DOL_VALUE_MONTH',
  177. 'dol_value_day' => 'DOL_VALUE_DAY',
  178. 'dol_value_day_letters' => 'DOL_VALUE_DAY',
  179. 'dol_print_payment' => 'DOL_PRINT_PAYMENT',
  180. 'dol_print_logo' => 'DOL_PRINT_LOGO',
  181. 'dol_print_logo_old' => 'DOL_PRINT_LOGO_OLD',
  182. 'dol_value_object_id' => 'InvoiceID',
  183. 'dol_value_object_ref' => 'InvoiceRef',
  184. 'dol_print_object_lines' => 'DOL_PRINT_OBJECT_LINES',
  185. 'dol_print_object_lines_with_notes' => 'DOL_PRINT_OBJECT_LINES_WITH_NOTES',
  186. 'dol_print_object_tax' => 'TotalVAT',
  187. 'dol_print_object_local_tax1' => 'TotalLT1',
  188. 'dol_print_object_local_tax2' => 'TotalLT2',
  189. 'dol_print_object_total' => 'Total',
  190. 'dol_print_object_number' => 'DOL_PRINT_OBJECT_NUMBER',
  191. //'dol_value_object_points' => 'DOL_VALUE_OBJECT_POINTS',
  192. 'dol_print_order_lines' => 'DOL_PRINT_ORDER_LINES',
  193. 'dol_value_customer_firstname' => 'DOL_VALUE_CUSTOMER_FIRSTNAME',
  194. 'dol_value_customer_lastname' => 'DOL_VALUE_CUSTOMER_LASTNAME',
  195. 'dol_value_customer_mail' => 'DOL_VALUE_CUSTOMER_MAIL',
  196. 'dol_value_customer_phone' => 'DOL_VALUE_CUSTOMER_PHONE',
  197. 'dol_value_customer_skype' => 'DOL_VALUE_CUSTOMER_SKYPE',
  198. 'dol_value_customer_tax_number' => 'DOL_VALUE_CUSTOMER_TAX_NUMBER',
  199. //'dol_value_customer_account_balance' => 'DOL_VALUE_CUSTOMER_ACCOUNT_BALANCE',
  200. //'dol_value_customer_points' => 'DOL_VALUE_CUSTOMER_POINTS',
  201. 'dol_value_mysoc_name' => 'DOL_VALUE_MYSOC_NAME',
  202. 'dol_value_mysoc_address' => 'Address',
  203. 'dol_value_mysoc_zip' => 'Zip',
  204. 'dol_value_mysoc_town' => 'Town',
  205. 'dol_value_mysoc_country' => 'Country',
  206. 'dol_value_mysoc_idprof1' => 'ProfId1',
  207. 'dol_value_mysoc_idprof2' => 'ProfId2',
  208. 'dol_value_mysoc_idprof3' => 'ProfId3',
  209. 'dol_value_mysoc_idprof4' => 'ProfId4',
  210. 'dol_value_mysoc_idprof5' => 'ProfId5',
  211. 'dol_value_mysoc_idprof6' => 'ProfId6',
  212. 'dol_value_mysoc_tva_intra' => 'VATIntra',
  213. 'dol_value_mysoc_capital' => 'Capital',
  214. 'dol_value_vendor_lastname' => 'VendorLastname',
  215. 'dol_value_vendor_firstname' => 'VendorFirstname',
  216. 'dol_value_vendor_mail' => 'VendorEmail',
  217. 'dol_value_place' => 'DOL_VALUE_PLACE',
  218. );
  219. }
  220. /**
  221. * list printers
  222. *
  223. * @return int 0 if OK; >0 if KO
  224. */
  225. public function listPrinters()
  226. {
  227. global $conf;
  228. $error = 0;
  229. $line = 0;
  230. $obj = array();
  231. $sql = 'SELECT rowid, name, fk_type, fk_profile, parameter';
  232. $sql .= ' FROM '.MAIN_DB_PREFIX.'printer_receipt';
  233. $sql .= ' WHERE entity = '.$conf->entity;
  234. $resql = $this->db->query($sql);
  235. if ($resql) {
  236. $num = $this->db->num_rows($resql);
  237. while ($line < $num) {
  238. $row = $this->db->fetch_array($resql);
  239. switch ($row['fk_type']) {
  240. case 1:
  241. $row['fk_type_name'] = 'CONNECTOR_DUMMY';
  242. break;
  243. case 2:
  244. $row['fk_type_name'] = 'CONNECTOR_FILE_PRINT';
  245. break;
  246. case 3:
  247. $row['fk_type_name'] = 'CONNECTOR_NETWORK_PRINT';
  248. break;
  249. case 4:
  250. $row['fk_type_name'] = 'CONNECTOR_WINDOWS_PRINT';
  251. break;
  252. case 5:
  253. $row['fk_type_name'] = 'CONNECTOR_CUPS_PRINT';
  254. break;
  255. default:
  256. $row['fk_type_name'] = 'CONNECTOR_UNKNOWN';
  257. break;
  258. }
  259. switch ($row['fk_profile']) {
  260. case 0:
  261. $row['fk_profile_name'] = 'PROFILE_DEFAULT';
  262. break;
  263. case 1:
  264. $row['fk_profile_name'] = 'PROFILE_SIMPLE';
  265. break;
  266. case 2:
  267. $row['fk_profile_name'] = 'PROFILE_EPOSTEP';
  268. break;
  269. case 3:
  270. $row['fk_profile_name'] = 'PROFILE_P822D';
  271. break;
  272. default:
  273. $row['fk_profile_name'] = 'PROFILE_STAR';
  274. break;
  275. }
  276. $obj[] = $row;
  277. $line++;
  278. }
  279. } else {
  280. $error++;
  281. $this->errors[] = $this->db->lasterror;
  282. }
  283. $this->listprinters = $obj;
  284. return $error;
  285. }
  286. /**
  287. * List printers templates
  288. *
  289. * @return int 0 if OK; >0 if KO
  290. */
  291. public function listPrintersTemplates()
  292. {
  293. global $conf;
  294. $error = 0;
  295. $line = 0;
  296. $obj = array();
  297. $sql = 'SELECT rowid, name, template';
  298. $sql .= ' FROM '.MAIN_DB_PREFIX.'printer_receipt_template';
  299. $sql .= ' WHERE entity = '.$conf->entity;
  300. $resql = $this->db->query($sql);
  301. if ($resql) {
  302. $num = $this->db->num_rows($resql);
  303. while ($line < $num) {
  304. $obj[] = $this->db->fetch_array($resql);
  305. $line++;
  306. }
  307. } else {
  308. $error++;
  309. $this->errors[] = $this->db->lasterror;
  310. }
  311. $this->listprinterstemplates = $obj;
  312. return $error;
  313. }
  314. /**
  315. * Form to Select type printer
  316. *
  317. * @param string $selected Id printer type pre-selected
  318. * @param string $htmlname select html name
  319. * @return int 0 if OK; >0 if KO
  320. */
  321. public function selectTypePrinter($selected = '', $htmlname = 'printertypeid')
  322. {
  323. global $langs;
  324. $options = array(
  325. 1 => $langs->trans('CONNECTOR_DUMMY'),
  326. 2 => $langs->trans('CONNECTOR_FILE_PRINT'),
  327. 3 => $langs->trans('CONNECTOR_NETWORK_PRINT'),
  328. 4 => $langs->trans('CONNECTOR_WINDOWS_PRINT'),
  329. 5 => $langs->trans('CONNECTOR_CUPS_PRINT'),
  330. );
  331. $this->resprint = Form::selectarray($htmlname, $options, $selected);
  332. return 0;
  333. }
  334. /**
  335. * Form to Select Profile printer
  336. *
  337. * @param string $selected Id printer profile pre-selected
  338. * @param string $htmlname select html name
  339. * @return int 0 if OK; >0 if KO
  340. */
  341. public function selectProfilePrinter($selected = '', $htmlname = 'printerprofileid')
  342. {
  343. global $langs;
  344. $options = array(
  345. 0 => $langs->trans('PROFILE_DEFAULT'),
  346. 1 => $langs->trans('PROFILE_SIMPLE'),
  347. 2 => $langs->trans('PROFILE_EPOSTEP'),
  348. 3 => $langs->trans('PROFILE_P822D'),
  349. 4 => $langs->trans('PROFILE_STAR'),
  350. );
  351. $this->profileresprint = Form::selectarray($htmlname, $options, $selected);
  352. return 0;
  353. }
  354. /**
  355. * Function to Add a printer in db
  356. *
  357. * @param string $name Printer name
  358. * @param int $type Printer type
  359. * @param int $profile Printer profile
  360. * @param string $parameter Printer parameter
  361. * @return int 0 if OK; >0 if KO
  362. */
  363. public function addPrinter($name, $type, $profile, $parameter)
  364. {
  365. global $conf;
  366. $error = 0;
  367. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'printer_receipt';
  368. $sql .= ' (name, fk_type, fk_profile, parameter, entity)';
  369. $sql .= ' VALUES ("'.$this->db->escape($name).'", '.((int) $type).', '.((int) $profile).', "'.$this->db->escape($parameter).'", '.$conf->entity.')';
  370. $resql = $this->db->query($sql);
  371. if (!$resql) {
  372. $error++;
  373. $this->errors[] = $this->db->lasterror;
  374. }
  375. return $error;
  376. }
  377. /**
  378. * Function to Update a printer in db
  379. *
  380. * @param string $name Printer name
  381. * @param int $type Printer type
  382. * @param int $profile Printer profile
  383. * @param string $parameter Printer parameter
  384. * @param int $printerid Printer id
  385. * @return int 0 if OK; >0 if KO
  386. */
  387. public function updatePrinter($name, $type, $profile, $parameter, $printerid)
  388. {
  389. global $conf;
  390. $error = 0;
  391. $sql = 'UPDATE '.MAIN_DB_PREFIX.'printer_receipt';
  392. $sql .= ' SET name="'.$this->db->escape($name).'"';
  393. $sql .= ', fk_type='.((int) $type);
  394. $sql .= ', fk_profile='.((int) $profile);
  395. $sql .= ', parameter="'.$this->db->escape($parameter).'"';
  396. $sql .= ' WHERE rowid='.((int) $printerid);
  397. $resql = $this->db->query($sql);
  398. if (!$resql) {
  399. $error++;
  400. $this->errors[] = $this->db->lasterror;
  401. }
  402. return $error;
  403. }
  404. /**
  405. * Function to Delete a printer from db
  406. *
  407. * @param int $printerid Printer id
  408. * @return int 0 if OK; >0 if KO
  409. */
  410. public function deletePrinter($printerid)
  411. {
  412. global $conf;
  413. $error = 0;
  414. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'printer_receipt';
  415. $sql .= ' WHERE rowid='.((int) $printerid);
  416. $resql = $this->db->query($sql);
  417. if (!$resql) {
  418. $error++;
  419. $this->errors[] = $this->db->lasterror;
  420. }
  421. return $error;
  422. }
  423. /**
  424. * Function to add a printer template in db
  425. *
  426. * @param string $name Template name
  427. * @param int $template Template
  428. * @return int 0 if OK; >0 if KO
  429. */
  430. public function addTemplate($name, $template)
  431. {
  432. global $conf;
  433. $error = 0;
  434. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'printer_receipt_template';
  435. $sql .= ' (name, template, entity) VALUES ("'.$this->db->escape($name).'"';
  436. $sql .= ', "'.$this->db->escape($template).'", '.$conf->entity.')';
  437. $resql = $this->db->query($sql);
  438. if (!$resql) {
  439. $error++;
  440. $this->errors[] = $this->db->lasterror;
  441. }
  442. return $error;
  443. }
  444. /**
  445. * Function to delete a printer template in db
  446. *
  447. * @param int $templateid Template ID
  448. * @return int 0 if OK; >0 if KO
  449. */
  450. public function deleteTemplate($templateid)
  451. {
  452. global $conf;
  453. $error = 0;
  454. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'printer_receipt_template';
  455. $sql .= " WHERE rowid = ".((int) $templateid);
  456. $sql .= " AND entity = ".$conf->entity;
  457. $resql = $this->db->query($sql);
  458. if (!$resql) {
  459. $error++;
  460. $this->errors[] = $this->db->lasterror;
  461. }
  462. return $error;
  463. }
  464. /**
  465. * Function to Update a printer template in db
  466. *
  467. * @param string $name Template name
  468. * @param int $template Template
  469. * @param int $templateid Template id
  470. * @return int 0 if OK; >0 if KO
  471. */
  472. public function updateTemplate($name, $template, $templateid)
  473. {
  474. global $conf;
  475. $error = 0;
  476. $sql = 'UPDATE '.MAIN_DB_PREFIX.'printer_receipt_template';
  477. $sql .= ' SET name="'.$this->db->escape($name).'"';
  478. $sql .= ', template="'.$this->db->escape($template).'"';
  479. $sql .= ' WHERE rowid='.((int) $templateid);
  480. $resql = $this->db->query($sql);
  481. if (!$resql) {
  482. $error++;
  483. $this->errors[] = $this->db->lasterror;
  484. }
  485. return $error;
  486. }
  487. /**
  488. * Function to Send Test page to Printer
  489. *
  490. * @param int $printerid Printer id
  491. * @return int 0 if OK; >0 if KO
  492. */
  493. public function sendTestToPrinter($printerid)
  494. {
  495. global $conf;
  496. $error = 0;
  497. $img = EscposImage::load(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo_bw.png');
  498. //$this->profile = CapabilityProfile::load("TM-T88IV");
  499. $ret = $this->initPrinter($printerid);
  500. if ($ret > 0) {
  501. setEventMessages($this->error, $this->errors, 'errors');
  502. } else {
  503. try {
  504. $this->printer->bitImage($img);
  505. $this->printer->text("Hello World!\n");
  506. $testStr = "1234567890";
  507. $this->printer->barcode($testStr);
  508. //$this->printer->qrcode($testStr, Printer::QR_ECLEVEL_M, 5, Printer::QR_MODEL_1);
  509. $this->printer->text("Most simple example\n");
  510. $this->printer->feed();
  511. $this->printer->cut();
  512. // If is DummyPrintConnector send to log to debugging
  513. if ($this->printer->connector instanceof DummyPrintConnector) {
  514. $data = $this->printer->connector->getData();
  515. dol_syslog($data);
  516. }
  517. $this->printer->close();
  518. } catch (Exception $e) {
  519. $this->errors[] = $e->getMessage();
  520. $error++;
  521. }
  522. }
  523. return $error;
  524. }
  525. /**
  526. * Function to Print Receipt Ticket
  527. *
  528. * @param Facture|Commande $object Order or invoice object
  529. * @param int $templateid Template id
  530. * @param int $printerid Printer id
  531. * @return int 0 if OK; >0 if KO
  532. */
  533. public function sendToPrinter($object, $templateid, $printerid)
  534. {
  535. global $conf, $mysoc, $langs, $user;
  536. $error = 0;
  537. $ret = $this->loadTemplate($templateid);
  538. // tags a remplacer par leur valeur avant de parser (dol_value_xxx)
  539. $this->template = str_replace('{dol_value_object_id}', $object->id, $this->template);
  540. $this->template = str_replace('{dol_value_object_ref}', $object->ref, $this->template);
  541. //$this->template = str_replace('<dol_value_object_points>', $object->points, $this->template);
  542. $this->template = str_replace('{dol_value_date}', dol_print_date($object->date, 'day'), $this->template);
  543. $this->template = str_replace('{dol_value_date_time}', dol_print_date($object->date, 'dayhour'), $this->template);
  544. $this->template = str_replace('{dol_value_year}', dol_print_date($object->date, '%Y'), $this->template);
  545. $this->template = str_replace('{dol_value_month_letters}', $langs->trans("Month".dol_print_date($object->date, '%m')), $this->template);
  546. $this->template = str_replace('{dol_value_month}', dol_print_date($object->date, '%m'), $this->template);
  547. $this->template = str_replace('{dol_value_day}', dol_print_date($object->date, '%d'), $this->template);
  548. $this->template = str_replace('{dol_value_day_letters}', $langs->trans("Day".dol_print_date($object->date, '%m')[1]), $this->template);
  549. $this->template = str_replace('{dol_value_customer_firstname}', $object->thirdparty->firstname, $this->template);
  550. $this->template = str_replace('{dol_value_customer_lastname}', $object->thirdparty->lastname, $this->template);
  551. $this->template = str_replace('{dol_value_customer_mail}', $object->thirdparty->email, $this->template);
  552. $this->template = str_replace('{dol_value_customer_phone}', $object->thirdparty->phone, $this->template);
  553. //$this->template = str_replace('<dol_value_customer_mobile>', $object->thirdparty->mobile, $this->template);
  554. $this->template = str_replace('{dol_value_customer_tax_number}', $object->thirdparty->tva_intra, $this->template);
  555. //$this->template = str_replace('<dol_value_customer_account_balance>', $object->customer_account_balance, $this->template);
  556. //$this->template = str_replace('<dol_value_customer_points>', $object->customer_points, $this->template);
  557. $this->template = str_replace('{dol_value_mysoc_name}', $mysoc->name, $this->template);
  558. $this->template = str_replace('{dol_value_mysoc_address}', $mysoc->address, $this->template);
  559. $this->template = str_replace('{dol_value_mysoc_zip}', $mysoc->zip, $this->template);
  560. $this->template = str_replace('{dol_value_mysoc_town}', $mysoc->town, $this->template);
  561. $this->template = str_replace('{dol_value_mysoc_country}', $mysoc->country, $this->template);
  562. $this->template = str_replace('{dol_value_mysoc_idprof1}', $mysoc->idprof1, $this->template);
  563. $this->template = str_replace('{dol_value_mysoc_idprof2}', $mysoc->idprof2, $this->template);
  564. $this->template = str_replace('{dol_value_mysoc_idprof3}', $mysoc->idprof3, $this->template);
  565. $this->template = str_replace('{dol_value_mysoc_idprof4}', $mysoc->idprof4, $this->template);
  566. $this->template = str_replace('{dol_value_mysoc_idprof5}', $mysoc->idprof5, $this->template);
  567. $this->template = str_replace('{dol_value_mysoc_idprof6}', $mysoc->idprof6, $this->template);
  568. $this->template = str_replace('{dol_value_mysoc_tva_intra}', $mysoc->tva_intra, $this->template);
  569. $this->template = str_replace('{dol_value_mysoc_capital}', $mysoc->capital, $this->template);
  570. $this->template = str_replace('{dol_value_vendor_firstname}', $user->firstname, $this->template);
  571. $this->template = str_replace('{dol_value_vendor_lastname}', $user->lastname, $this->template);
  572. $this->template = str_replace('{dol_value_vendor_mail}', $user->email, $this->template);
  573. // parse template
  574. $this->template = str_replace("{", "<", $this->template);
  575. $this->template = str_replace("}", ">", $this->template);
  576. $p = xml_parser_create();
  577. xml_parse_into_struct($p, $this->template, $vals, $index);
  578. xml_parser_free($p);
  579. //print '<pre>'.print_r($index, true).'</pre>';
  580. //print '<pre>'.print_r($vals, true).'</pre>';
  581. // print ticket
  582. $level = 0;
  583. $nbcharactbyline = (!empty($conf->global->RECEIPT_PRINTER_NB_CHARACT_BY_LINE) ? $conf->global->RECEIPT_PRINTER_NB_CHARACT_BY_LINE : 48);
  584. $ret = $this->initPrinter($printerid);
  585. if ($ret > 0) {
  586. setEventMessages($this->error, $this->errors, 'errors');
  587. } else {
  588. $nboflines = count($vals);
  589. for ($tplline = 0; $tplline < $nboflines; $tplline++) {
  590. //var_dump($vals[$tplline]['value']);
  591. switch ($vals[$tplline]['tag']) {
  592. case 'DOL_PRINT_TEXT':
  593. $this->printer->text($vals[$tplline]['value']);
  594. break;
  595. case 'DOL_PRINT_OBJECT_LINES':
  596. foreach ($object->lines as $line) {
  597. if ($line->fk_product) {
  598. $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - 10 - 1;
  599. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  600. $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  601. $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n");
  602. } else {
  603. $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - 10 - 1;
  604. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  605. $this->printer->text($line->description.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  606. }
  607. }
  608. break;
  609. case 'DOL_PRINT_OBJECT_LINES_WITH_NOTES':
  610. foreach ($object->lines as $line) {
  611. if ($line->fk_product) {
  612. $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - 10 - 1;
  613. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  614. $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  615. $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n");
  616. $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - 10 - 1;
  617. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  618. $this->printer->text($line->description."\n");
  619. } else {
  620. $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - 10 - 1;
  621. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  622. $this->printer->text($line->description.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  623. }
  624. }
  625. break;
  626. case 'DOL_PRINT_OBJECT_TAX':
  627. //var_dump($object);
  628. $vatarray = array();
  629. foreach ($object->lines as $line) {
  630. $vatarray[$line->tva_tx] += $line->total_tva;
  631. }
  632. foreach ($vatarray as $vatkey => $vatvalue) {
  633. $spacestoadd = $nbcharactbyline - strlen($vatkey) - 12;
  634. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  635. $this->printer->text($spaces.$vatkey.'% '.str_pad(price($vatvalue), 10, ' ', STR_PAD_LEFT)."\n");
  636. }
  637. break;
  638. case 'DOL_PRINT_OBJECT_TAX1':
  639. //var_dump($object);
  640. $total_localtax1 = 0;
  641. foreach ($object->lines as $line) {
  642. $total_localtax1 += $line->total_localtax1;
  643. }
  644. $this->printer->text(str_pad(price($total_localtax1), 10, ' ', STR_PAD_LEFT)."\n");
  645. break;
  646. case 'DOL_PRINT_OBJECT_TAX2':
  647. //var_dump($object);
  648. $total_localtax2 = 0;
  649. foreach ($object->lines as $line) {
  650. $total_localtax2 += $line->total_localtax2;
  651. }
  652. $this->printer->text(str_pad(price($total_localtax2), 10, ' ', STR_PAD_LEFT)."\n");
  653. break;
  654. case 'DOL_PRINT_OBJECT_TOTAL':
  655. $title = $langs->trans('TotalHT');
  656. $spacestoadd = $nbcharactbyline - strlen($title) - 10;
  657. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  658. $this->printer->text($title.$spaces.str_pad(price($object->total_ht), 10, ' ', STR_PAD_LEFT)."\n");
  659. $title = $langs->trans('TotalVAT');
  660. $spacestoadd = $nbcharactbyline - strlen($title) - 10;
  661. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  662. $this->printer->text($title.$spaces.str_pad(price($object->total_tva), 10, ' ', STR_PAD_LEFT)."\n");
  663. $title = $langs->trans('TotalTTC');
  664. $spacestoadd = $nbcharactbyline - strlen($title) - 10;
  665. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  666. $this->printer->text($title.$spaces.str_pad(price($object->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  667. break;
  668. case 'DOL_LINE_FEED':
  669. $this->printer->feed();
  670. break;
  671. case 'DOL_LINE_FEED_REVERSE':
  672. $this->printer->feedReverse();
  673. break;
  674. case 'DOL_ALIGN_CENTER':
  675. $this->printer->setJustification(Printer::JUSTIFY_CENTER);
  676. break;
  677. case 'DOL_ALIGN_RIGHT':
  678. $this->printer->setJustification(Printer::JUSTIFY_RIGHT);
  679. break;
  680. case 'DOL_ALIGN_LEFT':
  681. $this->printer->setJustification(Printer::JUSTIFY_LEFT);
  682. break;
  683. case 'DOL_OPEN_DRAWER':
  684. $this->printer->pulse();
  685. break;
  686. case 'DOL_ACTIVATE_BUZZER':
  687. //$this->printer->buzzer();
  688. break;
  689. case 'DOL_PRINT_BARCODE':
  690. // $vals[$tplline]['value'] -> barcode($content, $type)
  691. // var_dump($vals[$tplline]['value']);
  692. try {
  693. $this->printer->barcode($vals[$tplline]['value']);
  694. } catch (Exception $e) {
  695. $this->errors[] = 'Invalid Barcode value: '.$vals[$tplline]['value'];
  696. $error++;
  697. }
  698. break;
  699. case 'DOL_PRINT_LOGO':
  700. $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
  701. $this->printer->graphics($img);
  702. break;
  703. case 'DOL_PRINT_LOGO_OLD':
  704. $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
  705. $this->printer->bitImage($img);
  706. break;
  707. case 'DOL_PRINT_QRCODE':
  708. // $vals[$tplline]['value'] -> qrCode($content, $ec, $size, $model)
  709. $this->printer->qrcode($vals[$tplline]['value']);
  710. break;
  711. case 'DOL_CUT_PAPER_FULL':
  712. $this->printer->cut(Printer::CUT_FULL);
  713. break;
  714. case 'DOL_CUT_PAPER_PARTIAL':
  715. $this->printer->cut(Printer::CUT_PARTIAL);
  716. break;
  717. case 'DOL_USE_FONT_A':
  718. $this->printer->setFont(Printer::FONT_A);
  719. break;
  720. case 'DOL_USE_FONT_B':
  721. $this->printer->setFont(Printer::FONT_B);
  722. break;
  723. case 'DOL_USE_FONT_C':
  724. $this->printer->setFont(Printer::FONT_C);
  725. break;
  726. case 'DOL_BOLD':
  727. $this->printer->setEmphasis(true);
  728. break;
  729. case 'DOL_BOLD_DISABLED':
  730. $this->printer->setEmphasis(false);
  731. break;
  732. case 'DOL_DOUBLE_HEIGHT':
  733. $this->printer->setTextSize(1, 2);
  734. break;
  735. case 'DOL_DOUBLE_WIDTH':
  736. $this->printer->setTextSize(2, 1);
  737. break;
  738. case 'DOL_DEFAULT_HEIGHT_WIDTH':
  739. $this->printer->setTextSize(1, 1);
  740. break;
  741. case 'DOL_UNDERLINE':
  742. $this->printer->setUnderline(true);
  743. break;
  744. case 'DOL_UNDERLINE_DISABLED':
  745. $this->printer->setUnderline(false);
  746. break;
  747. case 'DOL_BEEP':
  748. $this->printer->getPrintConnector() -> write("\x1e");
  749. break;
  750. case 'DOL_PRINT_ORDER_LINES':
  751. foreach ($object->lines as $line) {
  752. if ($line->special_code == $this->orderprinter) {
  753. $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - 10 - 1;
  754. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  755. $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  756. $this->printer->text(strip_tags(htmlspecialchars_decode($line->desc))."\n");
  757. }
  758. }
  759. break;
  760. case 'DOL_PRINT_PAYMENT':
  761. $sql = "SELECT p.pos_change as pos_change, p.datep as date, p.fk_paiement, p.num_paiement as num, pf.amount as amount, pf.multicurrency_amount,";
  762. $sql .= " cp.code";
  763. $sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."paiement as p";
  764. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON p.fk_paiement = cp.id";
  765. $sql .= " WHERE pf.fk_paiement = p.rowid AND pf.fk_facture = ".((int) $object->id);
  766. $sql .= " ORDER BY p.datep";
  767. $resql = $this->db->query($sql);
  768. if ($resql) {
  769. $num = $this->db->num_rows($resql);
  770. $i = 0;
  771. while ($i < $num) {
  772. $row = $this->db->fetch_object($resql);
  773. $spacestoadd = $nbcharactbyline - strlen($langs->transnoentitiesnoconv("PaymentTypeShort".$row->code)) - 12;
  774. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  775. $amount_payment = (!empty($conf->multicurrency->enabled) && $object->multicurrency_tx != 1) ? $row->multicurrency_amount : $row->amount;
  776. if ($row->code == "LIQ") {
  777. $amount_payment = $amount_payment + $row->pos_change; // Show amount with excess received if is cash payment
  778. }
  779. $this->printer->text($spaces.$langs->transnoentitiesnoconv("PaymentTypeShort".$row->code).' '.str_pad(price($amount_payment), 10, ' ', STR_PAD_LEFT)."\n");
  780. if ($row->code == "LIQ" && $row->pos_change > 0) { // Print change only in cash payments
  781. $spacestoadd = $nbcharactbyline - strlen($langs->trans("Change")) - 12;
  782. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  783. $this->printer->text($spaces.$langs->trans("Change").' '.str_pad(price($row->pos_change), 10, ' ', STR_PAD_LEFT)."\n");
  784. }
  785. $i++;
  786. }
  787. }
  788. break;
  789. case 'DOL_VALUE_PLACE':
  790. $sql = "SELECT floor, label FROM ".MAIN_DB_PREFIX."takepos_floor_tables where rowid=".((int) str_replace(")", "", str_replace("(PROV-POS".$_SESSION["takeposterminal"]."-", "", $object->ref)));
  791. $resql = $this->db->query($sql);
  792. $obj = $this->db->fetch_object($resql);
  793. if ($obj) {
  794. $this->printer->text($obj->label);
  795. }
  796. break;
  797. default:
  798. $this->printer->text($vals[$tplline]['tag']);
  799. $this->printer->text($vals[$tplline]['value']);
  800. $this->errors[] = 'UnknowTag: &lt;'.strtolower($vals[$tplline]['tag']).'&gt;';
  801. $error++;
  802. break;
  803. }
  804. }
  805. // If is DummyPrintConnector send to log to debugging
  806. if ($this->printer->connector instanceof DummyPrintConnector || $conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") {
  807. $data = $this->printer->connector->getData();
  808. if ($conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") {
  809. echo rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  810. }
  811. dol_syslog($data);
  812. }
  813. // Close and print
  814. $this->printer->close();
  815. }
  816. return $error;
  817. }
  818. /**
  819. * Function to load Template
  820. *
  821. * @param int $templateid Template id
  822. * @return int 0 if OK; >0 if KO
  823. */
  824. public function loadTemplate($templateid)
  825. {
  826. global $conf;
  827. $error = 0;
  828. $sql = 'SELECT template';
  829. $sql .= ' FROM '.MAIN_DB_PREFIX.'printer_receipt_template';
  830. $sql .= ' WHERE rowid = '.((int) $templateid);
  831. $sql .= ' AND entity = '.$conf->entity;
  832. $resql = $this->db->query($sql);
  833. if ($resql) {
  834. $obj = $this->db->fetch_array($resql);
  835. } else {
  836. $error++;
  837. $this->errors[] = $this->db->lasterror;
  838. }
  839. if (empty($obj)) {
  840. $error++;
  841. $this->errors[] = 'TemplateDontExist';
  842. } else {
  843. $this->template = $obj['0'];
  844. }
  845. return $error;
  846. }
  847. /**
  848. * Function Init Printer
  849. *
  850. * @param int $printerid Printer id
  851. * @return int 0 if OK; >0 if KO
  852. */
  853. public function initPrinter($printerid)
  854. {
  855. global $conf;
  856. if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
  857. $this->connector = new DummyPrintConnector();
  858. $this->printer = new Printer($this->connector, $this->profile);
  859. return;
  860. }
  861. $error = 0;
  862. $sql = 'SELECT rowid, name, fk_type, fk_profile, parameter';
  863. $sql .= ' FROM '.MAIN_DB_PREFIX.'printer_receipt';
  864. $sql .= ' WHERE rowid = '.((int) $printerid);
  865. $sql .= ' AND entity = '.((int) $conf->entity);
  866. $resql = $this->db->query($sql);
  867. if ($resql) {
  868. $obj = $this->db->fetch_array($resql);
  869. } else {
  870. $error++;
  871. $this->errors[] = $this->db->lasterror;
  872. }
  873. if (empty($obj)) {
  874. $error++;
  875. $this->errors[] = 'PrinterDontExist';
  876. }
  877. if (!$error) {
  878. $parameter = $obj['parameter'];
  879. try {
  880. switch ($obj['fk_type']) {
  881. case 1:
  882. $this->connector = new DummyPrintConnector();
  883. break;
  884. case 2:
  885. $this->connector = new FilePrintConnector($parameter);
  886. break;
  887. case 3:
  888. $parameters = explode(':', $parameter);
  889. $this->connector = new NetworkPrintConnector($parameters[0], $parameters[1]);
  890. break;
  891. case 4: // LPT1, smb://...
  892. $this->connector = new WindowsPrintConnector(dol_sanitizePathName($parameter));
  893. break;
  894. case 5:
  895. $this->connector = new CupsPrintConnector($parameter);
  896. break;
  897. default:
  898. $this->connector = 'CONNECTOR_UNKNOWN';
  899. break;
  900. }
  901. $this->printer = new Printer($this->connector, $this->profile);
  902. } catch (Exception $e) {
  903. $this->errors[] = $e->getMessage();
  904. $error++;
  905. }
  906. }
  907. return $error;
  908. }
  909. }