dolibarr  17.0.3
receiptprinter.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
4  * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
5  * Copyright (C) 2020 Andreu Bisquerra Gaya <jove@bisquerra.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 // Load Dolibarr environment
28 require '../main.inc.php';
29 
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/receiptprinter.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array("admin", "receiptprinter"));
37 
38 if (!$user->admin) {
40 }
41 
42 $action = GETPOST('action', 'aZ09');
43 $mode = GETPOST('mode', 'alpha');
44 
45 $printername = GETPOST('printername', 'alpha');
46 $printerid = GETPOST('printerid', 'int');
47 $parameter = GETPOST('parameter', 'alpha');
48 
49 $template = GETPOST('template', 'alphanohtml');
50 $templatename = GETPOST('templatename', 'alpha');
51 $templateid = GETPOST('templateid', 'int');
52 
53 $printer = new dolReceiptPrinter($db);
54 
55 if (!$mode) {
56  $mode = 'config';
57 }
58 
59 // used in library escpos maybe useful if php doesn't support gzdecode
60 if (!function_exists('gzdecode')) {
67  function gzdecode($data)
68  {
69  return gzinflate(substr($data, 10, -8));
70  }
71 }
72 
73 
74 /*
75  * Action
76  */
77 
78 if ($action == 'addprinter' && $user->admin) {
79  $error = 0;
80  if (empty($printername)) {
81  $error++;
82  setEventMessages($langs->trans("PrinterNameEmpty"), null, 'errors');
83  }
84 
85  if (empty($parameter)) {
86  setEventMessages($langs->trans("PrinterParameterEmpty"), null, 'warnings');
87  }
88 
89  if (!$error) {
90  $db->begin();
91  $result = $printer->addPrinter($printername, GETPOST('printertypeid', 'int'), GETPOST('printerprofileid', 'int'), $parameter);
92  if ($result > 0) {
93  $error++;
94  }
95 
96  if (!$error) {
97  $db->commit();
98  setEventMessages($langs->trans("PrinterAdded", $printername), null);
99  } else {
100  $db->rollback();
101  dol_print_error($db);
102  }
103  }
104  $action = '';
105 }
106 
107 if ($action == 'deleteprinter' && $user->admin) {
108  $error = 0;
109  if (empty($printerid)) {
110  $error++;
111  setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
112  }
113 
114  if (!$error) {
115  $db->begin();
116  $result = $printer->deletePrinter($printerid);
117  if ($result > 0) {
118  $error++;
119  }
120 
121  if (!$error) {
122  $db->commit();
123  setEventMessages($langs->trans("PrinterDeleted", $printername), null);
124  } else {
125  $db->rollback();
126  dol_print_error($db);
127  }
128  }
129  $action = '';
130 }
131 
132 if ($action == 'updateprinter' && $user->admin) {
133  $error = 0;
134  if (empty($printerid)) {
135  $error++;
136  setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
137  }
138 
139  if (!$error) {
140  $db->begin();
141  $result = $printer->updatePrinter($printername, GETPOST('printertypeid', 'int'), GETPOST('printerprofileid', 'int'), $parameter, $printerid);
142  if ($result > 0) {
143  $error++;
144  }
145 
146  if (!$error) {
147  $db->commit();
148  setEventMessages($langs->trans("PrinterUpdated", $printername), null);
149  } else {
150  $db->rollback();
151  dol_print_error($db);
152  }
153  }
154  $action = '';
155 }
156 
157 if ($action == 'testprinter' && $user->admin) {
158  $error = 0;
159  if (empty($printerid)) {
160  $error++;
161  setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
162  }
163 
164  if (!$error) {
165  // test
166  $ret = $printer->sendTestToPrinter($printerid);
167  if ($ret == 0) {
168  setEventMessages($langs->trans("TestSentToPrinter", $printername), null);
169  } else {
170  setEventMessages($printer->error, $printer->errors, 'errors');
171  }
172  }
173  $action = '';
174 }
175 
176 if ($action == 'testtemplate' && $user->admin) {
177  $error = 0;
178  // if (empty($printerid)) {
179  // $error++;
180  // setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
181  // }
182 
183  // if (! $error) {
184  // test
185  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
186  $object = new Facture($db);
187  $object->initAsSpecimen();
188  //$object->fetch(18);
189  //var_dump($object->lines);
190  $ret = $printer->sendToPrinter($object, $templateid, 1);
191  if ($ret == 0) {
192  setEventMessages($langs->trans("TestTemplateToPrinter", $printername), null);
193  } else {
194  setEventMessages($printer->error, $printer->errors, 'errors');
195  }
196  //}
197  $action = '';
198 }
199 
200 if ($action == 'updatetemplate' && $user->admin) {
201  $error = 0;
202  if (empty($templateid)) {
203  $error++;
204  setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
205  }
206 
207  if (!$error) {
208  $db->begin();
209  $result = $printer->updateTemplate($templatename, $template, $templateid);
210  if ($result > 0) {
211  $error++;
212  }
213 
214  if (!$error) {
215  $db->commit();
216  setEventMessages($langs->trans("TemplateUpdated", $templatename), null);
217  } else {
218  $db->rollback();
219  dol_print_error($db);
220  }
221  }
222  $action = '';
223 }
224 
225 if ($action == 'addtemplate' && $user->admin) {
226  $error = 0;
227  if (empty($templatename)) {
228  $error++;
229  setEventMessages($langs->trans("TemplateNameEmpty"), null, 'errors');
230  }
231 
232  if (!$error) {
233  $db->begin();
234  $result = $printer->addTemplate($templatename, $template);
235  if ($result > 0) {
236  $error++;
237  }
238 
239  if (!$error) {
240  $db->commit();
241  setEventMessages($langs->trans("TemplateAdded", $templatename), null);
242  } else {
243  $db->rollback();
244  dol_print_error($db);
245  }
246  }
247  $action = '';
248 }
249 
250 if ($action == 'deletetemplate' && $user->admin) {
251  $error = 0;
252  if (empty($templateid)) {
253  $error++;
254  setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
255  }
256 
257  if (!$error) {
258  $db->begin();
259  $result = $printer->deleteTemplate($templateid);
260  if ($result > 0) {
261  $error++;
262  }
263 
264  if (!$error) {
265  $db->commit();
266  setEventMessages($langs->trans("TemplateDeleted", $templatename), null);
267  } else {
268  $db->rollback();
269  dol_print_error($db);
270  }
271  }
272  $action = '';
273 }
274 
275 
276 /*
277  * View
278  */
279 
280 $form = new Form($db);
281 
282 llxHeader('', $langs->trans("ReceiptPrinterSetup"));
283 
284 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
285 print load_fiche_titre($langs->trans("ReceiptPrinterSetup"), $linkback, 'title_setup');
286 
287 $head = receiptprinteradmin_prepare_head($mode);
288 
289 // mode = config
290 if ($mode == 'config' && $user->admin) {
291  print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=config" autocomplete="off">';
292  print '<input type="hidden" name="token" value="'.newToken().'">';
293  if ($action != 'editprinter') {
294  print '<input type="hidden" name="action" value="addprinter">';
295  } else {
296  print '<input type="hidden" name="action" value="updateprinter">';
297  }
298 
299 
300  print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
301 
302  print '<span class="opacitymedium">'.$langs->trans("ReceiptPrinterDesc")."</span><br><br>\n";
303 
304  print '<table class="noborder centpercent">'."\n";
305  print '<tr class="liste_titre">';
306  print '<th>'.$langs->trans("Name").'</th>';
307  print '<th>'.$langs->trans("Type").'</th>';
308  print '<th>';
309  $htmltext = $langs->trans("PROFILE_DEFAULT").' = '.$langs->trans("PROFILE_DEFAULT_HELP").'<br>';
310  $htmltext .= $langs->trans("PROFILE_SIMPLE").' = '.$langs->trans("PROFILE_SIMPLE_HELP").'<br>';
311  $htmltext .= $langs->trans("PROFILE_EPOSTEP").' = '.$langs->trans("PROFILE_EPOSTEP_HELP").'<br>';
312  $htmltext .= $langs->trans("PROFILE_P822D").' = '.$langs->trans("PROFILE_P822D_HELP").'<br>';
313  $htmltext .= $langs->trans("PROFILE_STAR").' = '.$langs->trans("PROFILE_STAR_HELP").'<br>';
314 
315  print $form->textwithpicto($langs->trans("Profile"), $htmltext);
316  print '</th>';
317  print '<th>'.$langs->trans("Parameters").'</th>';
318  print '<th></th>';
319  print "</tr>\n";
320  $ret = $printer->listprinters();
321  $nbofprinters = count($printer->listprinters);
322 
323  if ($action != 'editprinter') {
324  print '<tr>';
325  print '<td><input class="minwidth200" type="text" name="printername"></td>';
326  $ret = $printer->selectTypePrinter();
327  print '<td>'.$printer->resprint.'</td>';
328  $ret = $printer->selectProfilePrinter();
329  print '<td>'.$printer->profileresprint.'</td>';
330  print '<td><input size="60" type="text" name="parameter"></td>';
331  print '<td class="right">';
332  if ($action != 'editprinter') {
333  print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
334  }
335  print '</td>';
336  print '</tr>';
337  }
338 
339  if ($ret > 0) {
340  setEventMessages($printer->error, $printer->errors, 'errors');
341  } else {
342  for ($line = 0; $line < $nbofprinters; $line++) {
343  print '<tr class="oddeven">';
344  if ($action == 'editprinter' && $printer->listprinters[$line]['rowid'] == $printerid) {
345  print '<input type="hidden" name="printerid" value="'.$printer->listprinters[$line]['rowid'].'">';
346  print '<td><input type="text" class="minwidth200" name="printername" value="'.$printer->listprinters[$line]['name'].'"></td>';
347  $ret = $printer->selectTypePrinter($printer->listprinters[$line]['fk_type']);
348  print '<td>'.$printer->resprint.'</td>';
349  $ret = $printer->selectProfilePrinter($printer->listprinters[$line]['fk_profile']);
350  print '<td>'.$printer->profileresprint.'</td>';
351  print '<td><input size="60" type="text" name="parameter" value="'.$printer->listprinters[$line]['parameter'].'"></td>';
352  print '<td>';
353  print $form->buttonsSaveCancel("Save", '');
354  print '</td>';
355  print '</tr>';
356  } else {
357  print '<td>'.$printer->listprinters[$line]['name'].'</td>';
358  print '<td>'.$langs->trans($printer->listprinters[$line]['fk_type_name']).'</td>';
359  print '<td>'.$langs->trans($printer->listprinters[$line]['fk_profile_name']).'</td>';
360  print '<td>'.$printer->listprinters[$line]['parameter'].'</td>';
361  // edit icon
362  print '<td class="right"><a class="editfielda marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=config&action=editprinter&token='.newToken().'&printerid='.$printer->listprinters[$line]['rowid'].'">';
363  print img_picto($langs->trans("Edit"), 'edit');
364  print '</a>';
365  // delete icon
366  print '<a class="marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=config&action=deleteprinter&token='.newToken().'&printerid='.$printer->listprinters[$line]['rowid'].'&printername='.urlencode($printer->listprinters[$line]['name']).'">';
367  print img_picto($langs->trans("Delete"), 'delete');
368  print '</a>';
369  // test icon
370  print '<a class="marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=config&action=testprinter&token='.newToken().'&printerid='.$printer->listprinters[$line]['rowid'].'&printername='.urlencode($printer->listprinters[$line]['name']).'">';
371  print img_picto($langs->trans("TestPrinter"), 'printer');
372  print '</a></td>';
373  print '</tr>';
374  }
375  }
376  }
377 
378  print '</table>';
379 
380  print dol_get_fiche_end();
381 
382  print '</form>';
383 
384  print '<br>';
385 
386 
387  print load_fiche_titre($langs->trans("ReceiptPrinterTypeDesc"), '', '')."\n";
388 
389  print '<table class="noborder centpercent">'."\n";
390  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_DUMMY").':</td><td>'.$langs->trans("CONNECTOR_DUMMY_HELP").'</td></tr>';
391  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT").':</td><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT_HELP").'</td></tr>';
392  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_FILE_PRINT").':</td><td>'.$langs->trans("CONNECTOR_FILE_PRINT_HELP").'</td></tr>';
393  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT").':</td><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT_HELP").'</td></tr>';
394  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_CUPS_PRINT").':</td><td>'.$langs->trans("CONNECTOR_CUPS_PRINT_HELP").'</td></tr>';
395  print '</table>';
396 
397  print '<br>';
398 }
399 
400 // mode = template
401 if ($mode == 'template' && $user->admin) {
402  print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
403 
404  print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=template" autocomplete="off">';
405  print '<input type="hidden" name="token" value="'.newToken().'">';
406  if ($action != 'edittemplate') {
407  print '<input type="hidden" name="action" value="addtemplate">';
408  } else {
409  print '<input type="hidden" name="action" value="updatetemplate">';
410  }
411 
412  print '<table class="noborder centpercent">'."\n";
413  print '<tr class="liste_titre">';
414  print '<th>'.$langs->trans("Name").'</th>';
415  print '<th>'.$langs->trans("Template").'</th>';
416  print '<th></th>';
417  print "</tr>\n";
418  $ret = $printer->listPrintersTemplates();
419  //print '<pre>'.print_r($printer->listprinterstemplates, true).'</pre>';
420  if ($ret > 0) {
421  setEventMessages($printer->error, $printer->errors, 'errors');
422  } else {
423  $max = count($printer->listprinterstemplates);
424  for ($line = 0; $line < $max; $line++) {
425  print '<tr class="oddeven">';
426  if ($action == 'edittemplate' && $printer->listprinterstemplates[$line]['rowid'] == $templateid) {
427  print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
428  print '<td><input type="text" class="minwidth200" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
429  print '<td>';
430  print '<textarea name="template" wrap="soft" cols="120" rows="12">'.$printer->listprinterstemplates[$line]['template'].'</textarea>';
431  print '</td>';
432  print '<td>';
433  print $form->buttonsSaveCancel("Save", '');
434  print '</td>';
435  } else {
436  print '<td>'.$printer->listprinterstemplates[$line]['name'].'</td>';
437  print '<td>'.dol_htmlentitiesbr($printer->listprinterstemplates[$line]['template']).'</td>';
438  // edit icon
439  print '<td><a class="editfielda paddingleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=template&action=edittemplate&token='.newToken().'&templateid='.$printer->listprinterstemplates[$line]['rowid'].'">';
440  print img_picto($langs->trans("Edit"), 'edit');
441  print '</a>';
442  // delete icon
443  print '<a class="paddingleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=template&action=deletetemplate&token='.newToken().'&templateid='.$printer->listprinterstemplates[$line]['rowid'].'&templatename='.urlencode($printer->listprinterstemplates[$line]['name']).'">';
444  print img_picto($langs->trans("Delete"), 'delete');
445  print '</a>';
446  // test icon
447  print '<a class="paddingleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=template&action=testtemplate&token='.newToken().'&templateid='.$printer->listprinterstemplates[$line]['rowid'].'&templatename='.urlencode($printer->listprinterstemplates[$line]['name']).'">';
448  print img_picto($langs->trans("TestPrinterTemplate"), 'printer');
449  print '</a></td>';
450  }
451  print '</tr>';
452  }
453  }
454 
455  if ($action != 'edittemplate') {
456  print '<tr>';
457  print '<td><input type="text" class="minwidth200" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
458  print '<td>';
459  print '<textarea name="template" wrap="soft" cols="120" rows="12">';
460  print '</textarea>';
461  print '</td>';
462  print '<td>';
463  print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
464  print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'">';
465  print '</td>';
466  print '</tr>';
467  }
468 
469  print '</table>';
470 
471  print '</form>';
472 
473  print dol_get_fiche_end();
474 
475  print '<br>';
476 
477  print '<table class="noborder centpercent">'."\n";
478  print '<tr class="liste_titre">';
479  print '<th>'.$langs->trans("Tag").'</th>';
480  print '<th>'.$langs->trans("Description").'</th>';
481  print "</tr>\n";
482 
483  $langs->loadLangs(array("bills", "companies"));
484  foreach ($printer->tags as $key => $val) {
485  print '<tr class="oddeven">';
486  print '<td>{'.$key.'}</td><td>'.$langs->trans($val).'</td>';
487  print '</tr>';
488  }
489  print '</table>';
490 }
491 
492 // End of page
493 llxFooter();
494 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:70
dolReceiptPrinter
Class to manage Receipt Printers.
Definition: dolreceiptprinter.class.php:121
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5360
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:520
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4993
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
Facture
Class to manage invoices.
Definition: facture.class.php:60
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4024
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
receiptprinteradmin_prepare_head
receiptprinteradmin_prepare_head($mode)
Define head array for tabs of receipt printer setup pages.
Definition: receiptprinter.lib.php:33
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1858
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:11290
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2054
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:53
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8465
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
Definition: security.lib.php:1120