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;
143 public $template;
144
149 public $orderprinter;
150
155 public $listprinters;
156
161 public $listprinterstemplates;
162
166 public $profileresprint;
167
171 public $resprint;
172
176 public $error = '';
177
181 public $errors = array();
182
188 public function __construct($db)
189 {
190 $this->db = $db;
191 $this->tags = array(
192 'dol_line_feed' => 'DOL_LINE_FEED',
193 'dol_line_feed_reverse' => 'DOL_LINE_FEED_REVERSE',
194 'dol_align_left' => 'DOL_ALIGN_LEFT',
195 'dol_align_center' => 'DOL_ALIGN_CENTER',
196 'dol_align_right' => 'DOL_ALIGN_RIGHT',
197 'dol_use_font_a' => 'DOL_USE_FONT_A',
198 'dol_use_font_b' => 'DOL_USE_FONT_B',
199 'dol_use_font_c' => 'DOL_USE_FONT_C',
200 'dol_bold' => 'DOL_BOLD',
201 'dol_bold_disabled' => 'DOL_BOLD_DISABLED',
202 'dol_double_height' => 'DOL_DOUBLE_HEIGHT',
203 'dol_double_width' => 'DOL_DOUBLE_WIDTH',
204 'dol_default_height_width' => 'DOL_DEFAULT_HEIGHT_WIDTH',
205 'dol_underline' => 'DOL_UNDERLINE',
206 'dol_underline_disabled' => 'DOL_UNDERLINE_DISABLED',
207 'dol_cut_paper_full' => 'DOL_CUT_PAPER_FULL',
208 'dol_cut_paper_partial' => 'DOL_CUT_PAPER_PARTIAL',
209 'dol_open_drawer' => 'DOL_OPEN_DRAWER',
210 'dol_beep' => 'DOL_BEEP',
211 'dol_beep_alternative' => 'DOL_BEEP_ALTERNATIVE',
212 'dol_print_text' => 'DOL_PRINT_TEXT',
213 'dol_print_barcode' => 'DOL_PRINT_BARCODE',
214 'dol_value_date' => 'DateInvoice',
215 'dol_value_date_time' => 'DateInvoiceWithTime',
216 'dol_value_year' => 'YearInvoice',
217 'dol_value_month_letters' => 'DOL_VALUE_MONTH_LETTERS',
218 'dol_value_month' => 'DOL_VALUE_MONTH',
219 'dol_value_day' => 'DOL_VALUE_DAY',
220 'dol_value_day_letters' => 'DOL_VALUE_DAY',
221 'dol_value_currentdate' => 'DOL_VALUE_CURRENTDATE',
222 'dol_value_currentdate_notime' => 'CurrentDateWithTime',
223 'dol_value_currentdate_letters' => 'DOL_VALUE_CURRENTDATE_LETTERS',
224 'dol_value_currentyear' => 'CurrentYear',
225 'dol_value_currentmonth_letters' => 'DOL_VALUE_CURRENT_MONTH_LETTERS',
226 'dol_value_currentmonth' => 'DOL_VALUE_CURRENT_MONTH',
227 'dol_value_currentday' => 'DOL_VALUE_CURRENT_DAY',
228 'dol_value_currentday_letters' => 'DOL_VALUE_CURRENT_DAY',
229 'dol_print_payment' => 'DOL_PRINT_PAYMENT',
230 'dol_print_curr_date' => 'DOL_PRINT_CURR_DATE',
231 'dol_print_logo' => 'DOL_PRINT_LOGO',
232 'dol_print_logo_old' => 'DOL_PRINT_LOGO_OLD',
233 'dol_print_logo_old_cf' => 'DOL_PRINT_LOGO_OLD_CF',
234 'dol_value_object_id' => 'InvoiceID',
235 'dol_value_object_ref' => 'InvoiceRef',
236 'dol_print_object_lines' => 'DOL_PRINT_OBJECT_LINES',
237 'dol_print_object_lines_with_notes' => 'DOL_PRINT_OBJECT_LINES_WITH_NOTES',
238 'dol_print_object_tax' => 'TotalVAT',
239 'dol_print_object_local_tax1' => 'TotalLT1',
240 'dol_print_object_local_tax2' => 'TotalLT2',
241 'dol_print_object_total' => 'Total',
242 'dol_print_object_number' => 'DOL_PRINT_OBJECT_NUMBER',
243 //'dol_value_object_points' => 'DOL_VALUE_OBJECT_POINTS',
244 'dol_print_order_lines' => 'DOL_PRINT_ORDER_LINES',
245 'dol_value_customer_firstname' => 'DOL_VALUE_CUSTOMER_FIRSTNAME',
246 'dol_value_customer_lastname' => 'DOL_VALUE_CUSTOMER_LASTNAME',
247 'dol_value_customer_mail' => 'DOL_VALUE_CUSTOMER_MAIL',
248 'dol_value_customer_phone' => 'DOL_VALUE_CUSTOMER_PHONE',
249 'dol_value_customer_tax_number' => 'DOL_VALUE_CUSTOMER_TAX_NUMBER',
250 //'dol_value_customer_account_balance' => 'DOL_VALUE_CUSTOMER_ACCOUNT_BALANCE',
251 //'dol_value_customer_points' => 'DOL_VALUE_CUSTOMER_POINTS',
252 'dol_value_mysoc_name' => 'DOL_VALUE_MYSOC_NAME',
253 'dol_value_mysoc_address' => 'Address',
254 'dol_value_mysoc_zip' => 'Zip',
255 'dol_value_mysoc_town' => 'Town',
256 'dol_value_mysoc_country' => 'Country',
257 'dol_value_mysoc_idprof1' => 'ProfId1',
258 'dol_value_mysoc_idprof2' => 'ProfId2',
259 'dol_value_mysoc_idprof3' => 'ProfId3',
260 'dol_value_mysoc_idprof4' => 'ProfId4',
261 'dol_value_mysoc_idprof5' => 'ProfId5',
262 'dol_value_mysoc_idprof6' => 'ProfId6',
263 'dol_value_mysoc_tva_intra' => 'VATIntra',
264 'dol_value_mysoc_capital' => 'Capital',
265 'dol_value_mysoc_url' => 'Web',
266 'dol_value_vendor_lastname' => 'VendorLastname',
267 'dol_value_vendor_firstname' => 'VendorFirstname',
268 'dol_value_vendor_mail' => 'VendorEmail',
269 'dol_value_place' => 'DOL_VALUE_PLACE',
270 );
271 }
272
278 public function listPrinters()
279 {
280 global $conf;
281
282 $error = 0;
283 $line = 0;
284 $listofprinters = array();
285
286 $sql = "SELECT rowid, name, fk_type, fk_profile, parameter";
287 $sql .= " FROM ".$this->db->prefix()."printer_receipt";
288 $sql .= " WHERE entity = ".((int) $conf->entity);
289
290 $resql = $this->db->query($sql);
291
292 if ($resql) {
293 $num = $this->db->num_rows($resql);
294 while ($line < $num) {
295 $row = $this->db->fetch_array($resql);
296 if ($row) {
297 switch ($row['fk_type']) {
298 case 1:
299 $row['fk_type_name'] = 'CONNECTOR_DUMMY';
300 break;
301 case 2:
302 $row['fk_type_name'] = 'CONNECTOR_FILE_PRINT';
303 break;
304 case 3:
305 $row['fk_type_name'] = 'CONNECTOR_NETWORK_PRINT';
306 break;
307 case 4:
308 $row['fk_type_name'] = 'CONNECTOR_WINDOWS_PRINT';
309 break;
310 case 5:
311 $row['fk_type_name'] = 'CONNECTOR_CUPS_PRINT';
312 break;
313 default:
314 $row['fk_type_name'] = 'CONNECTOR_UNKNOWN';
315 break;
316 }
317 switch ($row['fk_profile']) {
318 case 0:
319 $row['fk_profile_name'] = 'PROFILE_DEFAULT';
320 break;
321 case 1:
322 $row['fk_profile_name'] = 'PROFILE_SIMPLE';
323 break;
324 case 2:
325 $row['fk_profile_name'] = 'PROFILE_EPOSTEP';
326 break;
327 case 3:
328 $row['fk_profile_name'] = 'PROFILE_P822D';
329 break;
330 default:
331 $row['fk_profile_name'] = 'PROFILE_STAR';
332 break;
333 }
334 $listofprinters[] = $row;
335 }
336 $line++;
337 }
338 } else {
339 $error++;
340 $this->errors[] = $this->db->lasterror;
341 }
342
343 $this->listprinters = $listofprinters;
344
345 return $error;
346 }
347
348
354 public function listPrintersTemplates()
355 {
356 global $conf;
357
358 $error = 0;
359 $line = 0;
360 $listofprinters = array();
361
362 $sql = "SELECT rowid, name, template";
363 $sql .= " FROM ".$this->db->prefix()."printer_receipt_template";
364 $sql .= " WHERE entity = ".$conf->entity;
365
366 $resql = $this->db->query($sql);
367
368 if ($resql) {
369 $num = $this->db->num_rows($resql);
370 while ($line < $num) {
371 $row = $this->db->fetch_array($resql);
372 if ($row) {
373 $listofprinters[] = $row;
374 }
375 $line++;
376 }
377 } else {
378 $error++;
379 $this->errors[] = $this->db->lasterror;
380 }
381
382 $this->listprinterstemplates = $listofprinters;
383
384 return $error;
385 }
386
387
395 public function selectTypePrinter($selected = '', $htmlname = 'printertypeid')
396 {
397 global $langs;
398
399 $options = array(
400 1 => $langs->trans('CONNECTOR_DUMMY'),
401 2 => $langs->trans('CONNECTOR_FILE_PRINT'),
402 3 => $langs->trans('CONNECTOR_NETWORK_PRINT'),
403 4 => $langs->trans('CONNECTOR_WINDOWS_PRINT'),
404 5 => $langs->trans('CONNECTOR_CUPS_PRINT'),
405 );
406
407 $this->resprint = Form::selectarray($htmlname, $options, $selected);
408
409 return 0;
410 }
411
412
420 public function selectProfilePrinter($selected = '', $htmlname = 'printerprofileid')
421 {
422 global $langs;
423
424 $options = array(
425 0 => $langs->trans('PROFILE_DEFAULT'),
426 1 => $langs->trans('PROFILE_SIMPLE'),
427 2 => $langs->trans('PROFILE_EPOSTEP'),
428 3 => $langs->trans('PROFILE_P822D'),
429 4 => $langs->trans('PROFILE_STAR'),
430 );
431
432 $this->profileresprint = Form::selectarray($htmlname, $options, $selected);
433 return 0;
434 }
435
436
446 public function addPrinter($name, $type, $profile, $parameter)
447 {
448 global $conf;
449 $error = 0;
450 $sql = "INSERT INTO ".$this->db->prefix()."printer_receipt";
451 $sql .= " (name, fk_type, fk_profile, parameter, entity)";
452 $sql .= " VALUES ('".$this->db->escape($name)."', ".((int) $type).", ".((int) $profile).", '".$this->db->escape($parameter)."', ".((int) $conf->entity).")";
453 $resql = $this->db->query($sql);
454 if (!$resql) {
455 $error++;
456 $this->errors[] = $this->db->lasterror;
457 }
458 return $error;
459 }
460
471 public function updatePrinter($name, $type, $profile, $parameter, $printerid)
472 {
473 $error = 0;
474
475 $sql = "UPDATE ".$this->db->prefix()."printer_receipt";
476 $sql .= " SET name='".$this->db->escape($name)."'";
477 $sql .= ", fk_type=".((int) $type);
478 $sql .= ", fk_profile=".((int) $profile);
479 $sql .= ", parameter='".$this->db->escape($parameter)."'";
480 $sql .= " WHERE rowid=".((int) $printerid);
481
482 $resql = $this->db->query($sql);
483 if (!$resql) {
484 $error++;
485 $this->errors[] = $this->db->lasterror;
486 }
487 return $error;
488 }
489
496 public function deletePrinter($printerid)
497 {
498 global $conf;
499 $error = 0;
500 $sql = 'DELETE FROM '.$this->db->prefix().'printer_receipt';
501 $sql .= ' WHERE rowid='.((int) $printerid);
502 $resql = $this->db->query($sql);
503 if (!$resql) {
504 $error++;
505 $this->errors[] = $this->db->lasterror;
506 }
507 return $error;
508 }
509
517 public function addTemplate($name, $template)
518 {
519 global $conf;
520 $error = 0;
521 $sql = "INSERT INTO ".$this->db->prefix()."printer_receipt_template";
522 $sql .= " (name, template, entity) VALUES ('".$this->db->escape($name)."'";
523 $sql .= ", '".$this->db->escape($template)."', ".$conf->entity.")";
524 $resql = $this->db->query($sql);
525 if (!$resql) {
526 $error++;
527 $this->errors[] = $this->db->lasterror;
528 }
529 return $error;
530 }
531
538 public function deleteTemplate($templateid)
539 {
540 global $conf;
541 $error = 0;
542 $sql = 'DELETE FROM '.$this->db->prefix().'printer_receipt_template';
543 $sql .= " WHERE rowid = ".((int) $templateid);
544 $sql .= " AND entity = ".$conf->entity;
545 $resql = $this->db->query($sql);
546 if (!$resql) {
547 $error++;
548 $this->errors[] = $this->db->lasterror;
549 }
550 return $error;
551 }
552
561 public function updateTemplate($name, $template, $templateid)
562 {
563 global $conf;
564 $error = 0;
565
566 $sql = "UPDATE ".$this->db->prefix()."printer_receipt_template";
567 $sql .= " SET name='".$this->db->escape($name)."'";
568 $sql .= ", template='".$this->db->escape($template)."'";
569 $sql .= " WHERE rowid=".((int) $templateid);
570 $resql = $this->db->query($sql);
571 if (!$resql) {
572 $error++;
573 $this->errors[] = $this->db->lasterror;
574 }
575 return $error;
576 }
577
578
586 public function sendTestToPrinter($printerid, $addimgandbarcode = 0)
587 {
588 $error = 0;
589 $img = EscposImage::load(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo_bw.png');
590 //$this->profile = CapabilityProfile::load("TM-T88IV");
591 $ret = $this->initPrinter($printerid);
592 if ($ret > 0) {
593 setEventMessages($this->error, $this->errors, 'errors');
594 } else {
595 try {
596 if ($addimgandbarcode) {
597 $this->printer->bitImage($img);
598 }
599 $this->printer->text("Hello World!\n");
600 if ($addimgandbarcode) {
601 $testStr = "1234567890";
602 $this->printer->barcode($testStr);
603 //$this->printer->qrcode($testStr, Printer::QR_ECLEVEL_M, 5, Printer::QR_MODEL_1);
604 }
605 $this->printer->text("\n");
606 $this->printer->text("Most simple example\n");
607 $this->printer->feed();
608 $this->printer->cut();
609
610 // If is DummyPrintConnector send to log to debugging
611 if ($this->printer->connector instanceof DummyPrintConnector) {
612 $data = $this->printer->connector->getData();
613 dol_syslog($data);
614 }
615 // Close and print
616 $this->printer->close();
617 } catch (Exception $e) {
618 $this->errors[] = $e->getMessage();
619 $error++;
620 }
621 }
622 return $error;
623 }
624
633 public function sendToPrinter($object, $templateid, $printerid)
634 {
635 global $mysoc, $langs, $user;
636 global $hookmanager;
637
638 $langs->load('bills');
639
640 $error = 0;
641 $ret = $this->loadTemplate($templateid);
642
643 $now = dol_now('tzuser');
644 // tags a remplacer par leur valeur avant de parser (dol_value_xxx)
645 $this->template = str_replace('{dol_value_object_id}', (string) $object->id, $this->template);
646 $this->template = str_replace('{dol_value_object_ref}', $object->ref, $this->template);
647 //$this->template = str_replace('<dol_value_object_points>', $object->points, $this->template);
648 $this->template = str_replace('{dol_value_date}', dol_print_date($object->date, 'day'), $this->template);
649 $this->template = str_replace('{dol_value_date_time}', dol_print_date($object->date, 'dayhour'), $this->template);
650 $this->template = str_replace('{dol_value_year}', dol_print_date($object->date, '%Y'), $this->template);
651 $this->template = str_replace('{dol_value_month_letters}', $langs->trans("Month".dol_print_date($object->date, '%m')), $this->template);
652 $this->template = str_replace('{dol_value_month}', dol_print_date($object->date, '%m'), $this->template);
653 $this->template = str_replace('{dol_value_day}', dol_print_date($object->date, '%d'), $this->template);
654 $this->template = str_replace('{dol_value_day_letters}', $langs->trans("Day".dol_print_date($object->date, '%m')[1]), $this->template);
655
656 $this->template = str_replace('{dol_value_currentdate}', dol_print_date($now, 'dayhour'), $this->template);
657 $this->template = str_replace('{dol_value_currentdate_notime}', dol_print_date($now, 'day'), $this->template);
658 $this->template = str_replace('{dol_value_currentdate_letters}', dol_print_date($now, 'dayhourtext'), $this->template);
659 $this->template = str_replace('{dol_value_currentyear}', dol_print_date($now, '%Y'), $this->template);
660 $this->template = str_replace('{dol_value_currentmonth_letters}', $langs->trans("Month".dol_print_date($now, '%m')), $this->template);
661 $this->template = str_replace('{dol_value_currentmonth}', dol_print_date($now, '%m'), $this->template);
662 $this->template = str_replace('{dol_value_currentday}', dol_print_date($now, '%d'), $this->template);
663 $this->template = str_replace('{dol_value_currentday_letters}', $langs->trans("Day".dol_print_date($now, '%m')[1]), $this->template);
664
665 $this->template = str_replace('{dol_value_customer_firstname}', $object->thirdparty->firstname, $this->template);
666 $this->template = str_replace('{dol_value_customer_lastname}', $object->thirdparty->lastname, $this->template);
667 $this->template = str_replace('{dol_value_customer_mail}', $object->thirdparty->email, $this->template);
668 $this->template = str_replace('{dol_value_customer_phone}', $object->thirdparty->phone, $this->template);
669 //$this->template = str_replace('<dol_value_customer_mobile>', $object->thirdparty->mobile, $this->template);
670 $this->template = str_replace('{dol_value_customer_tax_number}', $object->thirdparty->tva_intra, $this->template);
671 //$this->template = str_replace('<dol_value_customer_account_balance>', $object->customer_account_balance, $this->template);
672 //$this->template = str_replace('<dol_value_customer_points>', $object->customer_points, $this->template);
673
674 $this->template = str_replace('{dol_value_mysoc_name}', $mysoc->name, $this->template);
675 $this->template = str_replace('{dol_value_mysoc_address}', $mysoc->address, $this->template);
676 $this->template = str_replace('{dol_value_mysoc_zip}', $mysoc->zip, $this->template);
677 $this->template = str_replace('{dol_value_mysoc_town}', $mysoc->town, $this->template);
678 $this->template = str_replace('{dol_value_mysoc_country}', $mysoc->country, $this->template);
679 $this->template = str_replace('{dol_value_mysoc_idprof1}', $mysoc->idprof1, $this->template);
680 $this->template = str_replace('{dol_value_mysoc_idprof2}', $mysoc->idprof2, $this->template);
681 $this->template = str_replace('{dol_value_mysoc_idprof3}', $mysoc->idprof3, $this->template);
682 $this->template = str_replace('{dol_value_mysoc_idprof4}', $mysoc->idprof4, $this->template);
683 $this->template = str_replace('{dol_value_mysoc_idprof5}', $mysoc->idprof5, $this->template);
684 $this->template = str_replace('{dol_value_mysoc_idprof6}', $mysoc->idprof6, $this->template);
685 $this->template = str_replace('{dol_value_mysoc_tva_intra}', $mysoc->tva_intra, $this->template);
686 $this->template = str_replace('{dol_value_mysoc_capital}', (string) $mysoc->capital, $this->template);
687 $this->template = str_replace('{dol_value_mysoc_url}', $mysoc->url, $this->template);
688
689 $this->template = str_replace('{dol_value_vendor_firstname}', $user->firstname, $this->template);
690 $this->template = str_replace('{dol_value_vendor_lastname}', $user->lastname, $this->template);
691 $this->template = str_replace('{dol_value_vendor_mail}', $user->email, $this->template);
692
693 $parameters = array('object' => $object);
694 $action = '';
695 $reshook = $hookmanager->executeHooks('sendToPrinterBefore', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
696 if ($reshook < 0) {
697 $this->error = "Error in hook dolReceiptPrinter sendToPrinterBefore ".$reshook;
698 dol_syslog("dolReceiptPrinter::sendToPrinter: error=".$this->error, LOG_ERR);
699 return $reshook;
700 }
701
702 // escape special characters for xml_parse_into_struct
703 $this->template = htmlspecialchars($this->template, ENT_QUOTES | ENT_XML1);
704
705 // parse template
706 $this->template = str_replace("{", "<", $this->template);
707 $this->template = str_replace("}", ">", $this->template);
708
709 if (LIBXML_VERSION < 20900) {
710 // Avoid load of external entities (security problem).
711 // Required only if LIBXML_VERSION < 20900
712 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
713 libxml_disable_entity_loader(true);
714 }
715
716 $vals = array();
717 $index = array();
718
719 $p = xml_parser_create();
720 xml_parse_into_struct($p, $this->template, $vals, $index);
721 xml_parser_free($p);
722
723 //print '<pre>'.print_r($index, true).'</pre>';
724 //print '<pre>'.print_r($vals, true).'</pre>';
725 // print ticket
726 $nbcharactbyline = getDolGlobalInt('RECEIPT_PRINTER_NB_CHARACT_BY_LINE', 48);
727 $ret = $this->initPrinter($printerid);
728
729 if ($ret > 0) {
730 setEventMessages($this->error, $this->errors, 'errors');
731 } else {
732 $nboflines = count($vals);
733 for ($tplline = 0; $tplline < $nboflines; $tplline++) {
734 //var_dump($vals[$tplline]['value']);
735 switch ($vals[$tplline]['tag']) {
736 case 'DOL_PRINT_TEXT':
737 $this->printer->text($vals[$tplline]['value']);
738 break;
739 case 'DOL_PRINT_OBJECT_LINES':
740 foreach ($object->lines as $line) {
741 if ($line->fk_product) {
742 $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen((string) $line->qty) - strlen(price($line->subprice)) - 10 - 1;
743 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
744 $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");
745 $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n \n");
746 } else {
747 $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen((string) $line->qty) - strlen(price($line->subprice)) - 10 - 1;
748 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
749 $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");
750 }
751 }
752 break;
753 case 'DOL_PRINT_OBJECT_LINES_WITH_NOTES':
754 foreach ($object->lines as $line) {
755 if ($line->fk_product) {
756 $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen((string) $line->qty) - 10 - 1;
757 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
758 $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
759 $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n");
760 $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen((string) $line->qty) - 10 - 1;
761 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
762 $this->printer->text($line->description."\n");
763 } else {
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.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
767 }
768 }
769 break;
770 case 'DOL_PRINT_OBJECT_TAX':
771 //var_dump($object);
772 $vatarray = array();
773 foreach ($object->lines as $line) {
774 $vatarray[$line->tva_tx] += $line->total_tva;
775 }
776 foreach ($vatarray as $vatkey => $vatvalue) {
777 $spacestoadd = $nbcharactbyline - strlen($vatkey) - 12;
778 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
779 $this->printer->text($spaces.$vatkey.'% '.str_pad(price($vatvalue), 10, ' ', STR_PAD_LEFT)."\n");
780 }
781 break;
782 case 'DOL_PRINT_OBJECT_TAX1':
783 //var_dump($object);
784 $total_localtax1 = 0;
785 foreach ($object->lines as $line) {
786 $total_localtax1 += $line->total_localtax1;
787 }
788 $this->printer->text(str_pad(price($total_localtax1), 10, ' ', STR_PAD_LEFT)."\n");
789 break;
790 case 'DOL_PRINT_OBJECT_TAX2':
791 //var_dump($object);
792 $total_localtax2 = 0;
793 foreach ($object->lines as $line) {
794 $total_localtax2 += $line->total_localtax2;
795 }
796 $this->printer->text(str_pad(price($total_localtax2), 10, ' ', STR_PAD_LEFT)."\n");
797 break;
798 case 'DOL_PRINT_OBJECT_TOTAL':
799 $title = $langs->trans('TotalHT');
800 $spacestoadd = $nbcharactbyline - strlen($title) - 10;
801 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
802 $this->printer->text($title.$spaces.str_pad(price($object->total_ht), 10, ' ', STR_PAD_LEFT)."\n");
803 $title = $langs->trans('TotalVAT');
804 $spacestoadd = $nbcharactbyline - strlen($title) - 10;
805 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
806 $this->printer->text($title.$spaces.str_pad(price($object->total_tva), 10, ' ', STR_PAD_LEFT)."\n");
807 $title = $langs->trans('TotalTTC');
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_ttc), 10, ' ', STR_PAD_LEFT)."\n");
811 break;
812 case 'DOL_PRINT_CURR_DATE':
813 if (strlen($vals[$tplline]['value']) < 2) {
814 $this->printer->text(date('d/m/Y H:i:s')."\n");
815 } else {
816 $this->printer->text(date($vals[$tplline]['value'])."\n");
817 }
818 break;
819 case 'DOL_LINE_FEED':
820 $this->printer->feed();
821 break;
822 case 'DOL_LINE_FEED_REVERSE':
823 $this->printer->feedReverse();
824 break;
825 case 'DOL_ALIGN_CENTER':
826 $this->printer->setJustification(Printer::JUSTIFY_CENTER);
827 break;
828 case 'DOL_ALIGN_RIGHT':
829 $this->printer->setJustification(Printer::JUSTIFY_RIGHT);
830 break;
831 case 'DOL_ALIGN_LEFT':
832 $this->printer->setJustification(Printer::JUSTIFY_LEFT);
833 break;
834 case 'DOL_OPEN_DRAWER':
835 $this->printer->pulse();
836 break;
837 case 'DOL_ACTIVATE_BUZZER':
838 //$this->printer->buzzer();
839 break;
840 case 'DOL_PRINT_BARCODE':
841 // $vals[$tplline]['value'] -> barcode($content, $type)
842 // var_dump($vals[$tplline]['value']);
843 try {
844 $this->printer->barcode($vals[$tplline]['value']);
845 } catch (Exception $e) {
846 $this->errors[] = 'Invalid Barcode value: '.$vals[$tplline]['value'];
847 $error++;
848 }
849 break;
850 case 'DOL_PRINT_LOGO':
851 $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
852 $this->printer->graphics($img);
853 break;
854 case 'DOL_PRINT_LOGO_OLD':
855 $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
856 $this->printer->bitImage($img);
857 break;
858 case 'DOL_PRINT_LOGO_OLD_CF':
859 $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
860 $this->printer->bitImageColumnFormat($img);
861 break;
862 case 'DOL_PRINT_QRCODE':
863 // $vals[$tplline]['value'] -> qrCode($content, $ec, $size, $model)
864 $this->printer->qrcode($vals[$tplline]['value']);
865 break;
866 case 'DOL_CUT_PAPER_FULL':
867 $this->printer->cut(Printer::CUT_FULL);
868 break;
869 case 'DOL_CUT_PAPER_PARTIAL':
870 $this->printer->cut(Printer::CUT_PARTIAL);
871 break;
872 case 'DOL_USE_FONT_A':
873 $this->printer->setFont(Printer::FONT_A);
874 break;
875 case 'DOL_USE_FONT_B':
876 $this->printer->setFont(Printer::FONT_B);
877 break;
878 case 'DOL_USE_FONT_C':
879 $this->printer->setFont(Printer::FONT_C);
880 break;
881 case 'DOL_BOLD':
882 $this->printer->setEmphasis(true);
883 break;
884 case 'DOL_BOLD_DISABLED':
885 $this->printer->setEmphasis(false);
886 break;
887 case 'DOL_DOUBLE_HEIGHT':
888 $this->printer->setTextSize(1, 2);
889 break;
890 case 'DOL_DOUBLE_WIDTH':
891 $this->printer->setTextSize(2, 1);
892 break;
893 case 'DOL_DEFAULT_HEIGHT_WIDTH':
894 $this->printer->setTextSize(1, 1);
895 break;
896 case 'DOL_UNDERLINE':
897 $this->printer->setUnderline(true);
898 break;
899 case 'DOL_UNDERLINE_DISABLED':
900 $this->printer->setUnderline(false);
901 break;
902 case 'DOL_BEEP':
903 $this->printer->getPrintConnector() -> write("\x1e");
904 break;
905 case 'DOL_BEEP_ALTERNATIVE': //if DOL_BEEP not works
906 $this->printer->getPrintConnector() -> write(Printer::ESC . "B" . chr(4) . chr(1));
907 break;
908 case 'DOL_PRINT_ORDER_LINES':
909 foreach ($object->lines as $line) {
910 if ($line->special_code == $this->orderprinter) {
911 $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen((string) $line->qty) - 10 - 1;
912 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
913 $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
914 $this->printer->text(strip_tags(htmlspecialchars_decode($line->desc))."\n");
915 }
916 }
917 break;
918 case 'DOL_PRINT_PAYMENT':
919 $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,";
920 $sql .= " cp.code";
921 $sql .= " FROM ".$this->db->prefix()."paiement_facture as pf, ".$this->db->prefix()."paiement as p";
922 $sql .= " LEFT JOIN ".$this->db->prefix()."c_paiement as cp ON p.fk_paiement = cp.id";
923 $sql .= " WHERE pf.fk_paiement = p.rowid AND pf.fk_facture = ".((int) $object->id);
924 $sql .= " ORDER BY p.datep";
925 $resql = $this->db->query($sql);
926 if ($resql) {
927 $num = $this->db->num_rows($resql);
928 $i = 0;
929 while ($i < $num) {
930 $row = $this->db->fetch_object($resql);
931 $spacestoadd = $nbcharactbyline - strlen($langs->transnoentitiesnoconv("PaymentTypeShort".$row->code)) - 12;
932 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
933 $amount_payment = (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? $row->multicurrency_amount : $row->amount;
934 if ($row->code == "LIQ") {
935 $amount_payment = $amount_payment + $row->pos_change; // Show amount with excess received if is cash payment
936 }
937 $this->printer->text($spaces.$langs->transnoentitiesnoconv("PaymentTypeShort".$row->code).' '.str_pad(price($amount_payment), 10, ' ', STR_PAD_LEFT)."\n");
938 if ($row->code == "LIQ" && $row->pos_change > 0) { // Print change only in cash payments
939 $spacestoadd = $nbcharactbyline - strlen($langs->trans("Change")) - 12;
940 $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
941 $this->printer->text($spaces.$langs->trans("Change").' '.str_pad(price($row->pos_change), 10, ' ', STR_PAD_LEFT)."\n");
942 }
943 $i++;
944 }
945 }
946 break;
947 case 'DOL_VALUE_PLACE':
948 $sql = "SELECT floor, label FROM ".$this->db->prefix()."takepos_floor_tables where rowid=".((int) str_replace(")", "", str_replace("(PROV-POS".$_SESSION["takeposterminal"]."-", "", $object->ref)));
949 $resql = $this->db->query($sql);
950 $obj = $this->db->fetch_object($resql);
951 if ($obj) {
952 $this->printer->text($obj->label);
953 }
954 break;
955 default:
956 $parameters = array('vals' => $vals[$tplline],'object' => $object,'nbcharactbyline' => $nbcharactbyline);
957 $action = '';
958 $reshook = $hookmanager->executeHooks('sendToPrinterAfter', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
959
960 if (!$reshook || $reshook < 0) {
961 $this->printer->text($vals[$tplline]['tag']);
962 $this->printer->text($vals[$tplline]['value']);
963 $this->errors[] = 'UnknowTag: &lt;'.strtolower($vals[$tplline]['tag']).'&gt;';
964 $error++;
965 }
966 break;
967 }
968 }
969 // If is DummyPrintConnector send to log to debugging
970 if ($this->printer->connector instanceof DummyPrintConnector || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
971 $data = $this->printer->connector->getData();
972 if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
973 echo rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
974 }
975 dol_syslog($data);
976 }
977 // Close and print
978 $this->printer->close();
979 }
980 return $error;
981 }
982
989 public function loadTemplate($templateid)
990 {
991 $error = 0;
992
993 $sql = "SELECT template";
994 $sql .= " FROM ".$this->db->prefix()."printer_receipt_template";
995 $sql .= " WHERE rowid = ".((int) $templateid);
996 $sql .= " AND entity IN (".getEntity('printer_receipt_template').")";
997
998 $resql = $this->db->query($sql);
999 if ($resql) {
1000 $obj = $this->db->fetch_object($resql);
1001 } else {
1002 $error++;
1003 $this->errors[] = $this->db->lasterror;
1004 }
1005 if (empty($obj)) {
1006 $error++;
1007 $this->errors[] = 'TemplateDontExist';
1008 } else {
1009 $this->template = $obj->template;
1010 }
1011
1012 return $error;
1013 }
1014
1015
1022 public function initPrinter($printerid)
1023 {
1024 if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
1025 $this->connector = new DummyPrintConnector();
1026 $this->printer = new Printer($this->connector, $this->profile);
1027 return;
1028 }
1029
1030 $error = 0;
1031 $sql = "SELECT rowid, name, fk_type, fk_profile, parameter";
1032 $sql .= " FROM ".$this->db->prefix()."printer_receipt";
1033 $sql .= " WHERE rowid = ".((int) $printerid);
1034 $sql .= " AND entity IN (".getEntity('printer_receipt').")";
1035
1036 $resql = $this->db->query($sql);
1037 if ($resql) {
1038 $obj = $this->db->fetch_array($resql);
1039 if (empty($obj)) {
1040 $error++;
1041 $this->errors[] = 'PrinterDontExist';
1042 }
1043 if (!$error) {
1044 $parameter = (isset($obj['parameter']) ? $obj['parameter'] : '');
1045
1046 dol_syslog("initPrinter printerid=".$printerid." parameter=".$parameter);
1047
1048 try {
1049 $type = empty($obj['fk_type']) ? 0 : (int) $obj['fk_type'];
1050 $found = true;
1051 switch ($type) {
1052 case 1:
1053 $this->connector = new DummyPrintConnector();
1054 break;
1055 case 2:
1056 $this->connector = new FilePrintConnector($parameter);
1057 break;
1058 case 3:
1059 $parameters = explode(':', $parameter);
1060 $this->connector = new NetworkPrintConnector($parameters[0], $parameters[1]);
1061 break;
1062 case 4: // LPT1, smb://...
1063 $this->connector = new WindowsPrintConnector(dol_sanitizePathName($parameter));
1064 break;
1065 case 5:
1066 $this->connector = new CupsPrintConnector($parameter);
1067 break;
1068 default:
1069 $found = false;
1070 break;
1071 }
1072 if ($found) {
1073 $this->printer = new Printer($this->connector, $this->profile);
1074 } else {
1075 $error++;
1076 $this->errors[] = 'CONNECTOR_UNKNOWN';
1077 }
1078 } catch (Exception $e) {
1079 $this->errors[] = $e->getMessage();
1080 $error++;
1081 }
1082 }
1083 } else {
1084 $error++;
1085 $this->errors[] = $this->db->lasterror;
1086 }
1087
1088 return $error;
1089 }
1090}
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.
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).
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return 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.