diff --git a/htdocs/admin/receiptprinter.php b/htdocs/admin/receiptprinter.php index fe20da1bef9..8e3c7388adf 100644 --- a/htdocs/admin/receiptprinter.php +++ b/htdocs/admin/receiptprinter.php @@ -375,7 +375,7 @@ if ($mode == 'template' && $user->admin) print '
'.print_r($obj, true).''; - if (! $error) { - $parameter = $obj['parameter']; try { - switch ($obj['fk_type']) { - case 1: - require_once DOL_DOCUMENT_ROOT .'/includes/escpos/src/DummyPrintConnector.php'; - $connector = new DummyPrintConnector(); - break; - case 2: - $connector = new FilePrintConnector($parameter); - break; - case 3: - $parameters = explode(':', $parameter); - $connector = new NetworkPrintConnector($parameters[0], $parameters[1]); - break; - case 4: - $connector = new WindowsPrintConnector($parameter); - break; - default: - $connector = 'CONNECTOR_UNKNOWN'; - break; - } - $testprinter = new Escpos($connector); - $img = new EscposImage(DOL_DOCUMENT_ROOT .'/theme/common/dolibarr_logo_bw.png'); - $testprinter -> graphics($img); - $testprinter -> text("Hello World!\n"); + $this->printer->graphics($img); + $this->printer->text("Hello World!\n"); $testStr = "Testing 123"; - $testprinter -> qrCode($testStr); - $testprinter -> text("Most simple example\n"); - $testprinter -> feed(); - $testprinter -> cut(); - //print '
'.print_r($connector, true).''; - $testprinter -> close(); + $this->printer->qrCode($testStr); + $this->printer->text("Most simple example\n"); + $this->printer->feed(); + $this->printer->cut(); + //print '
'.print_r($this->connector, true).''; + $this->printer->close(); - //print '
'.print_r($connector, true).''; - //print '
'.print_r($testprinter, true).''; } catch (Exception $e) { $this->errors[] = $e->getMessage(); $error++; @@ -437,21 +407,186 @@ class dolReceiptPrinter extends Escpos * Function to Print Receipt Ticket * * @param object $object order or invoice object - * @param int $template Template id + * @param int $templateid Template id * @param int $printerid Printer id * @return int 0 if OK; >0 if KO */ - function SendToPrinter($object, $template, $printerid) + function SendToPrinter($object, $templateid, $printerid) { global $conf; $error = 0; + $ret = $this->loadTemplate($templateid); + + // tags a remplacer par leur valeur avant de parser + $this->template = str_replace('
'.print_r($index, true).''; + //print '
'.print_r($vals, true).''; // print ticket + $level = 0; + $ret = $this->InitPrinter($printerid); + if ($ret>0) { + setEventMessages($this->error, $this->errors, 'errors'); + } else { + for ($line=0; $line < count($vals); $line++) { + switch ($vals[$line]['tag']) { + case 'DOL_ALIGN_CENTER': + $this->printer->setJustification(Escpos::JUSTIFY_CENTER); + $this->printer->text($vals[$line]['value']); + break; + case 'DOL_ALIGN_RIGHT': + $this->printer->setJustification(Escpos::JUSTIFY_RIGHT); + break; + case 'DOL_ALIGN_LEFT': + $this->printer->setJustification(Escpos::JUSTIFY_LEFT); + break; + case 'DOL_OPEN_DRAWER': + $this->printer->pulse(); + break; + case 'DOL_PRINT_BARCODE': + // $vals[$line]['value'] -> barcode($content, $type) + $this->printer->barcode($object->barcode); + break; + case 'DOL_PRINT_DATE_TIME': + $this->printer->text($object->date); + break; + case 'DOL_PRINT_QRCODE': + // $vals[$line]['value'] -> qrCode($content, $ec, $size, $model) + $this->printer->qrcode($vals[$line]['value']); + break; + case 'DOL_CUT_PAPER_FULL': + $this->printer->cut(Escpos::CUT_FULL); + break; + case 'DOL_CUT_PAPER_PARTIAL': + $this->printer->cut(Escpos::CUT_PARTIAL); + break; + case 'DOL_USE_FONT_A': + $this->printer->setFont(Escpos::FONT_A); + $this->printer->text($vals[$line]['value']); + break; + case 'DOL_USE_FONT_B': + $this->printer->setFont(Escpos::FONT_B); + $this->printer->text($vals[$line]['value']); + break; + case 'DOL_USE_FONT_C': + $this->printer->setFont(Escpos::FONT_C); + $this->printer->text($vals[$line]['value']); + break; + default: + $this->printer->text($vals[$line]['value']); + $this->errors[] = 'UnknowTag: <'.strtolower($vals[$line]['tag']).'>'; + $error++; + break; + } + } + // Close and print + // uncomment next line to see content sent to printer + //print '
'.print_r($this->connector, true).''; + $this->printer->close(); + + } + return $error; + } + + /** + * Function to load Template + * + * @param int $templateid Template id + * @return int 0 if OK; >0 if KO + */ + function loadTemplate($templateid) + { + global $conf; + $error = 0; + $sql = 'SELECT template'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt_template'; + $sql.= ' WHERE rowid='.$templateid; + $sql.= ' AND entity = '.$conf->entity; + $resql = $this->db->query($sql); + if ($resql) { + $obj = $this->db->fetch_array($resql); + } else { + $error++; + $this->errors[] = $this->db->lasterror; + } + if (empty($obj)) { + $error++; + $this->errors[] = 'TemplateDontExist'; + } else { + $this->template = $obj['0']; + } return $error; } + + /** + * Function Init Printer + * + * @param int $printerid Printer id + * @return int 0 if OK; >0 if KO + */ + function InitPrinter($printerid) + { + global $conf; + $error=0; + $sql = 'SELECT rowid, name, fk_type, parameter'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt'; + $sql.= ' WHERE rowid = '.$printerid; + $sql.= ' AND entity = '.$conf->entity; + $resql = $this->db->query($sql); + if ($resql) { + $obj = $this->db->fetch_array($resql); + } else { + $error++; + $this->errors[] = $this->db->lasterror; + } + if (empty($obj)) { + $error++; + $this->errors[] = 'PrinterDontExist'; + } + if (! $error) { + $parameter = $obj['parameter']; + try { + switch ($obj['fk_type']) { + case 1: + require_once DOL_DOCUMENT_ROOT .'/includes/escpos/src/DummyPrintConnector.php'; + $this->connector = new DummyPrintConnector(); + break; + case 2: + $this->connector = new FilePrintConnector($parameter); + break; + case 3: + $parameters = explode(':', $parameter); + $this->connector = new NetworkPrintConnector($parameters[0], $parameters[1]); + break; + case 4: + $this->connector = new WindowsPrintConnector($parameter); + break; + default: + $this->connector = 'CONNECTOR_UNKNOWN'; + break; + } + $this->printer = new Escpos($this->connector); + } catch (Exception $e) { + $this->errors[] = $e->getMessage(); + $error++; + } + } + return $error; + } } diff --git a/htdocs/core/modules/modReceiptPrinter.class.php b/htdocs/core/modules/modReceiptPrinter.class.php index 375ab653d3c..af66f45d23c 100644 --- a/htdocs/core/modules/modReceiptPrinter.class.php +++ b/htdocs/core/modules/modReceiptPrinter.class.php @@ -49,7 +49,7 @@ class modReceiptPrinter extends DolibarrModules $this->family = "technic"; // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module) $this->name = preg_replace('/^mod/i','',get_class($this)); - // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module) + // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module) $this->description = "ReceiptPrinterDesc"; $this->version = 'dolibarr'; // 'development' or 'experimental' or 'dolibarr' or version $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name); diff --git a/htdocs/langs/en_US/receiptprinter.lang b/htdocs/langs/en_US/receiptprinter.lang index d95b1631b40..dc2cd392c20 100644 --- a/htdocs/langs/en_US/receiptprinter.lang +++ b/htdocs/langs/en_US/receiptprinter.lang @@ -57,16 +57,16 @@ DOL_PRINT_ORDER_LOCAL_TAX=Print order local tax DOL_PRINT_ORDER_TOTAL=Print order total DOL_PRINT_ORDER_NUMBER=Print order number DOL_PRINT_ORDER_NUMBER_UNIQUE=Print order number after validation -DOL_PRINT_CUSTOMER_FIRST_NAME=Print customer firstname -DOL_PRINT_CUSTOMER_LAST_NAME=Print customer name +DOL_PRINT_CUSTOMER_FIRSTNAME=Print customer firstname +DOL_PRINT_CUSTOMER_LASTNAME=Print customer name DOL_PRINT_CUSTOMER_MAIL=Print customer mail -DOL_PRINT_CUSTOMER_TELEPHONE=Print customer phone +DOL_PRINT_CUSTOMER_PHONE=Print customer phone DOL_PRINT_CUSTOMER_MOBILE=Print customer mobile DOL_PRINT_CUSTOMER_SKYPE=Print customer skype DOL_PRINT_CUSTOMER_TAX_NUMBER=Print customer VAT number DOL_PRINT_CUSTOMER_ACCOUNT_BALANCE=Print customer account balance -DOL_PRINT_VENDOR_LAST_NAME=Print vendor name -DOL_PRINT_VENDOR_FIRST_NAME=Print vendor firstname +DOL_PRINT_VENDOR_LASTNAME=Print vendor name +DOL_PRINT_VENDOR_FIRSTNAME=Print vendor firstname DOL_PRINT_VENDOR_MAIL=Print vendor mail DOL_PRINT_CUSTOMER_POINTS=Print customer points DOL_PRINT_ORDER_POINTS=Print number of points for this order