| 1 |
<?php |
| 2 |
/** |
| 3 |
* Order Customer Builder |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Services |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Services\Order\Builder; |
| 10 |
|
| 11 |
use SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\Customer; |
| 12 |
use SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\PreviousOrder; |
| 13 |
use SeQura\Core\BusinessLogic\Domain\Order\OrderStates; |
| 14 |
use SeQura\WC\Core\Extension\BusinessLogic\Domain\OrderStatusSettings\Services\Order_Status_Settings_Service; |
| 15 |
use SeQura\WC\Services\Pricing\Interface_Pricing_Service; |
| 16 |
use SeQura\WC\Services\Shopper\Interface_Shopper_Service; |
| 17 |
use WC_DateTime; |
| 18 |
use WC_Order; |
| 19 |
|
| 20 |
/** |
| 21 |
* Order Customer Builder |
| 22 |
*/ |
| 23 |
class Order_Customer_Builder implements Interface_Order_Customer_Builder { |
| 24 |
|
| 25 |
/** |
| 26 |
* Shopper service |
| 27 |
* |
| 28 |
* @var Interface_Shopper_Service |
| 29 |
*/ |
| 30 |
private $shopper_service; |
| 31 |
|
| 32 |
/** |
| 33 |
* Order status settings service |
| 34 |
* |
| 35 |
* @var Order_Status_Settings_Service |
| 36 |
*/ |
| 37 |
private $order_status_service; |
| 38 |
|
| 39 |
/** |
| 40 |
* Pricing service |
| 41 |
* |
| 42 |
* @var Interface_Pricing_Service |
| 43 |
*/ |
| 44 |
private $pricing_service; |
| 45 |
|
| 46 |
/** |
| 47 |
* Constructor |
| 48 |
*/ |
| 49 |
public function __construct( |
| 50 |
Interface_Shopper_Service $shopper_service, |
| 51 |
Order_Status_Settings_Service $order_status_service, |
| 52 |
Interface_Pricing_Service $pricing_service |
| 53 |
) { |
| 54 |
$this->shopper_service = $shopper_service; |
| 55 |
$this->order_status_service = $order_status_service; |
| 56 |
$this->pricing_service = $pricing_service; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get customer |
| 61 |
*/ |
| 62 |
public function build( ?WC_Order $order, string $lang, int $fallback_user_id = 0, string $fallback_ip = '', string $fallback_user_agent = '' ): Customer { |
| 63 |
$current_user_id = $order ? $order->get_customer_id() : $fallback_user_id; |
| 64 |
$logged_in = $current_user_id > 0; |
| 65 |
$ref = $logged_in ? $current_user_id : null; |
| 66 |
$ip = $order ? $order->get_customer_ip_address( 'edit' ) : $fallback_ip; |
| 67 |
$user_agent = $order ? $order->get_customer_user_agent( 'edit' ) : $fallback_user_agent; |
| 68 |
|
| 69 |
return new Customer( |
| 70 |
$this->shopper_service->get_email( $order ), |
| 71 |
$lang, |
| 72 |
$ip, |
| 73 |
$user_agent, |
| 74 |
$this->shopper_service->get_first_name( $order, true ), |
| 75 |
$this->shopper_service->get_last_name( $order, true ), |
| 76 |
$this->shopper_service->get_shopper_title( $order ), // title. |
| 77 |
$ref, |
| 78 |
$this->shopper_service->get_dob( $order ), // dateOfBirth. |
| 79 |
$this->shopper_service->get_nin( $order ), // nin. |
| 80 |
$this->shopper_service->get_company( $order, true ), |
| 81 |
$this->shopper_service->get_vat( $order, true ), // vatNumber. |
| 82 |
$this->shopper_service->get_shopper_created_at( $order ), // createdAt. |
| 83 |
$this->shopper_service->get_shopper_updated_at( $order ), // updatedAt. |
| 84 |
$this->shopper_service->get_shopper_rating( $order ), // rating. |
| 85 |
null, // ninControl. |
| 86 |
$this->get_previous_orders( $current_user_id ), |
| 87 |
null, // vehicle. |
| 88 |
$logged_in |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get previous orders |
| 94 |
* |
| 95 |
* @return PreviousOrder[] |
| 96 |
*/ |
| 97 |
private function get_previous_orders( int $customer_id ): array { |
| 98 |
if ( $customer_id <= 0 ) { |
| 99 |
return array(); |
| 100 |
} |
| 101 |
|
| 102 |
$order_statuses = $this->order_status_service->getOrderStatusSettings(); |
| 103 |
|
| 104 |
if ( ! $order_statuses ) { |
| 105 |
return array(); |
| 106 |
} |
| 107 |
|
| 108 |
$statuses = array(); |
| 109 |
foreach ( $order_statuses as $order_status ) { |
| 110 |
if ( \in_array( $order_status->getSequraStatus(), array( OrderStates::STATE_APPROVED, OrderStates::STATE_SHIPPED ), true ) ) { |
| 111 |
$statuses[] = $order_status->getShopStatus(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! $statuses ) { |
| 116 |
return array(); |
| 117 |
} |
| 118 |
/** |
| 119 |
* Get previous orders |
| 120 |
* |
| 121 |
* @var WC_Order[] $orders */ |
| 122 |
$orders = \wc_get_orders( |
| 123 |
array( |
| 124 |
'limit' => -1, |
| 125 |
'customer' => $customer_id, |
| 126 |
'status' => $statuses, |
| 127 |
) |
| 128 |
); |
| 129 |
if ( ! \is_array( $orders ) ) { |
| 130 |
return array(); |
| 131 |
} |
| 132 |
$previous_orders = array(); |
| 133 |
foreach ( $orders as $order ) { |
| 134 |
$previous_orders[] = $this->get_previous_order( $order ); |
| 135 |
} |
| 136 |
return $previous_orders; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Build previous order from WC_Order |
| 141 |
*/ |
| 142 |
private function get_previous_order( WC_Order $order ): PreviousOrder { |
| 143 |
/** |
| 144 |
* Order date |
| 145 |
* |
| 146 |
* @var WC_DateTime $date |
| 147 |
*/ |
| 148 |
$date = $order->get_date_created( 'edit' ); |
| 149 |
$postcode = $this->shopper_service->get_postcode( $order ); |
| 150 |
$country = $this->shopper_service->get_country( $order ); |
| 151 |
|
| 152 |
return new PreviousOrder( |
| 153 |
$date ? $date->date( 'c' ) : '', |
| 154 |
$this->pricing_service->to_cents( (float) $order->get_total( 'edit' ) ), |
| 155 |
$order->get_currency(), |
| 156 |
$order->get_status( 'edit' ), |
| 157 |
\wc_get_order_status_name( $order->get_status( 'edit' ) ), |
| 158 |
$order->get_payment_method( 'edit' ), |
| 159 |
$order->get_payment_method_title( 'edit' ), |
| 160 |
$postcode ? $postcode : null, |
| 161 |
$country ? $country : null |
| 162 |
); |
| 163 |
} |
| 164 |
} |
| 165 |
|