PluginProbe
Packeta / 2.3.1
Packeta v2.3.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
packeta / src / Packetery / Core / Api / Soap / CreatePacketMapper.php

CreatePacketMapper.php in Packeta 2.3.1, at src/Packetery/Core/Api/Soap/CreatePacketMapper.php

218 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class CreatePacketMapper.
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Core\Api\Soap;
11
12 use Packetery\Core\CoreHelper;
13 use Packetery\Core\Entity;
14 use Packetery\Core\Rounder;
15 use Packetery\Module\Carrier\CarrierOptionsFactory;
16
17 /**
18 * Class CreatePacketMapper.
19 *
20 * @package Packetery
21 */
22 class CreatePacketMapper {
23
24 /**
25 * CoreHelper.
26 *
27 * @var CoreHelper
28 */
29 private $coreHelper;
30
31 /**
32 * @var CarrierOptionsFactory
33 */
34 private $carrierOptionsFactory;
35
36 /**
37 * @var string
38 */
39 private $affiliateId;
40
41 public function __construct(
42 CoreHelper $coreHelper,
43 CarrierOptionsFactory $carrierOptionsFactory,
44 string $affiliateId
45 ) {
46 $this->coreHelper = $coreHelper;
47 $this->carrierOptionsFactory = $carrierOptionsFactory;
48 $this->affiliateId = $affiliateId;
49 }
50
51 /**
52 * Maps order data to CreatePacket structure.
53 *
54 * @param Entity\Order $order Order entity.
55 *
56 * @return array<string, array<int<0, max>|string, array<string, array<int, array<string, bool|float|int|string|null>>|float|string|null>|float|null>|float|int|string|null>
57 */
58 public function fromOrderToArray( Entity\Order $order ): array {
59 $createPacketData = [
60 // Required attributes.
61 'number' => $order->getCustomNumberOrNumber(),
62 'name' => $order->getName(),
63 'surname' => $order->getSurname(),
64 'value' => $order->getFinalValue(),
65 'weight' => $order->getFinalWeight(),
66 'addressId' => $order->getPickupPointOrCarrierId(),
67 'eshop' => $order->getEshop(),
68 // Optional attributes.
69 'adultContent' => (int) $order->containsAdultContent(),
70 'cod' => null,
71 'currency' => $order->getCurrency(),
72 'email' => $order->getEmail(),
73 'note' => $order->getNote(),
74 'phone' => $order->getPhone(),
75 'deliverOn' => $this->coreHelper->getStringFromDateTime( $order->getDeliverOn(), CoreHelper::DATEPICKER_FORMAT ),
76 'affiliateId' => $this->affiliateId,
77 ];
78
79 $codValue = $order->getFinalCod();
80 if ( $codValue !== null ) {
81 $roundingType = $this->carrierOptionsFactory->createByCarrierId( $order->getCarrier()->getId() )->getCodRoundingType();
82 $roundedCod = Rounder::roundByCurrency( $codValue, $createPacketData['currency'], $roundingType );
83 $createPacketData['cod'] = $roundedCod;
84 }
85
86 $pickupPoint = $order->getPickupPoint();
87 if ( $pickupPoint !== null && $order->isExternalCarrier() ) {
88 $createPacketData['carrierPickupPoint'] = $pickupPoint->getId();
89 }
90
91 if ( $order->isHomeDelivery() || $order->isCarDelivery() ) {
92 $address = $order->getDeliveryAddress();
93 if ( $address !== null ) {
94 $createPacketData['street'] = $address->getStreet();
95 $createPacketData['city'] = $address->getCity();
96 $createPacketData['zip'] = $address->getZip();
97 if ( $address->getHouseNumber() !== null ) {
98 $createPacketData['houseNumber'] = $address->getHouseNumber();
99 }
100 }
101 }
102
103 $carrier = $order->getCarrier();
104 if ( $carrier->requiresSize() ) {
105 $size = $order->getSize();
106 if ( $size !== null ) {
107 $createPacketData['size'] = [
108 'length' => $size->getLength(),
109 'width' => $size->getWidth(),
110 'height' => $size->getHeight(),
111 ];
112 }
113 }
114
115 $createPacketData['attributes'] = [];
116
117 if ( $order->isCarDelivery() ) {
118 $createPacketData['attributes']['attribute'] = [
119 'key' => 'carDeliveryId',
120 'value' => $order->getCarDeliveryId(),
121 ];
122 }
123
124 if ( $carrier->requiresCustomsDeclarations() === false ) {
125 return $createPacketData;
126 }
127
128 $customsDeclaration = $order->getCustomsDeclaration();
129 if ( $customsDeclaration === null ) {
130 return $createPacketData;
131 }
132
133 $createPacketData['items'] = [];
134 foreach ( $customsDeclaration->getItems() as $customsDeclarationItem ) {
135 $createPacketData['items'][] = [
136 'attributes' => [
137 [
138 'key' => 'countryOfOrigin',
139 'value' => $customsDeclarationItem->getCountryOfOrigin(),
140 ],
141 [
142 'key' => 'customsCode',
143 'value' => $customsDeclarationItem->getCustomsCode(),
144 ],
145 [
146 'key' => 'productName',
147 'value' => $customsDeclarationItem->getProductName(),
148 ],
149 [
150 'key' => 'productNameEn',
151 'value' => $customsDeclarationItem->getProductNameEn(),
152 ],
153 [
154 'key' => 'value',
155 'value' => $customsDeclarationItem->getValue(),
156 ],
157 [
158 'key' => 'unitsCount',
159 'value' => $customsDeclarationItem->getUnitsCount(),
160 ],
161 [
162 'key' => 'weight',
163 'value' => $customsDeclarationItem->getWeight(),
164 ],
165 [
166 'key' => 'isFoodBook',
167 'value' => $customsDeclarationItem->isFoodOrBook(),
168 ],
169 [
170 'key' => 'isVoc',
171 'value' => $customsDeclarationItem->isVoc(),
172 ],
173 ],
174 ];
175 }
176
177 $createPacketData['attributes'][] = [
178 'key' => 'ead',
179 'value' => $customsDeclaration->getEad(),
180 ];
181 $createPacketData['attributes'][] = [
182 'key' => 'deliveryCost',
183 'value' => $customsDeclaration->getDeliveryCost(),
184 ];
185 $createPacketData['attributes'][] = [
186 'key' => 'invoiceNumber',
187 'value' => $customsDeclaration->getInvoiceNumber(),
188 ];
189 $createPacketData['attributes'][] = [
190 'key' => 'invoiceIssueDate',
191 'value' => $customsDeclaration->getInvoiceIssueDate()->format( 'Y-m-d' ),
192 ];
193
194 if ( $customsDeclaration->getMrn() !== null ) {
195 $createPacketData['attributes'][] = [
196 'key' => 'mrn',
197 'value' => $customsDeclaration->getMrn(),
198 ];
199 }
200
201 if ( $customsDeclaration->getEadFileId() !== null ) {
202 $createPacketData['attributes'][] = [
203 'key' => 'eadFile',
204 'value' => $customsDeclaration->getEadFileId(),
205 ];
206 }
207
208 if ( $customsDeclaration->getInvoiceFileId() !== null ) {
209 $createPacketData['attributes'][] = [
210 'key' => 'invoiceFile',
211 'value' => $customsDeclaration->getInvoiceFileId(),
212 ];
213 }
214
215 return $createPacketData;
216 }
217 }
218