| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Helpers\AddressHelper; |
| 7 |
use FluentCart\App\Http\Requests\TaxClassRequest; |
| 8 |
use FluentCart\App\Http\Requests\TaxRateRequest; |
| 9 |
use FluentCart\App\Models\Meta; |
| 10 |
use FluentCart\App\Models\TaxClass; |
| 11 |
use FluentCart\App\Models\TaxRate; |
| 12 |
use FluentCart\App\Services\Localization\LocalizationManager; |
| 13 |
use FluentCart\App\Services\Tax\TaxManager; |
| 14 |
use FluentCart\Framework\Http\Request\Request; |
| 15 |
use FluentCart\Framework\Support\Arr; |
| 16 |
use FluentCart\Framework\Support\Str; |
| 17 |
|
| 18 |
class TaxRateController extends Controller |
| 19 |
{ |
| 20 |
|
| 21 |
public function index(Request $request) |
| 22 |
{ |
| 23 |
$taxManager = TaxManager::getInstance(); |
| 24 |
$rates = $taxManager->getTaxRates(); |
| 25 |
|
| 26 |
return $this->sendSuccess([ |
| 27 |
'tax_rates' => $rates |
| 28 |
]); |
| 29 |
} |
| 30 |
|
| 31 |
public function show(Request $request) |
| 32 |
{ |
| 33 |
$countryCode = sanitize_text_field($request->get('country_code')); |
| 34 |
|
| 35 |
// get taxRates by $countryCode |
| 36 |
$taxRates = TaxRate::query() |
| 37 |
->where('country', $countryCode) |
| 38 |
->with(['tax_class:id,title']) |
| 39 |
->orderBy('priority', 'DESC') |
| 40 |
->get(); |
| 41 |
|
| 42 |
$taxManager = TaxManager::getInstance(); |
| 43 |
$settings = $taxManager->getCountryConfiguration($countryCode); |
| 44 |
|
| 45 |
return $this->sendSuccess([ |
| 46 |
'tax_rates' => $taxRates, |
| 47 |
'settings' => $settings |
| 48 |
]); |
| 49 |
} |
| 50 |
|
| 51 |
public function update(TaxRateRequest $request, $id) |
| 52 |
{ |
| 53 |
$data = $request->getSafe($request->sanitize()); |
| 54 |
|
| 55 |
$taxRate = TaxRate::query()->findOrFail($id); |
| 56 |
$isUpdated = $taxRate->update($data); |
| 57 |
|
| 58 |
if (!$isUpdated) { |
| 59 |
return $this->sendError([ |
| 60 |
'message' => __('Failed to update tax rate', 'fluent-cart') |
| 61 |
]); |
| 62 |
} |
| 63 |
$taxClass = TaxClass::query()->select('id', 'title')->find($taxRate->class_id); |
| 64 |
$taxRate->tax_class = [ |
| 65 |
'id' => 0, |
| 66 |
'title' => '' |
| 67 |
]; |
| 68 |
if ($taxClass) { |
| 69 |
$taxRate->tax_class = $taxClass; |
| 70 |
} |
| 71 |
|
| 72 |
return $this->sendSuccess([ |
| 73 |
'tax_rate' => $taxRate, |
| 74 |
'message' => __('Tax rate has been updated successfully', 'fluent-cart') |
| 75 |
]); |
| 76 |
} |
| 77 |
|
| 78 |
public function store(TaxRateRequest $request) |
| 79 |
{ |
| 80 |
$data = $request->getSafe($request->sanitize()); |
| 81 |
|
| 82 |
$classId = Arr::get($data, 'class_id'); |
| 83 |
|
| 84 |
if (!$classId) { |
| 85 |
return $this->sendError([ |
| 86 |
'message' => __('Tax class is required', 'fluent-cart') |
| 87 |
]); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
$taxRate = TaxRate::query()->create($data); |
| 92 |
|
| 93 |
if (!$taxRate) { |
| 94 |
return $this->sendError([ |
| 95 |
'message' => __('Failed to create tax rate', 'fluent-cart') |
| 96 |
]); |
| 97 |
} |
| 98 |
|
| 99 |
$taxClass = TaxClass::query()->select('id', 'title')->find($taxRate->class_id); |
| 100 |
$taxRate->tax_class = [ |
| 101 |
'id' => 0, |
| 102 |
'title' => '' |
| 103 |
]; |
| 104 |
if ($taxClass) { |
| 105 |
$taxRate->tax_class = $taxClass; |
| 106 |
} |
| 107 |
|
| 108 |
return $this->sendSuccess([ |
| 109 |
'tax_rate' => $taxRate, |
| 110 |
'message' => __('Tax rate has been created successfully', 'fluent-cart') |
| 111 |
]); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
public function delete(Request $request, $id) |
| 116 |
{ |
| 117 |
$taxRate = TaxRate::query()->findOrFail($id); |
| 118 |
$isDeleted = $taxRate->delete(); |
| 119 |
|
| 120 |
if (!$isDeleted) { |
| 121 |
return $this->sendError([ |
| 122 |
'message' => __('Failed to delete tax rate', 'fluent-cart') |
| 123 |
]); |
| 124 |
} |
| 125 |
|
| 126 |
return $this->sendSuccess([ |
| 127 |
'message' => __('Tax rate has been deleted successfully', 'fluent-cart') |
| 128 |
]); |
| 129 |
} |
| 130 |
|
| 131 |
public function deleteCountry(Request $request, $country_code) |
| 132 |
{ |
| 133 |
$countryCode = sanitize_text_field($country_code); |
| 134 |
|
| 135 |
$taxRates = TaxRate::query()->where('country', $countryCode)->delete(); |
| 136 |
|
| 137 |
if (!$taxRates) { |
| 138 |
return $this->sendError([ |
| 139 |
'message' => __('Failed to delete country', 'fluent-cart') |
| 140 |
]); |
| 141 |
} |
| 142 |
|
| 143 |
return $this->sendSuccess([ |
| 144 |
'message' => __('Country has been deleted successfully', 'fluent-cart') |
| 145 |
]); |
| 146 |
} |
| 147 |
|
| 148 |
public function getCountryTaxId(Request $request, $country_code) |
| 149 |
{ |
| 150 |
$countryCode = sanitize_text_field($country_code); |
| 151 |
$taxData = Meta::query() |
| 152 |
->where('meta_key', 'fluent_cart_tax_id_' . $countryCode) |
| 153 |
->where('object_type', 'tax') |
| 154 |
->value('meta_value'); |
| 155 |
|
| 156 |
if (!$taxData) { |
| 157 |
return $this->sendSuccess([ |
| 158 |
'tax_data' => [ |
| 159 |
'tax_id' => '' |
| 160 |
] |
| 161 |
]); |
| 162 |
} |
| 163 |
|
| 164 |
return $this->sendSuccess([ |
| 165 |
'tax_data' => $taxData |
| 166 |
]); |
| 167 |
} |
| 168 |
|
| 169 |
public function saveCountryTaxId(Request $request, $country_code) |
| 170 |
{ |
| 171 |
$countryCode = sanitize_text_field($country_code); |
| 172 |
$taxId = sanitize_text_field($request->get('tax_id')); |
| 173 |
|
| 174 |
$data = [ |
| 175 |
'tax_id' => $taxId |
| 176 |
]; |
| 177 |
|
| 178 |
// save taxId to fct_meta |
| 179 |
$meta = Meta::query() |
| 180 |
->where('meta_key', 'fluent_cart_tax_id_' . $countryCode) |
| 181 |
->where('object_type', 'tax') |
| 182 |
->first(); |
| 183 |
|
| 184 |
if ($meta) { |
| 185 |
$meta->meta_value = $data; |
| 186 |
$meta->save(); |
| 187 |
} else { |
| 188 |
Meta::query()->create([ |
| 189 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 190 |
'meta_key' => 'fluent_cart_tax_id_' . $countryCode, |
| 191 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 192 |
'meta_value' => [ |
| 193 |
'tax_id' => $taxId |
| 194 |
], |
| 195 |
'object_type' => 'tax' |
| 196 |
]); |
| 197 |
} |
| 198 |
|
| 199 |
return $this->sendSuccess([ |
| 200 |
'message' => __('Tax ID has been saved successfully', 'fluent-cart') |
| 201 |
]); |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
public function deleteShippingOverride(Request $request, $id) |
| 206 |
{ |
| 207 |
$taxRate = TaxRate::query()->findOrFail($id); |
| 208 |
$taxRate->for_shipping = null; |
| 209 |
$taxRate->save(); |
| 210 |
|
| 211 |
return $this->sendSuccess([ |
| 212 |
'message' => __('Shipping override has been deleted successfully', 'fluent-cart') |
| 213 |
]); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public function saveShippingOverride(Request $request) |
| 218 |
{ |
| 219 |
$taxRate = TaxRate::query()->findOrFail($request->getSafe('id', 'intval')); |
| 220 |
|
| 221 |
if (!$taxRate) { |
| 222 |
return $this->sendError([ |
| 223 |
'message' => __('Tax rate not found', 'fluent-cart') |
| 224 |
]); |
| 225 |
} |
| 226 |
|
| 227 |
$taxRate->for_shipping = $request->getSafe('override_tax_rate', 'intval'); |
| 228 |
$taxRate->save(); |
| 229 |
|
| 230 |
return $this->sendSuccess([ |
| 231 |
'message' => __('Tax override has been saved successfully', 'fluent-cart') |
| 232 |
]); |
| 233 |
} |
| 234 |
|
| 235 |
public function addCountry(Request $request) |
| 236 |
{ |
| 237 |
$countryCode = sanitize_text_field($request->get('country')); |
| 238 |
$taxClassId = intval($request->get('class_id')); |
| 239 |
if (!$countryCode) { |
| 240 |
return $this->sendError([ |
| 241 |
'message' => __('Country code is required', 'fluent-cart') |
| 242 |
]); |
| 243 |
} |
| 244 |
|
| 245 |
if (!$taxClassId) { |
| 246 |
return $this->sendError([ |
| 247 |
'message' => __('Tax class is required', 'fluent-cart') |
| 248 |
]); |
| 249 |
} |
| 250 |
|
| 251 |
$taxClass = TaxClass::query()->select('id')->find($taxClassId); |
| 252 |
if (!$taxClass) { |
| 253 |
return $this->sendError([ |
| 254 |
'message' => __('Tax class not found', 'fluent-cart') |
| 255 |
]); |
| 256 |
} |
| 257 |
|
| 258 |
$localization = App::localization(); |
| 259 |
$continent = $localization->continentFromCountry($countryCode); |
| 260 |
|
| 261 |
$taxRate = TaxRate::query()->create([ |
| 262 |
'country' => $countryCode, |
| 263 |
'group' => $continent, |
| 264 |
'class_id' => $taxClass->id, |
| 265 |
]); |
| 266 |
|
| 267 |
if (!$taxRate) { |
| 268 |
return $this->sendError([ |
| 269 |
'message' => __('Failed to add country', 'fluent-cart') |
| 270 |
]); |
| 271 |
} |
| 272 |
|
| 273 |
return $this->sendSuccess([ |
| 274 |
'message' => __('Country has been added successfully', 'fluent-cart') |
| 275 |
]); |
| 276 |
} |
| 277 |
|
| 278 |
} |
| 279 |
|