Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.96% |
20 / 23 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| StockIOStoreService | |
86.96% |
20 / 23 |
|
0.00% |
0 / 1 |
6.08 | |
0.00% |
0 / 1 |
| store | |
86.96% |
20 / 23 |
|
0.00% |
0 / 1 |
6.08 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\StockIO\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\StockIO\StockIO; |
| 7 | use App\Models\User; |
| 8 | use App\Notifications\StockInputNotification; |
| 9 | use Domain\Integration\Services\IntegrationStoreService; |
| 10 | use Domain\Shared\Services\SendNotificationUserExpo; |
| 11 | use Domain\StockIO\DTOs\StockIODTO; |
| 12 | use Domain\StockIO\Enums\StockInputReasonEnum; |
| 13 | use Domain\Transferable\Services\TransferableStoreService; |
| 14 | use Exception; |
| 15 | use Illuminate\Support\Facades\DB; |
| 16 | |
| 17 | readonly class StockIOStoreService |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * @param StockIODTO $stockIODTO |
| 22 | * @param User $user |
| 23 | * @return StockIO |
| 24 | * @throws Exception |
| 25 | */ |
| 26 | public function store(StockIODTO $stockIODTO, User $user): StockIO |
| 27 | { |
| 28 | try { |
| 29 | DB::beginTransaction(); |
| 30 | |
| 31 | $stockIO = new StockIO(); |
| 32 | $stockIO->fill($stockIODTO->toArray()); |
| 33 | $stockIO->responsible()->associate($stockIODTO->responsible); |
| 34 | $stockIO->team()->associate($stockIODTO->team); |
| 35 | $stockIO->setAttribute('type', $stockIODTO->typeEnum); |
| 36 | $stockIO->save(); |
| 37 | |
| 38 | (new TransferableStoreService())->store($stockIO); |
| 39 | |
| 40 | if ($stockIODTO->integrationDTO) { |
| 41 | if ($stockIO->isInput()) { |
| 42 | |
| 43 | if ($stockIO->reason == StockInputReasonEnum::INVOICE->value) { |
| 44 | (new StockInputWarehouseIntegrationStoreService())->store($stockIODTO->integrationDTO, $stockIO, $user); |
| 45 | } elseif ($stockIO->reason == StockInputReasonEnum::DEVOLUTION->value) { |
| 46 | (new StockInputDevolutionIntegrationStoreService())->store($stockIODTO->integrationDTO, $stockIO, $user); |
| 47 | } else { |
| 48 | (new StockInputIntegrationStoreService())->store($stockIODTO->integrationDTO, $stockIO, $user); |
| 49 | } |
| 50 | } else { |
| 51 | (new IntegrationStoreService())->store($stockIODTO->integrationDTO, $stockIO, $user); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | $stockIO->responsible?->notify(new StockInputNotification($stockIO)); |
| 56 | |
| 57 | // if(!is_null($stockIO->responsible?->getAttribute('expo_token'))){ |
| 58 | // (new SendNotificationUserExpo())->send('Nova entrada no estoque', 'Nova entrada no estoque', $user); |
| 59 | // } |
| 60 | |
| 61 | DB::commit(); |
| 62 | } catch (Exception $exception) { |
| 63 | DB::rollBack(); |
| 64 | throw $exception; |
| 65 | } |
| 66 | |
| 67 | ServiceProcessed::dispatch(self::class, $user); |
| 68 | return $stockIO; |
| 69 | } |
| 70 | |
| 71 | } |