| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Filter; |
| 4 |
|
| 5 |
use FluentCart\App\Models\Coupon; |
| 6 |
use FluentCart\App\Services\DateTime\DateTime; |
| 7 |
use FluentCart\Framework\Support\Str; |
| 8 |
|
| 9 |
class CouponFilter extends BaseFilter |
| 10 |
{ |
| 11 |
|
| 12 |
public function applySimpleFilter(?string $search = null): void |
| 13 |
{ |
| 14 |
|
| 15 |
$this->query->when($search ?? $this->search, function ($query, $search) { |
| 16 |
return $query |
| 17 |
->where(function ($query) use ($search) { |
| 18 |
if (Str::of($search)->contains('%')) { |
| 19 |
$query |
| 20 |
->where('type', 'percentage') |
| 21 |
->search([ |
| 22 |
'amount' => [ |
| 23 |
'column' => 'amount', |
| 24 |
'operator' => 'like_all', |
| 25 |
'value' => Str::of($search)->remove('%')->toString() |
| 26 |
], |
| 27 |
]); |
| 28 |
} else { |
| 29 |
|
| 30 |
$searchArray = [ |
| 31 |
'title' => [ |
| 32 |
'column' => 'title', |
| 33 |
'operator' => 'like_all', |
| 34 |
'value' => $search |
| 35 |
], |
| 36 |
'code' => [ |
| 37 |
'column' => 'code', |
| 38 |
'operator' => 'or_like_all', |
| 39 |
'value' => $search |
| 40 |
], |
| 41 |
'id' => [ |
| 42 |
'column' => 'id', |
| 43 |
'operator' => 'or_like_all', |
| 44 |
'value' => $search |
| 45 |
] |
| 46 |
]; |
| 47 |
if (is_numeric($search)) { |
| 48 |
$searchArray['amount'] = [ |
| 49 |
'column' => 'amount', |
| 50 |
'operator' => 'or_where', |
| 51 |
'value' => $search * 100 |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
$query->search($searchArray); |
| 57 |
} |
| 58 |
}); |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public function tabsMap(): array |
| 64 |
{ |
| 65 |
return [ |
| 66 |
'active' => 'status', |
| 67 |
//'disabled' => 'status', |
| 68 |
'expired' => 'status', |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
public function getModel(): string |
| 73 |
{ |
| 74 |
return Coupon::class; |
| 75 |
} |
| 76 |
|
| 77 |
public static function getFilterName(): string |
| 78 |
{ |
| 79 |
return 'coupons'; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public function applyActiveViewFilter(?string $activeView = null): void |
| 84 |
{ |
| 85 |
$activeView = $activeView ?? $this->activeView; |
| 86 |
$tabsMap = $this->tabsMap(); |
| 87 |
|
| 88 |
if ($activeView === 'expired') { |
| 89 |
$this->query->where(function ($query) { |
| 90 |
$query->where('end_date', '<', DateTime::gmtNow()) |
| 91 |
->where('end_date', '!=', '0000-00-00 00:00:00') |
| 92 |
->whereNotNull('end_date'); |
| 93 |
}) |
| 94 |
->orWhere('status', '!=', 'active'); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$this->query->when($activeView, function ($query, $activeView) use ($tabsMap) { |
| 99 |
$query->where($tabsMap[$activeView], $activeView); |
| 100 |
}); |
| 101 |
} |
| 102 |
} |
| 103 |
|