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