| Server IP : 121.121.20.254 / Your IP : 216.73.216.146 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/inetpub/wwwroot/CAFE1/wp-content/plugins/woocommerce-openpos-printer/ |
Upload File : |
<?php
class Op_Themial_Printer{
public $_filesystem;
public function __construct()
{
if(!class_exists('WP_Filesystem_Direct'))
{
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php');
}
$this->_filesystem = new WP_Filesystem_Direct(false);
add_action( 'init', array($this,'registerScripts'));
}
public function init(){
add_filter('op_addon_setting',array($this,'op_addon_setting'),10,1);
add_filter('op_get_login_cashdrawer_data',array($this,'op_get_login_cashdrawer_data'),10,1);
add_filter('openpos_pos_footer_js',array($this,'pos_footer'),20,1);
//add_action( 'op_pos_page_after', array($this,'custom_op_prnt_op_pos_page_after') );
add_action( 'wp_ajax_nopriv_op_print_get_receipt', array($this,'get_receipt') );
add_action( 'wp_ajax_op_print_get_receipt', array($this,'get_receipt') );
// add_action( 'op_pos_page_after', function(){
// echo '<div id="op-print-preview" style="display: block;
// position: absolute;
// top: -1000000px;
// height: fit-content;
// width: fit-content;"></div>';
// });
}
public function registerScripts(){
global $OPENPOS_SETTING;
global $op_in_pos_screen;
if($op_in_pos_screen)
{
$printinterface = $OPENPOS_SETTING->get_option('op_themal_printer_interface','openpos_addon');
if(!$printinterface)
{
$printinterface = 'usb';
}
//wp_enqueue_script('openpos.op_printer.html2canvas', 'https://html2canvas.hertzen.com/dist/html2canvas.min.js',array('jquery'));
//wp_enqueue_script('openpos.op_printer.js', OPENPOS_T_OP_PRINTER_URL.'/assets/js/printer.js',array('jquery','openpos.op_printer.html2canvas'),time());
wp_enqueue_script('openpos.op_printer.js', OPENPOS_T_OP_PRINTER_URL.'/assets/js/printer.js',array('jquery'),time());
wp_add_inline_script('openpos.op_printer.js',"
var op_printer_server_url = '".admin_url('admin-ajax.php')."';
var print_type = '".$printinterface."';
",'before');
}
}
function op_addon_setting($addition_general_setting){
global $OPENPOS_SETTING;
$addition_general_setting[] = array(
'name' => 'op_themal_printer_interface',
'label' => __( 'Thermal Printer Interface', 'openpos' ),
'desc' => 'Thermal Printer Interface. It depend your printer connection',
'type' => 'select',
'default' => 'usb',
'options' => array(
'usb' => __( 'USB', 'openpos' ),
'ble' => __( 'Bluethooth', 'openpos' ),
)
);
return $addition_general_setting;
}
function op_get_login_cashdrawer_data($session_response_data){
$session_response_data['setting']['pos_event_print'] = 'yes';
// $session_response_data['setting']['trigger_cashdrawer'] = 'yes';
$session_response_data['setting']['pos_kitchent_event_print'] = 'yes';
return $session_response_data;
}
function pos_footer($handles){
$handles[] = 'openpos.op_printer.js';
return $handles;
}
function custom_op_prnt_op_pos_page_after(){
?>
<iframe id="vid-oprinter" src="<?php echo OPENPOS_T_OP_PRINTER_URL; ?>/printer/index.php" style="display:none;"></iframe>
<?php
}
//register form
function op_register_form_end($default,$warehouses,$cashiers){
}
function op_register_save_after($id,$params,$op_register){
}
function byteStr2byteArray($s) {
return array_slice(unpack("C*", "\0".$s), 1);
}
function get_receipt(){
$result = array(
'status' => 0,
'message' => 'Unknown',
'data' => ''
);
$session = isset($_REQUEST['session']) ? $_REQUEST['session'] : '';
$source = isset($_REQUEST['source']) ? $_REQUEST['source'] : '';
$source_type = isset($_REQUEST['source_type']) ? $_REQUEST['source_type'] : '';
$print_type = isset($_REQUEST['print_type']) ? $_REQUEST['print_type'] : 'usb';
$order = isset($_REQUEST['print_data']) ? json_decode(stripslashes($_REQUEST['print_data']),true): array();
$print_details = array(
'max_char' => 32,
'print_id' => 1,
);
$max_char = isset($print_details['max_char']) ? $print_details['max_char'] : 0;
if($print_type == 'ble')
{
$receipt_data = array();
$print_content = array();
if($source_type != 'kitchen')
{
$receipt_data = $this->generatePrintContent($max_char,$order,$source_type,$source,true);
}else{
$receipt_data = $this->generateKitchenPrintContent($max_char,$order,$source_type,$source,true);
}
foreach($receipt_data as $c)
{
$print_content[] = $this->byteStr2byteArray($c);
}
}else{
if($source_type != 'kitchen')
{
$print_content = $this->generatePrintContent($max_char,$order,$source_type,$source);
}else{
$print_content = $this->generateKitchenPrintContent($max_char,$order,$source_type,$source);
}
}
$result['status'] = 1;
$result['data'] = $print_content;
echo json_encode($result);exit;
}
function custom_op_printnode_print(){
global $op_woo;
global $OPENPOS_SETTING;
global $op_session;
global $op_bizzswoop_printer;
$session = isset($_REQUEST['session']) ? $_REQUEST['session'] : '';
$source = isset($_REQUEST['source']) ? $_REQUEST['source'] : '';
$source_type = isset($_REQUEST['source_type']) ? $_REQUEST['source_type'] : '';
$order_html = stripslashes($_REQUEST['html']);
$order = json_decode(stripslashes($_REQUEST['order']),true);
$print_id = 0;
$session_data = $op_session->data($session);
$locations = $op_woo->getListRestaurantArea();
$openpos_type = $OPENPOS_SETTING->get_option('openpos_type','openpos_pos');
if($openpos_type != 'restaurant')
{
$locations = array();
}
if( !empty($session_data))
{
$register_id = isset($session_data['login_cashdrawer_id']) ? $session_data['login_cashdrawer_id'] : 0;
if($register_id)
{
$is_takeaway = isset($order['is_takeaway']) ? 1*$order['is_takeaway'] : 0;
$register_locations = get_post_meta($register_id,'_op_register_printnode',true);
$kitchen_items = array();
$order_items = isset($order['items']) ? $order['items'] : array();
foreach($locations as $code => $location)
{
$print_id = isset($register_locations[$code]) ? $register_locations[$code] : 0;
if($print_id)
{
foreach($order_items as $item)
{
if(isset($item['product_id']) && $item['product_id'])
{
$a_key = '_op_'.$code;
$product_id = $item['product_id'];
$parent_id = wp_get_post_parent_id($product_id);
if($parent_id)
{
$_op_value = get_post_meta($parent_id,$a_key,true);
}else{
$_op_value = get_post_meta($item['product_id'],$a_key,true);
}
$default_value = 'no';
if($_op_value )
{
if( $_op_value == 'no')
{
$default_value = 'no';
}
if( $_op_value == 'yes')
{
$default_value = 'yes';
}
}
if($default_value == 'yes')
{
if(!isset($kitchen_items[$code]))
{
$kitchen_items[$code] = array(
'print_id' => $print_id,
'items' => array()
);
}
$kitchen_items[$code]['items'][] = $item;
}
}
}
}
}
if($source_type != 'kitchen')
{
$print_id = isset($register_locations['_default']) ? $register_locations['_default'] : 0;
if($print_id)
{
$print_details = $this->getPrinterDetails($print_id);
$max_char = isset($print_details['max_char']) ? $print_details['max_char'] : 0;
$print_content = $this->generateBase64Content($max_char,$order,$source_type,$source);
$job_id = $this->print( $print_id, $print_content );
}
if(!$is_takeaway)
{
$kitchen_items = array();
}
}
foreach($kitchen_items as $kitchen)
{
$print_id = isset($kitchen['print_id']) ? $kitchen['print_id'] : 0;
if($print_id)
{
$print_details = $this->getPrinterDetails($print_id);
$max_char = isset($print_details['max_char']) ? $print_details['max_char'] : 0;
$tmp = $order;
$tmp['items'] = $kitchen['items'];
if(!isset($tmp['order_number_format']))
{
$tmp['order_number_format'] = isset($tmp['label']) ? $tmp['label'] : '';
}
if(!$tmp['order_number_format'])
{
$tmp['order_number_format'] = isset($tmp['desk']) ? $tmp['desk']['name'].'-'.$tmp['desk']['type'] : 'Unknown';
}
$print_content = $this->generateBase64KitchenContent($max_char,$tmp,$source_type,$source);
$job_id = $this->print( $print_id, $print_content );
}
}
}
}
exit;
}
public function getPrinterDetails($printer_id){
$result = array(
'max_char' => 32,
'print_id' => $printer_id,
);
return apply_filters('op_printnode_print',$result);
}
public function generateKitchenPrintContent($max_char,$order,$source_type,$source,$return_array = false){
$ESC = "\e";
$GS = chr(29);
$InitializePrinter = "@";
$nl="\n"; //Single new line
$dnl=$nl.$nl; //Double new line
$BoldOn = $ESC . "E" . "1";
$BoldOff = $ESC . "E" . "0";
$DoubleOn = $GS . "!" . "16"; // 2x sized text (double-high + double-wide)
$DoubleOff = $GS . "!" . "0";
$jLeft = $ESC."a"."0";//allign Left
$jCenter=$ESC."a"."1";//allign Center
$jRight=$ESC."a"."2";//allign Right
$order_number = isset($order['label']) ? $order['label'] : $order['order_number_format'];
$cmd = $ESC.$InitializePrinter.$jCenter.$BoldOn.$order_number.$BoldOff.$nl;
$cmd.= $dnl;
foreach($order['items'] as $item)
{
$qty = $item['qty'];
$name = $item['name'];
$columns = array();
$columns[] = $qty." x ";
$columns[] = $name;
$line_text = '';
if($max_char)
{
$line_text = $this->get_column_separated_data($columns,$max_char);
}
if(!$line_text)
{
$line_text = implode(' ',$columns);
}
$cmd.= $line_text;
$cmd.= $nl;
if(isset($item['sub_name']) && $item['sub_name'])
{
$substr = str_replace('</span>','</span>, ',$item['sub_name']);
$substr = strip_tags($substr);
$substr = trim($substr);
$substr = trim($substr,',');
if(strlen($substr) > 0)
{
$cmd.= $substr;
$cmd.= $nl;
}
}
}
$cmd.= $nl;
$cmd.=$jCenter."*******".__('Thank you','openpos-woocommerce-printnode')."*******".$nl;
$cmd.= $dnl;
$cmd.=$ESC;
return apply_filters('op_thermal_printer_kitchen_receipt_content',$cmd,$order);
}
public function generatePrintContent($max_char,$order,$source_type,$source,$return_array = false){
$ESC = "\e";
$GS = chr(29);
$InitializePrinter = "@";
$nl="\n"; //Single new line
$dnl=$nl.$nl; //Double new line
$BoldOn = $ESC . "E" . "1";
$BoldOff = $ESC . "E" . "0";
$DoubleOn = $GS . "!" . "16"; // 2x sized text (double-high + double-wide)
$DoubleOff = $GS . "!" . "0";
$jLeft = $ESC."a"."0";//allign Left
$jCenter=$ESC."a"."1";//allign Center
$jRight=$ESC."a"."2";//allign Right
$cutPaper = $ESC . "V" . "1";
$receipt_array_data = array();
if($source_type != 'kitchen')
{
$order_number = $order['order_number_format'];
$receipt_array_data[] = $ESC.$InitializePrinter.$jCenter.$BoldOn.$order_number.$BoldOff.$nl;
$receipt_array_data[] = $dnl;
$customer = isset($order['customer']) ? $order['customer'] : array();
$register = isset($order['register']) ? $order['register'] : array();
$customer_name = '';
$staff_name = isset($order['sale_person_name']) ? $order['sale_person_name'] : '';
if(!$staff_name)
{
$staff_name = __('Staff','openpos-woocommerce-printnode');
}
$register_name = isset($register['name']) ? $register['name'] : '';
if(isset($customer['name']) && $customer['name'])
{
$customer_name = $customer['name'];
}
if(!$customer_name && isset($customer['email']) && $customer['email'])
{
$customer_name = $customer['email'];
}
if(!$customer_name && isset($customer['phone']) && $customer['phone'])
{
$customer_name = $customer['phone'];
}
if(!$customer_name)
{
$customer_name = __('Guest','openpos-woocommerce-printnode');
}
$receipt_array_data[] = sprintf(__('Customer: %s','openpos-woocommerce-printnode'),$customer_name).$nl;
$receipt_array_data[] = sprintf(__('Staff: %s','openpos-woocommerce-printnode'),$staff_name).$nl;
if($register_name)
{
$receipt_array_data[] = sprintf(__('Register: %s','openpos-woocommerce-printnode'),$register_name).$nl;
}
$receipt_array_data[] = $nl;
foreach($order['items'] as $item)
{
$qty = $item['qty'];
$name = $item['name'];
$total_incl_tax_currency_formatted = $item['total_incl_tax_currency_formatted'];
$columns = array();
$columns[] = $qty." x ".$name;
$columns[] = $total_incl_tax_currency_formatted;
$line_text = '';
if($max_char)
{
$line_text = $this->get_column_separated_data($columns,$max_char);
}
if(!$line_text)
{
$line_text = implode(' ',$columns);
}
$receipt_array_data[] = $line_text.$nl;
if(isset($item['sub_name']) && $item['sub_name'])
{
$substr = str_replace('</span>','</span>, ',$item['sub_name']);
$substr = strip_tags($substr);
$substr = trim($substr);
$substr = trim($substr,',');
if(strlen($substr) > 0)
{
$receipt_array_data[] = $substr. $nl;
}
}
}
$receipt_array_data[] = $nl;
$subtotal = $order['sub_total_currency_formatted'];
$discount = $order['discount_final_amount_currency_formatted'];
$total_shipping = $order['shipping_cost_currency_formatted'];
$total_tax = $order['tax_amount_currency_formatted'];
$grand_total = $order['grand_total_currency_formatted'];
$receipt_array_data[] =$jLeft.__('SubTotal','openpos-woocommerce-printnode').":".$jRight." ".$subtotal.$nl;
$receipt_array_data[] =$jLeft.__('Discount','openpos-woocommerce-printnode').":".$jRight." ".$discount.$nl;
if($order['shipping_cost'] > 0)
{
$receipt_array_data[] =$jLeft.__('Shipping','openpos-woocommerce-printnode').":".$jRight." ".$total_shipping.$nl;
}
$receipt_array_data[] =$jLeft.__('TAX','openpos-woocommerce-printnode').":".$jRight." ".$total_tax.$nl;
$receipt_array_data[] =$jLeft.__('Grand Total','openpos-woocommerce-printnode').":".$jRight." ".$grand_total.$nl;
$receipt_array_data[] =$jCenter."-----".__('Payment','openpos-woocommerce-printnode')."-----".$nl;
foreach($order['payment_method'] as $method)
{
$receipt_array_data[] =$jLeft.$method['name'].":".$jRight." ".$method['paid_currency_formatted'].$nl;
if($method['code'] == 'cash' && $method['return'] > 0)
{
$cmd.=$jLeft.__('Return','openpos-woocommerce-printnode').":".$jRight." ".$method['return_currency_formatted'].$nl;
}
}
$receipt_array_data[] = $nl;
$receipt_array_data[] =$jCenter."*******".__('Thank you','openpos-woocommerce-printnode')."*******".$nl;
$receipt_array_data[] = $dnl;
$receipt_array_data[] =$ESC;
}else{
$cmd=$ESC.$InitializePrinter."#112".$nl;
$cmd.=$BoldOn."My Bold text".$BoldOff." no more bold".$nl;
$cmd.="Regular allign".$nl;
$cmd.=$jLeft."Left allign".$nl;
$cmd.=$jCenter."Center allign".$nl;
$cmd.=$jRight."Right allign".$nl;
$cmd.=$jLeft."VAT:".$jRight."45 $".$nl;
$cmd.=$ESC;
}
$receipt_array_data[] = $cutPaper;
if($return_array)
{
return apply_filters('op_thermal_printer_receipt_content',$receipt_array_data,$order,$return_array);
}else{
return apply_filters('op_thermal_printer_receipt_content',implode('',$receipt_array_data),$order,$return_array);
}
}
function get_column_separated_data($columns, $max_chars)
{
$total_columns = count($columns);
if ($total_columns == 0) return "";
if ($total_columns == 1) return $columns[0];
if ($total_columns == 2)
{
$total_characters = strlen($columns[0])+strlen($columns[1]);
$total_whitespace = $max_chars - $total_characters;
if ($total_whitespace < 0) return "";
return $columns[0].str_repeat(" ", $total_whitespace).$columns[1];
}
$total_characters = 0;
foreach ($columns as $column)
{
$total_characters += strlen($column);
}
$total_whitespace = $max_chars - $total_characters;
if ($total_whitespace < 0) return "";
$total_spaces = $total_columns-1;
$space_width = floor($total_whitespace / $total_spaces);
$result = $columns[0].str_repeat(" ", $space_width);
for ($i = 1; $i < ($total_columns-1); $i++)
{
$result .= $columns[$i].str_repeat(" ", $space_width);
}
$result .= $columns[$total_columns-1];
return $result;
}
}