PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.15
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.15
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 / Epos_Xml_Output_Adapter.php

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

106 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Epson ePOS-Print XML 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 * Epos_Xml_Output_Adapter class.
14 */
15 class Epos_Xml_Output_Adapter implements Receipt_Output_Adapter_Interface {
16 /**
17 * Transform receipt payload to Epson ePOS-Print XML.
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 $store_name = isset( $receipt_data['store']['name'] ) ? (string) $receipt_data['store']['name'] : get_bloginfo( 'name' );
26 $order_number = isset( $receipt_data['order']['number'] ) ? (string) $receipt_data['order']['number'] : '';
27 $total = isset( $receipt_data['totals']['total_incl'] ) ? (float) $receipt_data['totals']['total_incl'] : 0;
28 $lines = array(
29 '<epos-print xmlns="http://www.epson-pos.com/schemas/2011/03/epos-print">',
30 // ePOS <text> attributes are printer state that persists across jobs, so
31 // start from a known reset and switch emphasis off again explicitly.
32 '<text align="left" em="false" ul="false" reverse="false" dw="false" dh="false"/>',
33 '<text align="center" em="true">' . $this->escape( $store_name ) . "\n" . '</text>',
34 '<text align="center" em="false">RECEIPT' . "\n" . '</text>',
35 '<text align="left">Order #' . $this->escape( $order_number ) . "\n" . '</text>',
36 );
37
38 foreach ( $this->line_items( $receipt_data ) as $item ) {
39 $name = isset( $item['name'] ) ? (string) $item['name'] : '';
40 $quantity = isset( $item['quantity'] ) ? (float) $item['quantity'] : (float) ( $item['qty'] ?? 1 );
41 $item_total = isset( $item['total'] ) ? (float) $item['total'] : (float) ( $item['line_total_incl'] ?? 0 );
42 $lines[] = '<text>' . $this->escape( $name ) . ' x' . $this->format_quantity( $quantity ) . ' ' . $this->format_money( $item_total ) . "\n" . '</text>';
43 }
44
45 $lines[] = '<text>Total ' . $this->escape( $this->format_money( $total ) ) . "\n" . '</text>';
46 $lines[] = '<cut type="feed" />';
47 $lines[] = '</epos-print>';
48
49 return implode( '', $lines );
50 }
51
52 /**
53 * Read line items from known canonical keys.
54 *
55 * @param array $receipt_data Canonical payload.
56 *
57 * @return array
58 */
59 private function line_items( array $receipt_data ): array {
60 if ( isset( $receipt_data['lines'] ) && is_array( $receipt_data['lines'] ) ) {
61 return $receipt_data['lines'];
62 }
63 if ( isset( $receipt_data['line_items'] ) && is_array( $receipt_data['line_items'] ) ) {
64 return $receipt_data['line_items'];
65 }
66 if ( isset( $receipt_data['items'] ) && is_array( $receipt_data['items'] ) ) {
67 return $receipt_data['items'];
68 }
69
70 return array();
71 }
72
73 /**
74 * Escape XML text content.
75 *
76 * @param string $value Raw text.
77 *
78 * @return string
79 */
80 private function escape( string $value ): string {
81 return htmlspecialchars( $value, ENT_XML1 | ENT_COMPAT, 'UTF-8' );
82 }
83
84 /**
85 * Format an item quantity for receipt output.
86 *
87 * @param float $quantity Quantity.
88 *
89 * @return string
90 */
91 private function format_quantity( float $quantity ): string {
92 return wc_format_decimal( $quantity, 2 );
93 }
94
95 /**
96 * Format a money amount for receipt output.
97 *
98 * @param float $amount Amount.
99 *
100 * @return string
101 */
102 private function format_money( float $amount ): string {
103 return get_woocommerce_currency() . ' ' . wc_format_decimal( $amount, wc_get_price_decimals() );
104 }
105 }
106