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