diff --git a/app/Http/Controllers/Shop/CustomerController.php b/app/Http/Controllers/Shop/CustomerController.php index 7a07e89f..1c0ac6b7 100644 --- a/app/Http/Controllers/Shop/CustomerController.php +++ b/app/Http/Controllers/Shop/CustomerController.php @@ -8,6 +8,7 @@ use App\Repositories\Shop\CustomerAddresses; use App\Repositories\Shop\Customers; use App\Repositories\Shop\Offers; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Validator; class CustomerController extends Controller @@ -81,14 +82,33 @@ class CustomerController extends Controller } } + $customerId = $data['id'] ?? Customers::getId(); + $requestedDefaultSaleChannelId = $data['default_sale_channel_id'] ?? null; + $hasDefaultSaleChannelColumn = Schema::hasColumn('shop_customers', 'default_sale_channel_id'); + + if (! $hasDefaultSaleChannelColumn) { + unset($data['default_sale_channel_id']); + } + $customer = Customers::store($data); - if ($customer) { - Customers::guard()->setUser($customer->fresh(['sale_channels'])); - if (array_key_exists('default_sale_channel_id', $data)) { - session(['shop.default_sale_channel_id' => $data['default_sale_channel_id']]); - Baskets::refreshPrices((int) $data['default_sale_channel_id']); - } + if (! $customer) { + return response()->json([ + 'error' => 1, + 'message' => __('Impossible de mettre à jour votre profil pour le moment.'), + ], 422); + } + + if ($hasDefaultSaleChannelColumn && $requestedDefaultSaleChannelId !== null) { + Customers::setDefaultSaleChannel($customerId, $requestedDefaultSaleChannelId); + } + + $freshCustomer = Customers::get($customerId, ['sale_channels']); + Customers::guard()->setUser($freshCustomer); + + if ($requestedDefaultSaleChannelId !== null) { + session(['shop.default_sale_channel_id' => $requestedDefaultSaleChannelId]); + Baskets::refreshPrices((int) $requestedDefaultSaleChannelId); } return response()->json(['error' => 0]); diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index 6ec4cf4d..1a5a439d 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -6,6 +6,8 @@ use App\Models\Shop\Article; use App\Repositories\Botanic\Species; use App\Repositories\Botanic\Varieties; use App\Repositories\Shop\SaleChannels; +use App\Repositories\Shop\Customers; +use Illuminate\Support\Facades\Schema; use App\Repositories\Core\Comments; use App\Traits\Model\Basic; use App\Traits\Repository\Imageable; @@ -72,12 +74,31 @@ class Articles public static function getArticleToSell($id, $saleChannelId = false) { $saleChannelId = $saleChannelId ?: SaleChannels::getDefaultID(); + $sessionSaleChannelId = session('shop.default_sale_channel_id'); + $customer = Customers::getAuth(); + $hasDefaultSaleChannelColumn = Schema::hasColumn('shop_customers', 'default_sale_channel_id'); + $customerDefaultSaleChannelId = ($customer && $hasDefaultSaleChannelColumn) + ? $customer->default_sale_channel_id + : null; + $customerSaleChannelIds = []; + + if ($customer) { + $customer->loadMissing('sale_channels:id'); + $customerSaleChannelIds = $customer->sale_channels->pluck('id')->toArray(); + } $data = self::getArticle($id); $data['offers'] = self::getOffersGroupedByNature($id, $saleChannelId); $currentSaleChannel = $saleChannelId ? SaleChannels::get($saleChannelId) : null; $data['current_sale_channel'] = $currentSaleChannel ? $currentSaleChannel->toArray() : null; $data['available_sale_channels'] = Offers::getSaleChannelsForArticle($id); + $data['debug_sale_channel'] = [ + 'session_default_sale_channel_id' => $sessionSaleChannelId, + 'customer_default_sale_channel_id' => $customerDefaultSaleChannelId, + 'customer_linked_sale_channel_ids' => $customerSaleChannelIds, + 'resolved_sale_channel_id' => $saleChannelId, + 'has_default_sale_channel_column' => $hasDefaultSaleChannelColumn, + ]; return $data; } diff --git a/app/Repositories/Shop/Customers.php b/app/Repositories/Shop/Customers.php index 7c7ca9b8..d00861a4 100644 --- a/app/Repositories/Shop/Customers.php +++ b/app/Repositories/Shop/Customers.php @@ -179,6 +179,24 @@ class Customers return $customer->sale_channels()->sync($saleChannels); } + public static function setDefaultSaleChannel($customerId, $saleChannelId) + { + if (! $customerId) { + return false; + } + + $customer = self::get($customerId); + + if (! $customer) { + return false; + } + + $customer->default_sale_channel_id = $saleChannelId ?: null; + $customer->save(); + + return $customer->fresh(['sale_channels']); + } + public static function create($data) { $data['uuid'] = Str::uuid(); diff --git a/app/Repositories/Shop/Offers.php b/app/Repositories/Shop/Offers.php index 181e5080..6350dda9 100644 --- a/app/Repositories/Shop/Offers.php +++ b/app/Repositories/Shop/Offers.php @@ -182,27 +182,45 @@ class Offers $offers = Offer::query() ->byArticle($articleId) - ->with('article') + ->with([ + 'article', + 'tariff:id,status_id', + ]) ->get(); return $channels->map(function ($channel) use ($offers) { $priceValue = null; + $candidateOffer = null; foreach ($offers as $offer) { $priceCandidate = self::getPrice($offer->id, 1, $channel->id); if ($priceCandidate && (float) $priceCandidate->price_taxed > 0) { $priceValue = $priceCandidate; + $candidateOffer = $offer; break; } } + $offerId = $candidateOffer ? $candidateOffer->id : null; + $offerStock = $candidateOffer ? (int) $candidateOffer->stock_current : null; + $offerIsActive = $candidateOffer ? (int) $candidateOffer->status_id === 1 : false; + $offerTariffStatus = $candidateOffer && $candidateOffer->tariff ? (int) $candidateOffer->tariff->status_id : null; + $offerHasStock = $candidateOffer && $candidateOffer->stock_current !== null + ? (float) $candidateOffer->stock_current > 0 + : null; + return [ 'id' => $channel->id, 'name' => $channel->name, 'code' => $channel->code, 'price_taxed' => $priceValue ? (float) $priceValue->price_taxed : null, 'quantity' => $priceValue ? (int) $priceValue->quantity : null, + 'offer_id' => $offerId, + 'offer_is_active' => $offerIsActive, + 'offer_stock_current' => $offerStock, + 'offer_has_stock' => $offerHasStock, + 'tariff_status_id' => $offerTariffStatus, ]; })->toArray(); } diff --git a/resources/views/Shop/Articles/show.blade.php b/resources/views/Shop/Articles/show.blade.php index 333d1a88..694d33c5 100644 --- a/resources/views/Shop/Articles/show.blade.php +++ b/resources/views/Shop/Articles/show.blade.php @@ -48,35 +48,53 @@
- @if (config('app.debug') && ($article['current_sale_channel'] ?? false)) + @if (config('app.debug') && !empty($article['available_sale_channels']))
- Canal actif : - {{ $article['current_sale_channel']['name'] ?? 'N/A' }} - - ID {{ $article['current_sale_channel']['id'] ?? '–' }} · Code {{ $article['current_sale_channel']['code'] ?? '–' }} - - @if (!empty($article['available_sale_channels'])) -
- Offres disponibles dans : -
+ + @endforeach +
@endif @include('Shop.Articles.partials.ArticleAddBasket') diff --git a/resources/views/Shop/Customers/partials/deliveries.blade.php b/resources/views/Shop/Customers/partials/deliveries.blade.php index abb225cc..e6b840d8 100644 --- a/resources/views/Shop/Customers/partials/deliveries.blade.php +++ b/resources/views/Shop/Customers/partials/deliveries.blade.php @@ -157,6 +157,12 @@ ? xhr.responseJSON.message : "{{ __('Une erreur est survenue lors de l\'enregistrement du canal de vente préféré.') }}"; + console.error('Sale channel update failed', { + status: xhr.status, + response: xhr.responseJSON || xhr.responseText, + selectedSaleChannel + }); + alert(message); if (currentSaleChannelId) {