From c4bb4fdd596714de3199f92d5024750fdfc51eab Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Wed, 30 Mar 2022 00:36:58 +0200 Subject: [PATCH] Try to fix price_lists by sale_channel --- app/Models/Shop/Article.php | 10 ++++++ app/Models/Shop/Offer.php | 3 +- app/Models/Shop/PriceListValue.php | 8 +++++ app/Models/Shop/Tariff.php | 2 +- app/Repositories/Shop/Articles.php | 21 ++++++++++-- app/Repositories/Shop/Offers.php | 32 ++++++++----------- composer.json | 1 + .../Shop/layout/partials/sections.blade.php | 2 +- 8 files changed, 54 insertions(+), 25 deletions(-) diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index 9c0bde82..a57e26a9 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -38,6 +38,16 @@ class Article extends Model implements HasMedia return $this->hasMany(Offer::class); } + public function price_lists() + { + return $this->hasManyDeep( + PriceList::class, + [Offer::class, Tariff::class], + ['article_id', 'id'], + ['id', 'tariff_id'], + ); + } + public function prices() { return $this->hasManyDeep( diff --git a/app/Models/Shop/Offer.php b/app/Models/Shop/Offer.php index f44fda8a..d7e5afb9 100644 --- a/app/Models/Shop/Offer.php +++ b/app/Models/Shop/Offer.php @@ -163,10 +163,9 @@ class Offer extends Model public function scopeWithPriceListValuesBySaleChannel($query, $sale_channel_id) { return $query->with([ - 'price_lists' => function($query) use ($sale_channel_id) { + 'price_lists.price_list_values' => function($query) use ($sale_channel_id) { $query->bySaleChannel($sale_channel_id); }, - 'price_lists.price_list_values', ]); } diff --git a/app/Models/Shop/PriceListValue.php b/app/Models/Shop/PriceListValue.php index ef626982..96acd65f 100644 --- a/app/Models/Shop/PriceListValue.php +++ b/app/Models/Shop/PriceListValue.php @@ -39,4 +39,12 @@ class PriceListValue extends Model { return $query->orderBy('quantity', 'desc')->where($this->table . '.quantity', '<=', $quantity)->first(); } + + public function scopeBySaleChannel($query, $sale_channel_id) { + return $query->with([ + 'price_list' => function ($query) use ($sale_channel_id) { + return $query->bySaleChannel($sale_channel_id); + }, + ]); + } } diff --git a/app/Models/Shop/Tariff.php b/app/Models/Shop/Tariff.php index 2f39b6bd..f611994e 100644 --- a/app/Models/Shop/Tariff.php +++ b/app/Models/Shop/Tariff.php @@ -53,7 +53,7 @@ class Tariff extends Model ->orWhere($this->table . '.code', 'LIKE', "${str}%"); } - public function scopeBySaleChanel($query, $id) + public function scopeBySaleChanelDefault($query, $id) { return $query->where($this->table . '.sale_channel_id', $id); } diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index 1eac5490..850669e4 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -145,9 +145,13 @@ class Articles public static function getArticlesToSell($options) { $articles = self::getArticlesWithOffers($options); + // dump($options); + // dump($articles->toArray()); + // exit; + /* foreach ($articles as $article) { $price_lists = $article->offers[0]->tariff->price_lists->toArray(); - // dump($price_lists); + dump($price_lists); if (count($price_lists)) { if (!is_array($data[$article->name] ?? false)) { $data[$article->name] = self::getDataForSale($article); @@ -157,6 +161,7 @@ class Articles $data[$article->name][$article_nature_name] = self::getDataPriceForSale($article, $prices); } } + */ return $data ?? false; } @@ -190,14 +195,24 @@ class Articles $sale_channel_id = $options['sale_channel_id'] ?? SaleChannels::getDefaultID(); $tags = $options['tags'] ?? false; $model = ($options['homepage'] ?? false) ? Article::homepage()->visible() : Article::visible(); + + dump($model->with([ + 'offers' => function($query) use ($sale_channel_id) { + $query->withPriceListValuesBySaleChannel($sale_channel_id); + }, + ]) + ->get()->toArray()); + dump('ici'); + + // exit; return $model->byCategory($category_id)->byTags($tags)->withAvailableOffers($sale_channel_id)->with([ 'image', 'product', 'article_nature', - 'offers.variation.package', - 'offers.tariff.price_lists' => function($query) use ($sale_channel_id) { + 'offers' => function($query) use ($sale_channel_id) { $query->bySaleChannel($sale_channel_id); }, + 'offers.variation.package', 'offers.tariff.price_lists.price_list_values', ])->get(); } diff --git a/app/Repositories/Shop/Offers.php b/app/Repositories/Shop/Offers.php index 2f735ad2..20b0fcd0 100644 --- a/app/Repositories/Shop/Offers.php +++ b/app/Repositories/Shop/Offers.php @@ -48,27 +48,25 @@ class Offers ]; } - public static function getOffersByArticles($articles_ids, $sale_channel_id = false) + public static function getOffersByArticles($article_ids, $sale_channel_id = false) { - $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID(); - return Offer::active() - ->with([ - 'article_nature', - 'variation', - 'tariff.price_lists' => function ($query) use ($sale_channel_id) { - $query->bySaleChannel($sale_channel_id); - }, - 'tariff.price_lists.price_list_values', - ]) - ->byArticles($articles_ids) - ->bySaleChannel($sale_channel_id) - ->get(); + return self::getOffersBySaleChannelRaw($sale_channel_id)->byArticles($article_ids)->get(); } public static function getOffersByArticle($article_id, $sale_channel_id = false) + { + return self::getOffersBySaleChannelRaw($sale_channel_id)->byArticle($article_id)->get(); + } + + public static function getOffersBySaleChannel($sale_channel_id = false) + { + return self::getOffersBySaleChannelRaw($sale_channel_id)->get(); + } + + public static function getOffersBySaleChannelRaw($sale_channel_id = false) { $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID(); - return Offer::active() + return Offer::active()->byStockAvailable() ->with([ 'article_nature', 'variation', @@ -77,9 +75,7 @@ class Offers }, 'tariff.price_lists.price_list_values', ]) - ->byArticle($article_id) - ->bySaleChannel($sale_channel_id) - ->get(); + ->bySaleChannel($sale_channel_id); } public static function getThumbSrcById($id) diff --git a/composer.json b/composer.json index 8c910cdc..11442ac4 100644 --- a/composer.json +++ b/composer.json @@ -67,6 +67,7 @@ "php-console/php-console": "^3.1", "proengsoft/laravel-jsvalidation": "^4.5", "qoraiche/laravel-mail-editor": "^3.2", + "rahul900day/laravel-captcha": "^1.0", "respect/validation": "^2.2", "rinvex/laravel-categories": "^6.0", "rinvex/laravel-tags": "^6.0", diff --git a/resources/views/Shop/layout/partials/sections.blade.php b/resources/views/Shop/layout/partials/sections.blade.php index 663c128f..16ff7175 100644 --- a/resources/views/Shop/layout/partials/sections.blade.php +++ b/resources/views/Shop/layout/partials/sections.blade.php @@ -1,6 +1,6 @@