| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Models\CustomerAddresses; |
| 7 |
use FluentCart\App\Services\Renderer\CheckoutFieldsSchema; |
| 8 |
use FluentCart\Framework\Database\Orm\Builder; |
| 9 |
use FluentCart\Framework\Support\Arr; |
| 10 |
|
| 11 |
class CustomerAddressResource extends BaseResourceApi |
| 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 |
* 'type' => (string) Optional. The type of address ('billing' by default). |
| 26 |
* ] |
| 27 |
* |
| 28 |
*/ |
| 29 |
public static function get(array $params = []) |
| 30 |
{ |
| 31 |
$id = Arr::get($params, 'customer_id'); |
| 32 |
|
| 33 |
$type = Arr::get($params, 'type', 'billing'); |
| 34 |
|
| 35 |
$addresses = static::getQuery()->search(['customer_id' => ['column' => 'customer_id', 'value' => $id]]); |
| 36 |
|
| 37 |
if ($type) { |
| 38 |
$addresses->search(['type' => ['column' => 'type', 'value' => $type]]); |
| 39 |
} |
| 40 |
return $addresses->orderBy('is_primary', 'DESC')->get()->toArray(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Find customer address based on the given ID and parameters. |
| 45 |
* |
| 46 |
* @param int $id Required. The ID of the customer. |
| 47 |
* @param array $params Optional. Array containing the necessary parameters. |
| 48 |
* [ |
| 49 |
* 'type' => (string) Optional. The type of address ('billing' by default). |
| 50 |
* ] |
| 51 |
* |
| 52 |
*/ |
| 53 |
public static function find($id, $params = []) |
| 54 |
{ |
| 55 |
$type = Arr::get($params, 'type', 'billing'); |
| 56 |
$params = [ |
| 57 |
"customer_id" => $id, |
| 58 |
"type" => $type, |
| 59 |
]; |
| 60 |
$all = CustomerAddressResource::get($params); |
| 61 |
|
| 62 |
if (empty($all)) { |
| 63 |
return []; |
| 64 |
} |
| 65 |
|
| 66 |
$firstItem = Arr::first($all); |
| 67 |
return Arr::only($firstItem, [ |
| 68 |
'address_1', |
| 69 |
'address_2', |
| 70 |
'city', |
| 71 |
'state', |
| 72 |
'postcode', |
| 73 |
'country' |
| 74 |
]); |
| 75 |
} |
| 76 |
|
| 77 |
/*public static function find($id, $params = []) |
| 78 |
{ |
| 79 |
return static::getQuery()->find($id); |
| 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 |
// Extract the customer ID from the provided parameters |
| 107 |
$id = Arr::get($params, 'id'); |
| 108 |
|
| 109 |
// Retrieve the 'type' of address from the provided data array |
| 110 |
$addressType = Arr::get($data, 'type'); |
| 111 |
|
| 112 |
// Check if the customer already has a primary address of the given type |
| 113 |
$hasPrimary = static::getQuery() |
| 114 |
->where('customer_id', $id) // Match the customer ID |
| 115 |
->where('type', Arr::get($data, 'type')) // Match the address type from the data |
| 116 |
->where('is_primary', '1') // Look for addresses marked as primary |
| 117 |
->count(); // Get the count of matching primary addresses |
| 118 |
|
| 119 |
// If no primary address exists, set 'is_primary' to '1' |
| 120 |
if (!$hasPrimary) { |
| 121 |
$data['is_primary'] = '1'; |
| 122 |
} |
| 123 |
|
| 124 |
// Assign the customer ID to the data array |
| 125 |
$data['customer_id'] = $id; |
| 126 |
|
| 127 |
// Request::getSafe() null-fills sanitize-map keys the client omitted; |
| 128 |
// label is nullable at validation but NOT NULL DEFAULT '' at the column. |
| 129 |
if (!isset($data['label'])) { |
| 130 |
$data['label'] = ''; |
| 131 |
} |
| 132 |
|
| 133 |
$data = static::normalizeBusinessFields($data); |
| 134 |
$data = static::mergeAddressMetaFields($data); |
| 135 |
|
| 136 |
// Create a new address record with the given data |
| 137 |
$isCreated = static::getQuery()->create($data); |
| 138 |
|
| 139 |
// If the address was successfully created |
| 140 |
if ($isCreated) { |
| 141 |
// Check if an 'order_id' is provided and the address is marked as primary |
| 142 |
if (!empty($orderId = Arr::get($params, 'order_id', null)) && Arr::get($data, 'is_primary', null) == 1) { |
| 143 |
// Include 'order_id' in the data for the order address |
| 144 |
$data['order_id'] = $orderId; |
| 145 |
// Check if the order already has an address of the given type |
| 146 |
$alreadyOrderHasAddress = OrderAddressResource::find($orderId, ['type' => Arr::get($data, 'type', null)]); |
| 147 |
|
| 148 |
if ($alreadyOrderHasAddress) { |
| 149 |
// If an address already exists for the order, update it with the new data |
| 150 |
OrderAddressResource::update($data, $orderId); |
| 151 |
} else { |
| 152 |
// If no address exists for the order, create a new one |
| 153 |
OrderAddressResource::create($data); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if ($addressType === 'billing') { |
| 158 |
return static::makeSuccessResponse( |
| 159 |
$isCreated, |
| 160 |
__('Billing address created successfully!', 'fluent-cart') |
| 161 |
); |
| 162 |
} |
| 163 |
return static::makeSuccessResponse( |
| 164 |
$isCreated, |
| 165 |
__('Shipping address created successfully!', 'fluent-cart') |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
if ($addressType === 'billing') { |
| 170 |
return static::makeErrorResponse([ |
| 171 |
['code' => 400, 'message' => __('Failed creating billing address', 'fluent-cart')] |
| 172 |
]); |
| 173 |
} |
| 174 |
|
| 175 |
return static::makeErrorResponse([ |
| 176 |
['code' => 400, 'message' => __('Failed creating shipping address', 'fluent-cart')] |
| 177 |
]); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Update the customer address with the given data. |
| 182 |
* |
| 183 |
* @param array $data Required. Array containing the necessary parameters |
| 184 |
* [ |
| 185 |
* 'Email' => (string) Optional. The email of the address. |
| 186 |
* 'Name' => (string) Required. The name of the address. |
| 187 |
* 'Phone' => (string) Required. The phone of the address. |
| 188 |
* 'Address_1' => (string) Required. The primary address line. |
| 189 |
* 'Address_2' => (string) Optional. The secondary address line. |
| 190 |
* 'City' => (string) Required. The city of the address. |
| 191 |
* 'Country' => (string) Required. The country of the address. |
| 192 |
* 'Postcode' => (string) Required. The postal code of the address. |
| 193 |
* 'State' => (string) Required. The state of the address. |
| 194 |
* 'Type' => (string) Required. (E.g., 'billing', 'shipping'). |
| 195 |
* ] |
| 196 |
* @param int $id Required. The ID of the customer address to be updated. |
| 197 |
* |
| 198 |
*/ |
| 199 |
public static function update($data, $id, $params = []) |
| 200 |
{ |
| 201 |
if (!$id) { |
| 202 |
return static::makeErrorResponse([ |
| 203 |
['code' => 403, 'message' => __('Please edit a valid address!', 'fluent-cart')] |
| 204 |
]); |
| 205 |
} |
| 206 |
|
| 207 |
$address = static::getQuery()->find($id); |
| 208 |
|
| 209 |
// Retrieve the 'type' of address from the provided data array |
| 210 |
$addressType = Arr::get($address, 'type'); |
| 211 |
|
| 212 |
|
| 213 |
if (!$address) { |
| 214 |
return static::makeErrorResponse([ |
| 215 |
['code' => 404, 'message' => __('Address not found, please reload the page and try again!', 'fluent-cart')] |
| 216 |
]); |
| 217 |
} |
| 218 |
|
| 219 |
$data = static::normalizeBusinessFields($data); |
| 220 |
$data = static::mergeAddressMetaFields($data, $address); |
| 221 |
|
| 222 |
$address->update($data); |
| 223 |
$address->refresh(); |
| 224 |
|
| 225 |
if ($address) { |
| 226 |
if (!empty($orderId = Arr::get($params, 'order_id', null))) { |
| 227 |
OrderAddressResource::update($data, $orderId); |
| 228 |
} |
| 229 |
|
| 230 |
if ($addressType === 'billing') { |
| 231 |
return static::makeSuccessResponse( |
| 232 |
$address, |
| 233 |
__('Billing address updated successfully!', 'fluent-cart') |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
return static::makeSuccessResponse( |
| 238 |
$address, |
| 239 |
__('Shipping address updated successfully!', 'fluent-cart') |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
if ($addressType === 'billing') { |
| 244 |
return static::makeErrorResponse([ |
| 245 |
['code' => 400, 'message' => __('Failed updating billing address', 'fluent-cart')] |
| 246 |
]); |
| 247 |
} |
| 248 |
|
| 249 |
return static::makeErrorResponse([ |
| 250 |
['code' => 400, 'message' => __('Failed updating shipping address', 'fluent-cart')] |
| 251 |
]); |
| 252 |
} |
| 253 |
|
| 254 |
public static function normalizeBusinessFields(array $data): array |
| 255 |
{ |
| 256 |
$type = Arr::get($data, 'type', 'billing'); |
| 257 |
|
| 258 |
if ($type !== 'billing') { |
| 259 |
unset($data['company_name'], $data['vat_number'], $data['legal_registration_id']); |
| 260 |
return $data; |
| 261 |
} |
| 262 |
|
| 263 |
if (!CheckoutFieldsSchema::isCompanyNameEnabled()) { |
| 264 |
unset($data['company_name']); |
| 265 |
} |
| 266 |
|
| 267 |
if (!CheckoutFieldsSchema::isVatNumberEnabled()) { |
| 268 |
unset($data['vat_number']); |
| 269 |
} |
| 270 |
|
| 271 |
if (!CheckoutFieldsSchema::isLegalRegistrationIdEnabled()) { |
| 272 |
unset($data['legal_registration_id']); |
| 273 |
} |
| 274 |
|
| 275 |
return $data; |
| 276 |
} |
| 277 |
|
| 278 |
private static function mergeAddressMetaFields(array $data, ?CustomerAddresses $address = null): array |
| 279 |
{ |
| 280 |
$meta = $address ? $address->meta : Arr::get($data, 'meta', []); |
| 281 |
$meta = is_array($meta) ? $meta : []; |
| 282 |
|
| 283 |
$metaKeys = ['company_name', 'vat_number', 'legal_registration_id']; |
| 284 |
foreach ($metaKeys as $metaKey) { |
| 285 |
if (!array_key_exists($metaKey, $data)) { |
| 286 |
continue; |
| 287 |
} |
| 288 |
|
| 289 |
$value = Arr::get($data, $metaKey); |
| 290 |
|
| 291 |
if ($value === '' || $value === null) { |
| 292 |
unset($meta['other_data'][$metaKey]); |
| 293 |
} else { |
| 294 |
Arr::set($meta, 'other_data.' . $metaKey, $value); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
$data['meta'] = $meta; |
| 299 |
unset($data['company_name'], $data['vat_number'], $data['legal_registration_id']); |
| 300 |
|
| 301 |
return $data; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Delete an address based on the given ID and parameters. |
| 306 |
* |
| 307 |
* @param int $id Required. The ID of the customer address. |
| 308 |
* @param array $params Optional. Additional parameters for address deletion. |
| 309 |
* |
| 310 |
*/ |
| 311 |
public static function delete($id, $params = []) |
| 312 |
{ |
| 313 |
// Check if a valid ID is provided |
| 314 |
if (!$id) { |
| 315 |
return static::makeErrorResponse([ |
| 316 |
['code' => 403, 'message' => __('Please use a valid address ID!', 'fluent-cart')] |
| 317 |
], 403); |
| 318 |
} |
| 319 |
|
| 320 |
// Retrieve the customer address using the ID |
| 321 |
$customerAddress = static::getQuery()->find($id); |
| 322 |
|
| 323 |
if ($customerAddress) { |
| 324 |
// Check if the address is marked as primary |
| 325 |
if ($customerAddress->is_primary) { |
| 326 |
return static::makeErrorResponse([ |
| 327 |
['code' => 403, 'message' => __('Primary address cannot be deleted!', 'fluent-cart')] |
| 328 |
], 403); |
| 329 |
} |
| 330 |
// Get the count of addresses for this customer |
| 331 |
$addressCount = static::getQuery()->where('customer_id', $customerAddress->customer_id)->count(); |
| 332 |
|
| 333 |
// Check if there's only one address, do not allow deletion in that case |
| 334 |
if ($addressCount <= 1) { |
| 335 |
return static::makeErrorResponse([ |
| 336 |
['code' => 403, 'message' => __('At least one address must remain. Address deletion failed!', 'fluent-cart')] |
| 337 |
], 403); |
| 338 |
} |
| 339 |
|
| 340 |
// If the address is not primary and there are multiple addresses, proceed with deletion |
| 341 |
if ($customerAddress->delete()) { |
| 342 |
return static::makeSuccessResponse('', __('Address successfully deleted.', 'fluent-cart')); |
| 343 |
} |
| 344 |
|
| 345 |
// Return an error if the address is not found in the database |
| 346 |
return static::makeErrorResponse([ |
| 347 |
['code' => 400, 'message' => __('Address deletion failed!', 'fluent-cart')] |
| 348 |
], 400); |
| 349 |
} |
| 350 |
|
| 351 |
return static::makeErrorResponse([ |
| 352 |
['code' => 404, 'message' => __('Address not found in database, failed to remove.', 'fluent-cart')] |
| 353 |
], 404); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Make primary address for the specified customer. |
| 358 |
* |
| 359 |
* @param int $customerId Required. The ID of the customer. |
| 360 |
* @param array $params Optional. Additional parameters for making an address primary. |
| 361 |
* [ |
| 362 |
* 'address' => [ |
| 363 |
* 'id' => (int) Required. The ID of the address to be set as primary. |
| 364 |
* 'type' => (string) Required. The type of the address (e.g., 'billing', 'shipping'). |
| 365 |
* ], |
| 366 |
* ] |
| 367 |
*/ |
| 368 |
public static function makePrimary($customerId, $addressId, $type) |
| 369 |
{ |
| 370 |
static::getQuery()->where('customer_id', $customerId) |
| 371 |
->where('type', $type) |
| 372 |
->update(array('is_primary' => '0')); |
| 373 |
|
| 374 |
$isUpdated = static::getQuery()->where('id', $addressId)->where('customer_id', $customerId)->where('type', $type)->update(array('is_primary' => '1')); |
| 375 |
|
| 376 |
if ($isUpdated) { |
| 377 |
return static::makeSuccessResponse('', __('Address successfully set as the primary', 'fluent-cart')); |
| 378 |
} |
| 379 |
|
| 380 |
return static::makeErrorResponse([ |
| 381 |
['code' => 400, 'message' => __('Address set as primary failed.', 'fluent-cart')] |
| 382 |
]); |
| 383 |
} |
| 384 |
} |
| 385 |
|