From 3b6108b449e59bc53b7fcba5e3af12f664505d96 Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Mon, 24 Aug 2020 01:14:54 +0200 Subject: [PATCH] [WIP] Refactor models to better lisibility and speed --- app/Models/Shop/Article.php | 2 +- app/Models/Shop/ArticleAttribute.php | 43 ---------- app/Models/Shop/ArticleAttributeFamily.php | 25 ------ app/Models/Shop/ArticleAttributeValue.php | 32 -------- app/Models/Shop/ArticleCategory.php | 31 -------- app/Models/Shop/ArticlePrice.php | 60 +++++--------- app/Models/Shop/ArticlePriceGeneric.php | 20 ----- app/Models/Shop/Inventory.php | 18 ----- app/Models/Shop/InventoryPlace.php | 18 ----- app/Models/Shop/Price.php | 40 ++++++++++ app/Models/Shop/PriceFamily.php | 30 +++++++ app/Models/Shop/PriceFamilyValue.php | 22 ++++++ app/Models/Shop/PriceGeneric.php | 30 +++++++ app/Models/Shop/PriceGenericValue.php | 30 +++++++ .../Shop/ArticleAttributeFamilies.php | 55 ------------- app/Repositories/Shop/ArticleAttributes.php | 78 ------------------- app/Repositories/Shop/Articles.php | 13 +--- ...rticleCategories.php => PriceFamilies.php} | 21 +++-- ...ributeValues.php => PriceFamilyValues.php} | 24 +++--- app/Repositories/Shop/PriceGenericValues.php | 61 +++++++++++++++ ...clePriceGenerics.php => PriceGenerics.php} | 22 +++--- app/Repositories/Shop/Prices.php | 76 ++++++++++++++++++ 22 files changed, 351 insertions(+), 400 deletions(-) delete mode 100644 app/Models/Shop/ArticleAttribute.php delete mode 100644 app/Models/Shop/ArticleAttributeFamily.php delete mode 100644 app/Models/Shop/ArticleAttributeValue.php delete mode 100644 app/Models/Shop/ArticleCategory.php delete mode 100644 app/Models/Shop/ArticlePriceGeneric.php delete mode 100644 app/Models/Shop/Inventory.php delete mode 100644 app/Models/Shop/InventoryPlace.php create mode 100644 app/Models/Shop/Price.php create mode 100644 app/Models/Shop/PriceFamily.php create mode 100644 app/Models/Shop/PriceFamilyValue.php create mode 100644 app/Models/Shop/PriceGeneric.php create mode 100644 app/Models/Shop/PriceGenericValue.php delete mode 100644 app/Repositories/Shop/ArticleAttributeFamilies.php delete mode 100644 app/Repositories/Shop/ArticleAttributes.php rename app/Repositories/Shop/{ArticleCategories.php => PriceFamilies.php} (58%) rename app/Repositories/Shop/{ArticleAttributeValues.php => PriceFamilyValues.php} (55%) create mode 100644 app/Repositories/Shop/PriceGenericValues.php rename app/Repositories/Shop/{ArticlePriceGenerics.php => PriceGenerics.php} (59%) create mode 100644 app/Repositories/Shop/Prices.php diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index ee7737c2..08aa4dbb 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -48,7 +48,7 @@ class Article extends Model implements HasMedia public function prices() { - return $this->hasManyThrough('App\Models\Shop\ArticlePrice','App\Models\Shop\ArticleAttribute'); + return $this->hasMany('App\Models\Shop\Price'); } public function product() diff --git a/app/Models/Shop/ArticleAttribute.php b/app/Models/Shop/ArticleAttribute.php deleted file mode 100644 index 8f70a37f..00000000 --- a/app/Models/Shop/ArticleAttribute.php +++ /dev/null @@ -1,43 +0,0 @@ -belongsTo('App\Models\Shop\Article'); - } - - public function attribute_value() - { - return $this->belongsTo('App\Models\Shop\ArticleAttributeValue', 'article_attribute_value_id'); - } - - public function prices() - { - return $this->hasMany('App\Models\Shop\ArticlePrice'); - } - - public function scopeByArticle($query, $id) - { - return $query->where('article_id', $id); - } - - public function scopeByAttributeValue($query, $id) - { - return $query->where('attribute_value_id', $id); - } - - public function scopeByFamily($query, $id) - { - return $query->whereHas('attribute_value', function ($query) use ($id) { - $query->byFamily($id); - }); - } -} \ No newline at end of file diff --git a/app/Models/Shop/ArticleAttributeFamily.php b/app/Models/Shop/ArticleAttributeFamily.php deleted file mode 100644 index 4300664c..00000000 --- a/app/Models/Shop/ArticleAttributeFamily.php +++ /dev/null @@ -1,25 +0,0 @@ -hasMany('App\Models\Shop\ArticleAttributeValue'); - } - - public function articles() - { - return $this->hasManyThrough('App\Models\Shop\ArticleAttribute','App\Models\Shop\ArticleAttributeValue'); - } - -} \ No newline at end of file diff --git a/app/Models/Shop/ArticleAttributeValue.php b/app/Models/Shop/ArticleAttributeValue.php deleted file mode 100644 index 027cd9b9..00000000 --- a/app/Models/Shop/ArticleAttributeValue.php +++ /dev/null @@ -1,32 +0,0 @@ -belongsTo('App\Models\Shop\ArticleAttributeFamily'); - } - - public function attributes() - { - return $this->hasMany('App\Models\Shop\ArticleAttribute'); - } - - public function articles() - { - return $this->hasMany('App\Models\Shop\ArticleAttribute')->groupBy('article_id'); - } - - public function scopeByFamily($query, $id) - { - return $query->where('article_attribute_family_id', $id); - } - -} \ No newline at end of file diff --git a/app/Models/Shop/ArticleCategory.php b/app/Models/Shop/ArticleCategory.php deleted file mode 100644 index db0eb9f6..00000000 --- a/app/Models/Shop/ArticleCategory.php +++ /dev/null @@ -1,31 +0,0 @@ -belongsTo('App\Models\Shop\Article'); - } - - public function category() - { - return $this->belongsTo('App\Models\Shop\Category'); - } - - public function scopeByArticle($query, $id) - { - return $query->where('article_id', $id); - } - - public function scopeByCategory($query, $id) - { - return $query->where('category_id', $id); - } -} \ No newline at end of file diff --git a/app/Models/Shop/ArticlePrice.php b/app/Models/Shop/ArticlePrice.php index e9b47173..b3c12e09 100644 --- a/app/Models/Shop/ArticlePrice.php +++ b/app/Models/Shop/ArticlePrice.php @@ -7,46 +7,28 @@ use Znck\Eloquent\Traits\BelongsToThrough; class ArticlePrice extends Model { - use BelongsToThrough; + use BelongsToThrough; - protected $guarded = ['id']; - protected $table = 'shop_article_prices'; + protected $guarded = ['id']; + protected $table = 'shop_article_prices'; - public function article_attribute() + public function price() + { + return $this->hasOne('App\Models\Price'); + } + + public function priceFamilyValue() + { + return $this->belongsTo('App\Models\PriceFamilyValue'); + } + + public function scopeByQuantity($query, $quantity) + { + return $query->orderBy('quantity', 'desc')->where('quantity', '<', $quantity)->first(); + } + + public function scopeByPriceFamilyValue($query, $id) { - return $this->belongsTo('App\Models\Shop\ArticleAttribute'); - } - - public function article() - { - return $this->belongsToThrough( - 'App\Models\Shop\Article', - 'App\Models\Shop\ArticleAttribute', - null, - '', - ['App\Models\Shop\Article' => 'article_id', 'App\Models\Shop\ArticleAttribute' => 'article_attribute_id'] - ); - } - - public function scopeByArticle($query, $id) - { - return $query->whereHas('article', function ($query) use ($id) { - $query->byArticle($id); - }); - } - - public function scopeByAttributeValue($query, $id) - { - return $query->whereHas('article_attribute', function ($query) use ($id) { - $query->byAttributeValue($id); - }); - } - - public function scopeByFamily($query, $id) - { - return $query->whereHas('article_attribute', function ($query) use ($id) { - $query->byFamily($id); - }); - } - + return $query->where('price_family_value_id', $id); + } } \ No newline at end of file diff --git a/app/Models/Shop/ArticlePriceGeneric.php b/app/Models/Shop/ArticlePriceGeneric.php deleted file mode 100644 index 8cce8ae2..00000000 --- a/app/Models/Shop/ArticlePriceGeneric.php +++ /dev/null @@ -1,20 +0,0 @@ -hasMany('App\Models\Shop\ArticlePrice'); - } - -} \ No newline at end of file diff --git a/app/Models/Shop/Inventory.php b/app/Models/Shop/Inventory.php deleted file mode 100644 index 4e35ea44..00000000 --- a/app/Models/Shop/Inventory.php +++ /dev/null @@ -1,18 +0,0 @@ -belongsTo('App\Models\Shop\InventoryPlace'); - } -} \ No newline at end of file diff --git a/app/Models/Shop/InventoryPlace.php b/app/Models/Shop/InventoryPlace.php deleted file mode 100644 index d9f604c8..00000000 --- a/app/Models/Shop/InventoryPlace.php +++ /dev/null @@ -1,18 +0,0 @@ -hasMany('App\Models\Inventory'); - } -} \ No newline at end of file diff --git a/app/Models/Shop/Price.php b/app/Models/Shop/Price.php new file mode 100644 index 00000000..b6758c1d --- /dev/null +++ b/app/Models/Shop/Price.php @@ -0,0 +1,40 @@ +belongsTo('App\Models\Shop\Article'); + } + + public function price_family() + { + return $this->belongsTo('App\Models\Shop\PriceFamily'); + } + + public function price() + { + return $this->morphTo(); + } + + public function scopeByArticle($query, $id) + { + return $query->where('article_id', $id); + } + + public function scopeByPriceFamily($query, $id) + { + return $query->where('price_family_id', $id); + } + +} \ No newline at end of file diff --git a/app/Models/Shop/PriceFamily.php b/app/Models/Shop/PriceFamily.php new file mode 100644 index 00000000..ebe55210 --- /dev/null +++ b/app/Models/Shop/PriceFamily.php @@ -0,0 +1,30 @@ +hasMany('App\Models\Shop\Price'); + } + + public function values() + { + return $this->hasMany('App\Models\Shop\PriceFamilyValue'); + } + + public function articles() + { + return $this->hasManyThrough('App\Models\Shop\Article','App\Models\Shop\Price'); + } + +} \ No newline at end of file diff --git a/app/Models/Shop/PriceFamilyValue.php b/app/Models/Shop/PriceFamilyValue.php new file mode 100644 index 00000000..b5446b63 --- /dev/null +++ b/app/Models/Shop/PriceFamilyValue.php @@ -0,0 +1,22 @@ +belongsTo('App\Models\Shop\PriceFamily'); + } + + public function scopeByFamily($query, $id) + { + return $query->where('price_family_id'); + } + +} \ No newline at end of file diff --git a/app/Models/Shop/PriceGeneric.php b/app/Models/Shop/PriceGeneric.php new file mode 100644 index 00000000..b0592a64 --- /dev/null +++ b/app/Models/Shop/PriceGeneric.php @@ -0,0 +1,30 @@ +hasMany('App\Models\Shop\Price'); + } + + public function values() + { + return $this->hasMany('App\Models\Shop\PriceGenericValue'); + } + + public function articles() + { + return $this->hasManyThrough('App\Models\Shop\Article','App\Models\Shop\Price'); + } + +} \ No newline at end of file diff --git a/app/Models/Shop/PriceGenericValue.php b/app/Models/Shop/PriceGenericValue.php new file mode 100644 index 00000000..c922ddd4 --- /dev/null +++ b/app/Models/Shop/PriceGenericValue.php @@ -0,0 +1,30 @@ +belongsTo('App\Models\Shop\PriceGeneric'); + } + + public function scopeByPriceGeneric($query, $id) + { + return $query->where('price_generic_id', $id); + } + + public function scopeByQuantity($query, $quantity) + { + return $query->orderBy('quantity', 'desc')->where('quantity', '<', $quantity)->first(); + } + +} \ No newline at end of file diff --git a/app/Repositories/Shop/ArticleAttributeFamilies.php b/app/Repositories/Shop/ArticleAttributeFamilies.php deleted file mode 100644 index 5b8dd89b..00000000 --- a/app/Repositories/Shop/ArticleAttributeFamilies.php +++ /dev/null @@ -1,55 +0,0 @@ -make(true); - } - - public static function getAll() - { - return ArticleAttributeFamily::orderBy('name','asc')->get(); - } - - public static function get($id) - { - return ArticleAttributeFamily::find($id); - } - - public static function getOptions() - { - return ArticleAttributeFamily::get()->pluck('name','id')->toArray(); - } - - public static function store($data) - { - $id = isset($data['id']) ? $data['id'] : false; - $item = $id ? self::update($data) : self::create($data); - return $item->id; - } - - public static function create($data) - { - return ArticleAttributeFamily::create($data); - } - - public static function update($data) - { - return ArticleAttributeFamily::find($id)->update($data); - } - - public static function destroy($id) - { - return ArticleAttributeFamily::destroy($id); - } - -} diff --git a/app/Repositories/Shop/ArticleAttributes.php b/app/Repositories/Shop/ArticleAttributes.php deleted file mode 100644 index 51952bc5..00000000 --- a/app/Repositories/Shop/ArticleAttributes.php +++ /dev/null @@ -1,78 +0,0 @@ -make(true); - } - - public static function getAll() - { - return ArticleAttribute::orderBy('name','asc')->get(); - } - - public static function get($id) - { - return ArticleAttribute::find($id); - } - - public static function getByArticle($id) - { - return ArticleAttribute::byArticle($id)->get(); - } - - public static function getByArticleWithPrices($id) - { - return ArticleAttribute::with('prices')->byArticle($id)->get(); - } - - public static function storeAttributes($article_id, $attributes) - { - foreach ($attributes as $key => $attribute) - { - self::storeAttribute($article_id, $attributes[$key]); - } - } - - public static function storeAttribute($article_id, $attribute) - { - $attribute['article_id'] = $article_id; - unset($attribute['attribute_family_id']); - return self::store($attribute); - } - - public static function store($data) - { - $id = isset($data['id']) ? $data['id'] : false; - $item = $id ? self::update($data) : self::create($data); - return $item->id; - } - - public static function create($data) - { - return ArticleAttribute::create($data); - } - - public static function update($data, $id = false) - { - $id = isset($data['id']) ? $data['id'] : $id; - $item = ArticleAttribute::find($id); - $item->update($data); - return $item; - } - - public static function destroy($id) - { - return ArticleAttribute::destroy($id); - } - -} diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index b7ce2ce9..5a2ebe7b 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -30,7 +30,6 @@ class Articles $data = $article->toArray(); $data['categories'] = self::getCategoriesByArticle($article); $data['tags'] = self::getTagsByArticle($article); - // $data['attributes'] = self::getAttributesByArticle($article); $data['prices'] = self::getPricesByArticle($article); self::getMeta($data); return $data; @@ -41,7 +40,7 @@ class Articles $data['categories_options'] = Categories::getOptions(); $data['families_options'] = ArticleFamilies::getOptions(); $data['taxes_options'] = Taxes::getOptions(); - $data['attribute_families_options'] = ArticleAttributeFamilies::getOptions(); + $data['attribute_families_options'] = PriceFamilies::getOptions(); $data['tags_list'] = TagGroups::getTreeTags(); $data['models_options'] = ['App\Models\Botanic\Specie' => 'Espèces', 'App\Models\Botanic\Variety' => 'Variétés']; return $data; @@ -49,7 +48,8 @@ class Articles public static function getByCategory($category_id) { - return Article::with(['prices.article_attribute.attribute_value','product','image'])->get(); + // TODO add category + return Article::with(['prices','product','image'])->get(); } public static function getCategoriesByArticle($article) @@ -62,14 +62,9 @@ class Articles return $article->tags->pluck('id')->toArray(); } - public static function getAttributesByArticle($article) - { - return ArticleAttributes::getByArticleWithPrices($article->id)->toArray(); - } - public static function getPricesByArticle($article) { - return ArticlePrices::getByArticleWithAttribute($article->id)->toArray(); + return Prices::getByArticle($article->id)->toArray(); } public static function get($id) diff --git a/app/Repositories/Shop/ArticleCategories.php b/app/Repositories/Shop/PriceFamilies.php similarity index 58% rename from app/Repositories/Shop/ArticleCategories.php rename to app/Repositories/Shop/PriceFamilies.php index 8ea28331..d399472f 100644 --- a/app/Repositories/Shop/ArticleCategories.php +++ b/app/Repositories/Shop/PriceFamilies.php @@ -4,27 +4,32 @@ namespace App\Repositories\Shop; use Yajra\DataTables\DataTables; -use App\Models\Shop\ArticleCategory; +use App\Models\Shop\PriceFamily; -class ArticleCategories +class PriceFamilies { public static function getDatatable() { - $model = ArticleCategory::orderBy('name'); + $model = PriceFamily::orderBy('name'); return Datatables::of($model)->make(true); } public static function getAll() { - return ArticleCategory::orderBy('name','asc')->get(); + return PriceFamily::orderBy('name','asc')->get(); } public static function get($id) { - return ArticleCategory::find($id); + return PriceFamily::find($id); } + public static function getOptions() + { + return PriceFamily::get()->pluck('name','id')->toArray(); + } + public static function store($data) { $id = isset($data['id']) ? $data['id'] : false; @@ -34,17 +39,17 @@ class ArticleCategories public static function create($data) { - return ArticleCategory::create($data); + return PriceFamily::create($data); } public static function update($data) { - return ArticleCategory::find($id)->update($data); + return PriceFamily::find($id)->update($data); } public static function destroy($id) { - return ArticleCategory::destroy($id); + return PriceFamily::destroy($id); } } diff --git a/app/Repositories/Shop/ArticleAttributeValues.php b/app/Repositories/Shop/PriceFamilyValues.php similarity index 55% rename from app/Repositories/Shop/ArticleAttributeValues.php rename to app/Repositories/Shop/PriceFamilyValues.php index 78177563..ecf1140b 100644 --- a/app/Repositories/Shop/ArticleAttributeValues.php +++ b/app/Repositories/Shop/PriceFamilyValues.php @@ -4,36 +4,36 @@ namespace App\Repositories\Shop; use Yajra\DataTables\DataTables; -use App\Models\Shop\ArticleAttributeValue; +use App\Models\Shop\PriceFamilyValue; -class ArticleAttributeValues +class PriceFamilyValues { public static function getDatatable() { - $model = ArticleAttributeValue::orderBy('name'); + $model = PriceFamilyValue::orderBy('name'); return Datatables::of($model)->make(true); } public static function getAll() { - return ArticleAttributeValue::orderBy('name','asc')->get(); + return PriceFamilyValue::orderBy('name','asc')->get(); } public static function get($id) { - return ArticleAttributeValue::find($id); + return PriceFamilyValue::find($id); } public static function getOptions() { - return ArticleAttributeValue::get()->pluck('value','id')->toArray(); + return PriceFamilyValue::get()->pluck('value','id')->toArray(); } - public static function getSelectByFamily($attribute_family_id) + public static function getSelectByFamily($family_id) { - // return ArticleAttributeValue::byFamily($attribute_family_id)->get()->pluck('value','id')->toArray(); - $values = ArticleAttributeValue::byFamily($attribute_family_id)->get(); + // return PriceFamilyValue::byFamily($attribute_family_id)->get()->pluck('value','id')->toArray(); + $values = PriceFamilyValue::byFamily($family_id)->get(); $data = []; foreach ($values as $value) { @@ -51,17 +51,17 @@ class ArticleAttributeValues public static function create($data) { - return ArticleAttributeValue::create($data); + return PriceFamilyValue::create($data); } public static function update($data) { - return ArticleAttributeValue::find($id)->update($data); + return PriceFamilyValue::find($id)->update($data); } public static function destroy($id) { - return ArticleAttributeValue::destroy($id); + return PriceFamilyValue::destroy($id); } } diff --git a/app/Repositories/Shop/PriceGenericValues.php b/app/Repositories/Shop/PriceGenericValues.php new file mode 100644 index 00000000..cd85ac85 --- /dev/null +++ b/app/Repositories/Shop/PriceGenericValues.php @@ -0,0 +1,61 @@ +get(); + } + + public static function getDatatable() + { + $model = PriceGenericValue::orderBy('name'); + return Datatables::of($model)->make(true); + } + + public static function getAll() + { + return PriceGenericValue::orderBy('name','asc')->get(); + } + + public static function get($id) + { + return PriceGenericValue::find($id); + } + + public static function store($data) + { + $id = isset($data['id']) ? $data['id'] : false; + $price = $id ? self::update($data) : self::create($data); + return $price->id; + } + + public static function create($data) + { + return PriceGenericValue::create($data); + } + + public static function update($data, $id = false) + { + $id = isset($data['id']) ? $data['id'] : false; + $article = PriceGenericValue::find($id); + $article->update($data); + return $article; + } + + public static function destroy($id) + { + return PriceGenericValue::destroy($id); + } + +} diff --git a/app/Repositories/Shop/ArticlePriceGenerics.php b/app/Repositories/Shop/PriceGenerics.php similarity index 59% rename from app/Repositories/Shop/ArticlePriceGenerics.php rename to app/Repositories/Shop/PriceGenerics.php index fec62d3b..63ec4fc5 100644 --- a/app/Repositories/Shop/ArticlePriceGenerics.php +++ b/app/Repositories/Shop/PriceGenerics.php @@ -8,35 +8,35 @@ use Illuminate\Support\Str; use Yajra\DataTables\DataTables; -use App\Models\Shop\ArticlePriceGeneric; +use App\Models\Shop\PriceGeneric; -class ArticlePriceGenerics +class PriceGenerics { public static function getByArticle($id) { - return ArticlePriceGeneric::byArticle($id)->get(); + return PriceGeneric::byArticle($id)->get(); } - public static function getByArticleWithAttribute($id) + public static function getByArticleWithValues($id) { - return ArticlePriceGeneric::with('article_attribute.attribute_value')->byArticle($id)->get(); + return PriceGeneric::with('values')->byArticle($id)->get(); } public static function getDatatable() { - $model = ArticlePriceGeneric::orderBy('name'); + $model = PriceGeneric::orderBy('name'); return Datatables::of($model)->make(true); } public static function getAll() { - return ArticlePriceGeneric::orderBy('name','asc')->get(); + return PriceGeneric::orderBy('name','asc')->get(); } public static function get($id) { - return ArticlePriceGeneric::find($id); + return PriceGeneric::find($id); } public static function store($data) @@ -48,20 +48,20 @@ class ArticlePriceGenerics public static function create($data) { - return ArticlePriceGeneric::create($data); + return PriceGeneric::create($data); } public static function update($data, $id = false) { $id = isset($data['id']) ? $data['id'] : false; - $article = ArticlePriceGeneric::find($id); + $article = PriceGeneric::find($id); $article->update($data); return $article; } public static function destroy($id) { - return ArticlePriceGeneric::destroy($id); + return PriceGeneric::destroy($id); } } diff --git a/app/Repositories/Shop/Prices.php b/app/Repositories/Shop/Prices.php new file mode 100644 index 00000000..3f82ca55 --- /dev/null +++ b/app/Repositories/Shop/Prices.php @@ -0,0 +1,76 @@ +with('prices.price')->get()->pluck('prices')->toArray()[0]; + // dump($prices); + $data = []; + foreach ($prices as $price) + { + if ($price['price_type'] == 'App\Models\Shop\ArticlePrice') + { + $data[] = $price['price']; + } else { + $values = PriceGenericValues::getByPriceGeneric($price['price']['id'])->toArray(); + foreach ($values as $value) + { + $data[] = $value; + } + } + } + dump($data); + } + + public static function getDatatable() + { + $model = Price::orderBy('name'); + return Datatables::of($model)->make(true); + } + + public static function getAll() + { + return Price::orderBy('name','asc')->get(); + } + + public static function get($id) + { + return Price::find($id); + } + + public static function store($data) + { + $id = isset($data['id']) ? $data['id'] : false; + $item = $id ? self::update($data) : self::create($data); + return $item->id; + } + + public static function create($data) + { + return Price::create($data); + } + + public static function update($data) + { + return Price::find($id)->update($data); + } + + public static function destroy($id) + { + return Price::destroy($id); + } + +}