receiptprinter.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. $langs->load("admin");
  30. $langs->load("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. function gzdecode($data)
  45. {
  46. return gzinflate(substr($data,10,-8));
  47. }
  48. }
  49. /*
  50. * Action
  51. */
  52. if ($action == 'addprinter' && $user->admin)
  53. {
  54. $error=0;
  55. $db->begin();
  56. if (empty($printername)) {
  57. $error++;
  58. setEventMessages($langs->trans("PrinterNameEmpty"), null, 'errors');
  59. }
  60. if (empty($parameter)) {
  61. setEventMessages($langs->trans("PrinterParameterEmpty"), null, 'warnings');
  62. }
  63. if (! $error)
  64. {
  65. $result= $printer->AddPrinter($printername, GETPOST('printertypeid', 'int'), GETPOST('printerprofileid', 'int'), $parameter);
  66. if ($result > 0) $error++;
  67. if (! $error)
  68. {
  69. $db->commit();
  70. setEventMessages($langs->trans("PrinterAdded",$printername), null);
  71. }
  72. else
  73. {
  74. $db->rollback();
  75. dol_print_error($db);
  76. }
  77. }
  78. $action = '';
  79. }
  80. if ($action == 'deleteprinter' && $user->admin)
  81. {
  82. $error=0;
  83. $db->begin();
  84. if (empty($printerid)) {
  85. $error++;
  86. setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
  87. }
  88. if (! $error)
  89. {
  90. $result= $printer->DeletePrinter($printerid);
  91. if ($result > 0) $error++;
  92. if (! $error)
  93. {
  94. $db->commit();
  95. setEventMessages($langs->trans("PrinterDeleted",$printername), null);
  96. }
  97. else
  98. {
  99. $db->rollback();
  100. dol_print_error($db);
  101. }
  102. }
  103. $action = '';
  104. }
  105. if ($action == 'updateprinter' && $user->admin)
  106. {
  107. $error=0;
  108. $db->begin();
  109. if (empty($printerid)) {
  110. $error++;
  111. setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
  112. }
  113. if (! $error)
  114. {
  115. $result= $printer->UpdatePrinter($printername, GETPOST('printertypeid', 'int'), GETPOST('printerprofileid', 'int'), $parameter, $printerid);
  116. if ($result > 0) $error++;
  117. if (! $error)
  118. {
  119. $db->commit();
  120. setEventMessages($langs->trans("PrinterUpdated",$printername), null);
  121. }
  122. else
  123. {
  124. $db->rollback();
  125. dol_print_error($db);
  126. }
  127. }
  128. $action = '';
  129. }
  130. if ($action == 'testprinter' && $user->admin)
  131. {
  132. $error=0;
  133. if (empty($printerid)) {
  134. $error++;
  135. setEventMessages($langs->trans("PrinterIdEmpty"), null, 'errors');
  136. }
  137. if (! $error)
  138. {
  139. // test
  140. $ret = $printer->SendTestToPrinter($printerid);
  141. if ($ret == 0)
  142. {
  143. setEventMessages($langs->trans("TestSentToPrinter", $printername), null);
  144. }
  145. else
  146. {
  147. setEventMessages($printer->error, $printer->errors, 'errors');
  148. }
  149. }
  150. $action = '';
  151. }
  152. if ($action == 'updatetemplate' && $user->admin)
  153. {
  154. $error=0;
  155. $db->begin();
  156. if (empty($templateid)) {
  157. $error++;
  158. setEventMessages($langs->trans("TemplateIdEmpty"), null, 'errors');
  159. }
  160. if (! $error)
  161. {
  162. $result= $printer->UpdateTemplate($templatename, $template, $templateid);
  163. if ($result > 0) $error++;
  164. if (! $error)
  165. {
  166. $db->commit();
  167. setEventMessages($langs->trans("TemplateUpdated",$templatename), null);
  168. }
  169. else
  170. {
  171. $db->rollback();
  172. dol_print_error($db);
  173. }
  174. }
  175. $action = '';
  176. }
  177. /*
  178. * View
  179. */
  180. $form = new Form($db);
  181. llxHeader('',$langs->trans("ReceiptPrinterSetup"));
  182. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  183. print load_fiche_titre($langs->trans("ReceiptPrinterSetup"),$linkback,'title_setup');
  184. $head = receiptprinteradmin_prepare_head($mode);
  185. if ($mode == 'config' && $user->admin)
  186. {
  187. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=config" autocomplete="off">';
  188. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  189. if ($action!='editprinter') {
  190. print '<input type="hidden" name="action" value="addprinter">';
  191. } else {
  192. print '<input type="hidden" name="action" value="updateprinter">';
  193. }
  194. dol_fiche_head($head, $mode, $langs->trans("ModuleSetup"), 0, 'technic');
  195. print $langs->trans("ReceiptPrinterDesc")."<br><br>\n";
  196. print '<table class="noborder" width="100%">'."\n";
  197. $var=true;
  198. print '<tr class="liste_titre">';
  199. print '<th>'.$langs->trans("Name").'</th>';
  200. print '<th>'.$langs->trans("Type").'</th>';
  201. print '<th>'.$langs->trans("Profile").'</th>';
  202. print '<th>'.$langs->trans("Parameters").'</th>';
  203. print '<th></th>';
  204. print '<th></th>';
  205. print '<th></th>';
  206. print "</tr>\n";
  207. $ret = $printer->listprinters();
  208. $nbofprinters = count($printer->listprinters);
  209. if ($ret > 0) {
  210. setEventMessages($printer->error, $printer->errors, 'errors');
  211. } else {
  212. for ($line=0; $line < $nbofprinters; $line++) {
  213. $var = !$var;
  214. print '<tr class="oddeven">';
  215. if ($action=='editprinter' && $printer->listprinters[$line]['rowid']==$printerid) {
  216. print '<input type="hidden" name="printerid" value="'.$printer->listprinters[$line]['rowid'].'">';
  217. print '<td><input size="50" type="text" name="printername" value="'.$printer->listprinters[$line]['name'].'"></td>';
  218. $ret = $printer->selectTypePrinter($printer->listprinters[$line]['fk_type']);
  219. print '<td>'.$printer->resprint.'</td>';
  220. $ret = $printer->selectProfilePrinter($printer->listprinters[$line]['fk_profile']);
  221. print '<td>'.$printer->profileresprint.'</td>';
  222. print '<td><input size="60" type="text" name="parameter" value="'.$printer->listprinters[$line]['parameter'].'"></td>';
  223. print '<td></td>';
  224. print '<td></td>';
  225. print '<td></td>';
  226. print '</tr>';
  227. } else {
  228. print '<td>'.$printer->listprinters[$line]['name'].'</td>';
  229. print '<td>'.$langs->trans($printer->listprinters[$line]['fk_type_name']).'</td>';
  230. print '<td>'.$langs->trans($printer->listprinters[$line]['fk_profile_name']).'</td>';
  231. print '<td>'.$printer->listprinters[$line]['parameter'].'</td>';
  232. // edit icon
  233. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=editprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'">';
  234. print img_picto($langs->trans("Edit"),'edit');
  235. print '</a></td>';
  236. // delete icon
  237. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=deleteprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'&amp;printername='.$printer->listprinters[$line]['name'].'">';
  238. print img_picto($langs->trans("Delete"),'delete');
  239. print '</a></td>';
  240. // test icon
  241. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=config&amp;action=testprinter&amp;printerid='.$printer->listprinters[$line]['rowid'].'&amp;printername='.$printer->listprinters[$line]['name'].'">';
  242. print img_picto($langs->trans("TestPrinter"),'printer');
  243. print '</a></td>';
  244. print '</tr>';
  245. }
  246. }
  247. }
  248. if ($action!='editprinter')
  249. {
  250. if ($nbofprinters > 0)
  251. {
  252. print '<tr class="liste_titre">';
  253. print '<th>'.$langs->trans("Name").'</th>';
  254. print '<th>'.$langs->trans("Type").'</th>';
  255. print '<th>'.$langs->trans("Profile").'</th>';
  256. print '<th>'.$langs->trans("Parameters").'</th>';
  257. print '<th></th>';
  258. print '<th></th>';
  259. print '<th></th>';
  260. print "</tr>\n";
  261. }
  262. print '<tr>';
  263. print '<td><input size="50" type="text" name="printername"></td>';
  264. $ret = $printer->selectTypePrinter();
  265. print '<td>'.$printer->resprint.'</td>';
  266. $ret = $printer->selectProfilePrinter();
  267. print '<td>'.$printer->profileresprint.'</td>';
  268. print '<td><input size="60" type="text" name="parameter"></td>';
  269. print '<td></td>';
  270. print '<td></td>';
  271. print '<td></td>';
  272. print '</tr>';
  273. }
  274. print '</table>';
  275. dol_fiche_end();
  276. if ($action!='editprinter') {
  277. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
  278. } else {
  279. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
  280. }
  281. print '</form>';
  282. print '<div><p></div>';
  283. dol_fiche_head();
  284. print $langs->trans("ReceiptPrinterTypeDesc")."<br><br>\n";
  285. print '<table class="noborder" width="100%">'."\n";
  286. print '<tr '.$bc[1].'><td>'.$langs->trans("CONNECTOR_DUMMY").':</td><td>'.$langs->trans("CONNECTOR_DUMMY_HELP").'</td></tr>';
  287. print '<tr '.$bc[0].'><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT").':</td><td>'.$langs->trans("CONNECTOR_NETWORK_PRINT_HELP").'</td></tr>';
  288. print '<tr '.$bc[1].'><td>'.$langs->trans("CONNECTOR_FILE_PRINT").':</td><td>'.$langs->trans("CONNECTOR_FILE_PRINT_HELP").'</td></tr>';
  289. print '<tr '.$bc[0].'><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT").':</td><td>'.$langs->trans("CONNECTOR_WINDOWS_PRINT_HELP").'</td></tr>';
  290. //print '<tr '.$bc[1].'><td>'.$langs->trans("CONNECTOR_JAVA").':</td><td>'.$langs->trans("CONNECTOR_JAVA_HELP").'</td></tr>';
  291. print '</table>';
  292. dol_fiche_end();
  293. print '<div><p></div>';
  294. dol_fiche_head();
  295. print $langs->trans("ReceiptPrinterProfileDesc")."<br><br>\n";
  296. print '<table class="noborder" width="100%">'."\n";
  297. print '<tr '.$bc[1].'><td>'.$langs->trans("PROFILE_DEFAULT").':</td><td>'.$langs->trans("PROFILE_DEFAULT_HELP").'</td></tr>';
  298. print '<tr '.$bc[0].'><td>'.$langs->trans("PROFILE_SIMPLE").':</td><td>'.$langs->trans("PROFILE_SIMPLE_HELP").'</td></tr>';
  299. print '<tr '.$bc[1].'><td>'.$langs->trans("PROFILE_EPOSTEP").':</td><td>'.$langs->trans("PROFILE_EPOSTEP_HELP").'</td></tr>';
  300. print '<tr '.$bc[0].'><td>'.$langs->trans("PROFILE_P822D").':</td><td>'.$langs->trans("PROFILE_P822D_HELP").'</td></tr>';
  301. print '<tr '.$bc[1].'><td>'.$langs->trans("PROFILE_STAR").':</td><td>'.$langs->trans("PROFILE_STAR_HELP").'</td></tr>';
  302. print '</table>';
  303. dol_fiche_end();
  304. }
  305. if ($mode == 'template' && $user->admin)
  306. {
  307. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=template" autocomplete="off">';
  308. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  309. if ($action!='edittemplate') {
  310. print '<input type="hidden" name="action" value="addtemplate">';
  311. } else {
  312. print '<input type="hidden" name="action" value="updatetemplate">';
  313. }
  314. dol_fiche_head($head, $mode, $langs->trans("ModuleSetup"), 0, 'technic');
  315. print $langs->trans("ReceiptPrinterTemplateDesc")."<br><br>\n";
  316. print '<table class="noborder" width="100%">'."\n";
  317. $var=true;
  318. print '<tr class="liste_titre">';
  319. print '<th>'.$langs->trans("Name").'</th>';
  320. print '<th>'.$langs->trans("Template").'</th>';
  321. print '<th></th>';
  322. print '<th></th>';
  323. print '<th></th>';
  324. print "</tr>\n";
  325. $ret = $printer->listPrintersTemplates();
  326. //print '<pre>'.print_r($printer->listprinterstemplates, true).'</pre>';
  327. if ($ret > 0) {
  328. setEventMessages($printer->error, $printer->errors, 'errors');
  329. } else {
  330. $max = count($printer->listprinterstemplates);
  331. for ($line=0; $line < $max; $line++) {
  332. $var = !$var;
  333. print '<tr class="oddeven">';
  334. if ($action=='edittemplate' && $printer->listprinterstemplates[$line]['rowid']==$templateid) {
  335. print '<input type="hidden" name="templateid" value="'.$printer->listprinterstemplates[$line]['rowid'].'">';
  336. print '<td><input size="50" type="text" name="templatename" value="'.$printer->listprinterstemplates[$line]['name'].'"></td>';
  337. print '<td><textarea name="template" wrap="soft" cols="120" rows="12">'.$printer->listprinterstemplates[$line]['template'].'</textarea>';
  338. print '</td>';
  339. print '<td></td>';
  340. print '<td></td>';
  341. print '<td></td>';
  342. } else {
  343. print '<td>'.$printer->listprinterstemplates[$line]['name'].'</td>';
  344. print '<td>'.nl2br(htmlentities($printer->listprinterstemplates[$line]['template'])).'</td>';
  345. // edit icon
  346. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=edittemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'">';
  347. print img_picto($langs->trans("Edit"),'edit');
  348. print '</a></td>';
  349. // delete icon
  350. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=deletetemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'&amp;templatename='.$printer->listprinterstemplates[$line]['name'].'">';
  351. print img_picto($langs->trans("Delete"),'delete');
  352. print '</a></td>';
  353. // test icon
  354. print '<td><a href="'.$_SERVER['PHP_SELF'].'?mode=template&amp;action=testtemplate&amp;templateid='.$printer->listprinterstemplates[$line]['rowid'].'&amp;templatename='.$printer->listprinterstemplates[$line]['name'].'">';
  355. print img_picto($langs->trans("TestPrinterTemplate"),'printer');
  356. print '</a></td>';
  357. }
  358. print '</tr>';
  359. }
  360. }
  361. print '</table>';
  362. if ($action!='edittemplate') {
  363. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Add")).'"></div>';
  364. } else {
  365. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
  366. }
  367. print '</form>';
  368. print '<div><p></div>';
  369. print '<table class="noborder" width="100%">'."\n";
  370. $var=true;
  371. print '<tr class="liste_titre">';
  372. print '<th>'.$langs->trans("Tag").'</th>';
  373. print '<th>'.$langs->trans("Description").'</th>';
  374. print "</tr>\n";
  375. $max = count($printer->tags);
  376. for ($tag=0; $tag < $max; $tag++) {
  377. $var = !$var;
  378. print '<tr class="oddeven">';
  379. print '<td>&lt;'.$printer->tags[$tag].'&gt;</td><td>'.$langs->trans(strtoupper($printer->tags[$tag])).'</td>';
  380. print '</tr>';
  381. }
  382. print '</table>';
  383. dol_fiche_end();
  384. }
  385. // to remove after test
  386. $object=new stdClass();
  387. $object->date_time = '2015-11-02 22:30:25';
  388. $object->id = 1234;
  389. $object->customer_firstname = 'John';
  390. $object->customer_lastname = 'Deuf';
  391. $object->vendor_firstname = 'Jim';
  392. $object->vendor_lastname = 'Big';
  393. $object->barcode = '3700123862396';
  394. //$printer->sendToPrinter($object, 1, 16);
  395. //setEventMessages($printer->error, $printer->errors, 'errors');
  396. llxFooter();
  397. $db->close();