new: add persistence of default address selection

This commit is contained in:
Valentin Lab
2025-10-04 11:49:26 +02:00
parent 8d8528f6fb
commit a34905f34e
3 changed files with 87 additions and 15 deletions

View File

@@ -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']);

View File

@@ -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) ? '<i class="fa fa-fw fa-truck"></i>' : '<i class="fa fa-fw fa-file-invoice"></i>';
}
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);

View File

@@ -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 $.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') {