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