receiptprinter.php 16 KB

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