dolreceiptprinter.class.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. <?php
  2. /* Copyright (C) 2015-2021 Frédéric France <frederic.france@netlogic.fr>
  3. * Copyright (C) 2020 Andreu Bisquerra <jove@bisquerra.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/class/dolreceiptprinter.class.php
  21. * \brief Print receipt ticket on various ESC/POS printer
  22. */
  23. /*
  24. * Tags for ticket template
  25. *
  26. * {dol_align_left} Left align text
  27. * {dol_align_center} Center text
  28. * {dol_align_right} Right align text
  29. * {dol_use_font_a} Use font A of printer
  30. * {dol_use_font_b} Use font B of printer
  31. * {dol_use_font_c} Use font C of printer
  32. * {dol_bold} Text Bold
  33. * {dol_bold_disabled} Disable Text Bold
  34. * {dol_double_height} Text double height
  35. * {dol_double_width} Text double width
  36. * {dol_default_height_width} Text default height and width
  37. * {dol_underline} Underline text
  38. * {dol_underline_disabled} Disable underline text
  39. * {dol_cut_paper_full} Cut ticket completely
  40. * {dol_cut_paper_partial} Cut ticket partially
  41. * {dol_open_drawer} Open cash drawer
  42. * {dol_beep} Activate buzzer
  43. * {dol_beep_alternative} Activate buzzer (alternative mode)
  44. * {dol_print_barcode} Print barcode
  45. * {dol_print_logo} Print logo stored on printer. Example : <print_logo>32|32
  46. * {dol_print_logo_old} Print logo stored on printer. Must be followed by logo code. For old printers.
  47. * {dol_print_logo_old_cf} Print logo stored on printer. Must be followed by logo code. For old printers. May help for centering image.
  48. * {dol_print_object_lines} Print object lines
  49. * {dol_print_object_tax} Print object total tax
  50. * {dol_print_object_local_tax} Print object local tax
  51. * {dol_print_object_total} Print object total
  52. * {dol_print_order_lines} Print order lines for Printer
  53. * {dol_print_object_lines_with_notes} Print object lines with notes
  54. * {dol_print_payment} Print payment method
  55. * {dol_print_curr_date} Print the current date/time. Must be followed by format string.
  56. *
  57. * Code which can be placed everywhere
  58. * <dol_value_date> Replaced by date AAAA-MM-DD
  59. * <dol_value_date_time> Replaced by date and time AAAA-MM-DD HH:MM:SS
  60. * <dol_value_year> Replaced by Year
  61. * <dol_value_month_letters> Replaced by month in letters (example : november)
  62. * <dol_value_month> Replaced by month number
  63. * <dol_value_day> Replaced by day number
  64. * <dol_value_day_letters> Replaced by day number
  65. * <dol_value_currentdate> Replaced by current date and time
  66. * <dol_value_currentdate_notime> Replaced by current date without time
  67. * <dol_object_id> Replaced by object id
  68. * <dol_object_ref> Replaced by object ref
  69. * <dol_value_customer_firstname> Replaced by customer firstname
  70. * <dol_value_customer_lastname> Replaced by customer name
  71. * <dol_value_customer_mail> Replaced by customer mail
  72. * <dol_value_customer_phone> Replaced by customer phone
  73. * <dol_value_customer_mobile> Replaced by customer mobile
  74. * <dol_value_customer_skype> Replaced by customer skype
  75. * <dol_value_customer_tax_number> Replaced by customer VAT number
  76. * <dol_value_customer_account_balance> Replaced by customer account balance
  77. * <dol_value_mysoc_name> Replaced by mysoc name
  78. * <dol_value_mysoc_address> Replaced by mysoc address
  79. * <dol_value_mysoc_zip> Replaced by mysoc zip
  80. * <dol_value_mysoc_town> Replaced by mysoc town
  81. * <dol_value_mysoc_country> Replaced by mysoc country
  82. * <dol_value_mysoc_idprof1> Replaced by mysoc idprof1
  83. * <dol_value_mysoc_idprof2> Replaced by mysoc idprof2
  84. * <dol_value_mysoc_idprof3> Replaced by mysoc idprof3
  85. * <dol_value_mysoc_idprof4> Replaced by mysoc idprof4
  86. * <dol_value_mysoc_idprof5> Replaced by mysoc idprof5
  87. * <dol_value_mysoc_idprof6> Replaced by mysoc idprof6
  88. * <dol_value_vendor_lastname> Replaced by vendor name
  89. * <dol_value_vendor_firstname> Replaced by vendor firstname
  90. * <dol_value_vendor_mail> Replaced by vendor mail
  91. * <dol_value_customer_points> Replaced by customer points
  92. * <dol_value_object_points> Replaced by number of points for this object
  93. *
  94. * Conditional code at line start (if then Print)
  95. * <dol_print_if_customer> Print the line IF a customer is affected to the object
  96. * <dol_print_if_vendor> Print the line IF a vendor is affected to the object
  97. * <dol_print_if_happy_hour> Print the line IF Happy Hour
  98. * <dol_print_if_num_object_unique> Print the line IF object is validated
  99. * <dol_print_if_customer_points> Print the line IF customer points > 0
  100. * <dol_print_if_object_points> Print the line IF points of the object > 0
  101. * <dol_print_if_customer_tax_number> Print the line IF customer has vat number
  102. * <dol_print_if_customer_account_balance_positive> Print the line IF customer balance > 0
  103. *
  104. */
  105. require_once DOL_DOCUMENT_ROOT.'/includes/mike42/escpos-php/autoload.php';
  106. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  107. use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
  108. use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
  109. use Mike42\Escpos\PrintConnectors\CupsPrintConnector;
  110. use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
  111. use Mike42\Escpos\CapabilityProfile;
  112. use Mike42\Escpos\Printer;
  113. use Mike42\Escpos\EscposImage;
  114. /**
  115. * Class to manage Receipt Printers
  116. */
  117. class dolReceiptPrinter extends Printer
  118. {
  119. const CONNECTOR_DUMMY = 1;
  120. const CONNECTOR_FILE_PRINT = 2;
  121. const CONNECTOR_NETWORK_PRINT = 3;
  122. const CONNECTOR_WINDOWS_PRINT = 4;
  123. const CONNECTOR_CUPS_PRINT = 5;
  124. /**
  125. * @var DoliDB Database handler.
  126. */
  127. public $db;
  128. /*
  129. * @var string[] array of tags
  130. */
  131. public $tags;
  132. public $printer;
  133. public $template;
  134. /**
  135. * Number of order printer
  136. * @var int
  137. */
  138. public $orderprinter;
  139. /**
  140. * @var string Error code (or message)
  141. */
  142. public $error = '';
  143. /**
  144. * @var string[] Error codes (or messages)
  145. */
  146. public $errors = array();
  147. /**
  148. * Constructor
  149. *
  150. * @param DoliDB $db database
  151. */
  152. public function __construct($db)
  153. {
  154. $this->db = $db;
  155. $this->tags = array(
  156. 'dol_line_feed' => 'DOL_LINE_FEED',
  157. 'dol_line_feed_reverse' => 'DOL_LINE_FEED_REVERSE',
  158. 'dol_align_left' => 'DOL_ALIGN_LEFT',
  159. 'dol_align_center' => 'DOL_ALIGN_CENTER',
  160. 'dol_align_right' => 'DOL_ALIGN_RIGHT',
  161. 'dol_use_font_a' => 'DOL_USE_FONT_A',
  162. 'dol_use_font_b' => 'DOL_USE_FONT_B',
  163. 'dol_use_font_c' => 'DOL_USE_FONT_C',
  164. 'dol_bold' => 'DOL_BOLD',
  165. 'dol_bold_disabled' => 'DOL_BOLD_DISABLED',
  166. 'dol_double_height' => 'DOL_DOUBLE_HEIGHT',
  167. 'dol_double_width' => 'DOL_DOUBLE_WIDTH',
  168. 'dol_default_height_width' => 'DOL_DEFAULT_HEIGHT_WIDTH',
  169. 'dol_underline' => 'DOL_UNDERLINE',
  170. 'dol_underline_disabled' => 'DOL_UNDERLINE_DISABLED',
  171. 'dol_cut_paper_full' => 'DOL_CUT_PAPER_FULL',
  172. 'dol_cut_paper_partial' => 'DOL_CUT_PAPER_PARTIAL',
  173. 'dol_open_drawer' => 'DOL_OPEN_DRAWER',
  174. 'dol_beep' => 'DOL_BEEP',
  175. 'dol_beep_alternative' => 'DOL_BEEP_ALTERNATIVE',
  176. 'dol_print_text' => 'DOL_PRINT_TEXT',
  177. 'dol_print_barcode' => 'DOL_PRINT_BARCODE',
  178. 'dol_value_date' => 'DateInvoice',
  179. 'dol_value_date_time' => 'DateInvoiceWithTime',
  180. 'dol_value_year' => 'YearInvoice',
  181. 'dol_value_month_letters' => 'DOL_VALUE_MONTH_LETTERS',
  182. 'dol_value_month' => 'DOL_VALUE_MONTH',
  183. 'dol_value_day' => 'DOL_VALUE_DAY',
  184. 'dol_value_day_letters' => 'DOL_VALUE_DAY',
  185. 'dol_value_currentdate' => 'DOL_VALUE_CURRENTDATE',
  186. 'dol_value_currentdate_notime' => 'CurrentDateWithTime',
  187. 'dol_value_currentdate_letters' => 'DOL_VALUE_CURRENTDATE_LETTERS',
  188. 'dol_value_currentyear' => 'CurrentYear',
  189. 'dol_value_currentmonth_letters' => 'DOL_VALUE_CURRENT_MONTH_LETTERS',
  190. 'dol_value_currentmonth' => 'DOL_VALUE_CURRENT_MONTH',
  191. 'dol_value_currentday' => 'DOL_VALUE_CURRENT_DAY',
  192. 'dol_value_currentday_letters' => 'DOL_VALUE_CURRENT_DAY',
  193. 'dol_print_payment' => 'DOL_PRINT_PAYMENT',
  194. 'dol_print_curr_date' => 'DOL_PRINT_CURR_DATE',
  195. 'dol_print_logo' => 'DOL_PRINT_LOGO',
  196. 'dol_print_logo_old' => 'DOL_PRINT_LOGO_OLD',
  197. 'dol_print_logo_old_cf' => 'DOL_PRINT_LOGO_OLD_CF',
  198. 'dol_value_object_id' => 'InvoiceID',
  199. 'dol_value_object_ref' => 'InvoiceRef',
  200. 'dol_print_object_lines' => 'DOL_PRINT_OBJECT_LINES',
  201. 'dol_print_object_lines_with_notes' => 'DOL_PRINT_OBJECT_LINES_WITH_NOTES',
  202. 'dol_print_object_tax' => 'TotalVAT',
  203. 'dol_print_object_local_tax1' => 'TotalLT1',
  204. 'dol_print_object_local_tax2' => 'TotalLT2',
  205. 'dol_print_object_total' => 'Total',
  206. 'dol_print_object_number' => 'DOL_PRINT_OBJECT_NUMBER',
  207. //'dol_value_object_points' => 'DOL_VALUE_OBJECT_POINTS',
  208. 'dol_print_order_lines' => 'DOL_PRINT_ORDER_LINES',
  209. 'dol_value_customer_firstname' => 'DOL_VALUE_CUSTOMER_FIRSTNAME',
  210. 'dol_value_customer_lastname' => 'DOL_VALUE_CUSTOMER_LASTNAME',
  211. 'dol_value_customer_mail' => 'DOL_VALUE_CUSTOMER_MAIL',
  212. 'dol_value_customer_phone' => 'DOL_VALUE_CUSTOMER_PHONE',
  213. 'dol_value_customer_skype' => 'DOL_VALUE_CUSTOMER_SKYPE',
  214. 'dol_value_customer_tax_number' => 'DOL_VALUE_CUSTOMER_TAX_NUMBER',
  215. //'dol_value_customer_account_balance' => 'DOL_VALUE_CUSTOMER_ACCOUNT_BALANCE',
  216. //'dol_value_customer_points' => 'DOL_VALUE_CUSTOMER_POINTS',
  217. 'dol_value_mysoc_name' => 'DOL_VALUE_MYSOC_NAME',
  218. 'dol_value_mysoc_address' => 'Address',
  219. 'dol_value_mysoc_zip' => 'Zip',
  220. 'dol_value_mysoc_town' => 'Town',
  221. 'dol_value_mysoc_country' => 'Country',
  222. 'dol_value_mysoc_idprof1' => 'ProfId1',
  223. 'dol_value_mysoc_idprof2' => 'ProfId2',
  224. 'dol_value_mysoc_idprof3' => 'ProfId3',
  225. 'dol_value_mysoc_idprof4' => 'ProfId4',
  226. 'dol_value_mysoc_idprof5' => 'ProfId5',
  227. 'dol_value_mysoc_idprof6' => 'ProfId6',
  228. 'dol_value_mysoc_tva_intra' => 'VATIntra',
  229. 'dol_value_mysoc_capital' => 'Capital',
  230. 'dol_value_mysoc_url' => 'Web',
  231. 'dol_value_vendor_lastname' => 'VendorLastname',
  232. 'dol_value_vendor_firstname' => 'VendorFirstname',
  233. 'dol_value_vendor_mail' => 'VendorEmail',
  234. 'dol_value_place' => 'DOL_VALUE_PLACE',
  235. );
  236. }
  237. /**
  238. * list printers
  239. *
  240. * @return int 0 if OK; >0 if KO
  241. */
  242. public function listPrinters()
  243. {
  244. global $conf;
  245. $error = 0;
  246. $line = 0;
  247. $obj = array();
  248. $sql = "SELECT rowid, name, fk_type, fk_profile, parameter";
  249. $sql .= " FROM ".$this->db->prefix()."printer_receipt";
  250. $sql .= " WHERE entity = ".$conf->entity;
  251. $resql = $this->db->query($sql);
  252. if ($resql) {
  253. $num = $this->db->num_rows($resql);
  254. while ($line < $num) {
  255. $row = $this->db->fetch_array($resql);
  256. switch ($row['fk_type']) {
  257. case 1:
  258. $row['fk_type_name'] = 'CONNECTOR_DUMMY';
  259. break;
  260. case 2:
  261. $row['fk_type_name'] = 'CONNECTOR_FILE_PRINT';
  262. break;
  263. case 3:
  264. $row['fk_type_name'] = 'CONNECTOR_NETWORK_PRINT';
  265. break;
  266. case 4:
  267. $row['fk_type_name'] = 'CONNECTOR_WINDOWS_PRINT';
  268. break;
  269. case 5:
  270. $row['fk_type_name'] = 'CONNECTOR_CUPS_PRINT';
  271. break;
  272. default:
  273. $row['fk_type_name'] = 'CONNECTOR_UNKNOWN';
  274. break;
  275. }
  276. switch ($row['fk_profile']) {
  277. case 0:
  278. $row['fk_profile_name'] = 'PROFILE_DEFAULT';
  279. break;
  280. case 1:
  281. $row['fk_profile_name'] = 'PROFILE_SIMPLE';
  282. break;
  283. case 2:
  284. $row['fk_profile_name'] = 'PROFILE_EPOSTEP';
  285. break;
  286. case 3:
  287. $row['fk_profile_name'] = 'PROFILE_P822D';
  288. break;
  289. default:
  290. $row['fk_profile_name'] = 'PROFILE_STAR';
  291. break;
  292. }
  293. $obj[] = $row;
  294. $line++;
  295. }
  296. } else {
  297. $error++;
  298. $this->errors[] = $this->db->lasterror;
  299. }
  300. $this->listprinters = $obj;
  301. return $error;
  302. }
  303. /**
  304. * List printers templates
  305. *
  306. * @return int 0 if OK; >0 if KO
  307. */
  308. public function listPrintersTemplates()
  309. {
  310. global $conf;
  311. $error = 0;
  312. $line = 0;
  313. $obj = array();
  314. $sql = "SELECT rowid, name, template";
  315. $sql .= " FROM ".$this->db->prefix()."printer_receipt_template";
  316. $sql .= " WHERE entity = ".$conf->entity;
  317. $resql = $this->db->query($sql);
  318. if ($resql) {
  319. $num = $this->db->num_rows($resql);
  320. while ($line < $num) {
  321. $obj[] = $this->db->fetch_array($resql);
  322. $line++;
  323. }
  324. } else {
  325. $error++;
  326. $this->errors[] = $this->db->lasterror;
  327. }
  328. $this->listprinterstemplates = $obj;
  329. return $error;
  330. }
  331. /**
  332. * Form to Select type printer
  333. *
  334. * @param string $selected Id printer type pre-selected
  335. * @param string $htmlname select html name
  336. * @return int 0 if OK; >0 if KO
  337. */
  338. public function selectTypePrinter($selected = '', $htmlname = 'printertypeid')
  339. {
  340. global $langs;
  341. $options = array(
  342. 1 => $langs->trans('CONNECTOR_DUMMY'),
  343. 2 => $langs->trans('CONNECTOR_FILE_PRINT'),
  344. 3 => $langs->trans('CONNECTOR_NETWORK_PRINT'),
  345. 4 => $langs->trans('CONNECTOR_WINDOWS_PRINT'),
  346. 5 => $langs->trans('CONNECTOR_CUPS_PRINT'),
  347. );
  348. $this->resprint = Form::selectarray($htmlname, $options, $selected);
  349. return 0;
  350. }
  351. /**
  352. * Form to Select Profile printer
  353. *
  354. * @param string $selected Id printer profile pre-selected
  355. * @param string $htmlname select html name
  356. * @return int 0 if OK; >0 if KO
  357. */
  358. public function selectProfilePrinter($selected = '', $htmlname = 'printerprofileid')
  359. {
  360. global $langs;
  361. $options = array(
  362. 0 => $langs->trans('PROFILE_DEFAULT'),
  363. 1 => $langs->trans('PROFILE_SIMPLE'),
  364. 2 => $langs->trans('PROFILE_EPOSTEP'),
  365. 3 => $langs->trans('PROFILE_P822D'),
  366. 4 => $langs->trans('PROFILE_STAR'),
  367. );
  368. $this->profileresprint = Form::selectarray($htmlname, $options, $selected);
  369. return 0;
  370. }
  371. /**
  372. * Function to Add a printer in db
  373. *
  374. * @param string $name Printer name
  375. * @param int $type Printer type
  376. * @param int $profile Printer profile
  377. * @param string $parameter Printer parameter
  378. * @return int 0 if OK; >0 if KO
  379. */
  380. public function addPrinter($name, $type, $profile, $parameter)
  381. {
  382. global $conf;
  383. $error = 0;
  384. $sql = "INSERT INTO ".$this->db->prefix()."printer_receipt";
  385. $sql .= " (name, fk_type, fk_profile, parameter, entity)";
  386. $sql .= " VALUES ('".$this->db->escape($name)."', ".((int) $type).", ".((int) $profile).", '".$this->db->escape($parameter)."', ".((int) $conf->entity).")";
  387. $resql = $this->db->query($sql);
  388. if (!$resql) {
  389. $error++;
  390. $this->errors[] = $this->db->lasterror;
  391. }
  392. return $error;
  393. }
  394. /**
  395. * Function to Update a printer in db
  396. *
  397. * @param string $name Printer name
  398. * @param int $type Printer type
  399. * @param int $profile Printer profile
  400. * @param string $parameter Printer parameter
  401. * @param int $printerid Printer id
  402. * @return int 0 if OK; >0 if KO
  403. */
  404. public function updatePrinter($name, $type, $profile, $parameter, $printerid)
  405. {
  406. global $conf;
  407. $error = 0;
  408. $sql = "UPDATE ".$this->db->prefix()."printer_receipt";
  409. $sql .= " SET name='".$this->db->escape($name)."'";
  410. $sql .= ", fk_type=".((int) $type);
  411. $sql .= ", fk_profile=".((int) $profile);
  412. $sql .= ", parameter='".$this->db->escape($parameter)."'";
  413. $sql .= " WHERE rowid=".((int) $printerid);
  414. $resql = $this->db->query($sql);
  415. if (!$resql) {
  416. $error++;
  417. $this->errors[] = $this->db->lasterror;
  418. }
  419. return $error;
  420. }
  421. /**
  422. * Function to Delete a printer from db
  423. *
  424. * @param int $printerid Printer id
  425. * @return int 0 if OK; >0 if KO
  426. */
  427. public function deletePrinter($printerid)
  428. {
  429. global $conf;
  430. $error = 0;
  431. $sql = 'DELETE FROM '.$this->db->prefix().'printer_receipt';
  432. $sql .= ' WHERE rowid='.((int) $printerid);
  433. $resql = $this->db->query($sql);
  434. if (!$resql) {
  435. $error++;
  436. $this->errors[] = $this->db->lasterror;
  437. }
  438. return $error;
  439. }
  440. /**
  441. * Function to add a printer template in db
  442. *
  443. * @param string $name Template name
  444. * @param int $template Template
  445. * @return int 0 if OK; >0 if KO
  446. */
  447. public function addTemplate($name, $template)
  448. {
  449. global $conf;
  450. $error = 0;
  451. $sql = "INSERT INTO ".$this->db->prefix()."printer_receipt_template";
  452. $sql .= " (name, template, entity) VALUES ('".$this->db->escape($name)."'";
  453. $sql .= ", '".$this->db->escape($template)."', ".$conf->entity.")";
  454. $resql = $this->db->query($sql);
  455. if (!$resql) {
  456. $error++;
  457. $this->errors[] = $this->db->lasterror;
  458. }
  459. return $error;
  460. }
  461. /**
  462. * Function to delete a printer template in db
  463. *
  464. * @param int $templateid Template ID
  465. * @return int 0 if OK; >0 if KO
  466. */
  467. public function deleteTemplate($templateid)
  468. {
  469. global $conf;
  470. $error = 0;
  471. $sql = 'DELETE FROM '.$this->db->prefix().'printer_receipt_template';
  472. $sql .= " WHERE rowid = ".((int) $templateid);
  473. $sql .= " AND entity = ".$conf->entity;
  474. $resql = $this->db->query($sql);
  475. if (!$resql) {
  476. $error++;
  477. $this->errors[] = $this->db->lasterror;
  478. }
  479. return $error;
  480. }
  481. /**
  482. * Function to Update a printer template in db
  483. *
  484. * @param string $name Template name
  485. * @param int $template Template
  486. * @param int $templateid Template id
  487. * @return int 0 if OK; >0 if KO
  488. */
  489. public function updateTemplate($name, $template, $templateid)
  490. {
  491. global $conf;
  492. $error = 0;
  493. $sql = "UPDATE ".$this->db->prefix()."printer_receipt_template";
  494. $sql .= " SET name='".$this->db->escape($name)."'";
  495. $sql .= ", template='".$this->db->escape($template)."'";
  496. $sql .= " WHERE rowid=".((int) $templateid);
  497. $resql = $this->db->query($sql);
  498. if (!$resql) {
  499. $error++;
  500. $this->errors[] = $this->db->lasterror;
  501. }
  502. return $error;
  503. }
  504. /**
  505. * Function to Send Test page to Printer
  506. *
  507. * @param int $printerid Printer id
  508. * @return int 0 if OK; >0 if KO
  509. */
  510. public function sendTestToPrinter($printerid)
  511. {
  512. global $conf;
  513. $error = 0;
  514. $img = EscposImage::load(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo_bw.png');
  515. //$this->profile = CapabilityProfile::load("TM-T88IV");
  516. $ret = $this->initPrinter($printerid);
  517. if ($ret > 0) {
  518. setEventMessages($this->error, $this->errors, 'errors');
  519. } else {
  520. try {
  521. $this->printer->bitImage($img);
  522. $this->printer->text("Hello World!\n");
  523. $testStr = "1234567890";
  524. $this->printer->barcode($testStr);
  525. //$this->printer->qrcode($testStr, Printer::QR_ECLEVEL_M, 5, Printer::QR_MODEL_1);
  526. $this->printer->text("Most simple example\n");
  527. $this->printer->feed();
  528. $this->printer->cut();
  529. // If is DummyPrintConnector send to log to debugging
  530. if ($this->printer->connector instanceof DummyPrintConnector) {
  531. $data = $this->printer->connector->getData();
  532. dol_syslog($data);
  533. }
  534. $this->printer->close();
  535. } catch (Exception $e) {
  536. $this->errors[] = $e->getMessage();
  537. $error++;
  538. }
  539. }
  540. return $error;
  541. }
  542. /**
  543. * Function to Print Receipt Ticket
  544. *
  545. * @param Facture|Commande $object Order or invoice object
  546. * @param int $templateid Template id
  547. * @param int $printerid Printer id
  548. * @return int 0 if OK; >0 if KO
  549. */
  550. public function sendToPrinter($object, $templateid, $printerid)
  551. {
  552. global $conf, $mysoc, $langs, $user;
  553. $langs->load('bills');
  554. $error = 0;
  555. $ret = $this->loadTemplate($templateid);
  556. $now = dol_now('tzuser');
  557. // tags a remplacer par leur valeur avant de parser (dol_value_xxx)
  558. $this->template = str_replace('{dol_value_object_id}', $object->id, $this->template);
  559. $this->template = str_replace('{dol_value_object_ref}', $object->ref, $this->template);
  560. //$this->template = str_replace('<dol_value_object_points>', $object->points, $this->template);
  561. $this->template = str_replace('{dol_value_date}', dol_print_date($object->date, 'day'), $this->template);
  562. $this->template = str_replace('{dol_value_date_time}', dol_print_date($object->date, 'dayhour'), $this->template);
  563. $this->template = str_replace('{dol_value_year}', dol_print_date($object->date, '%Y'), $this->template);
  564. $this->template = str_replace('{dol_value_month_letters}', $langs->trans("Month".dol_print_date($object->date, '%m')), $this->template);
  565. $this->template = str_replace('{dol_value_month}', dol_print_date($object->date, '%m'), $this->template);
  566. $this->template = str_replace('{dol_value_day}', dol_print_date($object->date, '%d'), $this->template);
  567. $this->template = str_replace('{dol_value_day_letters}', $langs->trans("Day".dol_print_date($object->date, '%m')[1]), $this->template);
  568. $this->template = str_replace('{dol_value_currentdate}', dol_print_date($now, 'dayhour'), $this->template);
  569. $this->template = str_replace('{dol_value_currentdate_notime}', dol_print_date($now, 'day'), $this->template);
  570. $this->template = str_replace('{dol_value_currentdate_letters}', dol_print_date($now, 'dayhourtext'), $this->template);
  571. $this->template = str_replace('{dol_value_currentyear}', dol_print_date($now, '%Y'), $this->template);
  572. $this->template = str_replace('{dol_value_currentmonth_letters}', $langs->trans("Month".dol_print_date($now, '%m')), $this->template);
  573. $this->template = str_replace('{dol_value_currentmonth}', dol_print_date($now, '%m'), $this->template);
  574. $this->template = str_replace('{dol_value_currentday}', dol_print_date($now, '%d'), $this->template);
  575. $this->template = str_replace('{dol_value_currentday_letters}', $langs->trans("Day".dol_print_date($now, '%m')[1]), $this->template);
  576. $this->template = str_replace('{dol_value_customer_firstname}', $object->thirdparty->firstname, $this->template);
  577. $this->template = str_replace('{dol_value_customer_lastname}', $object->thirdparty->lastname, $this->template);
  578. $this->template = str_replace('{dol_value_customer_mail}', $object->thirdparty->email, $this->template);
  579. $this->template = str_replace('{dol_value_customer_phone}', $object->thirdparty->phone, $this->template);
  580. //$this->template = str_replace('<dol_value_customer_mobile>', $object->thirdparty->mobile, $this->template);
  581. $this->template = str_replace('{dol_value_customer_tax_number}', $object->thirdparty->tva_intra, $this->template);
  582. //$this->template = str_replace('<dol_value_customer_account_balance>', $object->customer_account_balance, $this->template);
  583. //$this->template = str_replace('<dol_value_customer_points>', $object->customer_points, $this->template);
  584. $this->template = str_replace('{dol_value_mysoc_name}', $mysoc->name, $this->template);
  585. $this->template = str_replace('{dol_value_mysoc_address}', $mysoc->address, $this->template);
  586. $this->template = str_replace('{dol_value_mysoc_zip}', $mysoc->zip, $this->template);
  587. $this->template = str_replace('{dol_value_mysoc_town}', $mysoc->town, $this->template);
  588. $this->template = str_replace('{dol_value_mysoc_country}', $mysoc->country, $this->template);
  589. $this->template = str_replace('{dol_value_mysoc_idprof1}', $mysoc->idprof1, $this->template);
  590. $this->template = str_replace('{dol_value_mysoc_idprof2}', $mysoc->idprof2, $this->template);
  591. $this->template = str_replace('{dol_value_mysoc_idprof3}', $mysoc->idprof3, $this->template);
  592. $this->template = str_replace('{dol_value_mysoc_idprof4}', $mysoc->idprof4, $this->template);
  593. $this->template = str_replace('{dol_value_mysoc_idprof5}', $mysoc->idprof5, $this->template);
  594. $this->template = str_replace('{dol_value_mysoc_idprof6}', $mysoc->idprof6, $this->template);
  595. $this->template = str_replace('{dol_value_mysoc_tva_intra}', $mysoc->tva_intra, $this->template);
  596. $this->template = str_replace('{dol_value_mysoc_capital}', $mysoc->capital, $this->template);
  597. $this->template = str_replace('{dol_value_mysoc_url}', $mysoc->url, $this->template);
  598. $this->template = str_replace('{dol_value_vendor_firstname}', $user->firstname, $this->template);
  599. $this->template = str_replace('{dol_value_vendor_lastname}', $user->lastname, $this->template);
  600. $this->template = str_replace('{dol_value_vendor_mail}', $user->email, $this->template);
  601. // parse template
  602. $this->template = str_replace("{", "<", $this->template);
  603. $this->template = str_replace("}", ">", $this->template);
  604. $p = xml_parser_create();
  605. xml_parse_into_struct($p, $this->template, $vals, $index);
  606. xml_parser_free($p);
  607. //print '<pre>'.print_r($index, true).'</pre>';
  608. //print '<pre>'.print_r($vals, true).'</pre>';
  609. // print ticket
  610. $level = 0;
  611. $nbcharactbyline = (!empty($conf->global->RECEIPT_PRINTER_NB_CHARACT_BY_LINE) ? $conf->global->RECEIPT_PRINTER_NB_CHARACT_BY_LINE : 48);
  612. $ret = $this->initPrinter($printerid);
  613. if ($ret > 0) {
  614. setEventMessages($this->error, $this->errors, 'errors');
  615. } else {
  616. $nboflines = count($vals);
  617. for ($tplline = 0; $tplline < $nboflines; $tplline++) {
  618. //var_dump($vals[$tplline]['value']);
  619. switch ($vals[$tplline]['tag']) {
  620. case 'DOL_PRINT_TEXT':
  621. $this->printer->text($vals[$tplline]['value']);
  622. break;
  623. case 'DOL_PRINT_OBJECT_LINES':
  624. foreach ($object->lines as $line) {
  625. if ($line->fk_product) {
  626. $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - strlen($line->subprice) - 10 - 1;
  627. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  628. $this->printer->text($line->ref . $spaces . $line->qty . str_pad(price($line->subprice), 10, ' ', STR_PAD_LEFT) . ' ' . str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT) . "\n");
  629. $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n \n");
  630. } else {
  631. $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - strlen($line->subprice) - 10 - 1;
  632. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  633. $this->printer->text($line->description.$spaces.$line->qty.' '.str_pad(price($line->subprice), 10, ' ', STR_PAD_LEFT).' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  634. }
  635. }
  636. break;
  637. case 'DOL_PRINT_OBJECT_LINES_WITH_NOTES':
  638. foreach ($object->lines as $line) {
  639. if ($line->fk_product) {
  640. $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - 10 - 1;
  641. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  642. $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  643. $this->printer->text(strip_tags(htmlspecialchars_decode($line->product_label))."\n");
  644. $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - 10 - 1;
  645. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  646. $this->printer->text($line->description."\n");
  647. } else {
  648. $spacestoadd = $nbcharactbyline - strlen($line->description) - strlen($line->qty) - 10 - 1;
  649. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  650. $this->printer->text($line->description.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  651. }
  652. }
  653. break;
  654. case 'DOL_PRINT_OBJECT_TAX':
  655. //var_dump($object);
  656. $vatarray = array();
  657. foreach ($object->lines as $line) {
  658. $vatarray[$line->tva_tx] += $line->total_tva;
  659. }
  660. foreach ($vatarray as $vatkey => $vatvalue) {
  661. $spacestoadd = $nbcharactbyline - strlen($vatkey) - 12;
  662. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  663. $this->printer->text($spaces.$vatkey.'% '.str_pad(price($vatvalue), 10, ' ', STR_PAD_LEFT)."\n");
  664. }
  665. break;
  666. case 'DOL_PRINT_OBJECT_TAX1':
  667. //var_dump($object);
  668. $total_localtax1 = 0;
  669. foreach ($object->lines as $line) {
  670. $total_localtax1 += $line->total_localtax1;
  671. }
  672. $this->printer->text(str_pad(price($total_localtax1), 10, ' ', STR_PAD_LEFT)."\n");
  673. break;
  674. case 'DOL_PRINT_OBJECT_TAX2':
  675. //var_dump($object);
  676. $total_localtax2 = 0;
  677. foreach ($object->lines as $line) {
  678. $total_localtax2 += $line->total_localtax2;
  679. }
  680. $this->printer->text(str_pad(price($total_localtax2), 10, ' ', STR_PAD_LEFT)."\n");
  681. break;
  682. case 'DOL_PRINT_OBJECT_TOTAL':
  683. $title = $langs->trans('TotalHT');
  684. $spacestoadd = $nbcharactbyline - strlen($title) - 10;
  685. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  686. $this->printer->text($title.$spaces.str_pad(price($object->total_ht), 10, ' ', STR_PAD_LEFT)."\n");
  687. $title = $langs->trans('TotalVAT');
  688. $spacestoadd = $nbcharactbyline - strlen($title) - 10;
  689. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  690. $this->printer->text($title.$spaces.str_pad(price($object->total_tva), 10, ' ', STR_PAD_LEFT)."\n");
  691. $title = $langs->trans('TotalTTC');
  692. $spacestoadd = $nbcharactbyline - strlen($title) - 10;
  693. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  694. $this->printer->text($title.$spaces.str_pad(price($object->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  695. break;
  696. case 'DOL_PRINT_CURR_DATE':
  697. if (strlen($vals[$tplline]['value'])<2) $this->printer->text(date('d/m/Y H:i:s')."\n");
  698. else $this->printer->text(date($vals[$tplline]['value'])."\n");
  699. break;
  700. case 'DOL_LINE_FEED':
  701. $this->printer->feed();
  702. break;
  703. case 'DOL_LINE_FEED_REVERSE':
  704. $this->printer->feedReverse();
  705. break;
  706. case 'DOL_ALIGN_CENTER':
  707. $this->printer->setJustification(Printer::JUSTIFY_CENTER);
  708. break;
  709. case 'DOL_ALIGN_RIGHT':
  710. $this->printer->setJustification(Printer::JUSTIFY_RIGHT);
  711. break;
  712. case 'DOL_ALIGN_LEFT':
  713. $this->printer->setJustification(Printer::JUSTIFY_LEFT);
  714. break;
  715. case 'DOL_OPEN_DRAWER':
  716. $this->printer->pulse();
  717. break;
  718. case 'DOL_ACTIVATE_BUZZER':
  719. //$this->printer->buzzer();
  720. break;
  721. case 'DOL_PRINT_BARCODE':
  722. // $vals[$tplline]['value'] -> barcode($content, $type)
  723. // var_dump($vals[$tplline]['value']);
  724. try {
  725. $this->printer->barcode($vals[$tplline]['value']);
  726. } catch (Exception $e) {
  727. $this->errors[] = 'Invalid Barcode value: '.$vals[$tplline]['value'];
  728. $error++;
  729. }
  730. break;
  731. case 'DOL_PRINT_LOGO':
  732. $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
  733. $this->printer->graphics($img);
  734. break;
  735. case 'DOL_PRINT_LOGO_OLD':
  736. $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
  737. $this->printer->bitImage($img);
  738. break;
  739. case 'DOL_PRINT_LOGO_OLD_CF':
  740. $img = EscposImage::load(DOL_DATA_ROOT.'/mycompany/logos/'.$mysoc->logo);
  741. $this->printer->bitImageColumnFormat($img);
  742. break;
  743. case 'DOL_PRINT_QRCODE':
  744. // $vals[$tplline]['value'] -> qrCode($content, $ec, $size, $model)
  745. $this->printer->qrcode($vals[$tplline]['value']);
  746. break;
  747. case 'DOL_CUT_PAPER_FULL':
  748. $this->printer->cut(Printer::CUT_FULL);
  749. break;
  750. case 'DOL_CUT_PAPER_PARTIAL':
  751. $this->printer->cut(Printer::CUT_PARTIAL);
  752. break;
  753. case 'DOL_USE_FONT_A':
  754. $this->printer->setFont(Printer::FONT_A);
  755. break;
  756. case 'DOL_USE_FONT_B':
  757. $this->printer->setFont(Printer::FONT_B);
  758. break;
  759. case 'DOL_USE_FONT_C':
  760. $this->printer->setFont(Printer::FONT_C);
  761. break;
  762. case 'DOL_BOLD':
  763. $this->printer->setEmphasis(true);
  764. break;
  765. case 'DOL_BOLD_DISABLED':
  766. $this->printer->setEmphasis(false);
  767. break;
  768. case 'DOL_DOUBLE_HEIGHT':
  769. $this->printer->setTextSize(1, 2);
  770. break;
  771. case 'DOL_DOUBLE_WIDTH':
  772. $this->printer->setTextSize(2, 1);
  773. break;
  774. case 'DOL_DEFAULT_HEIGHT_WIDTH':
  775. $this->printer->setTextSize(1, 1);
  776. break;
  777. case 'DOL_UNDERLINE':
  778. $this->printer->setUnderline(true);
  779. break;
  780. case 'DOL_UNDERLINE_DISABLED':
  781. $this->printer->setUnderline(false);
  782. break;
  783. case 'DOL_BEEP':
  784. $this->printer->getPrintConnector() -> write("\x1e");
  785. break;
  786. case 'DOL_BEEP_ALTERNATIVE': //if DOL_BEEP not works
  787. $this->printer->getPrintConnector() -> write(Printer::ESC . "B" . chr(4) . chr(1));
  788. break;
  789. case 'DOL_PRINT_ORDER_LINES':
  790. foreach ($object->lines as $line) {
  791. if ($line->special_code == $this->orderprinter) {
  792. $spacestoadd = $nbcharactbyline - strlen($line->ref) - strlen($line->qty) - 10 - 1;
  793. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  794. $this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
  795. $this->printer->text(strip_tags(htmlspecialchars_decode($line->desc))."\n");
  796. }
  797. }
  798. break;
  799. case 'DOL_PRINT_PAYMENT':
  800. $sql = "SELECT p.pos_change as pos_change, p.datep as date, p.fk_paiement, p.num_paiement as num, pf.amount as amount, pf.multicurrency_amount,";
  801. $sql .= " cp.code";
  802. $sql .= " FROM ".$this->db->prefix()."paiement_facture as pf, ".$this->db->prefix()."paiement as p";
  803. $sql .= " LEFT JOIN ".$this->db->prefix()."c_paiement as cp ON p.fk_paiement = cp.id";
  804. $sql .= " WHERE pf.fk_paiement = p.rowid AND pf.fk_facture = ".((int) $object->id);
  805. $sql .= " ORDER BY p.datep";
  806. $resql = $this->db->query($sql);
  807. if ($resql) {
  808. $num = $this->db->num_rows($resql);
  809. $i = 0;
  810. while ($i < $num) {
  811. $row = $this->db->fetch_object($resql);
  812. $spacestoadd = $nbcharactbyline - strlen($langs->transnoentitiesnoconv("PaymentTypeShort".$row->code)) - 12;
  813. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  814. $amount_payment = (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? $row->multicurrency_amount : $row->amount;
  815. if ($row->code == "LIQ") {
  816. $amount_payment = $amount_payment + $row->pos_change; // Show amount with excess received if is cash payment
  817. }
  818. $this->printer->text($spaces.$langs->transnoentitiesnoconv("PaymentTypeShort".$row->code).' '.str_pad(price($amount_payment), 10, ' ', STR_PAD_LEFT)."\n");
  819. if ($row->code == "LIQ" && $row->pos_change > 0) { // Print change only in cash payments
  820. $spacestoadd = $nbcharactbyline - strlen($langs->trans("Change")) - 12;
  821. $spaces = str_repeat(' ', $spacestoadd > 0 ? $spacestoadd : 0);
  822. $this->printer->text($spaces.$langs->trans("Change").' '.str_pad(price($row->pos_change), 10, ' ', STR_PAD_LEFT)."\n");
  823. }
  824. $i++;
  825. }
  826. }
  827. break;
  828. case 'DOL_VALUE_PLACE':
  829. $sql = "SELECT floor, label FROM ".$this->db->prefix()."takepos_floor_tables where rowid=".((int) str_replace(")", "", str_replace("(PROV-POS".$_SESSION["takeposterminal"]."-", "", $object->ref)));
  830. $resql = $this->db->query($sql);
  831. $obj = $this->db->fetch_object($resql);
  832. if ($obj) {
  833. $this->printer->text($obj->label);
  834. }
  835. break;
  836. default:
  837. $this->printer->text($vals[$tplline]['tag']);
  838. $this->printer->text($vals[$tplline]['value']);
  839. $this->errors[] = 'UnknowTag: &lt;'.strtolower($vals[$tplline]['tag']).'&gt;';
  840. $error++;
  841. break;
  842. }
  843. }
  844. // If is DummyPrintConnector send to log to debugging
  845. if ($this->printer->connector instanceof DummyPrintConnector || $conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") {
  846. $data = $this->printer->connector->getData();
  847. if ($conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") {
  848. echo rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  849. }
  850. dol_syslog($data);
  851. }
  852. // Close and print
  853. $this->printer->close();
  854. }
  855. return $error;
  856. }
  857. /**
  858. * Function to load Template
  859. *
  860. * @param int $templateid Template id
  861. * @return int 0 if OK; >0 if KO
  862. */
  863. public function loadTemplate($templateid)
  864. {
  865. global $conf;
  866. $error = 0;
  867. $sql = "SELECT template";
  868. $sql .= " FROM ".$this->db->prefix()."printer_receipt_template";
  869. $sql .= " WHERE rowid = ".((int) $templateid);
  870. $sql .= " AND entity = ".$conf->entity;
  871. $resql = $this->db->query($sql);
  872. if ($resql) {
  873. $obj = $this->db->fetch_array($resql);
  874. } else {
  875. $error++;
  876. $this->errors[] = $this->db->lasterror;
  877. }
  878. if (empty($obj)) {
  879. $error++;
  880. $this->errors[] = 'TemplateDontExist';
  881. } else {
  882. $this->template = $obj['0'];
  883. }
  884. return $error;
  885. }
  886. /**
  887. * Function Init Printer
  888. *
  889. * @param int $printerid Printer id
  890. * @return int 0 if OK; >0 if KO
  891. */
  892. public function initPrinter($printerid)
  893. {
  894. global $conf;
  895. if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
  896. $this->connector = new DummyPrintConnector();
  897. $this->printer = new Printer($this->connector, $this->profile);
  898. return;
  899. }
  900. $error = 0;
  901. $sql = "SELECT rowid, name, fk_type, fk_profile, parameter";
  902. $sql .= " FROM ".$this->db->prefix()."printer_receipt";
  903. $sql .= " WHERE rowid = ".((int) $printerid);
  904. $sql .= " AND entity = ".((int) $conf->entity);
  905. $resql = $this->db->query($sql);
  906. if ($resql) {
  907. $obj = $this->db->fetch_array($resql);
  908. if (empty($obj)) {
  909. $error++;
  910. $this->errors[] = 'PrinterDontExist';
  911. }
  912. if (!$error) {
  913. $parameter = (isset($obj['parameter']) ? $obj['parameter'] : '');
  914. try {
  915. $type = $obj['fk_type'];
  916. switch ($type) {
  917. case 1:
  918. $this->connector = new DummyPrintConnector();
  919. break;
  920. case 2:
  921. $this->connector = new FilePrintConnector($parameter);
  922. break;
  923. case 3:
  924. $parameters = explode(':', $parameter);
  925. $this->connector = new NetworkPrintConnector($parameters[0], $parameters[1]);
  926. break;
  927. case 4: // LPT1, smb://...
  928. $this->connector = new WindowsPrintConnector(dol_sanitizePathName($parameter));
  929. break;
  930. case 5:
  931. $this->connector = new CupsPrintConnector($parameter);
  932. break;
  933. default:
  934. $this->connector = 'CONNECTOR_UNKNOWN';
  935. break;
  936. }
  937. $this->printer = new Printer($this->connector, $this->profile);
  938. } catch (Exception $e) {
  939. $this->errors[] = $e->getMessage();
  940. $error++;
  941. }
  942. }
  943. } else {
  944. $error++;
  945. $this->errors[] = $this->db->lasterror;
  946. }
  947. return $error;
  948. }
  949. }