PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/Order/Builder.php +208 -65 1.5.42.1 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2 /**
3 - * Class Order
3 + * Class Builder
4 4 *
5 - * @package Packetery\Module\EntityFactory
5 + * @package Packetery
6 6 */
7 7
8 8 declare( strict_types=1 );
9 9
@@ -8,21 +8,35 @@
8 8 declare( strict_types=1 );
9 9
10 10 namespace Packetery\Module\Order;
11 11
12 +use DateTimeImmutable;
13 +use DateTimeZone;
14 +use Packetery\Core\CoreHelper;
12 15 use Packetery\Core\Entity;
13 16 use Packetery\Core\Entity\Address;
14 -use Packetery\Module\WeightCalculator;
17 +use Packetery\Core\Entity\Order;
18 +use Packetery\Core\Entity\PickupPoint;
19 +use Packetery\Core\Entity\Size;
15 20 use Packetery\Module\Carrier;
16 21 use Packetery\Module\Carrier\PacketaPickupPointsConfig;
17 -use Packetery\Module\Options\Provider;
22 +use Packetery\Module\CustomsDeclaration;
23 +use Packetery\Module\Exception\InvalidCarrierException;
24 +use Packetery\Module\ModuleHelper;
25 +use Packetery\Module\Options\OptionsProvider;
26 +use Packetery\Module\Payment\PaymentHelper;
18 27 use Packetery\Module\Product;
28 +use Packetery\Module\WeightCalculator;
29 +use stdClass;
19 30 use WC_Order;
31 +use WC_Order_Item_Product;
32 +use WC_Product;
20 33
34 +// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
21 35 /**
22 - * Class Order
36 + * Class Builder
23 37 *
24 - * @package Packetery\Module\EntityFactory
38 + * @package Packetery
25 39 */
26 40 class Builder {
27 41
28 42 /**
@@ -27,25 +41,25 @@
27 41
28 42 /**
29 43 * Options provider.
30 44 *
31 - * @var Provider Options provider.
45 + * @var OptionsProvider Options provider.
32 46 */
33 47 private $optionsProvider;
34 48
35 49 /**
36 - * Carrier repository.
50 + * Weight calculator.
37 51 *
38 - * @var Carrier\EntityRepository Carrier repository.
52 + * @var WeightCalculator
39 53 */
40 - private $carrierRepository;
54 + private $calculator;
41 55
42 56 /**
43 - * Weight calculator.
57 + * Customs declaration repository.
44 58 *
45 - * @var WeightCalculator
59 + * @var CustomsDeclaration\Repository
46 60 */
47 - private $calculator;
61 + private $customsDeclarationRepository;
48 62
49 63 /**
50 64 * Internal pickup points config.
51 65 *
@@ -53,43 +67,156 @@
53 67 */
54 68 private $pickupPointsConfig;
55 69
56 70 /**
57 - * Order constructor.
71 + * CoreHelper.
58 72 *
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.
73 + * @var CoreHelper
63 74 */
75 + private $coreHelper;
76 +
77 + /**
78 + * Payment helper.
79 + *
80 + * @var PaymentHelper
81 + */
82 + private $paymentHelper;
83 +
84 + /**
85 + * Carrier repository.
86 + *
87 + * @var Carrier\EntityRepository
88 + */
89 + private $carrierRepository;
90 +
91 + /**
92 + * Builder constructor.
93 + *
94 + * @param OptionsProvider $optionsProvider Options Provider.
95 + * @param WeightCalculator $calculator Weight calculator.
96 + * @param CustomsDeclaration\Repository $customsDeclarationRepository Customs declaration repository.
97 + * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config.
98 + * @param CoreHelper $coreHelper CoreHelper.
99 + * @param PaymentHelper $paymentHelper Payment helper.
100 + * @param Carrier\EntityRepository $carrierRepository Carrier repository.
101 + */
64 102 public function __construct(
65 - Provider $optionsProvider,
66 - Carrier\EntityRepository $carrierRepository,
67 - WeightCalculator $weightCalculator,
68 - PacketaPickupPointsConfig $pickupPointsConfig
103 + OptionsProvider $optionsProvider,
104 + WeightCalculator $calculator,
105 + CustomsDeclaration\Repository $customsDeclarationRepository,
106 + PacketaPickupPointsConfig $pickupPointsConfig,
107 + CoreHelper $coreHelper,
108 + PaymentHelper $paymentHelper,
109 + Carrier\EntityRepository $carrierRepository
69 110 ) {
70 - $this->optionsProvider = $optionsProvider;
71 - $this->carrierRepository = $carrierRepository;
72 - $this->calculator = $weightCalculator;
73 - $this->pickupPointsConfig = $pickupPointsConfig;
111 + $this->optionsProvider = $optionsProvider;
112 + $this->calculator = $calculator;
113 + $this->customsDeclarationRepository = $customsDeclarationRepository;
114 + $this->pickupPointsConfig = $pickupPointsConfig;
115 + $this->coreHelper = $coreHelper;
116 + $this->paymentHelper = $paymentHelper;
117 + $this->carrierRepository = $carrierRepository;
74 118 }
75 119
76 120 /**
77 - * Creates common order entity from WC_Order.
121 + * Creates order entity from WC_Order and plugin data.
78 122 *
79 - * @param WC_Order $wcOrder WC_Order.
80 - * @param Entity\Order $order Partial order.
123 + * @param WC_Order $wcOrder WC_Order.
124 + * @param stdClass $result Db result, plugin specific data.
81 125 *
82 126 * @return Entity\Order
127 + * @throws InvalidCarrierException In case Carrier entity could not be created.
83 128 */
84 - public function finalize( WC_Order $wcOrder, Entity\Order $order ): Entity\Order {
129 + public function build( WC_Order $wcOrder, stdClass $result ): Entity\Order {
130 + $country = ModuleHelper::getWcOrderCountry( $wcOrder );
131 + if ( $country === '' ) {
132 + throw new InvalidCarrierException( __( 'Please set the country of the delivery address first.', 'packeta' ) );
133 + }
134 +
135 + $carrierId = $this->pickupPointsConfig->getFixedCarrierId( $result->carrier_id, $country );
136 + $carrier = $this->carrierRepository->getAnyById( $carrierId );
137 + if ( $carrier === null ) {
138 + throw new InvalidCarrierException(
139 + sprintf(
140 + // translators: %s is carrier id.
141 + __( 'Order carrier is invalid (%s). Please contact Packeta support.', 'packeta' ),
142 + $carrierId
143 + )
144 + );
145 + }
146 +
147 + $order = new Order( $result->id, $carrier );
148 + $orderWeight = $this->parseFloat( $result->weight );
149 + if ( $orderWeight !== null ) {
150 + $order->setWeight( $orderWeight );
151 + }
152 + $order->setPacketId( $result->packet_id );
153 + $order->setPacketTrackingUrl( $this->coreHelper->getTrackingUrl( $result->packet_id ) );
154 + $order->setPacketClaimId( $result->packet_claim_id );
155 + $order->setPacketClaimTrackingUrl( $this->coreHelper->getTrackingUrl( $result->packet_claim_id ) );
156 + $order->setPacketClaimPassword( $result->packet_claim_password );
157 + $order->setSize( new Size( $this->parseFloat( $result->length ), $this->parseFloat( $result->width ), $this->parseFloat( $result->height ) ) );
158 + $order->setIsExported( (bool) $result->is_exported );
159 + $order->setIsLabelPrinted( (bool) $result->is_label_printed );
160 + $order->setCarrierNumber( $result->carrier_number );
161 + $order->setPacketStatus( $result->packet_status );
162 + $order->setStoredUntil( $this->coreHelper->getDateTimeFromString( $result->stored_until ) );
163 + $order->setAddressValidated( (bool) $result->address_validated );
164 + $order->setAdultContent( $this->parseBool( $result->adult_content ) );
165 + $order->setManualValue( $this->parseFloat( $result->value ) );
166 + $order->setManualCod( $this->parseFloat( $result->cod ) );
167 + $order->setDeliverOn( $this->coreHelper->getDateTimeFromString( $result->deliver_on ) );
168 + $order->setLastApiErrorMessage( $result->api_error_message );
169 + $order->setLastApiErrorDateTime(
170 + ( $result->api_error_date === null )
171 + ? null
172 + : DateTimeImmutable::createFromFormat(
173 + CoreHelper::MYSQL_DATETIME_FORMAT,
174 + $result->api_error_date,
175 + new DateTimeZone( 'UTC' )
176 + )->setTimezone( wp_timezone() )
177 + );
178 +
179 + if ( $result->delivery_address ) {
180 + $deliveryAddressDecoded = json_decode( $result->delivery_address, false );
181 + $deliveryAddress = new Address(
182 + $deliveryAddressDecoded->street,
183 + $deliveryAddressDecoded->city,
184 + $deliveryAddressDecoded->zip
185 + );
186 +
187 + $deliveryAddress->setHouseNumber( $deliveryAddressDecoded->houseNumber );
188 + $deliveryAddress->setLongitude( $deliveryAddressDecoded->longitude );
189 + $deliveryAddress->setLatitude( $deliveryAddressDecoded->latitude );
190 + $deliveryAddress->setCounty( $deliveryAddressDecoded->county );
191 +
192 + $order->setDeliveryAddress( $deliveryAddress );
193 + }
194 +
195 + if ( $result->car_delivery_id ) {
196 + $order->setCarDeliveryId( $result->car_delivery_id );
197 + }
198 +
199 + if ( $result->point_id !== null ) {
200 + $pickUpPoint = new PickupPoint(
201 + $result->point_id,
202 + $result->point_name,
203 + $result->point_city,
204 + $result->point_zip,
205 + $result->point_street,
206 + $result->point_url
207 + );
208 +
209 + $order->setPickupPoint( $pickUpPoint );
210 + }
211 +
85 212 $order->setCalculatedWeight( $this->calculator->calculateOrderWeight( $wcOrder ) );
86 213
87 - if ( null === $order->containsAdultContent() ) {
214 + if ( $order->containsAdultContent() === null ) {
88 215 $order->setAdultContent( $this->containsAdultContent( $wcOrder ) );
89 216 }
90 217
91 - $order->setShippingCountry( strtolower( $wcOrder->get_shipping_country() ) );
218 + $order->setShippingCountry( ModuleHelper::getWcOrderCountry( $wcOrder ) );
92 219
93 220 $orderData = $wcOrder->get_data();
94 221 $contactInfo = ( $wcOrder->has_shipping_address() ? $orderData['shipping'] : $orderData['billing'] );
95 222
@@ -96,14 +223,12 @@
96 223 $order->setName( $contactInfo['first_name'] );
97 224 $order->setSurname( $contactInfo['last_name'] );
98 225 $order->setEshop( $this->optionsProvider->get_sender() );
99 226
100 - if ( null === $order->getValue() ) {
101 - $order->setValue( (float) $wcOrder->get_total( 'raw' ) );
102 - }
227 + $order->setCalculatedValue( (float) $wcOrder->get_total( 'raw' ) );
103 228
104 229 $address = $order->getDeliveryAddress();
105 - if ( null === $address ) {
230 + if ( $address === null ) {
106 231 $order->setAddressValidated( false );
107 232 $address = new Address( $contactInfo['address_1'], $contactInfo['city'], $contactInfo['postcode'] );
108 233 }
109 234
@@ -111,44 +236,31 @@
111 236 $order->setCustomNumber( $wcOrder->get_order_number() );
112 237
113 238 // Shipping address phone is optional.
114 239 $order->setPhone( $orderData['billing']['phone'] );
115 - if ( ! empty( $contactInfo['phone'] ) ) {
240 + if ( isset( $contactInfo['phone'] ) && $contactInfo['phone'] !== '' ) {
116 241 $order->setPhone( $contactInfo['phone'] );
117 242 }
118 243 // Additional address information.
119 - if ( ! empty( $contactInfo['address_2'] ) ) {
244 + if ( isset( $contactInfo['address_2'] ) && $contactInfo['address_2'] !== '' ) {
120 245 $order->setNote( $contactInfo['address_2'] );
121 246 }
122 247
123 248 $order->setEmail( $orderData['billing']['email'] );
124 - $codMethod = $this->optionsProvider->getCodPaymentMethod();
125 - if ( null === $order->getCod() && $orderData['payment_method'] === $codMethod ) {
126 - $order->setCod( $order->getValue() );
249 + $hasCodPaymentMethod = $this->paymentHelper->isCodPaymentMethod( $orderData['payment_method'] );
250 + if ( $hasCodPaymentMethod ) {
251 + $order->setCalculatedCod( $order->getCalculatedValue() );
252 + // manual COD is set from DB directly
253 + } else {
254 + $order->setCalculatedCod( null );
255 + $order->setManualCod( null );
127 256 }
128 257
129 - if ( $orderData['payment_method'] !== $codMethod ) {
130 - $order->setCod( null );
131 - }
132 -
133 258 $order->setSize( $order->getSize() );
259 + $order->setCurrency( $wcOrder->get_currency() );
134 260
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 );
261 + $order->setCustomsDeclaration( $this->customsDeclarationRepository->getByOrderNumber( $order->getNumber() ) );
148 262
149 - $order->setCurrency( $wcOrder->get_currency() );
150 -
151 263 return $order;
152 264 }
153 265
154 266 /**
@@ -159,13 +271,15 @@
159 271 * @return bool
160 272 */
161 273 private function containsAdultContent( WC_Order $wcOrder ): bool {
162 274 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;
275 + if ( $item instanceof WC_Order_Item_Product ) {
276 + $product = $item->get_product();
277 + if ( $product instanceof WC_Product ) {
278 + $productEntity = new Product\Entity( $product );
279 + if ( $productEntity->isPhysical() && $productEntity->isAgeVerificationRequired() ) {
280 + return true;
281 + }
168 282 }
169 283 }
170 284 }
171 285
@@ -171,5 +285,34 @@
171 285
172 286 return false;
173 287 }
174 288
289 + /**
290 + * Parses string value as float.
291 + *
292 + * @param string|float|null $value Value.
293 + *
294 + * @return float|null
295 + */
296 + private function parseFloat( $value ): ?float {
297 + if ( $value === null || $value === '' ) {
298 + return null;
299 + }
300 +
301 + return (float) $value;
302 + }
303 +
304 + /**
305 + * Parses string value as float.
306 + *
307 + * @param string|int|null $value Value.
308 + *
309 + * @return bool|null
310 + */
311 + private function parseBool( $value ): ?bool {
312 + if ( $value === null || $value === '' ) {
313 + return null;
314 + }
315 +
316 + return (bool) $value;
317 + }
175 318 }