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 / api / Resource / FrontendResource / CustomerAddressResource.php

CustomerAddressResource.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at api/Resource/FrontendResource/CustomerAddressResource.php

256 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api\Resource\FrontendResource;
4
5 use FluentCart\Api\Resource\BaseResourceApi;
6 use FluentCart\App\Models\CustomerAddresses;
7 use FluentCart\Framework\Database\Orm\Builder;
8 use FluentCart\Framework\Support\Arr;
9
10 class CustomerAddressResource extends BaseResourceApi
11 {
12
13
14 public static function getQuery(): Builder
15 {
16 return CustomerAddresses::query();
17 }
18
19 /**
20 * Get customer addresses with specified parameters.
21 *
22 * @param array $params Array containing the necessary parameters.
23 * [
24 * 'customer_id' => (int) Required. The ID of the customer.
25 * 'status' => (enum) active|.
26 * 'type' => (string) Optional. The type of address ('billing' by default).
27 * ]
28 *
29 */
30 public static function get(array $params = [])
31 {
32 $id = Arr::get($params, 'customer_id');
33
34 $type = Arr::get($params, 'type', 'billing');
35
36 $addresses = static::getQuery()
37 ->addAppends(['formatted_address'])
38 // ->when($type, function ($builder, $type) {
39 // return $builder->search(['type' => ['column' => 'type', 'value' => $type]]);
40 // })
41 ->search($params);
42 return $addresses->orderBy('is_primary', 'DESC')->get()->toArray();
43 }
44
45 /**
46 * Find customer address based on the given ID and parameters.
47 *
48 * @param int $id Required. The ID of the customer.
49 * @param array $params Optional. Array containing the necessary parameters.
50 * [
51 * 'type' => (string) Optional. The type of address ('billing' by default).
52 * ]
53 *
54 */
55 public static function find($id, $params = [])
56 {
57 $address = static::getQuery()->find($id);
58 return [
59 'address' => $address
60 ];
61 }
62
63 /**
64 * Find customer address based on the customer ID and parameters.
65 *
66 * @param int $id Required. The ID of the customer.
67 * @param array $params Optional. Array containing the necessary parameters.
68 * [
69 * 'type' => (string) Optional. The type of address ('billing' by default).
70 * ]
71 *
72 */
73 private static function findByCustomer($id, $params = [])
74 {
75 $type = Arr::get($params, 'type', 'billing');
76
77 return static::getQuery()->where('customer_id', $id)
78 ->where('type', $type)
79 ->get();
80 }
81
82 /**
83 * Create customer address with the given data.
84 *
85 * @param array $data Required. Array containing the necessary parameters
86 * [
87 * 'email' => (string) Optional. The email of the address.
88 * 'name' => (string) Required. The name of the address.
89 * 'phone' => (string) Required. The phone of the address.
90 * 'address_1' => (string) Required. The primary address line.
91 * 'address_2' => (string) Optional. The secondary address line.
92 * 'city' => (string) Required. The city of the address.
93 * 'country' => (string) Required. The country of the address.
94 * 'postcode' => (string) Required. The postal code of the address.
95 * 'state' => (string) Required. The state of the address.
96 * 'type' => (string) Required. (e.g., 'billing', 'shipping').
97 * ]
98 * @param array $params Required. Additional parameters for creating an address.
99 * [
100 * 'id' => (int) Required. The ID of the customer.
101 * ]
102 *
103 */
104 public static function create($data, $params = [])
105 {
106 $id = Arr::get($params, 'id');
107
108 $totalAddress = self::findByCustomer($id, $data)->count();
109 $hasPrimary = self::findByCustomer($id, $data)->where('is_primary', '1')->count();
110
111 if (!$hasPrimary) {
112 $data['is_primary'] = '1';
113 }
114 $data['customer_id'] = $id;
115
116 // set others data as meta
117 $otherData = Arr::only($data, ['company_name', 'vat_number', 'legal_registration_id', 'first_name', 'last_name', 'phone']);
118 Arr::set($data, 'meta.other_data', $otherData);
119
120 $isCreated = static::getQuery()->create($data);
121
122 if ($isCreated) {
123 return static::makeSuccessResponse(
124 [
125 'is_created' => $isCreated,
126 'total_address_count' => $totalAddress,
127
128 ],
129 __('Customer address created successfully!', 'fluent-cart')
130 );
131 }
132
133 return static::makeErrorResponse([
134 ['code' => 400, 'message' => __('Customer address creation failed.', 'fluent-cart')]
135 ]);
136 }
137
138 /**
139 * Update customer address with the given data.
140 *
141 * @param array $data Required. Array containing the necessary parameters
142 * [
143 * 'email' => (string) Optional. The email of the address.
144 * 'name' => (string) Required. The name of the address.
145 * 'phone' => (string) Required. The phone of the address.
146 * 'address_1' => (string) Required. The primary address line.
147 * 'address_2' => (string) Optional. The secondary address line.
148 * 'city' => (string) Required. The city of the address.
149 * 'country' => (string) Required. The country of the address.
150 * 'postcode' => (string) Required. The postal code of the address.
151 * 'state' => (string) Required. The state of the address.
152 * 'type' => (string) Required. (e.g., 'billing', 'shipping').
153 * ]
154 * @param int $id Required. The ID of the customer address to be updated.
155 *
156 */
157 public static function update($data, $id, $params = [])
158 {
159 if (!$id) {
160 return static::makeErrorResponse([
161 ['code' => 403, 'message' => __('Please edit a valid address!', 'fluent-cart')]
162 ]);
163 }
164
165 $address = static::getQuery()->find($id);
166
167 if (!$address) {
168 return static::makeErrorResponse([
169 ['code' => 404, 'message' => __('Address not found, please reload the page and try again!', 'fluent-cart')]
170 ]);
171 }
172
173 $otherData = Arr::only($data, ['company_name', 'vat_number', 'legal_registration_id', 'first_name', 'last_name', 'phone']);
174 $existingMeta = is_array($address->meta) ? $address->meta : [];
175 Arr::set($existingMeta, 'other_data', array_merge(
176 Arr::get($existingMeta, 'other_data', []),
177 $otherData
178 ));
179 $data['meta'] = $existingMeta;
180
181 $isUpdated = $address->update($data);
182
183 if ($isUpdated) {
184 static::makeSuccessResponse($isUpdated, __('Customer address updated successfully!', 'fluent-cart'));
185 }
186
187 return static::makeErrorResponse([
188 ['code' => 400, 'message' => __('Customer address update failed.', 'fluent-cart')]
189 ]);
190 }
191
192 /**
193 * Delete an address based on the given ID and parameters.
194 *
195 * @param int $id Required. The ID of the customer address.
196 * @param array $params Optional. Additional parameters for address deletion.
197 *
198 */
199 public static function delete($id, $params = [])
200 {
201 if (!$id) {
202 return static::makeErrorResponse([
203 ['code' => 403, 'message' => __('Please use a valid address ID!', 'fluent-cart')]
204 ]);
205 }
206
207 $customerAddress = static::getQuery()->find($id);
208
209 if ($customerAddress) {
210 if ($customerAddress->delete()) {
211 return static::makeSuccessResponse('', __('Address successfully deleted.', 'fluent-cart'));
212 }
213
214 return static::makeErrorResponse([
215 ['code' => 400, 'message' => __('Address deletion failed!', 'fluent-cart')]
216 ]);
217 }
218
219 return static::makeErrorResponse([
220 ['code' => 404, 'message' => __('Address not found in database, failed to remove.', 'fluent-cart')]
221 ]);
222 }
223
224 /**
225 * Make primary address for the specified customer.
226 *
227 * @param int $customerId Required. The ID of the customer.
228 * @param array $params Optional. Additional parameters for making an address primary.
229 * [
230 * 'address' => [
231 * 'id' => (int) Required. The ID of the address to be set as primary.
232 * 'type' => (string) Required. The type of the address (e.g., 'billing', 'shipping').
233 * ],
234 * ]
235 */
236 public static function makePrimary($customerId, $params = [])
237 {
238 $addressId = Arr::get($params, 'address.id');
239 $type = Arr::get($params, 'address.type');
240
241 static::getQuery()->where('customer_id', $customerId)
242 ->where('type', $type)
243 ->update(array('is_primary' => '0'));
244
245 $isUpdated = static::getQuery()->where('id', $addressId)->where('customer_id', $customerId)->where('type', $type)->update(array('is_primary' => '1'));
246
247 if ($isUpdated) {
248 return static::makeSuccessResponse('', __('Address successfully set as the primary', 'fluent-cart'));
249 }
250
251 return static::makeErrorResponse([
252 ['code' => 400, 'message' => __('Address set as primary failed.', 'fluent-cart')]
253 ]);
254 }
255 }
256