PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
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 +258 -48 1.2.32.3.2 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,19 +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\Core\Helper;
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 -use Packetery\Module\Options\Provider;
21 +use Packetery\Module\Carrier\PacketaPickupPointsConfig;
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;
27 +use Packetery\Module\Product;
28 +use Packetery\Module\WeightCalculator;
29 +use stdClass;
17 30 use WC_Order;
31 +use WC_Order_Item_Product;
32 +use WC_Product;
18 33
34 +// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
19 35 /**
20 - * Class Order
36 + * Class Builder
21 37 *
22 - * @package Packetery\Module\EntityFactory
38 + * @package Packetery
23 39 */
24 40 class Builder {
25 41
26 42 /**
@@ -25,86 +41,280 @@
25 41
26 42 /**
27 43 * Options provider.
28 44 *
29 - * @var Provider Options provider.
45 + * @var OptionsProvider Options provider.
30 46 */
31 47 private $optionsProvider;
32 48
33 49 /**
50 + * Weight calculator.
51 + *
52 + * @var WeightCalculator
53 + */
54 + private $calculator;
55 +
56 + /**
57 + * Customs declaration repository.
58 + *
59 + * @var CustomsDeclaration\Repository
60 + */
61 + private $customsDeclarationRepository;
62 +
63 + /**
64 + * Internal pickup points config.
65 + *
66 + * @var PacketaPickupPointsConfig
67 + */
68 + private $pickupPointsConfig;
69 +
70 + /**
71 + * CoreHelper.
72 + *
73 + * @var CoreHelper
74 + */
75 + private $coreHelper;
76 +
77 + /**
78 + * Payment helper.
79 + *
80 + * @var PaymentHelper
81 + */
82 + private $paymentHelper;
83 +
84 + /**
34 85 * Carrier repository.
35 86 *
36 - * @var Carrier\Repository Carrier repository.
87 + * @var Carrier\EntityRepository
37 88 */
38 89 private $carrierRepository;
39 90
40 91 /**
41 - * Order constructor.
92 + * Builder constructor.
42 93 *
43 - * @param Provider $optionsProvider Options Provider.
44 - * @param Carrier\Repository $carrierRepository Carrier repository.
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.
45 101 */
46 102 public function __construct(
47 - Provider $optionsProvider,
48 - Carrier\Repository $carrierRepository
103 + OptionsProvider $optionsProvider,
104 + WeightCalculator $calculator,
105 + CustomsDeclaration\Repository $customsDeclarationRepository,
106 + PacketaPickupPointsConfig $pickupPointsConfig,
107 + CoreHelper $coreHelper,
108 + PaymentHelper $paymentHelper,
109 + Carrier\EntityRepository $carrierRepository
49 110 ) {
50 - $this->optionsProvider = $optionsProvider;
51 - $this->carrierRepository = $carrierRepository;
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;
52 118 }
53 119
54 120 /**
55 - * Creates common order entity from WC_Order.
121 + * Creates order entity from WC_Order and plugin data.
56 122 *
57 - * @param WC_Order $order WC_Order.
58 - * @param Entity\Order $partialOrder Partial order.
123 + * @param WC_Order $wcOrder WC_Order.
124 + * @param stdClass $result Db result, plugin specific data.
59 125 *
60 - * @return Entity\Order|null
126 + * @return Entity\Order
127 + * @throws InvalidCarrierException In case Carrier entity could not be created.
61 128 */
62 - public function finalize( WC_Order $order, Entity\Order $partialOrder ): ?Entity\Order {
63 - $orderData = $order->get_data();
64 - $contactInfo = ( $order->has_shipping_address() ? $orderData['shipping'] : $orderData['billing'] );
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 + }
65 134
66 - if ( null === $partialOrder->getCarrierId() ) {
67 - return null;
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 + );
68 145 }
69 146
70 - $partialOrder->setName( $contactInfo['first_name'] );
71 - $partialOrder->setSurname( $contactInfo['last_name'] );
72 - $partialOrder->setEshop( $this->optionsProvider->get_sender() );
73 - $partialOrder->setWeight( Helper::simplifyWeight( $partialOrder->getWeight() ) );
74 - $partialOrder->setValue( (float) $order->get_total( 'raw' ) );
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->setConsignPassword( $result->consign_password );
158 + $order->setSize( new Size( $this->parseFloat( $result->length ), $this->parseFloat( $result->width ), $this->parseFloat( $result->height ) ) );
159 + $order->setIsExported( (bool) $result->is_exported );
160 + $order->setIsLabelPrinted( (bool) $result->is_label_printed );
161 + $order->setCarrierNumber( $result->carrier_number );
162 + $order->setPacketStatus( $result->packet_status );
163 + $order->setStoredUntil( $this->coreHelper->getDateTimeFromString( $result->stored_until ) );
164 + $order->setAddressValidated( (bool) $result->address_validated );
165 + $order->setAdultContent( $this->parseBool( $result->adult_content ) );
166 + $order->setManualValue( $this->parseFloat( $result->value ) );
167 + $order->setManualCod( $this->parseFloat( $result->cod ) );
168 + $order->setDeliverOn( $this->coreHelper->getDateTimeFromString( $result->deliver_on ) );
169 + $order->setLastApiErrorMessage( $result->api_error_message );
170 + $order->setLastApiErrorDateTime(
171 + ( $result->api_error_date === null )
172 + ? null
173 + : DateTimeImmutable::createFromFormat(
174 + CoreHelper::MYSQL_DATETIME_FORMAT,
175 + $result->api_error_date,
176 + new DateTimeZone( 'UTC' )
177 + )->setTimezone( wp_timezone() )
178 + );
75 179
76 - $address = $partialOrder->getDeliveryAddress();
77 - if ( null === $address ) {
78 - $partialOrder->setAddressValidated( false );
180 + if ( $result->delivery_address ) {
181 + $deliveryAddressDecoded = json_decode( $result->delivery_address, false );
182 + $deliveryAddress = new Address(
183 + $deliveryAddressDecoded->street,
184 + $deliveryAddressDecoded->city,
185 + $deliveryAddressDecoded->zip
186 + );
187 +
188 + $deliveryAddress->setHouseNumber( $deliveryAddressDecoded->houseNumber );
189 + $deliveryAddress->setLongitude( $deliveryAddressDecoded->longitude );
190 + $deliveryAddress->setLatitude( $deliveryAddressDecoded->latitude );
191 + $deliveryAddress->setCounty( $deliveryAddressDecoded->county );
192 +
193 + $order->setDeliveryAddress( $deliveryAddress );
194 + }
195 +
196 + if ( $result->car_delivery_id ) {
197 + $order->setCarDeliveryId( $result->car_delivery_id );
198 + }
199 +
200 + if ( $result->point_id !== null ) {
201 + $pickUpPoint = new PickupPoint(
202 + $result->point_id,
203 + $result->point_place,
204 + $result->point_name,
205 + $result->point_city,
206 + $result->point_zip,
207 + $result->point_street,
208 + $result->point_url
209 + );
210 +
211 + $order->setPickupPoint( $pickUpPoint );
212 + }
213 +
214 + $order->setCalculatedWeight( $this->calculator->calculateOrderWeight( $wcOrder ) );
215 +
216 + if ( $order->containsAdultContent() === null ) {
217 + $order->setAdultContent( $this->containsAdultContent( $wcOrder ) );
218 + }
219 +
220 + $order->setShippingCountry( ModuleHelper::getWcOrderCountry( $wcOrder ) );
221 +
222 + $orderData = $wcOrder->get_data();
223 + $contactInfo = ( $wcOrder->has_shipping_address() ? $orderData['shipping'] : $orderData['billing'] );
224 +
225 + $order->setName( $contactInfo['first_name'] );
226 + $order->setSurname( $contactInfo['last_name'] );
227 + $order->setEshop( $this->optionsProvider->get_sender() );
228 +
229 + $order->setCalculatedValue( (float) $wcOrder->get_total( 'raw' ) );
230 +
231 + $address = $order->getDeliveryAddress();
232 + if ( $address === null ) {
233 + $order->setAddressValidated( false );
79 234 $address = new Address( $contactInfo['address_1'], $contactInfo['city'], $contactInfo['postcode'] );
80 235 }
81 236
82 - $partialOrder->setDeliveryAddress( $address );
237 + $order->setDeliveryAddress( $address );
238 + $order->setCustomNumber( $wcOrder->get_order_number() );
83 239
84 240 // Shipping address phone is optional.
85 - $partialOrder->setPhone( $orderData['billing']['phone'] );
86 - if ( ! empty( $contactInfo['phone'] ) ) {
87 - $partialOrder->setPhone( $contactInfo['phone'] );
241 + $order->setPhone( $orderData['billing']['phone'] );
242 + if ( isset( $contactInfo['phone'] ) && $contactInfo['phone'] !== '' ) {
243 + $order->setPhone( $contactInfo['phone'] );
88 244 }
89 245 // Additional address information.
90 - if ( ! empty( $contactInfo['address_2'] ) ) {
91 - $partialOrder->setNote( $contactInfo['address_2'] );
246 + if ( isset( $contactInfo['address_2'] ) && $contactInfo['address_2'] !== '' ) {
247 + $order->setNote( $contactInfo['address_2'] );
92 248 }
93 249
94 - $partialOrder->setEmail( $orderData['billing']['email'] );
95 - $codMethod = $this->optionsProvider->getCodPaymentMethod();
96 - if ( $orderData['payment_method'] === $codMethod ) {
97 - $partialOrder->setCod( $partialOrder->getValue() );
250 + $order->setEmail( $orderData['billing']['email'] );
251 + $hasCodPaymentMethod = $this->paymentHelper->isCodPaymentMethod( $orderData['payment_method'] );
252 + if ( $hasCodPaymentMethod ) {
253 + $order->setCalculatedCod( $order->getCalculatedValue() );
254 + // manual COD is set from DB directly
255 + } else {
256 + $order->setCalculatedCod( null );
257 + $order->setManualCod( null );
98 258 }
99 - $partialOrder->setSize( $partialOrder->getSize() );
100 259
101 - if ( $partialOrder->isExternalCarrier() ) {
102 - $carrier = $this->carrierRepository->getById( (int) $partialOrder->getCarrierId() );
103 - $partialOrder->setCarrier( $carrier );
260 + $order->setSize( $order->getSize() );
261 + $order->setCurrency( $wcOrder->get_currency() );
262 +
263 + $order->setCustomsDeclaration( $this->customsDeclarationRepository->getByOrderNumber( $order->getNumber() ) );
264 +
265 + return $order;
266 + }
267 +
268 + /**
269 + * Finds out if adult content is present.
270 + *
271 + * @param WC_Order $wcOrder WC Order.
272 + *
273 + * @return bool
274 + */
275 + private function containsAdultContent( WC_Order $wcOrder ): bool {
276 + foreach ( $wcOrder->get_items() as $item ) {
277 + if ( $item instanceof WC_Order_Item_Product ) {
278 + $product = $item->get_product();
279 + if ( $product instanceof WC_Product ) {
280 + $productEntity = new Product\Entity( $product );
281 + if ( $productEntity->isPhysical() && $productEntity->isAgeVerificationRequired() ) {
282 + return true;
283 + }
284 + }
285 + }
104 286 }
105 287
106 - $partialOrder->setCurrency( $order->get_currency() );
288 + return false;
289 + }
107 290
108 - return $partialOrder;
291 + /**
292 + * Parses string value as float.
293 + *
294 + * @param string|float|null $value Value.
295 + *
296 + * @return float|null
297 + */
298 + private function parseFloat( $value ): ?float {
299 + if ( $value === null || $value === '' ) {
300 + return null;
301 + }
302 +
303 + return (float) $value;
304 + }
305 +
306 + /**
307 + * Parses string value as float.
308 + *
309 + * @param string|int|null $value Value.
310 + *
311 + * @return bool|null
312 + */
313 + private function parseBool( $value ): ?bool {
314 + if ( $value === null || $value === '' ) {
315 + return null;
316 + }
317 +
318 + return (bool) $value;
109 319 }
110 320 }