| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\Models\TaxRate; |
| 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 TaxConfigurationController extends Controller |
| 13 |
{ |
| 14 |
public function getTaxRates() |
| 15 |
{ |
| 16 |
$taxManager = TaxManager::getInstance(); |
| 17 |
$rates = $taxManager->getTaxRatesFromTaxPhp(); |
| 18 |
|
| 19 |
return $this->sendSuccess([ |
| 20 |
'tax_rates' => $rates |
| 21 |
]); |
| 22 |
} |
| 23 |
|
| 24 |
public function saveConfiguredCountries(Request $request) |
| 25 |
{ |
| 26 |
$countryCodes = array_values(array_filter( |
| 27 |
array_map('sanitize_text_field', (array) $request->get('countries', [])) |
| 28 |
)); |
| 29 |
$taxManager = TaxManager::getInstance(); |
| 30 |
$taxManager->generateTaxClasses($countryCodes); |
| 31 |
|
| 32 |
return $this->sendSuccess([ |
| 33 |
'message' => __('Countries saved successfully', 'fluent-cart') |
| 34 |
]); |
| 35 |
} |
| 36 |
|
| 37 |
public function getSettings() |
| 38 |
{ |
| 39 |
$settings = (new TaxModule())->getSettings(); |
| 40 |
$storeSettings = new StoreSettings(); |
| 41 |
|
| 42 |
return $this->sendSuccess([ |
| 43 |
'settings' => $settings, |
| 44 |
'store_country' => $storeSettings->get('store_country') ?: '', |
| 45 |
]); |
| 46 |
} |
| 47 |
|
| 48 |
public function saveSettings(Request $request) |
| 49 |
{ |
| 50 |
$settings = $request->get('settings', $this->defaultSettings()); |
| 51 |
|
| 52 |
foreach ($settings as $key => $value) { |
| 53 |
if ($key === 'eu_vat_settings' && is_array($value)) { |
| 54 |
// Sanitize nested eu_vat_settings |
| 55 |
$eu = $value; |
| 56 |
foreach ($eu as $ek => $ev) { |
| 57 |
if ($ek === 'vat_reverse_excluded_categories') { |
| 58 |
$cats = is_array($ev) ? $ev : []; |
| 59 |
$eu[$ek] = array_values(array_filter(array_map('intval', $cats), function ($v) { return $v > 0; })); |
| 60 |
} elseif ($ek === 'reverse_charge_price_mode') { |
| 61 |
$sanitized = sanitize_text_field($ev); |
| 62 |
$eu[$ek] = in_array($sanitized, ['fixed', 'dynamic'], true) ? $sanitized : 'fixed'; |
| 63 |
} elseif (is_array($ev)) { |
| 64 |
$eu[$ek] = self::sanitizeNestedArray($ev); |
| 65 |
} else { |
| 66 |
$eu[$ek] = sanitize_text_field($ev); |
| 67 |
} |
| 68 |
} |
| 69 |
$settings[$key] = $eu; |
| 70 |
} else if (is_array($value)) { |
| 71 |
$settings[$key] = array_map('sanitize_text_field', $value); |
| 72 |
} else { |
| 73 |
$settings[$key] = sanitize_text_field($value); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
$enumDefaults = [ |
| 78 |
'tax_inclusion' => ['included', 'excluded'], |
| 79 |
'tax_calculation_basis' => ['shipping', 'billing', 'store'], |
| 80 |
'tax_rounding' => ['item', 'total', 'subtotal'], |
| 81 |
'checkout_tax_breakdown_display' => ['itemized', 'simplified'], |
| 82 |
'enable_tax' => ['yes', 'no'], |
| 83 |
]; |
| 84 |
foreach ($enumDefaults as $key => $allowed) { |
| 85 |
if (isset($settings[$key]) && !in_array($settings[$key], $allowed, true)) { |
| 86 |
$settings[$key] = $allowed[0]; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// save settings to wp_options |
| 91 |
update_option('fluent_cart_tax_configuration_settings', $settings, true); |
| 92 |
|
| 93 |
if (Arr::get($settings, 'enable_tax') === 'yes') { |
| 94 |
(new TaxClassController())->checkAndCreateInitialTaxClasses(); |
| 95 |
} |
| 96 |
|
| 97 |
return $this->sendSuccess([ |
| 98 |
'message' => __('Settings saved successfully', 'fluent-cart') |
| 99 |
]); |
| 100 |
} |
| 101 |
|
| 102 |
private static function sanitizeNestedArray(array $data) |
| 103 |
{ |
| 104 |
$sanitized = []; |
| 105 |
foreach ($data as $key => $value) { |
| 106 |
if (is_array($value)) { |
| 107 |
$sanitized[$key] = self::sanitizeNestedArray($value); |
| 108 |
} else if (is_numeric($value)) { |
| 109 |
$sanitized[$key] = $value + 0; |
| 110 |
} else { |
| 111 |
$sanitized[$key] = sanitize_text_field($value); |
| 112 |
} |
| 113 |
} |
| 114 |
return $sanitized; |
| 115 |
} |
| 116 |
|
| 117 |
private function defaultSettings() |
| 118 |
{ |
| 119 |
return [ |
| 120 |
'tax_inclusion' => 'included', |
| 121 |
'tax_calculation_basis' => 'shipping', |
| 122 |
'tax_rounding' => 'item', |
| 123 |
'checkout_tax_breakdown_display' => 'itemized', |
| 124 |
'tax_display_label' => 'Tax', |
| 125 |
'enable_tax' => 'no', |
| 126 |
'price_suffix_included' => '', |
| 127 |
'price_suffix_excluded' => '', |
| 128 |
'eu_vat_settings' => [ |
| 129 |
'require_vat_number' => 'no', |
| 130 |
'local_reverse_charge' => 'yes', |
| 131 |
'reverse_charge_price_mode' => 'fixed', |
| 132 |
'vat_reverse_excluded_categories' => [], |
| 133 |
'country_wise_vat' => [] |
| 134 |
] |
| 135 |
]; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|