From 36f16921bb5238a0d5e68d899d46145d86591128 Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Sun, 20 Feb 2022 21:38:21 +0100 Subject: [PATCH] change construction of articles/offers --- app/Datatables/Shop/TariffsDataTable.php | 4 ++-- app/Models/Shop/Article.php | 11 +++++++---- app/Models/Shop/Offer.php | 14 +++++++++++++- app/Models/Shop/PriceList.php | 7 ++++++- app/Models/Shop/Tariff.php | 18 +++++++++++++++--- app/Repositories/Shop/Articles.php | 22 +++++++++++----------- app/Repositories/Shop/Categories.php | 3 --- 7 files changed, 54 insertions(+), 25 deletions(-) diff --git a/app/Datatables/Shop/TariffsDataTable.php b/app/Datatables/Shop/TariffsDataTable.php index ffd32345..624158f7 100644 --- a/app/Datatables/Shop/TariffsDataTable.php +++ b/app/Datatables/Shop/TariffsDataTable.php @@ -42,8 +42,8 @@ class TariffsDataTable extends DataTable Column::make('sale_channels2')->title('Canaux de vente')->searchable(false)->orderable(false), Column::make('code')->title('Code'), Column::make('ref')->title('Référence'), - Column::make('price_lists_count')->title('#Lst prix')->searchable(false)->orderable(false), - Column::make('offers_count')->title('#Offres')->searchable(false)->orderable(false), + Column::make('price_lists_count')->title('#Lst prix')->searchable(false)->orderable(false)->class('text-right'), + Column::make('offers_count')->title('#Offres')->searchable(false)->orderable(false)->class('text-right'), $this->makeColumnButtons(), ]; } diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index 10b92c29..70266ea3 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -38,12 +38,15 @@ class Article extends Model implements HasMedia return $this->hasMany(Offer::class); } - /* public function prices() { - return $this->hasManyDeep(PriceListValue::class, [Offer::class, Tariff::class, PriceList::class]); + return $this->hasManyDeep( + PriceListValue::class, + [Offer::class, Tariff::class, PriceList::class], + ['article_id', 'id', 'tariff_id'], + ['id', 'tariff_id', 'id'], + ); } - */ public function product() { @@ -120,7 +123,7 @@ class Article extends Model implements HasMedia public function scopeWithAvailableOffers($query) { return $query->whereHas('offers', function ($query) { - $query->byStatus(1); + $query->active(); }); } diff --git a/app/Models/Shop/Offer.php b/app/Models/Shop/Offer.php index 13a154ab..41148e63 100644 --- a/app/Models/Shop/Offer.php +++ b/app/Models/Shop/Offer.php @@ -64,7 +64,7 @@ class Offer extends Model public function scopeActive($query) { - return $query->where($this->table . '.status_id', 1); + return $query->byStatus(1)->byTariffActive(); } public function scopeByArticle($query, $id) @@ -77,6 +77,11 @@ class Offer extends Model return $query->whereIn($this->table . '.article_id', $ids); } + public function scopeByID($query, $id) + { + return $query->where($this->table . '.id', $id); + } + public function scopeByOffer($query, $id) { return $query->where($this->table . '.id', $id); @@ -126,4 +131,11 @@ class Offer extends Model $query->whereIn('tag_id', $tags); }); } + + public function scopeByTariffActive($query) + { + return $query->whereHas('tariff', function ($query) { + $query->active(); + }); + } } diff --git a/app/Models/Shop/PriceList.php b/app/Models/Shop/PriceList.php index 3f7bbf37..b6ba3d50 100644 --- a/app/Models/Shop/PriceList.php +++ b/app/Models/Shop/PriceList.php @@ -50,10 +50,15 @@ class PriceList extends Model return $query->where($this->table . '.status_id', $id); } + public function scopeActive($query) + { + return $query->byStatus(0)->has('price_list_values'); + } + public function scopeByOffer($query, $id) { return $query->whereHas('offers', function ($query) use ($id) { - $query->byOffer($id); + $query->byID($id); }); } } diff --git a/app/Models/Shop/Tariff.php b/app/Models/Shop/Tariff.php index 674477ef..d85c55b6 100644 --- a/app/Models/Shop/Tariff.php +++ b/app/Models/Shop/Tariff.php @@ -50,8 +50,8 @@ class Tariff extends Model public function scopeByAutocomplete($query, $str) { return $query->where($this->table . '.name', 'LIKE', "%${str}%") - ->orWhere($this->table . '.ref', 'LIKE', "${str}%") - ->orWhere($this->table . '.code', 'LIKE', "${str}%"); + ->orWhere($this->table . '.ref', 'LIKE', "${str}%") + ->orWhere($this->table . '.code', 'LIKE', "${str}%"); } public function scopeBySaleChanel($query, $id) @@ -67,7 +67,19 @@ class Tariff extends Model public function scopeByOffer($query, $id) { return $query->whereHas('offers', function ($query) use ($id) { - $query->where('id', $id); + $query->byID($id); + }); + } + + public function scopeActive($query) + { + return $query->byStatus(0)->byPriceListsActive(); + } + + public function scopeByPriceListsActive($query) + { + return $query->whereHas('price_lists', function ($query) { + $query->active(); }); } } diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index adb78f10..4d801962 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -111,14 +111,16 @@ class Articles $price_lists = $article->offers[0]->tariff->price_lists->toArray(); // dump($price_lists); if (count($price_lists)) { - $data[$article->name] = [ - 'description' => (!empty($article->description)) ? $article->description : $article->product->description, - 'image' => $article->image, - 'product_type' => $article->product_type, - 'product_id' => $article->product_id, - 'product_name' => $article->product->name, - 'parent_name' => trim(str_replace($article->product->name, '', $article->name)), - ]; + if (!is_array($data[$article->name] ?? false)) { + $data[$article->name] = [ + 'description' => (!empty($article->description)) ? $article->description : $article->product->description, + 'image' => $article->image, + 'product_type' => $article->product_type, + 'product_id' => $article->product_id, + 'product_name' => $article->product->name, + 'parent_name' => trim(str_replace($article->product->name, '', $article->name)), + ]; + } $prices = $price_lists[0]['price_list_values'][0]; $article_nature_name = strtolower($article->article_nature->name); // dump($prices); @@ -129,10 +131,8 @@ class Articles 'price' => $prices['price_taxed'], 'variation' => $article->offers[0]->variation->name, ]; - } + } } - // dump($data); - // exit; return $data ?? false; } diff --git a/app/Repositories/Shop/Categories.php b/app/Repositories/Shop/Categories.php index dcac0a4a..a3be9a4b 100644 --- a/app/Repositories/Shop/Categories.php +++ b/app/Repositories/Shop/Categories.php @@ -129,14 +129,11 @@ class Categories $id = $id ? (int) $id : $data['id']; $category = self::get($id); $ret = $category->update($data); - // CategoryTrees::update($data, $category->category_id); return $category; } public static function destroy($id) { - // $category = self::get($id); - // CategoryTrees::destroy($category->category_id); return Category::destroy($id); }