', $object->cutlery, $this->template);
// parse template
$p = xml_parser_create();
xml_parse_into_struct($p, $this->template, $vals, $index);
xml_parser_free($p);
//print ''.print_r($index, true).'
';
//print ''.print_r($vals, true).'
';
// print ticket
$level = 0;
$nbcharactbyline = 48;
$ret = $this->initPrinter($printerid);
if ($ret>0) {
setEventMessages($this->error, $this->errors, 'errors');
} else {
$nboflines = count($vals);
for ($tplline=0; $tplline < $nboflines; $tplline++) {
//var_dump($vals[$tplline]['value']);
switch ($vals[$tplline]['tag']) {
case 'DOL_PRINT_TEXT':
$this->printer->text($vals[$tplline]['value']);
break;
case 'DOL_PRINT_OBJECT_LINES':
foreach ($object->lines as $line) {
//var_dump($line);
$spacestoadd = $nbcharactbyline - strlen($line->ref)- strlen($line->qty) - 10 - 1;
$spaces = str_repeat(' ', $spacestoadd);
$this->printer->text($line->ref.$spaces.$line->qty.' '.str_pad(price($line->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
$this->printer->text(strip_tags(htmlspecialchars_decode($line->desc))."\n");
}
break;
case 'DOL_PRINT_OBJECT_TAX':
//var_dump($object);
$vatarray = array();
foreach ($object->lines as $line) {
$vatarray[$line->tva_tx] += $line->total_tva;
}
foreach($vatarray as $vatkey => $vatvalue) {
$spacestoadd = $nbcharactbyline - strlen($vatkey)- 12;
$spaces = str_repeat(' ', $spacestoadd);
$this->printer->text($spaces. $vatkey.'% '.str_pad(price($vatvalue), 10, ' ', STR_PAD_LEFT)."\n");
}
break;
case 'DOL_PRINT_OBJECT_TOTAL':
$title = $langs->trans('TotalHT');
$spacestoadd = $nbcharactbyline - strlen($title) - 10;
$spaces = str_repeat(' ', $spacestoadd);
$this->printer->text($title.$spaces.str_pad(price($object->total_ht), 10, ' ', STR_PAD_LEFT)."\n");
$title = $langs->trans('TotalVAT');
$spacestoadd = $nbcharactbyline - strlen($title) - 10;
$spaces = str_repeat(' ', $spacestoadd);
$this->printer->text($title.$spaces.str_pad(price($object->total_tva), 10, ' ', STR_PAD_LEFT)."\n");
$title = $langs->trans('TotalTTC');
$spacestoadd = $nbcharactbyline - strlen($title) - 10;
$spaces = str_repeat(' ', $spacestoadd);
$this->printer->text($title.$spaces.str_pad(price($object->total_ttc), 10, ' ', STR_PAD_LEFT)."\n");
break;
case 'DOL_LINE_FEED':
$this->printer->feed();
break;
case 'DOL_LINE_FEED_REVERSE':
$this->printer->feedReverse();
break;
case 'DOL_ALIGN_CENTER':
$this->printer->setJustification(Printer::JUSTIFY_CENTER);
break;
case 'DOL_ALIGN_RIGHT':
$this->printer->setJustification(Printer::JUSTIFY_RIGHT);
break;
case 'DOL_ALIGN_LEFT':
$this->printer->setJustification(Printer::JUSTIFY_LEFT);
break;
case 'DOL_OPEN_DRAWER':
$this->printer->pulse();
break;
case 'DOL_ACTIVATE_BUZZER':
//$this->printer->buzzer();
break;
case 'DOL_PRINT_BARCODE':
// $vals[$tplline]['value'] -> barcode($content, $type)
// var_dump($vals[$tplline]['value']);
try {
$this->printer->barcode($vals[$tplline]['value']);
} catch (Exception $e) {
$this->errors[] = 'Invalid Barcode value: '.$vals[$tplline]['value'];
$error++;
}
break;
case 'DOL_PRINT_LOGO':
$img = EscposImage::load(DOL_DATA_ROOT .'/mycompany/logos/'.$mysoc->logo);
$this->printer->graphics($img);
break;
case 'DOL_PRINT_LOGO_OLD':
$img = EscposImage::load(DOL_DATA_ROOT .'/mycompany/logos/'.$mysoc->logo);
$this->printer->bitImage($img);
break;
case 'DOL_PRINT_QRCODE':
// $vals[$tplline]['value'] -> qrCode($content, $ec, $size, $model)
$this->printer->qrcode($vals[$tplline]['value']);
break;
case 'DOL_CUT_PAPER_FULL':
$this->printer->cut(Printer::CUT_FULL);
break;
case 'DOL_CUT_PAPER_PARTIAL':
$this->printer->cut(Printer::CUT_PARTIAL);
break;
case 'DOL_USE_FONT_A':
$this->printer->setFont(Printer::FONT_A);
break;
case 'DOL_USE_FONT_B':
$this->printer->setFont(Printer::FONT_B);
break;
case 'DOL_USE_FONT_C':
$this->printer->setFont(Printer::FONT_C);
break;
default:
$this->printer->text($vals[$tplline]['tag']);
$this->printer->text($vals[$tplline]['value']);
$this->errors[] = 'UnknowTag: <'.strtolower($vals[$tplline]['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
*/
public 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
*/
public function initPrinter($printerid)
{
global $conf;
$error=0;
$sql = 'SELECT rowid, name, fk_type, fk_profile, 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/mike42/escpos-php/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 Printer($this->connector, $this->profile);
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
$error++;
}
}
return $error;
}
}