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