dolreceiptprinter.class.php 37 KB

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