new: add persistence of default address selection
This commit is contained in:
@@ -101,6 +101,7 @@ class CustomerController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$address = CustomerAddresses::store($data);
|
$address = CustomerAddresses::store($data);
|
||||||
|
CustomerAddresses::setDefault($customerId, $address->id, $types[$prefix]);
|
||||||
|
|
||||||
$html = view('Shop.Customers.partials.address_item', [
|
$html = view('Shop.Customers.partials.address_item', [
|
||||||
'address' => $address->toArray(),
|
'address' => $address->toArray(),
|
||||||
@@ -136,6 +137,7 @@ class CustomerController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
CustomerAddresses::destroy($id);
|
CustomerAddresses::destroy($id);
|
||||||
|
CustomerAddresses::ensureDefault($address->customer_id, $address->type);
|
||||||
|
|
||||||
return redirect()->route('Shop.Customers.edit')
|
return redirect()->route('Shop.Customers.edit')
|
||||||
->with('growl', [__('Adresse supprimée.'), 'success']);
|
->with('growl', [__('Adresse supprimée.'), 'success']);
|
||||||
|
|||||||
@@ -12,17 +12,35 @@ class CustomerAddresses
|
|||||||
public static function storeByCustomer($customer, $data)
|
public static function storeByCustomer($customer, $data)
|
||||||
{
|
{
|
||||||
$deliveries = $data['deliveries'] ?? false;
|
$deliveries = $data['deliveries'] ?? false;
|
||||||
if ($deliveries && $deliveries['zipcode'] && $deliveries['city']) {
|
if ($deliveries) {
|
||||||
$deliveries['customer_id'] = $customer->id;
|
if (! empty($deliveries['address_id'])) {
|
||||||
$deliveries['type'] = 1;
|
self::setDefault($customer->id, (int) $deliveries['address_id'], 1);
|
||||||
self::store($deliveries);
|
}
|
||||||
|
|
||||||
|
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;
|
$invoices = $data['invoices'] ?? false;
|
||||||
if ($invoices && $invoices['zipcode'] && $invoices['city']) {
|
if ($invoices) {
|
||||||
$invoices['customer_id'] = $customer->id;
|
if (! empty($invoices['address_id'])) {
|
||||||
$invoices['type'] = 2;
|
self::setDefault($customer->id, (int) $invoices['address_id'], 2);
|
||||||
self::store($invoices);
|
}
|
||||||
|
|
||||||
|
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)
|
public static function getInvoiceAddress($customerId)
|
||||||
{
|
{
|
||||||
$addresses = CustomerAddress::byCustomer($customerId)->byInvoicing()->get();
|
$address = CustomerAddress::byCustomer($customerId)
|
||||||
return count($addresses) ? $addresses->first() : self::getByCustomer($customerId);
|
->byInvoicing()
|
||||||
|
->orderByDesc('priority')
|
||||||
|
->orderBy('id')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
return $address ?? self::getByCustomer($customerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getDeliveryAddress($customerId)
|
public static function getDeliveryAddress($customerId)
|
||||||
{
|
{
|
||||||
$addresses = CustomerAddress::byCustomer($customerId)->byDelivery()->get();
|
$address = CustomerAddress::byCustomer($customerId)
|
||||||
return count($addresses) ? $addresses->first() : self::getByCustomer($customerId);
|
->byDelivery()
|
||||||
|
->orderByDesc('priority')
|
||||||
|
->orderBy('id')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
return $address ?? self::getByCustomer($customerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getByCustomer($customerId = false)
|
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>';
|
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)
|
public static function toggleActive($id, $active)
|
||||||
{
|
{
|
||||||
return self::update(['active' => $active], $id);
|
return self::update(['active' => $active], $id);
|
||||||
|
|||||||
@@ -70,10 +70,18 @@
|
|||||||
$formContainer.addClass('d-none');
|
$formContainer.addClass('d-none');
|
||||||
$formContainer.find('input[type="text"]').val('');
|
$formContainer.find('input[type="text"]').val('');
|
||||||
if (response.id) {
|
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') {
|
if (typeof $.fn.iCheck === 'function') {
|
||||||
initIcheck('#addresses_list_{{ $prefix }} input[type="radio"]');
|
$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.') }}';
|
const message = response.message || '{{ __('Adresse enregistrée.') }}';
|
||||||
if (typeof growl === 'function') {
|
if (typeof growl === 'function') {
|
||||||
|
|||||||
Reference in New Issue
Block a user