Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.77% covered (danger)
30.77%
12 / 39
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
StockOutputUpdateService
30.77% covered (danger)
30.77%
12 / 39
0.00% covered (danger)
0.00%
0 / 2
69.08
0.00% covered (danger)
0.00%
0 / 1
 update
34.29% covered (danger)
34.29%
12 / 35
0.00% covered (danger)
0.00%
0 / 1
45.34
 notifyAdminUser
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Domain\StockIO\Services;
4
5use App\Events\ServiceProcessed;
6use App\Models\StockIO\StockIO;
7use App\Models\User;
8use Domain\StockIO\DTOs\StockIODTO;
9use Domain\StockIO\Enums\StockStatusEnum;
10use Domain\StockIO\Services\StockIOProductCancelService;
11use Domain\StockIO\Services\StockIOProductConcludeService;
12use Domain\Integration\Services\IntegrationUpdateService;
13use Domain\PickingOrder\Enums\PickingOrderStatusEnum;
14use App\Notifications\StockIOCanceledNotification;
15use Domain\Integration\DTOs\IntegrationStockOutputDTO;
16use Exception;
17use Illuminate\Support\Facades\DB;
18use App\Helper\AppHelper;
19use Domain\Shared\Services\SendNotificationUserExpo;
20
21readonly class StockOutputUpdateService
22{
23
24    /**
25     * @param StockIODTO $stockIODTO
26     * @param StockIO $stockIO
27     * @param User $user
28     * @return StockIO
29     * @throws Exception
30     */
31    public function update(StockIODTO $stockIODTO, StockIO $stockIO, User $user): StockIO
32    {
33        try {
34            DB::beginTransaction();
35
36            $stockIO->setAttribute('status', $stockIODTO->statusEnum);
37
38            if ($stockIO->isDirty()) {
39                $stockIO->save();
40            }
41
42            if ($stockIODTO->integrationDTO) {
43                if ($stockIO->isInput()) {
44                    (new StockInputIntegrationUpdateService())->update($stockIODTO->integrationDTO, $stockIO->integration, $user);
45                } else {
46                    (new IntegrationUpdateService())->update($stockIODTO->integrationDTO, $stockIO->integration, $user);
47                }
48            }
49
50            if($stockIO->status == StockStatusEnum::CANCELED)
51            {
52                if($stockIO->pickingOrders()->exists()){
53                    $stockIO->pickingOrders()->each(function ($pickingOrder){
54                        $pickingOrder->setAttribute('status', PickingOrderStatusEnum::CANCELED);
55                        if ($pickingOrder->isDirty()) {
56                            $pickingOrder->save();
57                        }
58                    });
59                }
60
61                $stockIO->stockIOProducts()->each(function ($stockIOProduct) use ($user){
62                    (new StockIOProductCancelService())->cancel($stockIOProduct, $user);
63                });
64
65                $this->notifyAdminUser($stockIO, $stockIODTO->integrationDTO, StockStatusEnum::CANCELED);
66            } elseif($stockIO->status == StockStatusEnum::CONCLUDED)
67            {
68                if($stockIO->pickingOrders()->exists()){
69                    $stockIO->pickingOrders()->each(function ($pickingOrder){
70                        $pickingOrder->setAttribute('status', PickingOrderStatusEnum::CONCLUDED);
71                        if ($pickingOrder->isDirty()) {
72                            $pickingOrder->save();
73                        }
74                    });
75                }
76
77                $stockIO->stockIOProducts()->each(function ($stockIOProduct) use ($user){
78                    (new StockIOProductConcludeService())->conclude($stockIOProduct, $user);
79                });
80
81            }
82
83
84            DB::commit();
85        } catch (Exception $exception) {
86            DB::rollBack();
87            throw $exception;
88        }
89        ServiceProcessed::dispatch(self::class, $user);
90        return $stockIO;
91    }
92
93    /**
94     * @param StockIO $stockIO
95     * @param StockStatusEnum $stockStatusEnum
96     */
97    private function notifyAdminUser(StockIO $stockIO, IntegrationStockOutputDTO $integrationDTO, StockStatusEnum $stockStatusEnum)
98    {
99        $admins = AppHelper::getAdminUser($stockIO->team->getKey());
100
101        foreach($admins as $admin){
102            $user = (User::find($admin->id));
103            $user?->notify(new StockIOCanceledNotification($integrationDTO->NUMPFA, $integrationDTO->NUMANE));
104
105            // if(!is_null($user->getAttribute('expo_token'))){
106            //     (new SendNotificationUserExpo())->send(__('Output and picking orders of pre-invoice :preInvoice have been canceled',
107            //                                             ['preInvoice' => $integrationDTO->NUMANE.'/'.$integrationDTO->NUMPFA]), __('Output canceled'), $user);
108            // }
109        }
110    }
111}