dolibarr 18.0.6
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 == '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
200if ($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
225if ($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
250if ($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
282llxHeader('', $langs->trans("ReceiptPrinterSetup"));
283
284$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
285print load_fiche_titre($langs->trans("ReceiptPrinterSetup"), $linkback, 'title_setup');
286
288
289// mode = config
290if ($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
321 $ret = $printer->listprinters();
322 $nbofprinters = count($printer->listprinters);
323
324 if ($action != 'editprinter') {
325 print '<tr>';
326 print '<td><input class="minwidth200" type="text" name="printername"></td>';
327 $ret = $printer->selectTypePrinter();
328 print '<td>'.$printer->resprint.'</td>';
329 $ret = $printer->selectProfilePrinter();
330 print '<td>'.$printer->profileresprint.'</td>';
331 print '<td><input size="60" type="text" name="parameter"></td>';
332 print '<td class="right">';
333 if ($action != 'editprinter') {
334 print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
335 }
336 print '</td>';
337 print '</tr>';
338 }
339
340 if ($ret > 0) {
341 setEventMessages($printer->error, $printer->errors, 'errors');
342 } else {
343 for ($line = 0; $line < $nbofprinters; $line++) {
344 print '<tr class="oddeven">';
345 if ($action == 'editprinter' && $printer->listprinters[$line]['rowid'] == $printerid) {
346 print '<input type="hidden" name="printerid" value="'.$printer->listprinters[$line]['rowid'].'">';
347 print '<td><input type="text" class="minwidth200" name="printername" value="'.$printer->listprinters[$line]['name'].'"></td>';
348 $ret = $printer->selectTypePrinter($printer->listprinters[$line]['fk_type']);
349 print '<td>'.$printer->resprint.'</td>';
350 $ret = $printer->selectProfilePrinter($printer->listprinters[$line]['fk_profile']);
351 print '<td>'.$printer->profileresprint.'</td>';
352 print '<td><input size="60" type="text" name="parameter" value="'.$printer->listprinters[$line]['parameter'].'"></td>';
353 print '<td>';
354 print $form->buttonsSaveCancel("Save", '');
355 print '</td>';
356 print '</tr>';
357 } else {
358 print '<td>'.$printer->listprinters[$line]['name'].'</td>';
359 print '<td>'.$langs->trans($printer->listprinters[$line]['fk_type_name']).'</td>';
360 print '<td>'.$langs->trans($printer->listprinters[$line]['fk_profile_name']).'</td>';
361 print '<td>'.$printer->listprinters[$line]['parameter'].'</td>';
362 // edit icon
363 print '<td class="right"><a class="editfielda marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=config&action=editprinter&token='.newToken().'&printerid='.$printer->listprinters[$line]['rowid'].'">';
364 print img_picto($langs->trans("Edit"), 'edit');
365 print '</a>';
366 // delete icon
367 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']).'">';
368 print img_picto($langs->trans("Delete"), 'delete');
369 print '</a>';
370 // test icon
371 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']).'">';
372 print img_picto($langs->trans("TestPrinter"), 'printer');
373 print '</a></td>';
374 print '</tr>';
375 }
376 }
377 }
378
379 print '</table>';
380
381 print dol_get_fiche_end();
382
383 print '</form>';
384
385 print '<br>';
386
387
388 print load_fiche_titre($langs->trans("ReceiptPrinterTypeDesc"), '', '')."\n";
389
390 print '<table class="noborder centpercent">'."\n";
391 print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_DUMMY").':</td><td>'.$langs->trans("CONNECTOR_DUMMY_HELP").'</td></tr>';
392 print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT").':</td><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT_HELP").'</td></tr>';
393 print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_FILE_PRINT").':</td><td>'.$langs->trans("CONNECTOR_FILE_PRINT_HELP").'</td></tr>';
394 print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT").':</td><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT_HELP").'</td></tr>';
395 print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_CUPS_PRINT").':</td><td>'.$langs->trans("CONNECTOR_CUPS_PRINT_HELP").'</td></tr>';
396 print '</table>';
397
398 print '<br>';
399}
400
401// mode = template
402if ($mode == 'template' && $user->admin) {
403 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
404
405 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=template" autocomplete="off">';
406 print '<input type="hidden" name="token" value="'.newToken().'">';
407 if ($action != 'edittemplate') {
408 print '<input type="hidden" name="action" value="addtemplate">';
409 } else {
410 print '<input type="hidden" name="action" value="updatetemplate">';
411 }
412
413 print '<table class="noborder centpercent">'."\n";
414 print '<tr class="liste_titre">';
415 print '<th>'.$langs->trans("Name").'</th>';
416 print '<th>'.$langs->trans("Template").'</th>';
417 print '<th></th>';
418 print "</tr>\n";
419 $ret = $printer->listPrintersTemplates();
420 //print '<pre>'.print_r($printer->listprinterstemplates, true).'</pre>';
421 if ($ret > 0) {
422 setEventMessages($printer->error, $printer->errors, 'errors');
423 } else {
424 $max = count($printer->listprinterstemplates);
425 for ($line = 0; $line < $max; $line++) {
426 print '<tr class="oddeven">';
427 if ($action == 'edittemplate' && $printer->listprinterstemplates[$line]['rowid'] == $templateid) {
428 print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
429 print '<td><input type="text" class="minwidth200" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
430 print '<td>';
431 print '<textarea name="template" wrap="soft" cols="120" rows="12">'.$printer->listprinterstemplates[$line]['template'].'</textarea>';
432 print '</td>';
433 print '<td>';
434 print $form->buttonsSaveCancel("Save", '');
435 print '</td>';
436 } else {
437 print '<td>'.$printer->listprinterstemplates[$line]['name'].'</td>';
438 print '<td>'.dol_htmlentitiesbr($printer->listprinterstemplates[$line]['template']).'</td>';
439 // edit icon
440 print '<td><a class="editfielda paddingleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=template&action=edittemplate&token='.newToken().'&templateid='.$printer->listprinterstemplates[$line]['rowid'].'">';
441 print img_picto($langs->trans("Edit"), 'edit');
442 print '</a>';
443 // delete icon
444 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']).'">';
445 print img_picto($langs->trans("Delete"), 'delete');
446 print '</a>';
447 // test icon
448 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']).'">';
449 print img_picto($langs->trans("TestPrinterTemplate"), 'printer');
450 print '</a></td>';
451 }
452 print '</tr>';
453 }
454 }
455
456 if ($action != 'edittemplate') {
457 print '<tr>';
458 print '<td><input type="text" class="minwidth200" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
459 print '<td>';
460 print '<textarea name="template" wrap="soft" cols="120" rows="12">';
461 print '</textarea>';
462 print '</td>';
463 print '<td>';
464 print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
465 print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'">';
466 print '</td>';
467 print '</tr>';
468 }
469
470 print '</table>';
471
472 print '</form>';
473
474 print dol_get_fiche_end();
475
476 print '<br>';
477
478 print '<table class="noborder centpercent">'."\n";
479 print '<tr class="liste_titre">';
480 print '<th>'.$langs->trans("Tag").'</th>';
481 print '<th>'.$langs->trans("Description").'</th>';
482 print "</tr>\n";
483
484 $langs->loadLangs(array("bills", "companies"));
485 foreach ($printer->tags as $key => $val) {
486 print '<tr class="oddeven">';
487 print '<td>{'.$key.'}</td><td>'.$langs->trans($val).'</td>';
488 print '</tr>';
489 }
490 print '</table>';
491}
492
493// End of page
494llxFooter();
495$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:56
llxFooter()
Empty footer.
Definition wrapper.php:70
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.