Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| StockIOProductCancelService | |
0.00% |
0 / 21 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| cancel | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| 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\StockIOProduct; |
| 8 | use App\Models\User; |
| 9 | use App\Notifications\StockIOProductCanceledNotification; |
| 10 | use Domain\StockIO\Enums\StockProductStatusEnum; |
| 11 | use Domain\PickingOrder\Enums\PickingOrderStatusEnum; |
| 12 | use Domain\Shared\Services\SendNotificationUserExpo; |
| 13 | use Illuminate\Support\Facades\DB; |
| 14 | use Throwable; |
| 15 | |
| 16 | readonly class StockIOProductCancelService |
| 17 | { |
| 18 | /** |
| 19 | * @param StockIOProduct $stockIOProduct |
| 20 | * @param User $user |
| 21 | * @return void |
| 22 | * @throws Throwable |
| 23 | */ |
| 24 | public function cancel(StockIOProduct $stockIOProduct, User $user): void |
| 25 | { |
| 26 | try { |
| 27 | DB::beginTransaction(); |
| 28 | |
| 29 | $stockIOProduct->setAttribute('status', StockProductStatusEnum::CANCELED); |
| 30 | $stockIOProduct->save(); |
| 31 | |
| 32 | if($stockIOProduct->pickingOrderProduct()->exists()){ |
| 33 | $stockIOProduct->pickingOrderProduct()->each(function ($pickingOrderProduct){ |
| 34 | $pickingOrderProduct->setAttribute('status', PickingOrderStatusEnum::CANCELED); |
| 35 | if ($pickingOrderProduct->isDirty()) { |
| 36 | $pickingOrderProduct->save(); |
| 37 | } |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | $this->notifyAdminUser($stockIOProduct); |
| 42 | |
| 43 | DB::commit(); |
| 44 | } catch (Throwable $throwable) { |
| 45 | DB::rollBack(); |
| 46 | throw $throwable; |
| 47 | } |
| 48 | |
| 49 | ServiceProcessed::dispatch(self::class, $user); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param StockIOProduct $stockIO |
| 54 | */ |
| 55 | private function notifyAdminUser(StockIOProduct $stockIOProduct) |
| 56 | { |
| 57 | $admins = AppHelper::getAdminUser($stockIOProduct->stockIO->team->getKey()); |
| 58 | |
| 59 | foreach($admins as $admin){ |
| 60 | $user = (User::find($admin->id)); |
| 61 | $user?->notify(new StockIOProductCanceledNotification($stockIOProduct->stockIO->integration->parameters['NUMANE'], |
| 62 | $stockIOProduct->stockIO->integration->parameters['NUMPFA'], |
| 63 | $stockIOProduct->product->integration->getAttribute('parameters')['integration_code'])); |
| 64 | |
| 65 | // if(!is_null($user->getAttribute('expo_token'))){ |
| 66 | // (new SendNotificationUserExpo())->send(__('Output product and picking orders product of pre-invoice :preInvoice have been canceled. Product: :product', |
| 67 | // ['preInvoice' => $stockIOProduct->stockIO->integration->parameters['NUMANE'].'/'.$stockIOProduct->stockIO->integration->parameters['NUMPFA'], |
| 68 | // 'product' => $stockIOProduct->product->integration->getAttribute('parameters')['integration_code']]), __('Product canceled'), $user); |
| 69 | // } |
| 70 | } |
| 71 | } |
| 72 | } |