Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.50% covered (success)
97.50%
39 / 40
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
StockIoFilters
97.50% covered (success)
97.50%
39 / 40
75.00% covered (warning)
75.00%
3 / 4
4
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 applyInput
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
1.00
 applyOutput
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 applyCommon
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Domain\StockIO\Filters;
4
5use Domain\StockIO\Enums\StockOutputReasonEnum;
6use Domain\StockIO\Enums\StockStatusEnum;
7use Illuminate\Database\Eloquent\Builder;
8
9class StockIoFilters
10{
11    public function __construct(
12        protected array $filters = []
13    ) {}
14
15    public function applyInput(Builder $query): Builder
16    {
17        $this->applyCommon($query);
18
19        return $query->when($this->filters['created_at'] ?? null, fn ($q, $date) =>
20            $q->whereDate('stock_ios.created_at', $date)
21        )
22        ->when($this->filters['reason'] ?? null, fn ($q, $reason) =>
23            $q->whereIn('reason', $reason)
24        )
25        ->when($this->filters['key'] ?? null, fn ($q, $key) =>
26            $q->whereHas('integration', fn ($query) =>
27                $query->whereRaw("(parameters::jsonb)->>'invoice_key' = ?", [$key])
28            )
29        )
30        ->when($this->filters['invoice_number'] ?? null, fn ($q, $invoiceNumber) =>
31            $q->whereHas('integration', fn ($query) =>
32                $query->whereRaw("(parameters::jsonb)->>'invoice_number' = ?", [(string) $invoiceNumber])
33            )
34        );
35    }
36
37    public function applyOutput(Builder $query): Builder
38    {
39        $this->applyCommon($query);
40
41        return $query->when($this->filters['reason'] ?? null, fn ($q, $reason) =>
42            $q->whereIn('reason', (array) $reason)
43        );
44    }
45
46    protected function applyCommon(Builder $query): Builder
47    {
48        return $query
49            ->whereNotIn('status', [StockStatusEnum::SYNCING->value, StockStatusEnum::SYNC_FAIL->value])
50            ->when($this->filters['status'] ?? null, fn ($q, $status) =>
51                $q->whereIn('status', $status)
52            )
53            ->when($this->filters['suppliers'] ?? null, fn ($q, $ids) =>
54                $q->whereHas('integration', fn ($query) =>
55                    $query->whereIn(
56                        \DB::raw("(parameters::jsonb)->>'supplier_code'"),
57                        $ids
58                    )
59                )
60            )
61            ->when($this->filters['product_id'] ?? null, fn ($q, $ids) =>
62                $q->whereHas('stockIOProducts', fn ($query) =>
63                    $query->whereIn('product_id', (array) $ids)
64                )
65            );
66    }
67}