| 1 |
<?php |
| 2 |
/** |
| 3 |
* CPCL 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 |
* Cpcl_Output_Adapter class. |
| 14 |
*/ |
| 15 |
class Cpcl_Output_Adapter implements Receipt_Output_Adapter_Interface { |
| 16 |
/** |
| 17 |
* Transform receipt payload to CPCL print 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 |
$width = isset( $context['label_width'] ) ? (int) $context['label_width'] : 576; |
| 26 |
$height = isset( $context['label_height'] ) ? (int) $context['label_height'] : 700; |
| 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 |
$cpcl = array( |
| 36 |
'! 0 200 200 ' . max( 200, $height ) . ' 1', |
| 37 |
'PW ' . max( 200, $width ), |
| 38 |
'TEXT 4 0 30 20 ' . $this->sanitize_text( $store_name ), |
| 39 |
'TEXT 4 0 30 60 WCPOS RECEIPT', |
| 40 |
'TEXT 4 0 30 80 Order #' . $this->sanitize_text( $order_number ), |
| 41 |
'TEXT 4 0 30 130 Total ' . wc_format_decimal( $total, wc_get_price_decimals() ), |
| 42 |
); |
| 43 |
|
| 44 |
if ( $print_barcode ) { |
| 45 |
$cpcl[] = 'BARCODE 128 1 1 60 30 180 ' . $this->sanitize_text( $order_number ); |
| 46 |
} |
| 47 |
if ( $print_qr && '' !== $qr_payload ) { |
| 48 |
$cpcl[] = 'B QR 30 260 M 2 U 6'; |
| 49 |
$cpcl[] = 'MA,' . $this->sanitize_text( $qr_payload ); |
| 50 |
$cpcl[] = 'ENDQR'; |
| 51 |
} |
| 52 |
|
| 53 |
$cpcl[] = 'FORM'; |
| 54 |
$cpcl[] = 'PRINT'; |
| 55 |
|
| 56 |
return implode( "\n", $cpcl ) . "\n"; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Sanitize text for CPCL commands. |
| 61 |
* |
| 62 |
* @param string $value Text value. |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
private function sanitize_text( string $value ): string { |
| 67 |
return trim( preg_replace( '/[\r\n]/', ' ', $value ) ); |
| 68 |
} |
| 69 |
} |
| 70 |
|