PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.1
1.6.6 1.6.5 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 All 49 releases
fluent-cart / app / Modules / Shipping / Http / Controllers / ShippingZoneController.php

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

163 lines 5.0 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 $data = $request->getSafe($request->sanitize());
53 $shippingZone = ShippingZone::query()->findOrFail($id);
54 if (Arr::get($data, 'region') !== $shippingZone->region) {
55 ShippingMethod::where('zone_id', $id)->update(['states' => []]);
56 }
57 $shippingZone->update($data);
58
59 return $this->sendSuccess([
60 'shipping_zone' => $shippingZone,
61 'message' => __('Shipping zone has been updated successfully', 'fluent-cart'),
62 ]);
63 }
64
65 public function destroy($id)
66 {
67 $shippingZone = ShippingZone::findOrFail($id);
68
69 $DB = App::db();
70 $DB->beginTransaction();
71 try {
72 // Delete associated shipping methods
73 ShippingMethod::where('zone_id', $id)->delete();
74
75 // Delete the shipping zone
76 $shippingZone->delete();
77 $DB->commit();
78 } catch (\Exception $e) {
79 $DB->rollBack();
80 return $this->sendError([
81 'message' => __('Failed to delete shipping zone', 'fluent-cart')
82 ]);
83 }
84
85 return $this->sendSuccess([
86 'message' => __('Shipping zone has been deleted successfully', 'fluent-cart')
87 ]);
88 }
89
90 public function updateOrder(Request $request)
91 {
92 $zones = $request->get('zones', []);
93
94 if (!$zones || !is_array($zones)) {
95 return $this->sendError([
96 'message' => __('Invalid data provided', 'fluent-cart')
97 ]);
98 }
99
100 $zones = array_slice($zones, 0, 200);
101
102 $DB = App::db();
103 $DB->beginTransaction();
104 try {
105 foreach ($zones as $index => $zoneId) {
106 ShippingZone::where('id', intval($zoneId))->update(['order' => $index]);
107 }
108 $DB->commit();
109 } catch (\Exception $e) {
110 $DB->rollBack();
111 return $this->sendError([
112 'message' => __('Failed to update zone order', 'fluent-cart')
113 ]);
114 }
115
116 return $this->sendSuccess([
117 'message' => __('Shipping zones order has been updated', 'fluent-cart')
118 ]);
119 }
120
121 public function getZoneStates(Request $request)
122 {
123 $country_code = sanitize_text_field(Arr::get($request->all(), 'country_code', ''));
124
125 $countryInfo = LocalizationManager::getCountryInfoFromRequest(null, $country_code);
126
127 return $this->sendSuccess([
128 'data' => $countryInfo
129 ]);
130 }
131
132 public function getCountriesByContinent()
133 {
134 $continents = LocalizationManager::getContinents();
135 $allCountries = LocalizationManager::getCountries();
136
137 $grouped = [];
138 foreach ($continents as $code => $continent) {
139 $countryCodes = Arr::get($continent, 'countries', []);
140 $countries = [];
141 foreach ($countryCodes as $countryCode) {
142 if (isset($allCountries[$countryCode])) {
143 $countries[] = [
144 'code' => $countryCode,
145 'name' => $allCountries[$countryCode],
146 ];
147 }
148 }
149 if (!empty($countries)) {
150 $grouped[] = [
151 'code' => $code,
152 'name' => Arr::get($continent, 'name', $code),
153 'countries' => $countries,
154 ];
155 }
156 }
157
158 return $this->sendSuccess([
159 'continents' => $grouped,
160 ]);
161 }
162 }
163