dolibarr  9.0.0
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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 require '../main.inc.php';
27 
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/receiptprinter.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
32 
33 // Load translation files required by the page
34 $langs->loadLangs(array("admin","receiptprinter"));
35 
36 if (! $user->admin) accessforbidden();
37 
38 $action = GETPOST('action','alpha');
39 $mode = GETPOST('mode','alpha');
40 
41 $printername = GETPOST('printername', 'alpha');
42 $printerid = GETPOST('printerid', 'int');
43 $parameter = GETPOST('parameter', 'alpha');
44 
45 $template = GETPOST('template', 'alpha');
46 $templatename = GETPOST('templatename', 'alpha');
47 $templateid = GETPOST('templateid', 'int');
48 
49 $printer = new dolReceiptPrinter($db);
50 
51 if (!$mode) $mode='config';
52 
53 // used in library escpos maybe useful if php doesn't support gzdecode
54 if (!function_exists('gzdecode')) {
61  function gzdecode($data)
62  {
63  return gzinflate(substr($data,10,-8));
64  }
65 }
66 
67 /*
68  * Action
69  */
70 
71 if ($action == 'addprinter' && $user->admin)
72 {
73  $error=0;
74  $db->begin();
75  if (empty($printername)) {
76  $error++;
77  setEventMessages($langs->trans("PrinterNameEmpty"), null, 'errors');
78  }
79 
80  if (empty($parameter)) {
81  setEventMessages($langs->trans("PrinterParameterEmpty"), null, 'warnings');
82  }
83 
84  if (! $error)
85  {
86  $result= $printer->AddPrinter($printername, GETPOST('printertypeid', 'int'), GETPOST('printerprofileid', 'int'), $parameter);
87  if ($result > 0) $error++;
88 
89  if (! $error)
90  {
91  $db->commit();
92  setEventMessages($langs->trans("PrinterAdded",$printername), null);
93  }
94  else
95  {
96  $db->rollback();
97  dol_print_error($db);
98  }
99  }
100  $action = '';
101 }
102 
103 if ($action == 'deleteprinter' && $user->admin)
104 {
105  $error=0;
106  $db->begin();
107  if (empty($printerid)) {
108  $error++;
109  setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
110  }
111 
112  if (! $error)
113  {
114  $result= $printer->DeletePrinter($printerid);
115  if ($result > 0) $error++;
116 
117  if (! $error)
118  {
119  $db->commit();
120  setEventMessages($langs->trans("PrinterDeleted",$printername), null);
121  }
122  else
123  {
124  $db->rollback();
125  dol_print_error($db);
126  }
127  }
128  $action = '';
129 }
130 
131 if ($action == 'updateprinter' && $user->admin)
132 {
133  $error=0;
134  $db->begin();
135  if (empty($printerid)) {
136  $error++;
137  setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
138  }
139 
140  if (! $error)
141  {
142  $result= $printer->UpdatePrinter($printername, GETPOST('printertypeid', 'int'), GETPOST('printerprofileid', 'int'), $parameter, $printerid);
143  if ($result > 0) $error++;
144 
145  if (! $error)
146  {
147  $db->commit();
148  setEventMessages($langs->trans("PrinterUpdated",$printername), null);
149  }
150  else
151  {
152  $db->rollback();
153  dol_print_error($db);
154  }
155  }
156  $action = '';
157 }
158 
159 if ($action == 'testprinter' && $user->admin)
160 {
161  $error=0;
162  if (empty($printerid)) {
163  $error++;
164  setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
165  }
166 
167  if (! $error)
168  {
169  // test
170  $ret = $printer->SendTestToPrinter($printerid);
171  if ($ret == 0)
172  {
173  setEventMessages($langs->trans("TestSentToPrinter", $printername), null);
174  }
175  else
176  {
177  setEventMessages($printer->error, $printer->errors, 'errors');
178  }
179  }
180  $action = '';
181 }
182 
183 
184 if ($action == 'updatetemplate' && $user->admin)
185 {
186  $error=0;
187  $db->begin();
188  if (empty($templateid)) {
189  $error++;
190  setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
191  }
192 
193  if (! $error)
194  {
195  $result= $printer->UpdateTemplate($templatename, $template, $templateid);
196  if ($result > 0) $error++;
197 
198  if (! $error)
199  {
200  $db->commit();
201  setEventMessages($langs->trans("TemplateUpdated",$templatename), null);
202  }
203  else
204  {
205  $db->rollback();
206  dol_print_error($db);
207  }
208  }
209  $action = '';
210 }
211 
212 
213 /*
214  * View
215  */
216 
217 $form = new Form($db);
218 
219 llxHeader('',$langs->trans("ReceiptPrinterSetup"));
220 
221 $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
222 print load_fiche_titre($langs->trans("ReceiptPrinterSetup"),$linkback,'title_setup');
223 
224 $head = receiptprinteradmin_prepare_head($mode);
225 
226 if ($mode == 'config' && $user->admin)
227 {
228  print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=config" autocomplete="off">';
229  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
230  if ($action!='editprinter') {
231  print '<input type="hidden" name="action" value="addprinter">';
232  } else {
233  print '<input type="hidden" name="action" value="updateprinter">';
234  }
235 
236  dol_fiche_head($head, $mode, $langs->trans("ModuleSetup"), 0, 'technic');
237 
238  print $langs->trans("ReceiptPrinterDesc")."<br><br>\n";
239 
240  print '<table class="noborder" width="100%">'."\n";
241  print '<tr class="liste_titre">';
242  print '<th>'.$langs->trans("Name").'</th>';
243  print '<th>'.$langs->trans("Type").'</th>';
244  print '<th>'.$langs->trans("Profile").'</th>';
245  print '<th>'.$langs->trans("Parameters").'</th>';
246  print '<th></th>';
247  print '<th></th>';
248  print '<th></th>';
249  print "</tr>\n";
250  $ret = $printer->listprinters();
251  $nbofprinters = count($printer->listprinters);
252 
253  if ($ret > 0) {
254  setEventMessages($printer->error, $printer->errors, 'errors');
255  } else {
256  for ($line=0; $line < $nbofprinters; $line++)
257  {
258  print '<tr class="oddeven">';
259  if ($action=='editprinter' && $printer->listprinters[$line]['rowid']==$printerid)
260  {
261  print '<input type="hidden" name="printerid" value="'.$printer->listprinters[$line]['rowid'].'">';
262  print '<td><input size="50" type="text" name="printername" value="'.$printer->listprinters[$line]['name'].'"></td>';
263  $ret = $printer->selectTypePrinter($printer->listprinters[$line]['fk_type']);
264  print '<td>'.$printer->resprint.'</td>';
265  $ret = $printer->selectProfilePrinter($printer->listprinters[$line]['fk_profile']);
266  print '<td>'.$printer->profileresprint.'</td>';
267  print '<td><input size="60" type="text" name="parameter" value="'.$printer->listprinters[$line]['parameter'].'"></td>';
268  print '<td></td>';
269  print '<td></td>';
270  print '<td></td>';
271  print '</tr>';
272  } else {
273  print '<td>'.$printer->listprinters[$line]['name'].'</td>';
274  print '<td>'.$langs->trans($printer->listprinters[$line]['fk_type_name']).'</td>';
275  print '<td>'.$langs->trans($printer->listprinters[$line]['fk_profile_name']).'</td>';
276  print '<td>'.$printer->listprinters[$line]['parameter'].'</td>';
277  // edit icon
278  print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=editprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'">';
279  print img_picto($langs->trans("Edit"),'edit');
280  print '</a></td>';
281  // delete icon
282  print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=deleteprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'&amp;printername='.$printer->listprinters[$line]['name'].'">';
283  print img_picto($langs->trans("Delete"),'delete');
284  print '</a></td>';
285  // test icon
286  print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=testprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'&amp;printername='.$printer->listprinters[$line]['name'].'">';
287  print img_picto($langs->trans("TestPrinter"),'printer');
288  print '</a></td>';
289  print '</tr>';
290  }
291  }
292  }
293 
294  if ($action!='editprinter')
295  {
296  if ($nbofprinters > 0)
297  {
298  print '<tr class="liste_titre">';
299  print '<th>'.$langs->trans("Name").'</th>';
300  print '<th>'.$langs->trans("Type").'</th>';
301  print '<th>'.$langs->trans("Profile").'</th>';
302  print '<th>'.$langs->trans("Parameters").'</th>';
303  print '<th></th>';
304  print '<th></th>';
305  print '<th></th>';
306  print "</tr>\n";
307  }
308 
309  print '<tr>';
310  print '<td><input size="50" type="text" name="printername"></td>';
311  $ret = $printer->selectTypePrinter();
312  print '<td>'.$printer->resprint.'</td>';
313  $ret = $printer->selectProfilePrinter();
314  print '<td>'.$printer->profileresprint.'</td>';
315  print '<td><input size="60" type="text" name="parameter"></td>';
316  print '<td></td>';
317  print '<td></td>';
318  print '<td></td>';
319  print '</tr>';
320  }
321  print '</table>';
322 
323  dol_fiche_end();
324 
325  if ($action!='editprinter') {
326  print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
327  } else {
328  print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
329  }
330  print '</form>';
331 
332  print '<div><p></div>';
333 
334  dol_fiche_head();
335 
336  print $langs->trans("ReceiptPrinterTypeDesc")."<br><br>\n";
337  print '<table class="noborder" width="100%">'."\n";
338  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_DUMMY").':</td><td>'.$langs->trans("CONNECTOR_DUMMY_HELP").'</td></tr>';
339  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT").':</td><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT_HELP").'</td></tr>';
340  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_FILE_PRINT").':</td><td>'.$langs->trans("CONNECTOR_FILE_PRINT_HELP").'</td></tr>';
341  print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT").':</td><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT_HELP").'</td></tr>';
342  //print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_JAVA").':</td><td>'.$langs->trans("CONNECTOR_JAVA_HELP").'</td></tr>';
343  print '</table>';
344  dol_fiche_end();
345 
346  print '<div><p></div>';
347 
348  dol_fiche_head();
349  print $langs->trans("ReceiptPrinterProfileDesc")."<br><br>\n";
350  print '<table class="noborder" width="100%">'."\n";
351  print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_DEFAULT").':</td><td>'.$langs->trans("PROFILE_DEFAULT_HELP").'</td></tr>';
352  print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_SIMPLE").':</td><td>'.$langs->trans("PROFILE_SIMPLE_HELP").'</td></tr>';
353  print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_EPOSTEP").':</td><td>'.$langs->trans("PROFILE_EPOSTEP_HELP").'</td></tr>';
354  print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_P822D").':</td><td>'.$langs->trans("PROFILE_P822D_HELP").'</td></tr>';
355  print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_STAR").':</td><td>'.$langs->trans("PROFILE_STAR_HELP").'</td></tr>';
356  print '</table>';
357  dol_fiche_end();
358 }
359 
360 if ($mode == 'template' && $user->admin)
361 {
362  print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=template" autocomplete="off">';
363  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
364  if ($action!='edittemplate') {
365  print '<input type="hidden" name="action" value="addtemplate">';
366  } else {
367  print '<input type="hidden" name="action" value="updatetemplate">';
368  }
369 
370  dol_fiche_head($head, $mode, $langs->trans("ModuleSetup"), 0, 'technic');
371 
372  print $langs->trans("ReceiptPrinterTemplateDesc")."<br><br>\n";
373  print '<table class="noborder" width="100%">'."\n";
374  print '<tr class="liste_titre">';
375  print '<th>'.$langs->trans("Name").'</th>';
376  print '<th>'.$langs->trans("Template").'</th>';
377  print '<th></th>';
378  print '<th></th>';
379  print '<th></th>';
380  print "</tr>\n";
381  $ret = $printer->listPrintersTemplates();
382  //print '<pre>'.print_r($printer->listprinterstemplates, true).'</pre>';
383  if ($ret > 0) {
384  setEventMessages($printer->error, $printer->errors, 'errors');
385  } else {
386  $max = count($printer->listprinterstemplates);
387  for ($line=0; $line < $max; $line++)
388  {
389  print '<tr class="oddeven">';
390  if ($action=='edittemplate' && $printer->listprinterstemplates[$line]['rowid']==$templateid) {
391  print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
392  print '<td><input size="50" type="text" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
393  print '<td><textarea name="template" wrap="soft" cols="120" rows="12">'.$printer->listprinterstemplates[$line]['template'].'</textarea>';
394  print '</td>';
395  print '<td></td>';
396  print '<td></td>';
397  print '<td></td>';
398  } else {
399  print '<td>'.$printer->listprinterstemplates[$line]['name'].'</td>';
400  print '<td>'.nl2br(htmlentities($printer->listprinterstemplates[$line]['template'])).'</td>';
401  // edit icon
402  print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=edittemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'">';
403  print img_picto($langs->trans("Edit"),'edit');
404  print '</a></td>';
405  // delete icon
406  print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=deletetemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'&amp;templatename='.$printer->listprinterstemplates[$line]['name'].'">';
407  print img_picto($langs->trans("Delete"),'delete');
408  print '</a></td>';
409  // test icon
410  print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=testtemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'&amp;templatename='.$printer->listprinterstemplates[$line]['name'].'">';
411  print img_picto($langs->trans("TestPrinterTemplate"),'printer');
412  print '</a></td>';
413  }
414  print '</tr>';
415  }
416  }
417 
418  print '</table>';
419  if ($action!='edittemplate') {
420  print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
421  } else {
422  print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
423  }
424  print '</form>';
425  print '<div><p></div>';
426  print '<table class="noborder" width="100%">'."\n";
427  print '<tr class="liste_titre">';
428  print '<th>'.$langs->trans("Tag").'</th>';
429  print '<th>'.$langs->trans("Description").'</th>';
430  print "</tr>\n";
431  $max = count($printer->tags);
432  for ($tag=0; $tag < $max; $tag++)
433  {
434  print '<tr class="oddeven">';
435  print '<td>&lt;'.$printer->tags[$tag].'&gt;</td><td>'.$langs->trans(strtoupper($printer->tags[$tag])).'</td>';
436  print '</tr>';
437  }
438  print '</table>';
439 
440  dol_fiche_end();
441 }
442 
443 // to remove after test
444 // $object=new stdClass();
445 // $object->date_time = '2015-11-02 22:30:25';
446 // $object->id = 1234;
447 // $object->customer_firstname = 'John';
448 // $object->customer_lastname = 'Deuf';
449 // $object->vendor_firstname = 'Jim';
450 // $object->vendor_lastname = 'Big';
451 // $object->barcode = '3700123862396';
452 //$printer->sendToPrinter($object, 1, 16);
453 //setEventMessages($printer->error, $printer->errors, 'errors');
454 
455 // End of page
456 llxFooter();
457 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
Class to manage Receipt Printers.
receiptprinteradmin_prepare_head($mode)
Define head array for tabs of receipt printer setup pages.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.