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