PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.16
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.16
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / Templates / Adapters / Starprnt_Output_Adapter.php

Starprnt_Output_Adapter.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.16, at includes/Templates/Adapters/Starprnt_Output_Adapter.php

104 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * StarPRNT output adapter.
4 *
5 * NON-FUNCTIONAL PLACEHOLDER: this fixed-layout adapter emits human-readable
6 * marker strings (e.g. `[STARPRNT:ALIGN:CENTER]`), not real StarPRNT wire
7 * bytes, and is not decodable by any printer. The production StarPRNT emitter
8 * is `WCPOS\WooCommercePOS\Templates\Thermal\Starprnt_Thermal_Emitter`
9 * (template/AST path). Print job validation rejects the 'starprnt' format for
10 * Star CloudPRNT printers so this adapter cannot reach real Star hardware.
11 *
12 * @package WCPOS\WooCommercePOS\Templates\Adapters
13 */
14
15 namespace WCPOS\WooCommercePOS\Templates\Adapters;
16
17 use WCPOS\WooCommercePOS\Interfaces\Receipt_Output_Adapter_Interface;
18
19 /**
20 * Starprnt_Output_Adapter class.
21 */
22 class Starprnt_Output_Adapter implements Receipt_Output_Adapter_Interface {
23 /**
24 * StarPRNT initialize marker.
25 */
26 const CMD_INIT = "\x1b\x40";
27
28 /**
29 * StarPRNT center alignment marker.
30 */
31 const CMD_ALIGN_CENTER = '[STARPRNT:ALIGN:CENTER]';
32
33 /**
34 * StarPRNT left alignment marker.
35 */
36 const CMD_ALIGN_LEFT = '[STARPRNT:ALIGN:LEFT]';
37
38 /**
39 * StarPRNT full cut marker.
40 */
41 const CMD_CUT_FULL = '[STARPRNT:CUT:FULL]';
42
43 /**
44 * StarPRNT partial cut marker.
45 */
46 const CMD_CUT_PARTIAL = '[STARPRNT:CUT:PARTIAL]';
47
48 /**
49 * StarPRNT drawer marker.
50 */
51 const CMD_DRAWER = '[STARPRNT:DRAWER]';
52
53 /**
54 * Transform receipt payload to StarPRNT line format.
55 *
56 * @param array $receipt_data Canonical payload.
57 * @param array $context Optional context.
58 *
59 * @return string
60 */
61 public function transform( array $receipt_data, array $context = array() ): string {
62 $partial_cut = isset( $context['partial_cut'] ) ? (bool) $context['partial_cut'] : false;
63 $open_drawer = isset( $context['open_drawer'] ) ? (bool) $context['open_drawer'] : false;
64 $print_qr = isset( $context['print_qr'] ) ? (bool) $context['print_qr'] : false;
65
66 $order_number = isset( $receipt_data['order']['number'] ) ? (string) $receipt_data['order']['number'] : '';
67 $total = isset( $receipt_data['totals']['total_incl'] ) ? (float) $receipt_data['totals']['total_incl'] : 0;
68 $store_name = isset( $receipt_data['store']['name'] ) ? (string) $receipt_data['store']['name'] : get_bloginfo( 'name' );
69 $qr_payload = isset( $receipt_data['fiscal']['qr_payload'] ) ? (string) $receipt_data['fiscal']['qr_payload'] : '';
70
71 $lines = array(
72 self::CMD_INIT,
73 self::CMD_ALIGN_CENTER,
74 $this->sanitize_text( $store_name ),
75 'RECEIPT',
76 self::CMD_ALIGN_LEFT,
77 'Order #' . $this->sanitize_text( $order_number ),
78 'Total ' . wc_format_decimal( $total, wc_get_price_decimals() ),
79 );
80
81 if ( $print_qr && '' !== $qr_payload ) {
82 $lines[] = '[STARPRNT:QR] ' . $this->sanitize_text( $qr_payload );
83 }
84 if ( $open_drawer ) {
85 $lines[] = self::CMD_DRAWER;
86 }
87
88 $lines[] = $partial_cut ? self::CMD_CUT_PARTIAL : self::CMD_CUT_FULL;
89
90 return implode( "\n", $lines ) . "\n";
91 }
92
93 /**
94 * Sanitize text for StarPRNT commands.
95 *
96 * @param string $value Text value.
97 *
98 * @return string
99 */
100 private function sanitize_text( string $value ): string {
101 return trim( preg_replace( '/[\r\n]/', ' ', $value ) );
102 }
103 }
104