| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Filter; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\Framework\Support\Str; |
| 7 |
use FluentCart\App\Models\OrderTaxRate; |
| 8 |
use FluentCart\App\Services\Localization\LocalizationManager; |
| 9 |
|
| 10 |
class TaxFilter extends BaseFilter |
| 11 |
{ |
| 12 |
public function applySimpleFilter(?string $search = null): void |
| 13 |
{ |
| 14 |
$isApplied = $this->applySimpleOperatorFilter($search); |
| 15 |
|
| 16 |
if ($isApplied) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$this->query->when($search ?? $this->search, function ($query, $search) { |
| 21 |
return $query |
| 22 |
->where(function ($query) use ($search) { |
| 23 |
$search = trim($search); |
| 24 |
$search = Str::of($search)->remove('#')->toString(); |
| 25 |
|
| 26 |
$query->when( |
| 27 |
is_numeric($search), |
| 28 |
fn ($query) => $query->where('id', $search)->orWhere('order_id', $search) |
| 29 |
)->orWhereHas( |
| 30 |
'tax_rate', |
| 31 |
fn ($query) => $query->where('country', $search) |
| 32 |
->orWhere('state', $search) |
| 33 |
->orWhere('postcode', $search) |
| 34 |
->orWhere('name', 'LIKE', "%{$search}%") |
| 35 |
); |
| 36 |
}); |
| 37 |
}); |
| 38 |
} |
| 39 |
|
| 40 |
public function tabsMap(): array |
| 41 |
{ |
| 42 |
return [ |
| 43 |
'filed' => 'filed_at', |
| 44 |
'not_filed' => 'filed_at', |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
public function getModel(): string |
| 49 |
{ |
| 50 |
return OrderTaxRate::class; |
| 51 |
} |
| 52 |
|
| 53 |
public static function getFilterName(): string |
| 54 |
{ |
| 55 |
return 'taxes'; |
| 56 |
} |
| 57 |
|
| 58 |
public function applyActiveViewFilter(?string $activeView = null): void |
| 59 |
{ |
| 60 |
$activeView = $activeView ?? $this->activeView; |
| 61 |
if (!$activeView) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$whereMethod = $activeView === 'filed' ? 'whereNotNull' : 'whereNull'; |
| 66 |
|
| 67 |
$this->query->{$whereMethod}('filed_at'); |
| 68 |
} |
| 69 |
|
| 70 |
public static function advanceFilterOptions(): array |
| 71 |
{ |
| 72 |
return [ |
| 73 |
'tax_rates' => [ |
| 74 |
'label' => __('Tax Property', 'fluent-cart'), |
| 75 |
'value' => 'tax_rates', |
| 76 |
'children' => [ |
| 77 |
[ |
| 78 |
'label' => __('Country', 'fluent-cart'), |
| 79 |
'value' => 'country', |
| 80 |
'column' => 'country', |
| 81 |
'filter_type' => 'relation', |
| 82 |
'relation' => 'tax_rate', |
| 83 |
'remote_data_key' => 'countries', |
| 84 |
'type' => 'selections', |
| 85 |
'is_multiple' => true, |
| 86 |
'options' => TaxFilter::getCountriesOptions(), |
| 87 |
], |
| 88 |
[ |
| 89 |
'label' => __('Region', 'fluent-cart'), |
| 90 |
'value' => 'region', |
| 91 |
'column' => 'state', |
| 92 |
'filter_type' => 'relation', |
| 93 |
'relation' => 'tax_rate', |
| 94 |
'remote_data_key' => 'tax_rate_states', |
| 95 |
'type' => 'selections', |
| 96 |
'options' => static::getStatesOptions(), |
| 97 |
'is_multiple' => true, |
| 98 |
'limit' => 10, |
| 99 |
], |
| 100 |
[ |
| 101 |
'label' => __('Tax Name', 'fluent-cart'), |
| 102 |
'value' => 'name', |
| 103 |
'column' => 'name', |
| 104 |
'filter_type' => 'relation', |
| 105 |
'relation' => 'tax_rate', |
| 106 |
'type' => 'text', |
| 107 |
], |
| 108 |
[ |
| 109 |
'label' => __('Filed', 'fluent-cart'), |
| 110 |
'value' => 'filed', |
| 111 |
'column' => 'filed_at', |
| 112 |
'filter_type' => 'custom', |
| 113 |
'type' => 'selections', |
| 114 |
'options' => [ |
| 115 |
'filed' => __('Filed', 'fluent-cart'), |
| 116 |
'not_filed' => __('Not Filed', 'fluent-cart'), |
| 117 |
], |
| 118 |
'callback' => function ($query, $data) { |
| 119 |
if ($data === 'filed') { |
| 120 |
$query->whereNotNull('filed_at'); |
| 121 |
} else { |
| 122 |
$query->whereNull('filed_at'); |
| 123 |
} |
| 124 |
}, |
| 125 |
], |
| 126 |
], |
| 127 |
], |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
public static function getCountriesOptions() |
| 132 |
{ |
| 133 |
return LocalizationManager::getInstance()->countryIsoList(); |
| 134 |
} |
| 135 |
|
| 136 |
public static function getStatesOptions() |
| 137 |
{ |
| 138 |
$statesFromDB = App::db()->table('fct_tax_rates') |
| 139 |
->select(['country', 'state']) |
| 140 |
->whereNotNull('state') |
| 141 |
->where('state', '!=', '') |
| 142 |
->groupBy(['country', 'state']) |
| 143 |
->get(); |
| 144 |
|
| 145 |
$states = LocalizationManager::getInstance()->states(); |
| 146 |
|
| 147 |
$statesList = []; |
| 148 |
|
| 149 |
foreach ($statesFromDB as $state) { |
| 150 |
if (isset($states[$state->country]) && isset($states[$state->country][$state->state])) { |
| 151 |
$statesList[$state->state] = $states[$state->country][$state->state]; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return $statesList; |
| 156 |
} |
| 157 |
} |
| 158 |
|