6 Commits

11 changed files with 157 additions and 17 deletions

View File

@@ -55,6 +55,7 @@ RUN chmod +x artisan
RUN ./artisan vendor:publish --tag=public --force ## creates public/vendor/jsvalidation
RUN ./artisan vendor:publish --tag=boilerplate-public --force --ansi ## creates public/vendor/boilerplate
RUN ./artisan vendor:publish --tag=datatables-buttons --force --ansi ## creates public/vendor/datatables/buttons
## XXXvlab: 2025-09-25 these migration files are breaking first
## install, but we had to resolve to not install from scratch and use

View File

@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Shop\Auth;
use App\Http\Controllers\Controller;
use App\Repositories\Core\User\ShopCart;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@@ -31,6 +32,7 @@ class LoginController extends Controller
]);
if ($this->guard()->attempt($credentials, $request->get('remember'))) {
ShopCart::migrateGuestCartToUser();
$request->session()->regenerate();
if (back()->getTargetUrl() === route('Shop.Orders.store')) {
$route = 'Shop.Orders.order';

View File

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Shop\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\Shop\RegisterCustomer;
use App\Repositories\Core\User\ShopCart;
use App\Repositories\Shop\CustomerSaleChannels;
use App\Repositories\Shop\CustomerAddresses;
use App\Repositories\Shop\Customers;
@@ -33,6 +34,7 @@ class RegisterController extends Controller
$user = $this->create($request->all());
$this->guard()->login($user);
ShopCart::migrateGuestCartToUser();
return $request->wantsJson()
? new JsonResponse([], 201)

View File

@@ -3,9 +3,11 @@
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class CustomerAddress extends Model
{
use SoftDeletes;
protected $guarded = ['id'];
protected $table = 'shop_customer_addresses';

View File

@@ -39,7 +39,7 @@ class Invoice extends Model
public function address(): BelongsTo
{
return $this->belongsTo(CustomerAddress::class, 'invoice_address_id');
return $this->belongsTo(CustomerAddress::class, 'invoice_address_id')->withTrashed();
}
public function scopeByCustomer($query, $customerId)

View File

@@ -29,7 +29,7 @@ class Order extends Model
public function delivery_address(): BelongsTo
{
return $this->belongsTo(CustomerAddress::class, 'delivery_address_id');
return $this->belongsTo(CustomerAddress::class, 'delivery_address_id')->withTrashed();
}
public function delivery(): BelongsTo

View File

@@ -94,11 +94,106 @@ class ShopCart
return self::get()->getContent();
}
public static function get()
public static function migrateGuestCartToUser($userId = null)
{
$userId = Auth::guard('customer')->id();
$sessionKey = 'cart_'.sha1(static::class . ($userId ?? 'guest'));
$userId = self::resolveUserId($userId);
return Cart::session($sessionKey);
if ($userId === null) {
return;
}
$guestSessionKey = self::sessionKey();
$guestItems = Cart::session($guestSessionKey)->getContent();
if ($guestItems->count() === 0) {
return;
}
$userSessionKey = self::sessionKey($userId);
foreach ($guestItems as $item) {
$existing = Cart::session($userSessionKey)->get($item->id);
if ($existing) {
Cart::session($userSessionKey)->update($item->id, [
'quantity' => [
'relative' => false,
'value' => $existing->quantity + $item->quantity,
],
]);
continue;
}
$itemData = [
'id' => $item->id,
'name' => $item->name,
'price' => $item->price,
'quantity' => $item->quantity,
'attributes' => self::extractAttributes($item),
];
if (isset($item->associatedModel)) {
$itemData['associatedModel'] = $item->associatedModel;
}
$conditions = self::extractConditions($item);
if (! empty($conditions)) {
$itemData['conditions'] = $conditions;
}
Cart::session($userSessionKey)->add($itemData);
}
Cart::session($guestSessionKey)->clear();
Cart::session($userSessionKey);
}
protected static function extractAttributes($item)
{
if (! isset($item->attributes)) {
return [];
}
if (is_object($item->attributes) && method_exists($item->attributes, 'toArray')) {
return $item->attributes->toArray();
}
return (array) $item->attributes;
}
protected static function extractConditions($item)
{
if (! isset($item->conditions)) {
return [];
}
if (is_object($item->conditions) && method_exists($item->conditions, 'toArray')) {
return $item->conditions->toArray();
}
return (array) $item->conditions;
}
protected static function resolveUserId($userId = null)
{
return $userId ?? Auth::guard('customer')->id();
}
protected static function sessionKey($userId = null)
{
$key = $userId ?? 'guest';
return 'cart_'.sha1(static::class.$key);
}
protected static function session($userId = null)
{
return Cart::session(self::sessionKey($userId));
}
public static function get($userId = null)
{
return self::session(self::resolveUserId($userId));
}
}

View File

@@ -17,12 +17,15 @@ class InvoicePDF
public static function get($id)
{
$invoice = Invoices::getFull($id);
$customFields = [];
if ($orderRef = optional($invoice->order)->ref) {
$customFields['order number'] = $orderRef;
}
$customer = new Party([
'name' => $invoice->customer->name,
'name' => optional($invoice->customer)->name ?? __('Client inconnu'),
'address' => self::makeAddress($invoice->address),
'custom_fields' => [
'order number' => $invoice->order->ref,
],
'custom_fields' => $customFields,
]);
$items = self::makeItems($invoice->order->detail);
@@ -48,7 +51,17 @@ class InvoicePDF
public static function makeAddress($address)
{
return $address->address.'<br>'.$address->zipcode.' '.$address->city;
if (! $address) {
return '';
}
$lines = array_filter([
$address->address ?? '',
$address->address2 ?? '',
trim(($address->zipcode ?? '').' '.($address->city ?? '')),
]);
return implode('<br>', $lines);
}
public static function makeItems($details)

View File

@@ -48,13 +48,20 @@
var $list = $('#addresses_list_{{ $prefix }}');
var storeUrl = '{{ route('Shop.Customers.address.store') }}';
$('#add_address_{{ $prefix }}').on('click', function() {
$formContainer.toggleClass('d-none');
var $toggleBtn = $('#add_address_{{ $prefix }}');
$toggleBtn.on('click', function() {
var isHidden = $formContainer.hasClass('d-none');
if (isHidden) {
$formContainer.removeClass('d-none');
$toggleBtn.prop('disabled', true);
}
});
$('#cancel_address_{{ $prefix }}').on('click', function() {
$formContainer.addClass('d-none');
$formContainer.find('input[type="text"]').val('');
$toggleBtn.prop('disabled', false);
});
$('#save_address_{{ $prefix }}').on('click', function() {
@@ -69,6 +76,7 @@
}
$formContainer.addClass('d-none');
$formContainer.find('input[type="text"]').val('');
$toggleBtn.prop('disabled', false);
if (response.id) {
const $newRadio = $list.find('#address_' + response.id);
$list.find('input[type="radio"]').not($newRadio).prop('checked', false);

View File

@@ -16,11 +16,27 @@
@push('js')
<script>
$(function() {
const articleShowUrlTemplate = "{{ route('Shop.Articles.show', ['id' => '__ARTICLE_ID__']) }}";
$('#search-general .fa-search').click(function() {
$('#search-general').submit();
});
initAutocomplete('#search_name');
function redirectToArticle(item, evt) {
if (!item || typeof item.value === 'undefined' || item.value === null || item.value === '') {
return;
}
if (evt) {
evt.preventDefault();
evt.stopPropagation();
}
const targetUrl = articleShowUrlTemplate.replace('__ARTICLE_ID__', item.value);
window.location.href = targetUrl;
}
initAutocomplete('#search_name', redirectToArticle);
});
</script>
@endpush

View File

@@ -13,9 +13,10 @@
var id = item.value;
$('#' + field).val(id);
if (typeof(callback) != 'undefined') {
var c = callback + '(' + id + ')';
eval(c);
if (typeof callback === 'function') {
callback.call(this, item, evt);
} else if (typeof callback === 'string' && callback.length && typeof window[callback] === 'function') {
window[callback].call(this, item, evt);
}
});
}