| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
use FluentCart\App\Models\OrderTaxRate; |
| 7 |
use FluentCart\App\Services\Filter\TaxFilter; |
| 8 |
use FluentCart\App\Services\Tax\TaxManager; |
| 9 |
use FluentCart\App\Services\DateTime\DateTime; |
| 10 |
use FluentCart\Framework\Http\Request\Request; |
| 11 |
|
| 12 |
class TaxController extends Controller |
| 13 |
{ |
| 14 |
public function index(Request $request) |
| 15 |
{ |
| 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 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function markAsFiled(Request $request) |
| 74 |
{ |
| 75 |
$idsToMark = Arr::get($request->getSafe(['ids.*' => 'intval']), 'ids', []); |
| 76 |
|
| 77 |
if (empty($idsToMark)) { |
| 78 |
return $this->sendError([ |
| 79 |
'message' => __('No IDs provided to mark!', 'fluent-cart'), |
| 80 |
], 400); |
| 81 |
} |
| 82 |
|
| 83 |
$result = OrderTaxRate::whereIn('id', $idsToMark) |
| 84 |
->whereNull('filed_at') |
| 85 |
->update([ |
| 86 |
'filed_at' => DateTime::gmtNow(), |
| 87 |
]); |
| 88 |
|
| 89 |
if (is_wp_error($result)) { |
| 90 |
return $result; |
| 91 |
} |
| 92 |
|
| 93 |
return $this->sendSuccess(['message' => __('Taxes marked as filed successfully', 'fluent-cart')]); |
| 94 |
} |
| 95 |
} |
| 96 |
|