Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
PickedItemUpdateAmountService
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 update
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Domain\PickingOrder\Services;
4
5use App\Events\ServiceProcessed;
6use App\Models\Item\Item;
7use App\Models\PickedItem;
8use App\Models\User;
9use Domain\PickingOrder\DTOs\PickItemDTO;
10use Domain\PickingOrder\DTOs\PickItemWarehouseDTO;
11use Domain\PickingOrder\Exception\PickedItemException;
12use Exception;
13use Illuminate\Support\Facades\DB;
14
15readonly class PickedItemUpdateAmountService
16{
17
18    /**
19     * @param PickItemDTO|PickItemWarehouseDTO $pickItemDTO
20     * @param Item $item
21     * @param User $user
22     * @return PickedItem
23     * @throws Exception
24     */
25    public function update(PickItemDTO|PickItemWarehouseDTO $pickItemDTO, Item $item, User $user): PickedItem
26    {
27        try {
28            DB::beginTransaction();
29
30            /** @var PickedItem|null $pivot */
31            $pivot = $item->pickedItems->where('stock_io_product_id', $pickItemDTO->stockIOProduct->id)->first();
32            if (!$pivot) {
33                throw new PickedItemException(__('Picked item not found when updating picked quantity'));
34            }
35
36            $pivot->setAttribute('amount', $pivot->getAttribute('amount') + $pickItemDTO->amount);
37            $pivot->save();
38
39            DB::commit();
40        } catch (Exception $exception) {
41            DB::rollBack();
42            throw $exception;
43        }
44
45        ServiceProcessed::dispatch(self::class, $user);
46        return $pivot;
47    }
48}