dolibarr  17.0.4
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_skype> Replaced by customer skype
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 
108 require_once DOL_DOCUMENT_ROOT.'/includes/mike42/escpos-php/autoload.php';
109 use Mike42\Escpos\PrintConnectors\FilePrintConnector;
110 use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
111 use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
112 use Mike42\Escpos\PrintConnectors\CupsPrintConnector;
113 use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
114 use Mike42\Escpos\CapabilityProfile;
115 use Mike42\Escpos\Printer;
116 use Mike42\Escpos\EscposImage;
117 
121 class dolReceiptPrinter extends Printer
122 {
123  const CONNECTOR_DUMMY = 1;
124  const CONNECTOR_FILE_PRINT = 2;
125  const CONNECTOR_NETWORK_PRINT = 3;
126  const CONNECTOR_WINDOWS_PRINT = 4;
127  const CONNECTOR_CUPS_PRINT = 5;
128 
132  public $db;
133 
134  /*
135  * @var string[] array of tags
136  */
137  public $tags;
138  public $printer;
139  public $template;
140 
145  public $orderprinter;
146 
150  public $error = '';
151 
155  public $errors = array();
156 
162  public function __construct($db)
163  {
164  $this->db = $db;
165  $this->tags = array(
166  'dol_line_feed' => 'DOL_LINE_FEED',
167  'dol_line_feed_reverse' => 'DOL_LINE_FEED_REVERSE',
168  'dol_align_left' => 'DOL_ALIGN_LEFT',
169  'dol_align_center' => 'DOL_ALIGN_CENTER',
170  'dol_align_right' => 'DOL_ALIGN_RIGHT',
171  'dol_use_font_a' => 'DOL_USE_FONT_A',
172  'dol_use_font_b' => 'DOL_USE_FONT_B',
173  'dol_use_font_c' => 'DOL_USE_FONT_C',
174  'dol_bold' => 'DOL_BOLD',
175  'dol_bold_disabled' => 'DOL_BOLD_DISABLED',
176  'dol_double_height' => 'DOL_DOUBLE_HEIGHT',
177  'dol_double_width' => 'DOL_DOUBLE_WIDTH',
178  'dol_default_height_width' => 'DOL_DEFAULT_HEIGHT_WIDTH',
179  'dol_underline' => 'DOL_UNDERLINE',
180  'dol_underline_disabled' => 'DOL_UNDERLINE_DISABLED',
181  'dol_cut_paper_full' => 'DOL_CUT_PAPER_FULL',
182  'dol_cut_paper_partial' => 'DOL_CUT_PAPER_PARTIAL',
183  'dol_open_drawer' => 'DOL_OPEN_DRAWER',
184  'dol_beep' => 'DOL_BEEP',
185  'dol_beep_alternative' => 'DOL_BEEP_ALTERNATIVE',
186  'dol_print_text' => 'DOL_PRINT_TEXT',
187  'dol_print_barcode' => 'DOL_PRINT_BARCODE',
188  'dol_value_date' => 'DateInvoice',
189  'dol_value_date_time' => 'DateInvoiceWithTime',
190  'dol_value_year' => 'YearInvoice',
191  'dol_value_month_letters' => 'DOL_VALUE_MONTH_LETTERS',
192  'dol_value_month' => 'DOL_VALUE_MONTH',
193  'dol_value_day' => 'DOL_VALUE_DAY',
194  'dol_value_day_letters' => 'DOL_VALUE_DAY',
195  'dol_value_currentdate' => 'DOL_VALUE_CURRENTDATE',
196  'dol_value_currentdate_notime' => 'CurrentDateWithTime',
197  'dol_value_currentdate_letters' => 'DOL_VALUE_CURRENTDATE_LETTERS',
198  'dol_value_currentyear' => 'CurrentYear',
199  'dol_value_currentmonth_letters' => 'DOL_VALUE_CURRENT_MONTH_LETTERS',
200  'dol_value_currentmonth' => 'DOL_VALUE_CURRENT_MONTH',
201  'dol_value_currentday' => 'DOL_VALUE_CURRENT_DAY',
202  'dol_value_currentday_letters' => 'DOL_VALUE_CURRENT_DAY',
203  'dol_print_payment' => 'DOL_PRINT_PAYMENT',
204  'dol_print_curr_date' => 'DOL_PRINT_CURR_DATE',
205  'dol_print_logo' => 'DOL_PRINT_LOGO',
206  'dol_print_logo_old' => 'DOL_PRINT_LOGO_OLD',
207  'dol_print_logo_old_cf' => 'DOL_PRINT_LOGO_OLD_CF',
208  'dol_value_object_id' => 'InvoiceID',
209  'dol_value_object_ref' => 'InvoiceRef',
210  'dol_print_object_lines' => 'DOL_PRINT_OBJECT_LINES',
211  'dol_print_object_lines_with_notes' => 'DOL_PRINT_OBJECT_LINES_WITH_NOTES',
212  'dol_print_object_tax' => 'TotalVAT',
213  'dol_print_object_local_tax1' => 'TotalLT1',
214  'dol_print_object_local_tax2' => 'TotalLT2',
215  'dol_print_object_total' => 'Total',
216  'dol_print_object_number' => 'DOL_PRINT_OBJECT_NUMBER',
217  //'dol_value_object_points' => 'DOL_VALUE_OBJECT_POINTS',
218  'dol_print_order_lines' => 'DOL_PRINT_ORDER_LINES',
219  'dol_value_customer_firstname' => 'DOL_VALUE_CUSTOMER_FIRSTNAME',
220  'dol_value_customer_lastname' => 'DOL_VALUE_CUSTOMER_LASTNAME',
221  'dol_value_customer_mail' => 'DOL_VALUE_CUSTOMER_MAIL',
222  'dol_value_customer_phone' => 'DOL_VALUE_CUSTOMER_PHONE',
223  'dol_value_customer_skype' => 'DOL_VALUE_CUSTOMER_SKYPE',
224  'dol_value_customer_tax_number' => 'DOL_VALUE_CUSTOMER_TAX_NUMBER',
225  //'dol_value_customer_account_balance' => 'DOL_VALUE_CUSTOMER_ACCOUNT_BALANCE',
226  //'dol_value_customer_points' => 'DOL_VALUE_CUSTOMER_POINTS',
227  'dol_value_mysoc_name' => 'DOL_VALUE_MYSOC_NAME',
228  'dol_value_mysoc_address' => 'Address',
229  'dol_value_mysoc_zip' => 'Zip',
230  'dol_value_mysoc_town' => 'Town',
231  'dol_value_mysoc_country' => 'Country',
232  'dol_value_mysoc_idprof1' => 'ProfId1',
233  'dol_value_mysoc_idprof2' => 'ProfId2',
234  'dol_value_mysoc_idprof3' => 'ProfId3',
235  'dol_value_mysoc_idprof4' => 'ProfId4',
236  'dol_value_mysoc_idprof5' => 'ProfId5',
237  'dol_value_mysoc_idprof6' => 'ProfId6',
238  'dol_value_mysoc_tva_intra' => 'VATIntra',
239  'dol_value_mysoc_capital' => 'Capital',
240  'dol_value_mysoc_url' => 'Web',
241  'dol_value_vendor_lastname' => 'VendorLastname',
242  'dol_value_vendor_firstname' => 'VendorFirstname',
243  'dol_value_vendor_mail' => 'VendorEmail',
244  'dol_value_place' => 'DOL_VALUE_PLACE',
245  );
246  }
247 
253  public function listPrinters()
254  {
255  global $conf;
256  $error = 0;
257  $line = 0;
258  $obj = array();
259  $sql = "SELECT rowid, name, fk_type, fk_profile, parameter";
260  $sql .= " FROM ".$this->db->prefix()."printer_receipt";
261  $sql .= " WHERE entity = ".$conf->entity;
262  $resql = $this->db->query($sql);
263  if ($resql) {
264  $num = $this->db->num_rows($resql);
265  while ($line < $num) {
266  $row = $this->db->fetch_array($resql);
267  switch ($row['fk_type']) {
268  case 1:
269  $row['fk_type_name'] = 'CONNECTOR_DUMMY';
270  break;
271  case 2:
272  $row['fk_type_name'] = 'CONNECTOR_FILE_PRINT';
273  break;
274  case 3:
275  $row['fk_type_name'] = 'CONNECTOR_NETWORK_PRINT';
276  break;
277  case 4:
278  $row['fk_type_name'] = 'CONNECTOR_WINDOWS_PRINT';
279  break;
280  case 5:
281  $row['fk_type_name'] = 'CONNECTOR_CUPS_PRINT';
282  break;
283  default:
284  $row['fk_type_name'] = 'CONNECTOR_UNKNOWN';
285  break;
286  }
287  switch ($row['fk_profile']) {
288  case 0:
289  $row['fk_profile_name'] = 'PROFILE_DEFAULT';
290  break;
291  case 1:
292  $row['fk_profile_name'] = 'PROFILE_SIMPLE';
293  break;
294  case 2:
295  $row['fk_profile_name'] = 'PROFILE_EPOSTEP';
296  break;
297  case 3:
298  $row['fk_profile_name'] = 'PROFILE_P822D';
299  break;
300  default:
301  $row['fk_profile_name'] = 'PROFILE_STAR';
302  break;
303  }
304  $obj[] = $row;
305  $line++;
306  }
307  } else {
308  $error++;
309  $this->errors[] = $this->db->lasterror;
310  }
311  $this->listprinters = $obj;
312  return $error;
313  }
314 
315 
321  public function listPrintersTemplates()
322  {
323  global $conf;
324  $error = 0;
325  $line = 0;
326  $obj = array();
327  $sql = "SELECT rowid, name, template";
328  $sql .= " FROM ".$this->db->prefix()."printer_receipt_template";
329  $sql .= " WHERE entity = ".$conf->entity;
330  $resql = $this->db->query($sql);
331  if ($resql) {
332  $num = $this->db->num_rows($resql);
333  while ($line < $num) {
334  $obj[] = $this->db->fetch_array($resql);
335  $line++;
336  }
337  } else {
338  $error++;
339  $this->errors[] = $this->db->lasterror;
340  }
341  $this->listprinterstemplates = $obj;
342  return $error;
343  }
344 
345 
353  public function selectTypePrinter($selected = '', $htmlname = 'printertypeid')
354  {
355  global $langs;
356 
357  $options = array(
358  1 => $langs->trans('CONNECTOR_DUMMY'),
359  2 => $langs->trans('CONNECTOR_FILE_PRINT'),
360  3 => $langs->trans('CONNECTOR_NETWORK_PRINT'),
361  4 => $langs->trans('CONNECTOR_WINDOWS_PRINT'),
362  5 => $langs->trans('CONNECTOR_CUPS_PRINT'),
363  );
364 
365  $this->resprint = Form::selectarray($htmlname, $options, $selected);
366 
367  return 0;
368  }
369 
370 
378  public function selectProfilePrinter($selected = '', $htmlname = 'printerprofileid')
379  {
380  global $langs;
381 
382  $options = array(
383  0 => $langs->trans('PROFILE_DEFAULT'),
384  1 => $langs->trans('PROFILE_SIMPLE'),
385  2 => $langs->trans('PROFILE_EPOSTEP'),
386  3 => $langs->trans('PROFILE_P822D'),
387  4 => $langs->trans('PROFILE_STAR'),
388  );
389 
390  $this->profileresprint = Form::selectarray($htmlname, $options, $selected);
391  return 0;
392  }
393 
394 
404  public function addPrinter($name, $type, $profile, $parameter)
405  {
406  global $conf;
407  $error = 0;
408  $sql = "INSERT INTO ".$this->db->prefix()."printer_receipt";
409  $sql .= " (name, fk_type, fk_profile, parameter, entity)";
410  $sql .= " VALUES ('".$this->db->escape($name)."', ".((int) $type).", ".((int) $profile).", '".$this->db->escape($parameter)."', ".((int) $conf->entity).")";
411  $resql = $this->db->query($sql);
412  if (!$resql) {
413  $error++;
414  $this->errors[] = $this->db->lasterror;
415  }
416  return $error;
417  }
418 
429  public function updatePrinter($name, $type, $profile, $parameter, $printerid)
430  {
431  global $conf;
432  $error = 0;
433 
434  $sql = "UPDATE ".$this->db->prefix()."printer_receipt";
435  $sql .= " SET name='".$this->db->escape($name)."'";
436  $sql .= ", fk_type=".((int) $type);
437  $sql .= ", fk_profile=".((int) $profile);
438  $sql .= ", parameter='".$this->db->escape($parameter)."'";
439  $sql .= " WHERE rowid=".((int) $printerid);
440 
441  $resql = $this->db->query($sql);
442  if (!$resql) {
443  $error++;
444  $this->errors[] = $this->db->lasterror;
445  }
446  return $error;
447  }
448 
455  public function deletePrinter($printerid)
456  {
457  global $conf;
458  $error = 0;
459  $sql = 'DELETE FROM '.$this->db->prefix().'printer_receipt';
460  $sql .= ' WHERE rowid='.((int) $printerid);
461  $resql = $this->db->query($sql);
462  if (!$resql) {
463  $error++;
464  $this->errors[] = $this->db->lasterror;
465  }
466  return $error;
467  }
468 
476  public function addTemplate($name, $template)
477  {
478  global $conf;
479  $error = 0;
480  $sql = "INSERT INTO ".$this->db->prefix()."printer_receipt_template";
481  $sql .= " (name, template, entity) VALUES ('".$this->db->escape($name)."'";
482  $sql .= ", '".$this->db->escape($template)."', ".$conf->entity.")";
483  $resql = $this->db->query($sql);
484  if (!$resql) {
485  $error++;
486  $this->errors[] = $this->db->lasterror;
487  }
488  return $error;
489  }
490 
497  public function deleteTemplate($templateid)
498  {
499  global $conf;
500  $error = 0;
501  $sql = 'DELETE FROM '.$this->db->prefix().'printer_receipt_template';
502  $sql .= " WHERE rowid = ".((int) $templateid);
503  $sql .= " AND entity = ".$conf->entity;
504  $resql = $this->db->query($sql);
505  if (!$resql) {
506  $error++;
507  $this->errors[] = $this->db->lasterror;
508  }
509  return $error;
510  }
511 
520  public function updateTemplate($name, $template, $templateid)
521  {
522  global $conf;
523  $error = 0;
524 
525  $sql = "UPDATE ".$this->db->prefix()."printer_receipt_template";
526  $sql .= " SET name='".$this->db->escape($name)."'";
527  $sql .= ", template='".$this->db->escape($template)."'";
528  $sql .= " WHERE rowid=".((int) $templateid);
529  $resql = $this->db->query($sql);
530  if (!$resql) {
531  $error++;
532  $this->errors[] = $this->db->lasterror;
533  }
534  return $error;
535  }
536 
537 
544  public function sendTestToPrinter($printerid)
545  {
546  global $conf;
547 
548  $error = 0;
549  $img = EscposImage::load(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo_bw.png');
550  //$this->profile = CapabilityProfile::load("TM-T88IV");
551  $ret = $this->initPrinter($printerid);
552  if ($ret > 0) {
553  setEventMessages($this->error, $this->errors, 'errors');
554  } else {
555  try {
556  $this->printer->bitImage($img);
557  $this->printer->text("Hello World!\n");
558  $testStr = "1234567890";
559  $this->printer->barcode($testStr);
560  //$this->printer->qrcode($testStr, Printer::QR_ECLEVEL_M, 5, Printer::QR_MODEL_1);
561  $this->printer->text("Most simple example\n");
562  $this->printer->feed();
563  $this->printer->cut();
564 
565  // If is DummyPrintConnector send to log to debugging
566  if ($this->printer->connector instanceof DummyPrintConnector) {
567  $data = $this->printer->connector->getData();
568  dol_syslog($data);
569  }
570  $this->printer->close();
571  } catch (Exception $e) {
572  $this->errors[] = $e->getMessage();
573  $error++;
574  }
575  }
576  return $error;
577  }
578 
587  public function sendToPrinter($object, $templateid, $printerid)
588  {
589  global $conf, $mysoc, $langs, $user;
590 
591  $langs->load('bills');
592 
593  $error = 0;
594  $ret = $this->loadTemplate($templateid);
595  $now = dol_now('tzuser');
596  // tags a remplacer par leur valeur avant de parser (dol_value_xxx)
597  $this->template = str_replace('{dol_value_object_id}', $object->id, $this->template);
598  $this->template = str_replace('{dol_value_object_ref}', $object->ref, $this->template);
599  //$this->template = str_replace('<dol_value_object_points>', $object->points, $this->template);
600  $this->template = str_replace('{dol_value_date}', dol_print_date($object->date, 'day'), $this->template);
601  $this->template = str_replace('{dol_value_date_time}', dol_print_date($object->date, 'dayhour'), $this->template);
602  $this->template = str_replace('{dol_value_year}', dol_print_date($object->date, '%Y'), $this->template);
603  $this->template = str_replace('{dol_value_month_letters}', $langs->trans("Month".dol_print_date($object->date, '%m')), $this->template);
604  $this->template = str_replace('{dol_value_month}', dol_print_date($object->date, '%m'), $this->template);
605  $this->template = str_replace('{dol_value_day}', dol_print_date($object->date, '%d'), $this->template);
606  $this->template = str_replace('{dol_value_day_letters}', $langs->trans("Day".dol_print_date($object->date, '%m')[1]), $this->template);
607 
608  $this->template = str_replace('{dol_value_currentdate}', dol_print_date($now, 'dayhour'), $this->template);
609  $this->template = str_replace('{dol_value_currentdate_notime}', dol_print_date($now, 'day'), $this->template);
610  $this->template = str_replace('{dol_value_currentdate_letters}', dol_print_date($now, 'dayhourtext'), $this->template);
611  $this->template = str_replace('{dol_value_currentyear}', dol_print_date($now, '%Y'), $this->template);
612  $this->template = str_replace('{dol_value_currentmonth_letters}', $langs->trans("Month".dol_print_date($now, '%m')), $this->template);
613  $this->template = str_replace('{dol_value_currentmonth}', dol_print_date($now, '%m'), $this->template);
614  $this->template = str_replace('{dol_value_currentday}', dol_print_date($now, '%d'), $this->template);
615  $this->template = str_replace('{dol_value_currentday_letters}', $langs->trans("Day".dol_print_date($now, '%m')[1]), $this->template);
616 
617  $this->template = str_replace('{dol_value_customer_firstname}', $object->thirdparty->firstname, $this->template);
618  $this->template = str_replace('{dol_value_customer_lastname}', $object->thirdparty->lastname, $this->template);
619  $this->template = str_replace('{dol_value_customer_mail}', $object->thirdparty->email, $this->template);
620  $this->template = str_replace('{dol_value_customer_phone}', $object->thirdparty->phone, $this->template);
621  //$this->template = str_replace('<dol_value_customer_mobile>', $object->thirdparty->mobile, $this->template);
622  $this->template = str_replace('{dol_value_customer_tax_number}', $object->thirdparty->tva_intra, $this->template);
623  //$this->template = str_replace('<dol_value_customer_account_balance>', $object->customer_account_balance, $this->template);
624  //$this->template = str_replace('<dol_value_customer_points>', $object->customer_points, $this->template);
625 
626  $this->template = str_replace('{dol_value_mysoc_name}', $mysoc->name, $this->template);
627  $this->template = str_replace('{dol_value_mysoc_address}', $mysoc->address, $this->template);
628  $this->template = str_replace('{dol_value_mysoc_zip}', $mysoc->zip, $this->template);
629  $this->template = str_replace('{dol_value_mysoc_town}', $mysoc->town, $this->template);
630  $this->template = str_replace('{dol_value_mysoc_country}', $mysoc->country, $this->template);
631  $this->template = str_replace('{dol_value_mysoc_idprof1}', $mysoc->idprof1, $this->template);
632  $this->template = str_replace('{dol_value_mysoc_idprof2}', $mysoc->idprof2, $this->template);
633  $this->template = str_replace('{dol_value_mysoc_idprof3}', $mysoc->idprof3, $this->template);
634  $this->template = str_replace('{dol_value_mysoc_idprof4}', $mysoc->idprof4, $this->template);
635  $this->template = str_replace('{dol_value_mysoc_idprof5}', $mysoc->idprof5, $this->template);
636  $this->template = str_replace('{dol_value_mysoc_idprof6}', $mysoc->idprof6, $this->template);
637  $this->template = str_replace('{dol_value_mysoc_tva_intra}', $mysoc->tva_intra, $this->template);
638  $this->template = str_replace('{dol_value_mysoc_capital}', $mysoc->capital, $this->template);
639  $this->template = str_replace('{dol_value_mysoc_url}', $mysoc->url, $this->template);
640 
641  $this->template = str_replace('{dol_value_vendor_firstname}', $user->firstname, $this->template);
642  $this->template = str_replace('{dol_value_vendor_lastname}', $user->lastname, $this->template);
643  $this->template = str_replace('{dol_value_vendor_mail}', $user->email, $this->template);
644 
645  // parse template
646  $this->template = str_replace("{", "<", $this->template);
647  $this->template = str_replace("}", ">", $this->template);
648  $p = xml_parser_create();
649  xml_parse_into_struct($p, $this->template, $vals, $index);
650  xml_parser_free($p);
651  //print '<pre>'.print_r($index, true).'</pre>';
652  //print '<pre>'.print_r($vals, true).'</pre>';
653  // print ticket
654  $level = 0;
655  $nbcharactbyline = (!empty($conf->global->RECEIPT_PRINTER_NB_CHARACT_BY_LINE) ? $conf->global->RECEIPT_PRINTER_NB_CHARACT_BY_LINE : 48);
656  $ret = $this->initPrinter($printerid);
657  if ($ret > 0) {
658  setEventMessages($this->error, $this->errors, 'errors');
659  } else {
660  $nboflines = count($vals);
661  for ($tplline = 0; $tplline < $nboflines; $tplline++) {
662  //var_dump($vals[$tplline]['value']);
663  switch ($vals[$tplline]['tag']) {
664  case 'DOL_PRINT_TEXT':
665  $this->printer->text($vals[$tplline]['value']);
666  break;
667  case 'DOL_PRINT_OBJECT_LINES':
668  foreach ($object->lines as $line) {
669  if ($line->fk_product) {
670  $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - strlen($line->subprice) - 10 - 1;
671  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
672  $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");
673  $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n \n");
674  } else {
675  $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - strlen($line->subprice) - 10 - 1;
676  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
677  $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");
678  }
679  }
680  break;
681  case 'DOL_PRINT_OBJECT_LINES_WITH_NOTES':
682  foreach ($object->lines as $line) {
683  if ($line->fk_product) {
684  $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - 10 - 1;
685  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
686  $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
687  $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n");
688  $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - 10 - 1;
689  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
690  $this->printer->text($line->description."\n");
691  } else {
692  $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - 10 - 1;
693  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
694  $this->printer->text($line->description.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
695  }
696  }
697  break;
698  case 'DOL_PRINT_OBJECT_TAX':
699  //var_dump($object);
700  $vatarray = array();
701  foreach ($object->lines as $line) {
702  $vatarray[$line->tva_tx] += $line->total_tva;
703  }
704  foreach ($vatarray as $vatkey => $vatvalue) {
705  $spacestoadd = $nbcharactbyline - strlen($vatkey) - 12;
706  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
707  $this->printer->text($spaces.$vatkey.'% '.str_pad(price($vatvalue), 10, ' ', STR_PAD_LEFT)."\n");
708  }
709  break;
710  case 'DOL_PRINT_OBJECT_TAX1':
711  //var_dump($object);
712  $total_localtax1 = 0;
713  foreach ($object->lines as $line) {
714  $total_localtax1 += $line->total_localtax1;
715  }
716  $this->printer->text(str_pad(price($total_localtax1), 10, ' ', STR_PAD_LEFT)."\n");
717  break;
718  case 'DOL_PRINT_OBJECT_TAX2':
719  //var_dump($object);
720  $total_localtax2 = 0;
721  foreach ($object->lines as $line) {
722  $total_localtax2 += $line->total_localtax2;
723  }
724  $this->printer->text(str_pad(price($total_localtax2), 10, ' ', STR_PAD_LEFT)."\n");
725  break;
726  case 'DOL_PRINT_OBJECT_TOTAL':
727  $title = $langs->trans('TotalHT');
728  $spacestoadd = $nbcharactbyline - strlen($title) - 10;
729  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
730  $this->printer->text($title.$spaces.str_pad(price($object->total_ht), 10, ' ', STR_PAD_LEFT)."\n");
731  $title = $langs->trans('TotalVAT');
732  $spacestoadd = $nbcharactbyline - strlen($title) - 10;
733  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
734  $this->printer->text($title.$spaces.str_pad(price($object->total_tva), 10, ' ', STR_PAD_LEFT)."\n");
735  $title = $langs->trans('TotalTTC');
736  $spacestoadd = $nbcharactbyline - strlen($title) - 10;
737  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
738  $this->printer->text($title.$spaces.str_pad(price($object->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
739  break;
740  case 'DOL_PRINT_CURR_DATE':
741  if (strlen($vals[$tplline]['value'])<2) $this->printer->text(date('d/m/Y H:i:s')."\n");
742  else $this->printer->text(date($vals[$tplline]['value'])."\n");
743  break;
744  case 'DOL_LINE_FEED':
745  $this->printer->feed();
746  break;
747  case 'DOL_LINE_FEED_REVERSE':
748  $this->printer->feedReverse();
749  break;
750  case 'DOL_ALIGN_CENTER':
751  $this->printer->setJustification(Printer::JUSTIFY_CENTER);
752  break;
753  case 'DOL_ALIGN_RIGHT':
754  $this->printer->setJustification(Printer::JUSTIFY_RIGHT);
755  break;
756  case 'DOL_ALIGN_LEFT':
757  $this->printer->setJustification(Printer::JUSTIFY_LEFT);
758  break;
759  case 'DOL_OPEN_DRAWER':
760  $this->printer->pulse();
761  break;
762  case 'DOL_ACTIVATE_BUZZER':
763  //$this->printer->buzzer();
764  break;
765  case 'DOL_PRINT_BARCODE':
766  // $vals[$tplline]['value'] -> barcode($content, $type)
767  // var_dump($vals[$tplline]['value']);
768  try {
769  $this->printer->barcode($vals[$tplline]['value']);
770  } catch (Exception $e) {
771  $this->errors[] = 'Invalid Barcode value: '.$vals[$tplline]['value'];
772  $error++;
773  }
774  break;
775  case 'DOL_PRINT_LOGO':
776  $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
777  $this->printer->graphics($img);
778  break;
779  case 'DOL_PRINT_LOGO_OLD':
780  $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
781  $this->printer->bitImage($img);
782  break;
783  case 'DOL_PRINT_LOGO_OLD_CF':
784  $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
785  $this->printer->bitImageColumnFormat($img);
786  break;
787  case 'DOL_PRINT_QRCODE':
788  // $vals[$tplline]['value'] -> qrCode($content, $ec, $size, $model)
789  $this->printer->qrcode($vals[$tplline]['value']);
790  break;
791  case 'DOL_CUT_PAPER_FULL':
792  $this->printer->cut(Printer::CUT_FULL);
793  break;
794  case 'DOL_CUT_PAPER_PARTIAL':
795  $this->printer->cut(Printer::CUT_PARTIAL);
796  break;
797  case 'DOL_USE_FONT_A':
798  $this->printer->setFont(Printer::FONT_A);
799  break;
800  case 'DOL_USE_FONT_B':
801  $this->printer->setFont(Printer::FONT_B);
802  break;
803  case 'DOL_USE_FONT_C':
804  $this->printer->setFont(Printer::FONT_C);
805  break;
806  case 'DOL_BOLD':
807  $this->printer->setEmphasis(true);
808  break;
809  case 'DOL_BOLD_DISABLED':
810  $this->printer->setEmphasis(false);
811  break;
812  case 'DOL_DOUBLE_HEIGHT':
813  $this->printer->setTextSize(1, 2);
814  break;
815  case 'DOL_DOUBLE_WIDTH':
816  $this->printer->setTextSize(2, 1);
817  break;
818  case 'DOL_DEFAULT_HEIGHT_WIDTH':
819  $this->printer->setTextSize(1, 1);
820  break;
821  case 'DOL_UNDERLINE':
822  $this->printer->setUnderline(true);
823  break;
824  case 'DOL_UNDERLINE_DISABLED':
825  $this->printer->setUnderline(false);
826  break;
827  case 'DOL_BEEP':
828  $this->printer->getPrintConnector() -> write("\x1e");
829  break;
830  case 'DOL_BEEP_ALTERNATIVE': //if DOL_BEEP not works
831  $this->printer->getPrintConnector() -> write(Printer::ESC . "B" . chr(4) . chr(1));
832  break;
833  case 'DOL_PRINT_ORDER_LINES':
834  foreach ($object->lines as $line) {
835  if ($line->special_code == $this->orderprinter) {
836  $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - 10 - 1;
837  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
838  $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
839  $this->printer->text(strip_tags(htmlspecialchars_decode($line->desc))."\n");
840  }
841  }
842  break;
843  case 'DOL_PRINT_PAYMENT':
844  $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,";
845  $sql .= " cp.code";
846  $sql .= " FROM ".$this->db->prefix()."paiement_facture as pf, ".$this->db->prefix()."paiement as p";
847  $sql .= " LEFT JOIN ".$this->db->prefix()."c_paiement as cp ON p.fk_paiement = cp.id";
848  $sql .= " WHERE pf.fk_paiement = p.rowid AND pf.fk_facture = ".((int) $object->id);
849  $sql .= " ORDER BY p.datep";
850  $resql = $this->db->query($sql);
851  if ($resql) {
852  $num = $this->db->num_rows($resql);
853  $i = 0;
854  while ($i < $num) {
855  $row = $this->db->fetch_object($resql);
856  $spacestoadd = $nbcharactbyline - strlen($langs->transnoentitiesnoconv("PaymentTypeShort".$row->code)) - 12;
857  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
858  $amount_payment = (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? $row->multicurrency_amount : $row->amount;
859  if ($row->code == "LIQ") {
860  $amount_payment = $amount_payment + $row->pos_change; // Show amount with excess received if is cash payment
861  }
862  $this->printer->text($spaces.$langs->transnoentitiesnoconv("PaymentTypeShort".$row->code).' '.str_pad(price($amount_payment), 10, ' ', STR_PAD_LEFT)."\n");
863  if ($row->code == "LIQ" && $row->pos_change > 0) { // Print change only in cash payments
864  $spacestoadd = $nbcharactbyline - strlen($langs->trans("Change")) - 12;
865  $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
866  $this->printer->text($spaces.$langs->trans("Change").' '.str_pad(price($row->pos_change), 10, ' ', STR_PAD_LEFT)."\n");
867  }
868  $i++;
869  }
870  }
871  break;
872  case 'DOL_VALUE_PLACE':
873  $sql = "SELECT floor, label FROM ".$this->db->prefix()."takepos_floor_tables where rowid=".((int) str_replace(")", "", str_replace("(PROV-POS".$_SESSION["takeposterminal"]."-", "", $object->ref)));
874  $resql = $this->db->query($sql);
875  $obj = $this->db->fetch_object($resql);
876  if ($obj) {
877  $this->printer->text($obj->label);
878  }
879  break;
880  default:
881  $this->printer->text($vals[$tplline]['tag']);
882  $this->printer->text($vals[$tplline]['value']);
883  $this->errors[] = 'UnknowTag: &lt;'.strtolower($vals[$tplline]['tag']).'&gt;';
884  $error++;
885  break;
886  }
887  }
888  // If is DummyPrintConnector send to log to debugging
889  if ($this->printer->connector instanceof DummyPrintConnector || $conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") {
890  $data = $this->printer->connector->getData();
891  if ($conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") {
892  echo rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
893  }
894  dol_syslog($data);
895  }
896  // Close and print
897  $this->printer->close();
898  }
899  return $error;
900  }
901 
908  public function loadTemplate($templateid)
909  {
910  global $conf;
911  $error = 0;
912  $sql = "SELECT template";
913  $sql .= " FROM ".$this->db->prefix()."printer_receipt_template";
914  $sql .= " WHERE rowid = ".((int) $templateid);
915  $sql .= " AND entity = ".$conf->entity;
916  $resql = $this->db->query($sql);
917  if ($resql) {
918  $obj = $this->db->fetch_array($resql);
919  } else {
920  $error++;
921  $this->errors[] = $this->db->lasterror;
922  }
923  if (empty($obj)) {
924  $error++;
925  $this->errors[] = 'TemplateDontExist';
926  } else {
927  $this->template = $obj['0'];
928  }
929 
930  return $error;
931  }
932 
933 
940  public function initPrinter($printerid)
941  {
942  global $conf;
943  if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
944  $this->connector = new DummyPrintConnector();
945  $this->printer = new Printer($this->connector, $this->profile);
946  return;
947  }
948  $error = 0;
949  $sql = "SELECT rowid, name, fk_type, fk_profile, parameter";
950  $sql .= " FROM ".$this->db->prefix()."printer_receipt";
951  $sql .= " WHERE rowid = ".((int) $printerid);
952  $sql .= " AND entity = ".((int) $conf->entity);
953  $resql = $this->db->query($sql);
954  if ($resql) {
955  $obj = $this->db->fetch_array($resql);
956  if (empty($obj)) {
957  $error++;
958  $this->errors[] = 'PrinterDontExist';
959  }
960  if (!$error) {
961  $parameter = (isset($obj['parameter']) ? $obj['parameter'] : '');
962  try {
963  $type = $obj['fk_type'];
964  switch ($type) {
965  case 1:
966  $this->connector = new DummyPrintConnector();
967  break;
968  case 2:
969  $this->connector = new FilePrintConnector($parameter);
970  break;
971  case 3:
972  $parameters = explode(':', $parameter);
973  $this->connector = new NetworkPrintConnector($parameters[0], $parameters[1]);
974  break;
975  case 4: // LPT1, smb://...
976  $this->connector = new WindowsPrintConnector(dol_sanitizePathName($parameter));
977  break;
978  case 5:
979  $this->connector = new CupsPrintConnector($parameter);
980  break;
981  default:
982  $this->connector = 'CONNECTOR_UNKNOWN';
983  break;
984  }
985  $this->printer = new Printer($this->connector, $this->profile);
986  } catch (Exception $e) {
987  $this->errors[] = $e->getMessage();
988  $error++;
989  }
990  }
991  } else {
992  $error++;
993  $this->errors[] = $this->db->lasterror;
994  }
995 
996  return $error;
997  }
998 }
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.
__construct($db)
Constructor.
loadTemplate($templateid)
Function to load Template.
updateTemplate($name, $template, $templateid)
Function to Update a printer template in db.
sendTestToPrinter($printerid)
Function to Send Test page to Printer.
initPrinter($printerid)
Function Init 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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_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.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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.
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
$conf db
API class for accounts.
Definition: inc.php:41