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