| @@ -7,8 +7,12 @@ | ||
| 7 | 7 | use FluentCart\Api\StoreSettings; |
| 8 | 8 | use FluentCart\App\CPT\Pages; |
| 9 | 9 | use FluentCart\App\Helpers\Helper as HelperService; |
| 10 | 10 | use FluentCart\App\Http\Requests\CreatePageRequest; |
| 11 | +use FluentCart\App\Models\Meta; | |
| 12 | +use FluentCart\App\Modules\Tax\TaxModule; | |
| 13 | +use FluentCart\App\Services\Localization\LocalizationManager; | |
| 14 | +use FluentCart\App\Services\Tax\TaxManager; | |
| 11 | 15 | use FluentCart\Framework\Foundation\Async; |
| 12 | 16 | use FluentCart\Framework\Http\Request\Request; |
| 13 | 17 | use FluentCart\Framework\Support\Arr; |
| 14 | 18 | use FluentCart\Framework\Support\Str; |
| @@ -32,12 +36,38 @@ | ||
| 32 | 36 | } |
| 33 | 37 | |
| 34 | 38 | } |
| 35 | 39 | |
| 40 | + $taxSettings = (new TaxModule())->getSettings(); | |
| 41 | + $euVatSettings = Arr::get($taxSettings, 'eu_vat_settings', []); | |
| 42 | + $euMethod = Arr::get($euVatSettings, 'method', 'oss'); | |
| 43 | + $storeCountry = strtoupper(Arr::get($defaultSettings, 'store_country', '')); | |
| 44 | + $isEuCountry = !empty($storeCountry) && (new LocalizationManager())->isEuTaxCountry($storeCountry); | |
| 45 | + | |
| 46 | + if ($isEuCountry) { | |
| 47 | + $vatNumber = $euMethod === 'oss' | |
| 48 | + ? Arr::get($euVatSettings, 'oss_vat', '') | |
| 49 | + : Arr::get($euVatSettings, 'home_vat', ''); | |
| 50 | + } elseif (!empty($storeCountry)) { | |
| 51 | + $nonEuMeta = Meta::query() | |
| 52 | + ->where('meta_key', 'fluent_cart_tax_id_' . $storeCountry) | |
| 53 | + ->where('object_type', 'tax') | |
| 54 | + ->first(); | |
| 55 | + $vatNumber = $nonEuMeta ? Arr::get((array) $nonEuMeta->meta_value, 'tax_id', '') : ''; | |
| 56 | + } else { | |
| 57 | + $vatNumber = ''; | |
| 58 | + } | |
| 59 | + | |
| 36 | 60 | return $this->response->sendSuccess([ |
| 37 | 61 | 'pages' => Pages::getPages('', true), |
| 38 | 62 | 'currencies' => CurrencySettings::getFormattedCurrencies(), |
| 39 | - 'default_settings' => $defaultSettings | |
| 63 | + 'default_settings' => $defaultSettings, | |
| 64 | + 'tax_settings' => [ | |
| 65 | + 'enable_tax' => Arr::get($taxSettings, 'enable_tax', 'no'), | |
| 66 | + 'tax_inclusion' => Arr::get($taxSettings, 'tax_inclusion', 'excluded'), | |
| 67 | + 'eu_method' => $euMethod, | |
| 68 | + 'vat_number' => $vatNumber, | |
| 69 | + ], | |
| 40 | 70 | ]); |
| 41 | 71 | } |
| 42 | 72 | |
| 43 | 73 | public function createPages(Request $request): \WP_REST_Response |
| @@ -84,12 +114,8 @@ | ||
| 84 | 114 | ]); |
| 85 | 115 | |
| 86 | 116 | flush_rewrite_rules(true); |
| 87 | 117 | delete_option('rewrite_rules'); |
| 88 | -// if ($content === 'customer_profile') { | |
| 89 | -// flush_rewrite_rules(true); | |
| 90 | -// delete_option('rewrite_rules'); | |
| 91 | -// } | |
| 92 | 118 | } |
| 93 | 119 | |
| 94 | 120 | return $this->response->sendSuccess([ |
| 95 | 121 | 'page_id' => $pageId, |
| @@ -99,8 +125,161 @@ | ||
| 99 | 125 | } |
| 100 | 126 | |
| 101 | 127 | return $this->response->sendError([ |
| 102 | 128 | 'message' => __('Unable to create page', 'fluent-cart') |
| 129 | + ]); | |
| 130 | + } | |
| 131 | + | |
| 132 | + public function saveTaxSettings(Request $request): \WP_REST_Response | |
| 133 | + { | |
| 134 | + $enableTax = sanitize_text_field($request->get('enable_tax', 'no')); | |
| 135 | + | |
| 136 | + if (!in_array($enableTax, ['yes', 'no'], true)) { | |
| 137 | + return $this->response->sendError([ | |
| 138 | + 'message' => __('Invalid enable_tax value', 'fluent-cart') | |
| 139 | + ], 422); | |
| 140 | + } | |
| 141 | + | |
| 142 | + if ($enableTax === 'no') { | |
| 143 | + $currentSettings = (new TaxModule())->getSettings(); | |
| 144 | + $currentSettings['enable_tax'] = 'no'; | |
| 145 | + update_option('fluent_cart_tax_configuration_settings', $currentSettings, true); | |
| 146 | + | |
| 147 | + return $this->response->sendSuccess([ | |
| 148 | + 'data' => [ | |
| 149 | + 'tax_enabled' => false, | |
| 150 | + 'classes_created' => false, | |
| 151 | + 'eu_rates_imported' => false, | |
| 152 | + ], | |
| 153 | + 'message' => __('Tax settings saved', 'fluent-cart') | |
| 154 | + ]); | |
| 155 | + } | |
| 156 | + | |
| 157 | + $taxInclusion = sanitize_text_field($request->get('tax_inclusion', 'excluded')); | |
| 158 | + $storeCountry = strtoupper(sanitize_text_field($request->get('store_country', ''))); | |
| 159 | + | |
| 160 | + if (!empty($storeCountry) && (!ctype_alpha($storeCountry) || strlen($storeCountry) > 3)) { | |
| 161 | + return $this->response->sendError([ | |
| 162 | + 'message' => __('Invalid store_country value', 'fluent-cart') | |
| 163 | + ], 422); | |
| 164 | + } | |
| 165 | + | |
| 166 | + $localization = new LocalizationManager(); | |
| 167 | + $isEuCountry = !empty($storeCountry) && $localization->isEuTaxCountry($storeCountry); | |
| 168 | + $euMethod = sanitize_text_field($request->get('eu_method', 'oss')); | |
| 169 | + $vatNumber = substr(sanitize_text_field($request->get('vat_number', '')), 0, 50); | |
| 170 | + | |
| 171 | + if (!in_array($taxInclusion, ['included', 'excluded'], true)) { | |
| 172 | + return $this->response->sendError([ | |
| 173 | + 'message' => __('Invalid tax_inclusion value', 'fluent-cart') | |
| 174 | + ], 422); | |
| 175 | + } | |
| 176 | + | |
| 177 | + if ($isEuCountry && !in_array($euMethod, ['oss', 'home', 'specific'], true)) { | |
| 178 | + return $this->response->sendError([ | |
| 179 | + 'message' => __('Invalid eu_method value', 'fluent-cart') | |
| 180 | + ], 422); | |
| 181 | + } | |
| 182 | + | |
| 183 | + $currentSettings = (new TaxModule())->getSettings(); | |
| 184 | + $currentSettings['enable_tax'] = 'yes'; | |
| 185 | + $currentSettings['tax_inclusion'] = $taxInclusion; | |
| 186 | + | |
| 187 | + $classesCreated = true; | |
| 188 | + $euRatesImported = false; | |
| 189 | + $taxManager = TaxManager::getInstance(); | |
| 190 | + | |
| 191 | + if ($isEuCountry) { | |
| 192 | + $euVatSettings = Arr::get($currentSettings, 'eu_vat_settings', []); | |
| 193 | + $euVatSettings['method'] = $euMethod; | |
| 194 | + if ($euMethod === 'oss') { | |
| 195 | + $euVatSettings['oss_vat'] = $vatNumber; | |
| 196 | + $euVatSettings['oss_country'] = $storeCountry; | |
| 197 | + } else { | |
| 198 | + // home and specific both register the home country VAT | |
| 199 | + $euVatSettings['home_vat'] = $vatNumber; | |
| 200 | + $euVatSettings['home_country'] = $storeCountry; | |
| 201 | + } | |
| 202 | + | |
| 203 | + // Upsert country_registrations so the VAT number appears in Tax → EU VAT settings | |
| 204 | + $rawRates = $taxManager->getEuTaxRatesFromPhp($storeCountry); | |
| 205 | + $ratesMap = []; | |
| 206 | + $standardRate = 0.0; | |
| 207 | + foreach ($rawRates as $rateEntry) { | |
| 208 | + $typeKey = isset($rateEntry['type']) ? $rateEntry['type'] : ''; | |
| 209 | + if (!$typeKey) { | |
| 210 | + continue; | |
| 211 | + } | |
| 212 | + $ratesMap[$typeKey] = [ | |
| 213 | + 'rate' => (float) $rateEntry['rate'], | |
| 214 | + 'label' => '', | |
| 215 | + ]; | |
| 216 | + if ($typeKey === 'standard') { | |
| 217 | + $standardRate = (float) $rateEntry['rate']; | |
| 218 | + } | |
| 219 | + } | |
| 220 | + | |
| 221 | + if (!empty($ratesMap)) { | |
| 222 | + $taxManager->saveEuVatRegistration($storeCountry, [ | |
| 223 | + 'country' => $storeCountry, | |
| 224 | + 'vat' => $vatNumber, | |
| 225 | + 'rate' => $standardRate, | |
| 226 | + 'rates' => $ratesMap, | |
| 227 | + 'tax_label' => '', | |
| 228 | + ]); | |
| 229 | + } | |
| 230 | + | |
| 231 | + $currentSettings['eu_vat_settings'] = $euVatSettings; | |
| 232 | + } | |
| 233 | + | |
| 234 | + // Direct update_option used here intentionally — TaxModule::getSettings() caches its result | |
| 235 | + // on the instance, so going through the module would return stale data on the same request. | |
| 236 | + update_option('fluent_cart_tax_configuration_settings', $currentSettings, true); | |
| 237 | + | |
| 238 | + $euCountries = Arr::get($localization->taxContinents('EU'), 'countries', []); | |
| 239 | + | |
| 240 | + try { | |
| 241 | + if ($isEuCountry) { | |
| 242 | + $taxManager->generateTaxClasses($euCountries); | |
| 243 | + $taxManager->setTaxEnabledForCountry($storeCountry, true); | |
| 244 | + $euRatesImported = true; | |
| 245 | + } else { | |
| 246 | + $taxManager->generateTaxClasses(!empty($storeCountry) ? [$storeCountry] : []); | |
| 247 | + if (!empty($storeCountry)) { | |
| 248 | + $taxManager->setTaxEnabledForCountry($storeCountry, true); | |
| 249 | + } | |
| 250 | + } | |
| 251 | + } catch (\Exception $e) { | |
| 252 | + $classesCreated = false; | |
| 253 | + } | |
| 254 | + | |
| 255 | + if (!$isEuCountry && !empty($vatNumber) && !empty($storeCountry)) { | |
| 256 | + $meta = Meta::query() | |
| 257 | + ->where('meta_key', 'fluent_cart_tax_id_' . $storeCountry) | |
| 258 | + ->where('object_type', 'tax') | |
| 259 | + ->first(); | |
| 260 | + | |
| 261 | + if ($meta) { | |
| 262 | + $meta->meta_value = ['tax_id' => $vatNumber]; | |
| 263 | + $meta->save(); | |
| 264 | + } else { | |
| 265 | + Meta::query()->create([ | |
| 266 | + //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key | |
| 267 | + 'meta_key' => 'fluent_cart_tax_id_' . $storeCountry, | |
| 268 | + //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value | |
| 269 | + 'meta_value' => ['tax_id' => $vatNumber], | |
| 270 | + 'object_type' => 'tax' | |
| 271 | + ]); | |
| 272 | + } | |
| 273 | + } | |
| 274 | + | |
| 275 | + return $this->response->sendSuccess([ | |
| 276 | + 'data' => [ | |
| 277 | + 'tax_enabled' => true, | |
| 278 | + 'classes_created' => $classesCreated, | |
| 279 | + 'eu_rates_imported' => $euRatesImported, | |
| 280 | + ], | |
| 281 | + 'message' => __('Tax settings saved', 'fluent-cart') | |
| 103 | 282 | ]); |
| 104 | 283 | } |
| 105 | 284 | |
| 106 | 285 | public function saveSettings(Request $request) |