dolibarr 19.0.4
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
28require '../main.inc.php';
29
30require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/receiptprinter.lib.php';
33require_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
38if (!$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
55if (!$mode) {
56 $mode = 'config';
57}
58
59// used in library escpos maybe useful if php doesn't support gzdecode
60if (!function_exists('gzdecode')) {
67 function gzdecode($data)
68 {
69 return gzinflate(substr($data, 10, -8));
70 }
71}
72
73
74/*
75 * Action
76 */
77
78if ($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
107if ($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
132if ($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
157if ($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
176if ($action == 'testprinter2' && $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 $ret = $printer->sendTestToPrinter($printerid, 1);
186 if ($ret == 0) {
187 setEventMessages($langs->trans("TestSentToPrinter", $printername), null);
188 } else {
189 setEventMessages($printer->error, $printer->errors, 'errors');
190 }
191 }
192 $action = '';
193}
194
195if ($action == 'testtemplate' && $user->admin) {
196 $error = 0;
197 // if (empty($printerid)) {
198 // $error++;
199 // setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
200 // }
201
202 // if (! $error) {
203 // test
204 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
205 $object = new Facture($db);
206 $object->initAsSpecimen();
207 //$object->fetch(18);
208 //var_dump($object->lines);
209 $ret = $printer->sendToPrinter($object, $templateid, 1);
210 if ($ret == 0) {
211 setEventMessages($langs->trans("TestTemplateToPrinter", $printername), null);
212 } else {
213 setEventMessages($printer->error, $printer->errors, 'errors');
214 }
215 //}
216 $action = '';
217}
218
219if ($action == 'updatetemplate' && $user->admin) {
220 $error = 0;
221 if (empty($templateid)) {
222 $error++;
223 setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
224 }
225
226 if (!$error) {
227 $db->begin();
228 $result = $printer->updateTemplate($templatename, $template, $templateid);
229 if ($result > 0) {
230 $error++;
231 }
232
233 if (!$error) {
234 $db->commit();
235 setEventMessages($langs->trans("TemplateUpdated", $templatename), null);
236 } else {
237 $db->rollback();
238 dol_print_error($db);
239 }
240 }
241 $action = '';
242}
243
244if ($action == 'addtemplate' && $user->admin) {
245 $error = 0;
246 if (empty($templatename)) {
247 $error++;
248 setEventMessages($langs->trans("TemplateNameEmpty"), null, 'errors');
249 }
250
251 if (!$error) {
252 $db->begin();
253 $result = $printer->addTemplate($templatename, $template);
254 if ($result > 0) {
255 $error++;
256 }
257
258 if (!$error) {
259 $db->commit();
260 setEventMessages($langs->trans("TemplateAdded", $templatename), null);
261 } else {
262 $db->rollback();
263 dol_print_error($db);
264 }
265 }
266 $action = '';
267}
268
269if ($action == 'deletetemplate' && $user->admin) {
270 $error = 0;
271 if (empty($templateid)) {
272 $error++;
273 setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
274 }
275
276 if (!$error) {
277 $db->begin();
278 $result = $printer->deleteTemplate($templateid);
279 if ($result > 0) {
280 $error++;
281 }
282
283 if (!$error) {
284 $db->commit();
285 setEventMessages($langs->trans("TemplateDeleted", $templatename), null);
286 } else {
287 $db->rollback();
288 dol_print_error($db);
289 }
290 }
291 $action = '';
292}
293
294
295/*
296 * View
297 */
298
299$form = new Form($db);
300
301llxHeader('', $langs->trans("ReceiptPrinterSetup"));
302
303$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
304print load_fiche_titre($langs->trans("ReceiptPrinterSetup"), $linkback, 'title_setup');
305
307
308// mode = config
309if ($mode == 'config' && $user->admin) {
310 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=config" autocomplete="off">';
311 print '<input type="hidden" name="token" value="'.newToken().'">';
312 if ($action != 'editprinter') {
313 print '<input type="hidden" name="action" value="addprinter">';
314 } else {
315 print '<input type="hidden" name="action" value="updateprinter">';
316 }
317
318
319 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
320
321 //print info_admin($langs->trans("ThisFeatureIsForESCPOSPrintersOnly"));
322
323 print load_fiche_titre($langs->trans("ReceiptPrinterDesc"), '', '')."\n";
324
325 print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
326 print '<table class="noborder centpercent">'."\n";
327 print '<tr class="liste_titre">';
328 print '<th>'.$langs->trans("Name").'</th>';
329 print '<th>'.$langs->trans("Type").'</th>';
330 print '<th>';
331 $htmltext = $langs->trans("PROFILE_DEFAULT").' = '.$langs->trans("PROFILE_DEFAULT_HELP").'<br>';
332 $htmltext .= $langs->trans("PROFILE_SIMPLE").' = '.$langs->trans("PROFILE_SIMPLE_HELP").'<br>';
333 $htmltext .= $langs->trans("PROFILE_EPOSTEP").' = '.$langs->trans("PROFILE_EPOSTEP_HELP").'<br>';
334 $htmltext .= $langs->trans("PROFILE_P822D").' = '.$langs->trans("PROFILE_P822D_HELP").'<br>';
335 $htmltext .= $langs->trans("PROFILE_STAR").' = '.$langs->trans("PROFILE_STAR_HELP").'<br>';
336
337 print $form->textwithpicto($langs->trans("Profile"), $htmltext);
338 print '</th>';
339 print '<th>'.$langs->trans("Parameters").'</th>';
340 print '<th></th>';
341 print "</tr>\n";
342
343 $ret = $printer->listprinters();
344 $nbofprinters = count($printer->listprinters);
345
346 if ($action != 'editprinter') {
347 print '<tr>';
348 print '<td><input class="minwidth200" type="text" name="printername"></td>';
349 $ret = $printer->selectTypePrinter();
350 print '<td>'.$printer->resprint.'</td>';
351 $ret = $printer->selectProfilePrinter();
352 print '<td>'.$printer->profileresprint.'</td>';
353 print '<td><input size="60" type="text" name="parameter"></td>';
354 print '<td class="right">';
355 if ($action != 'editprinter') {
356 print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
357 }
358 print '</td>';
359 print '</tr>';
360 }
361
362 if ($ret > 0) {
363 setEventMessages($printer->error, $printer->errors, 'errors');
364 } else {
365 for ($line = 0; $line < $nbofprinters; $line++) {
366 print '<tr class="oddeven">';
367 if ($action == 'editprinter' && $printer->listprinters[$line]['rowid'] == $printerid) {
368 print '<input type="hidden" name="printerid" value="'.$printer->listprinters[$line]['rowid'].'">';
369 print '<td><input type="text" class="minwidth200" name="printername" value="'.$printer->listprinters[$line]['name'].'"></td>';
370 $ret = $printer->selectTypePrinter($printer->listprinters[$line]['fk_type']);
371 print '<td>'.$printer->resprint.'</td>';
372 $ret = $printer->selectProfilePrinter($printer->listprinters[$line]['fk_profile']);
373 print '<td>'.$printer->profileresprint.'</td>';
374 print '<td><input size="60" type="text" name="parameter" value="'.$printer->listprinters[$line]['parameter'].'"></td>';
375 print '<td>';
376 print $form->buttonsSaveCancel("Save", '');
377 print '</td>';
378 print '</tr>';
379 } else {
380 print '<td>'.$printer->listprinters[$line]['name'].'</td>';
381 print '<td>'.$langs->trans($printer->listprinters[$line]['fk_type_name']).'</td>';
382 print '<td>'.$langs->trans($printer->listprinters[$line]['fk_profile_name']).'</td>';
383 print '<td>'.$printer->listprinters[$line]['parameter'].'</td>';
384 // edit icon
385 print '<td class="center">';
386 print '<a class="editfielda marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=config&action=editprinter&token='.newToken().'&printerid='.$printer->listprinters[$line]['rowid'].'">';
387 print img_picto($langs->trans("Edit"), 'edit', 'class="paddingright"');
388 print '</a>';
389 // delete icon
390 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']).'">';
391 print img_picto($langs->trans("Delete"), 'delete', 'class="paddingright"');
392 print '</a>';
393 // test icon
394 print '<a class="marginrightonly nowraponall" href="'.$_SERVER['PHP_SELF'].'?mode=config&action=testprinter&token='.newToken().'&printerid='.$printer->listprinters[$line]['rowid'].'&printername='.urlencode($printer->listprinters[$line]['name']).'">';
395 print img_picto($langs->trans("TestPrinterDesc"), 'printer', 'class="paddingright paddingleft"').'TXT';
396 print '</a>';
397 // test icon
398 print '<a class="marginrightonly nowraponall" href="'.$_SERVER['PHP_SELF'].'?mode=config&action=testprinter2&token='.newToken().'&printerid='.$printer->listprinters[$line]['rowid'].'&printername='.urlencode($printer->listprinters[$line]['name']).'">';
399 print img_picto($langs->trans("TestPrinterDesc2"), 'printer', 'class="paddingright paddingleft"').'IMG';
400 print '</a>';
401 print '</td>';
402 print '</tr>';
403 }
404 }
405 }
406
407 print '</table>';
408 print '</div>';
409
410 print dol_get_fiche_end();
411
412 print '</form>';
413
414 print '<br>';
415
416
417 print '<span class="opacitymedium">'.$langs->trans("ReceiptPrinterTypeDesc").'...</span><br><br>'."\n";
418
419 print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
420 print '<table class="noborder centpercent">'."\n";
421 print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_DUMMY").'</td><td>'.$langs->trans("CONNECTOR_DUMMY_HELP").'</td></tr>';
422 print '<tr class="oddeven"><td>'.$form->textwithpicto($langs->trans("CONNECTOR_NETWORK_PRINT"), $langs->trans("FromServerPointOfView")).'</td><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT_HELP").'</td></tr>';
423 print '<tr class="oddeven"><td>'.$form->textwithpicto($langs->trans("CONNECTOR_FILE_PRINT"), $langs->trans("FromServerPointOfView")).'</td><td>'.$langs->trans("CONNECTOR_FILE_PRINT_HELP").'</td></tr>';
424 print '<tr class="oddeven"><td>'.$form->textwithpicto($langs->trans("CONNECTOR_WINDOWS_PRINT"), $langs->trans("FromServerPointOfView")).'</td><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT_HELP").'</td></tr>';
425 print '<tr class="oddeven"><td>'.$form->textwithpicto($langs->trans("CONNECTOR_CUPS_PRINT"), $langs->trans("FromServerPointOfView")).'</td><td>'.$langs->trans("CONNECTOR_CUPS_PRINT_HELP").'</td></tr>';
426 print '</table>';
427 print '</div>';
428
429 print '<br>';
430}
431
432// mode = template
433if ($mode == 'template' && $user->admin) {
434 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
435
436 //print info_admin($langs->trans("ThisFeatureIsForESCPOSPrintersOnly"));
437
438 print '<br>';
439
440 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=template" autocomplete="off">';
441 print '<input type="hidden" name="token" value="'.newToken().'">';
442 if ($action != 'edittemplate') {
443 print '<input type="hidden" name="action" value="addtemplate">';
444 } else {
445 print '<input type="hidden" name="action" value="updatetemplate">';
446 }
447
448 print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
449 print '<table class="noborder centpercent">'."\n";
450 print '<tr class="liste_titre">';
451 print '<th>'.$langs->trans("Name").'</th>';
452 print '<th>'.$langs->trans("Template").'</th>';
453 print '<th></th>';
454 print "</tr>\n";
455 $ret = $printer->listPrintersTemplates();
456 //print '<pre>'.print_r($printer->listprinterstemplates, true).'</pre>';
457 if ($ret > 0) {
458 setEventMessages($printer->error, $printer->errors, 'errors');
459 } else {
460 $max = count($printer->listprinterstemplates);
461 for ($line = 0; $line < $max; $line++) {
462 print '<tr class="oddeven">';
463 if ($action == 'edittemplate' && $printer->listprinterstemplates[$line]['rowid'] == $templateid) {
464 print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
465 print '<td><input type="text" class="minwidth200" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
466 print '<td class="wordbreak">';
467 print '<textarea name="template" wrap="soft" cols="120" rows="12">'.$printer->listprinterstemplates[$line]['template'].'</textarea>';
468 print '</td>';
469 print '<td>';
470 print $form->buttonsSaveCancel("Save", '');
471 print '</td>';
472 } else {
473 print '<td>'.$printer->listprinterstemplates[$line]['name'].'</td>';
474 print '<td class="wordbreak">'.dol_htmlentitiesbr($printer->listprinterstemplates[$line]['template']).'</td>';
475 // edit icon
476 print '<td class="center"><a class="editfielda paddingleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=template&action=edittemplate&token='.newToken().'&templateid='.$printer->listprinterstemplates[$line]['rowid'].'">';
477 print img_picto($langs->trans("Edit"), 'edit');
478 print '</a>';
479 // delete icon
480 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']).'">';
481 print img_picto($langs->trans("Delete"), 'delete');
482 print '</a>';
483 // test icon
484 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']).'">';
485 print img_picto($langs->trans("TestPrinterTemplate"), 'printer');
486 print '</a></td>';
487 }
488 print '</tr>';
489 }
490 }
491
492 if ($action != 'edittemplate') {
493 print '<tr>';
494 print '<td><input type="text" class="minwidth200" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
495 print '<td class="wordbreak">';
496 print '<textarea name="template" wrap="soft" cols="120" rows="12">';
497 print '</textarea>';
498 print '</td>';
499 print '<td>';
500 print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
501 print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'">';
502 print '</td>';
503 print '</tr>';
504 }
505
506 print '</table>';
507 print '</div>';
508
509 print '</form>';
510
511 print dol_get_fiche_end();
512
513 print '<br>';
514
515 print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
516 print '<table class="noborder centpercent">'."\n";
517 print '<tr class="liste_titre">';
518 print '<th>'.$langs->trans("Tag").'</th>';
519 print '<th>'.$langs->trans("Description").'</th>';
520 print "</tr>\n";
521
522 $langs->loadLangs(array("bills", "companies"));
523 foreach ($printer->tags as $key => $val) {
524 print '<tr class="oddeven">';
525 print '<td>{'.$key.'}</td><td>'.$langs->trans($val).'</td>';
526 print '</tr>';
527 }
528 print '</table>';
529 print '</div>';
530}
531
532// End of page
533llxFooter();
534$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage invoices.
Class to manage generation of HTML components Only common components must be here.
Class to manage Receipt Printers.
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
receiptprinteradmin_prepare_head($mode)
Define head array for tabs of receipt printer setup pages.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.