Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.00% |
24 / 25 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PickingOrderRemainingStepsWarehouseService | |
96.00% |
24 / 25 |
|
50.00% |
1 / 2 |
4 | |
0.00% |
0 / 1 |
| steps | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
3.02 | |||
| getItemAddresses | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\PickingOrder\Services; |
| 4 | |
| 5 | use function __; |
| 6 | use function is_null; |
| 7 | |
| 8 | use App\Events\ServiceProcessed; |
| 9 | use App\Models\Address\Address; |
| 10 | use App\Models\PickingOrder\PickingOrder; |
| 11 | use App\Models\PickingOrder\PickingOrderProduct; |
| 12 | use App\Models\User; |
| 13 | |
| 14 | use Domain\Address\Resources\AddressProductWarehouseLocationResource; |
| 15 | |
| 16 | use Illuminate\Database\Eloquent\Builder; |
| 17 | use Illuminate\Database\Eloquent\Relations\MorphMany; |
| 18 | use Illuminate\Support\Facades\DB; |
| 19 | |
| 20 | readonly class PickingOrderRemainingStepsWarehouseService |
| 21 | { |
| 22 | const RELATIONS = [ |
| 23 | 'parent', |
| 24 | 'transferable', |
| 25 | 'items', 'items.integration', 'items.product', 'items.product.integration', 'items.storage', |
| 26 | ]; |
| 27 | |
| 28 | /** |
| 29 | * @param PickingOrder $pickingOrder |
| 30 | * @param User $user |
| 31 | * @return array |
| 32 | */ |
| 33 | public function steps(PickingOrderProduct $pickingOrderProduct, User $user): array |
| 34 | { |
| 35 | $results = DB::select("select * from ordering_product_picking_warehouse({$pickingOrderProduct->getKey()})"); |
| 36 | |
| 37 | foreach ($results as $result) { |
| 38 | if (is_null($result->address_id)) { |
| 39 | $result->message = __('Product not located at addresses'); |
| 40 | } |
| 41 | |
| 42 | $result->address = $this->getItemAddresses($result); |
| 43 | |
| 44 | unset($result->address_id); |
| 45 | } |
| 46 | |
| 47 | ServiceProcessed::dispatch(self::class, $user); |
| 48 | return array_column($results, 'address'); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param $item |
| 53 | * @return array |
| 54 | */ |
| 55 | private function getItemAddresses($item): array |
| 56 | { |
| 57 | $relations = ['items' => function (MorphMany $query) use ($item) { |
| 58 | $query->where('product_id', $item->product_id) |
| 59 | ->whereHas('integration', function ($builder) use ($item) { |
| 60 | $builder->where('parameters->priority_value', $item->priority_value); |
| 61 | }); |
| 62 | }] + self::RELATIONS; |
| 63 | |
| 64 | $addresses = Address::query() |
| 65 | ->whereHas('items', function (Builder $query) use ($item) { |
| 66 | $query->where('product_id', $item->product_id) |
| 67 | ->whereHas('integration', function ($builder) use ($item) { |
| 68 | $builder->where('parameters->priority_value', $item->priority_value); |
| 69 | }); |
| 70 | }) |
| 71 | ->where('id', $item->address_id) |
| 72 | ->with($relations) |
| 73 | ->get(); |
| 74 | |
| 75 | return AddressProductWarehouseLocationResource::collection($addresses)->jsonSerialize(); |
| 76 | } |
| 77 | } |