PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.23
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.23
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Services / Filter / CouponFilter.php

CouponFilter.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.23, at app/Services/Filter/CouponFilter.php

103 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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