Files
webapp/src/Printer/PrintPOS.php
T
2026-10-06 14:05:52 +07:00

147 lines
4.4 KiB
PHP

<?php
/*******************************************************************
Module : /Printer/PrintPOS.php
Created By : Siau Pheng ([email protected]).
Created Date : June 15th, 2008.
Last Modified : October 3rd, 2026.
(c) 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace Webapp\Printer;
use \flight\Engine;
/**
* Class PrintPOS
*
* Pembuatan Nota/Struk untuk cetak ke Printer POS.
*
* @license MIT, https://www.3fonia.com
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
*
* @package Webapp\Printer
*/
class PrintPOS {
private int $sheet = 0;
private int $maxchr = 40;
private array $header = [];
private array $acontent = [];
private array $config = [
'fontsize' => 1,
'opendraw' => false,
'cutpaper' => false,
'logo' => null,
];
private array $webapp;
private array $webuser;
protected Engine $app;
/**
* @param Engine $app
*/
public function __construct(Engine $app) {
$this->app = $app;
$this->webapp = $app->session()->get('WEB_APP', ['APP_NAME'=>'', 'APP_VER'=>'']);
$this->webuser = $app->session()->get('WEB_USER', ['UserID'=>0, 'Username'=>'']);
}
public function set_opendrawer(): void {
$this->config['opendraw'] = true;
}
public function set_cutpaper(): void {
$this->config['cutpaper'] = true;
}
public function set_maxchr(int $max = 0): void {
if ($max > 0) $this->maxchr = $max;
}
public function set_fontsize(int $size = 1): void {
if ($size > 0) $this->config['fontsize'] = $size;
}
public function set_header_logo(string $logo_file = ""): void {
if (!empty($logo_file) && file_exists(PROJECT_ROOT."public/assets/images/".$logo_file)) {
//$this->config['logo'] = PROJECT_BASE."assets/images/".$logo_file;
$this->config['logo'] = base64_encode(file_get_contents(PROJECT_ROOT."public/assets/images/".$logo_file));
}
}
public function add_header(): void {
$extra = func_get_args();
if (count($extra) > 0) $this->header = $extra;
}
public function add_sheet(): void {
$this->sheet++;
}
public function add(): void {
$val_arr = func_get_args(); $ttext = ""; $tstyle = "L";
if (is_array($val_arr[0])) {
foreach ($val_arr as $aval) {
$ttext .= $this->set_align(($aval[0] ?? ""), ($aval[1] ?? 0), ($aval[2] ?? $tstyle));
}
} else {
$ttext = $val_arr[0] ?? "";
$tstyle = $val_arr[1] ?? $tstyle;
}
if (str_contains($ttext, "_LINE")) {
$tmp_arr = explode(" ", $ttext);
$ttext = str_pad($tmp_arr[1], $this->maxchr, $tmp_arr[1]);
}
$this->acontent[$this->sheet][] = ($tstyle === "X") ? $ttext : $this->set_align($ttext, $this->maxchr, $tstyle);
}
public function addl(string $text = ""): void {
$this->add($text);
}
public function addr(string $text = ""): void {
$this->add($text, "R");
}
public function addc(string $text = ""): void {
$this->add($text, "C");
}
public function add_line(string $line = "-"): void {
$this->add("__LINE ".$line);
}
public function add_empty(int $baris = 1): void {
for ($i=0; $i<$baris; $i++) $this->add("");
}
public function add_stamp(int $ctk_ke=1, bool $cetak_nama=false): void {
$this->add($this->webapp['APP_NAME']."v".$this->webapp['APP_VER'].date("\@ymd.His")."-".isi_nol($this->webuser['UserID'], 3).(($cetak_nama)?"/".strtoupper($this->webuser['Username']):"").(($ctk_ke>0)?"#".isi_nol($ctk_ke,2):""));
}
public function show(int $jumlah_ctk=1): void {
$this->config['times'] = $jumlah_ctk;
$aout = ['type' => "escp"];
$aout['config'] = $this->config;
$aout['header'] = $this->header;
$aout['content'] = $this->acontent;
$this->app->json($aout);
}
public function set_align(string $text, int $len=0, string $dir="L"): string {
$apad = ["L"=>STR_PAD_RIGHT, "R"=>STR_PAD_LEFT, "C"=>STR_PAD_BOTH];
$nlen = ($len==0) ? strlen($text) : $len;
$ttext = (strlen($text)>$len) ? substr($text, 0, $len) : $text;
return str_pad($text, $len, " ", $apad[$dir]);
}
}
?>