receiptprinter.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/admin/receiptprinter.php
  21. * \ingroup printing
  22. * \brief Page to setup receipt printer
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/receiptprinter.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("admin","receiptprinter"));
  31. if (! $user->admin) accessforbidden();
  32. $action = GETPOST('action','alpha');
  33. $mode = GETPOST('mode','alpha');
  34. $printername = GETPOST('printername', 'alpha');
  35. $printerid = GETPOST('printerid', 'int');
  36. $parameter = GETPOST('parameter', 'alpha');
  37. $template = GETPOST('template', 'alpha');
  38. $templatename = GETPOST('templatename', 'alpha');
  39. $templateid = GETPOST('templateid', 'int');
  40. $printer = new dolReceiptPrinter($db);
  41. if (!$mode) $mode='config';
  42. // used in library escpos maybe useful if php doesn't support gzdecode
  43. if (!function_exists('gzdecode')) {
  44. /**
  45. * Gzdecode
  46. *
  47. * @param string $data data to deflate
  48. * @return string data deflated
  49. */
  50. function gzdecode($data)
  51. {
  52. return gzinflate(substr($data,10,-8));
  53. }
  54. }
  55. /*
  56. * Action
  57. */
  58. if ($action == 'addprinter' && $user->admin)
  59. {
  60. $error=0;
  61. $db->begin();
  62. if (empty($printername)) {
  63. $error++;
  64. setEventMessages($langs->trans("PrinterNameEmpty"), null, 'errors');
  65. }
  66. if (empty($parameter)) {
  67. setEventMessages($langs->trans("PrinterParameterEmpty"), null, 'warnings');
  68. }
  69. if (! $error)
  70. {
  71. $result= $printer->AddPrinter($printername, GETPOST('printertypeid', 'int'), GETPOST('printerprofileid', 'int'), $parameter);
  72. if ($result > 0) $error++;
  73. if (! $error)
  74. {
  75. $db->commit();
  76. setEventMessages($langs->trans("PrinterAdded",$printername), null);
  77. }
  78. else
  79. {
  80. $db->rollback();
  81. dol_print_error($db);
  82. }
  83. }
  84. $action = '';
  85. }
  86. if ($action == 'deleteprinter' && $user->admin)
  87. {
  88. $error=0;
  89. $db->begin();
  90. if (empty($printerid)) {
  91. $error++;
  92. setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
  93. }
  94. if (! $error)
  95. {
  96. $result= $printer->DeletePrinter($printerid);
  97. if ($result > 0) $error++;
  98. if (! $error)
  99. {
  100. $db->commit();
  101. setEventMessages($langs->trans("PrinterDeleted",$printername), null);
  102. }
  103. else
  104. {
  105. $db->rollback();
  106. dol_print_error($db);
  107. }
  108. }
  109. $action = '';
  110. }
  111. if ($action == 'updateprinter' && $user->admin)
  112. {
  113. $error=0;
  114. $db->begin();
  115. if (empty($printerid)) {
  116. $error++;
  117. setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
  118. }
  119. if (! $error)
  120. {
  121. $result= $printer->UpdatePrinter($printername, GETPOST('printertypeid', 'int'), GETPOST('printerprofileid', 'int'), $parameter, $printerid);
  122. if ($result > 0) $error++;
  123. if (! $error)
  124. {
  125. $db->commit();
  126. setEventMessages($langs->trans("PrinterUpdated",$printername), null);
  127. }
  128. else
  129. {
  130. $db->rollback();
  131. dol_print_error($db);
  132. }
  133. }
  134. $action = '';
  135. }
  136. if ($action == 'testprinter' && $user->admin)
  137. {
  138. $error=0;
  139. if (empty($printerid)) {
  140. $error++;
  141. setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
  142. }
  143. if (! $error)
  144. {
  145. // test
  146. $ret = $printer->SendTestToPrinter($printerid);
  147. if ($ret == 0)
  148. {
  149. setEventMessages($langs->trans("TestSentToPrinter", $printername), null);
  150. }
  151. else
  152. {
  153. setEventMessages($printer->error, $printer->errors, 'errors');
  154. }
  155. }
  156. $action = '';
  157. }
  158. if ($action == 'updatetemplate' && $user->admin)
  159. {
  160. $error=0;
  161. $db->begin();
  162. if (empty($templateid)) {
  163. $error++;
  164. setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
  165. }
  166. if (! $error)
  167. {
  168. $result= $printer->UpdateTemplate($templatename, $template, $templateid);
  169. if ($result > 0) $error++;
  170. if (! $error)
  171. {
  172. $db->commit();
  173. setEventMessages($langs->trans("TemplateUpdated",$templatename), null);
  174. }
  175. else
  176. {
  177. $db->rollback();
  178. dol_print_error($db);
  179. }
  180. }
  181. $action = '';
  182. }
  183. /*
  184. * View
  185. */
  186. $form = new Form($db);
  187. llxHeader('',$langs->trans("ReceiptPrinterSetup"));
  188. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  189. print load_fiche_titre($langs->trans("ReceiptPrinterSetup"),$linkback,'title_setup');
  190. $head = receiptprinteradmin_prepare_head($mode);
  191. if ($mode == 'config' && $user->admin)
  192. {
  193. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=config" autocomplete="off">';
  194. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  195. if ($action!='editprinter') {
  196. print '<input type="hidden" name="action" value="addprinter">';
  197. } else {
  198. print '<input type="hidden" name="action" value="updateprinter">';
  199. }
  200. dol_fiche_head($head, $mode, $langs->trans("ModuleSetup"), 0, 'technic');
  201. print $langs->trans("ReceiptPrinterDesc")."<br><br>\n";
  202. print '<table class="noborder" width="100%">'."\n";
  203. print '<tr class="liste_titre">';
  204. print '<th>'.$langs->trans("Name").'</th>';
  205. print '<th>'.$langs->trans("Type").'</th>';
  206. print '<th>'.$langs->trans("Profile").'</th>';
  207. print '<th>'.$langs->trans("Parameters").'</th>';
  208. print '<th></th>';
  209. print '<th></th>';
  210. print '<th></th>';
  211. print "</tr>\n";
  212. $ret = $printer->listprinters();
  213. $nbofprinters = count($printer->listprinters);
  214. if ($ret > 0) {
  215. setEventMessages($printer->error, $printer->errors, 'errors');
  216. } else {
  217. for ($line=0; $line < $nbofprinters; $line++)
  218. {
  219. print '<tr class="oddeven">';
  220. if ($action=='editprinter' && $printer->listprinters[$line]['rowid']==$printerid)
  221. {
  222. print '<input type="hidden" name="printerid" value="'.$printer->listprinters[$line]['rowid'].'">';
  223. print '<td><input size="50" type="text" name="printername" value="'.$printer->listprinters[$line]['name'].'"></td>';
  224. $ret = $printer->selectTypePrinter($printer->listprinters[$line]['fk_type']);
  225. print '<td>'.$printer->resprint.'</td>';
  226. $ret = $printer->selectProfilePrinter($printer->listprinters[$line]['fk_profile']);
  227. print '<td>'.$printer->profileresprint.'</td>';
  228. print '<td><input size="60" type="text" name="parameter" value="'.$printer->listprinters[$line]['parameter'].'"></td>';
  229. print '<td></td>';
  230. print '<td></td>';
  231. print '<td></td>';
  232. print '</tr>';
  233. } else {
  234. print '<td>'.$printer->listprinters[$line]['name'].'</td>';
  235. print '<td>'.$langs->trans($printer->listprinters[$line]['fk_type_name']).'</td>';
  236. print '<td>'.$langs->trans($printer->listprinters[$line]['fk_profile_name']).'</td>';
  237. print '<td>'.$printer->listprinters[$line]['parameter'].'</td>';
  238. // edit icon
  239. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=editprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'">';
  240. print img_picto($langs->trans("Edit"),'edit');
  241. print '</a></td>';
  242. // delete icon
  243. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=deleteprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'&amp;printername='.$printer->listprinters[$line]['name'].'">';
  244. print img_picto($langs->trans("Delete"),'delete');
  245. print '</a></td>';
  246. // test icon
  247. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=testprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'&amp;printername='.$printer->listprinters[$line]['name'].'">';
  248. print img_picto($langs->trans("TestPrinter"),'printer');
  249. print '</a></td>';
  250. print '</tr>';
  251. }
  252. }
  253. }
  254. if ($action!='editprinter')
  255. {
  256. if ($nbofprinters > 0)
  257. {
  258. print '<tr class="liste_titre">';
  259. print '<th>'.$langs->trans("Name").'</th>';
  260. print '<th>'.$langs->trans("Type").'</th>';
  261. print '<th>'.$langs->trans("Profile").'</th>';
  262. print '<th>'.$langs->trans("Parameters").'</th>';
  263. print '<th></th>';
  264. print '<th></th>';
  265. print '<th></th>';
  266. print "</tr>\n";
  267. }
  268. print '<tr>';
  269. print '<td><input size="50" type="text" name="printername"></td>';
  270. $ret = $printer->selectTypePrinter();
  271. print '<td>'.$printer->resprint.'</td>';
  272. $ret = $printer->selectProfilePrinter();
  273. print '<td>'.$printer->profileresprint.'</td>';
  274. print '<td><input size="60" type="text" name="parameter"></td>';
  275. print '<td></td>';
  276. print '<td></td>';
  277. print '<td></td>';
  278. print '</tr>';
  279. }
  280. print '</table>';
  281. dol_fiche_end();
  282. if ($action!='editprinter') {
  283. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
  284. } else {
  285. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
  286. }
  287. print '</form>';
  288. print '<div><p></div>';
  289. dol_fiche_head();
  290. print $langs->trans("ReceiptPrinterTypeDesc")."<br><br>\n";
  291. print '<table class="noborder" width="100%">'."\n";
  292. print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_DUMMY").':</td><td>'.$langs->trans("CONNECTOR_DUMMY_HELP").'</td></tr>';
  293. print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT").':</td><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT_HELP").'</td></tr>';
  294. print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_FILE_PRINT").':</td><td>'.$langs->trans("CONNECTOR_FILE_PRINT_HELP").'</td></tr>';
  295. print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT").':</td><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT_HELP").'</td></tr>';
  296. //print '<tr class="oddeven"><td>'.$langs->trans("CONNECTOR_JAVA").':</td><td>'.$langs->trans("CONNECTOR_JAVA_HELP").'</td></tr>';
  297. print '</table>';
  298. dol_fiche_end();
  299. print '<div><p></div>';
  300. dol_fiche_head();
  301. print $langs->trans("ReceiptPrinterProfileDesc")."<br><br>\n";
  302. print '<table class="noborder" width="100%">'."\n";
  303. print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_DEFAULT").':</td><td>'.$langs->trans("PROFILE_DEFAULT_HELP").'</td></tr>';
  304. print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_SIMPLE").':</td><td>'.$langs->trans("PROFILE_SIMPLE_HELP").'</td></tr>';
  305. print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_EPOSTEP").':</td><td>'.$langs->trans("PROFILE_EPOSTEP_HELP").'</td></tr>';
  306. print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_P822D").':</td><td>'.$langs->trans("PROFILE_P822D_HELP").'</td></tr>';
  307. print '<tr class="oddeven"><td>'.$langs->trans("PROFILE_STAR").':</td><td>'.$langs->trans("PROFILE_STAR_HELP").'</td></tr>';
  308. print '</table>';
  309. dol_fiche_end();
  310. }
  311. if ($mode == 'template' && $user->admin)
  312. {
  313. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=template" autocomplete="off">';
  314. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  315. if ($action!='edittemplate') {
  316. print '<input type="hidden" name="action" value="addtemplate">';
  317. } else {
  318. print '<input type="hidden" name="action" value="updatetemplate">';
  319. }
  320. dol_fiche_head($head, $mode, $langs->trans("ModuleSetup"), 0, 'technic');
  321. print $langs->trans("ReceiptPrinterTemplateDesc")."<br><br>\n";
  322. print '<table class="noborder" width="100%">'."\n";
  323. print '<tr class="liste_titre">';
  324. print '<th>'.$langs->trans("Name").'</th>';
  325. print '<th>'.$langs->trans("Template").'</th>';
  326. print '<th></th>';
  327. print '<th></th>';
  328. print '<th></th>';
  329. print "</tr>\n";
  330. $ret = $printer->listPrintersTemplates();
  331. //print '<pre>'.print_r($printer->listprinterstemplates, true).'</pre>';
  332. if ($ret > 0) {
  333. setEventMessages($printer->error, $printer->errors, 'errors');
  334. } else {
  335. $max = count($printer->listprinterstemplates);
  336. for ($line=0; $line < $max; $line++)
  337. {
  338. print '<tr class="oddeven">';
  339. if ($action=='edittemplate' && $printer->listprinterstemplates[$line]['rowid']==$templateid) {
  340. print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
  341. print '<td><input size="50" type="text" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
  342. print '<td><textarea name="template" wrap="soft" cols="120" rows="12">'.$printer->listprinterstemplates[$line]['template'].'</textarea>';
  343. print '</td>';
  344. print '<td></td>';
  345. print '<td></td>';
  346. print '<td></td>';
  347. } else {
  348. print '<td>'.$printer->listprinterstemplates[$line]['name'].'</td>';
  349. print '<td>'.nl2br(htmlentities($printer->listprinterstemplates[$line]['template'])).'</td>';
  350. // edit icon
  351. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=edittemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'">';
  352. print img_picto($langs->trans("Edit"),'edit');
  353. print '</a></td>';
  354. // delete icon
  355. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=deletetemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'&amp;templatename='.$printer->listprinterstemplates[$line]['name'].'">';
  356. print img_picto($langs->trans("Delete"),'delete');
  357. print '</a></td>';
  358. // test icon
  359. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=testtemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'&amp;templatename='.$printer->listprinterstemplates[$line]['name'].'">';
  360. print img_picto($langs->trans("TestPrinterTemplate"),'printer');
  361. print '</a></td>';
  362. }
  363. print '</tr>';
  364. }
  365. }
  366. print '</table>';
  367. if ($action!='edittemplate') {
  368. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
  369. } else {
  370. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
  371. }
  372. print '</form>';
  373. print '<div><p></div>';
  374. print '<table class="noborder" width="100%">'."\n";
  375. print '<tr class="liste_titre">';
  376. print '<th>'.$langs->trans("Tag").'</th>';
  377. print '<th>'.$langs->trans("Description").'</th>';
  378. print "</tr>\n";
  379. $max = count($printer->tags);
  380. for ($tag=0; $tag < $max; $tag++)
  381. {
  382. print '<tr class="oddeven">';
  383. print '<td>&lt;'.$printer->tags[$tag].'&gt;</td><td>'.$langs->trans(strtoupper($printer->tags[$tag])).'</td>';
  384. print '</tr>';
  385. }
  386. print '</table>';
  387. dol_fiche_end();
  388. }
  389. // to remove after test
  390. // $object=new stdClass();
  391. // $object->date_time = '2015-11-02 22:30:25';
  392. // $object->id = 1234;
  393. // $object->customer_firstname = 'John';
  394. // $object->customer_lastname = 'Deuf';
  395. // $object->vendor_firstname = 'Jim';
  396. // $object->vendor_lastname = 'Big';
  397. // $object->barcode = '3700123862396';
  398. //$printer->sendToPrinter($object, 1, 16);
  399. //setEventMessages($printer->error, $printer->errors, 'errors');
  400. // End of page
  401. llxFooter();
  402. $db->close();