Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.67% covered (danger)
6.67%
2 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemPopulateMovementDateService
6.67% covered (danger)
6.67%
2 / 30
0.00% covered (danger)
0.00%
0 / 2
25.33
0.00% covered (danger)
0.00%
0 / 1
 get
7.69% covered (danger)
7.69%
2 / 26
0.00% covered (danger)
0.00%
0 / 1
16.58
 populateDTO
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Domain\Item\Services;
4
5use App\Models\Item\Item;
6use App\Models\User;
7use Domain\Integration\DTOs\IntegrationItemDTO;
8use Domain\Integration\Services\IntegrationUpdateService;
9use Domain\Item\DTOs\ItemGetMovementDateDTO;
10use Domain\Item\Enums\ItemSyncEnum;
11use Domain\Shared\Integrations\Senior\Exceptions\IntegrationSoapSeniorException;
12use Illuminate\Support\Facades\DB;
13use Throwable;
14use function config;
15
16readonly 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}