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