| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\App\Models\TaxRate; |
| 6 |
use FluentCart\App\Models\TaxClass; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCart\App\Modules\Tax\TaxModule; |
| 9 |
use FluentCart\App\Services\Tax\TaxManager; |
| 10 |
use FluentCart\Framework\Http\Request\Request; |
| 11 |
|
| 12 |
class TaxEUController extends Controller |
| 13 |
{ |
| 14 |
public function saveEuVatSettings(Request $request) |
| 15 |
{ |
| 16 |
if ($request->getSafe('action', 'sanitize_text_field') === 'euCrossBorderSettings') { |
| 17 |
return $this->euCrossBorderSettings($request); |
| 18 |
} else { |
| 19 |
return $this->sendError([ |
| 20 |
'message' => __('Invalid method', 'fluent-cart') |
| 21 |
], 423); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public function getEuTaxRates(Request $request) |
| 26 |
{ |
| 27 |
$taxRates = TaxRate::query()->where('group', 'EU')->select('group', 'country', 'name', 'rate', 'class_id','for_shipping') |
| 28 |
->orderBy('group') |
| 29 |
->orderBy('country') |
| 30 |
->orderBy('class_id') |
| 31 |
->get(); |
| 32 |
|
| 33 |
$taxManager = TaxManager::getInstance(); |
| 34 |
$groupedTaxRates = $taxManager->groupTaxRatesByGroup($taxRates); |
| 35 |
|
| 36 |
return $this->sendSuccess([ |
| 37 |
'tax_rates' => $groupedTaxRates |
| 38 |
]); |
| 39 |
} |
| 40 |
|
| 41 |
public function saveOssTaxOverride(Request $request) |
| 42 |
{ |
| 43 |
$newOssTaxOverride = $request->get('overrides', []); |
| 44 |
$countryCode = $request->get('country_code'); |
| 45 |
|
| 46 |
$errors = []; |
| 47 |
if (!$countryCode) { |
| 48 |
$errors['country_code'] = __('Select country of OSS registration', 'fluent-cart'); |
| 49 |
} |
| 50 |
|
| 51 |
if ($errors) { |
| 52 |
return $this->sendError([ |
| 53 |
'message' => __('Validation failed for OSS tax override', 'fluent-cart'), |
| 54 |
'errors' => $errors |
| 55 |
], 423); |
| 56 |
} |
| 57 |
|
| 58 |
$taxClassesFromType = TaxClass::query()->whereIn('slug', ['standard', 'reduced', 'zero'])->get()->mapWithKeys(function ($item) { |
| 59 |
return [$item->slug => $item->id]; |
| 60 |
})->toArray(); |
| 61 |
|
| 62 |
|
| 63 |
foreach ($newOssTaxOverride as $override) { |
| 64 |
if (!isset($override['type']) || !isset($taxClassesFromType[$override['type']])) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
$taxClassId = $taxClassesFromType[$override['type']]; |
| 69 |
|
| 70 |
$existingTaxRate = TaxRate::query()->where('country', $countryCode)->where('group', 'EU')-> |
| 71 |
where('class_id', intval($taxClassId))->first(); |
| 72 |
|
| 73 |
|
| 74 |
$overrideRate = Arr::get($override, 'rate'); |
| 75 |
if ($existingTaxRate) { |
| 76 |
$existingTaxRate->rate = $overrideRate; |
| 77 |
$existingTaxRate->save(); |
| 78 |
} else { |
| 79 |
TaxRate::query()->create([ |
| 80 |
'country' => $countryCode, |
| 81 |
'name' => $override['type'], |
| 82 |
'rate' => $overrideRate, |
| 83 |
'group' => 'EU', |
| 84 |
'class_id' => intval($taxClassId), |
| 85 |
'tax_rate' => $overrideRate |
| 86 |
]); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return $this->sendSuccess([ |
| 91 |
'message' => __('OSS tax override saved successfully', 'fluent-cart') |
| 92 |
]); |
| 93 |
} |
| 94 |
|
| 95 |
public function saveOssShippingOverride(Request $request) |
| 96 |
{ |
| 97 |
$newOssTaxOverride = $request->get('overrides', []); |
| 98 |
$countryCode = $request->get('country_code'); |
| 99 |
|
| 100 |
$errors = []; |
| 101 |
if (!$countryCode) { |
| 102 |
$errors['country_code'] = __('Select country of OSS registration', 'fluent-cart'); |
| 103 |
} |
| 104 |
|
| 105 |
if ($errors) { |
| 106 |
return $this->sendError([ |
| 107 |
'message' => __('Validation failed for OSS tax override', 'fluent-cart'), |
| 108 |
'errors' => $errors |
| 109 |
], 423); |
| 110 |
} |
| 111 |
|
| 112 |
$taxClassesFromType = TaxClass::query()->whereIn('slug', ['standard', 'reduced', 'zero'])->get()->mapWithKeys(function ($item) { |
| 113 |
return [$item->slug => $item->id]; |
| 114 |
})->toArray(); |
| 115 |
|
| 116 |
|
| 117 |
foreach ($newOssTaxOverride as $override) { |
| 118 |
if (!isset($override['type']) || !isset($taxClassesFromType[$override['type']])) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
$taxClassId = $taxClassesFromType[$override['type']]; |
| 123 |
|
| 124 |
$existingTaxRate = TaxRate::query()->where('country', $countryCode)->where('group', 'EU')-> |
| 125 |
where('class_id', intval($taxClassId))->first(); |
| 126 |
|
| 127 |
|
| 128 |
if ($existingTaxRate) { |
| 129 |
$existingTaxRate->rate = $override['rate']; |
| 130 |
$existingTaxRate->save(); |
| 131 |
} else { |
| 132 |
TaxRate::query()->create([ |
| 133 |
'country' => $countryCode, |
| 134 |
'name' => $override['type'], |
| 135 |
'rate' => $override['rate'], |
| 136 |
'for_shipping' => $override['for_shipping'] ?? 0, |
| 137 |
'group' => 'EU', |
| 138 |
'class_id' => intval($taxClassId), |
| 139 |
'tax_rate' => $override['rate'] |
| 140 |
]); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $this->sendSuccess([ |
| 145 |
'message' => __('OSS tax override saved successfully', 'fluent-cart') |
| 146 |
]); |
| 147 |
} |
| 148 |
|
| 149 |
public function deleteOssTaxOverride(Request $request) |
| 150 |
{ |
| 151 |
$countryCode = $request->getSafe('country', 'sanitize_text_field'); |
| 152 |
$state = $request->getSafe('state', 'sanitize_text_field'); |
| 153 |
|
| 154 |
if (!$countryCode) { |
| 155 |
return $this->sendError([ |
| 156 |
'message' => __('Country code is required', 'fluent-cart') |
| 157 |
], 423); |
| 158 |
} |
| 159 |
|
| 160 |
// select on base of countryCode, state if given and group eu |
| 161 |
$query = TaxRate::query()->where('country', $countryCode)->where('group', 'EU'); |
| 162 |
if ($state) { |
| 163 |
$query->where('state', $state); |
| 164 |
} |
| 165 |
|
| 166 |
$deleted = $query->delete(); |
| 167 |
|
| 168 |
if (!$deleted) { |
| 169 |
return $this->sendError([ |
| 170 |
'message' => __('No matching OSS tax override found to delete', 'fluent-cart') |
| 171 |
], 423); |
| 172 |
} |
| 173 |
|
| 174 |
return $this->sendSuccess([ |
| 175 |
'message' => __('OSS tax override deleted successfully', 'fluent-cart') |
| 176 |
]); |
| 177 |
} |
| 178 |
|
| 179 |
public function deleteOssShippingOverride(Request $request) |
| 180 |
{ |
| 181 |
$countryCode = $request->getSafe('country', 'sanitize_text_field'); |
| 182 |
$state = $request->getSafe('state', 'sanitize_text_field'); |
| 183 |
|
| 184 |
if (!$countryCode) { |
| 185 |
return $this->sendError([ |
| 186 |
'message' => __('Country code is required', 'fluent-cart') |
| 187 |
], 423); |
| 188 |
} |
| 189 |
|
| 190 |
// select on base of countryCode, state if given and group eu |
| 191 |
$query = TaxRate::query()->where('country', $countryCode)->where('group', 'EU'); |
| 192 |
if ($state) { |
| 193 |
$query->where('state', $state); |
| 194 |
} |
| 195 |
|
| 196 |
$deleted = $query->delete(); |
| 197 |
|
| 198 |
if (!$deleted) { |
| 199 |
return $this->sendError([ |
| 200 |
'message' => __('No matching OSS shipping override found to delete', 'fluent-cart') |
| 201 |
], 423); |
| 202 |
} |
| 203 |
|
| 204 |
return $this->sendSuccess([ |
| 205 |
'message' => __('OSS shipping override deleted successfully', 'fluent-cart') |
| 206 |
]); |
| 207 |
} |
| 208 |
|
| 209 |
public function euCrossBorderSettings(Request $request) |
| 210 |
{ |
| 211 |
$newEuVatSettings = $request->get('eu_vat_settings', []); |
| 212 |
$sanitizedEuVatSettings = array_map('sanitize_text_field', $newEuVatSettings); |
| 213 |
$method = Arr::get($sanitizedEuVatSettings, 'method'); |
| 214 |
$errors = []; |
| 215 |
|
| 216 |
if (!in_array($method, ['oss', 'home', 'specific'], true)) { |
| 217 |
$errors['method'] = __('Select a cross-border registration type', 'fluent-cart'); |
| 218 |
} else if ($method === 'oss') { |
| 219 |
if (!Arr::get($sanitizedEuVatSettings, 'oss_country')) { |
| 220 |
$errors['oss_country'] = __('Select country of OSS registration', 'fluent-cart'); |
| 221 |
} |
| 222 |
} else if ($method === 'home') { |
| 223 |
if (!Arr::get($sanitizedEuVatSettings, 'home_country')) { |
| 224 |
$errors['home_country'] = __('Select home country of registration', 'fluent-cart'); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if ($errors) { |
| 229 |
return $this->sendError([ |
| 230 |
'message' => __('Validation failed for EU VAT settings', 'fluent-cart'), |
| 231 |
'errors' => $errors |
| 232 |
], 423); |
| 233 |
} |
| 234 |
$currentSettings = (new TaxModule())->getSettings(); |
| 235 |
|
| 236 |
$currentSettings['eu_vat_settings'] = array_merge( |
| 237 |
Arr::get($currentSettings, 'eu_vat_settings', []), |
| 238 |
$sanitizedEuVatSettings |
| 239 |
); |
| 240 |
|
| 241 |
if ($request->getSafe('reset_registration', 'sanitize_text_field') === 'yes') { |
| 242 |
Arr::set($currentSettings['eu_vat_settings'], 'method', ''); |
| 243 |
} |
| 244 |
|
| 245 |
update_option('fluent_cart_tax_configuration_settings', $currentSettings, true); |
| 246 |
|
| 247 |
return $this->sendSuccess([ |
| 248 |
'message' => __('EU VAT settings saved successfully', 'fluent-cart') |
| 249 |
]); |
| 250 |
|
| 251 |
} |
| 252 |
} |