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 / TaxController.php

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

96 lines 3.1 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\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