| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use FluentCart\Framework\Support\Arr; |
| 6 | 6 | use FluentCart\App\Models\OrderTaxRate; |
| 7 | 7 | use FluentCart\App\Services\Filter\TaxFilter; |
| 8 | +use FluentCart\App\Services\Tax\TaxManager; | |
| 8 | 9 | use FluentCart\App\Services\DateTime\DateTime; |
| 9 | 10 | use FluentCart\Framework\Http\Request\Request; |
| 10 | 11 | |
| 11 | 12 | class TaxController extends Controller |
| @@ -11,11 +12,63 @@ | ||
| 11 | 12 | class TaxController extends Controller |
| 12 | 13 | { |
| 13 | 14 | public function index(Request $request) |
| 14 | 15 | { |
| 15 | - return $this->sendSuccess([ | |
| 16 | - 'taxes' => TaxFilter::fromRequest($request)->paginate(), | |
| 17 | - ]); | |
| 16 | + $taxes = TaxFilter::fromRequest($request)->paginate(); | |
| 17 | + $this->enrichOrphanedRateRows($taxes->getCollection()); | |
| 18 | + return $this->sendSuccess(['taxes' => $taxes]); | |
| 19 | + } | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * For rows where the tax_rate FK has no matching DB row (EU home/specific-mode virtual | |
| 23 | + * rates stored as tax_rate_id=0, or rates deleted after order placement), the label was | |
| 24 | + * never written to meta at order time. Attempt to recover it from the EU VAT registration | |
| 25 | + * for that country — one extra DB query per unique country, run only when needed. | |
| 26 | + */ | |
| 27 | + private function enrichOrphanedRateRows($items) | |
| 28 | + { | |
| 29 | + $needsLookup = []; | |
| 30 | + foreach ($items as $item) { | |
| 31 | + if ($item->tax_rate !== null) { | |
| 32 | + continue; | |
| 33 | + } | |
| 34 | + $meta = $item->meta; | |
| 35 | + $country = isset($meta['tax_country']) ? (string) $meta['tax_country'] : ''; | |
| 36 | + if ($country && empty($meta['label'])) { | |
| 37 | + $needsLookup[$country] = true; | |
| 38 | + } | |
| 39 | + } | |
| 40 | + | |
| 41 | + if (empty($needsLookup)) { | |
| 42 | + return; | |
| 43 | + } | |
| 44 | + | |
| 45 | + $taxManager = TaxManager::getInstance(); | |
| 46 | + $registrations = []; | |
| 47 | + foreach (array_keys($needsLookup) as $country) { | |
| 48 | + $reg = $taxManager->getEuVatRegistration($country); | |
| 49 | + if ($reg) { | |
| 50 | + $registrations[$country] = $reg; | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + if (empty($registrations)) { | |
| 55 | + return; | |
| 56 | + } | |
| 57 | + | |
| 58 | + foreach ($items as $item) { | |
| 59 | + if ($item->tax_rate !== null) { | |
| 60 | + continue; | |
| 61 | + } | |
| 62 | + $meta = $item->meta; | |
| 63 | + $country = isset($meta['tax_country']) ? (string) $meta['tax_country'] : ''; | |
| 64 | + if ($country && empty($meta['label']) && isset($registrations[$country])) { | |
| 65 | + $meta['label'] = isset($registrations[$country]['tax_label']) | |
| 66 | + ? (string) $registrations[$country]['tax_label'] | |
| 67 | + : ''; | |
| 68 | + $item->meta = $meta; | |
| 69 | + } | |
| 70 | + } | |
| 18 | 71 | } |
| 19 | 72 | |
| 20 | 73 | public function markAsFiled(Request $request) |
| 21 | 74 | { |