From b4856266c88227f84fd16fa44351546d3631012f Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Sun, 30 Jan 2022 22:48:04 +0100 Subject: [PATCH] Add method to get offers by articles with siblings, enhance display --- Gruntfile.js | 6 +- .../Controllers/Shop/ArticleController.php | 6 +- app/Models/Shop/Article.php | 5 + app/Models/Shop/Offer.php | 16 +++- app/Repositories/Shop/Articles.php | 43 +++++++++ app/Repositories/Shop/Offers.php | 10 ++ composer.json | 1 + resources/lang/fr.json | 4 +- .../Shop/Articles/partials/article.blade.php | 2 +- .../Articles/partials/article_rows.blade.php | 92 +++++++++---------- .../layout/partials/category_add.blade.php | 4 +- 11 files changed, 129 insertions(+), 60 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 12d04719..7338c92c 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -53,13 +53,13 @@ var jsCoreInclude = [ 'build/js/include/form/upload.js', 'build/js/include/form/validator.js', 'build/js/include/layout/animate.js', - 'build/js/include/layout/message.js', + // 'build/js/include/layout/message.js', // 'build/js/include/layout/modal.js', 'build/js/include/layout/scroll.js', 'build/js/include/layout/tooltip.js', // 'build/js/include/datatable.js', - 'build/js/include/file.js', - 'build/js/include/uploader.js', + // 'build/js/include/file.js', + // 'build/js/include/uploader.js', ] var jsBundle = [ diff --git a/app/Http/Controllers/Shop/ArticleController.php b/app/Http/Controllers/Shop/ArticleController.php index fe461fc6..3acefcb8 100644 --- a/app/Http/Controllers/Shop/ArticleController.php +++ b/app/Http/Controllers/Shop/ArticleController.php @@ -13,9 +13,9 @@ class ArticleController extends Controller public function show($id) { $data = self::init(); - $data['article'] = Articles::getArticle($id); - // dump($data); - // exit; + $data['article'] = Articles::getArticleToSell($id); + dump($data); + exit; return view('Shop.Articles.show', $data); } } diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index d9c778ca..10b92c29 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -50,6 +50,11 @@ class Article extends Model implements HasMedia return $this->morphTo(); } + public function siblings() + { + return $this->hasMany(Article::class, 'name', 'name'); + } + public function tags() { return $this->morphToMany(Tag::class, 'taggable'); diff --git a/app/Models/Shop/Offer.php b/app/Models/Shop/Offer.php index 0304a343..13a154ab 100644 --- a/app/Models/Shop/Offer.php +++ b/app/Models/Shop/Offer.php @@ -4,11 +4,12 @@ namespace App\Models\Shop; use Illuminate\Database\Eloquent\Model; +use Znck\Eloquent\Traits\BelongsToThrough; use App\Traits\Model\HasComments; class Offer extends Model { - use HasComments; + use BelongsToThrough, HasComments; protected $guarded = ['id']; protected $table = 'shop_offers'; @@ -18,6 +19,14 @@ class Offer extends Model return $this->belongsTo(Article::class); } + public function article_nature() + { + return $this->belongsToThrough(ArticleNature::class, Article::class, null, '', [ + 'App\Models\Shop\Article' => 'article_id', + 'App\Models\Shop\ArticleNature' => 'article_nature_id', + ]); + } + public function tariff() { return $this->belongsTo(Tariff::class); @@ -63,6 +72,11 @@ class Offer extends Model return $query->where($this->table . '.article_id', $id); } + public function scopeByArticles($query, $ids) + { + return $query->whereIn($this->table . '.article_id', $ids); + } + public function scopeByOffer($query, $id) { return $query->where($this->table . '.id', $id); diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index 65b871d9..bec27793 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -27,6 +27,35 @@ class Articles return $export; } + public static function getOffersGroupedByNature($id) + { + $article_ids = self::getSiblingsIds($id); + $offers = Offers::getOffersByArticles($article_ids); + dump($offers->toArray()); + foreach ($offers as $offer) { + $data[$offer->article_nature->name][] = [ + 'name' => $offer->variation->name, + 'tariff' => $offer->tariff, + ]; + } + return $data; + } + + public static function getSiblingsIDs($id) + { + return self::getSiblings($id)->pluck('id')->toArray(); + } + + public static function getSiblings($id) + { + return Article::with('siblings')->find($id)->siblings; + } + + public static function getOffersById($id) + { + return Offers::getOffersByArticle($id); + } + public static function getOptions() { return Article::orderBy('name', 'asc')->pluck('name', 'id')->toArray(); @@ -47,6 +76,20 @@ class Articles return Article::orderBy('name', 'asc')->get(); } + public static function getArticleToSell($id) + { + $article = self::get($id); + $data = $article->toArray(); + $data['description'] = (!empty($article->description)) ? $article->description : $article->product->description; + $data['image'] = self::getPreview($article->image); + $data['image_big'] = self::getImage($article->image); + $data['inherited'] = self::getInherited($id); + $data['categories'] = self::getCategoriesNameByArticle($article); + $data['tags'] = self::getTagsSlugByArticle($article); + $data['offers'] = self::getOffersById($id)->toArray(); + return $data; + } + public static function getArticle($id) { $article = self::get($id); diff --git a/app/Repositories/Shop/Offers.php b/app/Repositories/Shop/Offers.php index 8124bebd..590cc51d 100644 --- a/app/Repositories/Shop/Offers.php +++ b/app/Repositories/Shop/Offers.php @@ -7,6 +7,16 @@ use App\Models\Shop\Offer; class Offers { + public static function getOffersByArticles($articles_id) + { + return Offer::active()->with(['article_nature', 'tariff.price_lists.price_list_values', 'variation'])->byArticles($articles_id)->get(); + } + + public static function getOffersByArticle($article_id) + { + return Offer::active()->with('variation')->byArticle($article_id)->get(); + } + public static function getThumbSrcById($id) { return self::getThumbSrc(self::get($id)); diff --git a/composer.json b/composer.json index bda8c39a..d6724255 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "alexisgeneau/mailvalidate": "dev-master", "arcanedev/log-viewer": "^8.1", "arrilot/laravel-widgets": "^3.13", + "awobaz/compoships": "^2.1", "barryvdh/laravel-dompdf": "^0.9", "barryvdh/laravel-snappy": "^0.4.7", "bencoderus/min-auth": "^1.0", diff --git a/resources/lang/fr.json b/resources/lang/fr.json index d45e7e5d..925cf7d9 100644 --- a/resources/lang/fr.json +++ b/resources/lang/fr.json @@ -54,5 +54,7 @@ "cancel": "Annuler", "save": "Sauver", "comments": "Notes internes", - "comment_add": "Ajout de Note interne" + "comment_add": "Ajout de Note interne", + "apply": "Appliquer", + "close": "Fermer" } \ No newline at end of file diff --git a/resources/views/Shop/Articles/partials/article.blade.php b/resources/views/Shop/Articles/partials/article.blade.php index 7e61c112..1cb04564 100644 --- a/resources/views/Shop/Articles/partials/article.blade.php +++ b/resources/views/Shop/Articles/partials/article.blade.php @@ -8,7 +8,7 @@
-

{{ $article['parent_name'] }}

+

{{ $article['parent_name'] }}

{{ $article['product_name'] }}
diff --git a/resources/views/Shop/Articles/partials/article_rows.blade.php b/resources/views/Shop/Articles/partials/article_rows.blade.php index defa4690..32d85605 100644 --- a/resources/views/Shop/Articles/partials/article_rows.blade.php +++ b/resources/views/Shop/Articles/partials/article_rows.blade.php @@ -1,55 +1,49 @@ - -
-
-
{{ $product_name }}
+
+
+ -
+
- +
+
+
+ @if ($article['semences'] ?? false) + {{ $article['semences']['price'] ?? null }}
+ {{ $article['semences']['variation'] }} +
+ Quantité :
-
-
-

-

-
- @if ($article['semences'] ?? false) - {{ $article['semences']['price'] ?? null }}
- {{ $article['semences']['variation'] }} -
- Quantité : -
- @include('components.form.button', [ - 'class' => 'btn-success basket semences', - 'txt' => 'Ajouter au panier', - ]) - @endif -
-
- @if ($article['plants'] ?? false) - {{ $article['plants']['price'] }}
- {{ $article['plants']['variation'] }} - @endif -
-
-

-
+ @include('components.form.button', [ + 'class' => 'btn-success basket semences', + 'txt' => 'Ajouter au panier', + ]) + @endif +
+
+ @if ($article['plants'] ?? false) + {{ $article['plants']['price'] }}
+ {{ $article['plants']['variation'] }} + @endif
- +
diff --git a/resources/views/Shop/layout/partials/category_add.blade.php b/resources/views/Shop/layout/partials/category_add.blade.php index 1e3cbf2f..d44c980c 100644 --- a/resources/views/Shop/layout/partials/category_add.blade.php +++ b/resources/views/Shop/layout/partials/category_add.blade.php @@ -1,13 +1,13 @@