Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
57.69% |
15 / 26 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| StockIOProductStoreService | |
57.69% |
15 / 26 |
|
0.00% |
0 / 3 |
12.85 | |
0.00% |
0 / 1 |
| store | |
82.35% |
14 / 17 |
|
0.00% |
0 / 1 |
4.09 | |||
| processStockIO | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
3.19 | |||
| notifyAdminUser | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\StockIO\Services; |
| 4 | |
| 5 | use App\Helper\AppHelper; |
| 6 | use App\Events\ServiceProcessed; |
| 7 | use App\Models\StockIO\StockIO; |
| 8 | use App\Models\StockIO\StockIOProduct; |
| 9 | use App\Models\User; |
| 10 | use Domain\Integration\Services\IntegrationStoreService; |
| 11 | use Domain\StockIO\DTOs\StockIOProductDTO; |
| 12 | use Domain\StockIO\Services\StockOutputReturnToStartedService; |
| 13 | use Illuminate\Support\Facades\DB; |
| 14 | use Domain\StockIO\Enums\StockStatusEnum; |
| 15 | use App\Notifications\StockIOProductStoredNotification; |
| 16 | use Domain\Shared\Services\SendNotificationUserExpo; |
| 17 | use Throwable; |
| 18 | |
| 19 | readonly class StockIOProductStoreService |
| 20 | { |
| 21 | /** |
| 22 | * @param StockIOProductDTO $stockOutputProductDTO |
| 23 | * @param StockIO $stockIO |
| 24 | * @param User $user |
| 25 | * @return StockIOProduct |
| 26 | * @throws Throwable |
| 27 | */ |
| 28 | public function store(StockIOProductDTO $stockOutputProductDTO, StockIO $stockIO, User $user): StockIOProduct |
| 29 | { |
| 30 | try { |
| 31 | DB::beginTransaction(); |
| 32 | |
| 33 | $stockIOProduct = new StockIOProduct(); |
| 34 | $stockIOProduct->product()->associate($stockOutputProductDTO->product); |
| 35 | $stockIOProduct->fill($stockOutputProductDTO->toArray()); |
| 36 | $stockIO->stockIOProducts()->save($stockIOProduct); |
| 37 | |
| 38 | if ($stockOutputProductDTO->dataTransferObject) { |
| 39 | (new IntegrationStoreService())->store($stockOutputProductDTO->dataTransferObject, $stockIOProduct, $user); |
| 40 | } |
| 41 | |
| 42 | if ($stockIOProduct->product->integration) { |
| 43 | $this->processStockIO( |
| 44 | $stockIO, |
| 45 | $stockIOProduct->product->integration->getAttribute('parameters')['integration_code'] |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | DB::commit(); |
| 50 | } catch (Throwable $throwable) { |
| 51 | DB::rollBack(); |
| 52 | throw $throwable; |
| 53 | } |
| 54 | |
| 55 | return $stockIOProduct; |
| 56 | ServiceProcessed::dispatch(self::class, $user); |
| 57 | } |
| 58 | |
| 59 | private function processStockIO(StockIO $stockIO, string $codref) |
| 60 | { |
| 61 | if($stockIO->status == StockStatusEnum::SEPARATED){ |
| 62 | (new StockOutputReturnToStartedService())->processOutputReturnToStarted($stockIO); |
| 63 | |
| 64 | $this->notifyAdminUser($stockIO, $codref); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private function notifyAdminUser(StockIO $stockIO, string $codref) |
| 69 | { |
| 70 | $admins = AppHelper::getAdminUser($stockIO->team->getKey()); |
| 71 | |
| 72 | foreach($admins as $admin){ |
| 73 | $user = (User::find($admin->id)); |
| 74 | $user?->notify(new StockIOProductStoredNotification($stockIO->integration->getAttribute('parameters')['NUMANE'], |
| 75 | $stockIO->integration->getAttribute('parameters')['NUMPFA'], |
| 76 | $codref)); |
| 77 | |
| 78 | // if(!is_null($user->getAttribute('expo_token'))){ |
| 79 | // (new SendNotificationUserExpo())->send(__('New product stored for the pre-invoice :preInvoice, product :product', |
| 80 | // ['preInvoice' => $stockIO->integration->getAttribute('parameters')['NUMANE'].'/'.$stockIO->integration->getAttribute('parameters')['NUMPFA'], |
| 81 | // 'product' => $codref]), __('New product'), $user); |
| 82 | // } |
| 83 | } |
| 84 | } |
| 85 | } |