| 1 |
<?php |
| 2 |
/** |
| 3 |
* POS order audit notes. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Services |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services; |
| 9 |
|
| 10 |
use WC_Order; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Resolves audit-note labels and adds consistently attributed order notes. |
| 18 |
*/ |
| 19 |
class Order_Notes { |
| 20 |
// phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag -- Concise helper summaries and signatures document the parameters. |
| 21 |
/** Resolve a cashier display name. */ |
| 22 |
public static function cashier_name( $user_id ): string { |
| 23 |
$user = get_userdata( (int) $user_id ); |
| 24 |
return $user ? $user->display_name : /* translators: Fallback cashier name shown when the POS cashier user cannot be found. */ __( 'Unknown', 'woocommerce-pos' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** Resolve a customer display name. */ |
| 28 |
public static function customer_name( $customer_id ): string { |
| 29 |
$customer_id = (int) $customer_id; |
| 30 |
if ( 0 === $customer_id ) { |
| 31 |
return /* translators: Customer name shown in an order note for a guest checkout. */ __( 'Guest', 'woocommerce-pos' ); |
| 32 |
} |
| 33 |
$user = get_userdata( $customer_id ); |
| 34 |
return $user ? $user->display_name : sprintf( '#%d', $customer_id ); |
| 35 |
} |
| 36 |
|
| 37 |
/** Resolve a free or Pro store display name. */ |
| 38 |
public static function store_name( $store_id ): string { |
| 39 |
if ( ! is_scalar( $store_id ) || '' === (string) $store_id || ( is_numeric( $store_id ) && (int) $store_id <= 0 ) ) { |
| 40 |
return Store_Defaults::name(); |
| 41 |
} |
| 42 |
if ( is_numeric( $store_id ) ) { |
| 43 |
$store_id = (int) $store_id; |
| 44 |
$store = get_post( $store_id ); |
| 45 |
if ( $store && 'wcpos_store' === $store->post_type ) { |
| 46 |
return $store->post_title; |
| 47 |
} |
| 48 |
return sprintf( |
| 49 |
/* translators: %d: POS store post ID. */ |
| 50 |
__( 'Store #%d', 'woocommerce-pos' ), |
| 51 |
$store_id |
| 52 |
); |
| 53 |
} |
| 54 |
return (string) $store_id; |
| 55 |
} |
| 56 |
|
| 57 |
/** Add the POS creation audit note. */ |
| 58 |
public static function add_creation_note( WC_Order $order, $cashier_id, $store_id ): void { |
| 59 |
$note = sprintf( |
| 60 |
/* translators: 1: POS cashier, 2: POS store. */ |
| 61 |
__( 'Order created via POS by %1$s at %2$s.', 'woocommerce-pos' ), |
| 62 |
self::cashier_name( $cashier_id ), |
| 63 |
self::store_name( $store_id ) |
| 64 |
); |
| 65 |
$order->add_order_note( $note, 0, true ); |
| 66 |
} |
| 67 |
|
| 68 |
/** Add the cashier reassignment audit note. */ |
| 69 |
public static function add_cashier_change_note( WC_Order $order, $old_user_id, $new_user_id ): void { |
| 70 |
$note = sprintf( |
| 71 |
/* translators: 1: old POS cashier, 2: new POS cashier. */ |
| 72 |
__( 'POS cashier changed from %1$s to %2$s.', 'woocommerce-pos' ), |
| 73 |
self::cashier_name( $old_user_id ), |
| 74 |
self::cashier_name( $new_user_id ) |
| 75 |
); |
| 76 |
$order->add_order_note( $note, 0, true ); |
| 77 |
} |
| 78 |
|
| 79 |
/** Add the store reassignment audit note. */ |
| 80 |
public static function add_store_change_note( WC_Order $order, $old_store_id, $new_store_id ): void { |
| 81 |
$note = sprintf( |
| 82 |
/* translators: 1: old POS store, 2: new POS store. */ |
| 83 |
__( 'POS store changed from %1$s to %2$s.', 'woocommerce-pos' ), |
| 84 |
self::store_name( $old_store_id ), |
| 85 |
self::store_name( $new_store_id ) |
| 86 |
); |
| 87 |
$order->add_order_note( $note, 0, true ); |
| 88 |
} |
| 89 |
|
| 90 |
/** Add the POS re-open audit note. */ |
| 91 |
public static function add_reopen_note( WC_Order $order, $cashier_id, $store_id ): void { |
| 92 |
$note = sprintf( |
| 93 |
/* translators: 1: POS cashier, 2: POS store. */ |
| 94 |
__( 'Order re-opened via POS by %1$s at %2$s.', 'woocommerce-pos' ), |
| 95 |
self::cashier_name( $cashier_id ), |
| 96 |
self::store_name( $store_id ) |
| 97 |
); |
| 98 |
$order->add_order_note( $note, 0, true ); |
| 99 |
} |
| 100 |
|
| 101 |
/** Add the POS customer reassignment audit note. */ |
| 102 |
public static function add_pos_customer_change_note( WC_Order $order, $old_customer_id, $new_customer_id ): void { |
| 103 |
$note = sprintf( |
| 104 |
/* translators: 1: old customer, 2: new customer. */ |
| 105 |
__( 'Customer changed from %1$s to %2$s via WCPOS.', 'woocommerce-pos' ), |
| 106 |
self::customer_name( $old_customer_id ), |
| 107 |
self::customer_name( $new_customer_id ) |
| 108 |
); |
| 109 |
$order->add_order_note( $note, 0, true ); |
| 110 |
} |
| 111 |
|
| 112 |
/** Add the admin customer reassignment audit note. */ |
| 113 |
public static function add_admin_customer_change_note( WC_Order $order, $old_customer_id, $new_customer_id ): void { |
| 114 |
$note = sprintf( |
| 115 |
/* translators: 1: old customer, 2: new customer. */ |
| 116 |
__( 'Customer changed from %1$s to %2$s.', 'woocommerce-pos' ), |
| 117 |
self::customer_name( $old_customer_id ), |
| 118 |
self::customer_name( $new_customer_id ) |
| 119 |
); |
| 120 |
$order->add_order_note( $note, 0, true ); |
| 121 |
} |
| 122 |
|
| 123 |
/** Add the cash tender audit note. */ |
| 124 |
public static function add_cash_note( WC_Order $order, $tendered, $change ): void { |
| 125 |
// Format against the order's own currency, not the site default — the two can differ under |
| 126 |
// multi-currency, per-store currency, or simply after the shop currency setting was changed. |
| 127 |
$price_args = array( 'currency' => $order->get_currency() ); |
| 128 |
$note = sprintf( |
| 129 |
/* translators: 1: cash amount tendered, 2: change given. */ |
| 130 |
__( 'Cash payment received — amount tendered: %1$s, change given: %2$s.', 'woocommerce-pos' ), |
| 131 |
wc_price( $tendered, $price_args ), |
| 132 |
wc_price( $change, $price_args ) |
| 133 |
); |
| 134 |
$order->add_order_note( $note, 0, true ); |
| 135 |
} |
| 136 |
} |
| 137 |
|