*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
* or see http://www.gnu.org/
*/
/**
* \file htdocs/core/class/dolreceiptprinter.class.php
* \brief Print receipt ticket on various ESC/POS printer
*/
/*
* Tags for ticket template
*
* Left align text
* Center text
* Right align text
* Use font A of printer
* Use font B of printer
* Use font C of printer
* Text Bold
* Text double height
* Text double width
* Underline text
* Underline with double line
* Emphasized text
* Print in white on black
* Print barcode
* Print barcode customer id
* Ticket print width of 57mm
* Cut ticket completely
* Cut ticket partially
* Open cash drawer
* Activate buzzer
*
* Code which can be placed everywhere
* Print QR Code
* Print date AAAA-MM-DD
* Print date and time AAAA-MM-DD HH:MM:SS
* Print Year
* Print month in letters (example : november)
* Print month number
* Print day number
* Print day number
* Print table number (for restaurant, bar...)
* Print number of cutlery (for restaurant)
* Print payment method
* Print logo stored on printer. Example : 32|32
* Print logo stored on printer. Must be followed by logo code. For old printers.
* Print order lines
* Print order total tax
* Print order local tax
* Print order total
* Print order number
* Print order number after validation
* Print customer firstname
* Print customer name
* Print customer mail
* Print customer phone
* Print customer mobile
* Print customer skype
* Print customer VAT number
* Print customer account balance
* Print vendor name
* Print vendor firstname
* Print vendor mail
* Print customer points
* Print number of points for this order
*
* Conditional code at line start (if…then Print)
* Print the line IF a customer is affected to the order
* Print the line IF a vendor is affected to the order
* Print the line IF Happy Hour
* Print the line IF order is validated
* Print the line IF customer points > 0
* Print the line IF points of the order > 0
* Print the line IF customer has vat number
* Print the line IF customer balance > 0
*
*/
require_once DOL_DOCUMENT_ROOT .'/includes/escpos/Escpos.php';
/**
* Class to manage Receipt Printers
*/
class dolReceiptPrinter extends Escpos
{
const CONNECTOR_DUMMY = 1;
const CONNECTOR_FILE_PRINT = 2;
const CONNECTOR_NETWORK_PRINT = 3;
const CONNECTOR_WINDOWS_PRINT = 4;
//const CONNECTOR_JAVA = 5;
var $db;
var $error;
var $errors;
/**
* Constructor
*
* @param DoliDB $db database
*/
function __construct($db)
{
$this->db=$db;
}
/**
* list printers
*
* @return int 0 if OK; >0 if KO
*/
function listPrinters()
{
global $conf;
$error = 0;
$line = 0;
$sql = 'SELECT rowid, name, fk_type, parameter';
$sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt';
$sql.= ' WHERE entity = '.$conf->entity;
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
while ($line < $num) {
$obj[] = $this->db->fetch_array($resql);
$line++;
}
} else {
$error++;
$this->errors[] = $this->db->lasterror;
}
$this->listprinters = $obj;
return $error;
}
/**
* List printers templates
*
* @return int 0 if OK; >0 if KO
*/
function listPrintersTemplates()
{
global $conf;
$error = 0;
$line = 0;
$sql = 'SELECT rowid, name, template';
$sql.= ' FROM '.MAIN_DB_PREFIX.'printer_receipt_template';
$sql.= ' WHERE entity = '.$conf->entity;
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
while ($line < $num) {
$obj[] = $this->db->fetch_array($resql);
$line++;
}
} else {
$error++;
$this->errors[] = $this->db->lasterror;
}
$this->listprinterstemplates = $obj;
return $error;
}
/**
* Form to Select type printer
*
* @param string $selected Id printer type pre-selected
* @param string $htmlname select html name
* @return int 0 if OK; >0 if KO
*/
function selectTypePrinter($selected='', $htmlname='printertypeid')
{
global $langs;
$error = 0;
$html = '';
$this->resprint = $html;
return $error;
}
/**
* Function to Add a printer in db
*
* @param string $name Printer name
* @param int $type Printer type
* @param string $parameter Printer parameter
* @return int 0 if OK; >0 if KO
*/
function AddPrinter($name, $type, $parameter)
{
global $conf;
$error = 0;
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'printer_receipt';
$sql.= ' (name, fk_type, parameter, entity)';
$sql.= ' VALUES ("'.$name.'", '.$type.', "'.$parameter.'", '.$conf->entity.')';
$resql = $this->db->query($sql);
if (! $resql) {
$error++;
$this->errors[] = $this->db->lasterror;
}
return $error;
}
/**
* Function to Update a printer in db
*
* @param string $name Printer name
* @param int $type Printer type
* @param string $parameter Printer parameter
* @param int $printerid Printer id
* @return int 0 if OK; >0 if KO
*/
function UpdatePrinter($name, $type, $parameter, $printerid)
{
global $conf;
$error = 0;
$sql = 'UPDATE '.MAIN_DB_PREFIX.'printer_receipt';
$sql.= ' SET name="'.$name.'"';
$sql.= ', fk_type='.$type;
$sql.= ', parameter="'.$parameter.'"';
$sql.= ' WHERE rowid='.$printerid;
$resql = $this->db->query($sql);
if (! $resql) {
$error++;
$this->errors[] = $this->db->lasterror;
}
return $error;
}
/**
* Function to Delete a printer from db
*
* @param int $printerid Printer id
* @return int 0 if OK; >0 if KO
*/
function DeletePrinter($printerid)
{
global $conf;
$error = 0;
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'printer_receipt';
$sql.= ' WHERE rowid='.$printerid;
$resql = $this->db->query($sql);
if (! $resql) {
$error++;
$this->errors[] = $this->db->lasterror;
}
return $error;
}
/**
* Function to Send Test page to Printer
*
* @param int $printerid Printer id
* @return int 0 if OK; >0 if KO
*/
function SendTestToPrinter($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;
}
//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");
$testStr = "Testing 123";
$testprinter -> qrCode($testStr);
$testprinter -> text("Most simple example\n");
$testprinter -> feed();
$testprinter -> cut();
//print '
'.print_r($connector, true).'
';
$testprinter -> close();
//print '
'.print_r($connector, true).'
';
//print '
'.print_r($testprinter, true).'
';
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
$error++;
}
}
return $error;
}
/**
* Function to Print Receipt Ticket
*
* @param object $object order or invoice object
* @param int $template Template id
* @param int $printerid Printer id
* @return int 0 if OK; >0 if KO
*/
function SendToPrinter($object, $template, $printerid)
{
global $conf;
$error = 0;
// parse template
// print ticket
return $error;
}
}