Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
6.67% |
2 / 30 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ItemPopulateMovementDateService | |
6.67% |
2 / 30 |
|
0.00% |
0 / 2 |
25.33 | |
0.00% |
0 / 1 |
| get | |
7.69% |
2 / 26 |
|
0.00% |
0 / 1 |
16.58 | |||
| populateDTO | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Item\Services; |
| 4 | |
| 5 | use App\Models\Item\Item; |
| 6 | use App\Models\User; |
| 7 | use Domain\Integration\DTOs\IntegrationItemDTO; |
| 8 | use Domain\Integration\Services\IntegrationUpdateService; |
| 9 | use Domain\Item\DTOs\ItemGetMovementDateDTO; |
| 10 | use Domain\Item\Enums\ItemSyncEnum; |
| 11 | use Domain\Shared\Integrations\Senior\Exceptions\IntegrationSoapSeniorException; |
| 12 | use Illuminate\Support\Facades\DB; |
| 13 | use Throwable; |
| 14 | use function config; |
| 15 | |
| 16 | readonly class ItemPopulateMovementDateService |
| 17 | { |
| 18 | /** |
| 19 | * @param Item $item |
| 20 | * @param string $productionOrder |
| 21 | * @param User $user |
| 22 | * @return void |
| 23 | * @throws IntegrationSoapSeniorException |
| 24 | * @throws Throwable |
| 25 | */ |
| 26 | public function get(Item $item, string $productionOrder, User $user): void |
| 27 | { |
| 28 | if (config('integration.disable_movement_date')) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | try { |
| 33 | DB::beginTransaction(); |
| 34 | $itemGetMovementDateDTO = new ItemGetMovementDateDTO([ |
| 35 | 'production_order' => $productionOrder, |
| 36 | 'integration_product' => $item->product->integration->getAttribute('parameters->integration_product'), |
| 37 | 'integration_derivation' => $item->product->integration->getAttribute('parameters->integration_derivation'), |
| 38 | ]); |
| 39 | |
| 40 | $result = (new ItemGetMovementDateService())->process($itemGetMovementDateDTO, $user); |
| 41 | |
| 42 | if (!isset($result[0]['E210MVP_DATMOV'])) { |
| 43 | throw new IntegrationSoapSeniorException('Error retrieving movement date from Senior'); |
| 44 | } |
| 45 | |
| 46 | (new ItemChangeStatusService())->change( |
| 47 | $item, |
| 48 | ItemSyncEnum::DONE, |
| 49 | $user |
| 50 | ); |
| 51 | |
| 52 | (new IntegrationUpdateService())->update( |
| 53 | $this->populateDTO($item, $result[0]), |
| 54 | $item->integration, |
| 55 | $user |
| 56 | ); |
| 57 | |
| 58 | DB::commit(); |
| 59 | } catch (Throwable $throwable) { |
| 60 | DB::rollBack(); |
| 61 | (new ItemChangeStatusService())->change($item, ItemSyncEnum::SYNC_FAIL, $user); |
| 62 | throw $throwable; |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param Item $item |
| 69 | * @param array $result |
| 70 | * @return IntegrationItemDTO |
| 71 | */ |
| 72 | private function populateDTO(Item $item, array $result): IntegrationItemDTO |
| 73 | { |
| 74 | return new IntegrationItemDTO([ |
| 75 | 'production_order' => $item->integration->getAttribute('parameters')['production_order'], |
| 76 | 'movement_date' => $result['E210MVP_DATMOV'], |
| 77 | ]); |
| 78 | } |
| 79 | } |