diff --git a/app/Http/Controllers/Shop/CustomerController.php b/app/Http/Controllers/Shop/CustomerController.php
index 91b6754f..e5fbf25f 100644
--- a/app/Http/Controllers/Shop/CustomerController.php
+++ b/app/Http/Controllers/Shop/CustomerController.php
@@ -101,6 +101,7 @@ class CustomerController extends Controller
}
$address = CustomerAddresses::store($data);
+ CustomerAddresses::setDefault($customerId, $address->id, $types[$prefix]);
$html = view('Shop.Customers.partials.address_item', [
'address' => $address->toArray(),
@@ -136,6 +137,7 @@ class CustomerController extends Controller
}
CustomerAddresses::destroy($id);
+ CustomerAddresses::ensureDefault($address->customer_id, $address->type);
return redirect()->route('Shop.Customers.edit')
->with('growl', [__('Adresse supprimée.'), 'success']);
diff --git a/app/Repositories/Shop/CustomerAddresses.php b/app/Repositories/Shop/CustomerAddresses.php
index 3867ad2c..0de5ffa2 100644
--- a/app/Repositories/Shop/CustomerAddresses.php
+++ b/app/Repositories/Shop/CustomerAddresses.php
@@ -12,17 +12,35 @@ class CustomerAddresses
public static function storeByCustomer($customer, $data)
{
$deliveries = $data['deliveries'] ?? false;
- if ($deliveries && $deliveries['zipcode'] && $deliveries['city']) {
- $deliveries['customer_id'] = $customer->id;
- $deliveries['type'] = 1;
- self::store($deliveries);
+ if ($deliveries) {
+ if (! empty($deliveries['address_id'])) {
+ self::setDefault($customer->id, (int) $deliveries['address_id'], 1);
+ }
+
+ if (! empty($deliveries['zipcode']) && ! empty($deliveries['city'])) {
+ $payload = $deliveries;
+ unset($payload['address_id']);
+ $payload['customer_id'] = $customer->id;
+ $payload['type'] = 1;
+ $newAddress = self::store($payload);
+ self::setDefault($customer->id, $newAddress->id, 1);
+ }
}
$invoices = $data['invoices'] ?? false;
- if ($invoices && $invoices['zipcode'] && $invoices['city']) {
- $invoices['customer_id'] = $customer->id;
- $invoices['type'] = 2;
- self::store($invoices);
+ if ($invoices) {
+ if (! empty($invoices['address_id'])) {
+ self::setDefault($customer->id, (int) $invoices['address_id'], 2);
+ }
+
+ if (! empty($invoices['zipcode']) && ! empty($invoices['city'])) {
+ $payload = $invoices;
+ unset($payload['address_id']);
+ $payload['customer_id'] = $customer->id;
+ $payload['type'] = 2;
+ $newAddress = self::store($payload);
+ self::setDefault($customer->id, $newAddress->id, 2);
+ }
}
}
@@ -70,14 +88,24 @@ class CustomerAddresses
public static function getInvoiceAddress($customerId)
{
- $addresses = CustomerAddress::byCustomer($customerId)->byInvoicing()->get();
- return count($addresses) ? $addresses->first() : self::getByCustomer($customerId);
+ $address = CustomerAddress::byCustomer($customerId)
+ ->byInvoicing()
+ ->orderByDesc('priority')
+ ->orderBy('id')
+ ->first();
+
+ return $address ?? self::getByCustomer($customerId);
}
public static function getDeliveryAddress($customerId)
{
- $addresses = CustomerAddress::byCustomer($customerId)->byDelivery()->get();
- return count($addresses) ? $addresses->first() : self::getByCustomer($customerId);
+ $address = CustomerAddress::byCustomer($customerId)
+ ->byDelivery()
+ ->orderByDesc('priority')
+ ->orderBy('id')
+ ->first();
+
+ return $address ?? self::getByCustomer($customerId);
}
public static function getByCustomer($customerId = false)
@@ -92,6 +120,40 @@ class CustomerAddresses
return ((int) $type === 1) ? '' : '';
}
+ public static function setDefault($customerId, $addressId, $type)
+ {
+ if (! $addressId) {
+ return;
+ }
+
+ $address = self::get($addressId);
+
+ if (! $address || (int) $address->customer_id !== (int) $customerId || (int) $address->type !== (int) $type) {
+ return;
+ }
+
+ self::getModel()->byCustomer($customerId)->byType($type)->update(['priority' => null]);
+
+ $address->priority = 1;
+ $address->save();
+ }
+
+ public static function ensureDefault($customerId, $type)
+ {
+ $hasDefault = self::getModel()->byCustomer($customerId)->byType($type)->where('priority', 1)->exists();
+
+ if ($hasDefault) {
+ return;
+ }
+
+ $address = self::getModel()->byCustomer($customerId)->byType($type)->orderBy('id')->first();
+
+ if ($address) {
+ $address->priority = 1;
+ $address->save();
+ }
+ }
+
public static function toggleActive($id, $active)
{
return self::update(['active' => $active], $id);
diff --git a/resources/views/Shop/Customers/partials/addresses.blade.php b/resources/views/Shop/Customers/partials/addresses.blade.php
index 858b902d..a5adc0da 100644
--- a/resources/views/Shop/Customers/partials/addresses.blade.php
+++ b/resources/views/Shop/Customers/partials/addresses.blade.php
@@ -70,10 +70,18 @@
$formContainer.addClass('d-none');
$formContainer.find('input[type="text"]').val('');
if (response.id) {
- $list.find('#address_' + response.id).prop('checked', true);
+ const $newRadio = $list.find('#address_' + response.id);
+ $list.find('input[type="radio"]').not($newRadio).prop('checked', false);
+ $newRadio.prop('checked', true);
}
- if (typeof initIcheck === 'function') {
- initIcheck('#addresses_list_{{ $prefix }} input[type="radio"]');
+ if (typeof $.fn.iCheck === 'function') {
+ $list.find('input[type="radio"]').iCheck('destroy');
+ if (typeof initIcheck === 'function') {
+ initIcheck('#addresses_list_{{ $prefix }} input[type="radio"]');
+ if (response.id) {
+ $list.find('#address_' + response.id).iCheck('check');
+ }
+ }
}
const message = response.message || '{{ __('Adresse enregistrée.') }}';
if (typeof growl === 'function') {