receiptprinter.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 == 'testprinter2' && $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. $ret = $printer->sendTestToPrinter($printerid, 1);
  159. if ($ret == 0) {
  160. setEventMessages($langs->trans("TestSentToPrinter", $printername), null);
  161. } else {
  162. setEventMessages($printer->error, $printer->errors, 'errors');
  163. }
  164. }
  165. $action = '';
  166. }
  167. if ($action == 'testtemplate' && $user->admin) {
  168. $error = 0;
  169. // if (empty($printerid)) {
  170. // $error++;
  171. // setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
  172. // }
  173. // if (! $error) {
  174. // test
  175. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  176. $object = new Facture($db);
  177. $object->initAsSpecimen();
  178. //$object->fetch(18);
  179. //var_dump($object->lines);
  180. $ret = $printer->sendToPrinter($object, $templateid, 1);
  181. if ($ret == 0) {
  182. setEventMessages($langs->trans("TestTemplateToPrinter", $printername), null);
  183. } else {
  184. setEventMessages($printer->error, $printer->errors, 'errors');
  185. }
  186. //}
  187. $action = '';
  188. }
  189. if ($action == 'updatetemplate' && $user->admin) {
  190. $error = 0;
  191. if (empty($templateid)) {
  192. $error++;
  193. setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
  194. }
  195. if (!$error) {
  196. $db->begin();
  197. $result = $printer->updateTemplate($templatename, $template, $templateid);
  198. if ($result > 0) {
  199. $error++;
  200. }
  201. if (!$error) {
  202. $db->commit();
  203. setEventMessages($langs->trans("TemplateUpdated", $templatename), null);
  204. } else {
  205. $db->rollback();
  206. dol_print_error($db);
  207. }
  208. }
  209. $action = '';
  210. }
  211. if ($action == 'addtemplate' && $user->admin) {
  212. $error = 0;
  213. if (empty($templatename)) {
  214. $error++;
  215. setEventMessages($langs->trans("TemplateNameEmpty"), null, 'errors');
  216. }
  217. if (!$error) {
  218. $db->begin();
  219. $result = $printer->addTemplate($templatename, $template);
  220. if ($result > 0) {
  221. $error++;
  222. }
  223. if (!$error) {
  224. $db->commit();
  225. setEventMessages($langs->trans("TemplateAdded", $templatename), null);
  226. } else {
  227. $db->rollback();
  228. dol_print_error($db);
  229. }
  230. }
  231. $action = '';
  232. }
  233. if ($action == 'deletetemplate' && $user->admin) {
  234. $error = 0;
  235. if (empty($templateid)) {
  236. $error++;
  237. setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
  238. }
  239. if (!$error) {
  240. $db->begin();
  241. $result = $printer->deleteTemplate($templateid);
  242. if ($result > 0) {
  243. $error++;
  244. }
  245. if (!$error) {
  246. $db->commit();
  247. setEventMessages($langs->trans("TemplateDeleted", $templatename), null);
  248. } else {
  249. $db->rollback();
  250. dol_print_error($db);
  251. }
  252. }
  253. $action = '';
  254. }
  255. /*
  256. * View
  257. */
  258. $form = new Form($db);
  259. llxHeader('', $langs->trans("ReceiptPrinterSetup"));
  260. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  261. print load_fiche_titre($langs->trans("ReceiptPrinterSetup"), $linkback, 'title_setup');
  262. $head = receiptprinteradmin_prepare_head($mode);
  263. // mode = config
  264. if ($mode == 'config' && $user->admin) {
  265. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=config" autocomplete="off">';
  266. print '<input type="hidden" name="token" value="'.newToken().'">';
  267. if ($action != 'editprinter') {
  268. print '<input type="hidden" name="action" value="addprinter">';
  269. } else {
  270. print '<input type="hidden" name="action" value="updateprinter">';
  271. }
  272. print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
  273. //print info_admin($langs->trans("ThisFeatureIsForESCPOSPrintersOnly"));
  274. print load_fiche_titre($langs->trans("ReceiptPrinterDesc"), '', '')."\n";
  275. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
  276. print '<table class="noborder centpercent">'."\n";
  277. print '<tr class="liste_titre">';
  278. print '<th>'.$langs->trans("Name").'</th>';
  279. print '<th>'.$langs->trans("Type").'</th>';
  280. print '<th>';
  281. $htmltext = $langs->trans("PROFILE_DEFAULT").' = '.$langs->trans("PROFILE_DEFAULT_HELP").'<br>';
  282. $htmltext .= $langs->trans("PROFILE_SIMPLE").' = '.$langs->trans("PROFILE_SIMPLE_HELP").'<br>';
  283. $htmltext .= $langs->trans("PROFILE_EPOSTEP").' = '.$langs->trans("PROFILE_EPOSTEP_HELP").'<br>';
  284. $htmltext .= $langs->trans("PROFILE_P822D").' = '.$langs->trans("PROFILE_P822D_HELP").'<br>';
  285. $htmltext .= $langs->trans("PROFILE_STAR").' = '.$langs->trans("PROFILE_STAR_HELP").'<br>';
  286. print $form->textwithpicto($langs->trans("Profile"), $htmltext);
  287. print '</th>';
  288. print '<th>'.$langs->trans("Parameters").'</th>';
  289. print '<th></th>';
  290. print "</tr>\n";
  291. $ret = $printer->listprinters();
  292. $nbofprinters = count($printer->listprinters);
  293. if ($action != 'editprinter') {
  294. print '<tr>';
  295. print '<td><input class="minwidth200" type="text" name="printername"></td>';
  296. $ret = $printer->selectTypePrinter();
  297. print '<td>'.$printer->resprint.'</td>';
  298. $ret = $printer->selectProfilePrinter();
  299. print '<td>'.$printer->profileresprint.'</td>';
  300. print '<td><input size="60" type="text" name="parameter"></td>';
  301. print '<td class="right">';
  302. if ($action != 'editprinter') {
  303. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
  304. }
  305. print '</td>';
  306. print '</tr>';
  307. }
  308. if ($ret > 0) {
  309. setEventMessages($printer->error, $printer->errors, 'errors');
  310. } else {
  311. for ($line = 0; $line < $nbofprinters; $line++) {
  312. print '<tr class="oddeven">';
  313. if ($action == 'editprinter' && $printer->listprinters[$line]['rowid'] == $printerid) {
  314. print '<input type="hidden" name="printerid" value="'.$printer->listprinters[$line]['rowid'].'">';
  315. print '<td><input type="text" class="minwidth200" name="printername" value="'.$printer->listprinters[$line]['name'].'"></td>';
  316. $ret = $printer->selectTypePrinter($printer->listprinters[$line]['fk_type']);
  317. print '<td>'.$printer->resprint.'</td>';
  318. $ret = $printer->selectProfilePrinter($printer->listprinters[$line]['fk_profile']);
  319. print '<td>'.$printer->profileresprint.'</td>';
  320. print '<td><input size="60" type="text" name="parameter" value="'.$printer->listprinters[$line]['parameter'].'"></td>';
  321. print '<td>';
  322. print $form->buttonsSaveCancel("Save", '');
  323. print '</td>';
  324. print '</tr>';
  325. } else {
  326. print '<td>'.$printer->listprinters[$line]['name'].'</td>';
  327. print '<td>'.$langs->trans($printer->listprinters[$line]['fk_type_name']).'</td>';
  328. print '<td>'.$langs->trans($printer->listprinters[$line]['fk_profile_name']).'</td>';
  329. print '<td>'.$printer->listprinters[$line]['parameter'].'</td>';
  330. // edit icon
  331. print '<td class="center">';
  332. print '<a class="editfielda marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=config&action=editprinter&token='.newToken().'&printerid='.$printer->listprinters[$line]['rowid'].'">';
  333. print img_picto($langs->trans("Edit"), 'edit', 'class="paddingright"');
  334. print '</a>';
  335. // delete icon
  336. 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']).'">';
  337. print img_picto($langs->trans("Delete"), 'delete', 'class="paddingright"');
  338. print '</a>';
  339. // test icon
  340. 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']).'">';
  341. print img_picto($langs->trans("TestPrinterDesc"), 'printer', 'class="paddingright paddingleft"').'TXT';
  342. print '</a>';
  343. // test icon
  344. 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']).'">';
  345. print img_picto($langs->trans("TestPrinterDesc2"), 'printer', 'class="paddingright paddingleft"').'IMG';
  346. print '</a>';
  347. print '</td>';
  348. print '</tr>';
  349. }
  350. }
  351. }
  352. print '</table>';
  353. print '</div>';
  354. print dol_get_fiche_end();
  355. print '</form>';
  356. print '<br>';
  357. print '<span class="opacitymedium">'.$langs->trans("ReceiptPrinterTypeDesc").'...</span><br><br>'."\n";
  358. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
  359. print '<table class="noborder centpercent">'."\n";
  360. print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_DUMMY").'</td><td>'.$langs->trans("CONNECTOR_DUMMY_HELP").'</td></tr>';
  361. 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>';
  362. 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>';
  363. 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>';
  364. 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>';
  365. print '</table>';
  366. print '</div>';
  367. print '<br>';
  368. }
  369. // mode = template
  370. if ($mode == 'template' && $user->admin) {
  371. print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
  372. //print info_admin($langs->trans("ThisFeatureIsForESCPOSPrintersOnly"));
  373. print '<br>';
  374. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=template" autocomplete="off">';
  375. print '<input type="hidden" name="token" value="'.newToken().'">';
  376. if ($action != 'edittemplate') {
  377. print '<input type="hidden" name="action" value="addtemplate">';
  378. } else {
  379. print '<input type="hidden" name="action" value="updatetemplate">';
  380. }
  381. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
  382. print '<table class="noborder centpercent">'."\n";
  383. print '<tr class="liste_titre">';
  384. print '<th>'.$langs->trans("Name").'</th>';
  385. print '<th>'.$langs->trans("Template").'</th>';
  386. print '<th></th>';
  387. print "</tr>\n";
  388. $ret = $printer->listPrintersTemplates();
  389. //print '<pre>'.print_r($printer->listprinterstemplates, true).'</pre>';
  390. if ($ret > 0) {
  391. setEventMessages($printer->error, $printer->errors, 'errors');
  392. } else {
  393. $max = count($printer->listprinterstemplates);
  394. for ($line = 0; $line < $max; $line++) {
  395. print '<tr class="oddeven">';
  396. if ($action == 'edittemplate' && $printer->listprinterstemplates[$line]['rowid'] == $templateid) {
  397. print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
  398. print '<td><input type="text" class="minwidth200" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
  399. print '<td class="wordbreak">';
  400. print '<textarea name="template" wrap="soft" cols="120" rows="12">'.$printer->listprinterstemplates[$line]['template'].'</textarea>';
  401. print '</td>';
  402. print '<td>';
  403. print $form->buttonsSaveCancel("Save", '');
  404. print '</td>';
  405. } else {
  406. print '<td>'.$printer->listprinterstemplates[$line]['name'].'</td>';
  407. print '<td class="wordbreak">'.dol_htmlentitiesbr($printer->listprinterstemplates[$line]['template']).'</td>';
  408. // edit icon
  409. print '<td class="center"><a class="editfielda paddingleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?mode=template&action=edittemplate&token='.newToken().'&templateid='.$printer->listprinterstemplates[$line]['rowid'].'">';
  410. print img_picto($langs->trans("Edit"), 'edit');
  411. print '</a>';
  412. // delete icon
  413. 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']).'">';
  414. print img_picto($langs->trans("Delete"), 'delete');
  415. print '</a>';
  416. // test icon
  417. 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']).'">';
  418. print img_picto($langs->trans("TestPrinterTemplate"), 'printer');
  419. print '</a></td>';
  420. }
  421. print '</tr>';
  422. }
  423. }
  424. if ($action != 'edittemplate') {
  425. print '<tr>';
  426. print '<td><input type="text" class="minwidth200" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
  427. print '<td class="wordbreak">';
  428. print '<textarea name="template" wrap="soft" cols="120" rows="12">';
  429. print '</textarea>';
  430. print '</td>';
  431. print '<td>';
  432. print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
  433. print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'">';
  434. print '</td>';
  435. print '</tr>';
  436. }
  437. print '</table>';
  438. print '</div>';
  439. print '</form>';
  440. print dol_get_fiche_end();
  441. print '<br>';
  442. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
  443. print '<table class="noborder centpercent">'."\n";
  444. print '<tr class="liste_titre">';
  445. print '<th>'.$langs->trans("Tag").'</th>';
  446. print '<th>'.$langs->trans("Description").'</th>';
  447. print "</tr>\n";
  448. $langs->loadLangs(array("bills", "companies"));
  449. foreach ($printer->tags as $key => $val) {
  450. print '<tr class="oddeven">';
  451. print '<td>{'.$key.'}</td><td>'.$langs->trans($val).'</td>';
  452. print '</tr>';
  453. }
  454. print '</table>';
  455. print '</div>';
  456. }
  457. // End of page
  458. llxFooter();
  459. $db->close();