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