| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Order |
| 4 |
* |
| 5 |
* @package Packetery\Module\EntityFactory |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Entity; |
| 13 |
use Packetery\Core\Entity\Address; |
| 14 |
use Packetery\Module\WeightCalculator; |
| 15 |
use Packetery\Module\Carrier; |
| 16 |
use Packetery\Module\Carrier\PacketaPickupPointsConfig; |
| 17 |
use Packetery\Module\Options\Provider; |
| 18 |
use Packetery\Module\Product; |
| 19 |
use WC_Order; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class Order |
| 23 |
* |
| 24 |
* @package Packetery\Module\EntityFactory |
| 25 |
*/ |
| 26 |
class Builder { |
| 27 |
|
| 28 |
/** |
| 29 |
* Options provider. |
| 30 |
* |
| 31 |
* @var Provider Options provider. |
| 32 |
*/ |
| 33 |
private $optionsProvider; |
| 34 |
|
| 35 |
/** |
| 36 |
* Carrier repository. |
| 37 |
* |
| 38 |
* @var Carrier\EntityRepository Carrier repository. |
| 39 |
*/ |
| 40 |
private $carrierRepository; |
| 41 |
|
| 42 |
/** |
| 43 |
* Weight calculator. |
| 44 |
* |
| 45 |
* @var WeightCalculator |
| 46 |
*/ |
| 47 |
private $calculator; |
| 48 |
|
| 49 |
/** |
| 50 |
* Internal pickup points config. |
| 51 |
* |
| 52 |
* @var PacketaPickupPointsConfig |
| 53 |
*/ |
| 54 |
private $pickupPointsConfig; |
| 55 |
|
| 56 |
/** |
| 57 |
* Order constructor. |
| 58 |
* |
| 59 |
* @param Provider $optionsProvider Options Provider. |
| 60 |
* @param Carrier\EntityRepository $carrierRepository Carrier repository. |
| 61 |
* @param WeightCalculator $weightCalculator Weight calculator. |
| 62 |
* @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config. |
| 63 |
*/ |
| 64 |
public function __construct( |
| 65 |
Provider $optionsProvider, |
| 66 |
Carrier\EntityRepository $carrierRepository, |
| 67 |
WeightCalculator $weightCalculator, |
| 68 |
PacketaPickupPointsConfig $pickupPointsConfig |
| 69 |
) { |
| 70 |
$this->optionsProvider = $optionsProvider; |
| 71 |
$this->carrierRepository = $carrierRepository; |
| 72 |
$this->calculator = $weightCalculator; |
| 73 |
$this->pickupPointsConfig = $pickupPointsConfig; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Creates common order entity from WC_Order. |
| 78 |
* |
| 79 |
* @param WC_Order $wcOrder WC_Order. |
| 80 |
* @param Entity\Order $order Partial order. |
| 81 |
* |
| 82 |
* @return Entity\Order |
| 83 |
*/ |
| 84 |
public function finalize( WC_Order $wcOrder, Entity\Order $order ): Entity\Order { |
| 85 |
$order->setCalculatedWeight( $this->calculator->calculateOrderWeight( $wcOrder ) ); |
| 86 |
|
| 87 |
if ( null === $order->containsAdultContent() ) { |
| 88 |
$order->setAdultContent( $this->containsAdultContent( $wcOrder ) ); |
| 89 |
} |
| 90 |
|
| 91 |
$order->setShippingCountry( strtolower( $wcOrder->get_shipping_country() ) ); |
| 92 |
|
| 93 |
$orderData = $wcOrder->get_data(); |
| 94 |
$contactInfo = ( $wcOrder->has_shipping_address() ? $orderData['shipping'] : $orderData['billing'] ); |
| 95 |
|
| 96 |
$order->setName( $contactInfo['first_name'] ); |
| 97 |
$order->setSurname( $contactInfo['last_name'] ); |
| 98 |
$order->setEshop( $this->optionsProvider->get_sender() ); |
| 99 |
|
| 100 |
if ( null === $order->getValue() ) { |
| 101 |
$order->setValue( (float) $wcOrder->get_total( 'raw' ) ); |
| 102 |
} |
| 103 |
|
| 104 |
$address = $order->getDeliveryAddress(); |
| 105 |
if ( null === $address ) { |
| 106 |
$order->setAddressValidated( false ); |
| 107 |
$address = new Address( $contactInfo['address_1'], $contactInfo['city'], $contactInfo['postcode'] ); |
| 108 |
} |
| 109 |
|
| 110 |
$order->setDeliveryAddress( $address ); |
| 111 |
$order->setCustomNumber( $wcOrder->get_order_number() ); |
| 112 |
|
| 113 |
// Shipping address phone is optional. |
| 114 |
$order->setPhone( $orderData['billing']['phone'] ); |
| 115 |
if ( ! empty( $contactInfo['phone'] ) ) { |
| 116 |
$order->setPhone( $contactInfo['phone'] ); |
| 117 |
} |
| 118 |
// Additional address information. |
| 119 |
if ( ! empty( $contactInfo['address_2'] ) ) { |
| 120 |
$order->setNote( $contactInfo['address_2'] ); |
| 121 |
} |
| 122 |
|
| 123 |
$order->setEmail( $orderData['billing']['email'] ); |
| 124 |
$codMethod = $this->optionsProvider->getCodPaymentMethod(); |
| 125 |
if ( null === $order->getCod() && $orderData['payment_method'] === $codMethod ) { |
| 126 |
$order->setCod( $order->getValue() ); |
| 127 |
} |
| 128 |
|
| 129 |
if ( $orderData['payment_method'] !== $codMethod ) { |
| 130 |
$order->setCod( null ); |
| 131 |
} |
| 132 |
|
| 133 |
$order->setSize( $order->getSize() ); |
| 134 |
|
| 135 |
$carrierCode = null; |
| 136 |
if ( $order->isExternalCarrier() ) { |
| 137 |
$carrier = $this->carrierRepository->getById( (int) $order->getCarrierId() ); |
| 138 |
$order->setCarrier( $carrier ); |
| 139 |
$carrierCode = $order->getCarrierId(); |
| 140 |
} else { |
| 141 |
$shippingCountry = $order->getShippingCountry(); |
| 142 |
if ( null !== $shippingCountry ) { |
| 143 |
// TODO: for non-compound split carriers use value from database. See Metabox and PacketSubmitter. |
| 144 |
$carrierCode = $this->pickupPointsConfig->getCompoundCarrierIdByCountry( $shippingCountry ); |
| 145 |
} |
| 146 |
} |
| 147 |
$order->setCarrierCode( $carrierCode ); |
| 148 |
|
| 149 |
$order->setCurrency( $wcOrder->get_currency() ); |
| 150 |
|
| 151 |
return $order; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Finds out if adult content is present. |
| 156 |
* |
| 157 |
* @param WC_Order $wcOrder WC Order. |
| 158 |
* |
| 159 |
* @return bool |
| 160 |
*/ |
| 161 |
private function containsAdultContent( WC_Order $wcOrder ): bool { |
| 162 |
foreach ( $wcOrder->get_items() as $item ) { |
| 163 |
$product = $item->get_product(); |
| 164 |
if ( $product ) { |
| 165 |
$productEntity = new Product\Entity( $product ); |
| 166 |
if ( $productEntity->isPhysical() && $productEntity->isAgeVerification18PlusRequired() ) { |
| 167 |
return true; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|