| 1 |
<?php |
| 2 |
namespace Timetics\Core\Integrations\Woocommerce; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Status_Mapper |
| 8 |
* |
| 9 |
* Maps booking statuses to WooCommerce order statuses and vice versa |
| 10 |
* |
| 11 |
*/ |
| 12 |
class Status_Mapper { |
| 13 |
|
| 14 |
/** |
| 15 |
* Track currently syncing booking-order pairs in this request |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
private static $syncing_pairs = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Map Timetics booking status to WooCommerce order status |
| 23 |
* |
| 24 |
* Mapping: ( Timetics -> Woocommerce ) |
| 25 |
* |
| 26 |
* - pending -> pending |
| 27 |
* - approved -> processing |
| 28 |
* - completed -> completed |
| 29 |
* - cancel -> cancelled |
| 30 |
* - failed -> failed |
| 31 |
* |
| 32 |
* @param string $booking_status The Timetics booking status |
| 33 |
* @return string The WooCommerce order status (without 'wc-' prefix) |
| 34 |
*/ |
| 35 |
public static function booking_to_order_status( $booking_status ) { |
| 36 |
$mapping = [ |
| 37 |
'pending' => 'pending', |
| 38 |
'approved' => 'processing', |
| 39 |
'completed' => 'completed', |
| 40 |
'cancel' => 'cancelled', |
| 41 |
'failed' => 'failed', |
| 42 |
]; |
| 43 |
|
| 44 |
return isset( $mapping[ $booking_status ] ) ? $mapping[ $booking_status ] : $booking_status; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Map WooCommerce order status to Timetics booking status |
| 49 |
* |
| 50 |
* Mapping: |
| 51 |
* - pending -> pending |
| 52 |
* - processing -> approved |
| 53 |
* - on-hold -> pending |
| 54 |
* - completed -> completed |
| 55 |
* - cancelled -> cancel |
| 56 |
* - refunded -> cancel |
| 57 |
* - failed -> failed |
| 58 |
* |
| 59 |
* @param string $order_status The WooCommerce order status (may or may not include 'wc-' prefix) |
| 60 |
* @return string The Timetics booking status |
| 61 |
*/ |
| 62 |
public static function order_to_booking_status( $order_status ) { |
| 63 |
// Remove 'wc-' prefix if present |
| 64 |
$order_status = str_replace( 'wc-', '', $order_status ); |
| 65 |
|
| 66 |
$mapping = [ |
| 67 |
'pending' => 'pending', |
| 68 |
'processing' => 'approved', |
| 69 |
'on-hold' => 'pending', |
| 70 |
'completed' => 'completed', |
| 71 |
'cancelled' => 'cancel', |
| 72 |
'refunded' => 'cancel', |
| 73 |
'failed' => 'failed', |
| 74 |
]; |
| 75 |
|
| 76 |
return isset( $mapping[ $order_status ] ) ? $mapping[ $order_status ] : $order_status; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Check if a status transition should trigger sync |
| 81 |
* |
| 82 |
* @param string $from_status The previous status |
| 83 |
* @param string $to_status The new status |
| 84 |
* |
| 85 |
* @return bool Whether the transition should be synced |
| 86 |
*/ |
| 87 |
public static function should_sync_status( $from_status, $to_status ) { |
| 88 |
if ( $from_status === $to_status ) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the WooCommerce order ID from booking meta |
| 97 |
* |
| 98 |
* @param int $booking_id The booking ID |
| 99 |
* |
| 100 |
* @return int|false The WooCommerce order ID or false if not found |
| 101 |
*/ |
| 102 |
public static function get_order_id_from_booking( $booking_id ) { |
| 103 |
return get_post_meta( $booking_id, '_tt_wc_order_id', true ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Store the WooCommerce order ID in booking meta |
| 108 |
* |
| 109 |
* @param int $booking_id The booking ID |
| 110 |
* @param int $order_id The WooCommerce order ID |
| 111 |
* |
| 112 |
* @return bool Whether the meta was updated |
| 113 |
*/ |
| 114 |
public static function set_order_id_for_booking( $booking_id, $order_id ) { |
| 115 |
return update_post_meta( $booking_id, '_tt_wc_order_id', $order_id ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get the booking ID from WooCommerce order meta |
| 120 |
* |
| 121 |
* @param int $order_id The WooCommerce order ID |
| 122 |
* @return int|false The booking ID or false if not found |
| 123 |
*/ |
| 124 |
public static function get_booking_id_from_order( $order_id ) { |
| 125 |
return get_post_meta( $order_id, '_tt_booking_id', true ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Store the booking ID in WooCommerce order meta |
| 130 |
* |
| 131 |
* @param int $order_id The WooCommerce order ID |
| 132 |
* @param int $booking_id The booking ID |
| 133 |
* @return bool Whether the meta was updated |
| 134 |
*/ |
| 135 |
public static function set_booking_id_for_order( $order_id, $booking_id ) { |
| 136 |
return update_post_meta( $order_id, '_tt_booking_id', $booking_id ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Check if a booking-order pair is currently being synced |
| 141 |
* |
| 142 |
* @param int $booking_id The booking ID |
| 143 |
* @param int $order_id The WooCommerce order ID |
| 144 |
* |
| 145 |
* @return bool Whether this pair is currently syncing |
| 146 |
*/ |
| 147 |
public static function is_syncing_pair( $booking_id, $order_id ) { |
| 148 |
$pair_key = "{$booking_id}_{$order_id}"; |
| 149 |
return isset( self::$syncing_pairs[ $pair_key ] ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Mark a booking-order pair as currently syncing |
| 154 |
* |
| 155 |
* @param int $booking_id The booking ID |
| 156 |
* @param int $order_id The WooCommerce order ID |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public static function begin_sync_pair( $booking_id, $order_id ) { |
| 160 |
$pair_key = "{$booking_id}_{$order_id}"; |
| 161 |
self::$syncing_pairs[ $pair_key ] = true; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Mark a booking-order pair as finished syncing |
| 166 |
* |
| 167 |
* @param int $booking_id The booking ID |
| 168 |
* @param int $order_id The WooCommerce order ID |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public static function end_sync_pair( $booking_id, $order_id ) { |
| 172 |
$pair_key = "{$booking_id}_{$order_id}"; |
| 173 |
unset( self::$syncing_pairs[ $pair_key ] ); |
| 174 |
} |
| 175 |
} |
| 176 |
|