PluginProbe
Packeta / trunk
Packeta vtrunk
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 / Module / CustomsDeclaration / Repository.php

Repository.php in Packeta trunk, at src/Packetery/Module/CustomsDeclaration/Repository.php

419 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Repository.
4 *
5 * @package Packetery
6 */
7
8 declare(strict_types=1);
9
10 namespace Packetery\Module\CustomsDeclaration;
11
12 use Packetery\Core\CoreHelper;
13 use Packetery\Core\Entity\CustomsDeclaration;
14 use Packetery\Core\Entity\CustomsDeclarationItem;
15 use Packetery\Module\EntityFactory;
16 use Packetery\Module\Exception\DeleteErrorException;
17 use Packetery\Module\WpdbAdapter;
18
19 /**
20 * Class Repository.
21 */
22 class Repository {
23
24 /**
25 * Wpdb adapter.
26 *
27 * @var WpdbAdapter
28 */
29 private $wpdbAdapter;
30
31 /**
32 * Entity factory.
33 *
34 * @var EntityFactory\CustomsDeclaration
35 */
36 private $entityFactory;
37
38 /**
39 * Constructor.
40 *
41 * @param WpdbAdapter $wpdbAdapter Wpdb adapter.
42 * @param EntityFactory\CustomsDeclaration $entityFactory Entity factory.
43 */
44 public function __construct( WpdbAdapter $wpdbAdapter, EntityFactory\CustomsDeclaration $entityFactory ) {
45 $this->wpdbAdapter = $wpdbAdapter;
46 $this->entityFactory = $entityFactory;
47 }
48
49 /**
50 * Gets customs declaration ID by order ID.
51 *
52 * @param string $orderNumber Order ID.
53 * @return string|null
54 */
55 private function getIdByOrderNumber( string $orderNumber ): ?string {
56 return $this->wpdbAdapter->get_var(
57 $this->wpdbAdapter->prepare(
58 'SELECT `id` FROM `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` WHERE `order_id` = %d',
59 $orderNumber
60 )
61 );
62 }
63
64 /**
65 * Gets customs declaration by order.
66 *
67 * @param string $orderNumber Order.
68 *
69 * @return CustomsDeclaration|null
70 */
71 public function getByOrderNumber( string $orderNumber ): ?CustomsDeclaration {
72 $customsDeclarationRow = $this->wpdbAdapter->get_row(
73 sprintf(
74 'SELECT
75 `id`,
76 `order_id`,
77 `ead`,
78 `delivery_cost`,
79 `invoice_number`,
80 `invoice_issue_date`,
81 `mrn`,
82 `invoice_file_id`,
83 `ead_file_id`,
84 `invoice_file` IS NOT NULL AS `has_invoice_file_content`,
85 `ead_file` IS NOT NULL AS `has_ead_file_content`
86 FROM `%s`
87 WHERE `order_id` = %d',
88 $this->wpdbAdapter->packeteryCustomsDeclaration,
89 $orderNumber
90 ),
91 ARRAY_A
92 );
93
94 if ( $customsDeclarationRow === null ) {
95 return null;
96 }
97
98 $customsDeclaration = $this->entityFactory->fromStandardizedStructure( $customsDeclarationRow, $orderNumber );
99
100 $customsDeclaration->setInvoiceFile(
101 function () use ( $orderNumber ): ?string {
102 return $this->wpdbAdapter->get_var(
103 $this->wpdbAdapter->prepare(
104 'SELECT `invoice_file` FROM `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` WHERE `order_id` = %d',
105 $orderNumber
106 )
107 );
108 },
109 (bool) $customsDeclarationRow['has_invoice_file_content']
110 );
111
112 $customsDeclaration->setEadFile(
113 function () use ( $orderNumber ): ?string {
114 return $this->wpdbAdapter->get_var(
115 $this->wpdbAdapter->prepare(
116 'SELECT `ead_file` FROM `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` WHERE `order_id` = %d',
117 $orderNumber
118 )
119 );
120 },
121 (bool) $customsDeclarationRow['has_ead_file_content']
122 );
123
124 $customsDeclaration->setItems( $this->getItemsByCustomsDeclarationId( $customsDeclaration->getId() ) );
125
126 return $customsDeclaration;
127 }
128
129 /**
130 * Gets customs declaration items by order.
131 *
132 * @param string|null $customsDeclarationId Customs declaration ID.
133 * @return CustomsDeclarationItem[]
134 */
135 public function getItemsByCustomsDeclarationId( ?string $customsDeclarationId ): array {
136 if ( $customsDeclarationId === null ) {
137 return [];
138 }
139
140 $customsDeclarationItemRows = $this->getCustomsDeclarationItemRows( $customsDeclarationId );
141
142 if ( $customsDeclarationItemRows === null ) {
143 return [];
144 }
145
146 $customsDeclarationItems = [];
147 foreach ( $customsDeclarationItemRows as $row ) {
148 $customsDeclarationItems[] = $this->entityFactory->createItemFromStandardizedStructure( $row );
149 }
150
151 return $customsDeclarationItems;
152 }
153
154 public function getCustomsDeclarationItemRows( ?string $customsDeclarationId ): ?array {
155 return $this->wpdbAdapter->get_results(
156 sprintf(
157 'SELECT
158 `id`,
159 `customs_declaration_id`,
160 `customs_code`,
161 `value`,
162 `product_name_en`,
163 `product_name`,
164 `units_count`,
165 `country_of_origin`,
166 `weight`,
167 `is_food_or_book`,
168 `is_voc`
169 FROM `%s` WHERE `customs_declaration_id` = %d',
170 $this->wpdbAdapter->packeteryCustomsDeclarationItem,
171 $customsDeclarationId
172 ),
173 ARRAY_A
174 );
175 }
176
177 /**
178 * Saves customs declaration.
179 *
180 * @param CustomsDeclaration $customsDeclaration Customs declaration.
181 * @param array $fieldsToOmit Fields to omit.
182 * @return int|false The number of rows updated, or false on error.
183 */
184 public function save( CustomsDeclaration $customsDeclaration, array $fieldsToOmit = [ 'invoice_file', 'ead_file' ] ) {
185 if ( $customsDeclaration->getId() === null ) {
186 $updatedRowCount = $this->wpdbAdapter->insertReplaceHelper(
187 $this->wpdbAdapter->packeteryCustomsDeclaration,
188 $this->declarationToDbArray( $customsDeclaration, $fieldsToOmit )
189 );
190 if ( $updatedRowCount !== false ) {
191 $customsDeclaration->setId( $this->wpdbAdapter->getLastInsertId() );
192 }
193 } else {
194 $updatedRowCount = $this->wpdbAdapter->update(
195 $this->wpdbAdapter->packeteryCustomsDeclaration,
196 $this->declarationToDbArray( $customsDeclaration, $fieldsToOmit ),
197 [ 'id' => (int) $customsDeclaration->getId() ]
198 );
199 }
200
201 if ( $customsDeclaration->getId() !== null ) {
202 $omitInvoiceFile = in_array( 'invoice_file', $fieldsToOmit, true );
203 if ( $omitInvoiceFile === false && $customsDeclaration->hasInvoiceFileContent() ) {
204 $fileQueryResult = $this->wpdbAdapter->update(
205 $this->wpdbAdapter->packeteryCustomsDeclaration,
206 [ 'invoice_file' => $customsDeclaration->getInvoiceFile() ],
207 [ 'id' => (int) $customsDeclaration->getId() ]
208 );
209 if ( $fileQueryResult === false ) {
210 $updatedRowCount = false;
211 }
212 }
213
214 if ( $omitInvoiceFile === false && $customsDeclaration->hasInvoiceFileContent() === false ) {
215 $fileQueryResult = $this->wpdbAdapter->update(
216 $this->wpdbAdapter->packeteryCustomsDeclaration,
217 [ 'invoice_file' => null ],
218 [ 'id' => (int) $customsDeclaration->getId() ]
219 );
220 if ( $fileQueryResult === false ) {
221 $updatedRowCount = false;
222 }
223 }
224
225 $omitEadFile = in_array( 'ead_file', $fieldsToOmit, true );
226 if ( $omitEadFile === false && $customsDeclaration->hasEadFileContent() ) {
227 $fileQueryResult = $this->wpdbAdapter->update(
228 $this->wpdbAdapter->packeteryCustomsDeclaration,
229 [ 'ead_file' => $customsDeclaration->getEadFile() ],
230 [ 'id' => (int) $customsDeclaration->getId() ]
231 );
232 if ( $fileQueryResult === false ) {
233 $updatedRowCount = false;
234 }
235 }
236
237 if ( $omitEadFile === false && $customsDeclaration->hasEadFileContent() === false ) {
238 $fileQueryResult = $this->wpdbAdapter->update(
239 $this->wpdbAdapter->packeteryCustomsDeclaration,
240 [ 'ead_file' => null ],
241 [ 'id' => (int) $customsDeclaration->getId() ]
242 );
243 if ( $fileQueryResult === false ) {
244 $updatedRowCount = false;
245 }
246 }
247 }
248
249 return $updatedRowCount;
250 }
251
252 /**
253 * Saves customs declaration item.
254 *
255 * @param CustomsDeclarationItem $customsDeclarationItem Customs declaration item.
256 * @return int|false The number of rows updated, or false on error.
257 */
258 public function saveItem( CustomsDeclarationItem $customsDeclarationItem ) {
259 if ( $customsDeclarationItem->getId() === null ) {
260 $updatedRowCount = $this->wpdbAdapter->insert(
261 $this->wpdbAdapter->packeteryCustomsDeclarationItem,
262 $this->declarationItemToDbArray( $customsDeclarationItem )
263 );
264 $customsDeclarationItem->setId( $this->wpdbAdapter->getLastInsertId() );
265 } else {
266 $updatedRowCount = $this->wpdbAdapter->update(
267 $this->wpdbAdapter->packeteryCustomsDeclarationItem,
268 $this->declarationItemToDbArray( $customsDeclarationItem ),
269 [ 'id' => (int) $customsDeclarationItem->getId() ]
270 );
271 }
272
273 return $updatedRowCount;
274 }
275
276 /**
277 * @throws DeleteErrorException
278 */
279 public function deleteItem( int $itemId ): void {
280 $this->wpdbAdapter->delete( $this->wpdbAdapter->packeteryCustomsDeclarationItem, [ 'id' => $itemId ], '%d' );
281 }
282
283 /**
284 * @throws DeleteErrorException
285 */
286 private function deleteItems( string $customsDeclarationId ): void {
287 $items = $this->getItemsByCustomsDeclarationId( $customsDeclarationId );
288
289 if ( count( $items ) === 0 ) {
290 return;
291 }
292
293 foreach ( $items as $item ) {
294 $this->deleteItem( (int) $item->getId() );
295 }
296 }
297
298 /**
299 * Completely deletes Customs Declaration with all its items.
300 *
301 * @throws DeleteErrorException
302 */
303 public function delete( string $orderId ): void {
304 $customsDeclarationId = $this->getIdByOrderNumber( $orderId );
305
306 if ( $customsDeclarationId === null ) {
307 return;
308 }
309
310 $this->deleteItems( $customsDeclarationId );
311 $this->wpdbAdapter->delete( $this->wpdbAdapter->packeteryCustomsDeclaration, [ 'id' => $customsDeclarationId ], '%d' );
312 }
313
314 /**
315 * Converts customs declaration to DB array.
316 *
317 * @param CustomsDeclaration $customsDeclaration Customs declaration.
318 * @param array $fieldsToOmit Fields to omit.
319 * @return array<string, string|int|float>
320 */
321 public function declarationToDbArray( CustomsDeclaration $customsDeclaration, array $fieldsToOmit = [] ): array {
322 $data = [
323 'id' => $customsDeclaration->getId(),
324 'order_id' => $customsDeclaration->getOrderId(),
325 'ead' => $customsDeclaration->getEad(),
326 'delivery_cost' => $customsDeclaration->getDeliveryCost(),
327 'invoice_number' => $customsDeclaration->getInvoiceNumber(),
328 'invoice_issue_date' => $customsDeclaration->getInvoiceIssueDate()->format( CoreHelper::MYSQL_DATE_FORMAT ),
329 'invoice_file_id' => $customsDeclaration->getInvoiceFileId(),
330 'mrn' => $customsDeclaration->getMrn(),
331 'ead_file_id' => $customsDeclaration->getEadFileId(),
332 ];
333
334 foreach ( $fieldsToOmit as $fieldToOmit ) {
335 unset( $data[ $fieldToOmit ] );
336 }
337
338 return $data;
339 }
340
341 /**
342 * Converts customs declaration item to DB array.
343 *
344 * @param CustomsDeclarationItem $customsDeclarationItem Customs declaration item.
345 * @return array<string, string|int|float>
346 */
347 public function declarationItemToDbArray( CustomsDeclarationItem $customsDeclarationItem ): array {
348 return [
349 'id' => $customsDeclarationItem->getId(),
350 'customs_declaration_id' => $customsDeclarationItem->getCustomsDeclarationId(),
351 'customs_code' => $customsDeclarationItem->getCustomsCode(),
352 'value' => $customsDeclarationItem->getValue(),
353 'product_name_en' => $customsDeclarationItem->getProductNameEn(),
354 'product_name' => $customsDeclarationItem->getProductName(),
355 'units_count' => $customsDeclarationItem->getUnitsCount(),
356 'country_of_origin' => strtoupper( $customsDeclarationItem->getCountryOfOrigin() ),
357 'weight' => $customsDeclarationItem->getWeight(),
358 'is_food_or_book' => (int) $customsDeclarationItem->isFoodOrBook(),
359 'is_voc' => (int) $customsDeclarationItem->isVoc(),
360 ];
361 }
362
363 /**
364 * Creates main table.
365 *
366 * @return bool
367 */
368 public function createOrAlterTable(): bool {
369 $createTableQuery = sprintf(
370 'CREATE TABLE `%s` (
371 `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
372 `order_id` bigint(20) UNSIGNED NOT NULL,
373 `ead` varchar(50) NOT NULL,
374 `delivery_cost` decimal(13,2) UNSIGNED NOT NULL,
375 `invoice_number` varchar(255) NOT NULL,
376 `invoice_issue_date` date NOT NULL,
377 `invoice_file` mediumblob NULL DEFAULT NULL,
378 `invoice_file_id` varchar(255) NULL DEFAULT NULL,
379 `mrn` varchar(32) NULL DEFAULT NULL,
380 `ead_file` mediumblob NULL DEFAULT NULL,
381 `ead_file_id` varchar(255) NULL DEFAULT NULL,
382 PRIMARY KEY (`id`)
383 ) %s',
384 $this->wpdbAdapter->packeteryCustomsDeclaration,
385 $this->wpdbAdapter->get_charset_collate()
386 );
387
388 return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryCustomsDeclaration );
389 }
390
391 /**
392 * Creates item table.
393 *
394 * @return bool
395 */
396 public function createOrAlterItemTable(): bool {
397 $createItemTableQuery = sprintf(
398 'CREATE TABLE `%s` (
399 `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
400 `customs_declaration_id` int(11) UNSIGNED NOT NULL,
401 `customs_code` varchar(8) NOT NULL,
402 `value` decimal(13,2) UNSIGNED NOT NULL,
403 `product_name_en` varchar(255) NOT NULL,
404 `product_name` varchar(255) NULL DEFAULT NULL,
405 `units_count` int(11) UNSIGNED NOT NULL,
406 `country_of_origin` char(2) NOT NULL,
407 `weight` decimal(10,3) UNSIGNED NOT NULL,
408 `is_food_or_book` tinyint(1) NOT NULL,
409 `is_voc` tinyint(1) NOT NULL,
410 PRIMARY KEY (`id`)
411 ) %s',
412 $this->wpdbAdapter->packeteryCustomsDeclarationItem,
413 $this->wpdbAdapter->get_charset_collate()
414 );
415
416 return $this->wpdbAdapter->dbDelta( $createItemTableQuery, $this->wpdbAdapter->packeteryCustomsDeclarationItem );
417 }
418 }
419