PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.0
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.0
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.0, at includes/Templates/Adapters/Epos_Xml_Output_Adapter.php

103 lines 3.3 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 '<text align="center" em="true">' . $this->escape( $store_name ) . "\n" . '</text>',
31 '<text align="center">RECEIPT' . "\n" . '</text>',
32 '<text align="left">Order #' . $this->escape( $order_number ) . "\n" . '</text>',
33 );
34
35 foreach ( $this->line_items( $receipt_data ) as $item ) {
36 $name = isset( $item['name'] ) ? (string) $item['name'] : '';
37 $quantity = isset( $item['quantity'] ) ? (float) $item['quantity'] : (float) ( $item['qty'] ?? 1 );
38 $item_total = isset( $item['total'] ) ? (float) $item['total'] : (float) ( $item['line_total_incl'] ?? 0 );
39 $lines[] = '<text>' . $this->escape( $name ) . ' x' . $this->format_quantity( $quantity ) . ' ' . $this->format_money( $item_total ) . "\n" . '</text>';
40 }
41
42 $lines[] = '<text>Total ' . $this->escape( $this->format_money( $total ) ) . "\n" . '</text>';
43 $lines[] = '<cut type="feed" />';
44 $lines[] = '</epos-print>';
45
46 return implode( '', $lines );
47 }
48
49 /**
50 * Read line items from known canonical keys.
51 *
52 * @param array $receipt_data Canonical payload.
53 *
54 * @return array
55 */
56 private function line_items( array $receipt_data ): array {
57 if ( isset( $receipt_data['lines'] ) && is_array( $receipt_data['lines'] ) ) {
58 return $receipt_data['lines'];
59 }
60 if ( isset( $receipt_data['line_items'] ) && is_array( $receipt_data['line_items'] ) ) {
61 return $receipt_data['line_items'];
62 }
63 if ( isset( $receipt_data['items'] ) && is_array( $receipt_data['items'] ) ) {
64 return $receipt_data['items'];
65 }
66
67 return array();
68 }
69
70 /**
71 * Escape XML text content.
72 *
73 * @param string $value Raw text.
74 *
75 * @return string
76 */
77 private function escape( string $value ): string {
78 return htmlspecialchars( $value, ENT_XML1 | ENT_COMPAT, 'UTF-8' );
79 }
80
81 /**
82 * Format an item quantity for receipt output.
83 *
84 * @param float $quantity Quantity.
85 *
86 * @return string
87 */
88 private function format_quantity( float $quantity ): string {
89 return wc_format_decimal( $quantity, 2 );
90 }
91
92 /**
93 * Format a money amount for receipt output.
94 *
95 * @param float $amount Amount.
96 *
97 * @return string
98 */
99 private function format_money( float $amount ): string {
100 return get_woocommerce_currency() . ' ' . wc_format_decimal( $amount, wc_get_price_decimals() );
101 }
102 }
103