dolibarr 21.0.0-alpha
dolreceiptprinter.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015-2024 Frédéric France <frederic.france@free.fr>
3 * Copyright (C) 2020 Andreu Bisquerra <jove@bisquerra.com>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Abbes Bahfir <bafbes@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
27/*
28 * Tags for ticket template
29 *
30 * {dol_align_left} Left align text
31 * {dol_align_center} Center text
32 * {dol_align_right} Right align text
33 * {dol_use_font_a} Use font A of printer
34 * {dol_use_font_b} Use font B of printer
35 * {dol_use_font_c} Use font C of printer
36 * {dol_bold} Text Bold
37 * {dol_bold_disabled} Disable Text Bold
38 * {dol_double_height} Text double height
39 * {dol_double_width} Text double width
40 * {dol_default_height_width} Text default height and width
41 * {dol_underline} Underline text
42 * {dol_underline_disabled} Disable underline text
43 * {dol_cut_paper_full} Cut ticket completely
44 * {dol_cut_paper_partial} Cut ticket partially
45 * {dol_open_drawer} Open cash drawer
46 * {dol_beep} Activate buzzer
47 * {dol_beep_alternative} Activate buzzer (alternative mode)
48 * {dol_print_barcode} Print barcode
49 * {dol_print_logo} Print logo stored on printer. Example : <print_logo>32|32
50 * {dol_print_logo_old} Print logo stored on printer. Must be followed by logo code. For old printers.
51 * {dol_print_logo_old_cf} Print logo stored on printer. Must be followed by logo code. For old printers. May help for centering image.
52 * {dol_print_object_lines} Print object lines
53 * {dol_print_object_tax} Print object total tax
54 * {dol_print_object_local_tax} Print object local tax
55 * {dol_print_object_total} Print object total
56 * {dol_print_order_lines} Print order lines for Printer
57 * {dol_print_object_lines_with_notes} Print object lines with notes
58 * {dol_print_payment} Print payment method
59 * {dol_print_curr_date} Print the current date/time. Must be followed by format string.
60 *
61 * Code which can be placed everywhere
62 * <dol_value_date> Replaced by date AAAA-MM-DD
63 * <dol_value_date_time> Replaced by date and time AAAA-MM-DD HH:MM:SS
64 * <dol_value_year> Replaced by Year
65 * <dol_value_month_letters> Replaced by month in letters (example : november)
66 * <dol_value_month> Replaced by month number
67 * <dol_value_day> Replaced by day number
68 * <dol_value_day_letters> Replaced by day number
69 * <dol_value_currentdate> Replaced by current date and time
70 * <dol_value_currentdate_notime> Replaced by current date without time
71 * <dol_object_id> Replaced by object id
72 * <dol_object_ref> Replaced by object ref
73 * <dol_value_customer_firstname> Replaced by customer firstname
74 * <dol_value_customer_lastname> Replaced by customer name
75 * <dol_value_customer_mail> Replaced by customer mail
76 * <dol_value_customer_phone> Replaced by customer phone
77 * <dol_value_customer_mobile> Replaced by customer mobile
78 * <dol_value_customer_tax_number> Replaced by customer VAT number
79 * <dol_value_customer_account_balance> Replaced by customer account balance
80 * <dol_value_mysoc_name> Replaced by mysoc name
81 * <dol_value_mysoc_address> Replaced by mysoc address
82 * <dol_value_mysoc_zip> Replaced by mysoc zip
83 * <dol_value_mysoc_town> Replaced by mysoc town
84 * <dol_value_mysoc_country> Replaced by mysoc country
85 * <dol_value_mysoc_idprof1> Replaced by mysoc idprof1
86 * <dol_value_mysoc_idprof2> Replaced by mysoc idprof2
87 * <dol_value_mysoc_idprof3> Replaced by mysoc idprof3
88 * <dol_value_mysoc_idprof4> Replaced by mysoc idprof4
89 * <dol_value_mysoc_idprof5> Replaced by mysoc idprof5
90 * <dol_value_mysoc_idprof6> Replaced by mysoc idprof6
91 * <dol_value_vendor_lastname> Replaced by vendor name
92 * <dol_value_vendor_firstname> Replaced by vendor firstname
93 * <dol_value_vendor_mail> Replaced by vendor mail
94 * <dol_value_customer_points> Replaced by customer points
95 * <dol_value_object_points> Replaced by number of points for this object
96 *
97 * Conditional code at line start (if then Print)
98 * <dol_print_if_customer> Print the line IF a customer is affected to the object
99 * <dol_print_if_vendor> Print the line IF a vendor is affected to the object
100 * <dol_print_if_happy_hour> Print the line IF Happy Hour
101 * <dol_print_if_num_object_unique> Print the line IF object is validated
102 * <dol_print_if_customer_points> Print the line IF customer points > 0
103 * <dol_print_if_object_points> Print the line IF points of the object > 0
104 * <dol_print_if_customer_tax_number> Print the line IF customer has vat number
105 * <dol_print_if_customer_account_balance_positive> Print the line IF customer balance > 0
106 *
107 */
108
109require_once DOL_DOCUMENT_ROOT.'/includes/mike42/escpos-php/autoload.php';
110use Mike42\Escpos\PrintConnectors\FilePrintConnector;
111use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
112use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
113use Mike42\Escpos\PrintConnectors\CupsPrintConnector;
114use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
115use Mike42\Escpos\Printer;
116use Mike42\Escpos\EscposImage;
117
121class dolReceiptPrinter extends Printer
122{
123 const CONNECTOR_DUMMY = 1;
124 const CONNECTOR_FILE_PRINT = 2;
125 const CONNECTOR_NETWORK_PRINT = 3;
126 const CONNECTOR_WINDOWS_PRINT = 4;
127 const CONNECTOR_CUPS_PRINT = 5;
128
132 public $db;
133
137 public $tags;
138
142 public $printer;
146 public $template;
147
152 public $orderprinter;
153
158 public $listprinters;
159
164 public $listprinterstemplates;
165
169 public $profileresprint;
170
174 public $resprint;
175
179 public $error = '';
180
184 public $errors = array();
185
191 public function __construct($db)
192 {
193 $this->db = $db;
194 $this->tags = array(
195 'dol_line_feed' => 'DOL_LINE_FEED',
196 'dol_line_feed_reverse' => 'DOL_LINE_FEED_REVERSE',
197 'dol_align_left' => 'DOL_ALIGN_LEFT',
198 'dol_align_center' => 'DOL_ALIGN_CENTER',
199 'dol_align_right' => 'DOL_ALIGN_RIGHT',
200 'dol_use_font_a' => 'DOL_USE_FONT_A',
201 'dol_use_font_b' => 'DOL_USE_FONT_B',
202 'dol_use_font_c' => 'DOL_USE_FONT_C',
203 'dol_bold' => 'DOL_BOLD',
204 'dol_bold_disabled' => 'DOL_BOLD_DISABLED',
205 'dol_double_height' => 'DOL_DOUBLE_HEIGHT',
206 'dol_double_width' => 'DOL_DOUBLE_WIDTH',
207 'dol_default_height_width' => 'DOL_DEFAULT_HEIGHT_WIDTH',
208 'dol_underline' => 'DOL_UNDERLINE',
209 'dol_underline_disabled' => 'DOL_UNDERLINE_DISABLED',
210 'dol_cut_paper_full' => 'DOL_CUT_PAPER_FULL',
211 'dol_cut_paper_partial' => 'DOL_CUT_PAPER_PARTIAL',
212 'dol_open_drawer' => 'DOL_OPEN_DRAWER',
213 'dol_beep' => 'DOL_BEEP',
214 'dol_beep_alternative' => 'DOL_BEEP_ALTERNATIVE',
215 'dol_print_text' => 'DOL_PRINT_TEXT',
216 'dol_print_barcode' => 'DOL_PRINT_BARCODE',
217 'dol_value_date' => 'DateInvoice',
218 'dol_value_date_time' => 'DateInvoiceWithTime',
219 'dol_value_year' => 'YearInvoice',
220 'dol_value_month_letters' => 'DOL_VALUE_MONTH_LETTERS',
221 'dol_value_month' => 'DOL_VALUE_MONTH',
222 'dol_value_day' => 'DOL_VALUE_DAY',
223 'dol_value_day_letters' => 'DOL_VALUE_DAY',
224 'dol_value_currentdate' => 'DOL_VALUE_CURRENTDATE',
225 'dol_value_currentdate_notime' => 'CurrentDateWithTime',
226 'dol_value_currentdate_letters' => 'DOL_VALUE_CURRENTDATE_LETTERS',
227 'dol_value_currentyear' => 'CurrentYear',
228 'dol_value_currentmonth_letters' => 'DOL_VALUE_CURRENT_MONTH_LETTERS',
229 'dol_value_currentmonth' => 'DOL_VALUE_CURRENT_MONTH',
230 'dol_value_currentday' => 'DOL_VALUE_CURRENT_DAY',
231 'dol_value_currentday_letters' => 'DOL_VALUE_CURRENT_DAY',
232 'dol_print_payment' => 'DOL_PRINT_PAYMENT',
233 'dol_print_curr_date' => 'DOL_PRINT_CURR_DATE',
234 'dol_print_logo' => 'DOL_PRINT_LOGO',
235 'dol_print_logo_old' => 'DOL_PRINT_LOGO_OLD',
236 'dol_print_logo_old_cf' => 'DOL_PRINT_LOGO_OLD_CF',
237 'dol_value_object_id' => 'InvoiceID',
238 'dol_value_object_ref' => 'InvoiceRef',
239 'dol_print_object_lines' => 'DOL_PRINT_OBJECT_LINES',
240 'dol_print_object_lines_with_notes' => 'DOL_PRINT_OBJECT_LINES_WITH_NOTES',
241 'dol_print_object_tax' => 'TotalVAT',
242 'dol_print_object_local_tax1' => 'TotalLT1',
243 'dol_print_object_local_tax2' => 'TotalLT2',
244 'dol_print_object_total' => 'Total',
245 'dol_print_object_number' => 'DOL_PRINT_OBJECT_NUMBER',
246 //'dol_value_object_points' => 'DOL_VALUE_OBJECT_POINTS',
247 'dol_print_order_lines' => 'DOL_PRINT_ORDER_LINES',
248 'dol_value_customer_firstname' => 'DOL_VALUE_CUSTOMER_FIRSTNAME',
249 'dol_value_customer_lastname' => 'DOL_VALUE_CUSTOMER_LASTNAME',
250 'dol_value_customer_mail' => 'DOL_VALUE_CUSTOMER_MAIL',
251 'dol_value_customer_phone' => 'DOL_VALUE_CUSTOMER_PHONE',
252 'dol_value_customer_tax_number' => 'DOL_VALUE_CUSTOMER_TAX_NUMBER',
253 //'dol_value_customer_account_balance' => 'DOL_VALUE_CUSTOMER_ACCOUNT_BALANCE',
254 //'dol_value_customer_points' => 'DOL_VALUE_CUSTOMER_POINTS',
255 'dol_value_mysoc_name' => 'DOL_VALUE_MYSOC_NAME',
256 'dol_value_mysoc_address' => 'Address',
257 'dol_value_mysoc_zip' => 'Zip',
258 'dol_value_mysoc_town' => 'Town',
259 'dol_value_mysoc_country' => 'Country',
260 'dol_value_mysoc_idprof1' => 'ProfId1',
261 'dol_value_mysoc_idprof2' => 'ProfId2',
262 'dol_value_mysoc_idprof3' => 'ProfId3',
263 'dol_value_mysoc_idprof4' => 'ProfId4',
264 'dol_value_mysoc_idprof5' => 'ProfId5',
265 'dol_value_mysoc_idprof6' => 'ProfId6',
266 'dol_value_mysoc_tva_intra' => 'VATIntra',
267 'dol_value_mysoc_capital' => 'Capital',
268 'dol_value_mysoc_url' => 'Web',
269 'dol_value_vendor_lastname' => 'VendorLastname',
270 'dol_value_vendor_firstname' => 'VendorFirstname',
271 'dol_value_vendor_mail' => 'VendorEmail',
272 'dol_value_place' => 'DOL_VALUE_PLACE',
273 );
274 }
275
281 public function listPrinters()
282 {
283 global $conf;
284
285 $error = 0;
286 $line = 0;
287 $listofprinters = array();
288
289 $sql = "SELECT rowid, name, fk_type, fk_profile, parameter";
290 $sql .= " FROM ".$this->db->prefix()."printer_receipt";
291 $sql .= " WHERE entity = ".((int) $conf->entity);
292
293 $resql = $this->db->query($sql);
294
295 if ($resql) {
296 $num = $this->db->num_rows($resql);
297 while ($line < $num) {
298 $row = $this->db->fetch_array($resql);
299 if ($row) {
300 switch ($row['fk_type']) {
301 case 1:
302 $row['fk_type_name'] = 'CONNECTOR_DUMMY';
303 break;
304 case 2:
305 $row['fk_type_name'] = 'CONNECTOR_FILE_PRINT';
306 break;
307 case 3:
308 $row['fk_type_name'] = 'CONNECTOR_NETWORK_PRINT';
309 break;
310 case 4:
311 $row['fk_type_name'] = 'CONNECTOR_WINDOWS_PRINT';
312 break;
313 case 5:
314 $row['fk_type_name'] = 'CONNECTOR_CUPS_PRINT';
315 break;
316 default:
317 $row['fk_type_name'] = 'CONNECTOR_UNKNOWN';
318 break;
319 }
320 switch ($row['fk_profile']) {
321 case 0:
322 $row['fk_profile_name'] = 'PROFILE_DEFAULT';
323 break;
324 case 1:
325 $row['fk_profile_name'] = 'PROFILE_SIMPLE';
326 break;
327 case 2:
328 $row['fk_profile_name'] = 'PROFILE_EPOSTEP';
329 break;
330 case 3:
331 $row['fk_profile_name'] = 'PROFILE_P822D';
332 break;
333 default:
334 $row['fk_profile_name'] = 'PROFILE_STAR';
335 break;
336 }
337 $listofprinters[] = $row;
338 }
339 $line++;
340 }
341 } else {
342 $error++;
343 $this->errors[] = $this->db->lasterror;
344 }
345
346 $this->listprinters = $listofprinters;
347
348 return $error;
349 }
350
351
357 public function listPrintersTemplates()
358 {
359 global $conf;
360
361 $error = 0;
362 $line = 0;
363 $listofprinters = array();
364
365 $sql = "SELECT rowid, name, template";
366 $sql .= " FROM ".$this->db->prefix()."printer_receipt_template";
367 $sql .= " WHERE entity = ".$conf->entity;
368
369 $resql = $this->db->query($sql);
370
371 if ($resql) {
372 $num = $this->db->num_rows($resql);
373 while ($line < $num) {
374 $row = $this->db->fetch_array($resql);
375 if ($row) {
376 $listofprinters[] = $row;
377 }
378 $line++;
379 }
380 } else {
381 $error++;
382 $this->errors[] = $this->db->lasterror;
383 }
384
385 $this->listprinterstemplates = $listofprinters;
386
387 return $error;
388 }
389
390
398 public function selectTypePrinter($selected = '', $htmlname = 'printertypeid')
399 {
400 global $langs;
401
402 $options = array(
403 1 => $langs->trans('CONNECTOR_DUMMY'),
404 2 => $langs->trans('CONNECTOR_FILE_PRINT'),
405 3 => $langs->trans('CONNECTOR_NETWORK_PRINT'),
406 4 => $langs->trans('CONNECTOR_WINDOWS_PRINT'),
407 5 => $langs->trans('CONNECTOR_CUPS_PRINT'),
408 );
409
410 $this->resprint = Form::selectarray($htmlname, $options, $selected);
411
412 return 0;
413 }
414
415
423 public function selectProfilePrinter($selected = '', $htmlname = 'printerprofileid')
424 {
425 global $langs;
426
427 $options = array(
428 0 => $langs->trans('PROFILE_DEFAULT'),
429 1 => $langs->trans('PROFILE_SIMPLE'),
430 2 => $langs->trans('PROFILE_EPOSTEP'),
431 3 => $langs->trans('PROFILE_P822D'),
432 4 => $langs->trans('PROFILE_STAR'),
433 );
434
435 $this->profileresprint = Form::selectarray($htmlname, $options, $selected);
436 return 0;
437 }
438
439
449 public function addPrinter($name, $type, $profile, $parameter)
450 {
451 global $conf;
452 $error = 0;
453 $sql = "INSERT INTO ".$this->db->prefix()."printer_receipt";
454 $sql .= " (name, fk_type, fk_profile, parameter, entity)";
455 $sql .= " VALUES ('".$this->db->escape($name)."', ".((int) $type).", ".((int) $profile).", '".$this->db->escape($parameter)."', ".((int) $conf->entity).")";
456 $resql = $this->db->query($sql);
457 if (!$resql) {
458 $error++;
459 $this->errors[] = $this->db->lasterror;
460 }
461 return $error;
462 }
463
474 public function updatePrinter($name, $type, $profile, $parameter, $printerid)
475 {
476 $error = 0;
477
478 $sql = "UPDATE ".$this->db->prefix()."printer_receipt";
479 $sql .= " SET name='".$this->db->escape($name)."'";
480 $sql .= ", fk_type=".((int) $type);
481 $sql .= ", fk_profile=".((int) $profile);
482 $sql .= ", parameter='".$this->db->escape($parameter)."'";
483 $sql .= " WHERE rowid=".((int) $printerid);
484
485 $resql = $this->db->query($sql);
486 if (!$resql) {
487 $error++;
488 $this->errors[] = $this->db->lasterror;
489 }
490 return $error;
491 }
492
499 public function deletePrinter($printerid)
500 {
501 global $conf;
502 $error = 0;
503 $sql = 'DELETE FROM '.$this->db->prefix().'printer_receipt';
504 $sql .= ' WHERE rowid='.((int) $printerid);
505 $resql = $this->db->query($sql);
506 if (!$resql) {
507 $error++;
508 $this->errors[] = $this->db->lasterror;
509 }
510 return $error;
511 }
512
520 public function addTemplate($name, $template)
521 {
522 global $conf;
523 $error = 0;
524 $sql = "INSERT INTO ".$this->db->prefix()."printer_receipt_template";
525 $sql .= " (name, template, entity) VALUES ('".$this->db->escape($name)."'";
526 $sql .= ", '".$this->db->escape($template)."', ".$conf->entity.")";
527 $resql = $this->db->query($sql);
528 if (!$resql) {
529 $error++;
530 $this->errors[] = $this->db->lasterror;
531 }
532 return $error;
533 }
534
541 public function deleteTemplate($templateid)
542 {
543 global $conf;
544 $error = 0;
545 $sql = 'DELETE FROM '.$this->db->prefix().'printer_receipt_template';
546 $sql .= " WHERE rowid = ".((int) $templateid);
547 $sql .= " AND entity = ".$conf->entity;
548 $resql = $this->db->query($sql);
549 if (!$resql) {
550 $error++;
551 $this->errors[] = $this->db->lasterror;
552 }
553 return $error;
554 }
555
564 public function updateTemplate($name, $template, $templateid)
565 {
566 global $conf;
567 $error = 0;
568
569 $sql = "UPDATE ".$this->db->prefix()."printer_receipt_template";
570 $sql .= " SET name='".$this->db->escape($name)."'";
571 $sql .= ", template='".$this->db->escape($template)."'";
572 $sql .= " WHERE rowid=".((int) $templateid);
573 $resql = $this->db->query($sql);
574 if (!$resql) {
575 $error++;
576 $this->errors[] = $this->db->lasterror;
577 }
578 return $error;
579 }
580
581
589 public function sendTestToPrinter($printerid, $addimgandbarcode = 0)
590 {
591 $error = 0;
592 $img = EscposImage::load(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo_bw.png');
593 //$this->profile = CapabilityProfile::load("TM-T88IV");
594 $ret = $this->initPrinter($printerid);
595 if ($ret > 0) {
596 setEventMessages($this->error, $this->errors, 'errors');
597 } else {
598 try {
599 if ($addimgandbarcode) {
600 $this->printer->bitImage($img);
601 }
602 $this->printer->text("Hello World!\n");
603 if ($addimgandbarcode) {
604 $testStr = "1234567890";
605 $this->printer->barcode($testStr);
606 //$this->printer->qrcode($testStr, Printer::QR_ECLEVEL_M, 5, Printer::QR_MODEL_1);
607 }
608 $this->printer->text("\n");
609 $this->printer->text("Most simple example\n");
610 $this->printer->feed();
611 $this->printer->cut();
612
613 // If is DummyPrintConnector send to log to debugging
614 $connector = $this->printer->connector;
615 if ($connector instanceof DummyPrintConnector) {
616 $data = $connector->getData();
617 dol_syslog($data);
618 }
619 // Close and print
620 $this->printer->close();
621 } catch (Exception $e) {
622 $this->errors[] = $e->getMessage();
623 $error++;
624 }
625 }
626 return $error;
627 }
628
637 public function sendToPrinter($object, $templateid, $printerid)
638 {
639 global $mysoc, $langs, $user;
640 global $hookmanager;
641
642 $langs->load('bills');
643
644 $error = 0;
645 $ret = $this->loadTemplate($templateid);
646
647 $now = dol_now('tzuser');
648 // tags a remplacer par leur valeur avant de parser (dol_value_xxx)
649 $this->template = str_replace('{dol_value_object_id}', (string) $object->id, $this->template);
650 $this->template = str_replace('{dol_value_object_ref}', $object->ref, $this->template);
651 //$this->template = str_replace('<dol_value_object_points>', $object->points, $this->template);
652 $this->template = str_replace('{dol_value_date}', dol_print_date($object->date, 'day'), $this->template);
653 $this->template = str_replace('{dol_value_date_time}', dol_print_date($object->date, 'dayhour'), $this->template);
654 $this->template = str_replace('{dol_value_year}', dol_print_date($object->date, '%Y'), $this->template);
655 $this->template = str_replace('{dol_value_month_letters}', $langs->trans("Month".dol_print_date($object->date, '%m')), $this->template);
656 $this->template = str_replace('{dol_value_month}', dol_print_date($object->date, '%m'), $this->template);
657 $this->template = str_replace('{dol_value_day}', dol_print_date($object->date, '%d'), $this->template);
658 $this->template = str_replace('{dol_value_day_letters}', $langs->trans("Day".dol_print_date($object->date, '%m')[1]), $this->template);
659
660 $this->template = str_replace('{dol_value_currentdate}', dol_print_date($now, 'dayhour'), $this->template);
661 $this->template = str_replace('{dol_value_currentdate_notime}', dol_print_date($now, 'day'), $this->template);
662 $this->template = str_replace('{dol_value_currentdate_letters}', dol_print_date($now, 'dayhourtext'), $this->template);
663 $this->template = str_replace('{dol_value_currentyear}', dol_print_date($now, '%Y'), $this->template);
664 $this->template = str_replace('{dol_value_currentmonth_letters}', $langs->trans("Month".dol_print_date($now, '%m')), $this->template);
665 $this->template = str_replace('{dol_value_currentmonth}', dol_print_date($now, '%m'), $this->template);
666 $this->template = str_replace('{dol_value_currentday}', dol_print_date($now, '%d'), $this->template);
667 $this->template = str_replace('{dol_value_currentday_letters}', $langs->trans("Day".dol_print_date($now, '%m')[1]), $this->template);
668
669 $this->template = str_replace('{dol_value_customer_firstname}', $object->thirdparty->firstname, $this->template);
670 $this->template = str_replace('{dol_value_customer_lastname}', $object->thirdparty->lastname, $this->template);
671 $this->template = str_replace('{dol_value_customer_mail}', $object->thirdparty->email, $this->template);
672 $this->template = str_replace('{dol_value_customer_phone}', $object->thirdparty->phone, $this->template);
673 //$this->template = str_replace('<dol_value_customer_mobile>', $object->thirdparty->mobile, $this->template);
674 $this->template = str_replace('{dol_value_customer_tax_number}', $object->thirdparty->tva_intra, $this->template);
675 //$this->template = str_replace('<dol_value_customer_account_balance>', $object->customer_account_balance, $this->template);
676 //$this->template = str_replace('<dol_value_customer_points>', $object->customer_points, $this->template);
677
678 $this->template = str_replace('{dol_value_mysoc_name}', $mysoc->name, $this->template);
679 $this->template = str_replace('{dol_value_mysoc_address}', $mysoc->address, $this->template);
680 $this->template = str_replace('{dol_value_mysoc_zip}', $mysoc->zip, $this->template);
681 $this->template = str_replace('{dol_value_mysoc_town}', $mysoc->town, $this->template);
682 $this->template = str_replace('{dol_value_mysoc_country}', $mysoc->country, $this->template);
683 $this->template = str_replace('{dol_value_mysoc_idprof1}', $mysoc->idprof1, $this->template);
684 $this->template = str_replace('{dol_value_mysoc_idprof2}', $mysoc->idprof2, $this->template);
685 $this->template = str_replace('{dol_value_mysoc_idprof3}', $mysoc->idprof3, $this->template);
686 $this->template = str_replace('{dol_value_mysoc_idprof4}', $mysoc->idprof4, $this->template);
687 $this->template = str_replace('{dol_value_mysoc_idprof5}', $mysoc->idprof5, $this->template);
688 $this->template = str_replace('{dol_value_mysoc_idprof6}', $mysoc->idprof6, $this->template);
689 $this->template = str_replace('{dol_value_mysoc_tva_intra}', $mysoc->tva_intra, $this->template);
690 $this->template = str_replace('{dol_value_mysoc_capital}', (string) $mysoc->capital, $this->template);
691 $this->template = str_replace('{dol_value_mysoc_url}', $mysoc->url, $this->template);
692
693 $this->template = str_replace('{dol_value_vendor_firstname}', $user->firstname, $this->template);
694 $this->template = str_replace('{dol_value_vendor_lastname}', $user->lastname, $this->template);
695 $this->template = str_replace('{dol_value_vendor_mail}', $user->email, $this->template);
696
697 $parameters = array('object' => $object);
698 $action = '';
699 $reshook = $hookmanager->executeHooks('sendToPrinterBefore', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
700 if ($reshook < 0) {
701 $this->error = "Error in hook dolReceiptPrinter sendToPrinterBefore ".$reshook;
702 dol_syslog("dolReceiptPrinter::sendToPrinter: error=".$this->error, LOG_ERR);
703 return $reshook;
704 }
705
706 // escape special characters for xml_parse_into_struct
707 $this->template = htmlspecialchars($this->template, ENT_QUOTES | ENT_XML1);
708
709 // parse template
710 $this->template = str_replace("{", "<", $this->template);
711 $this->template = str_replace("}", ">", $this->template);
712
713 if (LIBXML_VERSION < 20900) {
714 // Avoid load of external entities (security problem).
715 // Required only if LIBXML_VERSION < 20900
716 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
717 libxml_disable_entity_loader(true);
718 }
719
720 $vals = array();
721 $index = array();
722
723 $p = xml_parser_create();
724 xml_parse_into_struct($p, $this->template, $vals, $index);
725 xml_parser_free($p);
726
727 //print '<pre>'.print_r($index, true).'</pre>';
728 //print '<pre>'.print_r($vals, true).'</pre>';
729 // print ticket
730 $nbcharactbyline = getDolGlobalInt('RECEIPT_PRINTER_NB_CHARACT_BY_LINE', 48);
731 $ret = $this->initPrinter($printerid);
732
733 if ($ret > 0) {
734 setEventMessages($this->error, $this->errors, 'errors');
735 } else {
736 $nboflines = count($vals);
737 for ($tplline = 0; $tplline < $nboflines; $tplline++) {
738 //var_dump($vals[$tplline]['value']);
739 switch ($vals[$tplline]['tag']) {
740 case 'DOL_PRINT_TEXT':
741 $this->printer->text($vals[$tplline]['value']);
742 break;
743 case 'DOL_PRINT_OBJECT_LINES':
744 foreach ($object->lines as $line) {
745 if ($line->fk_product) {
746 $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen((string) $line->qty) - strlen(price($line->subprice)) - 10 - 1;
747 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
748 $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");
749 $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n \n");
750 } else {
751 $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen((string) $line->qty) - strlen(price($line->subprice)) - 10 - 1;
752 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
753 $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");
754 }
755 }
756 break;
757 case 'DOL_PRINT_OBJECT_LINES_WITH_NOTES':
758 foreach ($object->lines as $line) {
759 if ($line->fk_product) {
760 $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen((string) $line->qty) - 10 - 1;
761 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
762 $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
763 $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n");
764 $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen((string) $line->qty) - 10 - 1;
765 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
766 $this->printer->text($line->description."\n");
767 } else {
768 $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen((string) $line->qty) - 10 - 1;
769 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
770 $this->printer->text($line->description.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
771 }
772 }
773 break;
774 case 'DOL_PRINT_OBJECT_TAX':
775 //var_dump($object);
776 $vatarray = array();
777 foreach ($object->lines as $line) {
778 $vat_rate = $line->tva_tx;
779 if (!array_key_exists($vat_rate, $vatarray)) {
780 $vatarray[$vat_rate] = 0;
781 }
782 $vatarray[$vat_rate] += $line->total_tva;
783 }
784 foreach ($vatarray as $vatkey => $vatvalue) {
785 $spacestoadd = $nbcharactbyline - strlen($vatkey) - 12;
786 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
787 $this->printer->text($spaces.$vatkey.'% '.str_pad(price($vatvalue), 10, ' ', STR_PAD_LEFT)."\n");
788 }
789 break;
790 case 'DOL_PRINT_OBJECT_TAX1':
791 //var_dump($object);
792 $total_localtax1 = 0;
793 foreach ($object->lines as $line) {
794 $total_localtax1 += $line->total_localtax1;
795 }
796 $this->printer->text(str_pad(price($total_localtax1), 10, ' ', STR_PAD_LEFT)."\n");
797 break;
798 case 'DOL_PRINT_OBJECT_TAX2':
799 //var_dump($object);
800 $total_localtax2 = 0;
801 foreach ($object->lines as $line) {
802 $total_localtax2 += $line->total_localtax2;
803 }
804 $this->printer->text(str_pad(price($total_localtax2), 10, ' ', STR_PAD_LEFT)."\n");
805 break;
806 case 'DOL_PRINT_OBJECT_TOTAL':
807 $title = $langs->trans('TotalHT');
808 $spacestoadd = $nbcharactbyline - strlen($title) - 10;
809 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
810 $this->printer->text($title.$spaces.str_pad(price($object->total_ht), 10, ' ', STR_PAD_LEFT)."\n");
811 $title = $langs->trans('TotalVAT');
812 $spacestoadd = $nbcharactbyline - strlen($title) - 10;
813 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
814 $this->printer->text($title.$spaces.str_pad(price($object->total_tva), 10, ' ', STR_PAD_LEFT)."\n");
815 $title = $langs->trans('TotalTTC');
816 $spacestoadd = $nbcharactbyline - strlen($title) - 10;
817 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
818 $this->printer->text($title.$spaces.str_pad(price($object->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
819 break;
820 case 'DOL_PRINT_CURR_DATE':
821 if (strlen($vals[$tplline]['value']) < 2) {
822 $this->printer->text(date('d/m/Y H:i:s')."\n");
823 } else {
824 $this->printer->text(date($vals[$tplline]['value'])."\n");
825 }
826 break;
827 case 'DOL_LINE_FEED':
828 $this->printer->feed();
829 break;
830 case 'DOL_LINE_FEED_REVERSE':
831 $this->printer->feedReverse();
832 break;
833 case 'DOL_ALIGN_CENTER':
834 $this->printer->setJustification(Printer::JUSTIFY_CENTER);
835 break;
836 case 'DOL_ALIGN_RIGHT':
837 $this->printer->setJustification(Printer::JUSTIFY_RIGHT);
838 break;
839 case 'DOL_ALIGN_LEFT':
840 $this->printer->setJustification(Printer::JUSTIFY_LEFT);
841 break;
842 case 'DOL_OPEN_DRAWER':
843 $this->printer->pulse();
844 break;
845 case 'DOL_ACTIVATE_BUZZER':
846 //$this->printer->buzzer();
847 break;
848 case 'DOL_PRINT_BARCODE':
849 // $vals[$tplline]['value'] -> barcode($content, $type)
850 // var_dump($vals[$tplline]['value']);
851 try {
852 $this->printer->barcode($vals[$tplline]['value']);
853 } catch (Exception $e) {
854 $this->errors[] = 'Invalid Barcode value: '.$vals[$tplline]['value'];
855 $error++;
856 }
857 break;
858 case 'DOL_PRINT_LOGO':
859 $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
860 $this->printer->graphics($img);
861 break;
862 case 'DOL_PRINT_LOGO_OLD':
863 $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
864 $this->printer->bitImage($img);
865 break;
866 case 'DOL_PRINT_LOGO_OLD_CF':
867 $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
868 $this->printer->bitImageColumnFormat($img);
869 break;
870 case 'DOL_PRINT_QRCODE':
871 // $vals[$tplline]['value'] -> qrCode($content, $ec, $size, $model)
872 $this->printer->qrcode($vals[$tplline]['value']);
873 break;
874 case 'DOL_CUT_PAPER_FULL':
875 $this->printer->cut(Printer::CUT_FULL);
876 break;
877 case 'DOL_CUT_PAPER_PARTIAL':
878 $this->printer->cut(Printer::CUT_PARTIAL);
879 break;
880 case 'DOL_USE_FONT_A':
881 $this->printer->setFont(Printer::FONT_A);
882 break;
883 case 'DOL_USE_FONT_B':
884 $this->printer->setFont(Printer::FONT_B);
885 break;
886 case 'DOL_USE_FONT_C':
887 $this->printer->setFont(Printer::FONT_C);
888 break;
889 case 'DOL_BOLD':
890 $this->printer->setEmphasis(true);
891 break;
892 case 'DOL_BOLD_DISABLED':
893 $this->printer->setEmphasis(false);
894 break;
895 case 'DOL_DOUBLE_HEIGHT':
896 $this->printer->setTextSize(1, 2);
897 break;
898 case 'DOL_DOUBLE_WIDTH':
899 $this->printer->setTextSize(2, 1);
900 break;
901 case 'DOL_DEFAULT_HEIGHT_WIDTH':
902 $this->printer->setTextSize(1, 1);
903 break;
904 case 'DOL_UNDERLINE':
905 $this->printer->setUnderline(true);
906 break;
907 case 'DOL_UNDERLINE_DISABLED':
908 $this->printer->setUnderline(false);
909 break;
910 case 'DOL_BEEP':
911 $this->printer->getPrintConnector() -> write("\x1e");
912 break;
913 case 'DOL_BEEP_ALTERNATIVE': //if DOL_BEEP not works
914 $this->printer->getPrintConnector() -> write(Printer::ESC . "B" . chr(4) . chr(1));
915 break;
916 case 'DOL_PRINT_ORDER_LINES':
917 foreach ($object->lines as $line) {
918 if ($line->special_code == $this->orderprinter) {
919 $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen((string) $line->qty) - 10 - 1;
920 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
921 $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
922 $this->printer->text(strip_tags(htmlspecialchars_decode($line->desc))."\n");
923 }
924 }
925 break;
926 case 'DOL_PRINT_PAYMENT':
927 $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,";
928 $sql .= " cp.code";
929 $sql .= " FROM ".$this->db->prefix()."paiement_facture as pf, ".$this->db->prefix()."paiement as p";
930 $sql .= " LEFT JOIN ".$this->db->prefix()."c_paiement as cp ON p.fk_paiement = cp.id";
931 $sql .= " WHERE pf.fk_paiement = p.rowid AND pf.fk_facture = ".((int) $object->id);
932 $sql .= " ORDER BY p.datep";
933 $resql = $this->db->query($sql);
934 if ($resql) {
935 $num = $this->db->num_rows($resql);
936 $i = 0;
937 while ($i < $num) {
938 $row = $this->db->fetch_object($resql);
939 $spacestoadd = $nbcharactbyline - strlen($langs->transnoentitiesnoconv("PaymentTypeShort".$row->code)) - 12;
940 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
941 $amount_payment = (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? $row->multicurrency_amount : $row->amount;
942 if ($row->code == "LIQ") {
943 $amount_payment += $row->pos_change; // Show amount with excess received if is cash payment
944 }
945 $this->printer->text($spaces.$langs->transnoentitiesnoconv("PaymentTypeShort".$row->code).' '.str_pad(price($amount_payment), 10, ' ', STR_PAD_LEFT)."\n");
946 if ($row->code == "LIQ" && $row->pos_change > 0) { // Print change only in cash payments
947 $spacestoadd = $nbcharactbyline - strlen($langs->trans("Change")) - 12;
948 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
949 $this->printer->text($spaces.$langs->trans("Change").' '.str_pad(price($row->pos_change), 10, ' ', STR_PAD_LEFT)."\n");
950 }
951 $i++;
952 }
953 }
954 break;
955 case 'DOL_VALUE_PLACE':
956 $sql = "SELECT floor, label FROM ".$this->db->prefix()."takepos_floor_tables where rowid=".((int) str_replace(")", "", str_replace("(PROV-POS".$_SESSION["takeposterminal"]."-", "", $object->ref)));
957 $resql = $this->db->query($sql);
958 $obj = $this->db->fetch_object($resql);
959 if ($obj) {
960 $this->printer->text($obj->label);
961 }
962 break;
963 default:
964 $parameters = array('vals' => $vals[$tplline],'object' => $object,'nbcharactbyline' => $nbcharactbyline);
965 $action = '';
966 $reshook = $hookmanager->executeHooks('sendToPrinterAfter', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
967
968 if (!$reshook || $reshook < 0) {
969 $this->printer->text($vals[$tplline]['tag']);
970 $this->printer->text($vals[$tplline]['value']);
971 $this->errors[] = 'UnknowTag: &lt;'.strtolower($vals[$tplline]['tag']).'&gt;';
972 $error++;
973 }
974 break;
975 }
976 }
977 // If is DummyPrintConnector send to log to debugging
978 $connector = $this->printer->connector;
979 if ($connector instanceof DummyPrintConnector || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
980 $data = $connector->getData();
981 if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
982 echo rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
983 }
984 dol_syslog($data);
985 }
986 // Close and print
987 $this->printer->close();
988 }
989 return $error;
990 }
991
998 public function loadTemplate($templateid)
999 {
1000 $error = 0;
1001
1002 $sql = "SELECT template";
1003 $sql .= " FROM ".$this->db->prefix()."printer_receipt_template";
1004 $sql .= " WHERE rowid = ".((int) $templateid);
1005 $sql .= " AND entity IN (".getEntity('printer_receipt_template').")";
1006
1007 $resql = $this->db->query($sql);
1008 if ($resql) {
1009 $obj = $this->db->fetch_object($resql);
1010 } else {
1011 $error++;
1012 $this->errors[] = $this->db->lasterror;
1013 }
1014 if (empty($obj)) {
1015 $error++;
1016 $this->errors[] = 'TemplateDontExist';
1017 } else {
1018 $this->template = $obj->template;
1019 }
1020
1021 return $error;
1022 }
1023
1024
1031 public function initPrinter($printerid)
1032 {
1033 if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
1034 $this->connector = new DummyPrintConnector();
1035 $this->printer = new Printer($this->connector, $this->profile);
1036 return;
1037 }
1038
1039 $error = 0;
1040 $sql = "SELECT rowid, name, fk_type, fk_profile, parameter";
1041 $sql .= " FROM ".$this->db->prefix()."printer_receipt";
1042 $sql .= " WHERE rowid = ".((int) $printerid);
1043 $sql .= " AND entity IN (".getEntity('printer_receipt').")";
1044
1045 $resql = $this->db->query($sql);
1046 if ($resql) {
1047 $obj = $this->db->fetch_array($resql);
1048 if (empty($obj)) {
1049 $error++;
1050 $this->errors[] = 'PrinterDontExist';
1051 }
1052 if (!$error) {
1053 $parameter = (isset($obj['parameter']) ? $obj['parameter'] : '');
1054
1055 dol_syslog("initPrinter printerid=".$printerid." parameter=".$parameter);
1056
1057 try {
1058 $type = empty($obj['fk_type']) ? 0 : (int) $obj['fk_type'];
1059 $found = true;
1060 switch ($type) {
1061 case 1:
1062 $this->connector = new DummyPrintConnector();
1063 break;
1064 case 2:
1065 $this->connector = new FilePrintConnector($parameter);
1066 break;
1067 case 3:
1068 $parameters = explode(':', $parameter);
1069 $this->connector = new NetworkPrintConnector($parameters[0], $parameters[1]);
1070 break;
1071 case 4: // LPT1, smb://...
1072 $this->connector = new WindowsPrintConnector(dol_sanitizePathName($parameter));
1073 break;
1074 case 5:
1075 $this->connector = new CupsPrintConnector($parameter);
1076 break;
1077 default:
1078 $found = false;
1079 break;
1080 }
1081 if ($found) {
1082 $this->printer = new Printer($this->connector, $this->profile);
1083 } else {
1084 $error++;
1085 $this->errors[] = 'CONNECTOR_UNKNOWN';
1086 }
1087 } catch (Exception $e) {
1088 $this->errors[] = $e->getMessage();
1089 $error++;
1090 }
1091 }
1092 } else {
1093 $error++;
1094 $this->errors[] = $this->db->lasterror;
1095 }
1096
1097 return $error;
1098 }
1099}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
static selectarray($htmlname, $array, $id='', $show_empty=0, $key_in_label=0, $value_as_key=0, $moreparam='', $translate=0, $maxlen=0, $disabled=0, $sort='', $morecss='minwidth75', $addjscombo=1, $moreparamonempty='', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
Class to manage Receipt Printers.
selectTypePrinter($selected='', $htmlname='printertypeid')
Form to Select type printer.
addPrinter($name, $type, $profile, $parameter)
Function to Add a printer in db.
updatePrinter($name, $type, $profile, $parameter, $printerid)
Function to Update a printer in db.
sendToPrinter($object, $templateid, $printerid)
Function to Print Receipt Ticket.
listPrinters()
List printers into the array ->listprinters.
loadTemplate($templateid)
Function to load Template into $this->template.
updateTemplate($name, $template, $templateid)
Function to Update a printer template in db.
initPrinter($printerid)
Function Init Printer into $this->printer.
deleteTemplate($templateid)
Function to delete a printer template in db.
deletePrinter($printerid)
Function to Delete a printer from db.
selectProfilePrinter($selected='', $htmlname='printerprofileid')
Form to Select Profile printer.
addTemplate($name, $template)
Function to add a printer template in db.
listPrintersTemplates()
List printers templates.
sendTestToPrinter($printerid, $addimgandbarcode=0)
Function to Send Test page to Printer.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.