Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| PickedItemUpdateAmountService | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| update | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\PickingOrder\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\Item\Item; |
| 7 | use App\Models\PickedItem; |
| 8 | use App\Models\User; |
| 9 | use Domain\PickingOrder\DTOs\PickItemDTO; |
| 10 | use Domain\PickingOrder\DTOs\PickItemWarehouseDTO; |
| 11 | use Domain\PickingOrder\Exception\PickedItemException; |
| 12 | use Exception; |
| 13 | use Illuminate\Support\Facades\DB; |
| 14 | |
| 15 | readonly 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 | } |