Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
27.03% |
10 / 37 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TransactionQueryBuilder | |
27.03% |
10 / 37 |
|
0.00% |
0 / 3 |
102.43 | |
0.00% |
0 / 1 |
| visibleTo | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| search | |
37.04% |
10 / 27 |
|
0.00% |
0 / 1 |
47.94 | |||
| transferable | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Transaction\QueryBuilders; |
| 4 | |
| 5 | use App\Models\Transferable\Transferable; |
| 6 | use App\Models\User; |
| 7 | use Domain\Item\Interfaces\HasItemsInterface; |
| 8 | use Domain\Transaction\DTOs\TransactionSearchDTO; |
| 9 | use Illuminate\Database\Eloquent\Builder; |
| 10 | |
| 11 | class TransactionQueryBuilder extends Builder |
| 12 | { |
| 13 | /** |
| 14 | * @param User|null $user |
| 15 | * @return $this |
| 16 | */ |
| 17 | public function visibleTo(?User $user = null): static |
| 18 | { |
| 19 | if ($user->isStorer()) { |
| 20 | $this->where(function ($query) use ($user) { |
| 21 | $query->where('user_id', '=', $user->getKey()); |
| 22 | }); |
| 23 | } |
| 24 | |
| 25 | return $this; |
| 26 | } |
| 27 | |
| 28 | public function search(TransactionSearchDTO $transactionSearchDTO): static |
| 29 | { |
| 30 | if ($transactionSearchDTO->productionOrder) { |
| 31 | $this->where('item->integration->parameters->production_order', $transactionSearchDTO->productionOrder); |
| 32 | } |
| 33 | |
| 34 | if ($transactionSearchDTO->priorityValue) { |
| 35 | $this->where('item->integration->parameters->priority_value', $transactionSearchDTO->priorityValue); |
| 36 | } |
| 37 | |
| 38 | if ($transactionSearchDTO->integrationCode) { |
| 39 | $this->where('item->product->integration->parameters->integration_code', $transactionSearchDTO->integrationCode); |
| 40 | } |
| 41 | |
| 42 | if ($transactionSearchDTO->product) { |
| 43 | $this->whereRaw("item->>'product_id' = ?", $transactionSearchDTO->product->getKey()); |
| 44 | } |
| 45 | |
| 46 | if ($transactionSearchDTO->user) { |
| 47 | $this->where('user_id', $transactionSearchDTO->user->getKey()); |
| 48 | } |
| 49 | |
| 50 | if (!empty($transactionSearchDTO->type)) { |
| 51 | $this->whereIn('type', $transactionSearchDTO->type); |
| 52 | |
| 53 | } elseif (!empty($transactionSearchDTO->reason)) { |
| 54 | $this->whereIn('type', array_keys($transactionSearchDTO->reason)); |
| 55 | } |
| 56 | |
| 57 | if (!empty($transactionSearchDTO->reason)) { |
| 58 | $reasons = []; |
| 59 | foreach ($transactionSearchDTO->reason as $reason) { |
| 60 | foreach ($reason as $value) { |
| 61 | array_push($reasons, $value); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | $reasons = array_unique($reasons); |
| 66 | |
| 67 | $this->whereIn('reason', $reasons); |
| 68 | } |
| 69 | |
| 70 | if ($transactionSearchDTO->created_at) { |
| 71 | $this->whereBetween('created_at', [ |
| 72 | $transactionSearchDTO->created_at[0], |
| 73 | $transactionSearchDTO->created_at[1], |
| 74 | ]); |
| 75 | } |
| 76 | |
| 77 | return $this; |
| 78 | } |
| 79 | |
| 80 | public function transferable(Transferable $transferable): static |
| 81 | { |
| 82 | $this->where(function (Builder $query) use ($transferable) { |
| 83 | $query->orWhere('to_transferable_id', $transferable->getKey()); |
| 84 | $query->orWhere('from_transferable_id', $transferable->getKey()); |
| 85 | }); |
| 86 | |
| 87 | return $this; |
| 88 | } |
| 89 | } |