← All changes
|
app/Http/Controllers/TaxConfigurationController.php
+52
-9
1.3.19
→
1.6.6
View file →
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCart\App\Http\Controllers; |
| 4 | 4 | |
| 5 | +use FluentCart\Api\StoreSettings; | |
| 6 | +use FluentCart\App\Models\TaxRate; | |
| 5 | 7 | use FluentCart\Framework\Support\Arr; |
| 6 | 8 | use FluentCart\App\Modules\Tax\TaxModule; |
| 7 | 9 | use FluentCart\App\Services\Tax\TaxManager; |
| 8 | 10 | use FluentCart\Framework\Http\Request\Request; |
| @@ -20,9 +22,11 @@ | ||
| 20 | 22 | } |
| 21 | 23 | |
| 22 | 24 | public function saveConfiguredCountries(Request $request) |
| 23 | 25 | { |
| 24 | - $countryCodes = $request->get('countries', []); | |
| 26 | + $countryCodes = array_values(array_filter( | |
| 27 | + array_map('sanitize_text_field', (array) $request->get('countries', [])) | |
| 28 | + )); | |
| 25 | 29 | $taxManager = TaxManager::getInstance(); |
| 26 | 30 | $taxManager->generateTaxClasses($countryCodes); |
| 27 | 31 | |
| 28 | 32 | return $this->sendSuccess([ |
| @@ -32,11 +36,13 @@ | ||
| 32 | 36 | |
| 33 | 37 | public function getSettings() |
| 34 | 38 | { |
| 35 | 39 | $settings = (new TaxModule())->getSettings(); |
| 40 | + $storeSettings = new StoreSettings(); | |
| 36 | 41 | |
| 37 | 42 | return $this->sendSuccess([ |
| 38 | - 'settings' => $settings | |
| 43 | + 'settings' => $settings, | |
| 44 | + 'store_country' => $storeSettings->get('store_country') ?: '', | |
| 39 | 45 | ]); |
| 40 | 46 | } |
| 41 | 47 | |
| 42 | 48 | public function saveSettings(Request $request) |
| @@ -50,10 +56,15 @@ | ||
| 50 | 56 | foreach ($eu as $ek => $ev) { |
| 51 | 57 | if ($ek === 'vat_reverse_excluded_categories') { |
| 52 | 58 | $cats = is_array($ev) ? $ev : []; |
| 53 | 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); | |
| 54 | 65 | } else { |
| 55 | - $eu[$ek] = is_array($ev) ? $ev : sanitize_text_field($ev); | |
| 66 | + $eu[$ek] = sanitize_text_field($ev); | |
| 56 | 67 | } |
| 57 | 68 | } |
| 58 | 69 | $settings[$key] = $eu; |
| 59 | 70 | } else if (is_array($value)) { |
| @@ -62,8 +73,21 @@ | ||
| 62 | 73 | $settings[$key] = sanitize_text_field($value); |
| 63 | 74 | } |
| 64 | 75 | } |
| 65 | 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 | + | |
| 66 | 90 | // save settings to wp_options |
| 67 | 91 | update_option('fluent_cart_tax_configuration_settings', $settings, true); |
| 68 | 92 | |
| 69 | 93 | if (Arr::get($settings, 'enable_tax') === 'yes') { |
| @@ -74,18 +98,38 @@ | ||
| 74 | 98 | 'message' => __('Settings saved successfully', 'fluent-cart') |
| 75 | 99 | ]); |
| 76 | 100 | } |
| 77 | 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 | + | |
| 78 | 117 | private function defaultSettings() |
| 79 | 118 | { |
| 80 | 119 | return [ |
| 81 | - 'tax_inclusion' => 'included', | |
| 82 | - 'tax_calculation_basis' => 'shipping', | |
| 83 | - 'tax_rounding' => 'item', | |
| 84 | - 'enable_tax' => 'no', | |
| 85 | - 'eu_vat_settings' => [ | |
| 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' => [ | |
| 86 | 129 | 'require_vat_number' => 'no', |
| 87 | 130 | 'local_reverse_charge' => 'yes', |
| 131 | + 'reverse_charge_price_mode' => 'fixed', | |
| 88 | 132 | 'vat_reverse_excluded_categories' => [], |
| 89 | 133 | 'country_wise_vat' => [] |
| 90 | 134 | ] |
| 91 | 135 | ]; |
| @@ -91,5 +135,4 @@ | ||
| 91 | 135 | ]; |
| 92 | 136 | } |
| 93 | 137 | |
| 94 | 138 | } |
| 95 | - | |