| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Filter; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Status; |
| 6 |
use FluentCart\App\Services\DateTime\DateTime; |
| 7 |
use FluentCartPro\App\Modules\Licensing\Models\License; |
| 8 |
use FluentCartPro\App\Modules\Promotional\Models\OrderPromotion; |
| 9 |
|
| 10 |
|
| 11 |
class OrderBumpFilter extends BaseFilter |
| 12 |
{ |
| 13 |
|
| 14 |
public function applySimpleFilter(?string $search = null): void |
| 15 |
{ |
| 16 |
$isApplied = $this->applySimpleOperatorFilter($search); |
| 17 |
if ($isApplied) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
$this->query->when($search ?? $this->search, function ($query, $search) { |
| 22 |
// If search is an array, implode it |
| 23 |
$search = is_array($search) ? implode(' ', $search) : $search; |
| 24 |
|
| 25 |
// If search is empty or null, return the query |
| 26 |
if (empty($search)) { |
| 27 |
return $query; |
| 28 |
} |
| 29 |
|
| 30 |
$query->where(function ($query) use ($search) { |
| 31 |
$query->where('title', 'like', '%' . $search . '%') |
| 32 |
->orWhere('id', 'like', '%' . $search . '%'); |
| 33 |
}); |
| 34 |
|
| 35 |
return $query; |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
public function tabsMap(): array |
| 40 |
{ |
| 41 |
return [ |
| 42 |
'active' => __('Active', 'fluent-cart'), |
| 43 |
'inactive' => __('Inactive', 'fluent-cart'), |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
public function getModel(): string |
| 48 |
{ |
| 49 |
return OrderPromotion::class; |
| 50 |
} |
| 51 |
|
| 52 |
public static function getFilterName(): string |
| 53 |
{ |
| 54 |
return 'order_bump'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return array<string, array<string, mixed>> |
| 59 |
*/ |
| 60 |
protected static function sortableColumns(): array |
| 61 |
{ |
| 62 |
return [ |
| 63 |
'id' => ['label' => __('Order Bump ID', 'fluent-cart'), 'column' => 'id'], |
| 64 |
'title' => ['label' => __('Title', 'fluent-cart'), 'column' => 'title'], |
| 65 |
'created_at' => ['label' => __('Created at', 'fluent-cart'), 'column' => 'created_at'], |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Intentionally empty. Nothing instantiates this filter: the order-bump |
| 71 |
* list endpoint is Pro's OrderBumpController::index(), which builds its own |
| 72 |
* OrderPromotion query and hardcodes `->with(['product_variant.product'])`. |
| 73 |
* OrderBumpTable.js's `product_variant` therefore never reaches this map. |
| 74 |
* |
| 75 |
* If the Pro controller is ever moved onto this filter, declare |
| 76 |
* `product_variant` (and its `.product` child) here — do not widen the map |
| 77 |
* speculatively before that happens. |
| 78 |
* |
| 79 |
* @return array<string, callable> |
| 80 |
*/ |
| 81 |
protected function allowedWiths(): array |
| 82 |
{ |
| 83 |
return []; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
public function applyActiveViewFilter(?string $activeView = null): void |
| 88 |
{ |
| 89 |
$activeView = $activeView ?? $this->activeView; |
| 90 |
$this->query->when($activeView, function ($query, $activeView) { |
| 91 |
$validStatuses = [ |
| 92 |
'active', |
| 93 |
'draft' |
| 94 |
]; |
| 95 |
|
| 96 |
if (in_array($activeView, $validStatuses)) { |
| 97 |
if ($activeView == 'expired') { |
| 98 |
$query->where('expiration_date', '<', DateTime::gmtNow()); |
| 99 |
} else if ($activeView == 'active') { |
| 100 |
$query->where(function ($query) { |
| 101 |
$query->where('expiration_date', '>', DateTime::gmtNow()) |
| 102 |
->orWhereNull('expiration_date'); |
| 103 |
}) |
| 104 |
->where('status', 'active'); |
| 105 |
} else { |
| 106 |
$query->where('status', $activeView); |
| 107 |
} |
| 108 |
} else if ($activeView == 'inactive') { |
| 109 |
$query->where('status', 'active') |
| 110 |
->whereDoesntHave('activations'); |
| 111 |
} |
| 112 |
|
| 113 |
return $query; |
| 114 |
}); |
| 115 |
} |
| 116 |
|
| 117 |
public static function getSearchableFields(): array |
| 118 |
{ |
| 119 |
return [ |
| 120 |
'key' => [ |
| 121 |
'column' => 'title', |
| 122 |
'description' => 'title', |
| 123 |
'type' => 'string' |
| 124 |
] |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
public static function advanceFilterOptions(): array |
| 129 |
{ |
| 130 |
$filters = [ |
| 131 |
'product' => [ |
| 132 |
'label' => __('Products', 'fluent-cart'), |
| 133 |
'value' => 'product', |
| 134 |
'children' => [ |
| 135 |
[ |
| 136 |
'label' => __('By Products', 'fluent-cart'), |
| 137 |
'value' => 'product', |
| 138 |
'column' => 'variation_id', |
| 139 |
'filter_type' => 'relation', |
| 140 |
'relation' => 'productVariant', |
| 141 |
'remote_data_key' => 'product_variations', |
| 142 |
'type' => 'remote_tree_select', |
| 143 |
'limit' => 10, |
| 144 |
], |
| 145 |
], |
| 146 |
], |
| 147 |
'customer' => [ |
| 148 |
'label' => __('Customer Property', 'fluent-cart'), |
| 149 |
'value' => 'customer', |
| 150 |
'children' => [ |
| 151 |
[ |
| 152 |
'label' => __('Customer first name', 'fluent-cart'), |
| 153 |
'value' => 'customer_first_name', |
| 154 |
'type' => 'text', |
| 155 |
'filter_type' => 'relation', |
| 156 |
'column' => 'first_name', |
| 157 |
'relation' => 'customer', |
| 158 |
], |
| 159 |
[ |
| 160 |
'label' => __('Customer last name', 'fluent-cart'), |
| 161 |
'value' => 'customer_last_name', |
| 162 |
'type' => 'text', |
| 163 |
'filter_type' => 'relation', |
| 164 |
'column' => 'last_name', |
| 165 |
'relation' => 'customer', |
| 166 |
] |
| 167 |
], |
| 168 |
], |
| 169 |
'license' => [ |
| 170 |
'label' => __('License Property', 'fluent-cart'), |
| 171 |
'value' => 'license', |
| 172 |
'children' => [ |
| 173 |
[ |
| 174 |
'label' => __('License key', 'fluent-cart'), |
| 175 |
'value' => 'license_key', |
| 176 |
'type' => 'text', |
| 177 |
'filter_type' => 'column', |
| 178 |
'column' => 'license_key', |
| 179 |
], |
| 180 |
[ |
| 181 |
'label' => __('Status', 'fluent-cart'), |
| 182 |
'value' => 'status', |
| 183 |
'type' => 'selections', |
| 184 |
'filter_type' => 'column', |
| 185 |
'column' => 'status', |
| 186 |
'options' => [ |
| 187 |
Status::LICENSE_ACTIVE => __('Active', 'fluent-cart'), |
| 188 |
Status::LICENSE_DISABLED => __('Disabled', 'fluent-cart'), |
| 189 |
Status::LICENSE_EXPIRED => __('Expired', 'fluent-cart'), |
| 190 |
], |
| 191 |
'is_multiple' => true, |
| 192 |
'is_only_in' => true |
| 193 |
], |
| 194 |
[ |
| 195 |
'label' => __('Activation Count', 'fluent-cart'), |
| 196 |
'value' => 'activation_count', |
| 197 |
'type' => 'numeric', |
| 198 |
'filter_type' => 'column', |
| 199 |
'column' => 'activation_count', |
| 200 |
], |
| 201 |
[ |
| 202 |
'label' => __('Expiration Date', 'fluent-cart'), |
| 203 |
'value' => 'expiration_date', |
| 204 |
'type' => 'dates', |
| 205 |
'filter_type' => 'date', |
| 206 |
'column' => 'expiration_date', |
| 207 |
] |
| 208 |
], |
| 209 |
], |
| 210 |
]; |
| 211 |
return LabelFilter::advanceFilterOptionsForOther($filters); |
| 212 |
} |
| 213 |
|
| 214 |
} |
| 215 |
|