| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 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 |
public static function getQuery(): Builder |
| 14 |
{ |
| 15 |
return CustomerAddresses::query(); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Get customer addresses with specified parameters. |
| 20 |
* |
| 21 |
* @param array $params Array containing the necessary parameters. |
| 22 |
* [ |
| 23 |
* 'customer_id' => (int) Required. The ID of the customer. |
| 24 |
* 'type' => (string) Optional. The type of address ('billing' by default). |
| 25 |
* ] |
| 26 |
* |
| 27 |
*/ |
| 28 |
public static function get(array $params = []) |
| 29 |
{ |
| 30 |
$id = Arr::get($params, 'customer_id'); |
| 31 |
|
| 32 |
$type = Arr::get($params, 'type', 'billing'); |
| 33 |
|
| 34 |
$addresses = static::getQuery()->search(['customer_id' => ['column' => 'customer_id', 'value' => $id]]); |
| 35 |
|
| 36 |
if ($type) { |
| 37 |
$addresses->search(['type' => ['column' => 'type', 'value' => $type]]); |
| 38 |
} |
| 39 |
return $addresses->orderBy('is_primary', 'DESC')->get()->toArray(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Find customer address based on the given ID and parameters. |
| 44 |
* |
| 45 |
* @param int $id Required. The ID of the customer. |
| 46 |
* @param array $params Optional. Array containing the necessary parameters. |
| 47 |
* [ |
| 48 |
* 'type' => (string) Optional. The type of address ('billing' by default). |
| 49 |
* ] |
| 50 |
* |
| 51 |
*/ |
| 52 |
public static function find($id, $params = []) |
| 53 |
{ |
| 54 |
$type = Arr::get($params, 'type', 'billing'); |
| 55 |
$params = [ |
| 56 |
"customer_id" => $id, |
| 57 |
"type" => $type, |
| 58 |
]; |
| 59 |
$all = CustomerAddressResource::get($params); |
| 60 |
|
| 61 |
if (empty($all)) { |
| 62 |
return []; |
| 63 |
} |
| 64 |
|
| 65 |
$firstItem = Arr::first($all); |
| 66 |
return Arr::only($firstItem, [ |
| 67 |
'address_1', |
| 68 |
'address_2', |
| 69 |
'city', |
| 70 |
'state', |
| 71 |
'postcode', |
| 72 |
'country' |
| 73 |
]); |
| 74 |
} |
| 75 |
|
| 76 |
/*public static function find($id, $params = []) |
| 77 |
{ |
| 78 |
return static::getQuery()->find($id); |
| 79 |
}*/ |
| 80 |
|
| 81 |
/** |
| 82 |
* Create customer address with the given data. |
| 83 |
* |
| 84 |
* @param array $data Required. Array containing the necessary parameters |
| 85 |
* [ |
| 86 |
* 'email' => (string) Optional. The email of the address. |
| 87 |
* 'name' => (string) Required. The name of the address. |
| 88 |
* 'phone' => (string) Required. The phone of the address. |
| 89 |
* 'address_1' => (string) Required. The primary address line. |
| 90 |
* 'address_2' => (string) Optional. The secondary address line. |
| 91 |
* 'city' => (string) Required. The city of the address. |
| 92 |
* 'country' => (string) Required. The country of the address. |
| 93 |
* 'postcode' => (string) Required. The postal code of the address. |
| 94 |
* 'state' => (string) Required. The state of the address. |
| 95 |
* 'type' => (string) Required. (e.g., 'billing', 'shipping'). |
| 96 |
* ] |
| 97 |
* @param array $params Required. Additional parameters for creating an address. |
| 98 |
* [ |
| 99 |
* 'id' => (int) Required. The ID of the customer. |
| 100 |
* ] |
| 101 |
* |
| 102 |
*/ |
| 103 |
public static function create($data, $params = []) |
| 104 |
{ |
| 105 |
// Extract the customer ID from the provided parameters |
| 106 |
$id = Arr::get($params, 'id'); |
| 107 |
|
| 108 |
// Retrieve the 'type' of address from the provided data array |
| 109 |
$addressType = Arr::get($data, 'type'); |
| 110 |
|
| 111 |
// Check if the customer already has a primary address of the given type |
| 112 |
$hasPrimary = static::getQuery() |
| 113 |
->where('customer_id', $id) // Match the customer ID |
| 114 |
->where('type', Arr::get($data, 'type')) // Match the address type from the data |
| 115 |
->where('is_primary', '1') // Look for addresses marked as primary |
| 116 |
->count(); // Get the count of matching primary addresses |
| 117 |
|
| 118 |
// If no primary address exists, set 'is_primary' to '1' |
| 119 |
if (!$hasPrimary) { |
| 120 |
$data['is_primary'] = '1'; |
| 121 |
} |
| 122 |
|
| 123 |
// Assign the customer ID to the data array |
| 124 |
$data['customer_id'] = $id; |
| 125 |
|
| 126 |
// Create a new address record with the given data |
| 127 |
$isCreated = static::getQuery()->create($data); |
| 128 |
|
| 129 |
// If the address was successfully created |
| 130 |
if ($isCreated) { |
| 131 |
// Check if an 'order_id' is provided and the address is marked as primary |
| 132 |
if (!empty($orderId = Arr::get($params, 'order_id', null)) && Arr::get($data, 'is_primary', null) == 1) { |
| 133 |
// Include 'order_id' in the data for the order address |
| 134 |
$data['order_id'] = $orderId; |
| 135 |
// Check if the order already has an address of the given type |
| 136 |
$alreadyOrderHasAddress = OrderAddressResource::find($orderId, ['type' => Arr::get($data, 'type', null)]); |
| 137 |
|
| 138 |
if ($alreadyOrderHasAddress) { |
| 139 |
// If an address already exists for the order, update it with the new data |
| 140 |
OrderAddressResource::update($data, $orderId); |
| 141 |
} else { |
| 142 |
// If no address exists for the order, create a new one |
| 143 |
OrderAddressResource::create($data); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if ($addressType === 'billing') { |
| 148 |
return static::makeSuccessResponse( |
| 149 |
$isCreated, |
| 150 |
__('Billing address created successfully!', 'fluent-cart') |
| 151 |
); |
| 152 |
} |
| 153 |
return static::makeSuccessResponse( |
| 154 |
$isCreated, |
| 155 |
__('Shipping address created successfully!', 'fluent-cart') |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
if ($addressType === 'billing') { |
| 160 |
return static::makeErrorResponse([ |
| 161 |
['code' => 400, 'message' => __('Failed creating billing address', 'fluent-cart')] |
| 162 |
]); |
| 163 |
} |
| 164 |
|
| 165 |
return static::makeErrorResponse([ |
| 166 |
['code' => 400, 'message' => __('Failed creating shipping address', 'fluent-cart')] |
| 167 |
]); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Update the customer address with the given data. |
| 172 |
* |
| 173 |
* @param array $data Required. Array containing the necessary parameters |
| 174 |
* [ |
| 175 |
* 'Email' => (string) Optional. The email of the address. |
| 176 |
* 'Name' => (string) Required. The name of the address. |
| 177 |
* 'Phone' => (string) Required. The phone of the address. |
| 178 |
* 'Address_1' => (string) Required. The primary address line. |
| 179 |
* 'Address_2' => (string) Optional. The secondary address line. |
| 180 |
* 'City' => (string) Required. The city of the address. |
| 181 |
* 'Country' => (string) Required. The country of the address. |
| 182 |
* 'Postcode' => (string) Required. The postal code of the address. |
| 183 |
* 'State' => (string) Required. The state of the address. |
| 184 |
* 'Type' => (string) Required. (E.g., 'billing', 'shipping'). |
| 185 |
* ] |
| 186 |
* @param int $id Required. The ID of the customer address to be updated. |
| 187 |
* |
| 188 |
*/ |
| 189 |
public static function update($data, $id, $params = []) |
| 190 |
{ |
| 191 |
if (!$id) { |
| 192 |
return static::makeErrorResponse([ |
| 193 |
['code' => 403, 'message' => __('Please edit a valid address!', 'fluent-cart')] |
| 194 |
]); |
| 195 |
} |
| 196 |
|
| 197 |
$address = static::getQuery()->find($id); |
| 198 |
|
| 199 |
// Retrieve the 'type' of address from the provided data array |
| 200 |
$addressType = Arr::get($address, 'type'); |
| 201 |
|
| 202 |
|
| 203 |
if (!$address) { |
| 204 |
return static::makeErrorResponse([ |
| 205 |
['code' => 404, 'message' => __('Address not found, please reload the page and try again!', 'fluent-cart')] |
| 206 |
]); |
| 207 |
} |
| 208 |
|
| 209 |
$address->update($data); |
| 210 |
$address->refresh(); |
| 211 |
|
| 212 |
if ($address) { |
| 213 |
if (!empty($orderId = Arr::get($params, 'order_id', null))) { |
| 214 |
OrderAddressResource::update($data, $orderId); |
| 215 |
} |
| 216 |
|
| 217 |
if ($addressType === 'billing') { |
| 218 |
return static::makeSuccessResponse( |
| 219 |
$address, |
| 220 |
__('Billing address updated successfully!', 'fluent-cart') |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
return static::makeSuccessResponse( |
| 225 |
$address, |
| 226 |
__('Shipping address updated successfully!', 'fluent-cart') |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
if ($addressType === 'billing') { |
| 231 |
return static::makeErrorResponse([ |
| 232 |
['code' => 400, 'message' => __('Failed updating billing address', 'fluent-cart')] |
| 233 |
]); |
| 234 |
} |
| 235 |
|
| 236 |
return static::makeErrorResponse([ |
| 237 |
['code' => 400, 'message' => __('Failed updating shipping address', 'fluent-cart')] |
| 238 |
]); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Delete an address based on the given ID and parameters. |
| 243 |
* |
| 244 |
* @param int $id Required. The ID of the customer address. |
| 245 |
* @param array $params Optional. Additional parameters for address deletion. |
| 246 |
* |
| 247 |
*/ |
| 248 |
public static function delete($id, $params = []) |
| 249 |
{ |
| 250 |
// Check if a valid ID is provided |
| 251 |
if (!$id) { |
| 252 |
return static::makeErrorResponse([ |
| 253 |
['code' => 403, 'message' => __('Please use a valid address ID!', 'fluent-cart')] |
| 254 |
]); |
| 255 |
} |
| 256 |
|
| 257 |
// Retrieve the customer address using the ID |
| 258 |
$customerAddress = static::getQuery()->find($id); |
| 259 |
|
| 260 |
if ($customerAddress) { |
| 261 |
// Check if the address is marked as primary |
| 262 |
if ($customerAddress->is_primary) { |
| 263 |
return static::makeErrorResponse([ |
| 264 |
['code' => 403, 'message' => __('Primary address cannot be deleted!', 'fluent-cart')] |
| 265 |
]); |
| 266 |
} |
| 267 |
// Get the count of addresses for this customer |
| 268 |
$addressCount = static::getQuery()->where('customer_id', $customerAddress->customer_id)->count(); |
| 269 |
|
| 270 |
// Check if there's only one address, do not allow deletion in that case |
| 271 |
if ($addressCount <= 1) { |
| 272 |
return static::makeErrorResponse([ |
| 273 |
['code' => 403, 'message' => __('At least one address must remain. Address deletion failed!', 'fluent-cart')] |
| 274 |
]); |
| 275 |
} |
| 276 |
|
| 277 |
// If the address is not primary and there are multiple addresses, proceed with deletion |
| 278 |
if ($customerAddress->delete()) { |
| 279 |
return static::makeSuccessResponse('', __('Address successfully deleted.', 'fluent-cart')); |
| 280 |
} |
| 281 |
|
| 282 |
// Return an error if the address is not found in the database |
| 283 |
return static::makeErrorResponse([ |
| 284 |
['code' => 400, 'message' => __('Address deletion failed!', 'fluent-cart')] |
| 285 |
]); |
| 286 |
} |
| 287 |
|
| 288 |
return static::makeErrorResponse([ |
| 289 |
['code' => 404, 'message' => __('Address not found in database, failed to remove.', 'fluent-cart')] |
| 290 |
]); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Make primary address for the specified customer. |
| 295 |
* |
| 296 |
* @param int $customerId Required. The ID of the customer. |
| 297 |
* @param array $params Optional. Additional parameters for making an address primary. |
| 298 |
* [ |
| 299 |
* 'address' => [ |
| 300 |
* 'id' => (int) Required. The ID of the address to be set as primary. |
| 301 |
* 'type' => (string) Required. The type of the address (e.g., 'billing', 'shipping'). |
| 302 |
* ], |
| 303 |
* ] |
| 304 |
*/ |
| 305 |
public static function makePrimary($customerId, $addressId, $type) |
| 306 |
{ |
| 307 |
static::getQuery()->where('customer_id', $customerId) |
| 308 |
->where('type', $type) |
| 309 |
->update(array('is_primary' => '0')); |
| 310 |
|
| 311 |
$isUpdated = static::getQuery()->where('id', $addressId)->where('customer_id', $customerId)->where('type', $type)->update(array('is_primary' => '1')); |
| 312 |
|
| 313 |
if ($isUpdated) { |
| 314 |
return static::makeSuccessResponse('', __('Address successfully set as the primary', 'fluent-cart')); |
| 315 |
} |
| 316 |
|
| 317 |
return static::makeErrorResponse([ |
| 318 |
['code' => 400, 'message' => __('Address set as primary failed.', 'fluent-cart')] |
| 319 |
]); |
| 320 |
} |
| 321 |
} |