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