| 1 |
<?php |
| 2 |
/** |
| 3 |
* TSPL output adapter. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Templates\Adapters |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Templates\Adapters; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Interfaces\Receipt_Output_Adapter_Interface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Tspl_Output_Adapter class. |
| 14 |
*/ |
| 15 |
class Tspl_Output_Adapter implements Receipt_Output_Adapter_Interface { |
| 16 |
/** |
| 17 |
* Transform receipt payload to TSPL/TSPL2 commands. |
| 18 |
* |
| 19 |
* @param array $receipt_data Canonical payload. |
| 20 |
* @param array $context Optional context. |
| 21 |
* |
| 22 |
* @return string |
| 23 |
*/ |
| 24 |
public function transform( array $receipt_data, array $context = array() ): string { |
| 25 |
$size_width_mm = isset( $context['label_width_mm'] ) ? (int) $context['label_width_mm'] : 72; |
| 26 |
$size_height_mm = isset( $context['label_height_mm'] ) ? (int) $context['label_height_mm'] : 120; |
| 27 |
$print_qr = isset( $context['print_qr'] ) ? (bool) $context['print_qr'] : false; |
| 28 |
$print_barcode = isset( $context['print_barcode'] ) ? (bool) $context['print_barcode'] : true; |
| 29 |
|
| 30 |
$order_number = isset( $receipt_data['order']['number'] ) ? (string) $receipt_data['order']['number'] : ''; |
| 31 |
$total = isset( $receipt_data['totals']['total_incl'] ) ? (float) $receipt_data['totals']['total_incl'] : 0; |
| 32 |
$store_name = isset( $receipt_data['store']['name'] ) ? (string) $receipt_data['store']['name'] : get_bloginfo( 'name' ); |
| 33 |
$qr_payload = isset( $receipt_data['fiscal']['qr_payload'] ) ? (string) $receipt_data['fiscal']['qr_payload'] : ''; |
| 34 |
|
| 35 |
$tspl = array( |
| 36 |
'SIZE ' . max( 30, $size_width_mm ) . ' mm,' . max( 30, $size_height_mm ) . ' mm', |
| 37 |
'GAP 2 mm,0 mm', |
| 38 |
'DENSITY 8', |
| 39 |
'DIRECTION 1', |
| 40 |
'CLS', |
| 41 |
'TEXT 30,30,"0",0,1,1,"' . $this->sanitize_text( $store_name ) . '"', |
| 42 |
'TEXT 30,55,"0",0,1,1,"WCPOS RECEIPT"', |
| 43 |
'TEXT 30,80,"0",0,1,1,"Order #' . $this->sanitize_text( $order_number ) . '"', |
| 44 |
'TEXT 30,130,"0",0,1,1,"Total ' . wc_format_decimal( $total, wc_get_price_decimals() ) . '"', |
| 45 |
); |
| 46 |
|
| 47 |
if ( $print_barcode ) { |
| 48 |
$tspl[] = 'BARCODE 30,180,"128",80,1,0,2,2,"' . $this->sanitize_text( $order_number ) . '"'; |
| 49 |
} |
| 50 |
if ( $print_qr && '' !== $qr_payload ) { |
| 51 |
$tspl[] = 'QRCODE 30,290,L,6,A,0,M2,S7,"' . $this->sanitize_text( $qr_payload ) . '"'; |
| 52 |
} |
| 53 |
|
| 54 |
$tspl[] = 'PRINT 1,1'; |
| 55 |
|
| 56 |
return implode( "\n", $tspl ) . "\n"; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Sanitize text for TSPL commands. |
| 61 |
* |
| 62 |
* @param string $value Text value. |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
private function sanitize_text( string $value ): string { |
| 67 |
$value = preg_replace( '/["\r\n]/', ' ', $value ); |
| 68 |
|
| 69 |
return trim( $value ); |
| 70 |
} |
| 71 |
} |
| 72 |
|