PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Http / Controllers / TaxConfigurationController.php

TaxConfigurationController.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Http/Controllers/TaxConfigurationController.php

139 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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