| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shared receipt store setting resolver. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Services |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services; |
| 9 |
|
| 10 |
use DateTimeZone; |
| 11 |
|
| 12 |
/** |
| 13 |
* Receipt_Store_Resolver class. |
| 14 |
*/ |
| 15 |
class Receipt_Store_Resolver { |
| 16 |
/** |
| 17 |
* POS store object. |
| 18 |
* |
| 19 |
* @var object |
| 20 |
*/ |
| 21 |
private $pos_store; |
| 22 |
|
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
* |
| 26 |
* @param object $pos_store POS store object. |
| 27 |
*/ |
| 28 |
public function __construct( $pos_store ) { |
| 29 |
$this->pos_store = $pos_store; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Safely read a value from a POS store object. |
| 34 |
* |
| 35 |
* @param string $getter Getter method name. |
| 36 |
* @param mixed $fallback Fallback value. |
| 37 |
* |
| 38 |
* @return mixed |
| 39 |
*/ |
| 40 |
public function get_store_value( string $getter, $fallback ) { |
| 41 |
if ( ! \is_object( $this->pos_store ) || ! method_exists( $this->pos_store, $getter ) ) { |
| 42 |
return $fallback; |
| 43 |
} |
| 44 |
|
| 45 |
return $this->pos_store->{$getter}(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Resolve a string setting from the store with a WooCommerce fallback. |
| 50 |
* |
| 51 |
* Empty strings are preserved as explicit overrides. |
| 52 |
* |
| 53 |
* @param string $getter Store getter method. |
| 54 |
* @param mixed $fallback Fallback value. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function resolve_store_string( string $getter, $fallback ): string { |
| 59 |
$value = $this->get_store_value( $getter, null ); |
| 60 |
|
| 61 |
return null !== $value ? (string) $value : (string) $fallback; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Resolve an enum-like store setting with a WooCommerce fallback. |
| 66 |
* |
| 67 |
* Empty strings fall back because these values must be valid option tokens. |
| 68 |
* |
| 69 |
* @param string $getter Store getter method. |
| 70 |
* @param mixed $fallback Fallback value. |
| 71 |
* |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public function resolve_store_option_string( string $getter, $fallback ): string { |
| 75 |
$value = $this->get_store_value( $getter, null ); |
| 76 |
|
| 77 |
return null !== $value && '' !== (string) $value ? (string) $value : (string) $fallback; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Resolve the receipt timezone from the store, falling back to the site timezone. |
| 82 |
* |
| 83 |
* @return DateTimeZone |
| 84 |
*/ |
| 85 |
public function resolve_store_timezone(): DateTimeZone { |
| 86 |
$timezone = (string) $this->get_store_value( 'get_timezone', '' ); |
| 87 |
|
| 88 |
if ( '' !== $timezone ) { |
| 89 |
try { |
| 90 |
return new DateTimeZone( $timezone ); |
| 91 |
} catch ( \Exception $error ) { |
| 92 |
return wp_timezone(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return wp_timezone(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Resolve the store locale with site fallback. |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
public function resolve_locale(): string { |
| 105 |
$store_locale = (string) $this->get_store_value( 'get_locale', '' ); |
| 106 |
|
| 107 |
return '' !== $store_locale ? $store_locale : get_locale(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Resolve the number of price decimals from the store with WC fallback. |
| 112 |
* |
| 113 |
* @return int |
| 114 |
*/ |
| 115 |
public function resolve_price_num_decimals(): int { |
| 116 |
$value = $this->get_store_value( 'get_price_number_of_decimals', wc_get_price_decimals() ); |
| 117 |
|
| 118 |
return is_numeric( $value ) && (float) $value >= 0 ? (int) $value : wc_get_price_decimals(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Build template-facing tax mode signals. |
| 123 |
* |
| 124 |
* @return array<string,mixed> |
| 125 |
*/ |
| 126 |
public function build_tax_section(): array { |
| 127 |
$display = 'incl' === $this->resolve_store_option_string( |
| 128 |
'get_tax_display_cart', |
| 129 |
get_option( 'woocommerce_tax_display_cart', 'excl' ) |
| 130 |
) ? 'incl' : 'excl'; |
| 131 |
|
| 132 |
$tax_enabled = 'yes' === $this->resolve_store_option_string( |
| 133 |
'get_calc_taxes', |
| 134 |
get_option( 'woocommerce_calc_taxes', 'no' ) |
| 135 |
); |
| 136 |
$breakdown = $tax_enabled ? $this->resolve_store_option_string( |
| 137 |
'get_tax_total_display', |
| 138 |
get_option( 'woocommerce_tax_total_display', 'itemized' ) |
| 139 |
) : 'hidden'; |
| 140 |
|
| 141 |
if ( ! in_array( $breakdown, array( 'hidden', 'single', 'itemized' ), true ) ) { |
| 142 |
$breakdown = 'itemized'; |
| 143 |
} |
| 144 |
|
| 145 |
return array( |
| 146 |
'display' => $display, |
| 147 |
'display_incl' => 'incl' === $display, |
| 148 |
'display_excl' => 'excl' === $display, |
| 149 |
'breakdown' => $breakdown, |
| 150 |
'breakdown_hidden' => 'hidden' === $breakdown, |
| 151 |
'breakdown_single' => 'single' === $breakdown, |
| 152 |
'breakdown_itemized' => 'itemized' === $breakdown, |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Build price, currency, and locale presentation hints for renderers. |
| 158 |
* |
| 159 |
* @param string $currency Currency code. |
| 160 |
* @param bool|null $prices_include_tax Optional precomputed prices-include-tax flag. |
| 161 |
* |
| 162 |
* @return array<string,mixed> |
| 163 |
*/ |
| 164 |
public function build_presentation_hints( string $currency, ?bool $prices_include_tax = null ): array { |
| 165 |
if ( null === $prices_include_tax ) { |
| 166 |
$prices_include_tax = 'yes' === $this->resolve_store_option_string( |
| 167 |
'get_prices_include_tax', |
| 168 |
wc_prices_include_tax() ? 'yes' : 'no' |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
return array( |
| 173 |
'prices_entered_with_tax' => $prices_include_tax, |
| 174 |
'rounding_mode' => $this->resolve_store_option_string( |
| 175 |
'get_tax_round_at_subtotal', |
| 176 |
get_option( 'woocommerce_tax_round_at_subtotal', 'no' ) |
| 177 |
), |
| 178 |
'locale' => $this->resolve_locale(), |
| 179 |
'timezone' => $this->resolve_store_timezone()->getName(), |
| 180 |
'currency_position' => $this->resolve_store_option_string( |
| 181 |
'get_currency_position', |
| 182 |
get_option( 'woocommerce_currency_pos', 'left' ) |
| 183 |
), |
| 184 |
'currency_symbol' => get_woocommerce_currency_symbol( $currency ), |
| 185 |
'price_thousand_separator' => $this->resolve_store_string( |
| 186 |
'get_price_thousand_separator', |
| 187 |
wc_get_price_thousand_separator() |
| 188 |
), |
| 189 |
'price_decimal_separator' => $this->resolve_store_string( |
| 190 |
'get_price_decimal_separator', |
| 191 |
wc_get_price_decimal_separator() |
| 192 |
), |
| 193 |
'price_num_decimals' => $this->resolve_price_num_decimals(), |
| 194 |
'price_display_suffix' => $this->resolve_store_string( |
| 195 |
'get_price_display_suffix', |
| 196 |
get_option( 'woocommerce_price_display_suffix', '' ) |
| 197 |
), |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Compose `address_lines[]` using the country's WC address format. |
| 203 |
* |
| 204 |
* @param array<string,string> $fields Store address fields. |
| 205 |
* |
| 206 |
* @return array<int,string> |
| 207 |
*/ |
| 208 |
public static function compose_address_lines( array $fields ): array { |
| 209 |
$country = isset( $fields['country'] ) ? (string) $fields['country'] : ''; |
| 210 |
$formatted = WC()->countries->get_formatted_address( |
| 211 |
array( |
| 212 |
'first_name' => '', |
| 213 |
'last_name' => '', |
| 214 |
'company' => '', |
| 215 |
'address_1' => $fields['address_1'] ?? '', |
| 216 |
'address_2' => $fields['address_2'] ?? '', |
| 217 |
'city' => $fields['city'] ?? '', |
| 218 |
'state' => $fields['state'] ?? '', |
| 219 |
'postcode' => $fields['postcode'] ?? '', |
| 220 |
'country' => $country, |
| 221 |
), |
| 222 |
"\n" |
| 223 |
); |
| 224 |
|
| 225 |
$lines = preg_split( '/\r?\n/', (string) $formatted ); |
| 226 |
if ( ! is_array( $lines ) ) { |
| 227 |
return array(); |
| 228 |
} |
| 229 |
|
| 230 |
return array_values( |
| 231 |
array_filter( |
| 232 |
array_map( 'trim', $lines ), |
| 233 |
static function ( string $line ): bool { |
| 234 |
return '' !== $line; |
| 235 |
} |
| 236 |
) |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Ensure store tax IDs include display labels for logicless templates. |
| 242 |
* |
| 243 |
* @param array<int,array<string,mixed>> $tax_ids Store tax IDs. |
| 244 |
* @param string $locale Receipt locale. |
| 245 |
* @return array<int,array<string,mixed>> |
| 246 |
*/ |
| 247 |
public static function with_store_tax_id_labels( array $tax_ids, string $locale = '' ): array { |
| 248 |
$labels = Receipt_I18n_Labels::get_labels( $locale ); |
| 249 |
|
| 250 |
return array_map( |
| 251 |
static function ( array $tax_id ) use ( $labels ): array { |
| 252 |
if ( ! empty( $tax_id['label'] ) ) { |
| 253 |
return $tax_id; |
| 254 |
} |
| 255 |
|
| 256 |
$type = isset( $tax_id['type'] ) ? (string) $tax_id['type'] : 'other'; |
| 257 |
$key = 'store_tax_id_label_' . $type; |
| 258 |
$tax_id['label'] = $labels[ $key ] ?? $labels['store_tax_id_label_other']; |
| 259 |
|
| 260 |
return $tax_id; |
| 261 |
}, |
| 262 |
$tax_ids |
| 263 |
); |
| 264 |
} |
| 265 |
} |
| 266 |
|