← All changes
|
src/Packetery/Module/CustomsDeclaration/Repository.php
+115
-61
1.6.4
→
2.1
View file →
| @@ -8,11 +8,11 @@ | ||
| 8 | 8 | declare(strict_types=1); |
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module\CustomsDeclaration; |
| 11 | 11 | |
| 12 | +use Packetery\Core\CoreHelper; | |
| 12 | 13 | use Packetery\Core\Entity\CustomsDeclaration; |
| 13 | 14 | use Packetery\Core\Entity\CustomsDeclarationItem; |
| 14 | -use Packetery\Core\Helper; | |
| 15 | 15 | use Packetery\Module\EntityFactory; |
| 16 | 16 | use Packetery\Module\WpdbAdapter; |
| 17 | 17 | |
| 18 | 18 | /** |
| @@ -45,8 +45,23 @@ | ||
| 45 | 45 | $this->entityFactory = $entityFactory; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 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 | + /** | |
| 49 | 64 | * Gets customs declaration by order. |
| 50 | 65 | * |
| 51 | 66 | * @param string $orderNumber Order. |
| 52 | 67 | * |
| @@ -68,15 +83,15 @@ | ||
| 68 | 83 | `invoice_file` IS NOT NULL AS `has_invoice_file_content`, |
| 69 | 84 | `ead_file` IS NOT NULL AS `has_ead_file_content` |
| 70 | 85 | FROM `%s` |
| 71 | 86 | WHERE `order_id` = %d', |
| 72 | - $this->wpdbAdapter->packetery_customs_declaration, | |
| 87 | + $this->wpdbAdapter->packeteryCustomsDeclaration, | |
| 73 | 88 | $orderNumber |
| 74 | 89 | ), |
| 75 | 90 | ARRAY_A |
| 76 | 91 | ); |
| 77 | 92 | |
| 78 | - if ( null === $customsDeclarationRow ) { | |
| 93 | + if ( $customsDeclarationRow === null ) { | |
| 79 | 94 | return null; |
| 80 | 95 | } |
| 81 | 96 | |
| 82 | 97 | $customsDeclaration = $this->entityFactory->fromStandardizedStructure( $customsDeclarationRow, $orderNumber ); |
| @@ -84,9 +99,9 @@ | ||
| 84 | 99 | $customsDeclaration->setInvoiceFile( |
| 85 | 100 | function () use ( $orderNumber ): ?string { |
| 86 | 101 | return $this->wpdbAdapter->get_var( |
| 87 | 102 | $this->wpdbAdapter->prepare( |
| 88 | - 'SELECT `invoice_file` FROM `' . $this->wpdbAdapter->packetery_customs_declaration . '` WHERE `order_id` = %d', | |
| 103 | + 'SELECT `invoice_file` FROM `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` WHERE `order_id` = %d', | |
| 89 | 104 | $orderNumber |
| 90 | 105 | ) |
| 91 | 106 | ); |
| 92 | 107 | }, |
| @@ -96,9 +111,9 @@ | ||
| 96 | 111 | $customsDeclaration->setEadFile( |
| 97 | 112 | function () use ( $orderNumber ): ?string { |
| 98 | 113 | return $this->wpdbAdapter->get_var( |
| 99 | 114 | $this->wpdbAdapter->prepare( |
| 100 | - 'SELECT `ead_file` FROM `' . $this->wpdbAdapter->packetery_customs_declaration . '` WHERE `order_id` = %d', | |
| 115 | + 'SELECT `ead_file` FROM `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` WHERE `order_id` = %d', | |
| 101 | 116 | $orderNumber |
| 102 | 117 | ) |
| 103 | 118 | ); |
| 104 | 119 | }, |
| @@ -116,34 +131,15 @@ | ||
| 116 | 131 | * @param string|null $customsDeclarationId Customs declaration ID. |
| 117 | 132 | * @return CustomsDeclarationItem[] |
| 118 | 133 | */ |
| 119 | 134 | public function getItemsByCustomsDeclarationId( ?string $customsDeclarationId ): array { |
| 120 | - if ( null === $customsDeclarationId ) { | |
| 135 | + if ( $customsDeclarationId === null ) { | |
| 121 | 136 | return []; |
| 122 | 137 | } |
| 123 | 138 | |
| 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 | - ); | |
| 139 | + $customsDeclarationItemRows = $this->getCustomsDeclarationItemRows( $customsDeclarationId ); | |
| 144 | 140 | |
| 145 | - if ( null === $customsDeclarationItemRows ) { | |
| 141 | + if ( $customsDeclarationItemRows === null ) { | |
| 146 | 142 | return []; |
| 147 | 143 | } |
| 148 | 144 | |
| 149 | 145 | $customsDeclarationItems = []; |
| @@ -153,8 +149,31 @@ | ||
| 153 | 149 | |
| 154 | 150 | return $customsDeclarationItems; |
| 155 | 151 | } |
| 156 | 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 | + | |
| 157 | 176 | /** |
| 158 | 177 | * Saves customs declaration. |
| 159 | 178 | * |
| 160 | 179 | * @param CustomsDeclaration $customsDeclaration Customs declaration. |
| @@ -161,17 +180,17 @@ | ||
| 161 | 180 | * @param array $fieldsToOmit Fields to omit. |
| 162 | 181 | * @return void |
| 163 | 182 | */ |
| 164 | 183 | public function save( CustomsDeclaration $customsDeclaration, array $fieldsToOmit = [ 'invoice_file', 'ead_file' ] ): void { |
| 165 | - if ( null === $customsDeclaration->getId() ) { | |
| 184 | + if ( $customsDeclaration->getId() === null ) { | |
| 166 | 185 | $this->wpdbAdapter->insertReplaceHelper( |
| 167 | - $this->wpdbAdapter->packetery_customs_declaration, | |
| 186 | + $this->wpdbAdapter->packeteryCustomsDeclaration, | |
| 168 | 187 | $this->declarationToDbArray( $customsDeclaration, $fieldsToOmit ) |
| 169 | 188 | ); |
| 170 | 189 | $customsDeclaration->setId( $this->wpdbAdapter->getLastInsertId() ); |
| 171 | 190 | } else { |
| 172 | 191 | $this->wpdbAdapter->update( |
| 173 | - $this->wpdbAdapter->packetery_customs_declaration, | |
| 192 | + $this->wpdbAdapter->packeteryCustomsDeclaration, | |
| 174 | 193 | $this->declarationToDbArray( $customsDeclaration, $fieldsToOmit ), |
| 175 | 194 | [ 'id' => (int) $customsDeclaration->getId() ] |
| 176 | 195 | ); |
| 177 | 196 | } |
| @@ -176,12 +195,12 @@ | ||
| 176 | 195 | ); |
| 177 | 196 | } |
| 178 | 197 | |
| 179 | 198 | $omitInvoiceFile = in_array( 'invoice_file', $fieldsToOmit, true ); |
| 180 | - if ( false === $omitInvoiceFile && $customsDeclaration->hasInvoiceFileContent() ) { | |
| 199 | + if ( $omitInvoiceFile === false && $customsDeclaration->hasInvoiceFileContent() ) { | |
| 181 | 200 | $this->wpdbAdapter->query( |
| 182 | 201 | $this->wpdbAdapter->prepare( |
| 183 | - 'UPDATE `' . $this->wpdbAdapter->packetery_customs_declaration . '` SET `invoice_file` = %s WHERE `id` = %d', | |
| 202 | + 'UPDATE `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` SET `invoice_file` = %s WHERE `id` = %d', | |
| 184 | 203 | $customsDeclaration->getInvoiceFile(), |
| 185 | 204 | $customsDeclaration->getId() |
| 186 | 205 | ) |
| 187 | 206 | ); |
| @@ -186,12 +205,12 @@ | ||
| 186 | 205 | ) |
| 187 | 206 | ); |
| 188 | 207 | } |
| 189 | 208 | |
| 190 | - if ( false === $omitInvoiceFile && false === $customsDeclaration->hasInvoiceFileContent() ) { | |
| 209 | + if ( $omitInvoiceFile === false && $customsDeclaration->hasInvoiceFileContent() === false ) { | |
| 191 | 210 | $this->wpdbAdapter->query( |
| 192 | 211 | $this->wpdbAdapter->prepare( |
| 193 | - 'UPDATE ' . $this->wpdbAdapter->packetery_customs_declaration . ' SET `invoice_file` = NULL WHERE `id` = %d', | |
| 212 | + 'UPDATE ' . $this->wpdbAdapter->packeteryCustomsDeclaration . ' SET `invoice_file` = NULL WHERE `id` = %d', | |
| 194 | 213 | $customsDeclaration->getId() |
| 195 | 214 | ) |
| 196 | 215 | ); |
| 197 | 216 | } |
| @@ -196,12 +215,12 @@ | ||
| 196 | 215 | ); |
| 197 | 216 | } |
| 198 | 217 | |
| 199 | 218 | $omitEadFile = in_array( 'ead_file', $fieldsToOmit, true ); |
| 200 | - if ( false === $omitEadFile && $customsDeclaration->hasEadFileContent() ) { | |
| 219 | + if ( $omitEadFile === false && $customsDeclaration->hasEadFileContent() ) { | |
| 201 | 220 | $this->wpdbAdapter->query( |
| 202 | 221 | $this->wpdbAdapter->prepare( |
| 203 | - 'UPDATE `' . $this->wpdbAdapter->packetery_customs_declaration . '` SET `ead_file` = %s WHERE `id` = %d', | |
| 222 | + 'UPDATE `' . $this->wpdbAdapter->packeteryCustomsDeclaration . '` SET `ead_file` = %s WHERE `id` = %d', | |
| 204 | 223 | $customsDeclaration->getEadFile(), |
| 205 | 224 | $customsDeclaration->getId() |
| 206 | 225 | ) |
| 207 | 226 | ); |
| @@ -206,12 +225,12 @@ | ||
| 206 | 225 | ) |
| 207 | 226 | ); |
| 208 | 227 | } |
| 209 | 228 | |
| 210 | - if ( false === $omitEadFile && false === $customsDeclaration->hasEadFileContent() ) { | |
| 229 | + if ( $omitEadFile === false && $customsDeclaration->hasEadFileContent() === false ) { | |
| 211 | 230 | $this->wpdbAdapter->query( |
| 212 | 231 | $this->wpdbAdapter->prepare( |
| 213 | - 'UPDATE ' . $this->wpdbAdapter->packetery_customs_declaration . ' SET `ead_file` = NULL WHERE `id` = %d', | |
| 232 | + 'UPDATE ' . $this->wpdbAdapter->packeteryCustomsDeclaration . ' SET `ead_file` = NULL WHERE `id` = %d', | |
| 214 | 233 | $customsDeclaration->getId() |
| 215 | 234 | ) |
| 216 | 235 | ); |
| 217 | 236 | } |
| @@ -223,17 +242,17 @@ | ||
| 223 | 242 | * @param CustomsDeclarationItem $customsDeclarationItem Customs declaration item. |
| 224 | 243 | * @return void |
| 225 | 244 | */ |
| 226 | 245 | public function saveItem( CustomsDeclarationItem $customsDeclarationItem ): void { |
| 227 | - if ( null === $customsDeclarationItem->getId() ) { | |
| 246 | + if ( $customsDeclarationItem->getId() === null ) { | |
| 228 | 247 | $this->wpdbAdapter->insert( |
| 229 | - $this->wpdbAdapter->packetery_customs_declaration_item, | |
| 248 | + $this->wpdbAdapter->packeteryCustomsDeclarationItem, | |
| 230 | 249 | $this->declarationItemToDbArray( $customsDeclarationItem ) |
| 231 | 250 | ); |
| 232 | 251 | $customsDeclarationItem->setId( $this->wpdbAdapter->getLastInsertId() ); |
| 233 | 252 | } else { |
| 234 | 253 | $this->wpdbAdapter->update( |
| 235 | - $this->wpdbAdapter->packetery_customs_declaration_item, | |
| 254 | + $this->wpdbAdapter->packeteryCustomsDeclarationItem, | |
| 236 | 255 | $this->declarationItemToDbArray( $customsDeclarationItem ), |
| 237 | 256 | [ 'id' => (int) $customsDeclarationItem->getId() ] |
| 238 | 257 | ); |
| 239 | 258 | } |
| @@ -245,12 +264,47 @@ | ||
| 245 | 264 | * @param int $itemId Item ID. |
| 246 | 265 | * @return void |
| 247 | 266 | */ |
| 248 | 267 | public function deleteItem( int $itemId ): void { |
| 249 | - $this->wpdbAdapter->delete( $this->wpdbAdapter->packetery_customs_declaration_item, [ 'id' => $itemId ], '%d' ); | |
| 268 | + $this->wpdbAdapter->delete( $this->wpdbAdapter->packeteryCustomsDeclarationItem, [ 'id' => $itemId ], '%d' ); | |
| 250 | 269 | } |
| 251 | 270 | |
| 252 | 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 | + /** | |
| 253 | 307 | * Converts customs declaration to DB array. |
| 254 | 308 | * |
| 255 | 309 | * @param CustomsDeclaration $customsDeclaration Customs declaration. |
| 256 | 310 | * @param array $fieldsToOmit Fields to omit. |
| @@ -262,9 +316,9 @@ | ||
| 262 | 316 | 'order_id' => $customsDeclaration->getOrderId(), |
| 263 | 317 | 'ead' => $customsDeclaration->getEad(), |
| 264 | 318 | 'delivery_cost' => $customsDeclaration->getDeliveryCost(), |
| 265 | 319 | 'invoice_number' => $customsDeclaration->getInvoiceNumber(), |
| 266 | - 'invoice_issue_date' => $customsDeclaration->getInvoiceIssueDate()->format( Helper::MYSQL_DATE_FORMAT ), | |
| 320 | + 'invoice_issue_date' => $customsDeclaration->getInvoiceIssueDate()->format( CoreHelper::MYSQL_DATE_FORMAT ), | |
| 267 | 321 | 'invoice_file_id' => $customsDeclaration->getInvoiceFileId(), |
| 268 | 322 | 'mrn' => $customsDeclaration->getMrn(), |
| 269 | 323 | 'ead_file_id' => $customsDeclaration->getEadFileId(), |
| 270 | 324 | ]; |
| @@ -305,26 +359,26 @@ | ||
| 305 | 359 | */ |
| 306 | 360 | public function createOrAlterTable(): bool { |
| 307 | 361 | $createTableQuery = sprintf( |
| 308 | 362 | '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, | |
| 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, | |
| 323 | 377 | $this->wpdbAdapter->get_charset_collate() |
| 324 | 378 | ); |
| 325 | 379 | |
| 326 | - return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packetery_customs_declaration ); | |
| 380 | + return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryCustomsDeclaration ); | |
| 327 | 381 | } |
| 328 | 382 | |
| 329 | 383 | /** |
| 330 | 384 | * Creates item table. |
| @@ -344,13 +398,13 @@ | ||
| 344 | 398 | `country_of_origin` char(2) NOT NULL, |
| 345 | 399 | `weight` decimal(10,3) UNSIGNED NOT NULL, |
| 346 | 400 | `is_food_or_book` tinyint(1) NOT NULL, |
| 347 | 401 | `is_voc` tinyint(1) NOT NULL, |
| 348 | - PRIMARY KEY (`id`) | |
| 402 | + PRIMARY KEY (`id`) | |
| 349 | 403 | ) %s', |
| 350 | - $this->wpdbAdapter->packetery_customs_declaration_item, | |
| 404 | + $this->wpdbAdapter->packeteryCustomsDeclarationItem, | |
| 351 | 405 | $this->wpdbAdapter->get_charset_collate() |
| 352 | 406 | ); |
| 353 | 407 | |
| 354 | - return $this->wpdbAdapter->dbDelta( $createItemTableQuery, $this->wpdbAdapter->packetery_customs_declaration_item ); | |
| 408 | + return $this->wpdbAdapter->dbDelta( $createItemTableQuery, $this->wpdbAdapter->packeteryCustomsDeclarationItem ); | |
| 355 | 409 | } |
| 356 | 410 | } |