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 / Modules / Shipping / Http / Controllers / ShippingZoneController.php

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

169 lines 5.4 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\Modules\Shipping\Http\Controllers;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Http\Controllers\Controller;
7 use FluentCart\App\Models\ShippingZone;
8 use FluentCart\App\Models\ShippingMethod;
9 use FluentCart\App\Modules\Shipping\Services\Filter\ShippingZoneFilter;
10 use FluentCart\App\Services\Localization\LocalizationManager;
11 use FluentCart\Framework\Http\Request\Request;
12 use FluentCart\Framework\Support\Arr;
13 use FluentCart\App\Modules\Shipping\Http\Requests\ShippingMethodRequest;
14 use FluentCart\App\Modules\Shipping\Http\Requests\ShippingZoneRequest;
15
16 class ShippingZoneController extends Controller
17 {
18 public function index(Request $request)
19 {
20 return $this->sendSuccess([
21 'shipping_zones' => ShippingZoneFilter::fromRequest($request)->paginate()
22 ]);
23 }
24
25 public function store(ShippingZoneRequest $request)
26 {
27 $data = $request->getSafe($request->sanitize());
28
29 if (empty($data['shipping_class_id'])) {
30 $data['shipping_class_id'] = null;
31 }
32
33 $shippingZone = ShippingZone::query()->create($data);
34
35 return $this->sendSuccess([
36 'shipping_zone' => $shippingZone,
37 'message' => __('Shipping zone has been created successfully', 'fluent-cart')
38 ]);
39 }
40
41 public function show($id)
42 {
43 $shippingZone = ShippingZone::with('methods')->findOrFail($id);
44
45 return $this->sendSuccess([
46 'shipping_zone' => $shippingZone
47 ]);
48 }
49
50 public function update(ShippingZoneRequest $request, $id)
51 {
52 // When the client omits shipping_class_id, ShippingZoneRequest::
53 // beforeValidation resolves the zone's stored class into the request
54 // (its return is merged in), so validation checks the effective class
55 // and this write preserves it — getSafe would otherwise emit the
56 // mapped key as null and wipe the stored link.
57 $data = $request->getSafe($request->sanitize());
58
59 $shippingZone = ShippingZone::query()->findOrFail($id);
60 if (Arr::get($data, 'region') !== $shippingZone->region) {
61 ShippingMethod::where('zone_id', $id)->update(['states' => []]);
62 }
63 $shippingZone->update($data);
64
65 return $this->sendSuccess([
66 'shipping_zone' => $shippingZone,
67 'message' => __('Shipping zone has been updated successfully', 'fluent-cart'),
68 ]);
69 }
70
71 public function destroy($id)
72 {
73 $shippingZone = ShippingZone::findOrFail($id);
74
75 $DB = App::db();
76 $DB->beginTransaction();
77 try {
78 // Delete associated shipping methods
79 ShippingMethod::where('zone_id', $id)->delete();
80
81 // Delete the shipping zone
82 $shippingZone->delete();
83 $DB->commit();
84 } catch (\Exception $e) {
85 $DB->rollBack();
86 return $this->sendError([
87 'message' => __('Failed to delete shipping zone', 'fluent-cart')
88 ]);
89 }
90
91 return $this->sendSuccess([
92 'message' => __('Shipping zone has been deleted successfully', 'fluent-cart')
93 ]);
94 }
95
96 public function updateOrder(Request $request)
97 {
98 $zones = $request->get('zones', []);
99
100 if (!$zones || !is_array($zones)) {
101 return $this->sendError([
102 'message' => __('Invalid data provided', 'fluent-cart')
103 ]);
104 }
105
106 $zones = array_slice($zones, 0, 200);
107
108 $DB = App::db();
109 $DB->beginTransaction();
110 try {
111 foreach ($zones as $index => $zoneId) {
112 ShippingZone::where('id', intval($zoneId))->update(['order' => $index]);
113 }
114 $DB->commit();
115 } catch (\Exception $e) {
116 $DB->rollBack();
117 return $this->sendError([
118 'message' => __('Failed to update zone order', 'fluent-cart')
119 ]);
120 }
121
122 return $this->sendSuccess([
123 'message' => __('Shipping zones order has been updated', 'fluent-cart')
124 ]);
125 }
126
127 public function getZoneStates(Request $request)
128 {
129 $country_code = sanitize_text_field(Arr::get($request->all(), 'country_code', ''));
130
131 $countryInfo = LocalizationManager::getCountryInfoFromRequest(null, $country_code);
132
133 return $this->sendSuccess([
134 'data' => $countryInfo
135 ]);
136 }
137
138 public function getCountriesByContinent()
139 {
140 $continents = LocalizationManager::getContinents();
141 $allCountries = LocalizationManager::getCountries();
142
143 $grouped = [];
144 foreach ($continents as $code => $continent) {
145 $countryCodes = Arr::get($continent, 'countries', []);
146 $countries = [];
147 foreach ($countryCodes as $countryCode) {
148 if (isset($allCountries[$countryCode])) {
149 $countries[] = [
150 'code' => $countryCode,
151 'name' => $allCountries[$countryCode],
152 ];
153 }
154 }
155 if (!empty($countries)) {
156 $grouped[] = [
157 'code' => $code,
158 'name' => Arr::get($continent, 'name', $code),
159 'countries' => $countries,
160 ];
161 }
162 }
163
164 return $this->sendSuccess([
165 'continents' => $grouped,
166 ]);
167 }
168 }
169