Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
14 / 20
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
StockIOUpdateService
70.00% covered (warning)
70.00%
14 / 20
0.00% covered (danger)
0.00%
0 / 1
8.32
0.00% covered (danger)
0.00%
0 / 1
 update
70.00% covered (warning)
70.00%
14 / 20
0.00% covered (danger)
0.00%
0 / 1
8.32
1<?php
2
3namespace Domain\StockIO\Services;
4
5use App\Events\ServiceProcessed;
6use App\Models\StockIO\StockIO;
7use App\Models\User;
8use Domain\Integration\Services\IntegrationUpdateService;
9use Domain\StockIO\DTOs\StockIODTO;
10use Domain\StockIO\Enums\StockInputReasonEnum;
11use Exception;
12use Illuminate\Support\Facades\DB;
13
14readonly class StockIOUpdateService
15{
16
17    /**
18     * @param StockIODTO $stockIODTO
19     * @param StockIO $stockIO
20     * @param User $user
21     * @return StockIO
22     * @throws Exception
23     */
24    public function update(StockIODTO $stockIODTO, StockIO $stockIO, User $user): StockIO
25    {
26        try {
27            DB::beginTransaction();
28
29            $stockIO->fill($stockIODTO->toArray());
30            $stockIO->responsible()->associate($stockIODTO->responsible);
31            $stockIO->team()->associate($stockIODTO->team);
32            if ($stockIO->isDirty()) {
33                $stockIO->save();
34            }
35
36            if ($stockIODTO->integrationDTO) {
37                if ($stockIO->isInput()) {
38
39                    if ($stockIO->reason == StockInputReasonEnum::INVOICE->value) {
40                        (new StockInputWarehouseIntegrationUpdateService())->update($stockIODTO->integrationDTO, $stockIO->integration, $user);
41                    } elseif ($stockIO->reason == StockInputReasonEnum::DEVOLUTION->value) {
42                        (new StockInputDevolutionIntegrationUpdateService())->update($stockIODTO->integrationDTO, $stockIO->integration, $user);
43                    } else {
44                        (new StockInputIntegrationUpdateService())->update($stockIODTO->integrationDTO, $stockIO->integration, $user);
45                    }
46                } else {
47                    (new IntegrationUpdateService())->update($stockIODTO->integrationDTO, $stockIO->integration, $user);
48                }
49            }
50
51            DB::commit();
52        } catch (Exception $exception) {
53            DB::rollBack();
54            throw $exception;
55        }
56        ServiceProcessed::dispatch(self::class, $user);
57        return $stockIO;
58    }
59
60}