[WIP] Refactor models to better lisibility and speed

This commit is contained in:
Ludovic CANDELLIER
2020-08-24 01:14:54 +02:00
parent 77a239fce9
commit 3b6108b449
22 changed files with 351 additions and 400 deletions

View File

@@ -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()

View File

@@ -1,43 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class ArticleAttribute extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_article_attributes';
public function article()
{
return $this->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);
});
}
}

View File

@@ -1,25 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class ArticleAttributeFamily extends Model
{
use HasRelationships;
protected $guarded = ['id'];
protected $table = 'shop_article_attribute_families';
public function values()
{
return $this->hasMany('App\Models\Shop\ArticleAttributeValue');
}
public function articles()
{
return $this->hasManyThrough('App\Models\Shop\ArticleAttribute','App\Models\Shop\ArticleAttributeValue');
}
}

View File

@@ -1,32 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class ArticleAttributeValue extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_article_attribute_values';
public function article_attribute_family()
{
return $this->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);
}
}

View File

@@ -1,31 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class ArticleCategory extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_article_categories';
public function article()
{
return $this->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);
}
}

View File

@@ -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');
return $query->where('price_family_value_id', $id);
}
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);
});
}
}

View File

@@ -1,20 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
class ArticlePriceGeneric extends Model
{
use BelongsToThrough;
protected $guarded = ['id'];
protected $table = 'shop_article_price_generics';
public function article_prices()
{
return $this->hasMany('App\Models\Shop\ArticlePrice');
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
protected $guarded = ['id'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function InventoryPlace()
{
return $this->belongsTo('App\Models\Shop\InventoryPlace');
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class InventoryPlace extends Model
{
protected $guarded = ['id'];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function Inventories()
{
return $this->hasMany('App\Models\Inventory');
}
}

40
app/Models/Shop/Price.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class Price extends Model
{
use HasRelationships;
protected $guarded = ['id'];
protected $table = 'shop_prices';
public function article()
{
return $this->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);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class PriceFamily extends Model
{
use HasRelationships;
protected $guarded = ['id'];
protected $table = 'shop_price_families';
public function prices()
{
return $this->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');
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class PriceFamilyValue extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_price_familiy_values';
public function price_family()
{
return $this->belongsTo('App\Models\Shop\PriceFamily');
}
public function scopeByFamily($query, $id)
{
return $query->where('price_family_id');
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
class PriceGeneric extends Model
{
use BelongsToThrough;
protected $guarded = ['id'];
protected $table = 'shop_price_generics';
public function prices()
{
return $this->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');
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
class PriceGenericValue extends Model
{
use BelongsToThrough;
protected $guarded = ['id'];
protected $table = 'shop_price_generic_values';
public function price_generic()
{
return $this->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();
}
}

View File

@@ -1,55 +0,0 @@
<?php
namespace App\Repositories\Shop;
use Yajra\DataTables\DataTables;
use App\Models\Shop\ArticleAttributeFamily;
class ArticleAttributeFamilies
{
public static function getDatatable()
{
$model = ArticleAttributeFamily::orderBy('name');
return Datatables::of($model)->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);
}
}

View File

@@ -1,78 +0,0 @@
<?php
namespace App\Repositories\Shop;
use Yajra\DataTables\DataTables;
use App\Models\Shop\ArticleAttribute;
class ArticleAttributes
{
public static function getDatatable()
{
$model = ArticleAttribute::orderBy('name');
return Datatables::of($model)->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);
}
}

View File

@@ -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)

View File

@@ -4,25 +4,30 @@ 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)
@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Repositories\Shop;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Yajra\DataTables\DataTables;
use App\Models\Shop\PriceGenericValue;
class PriceGenericValues
{
public static function getByPriceGeneric($id)
{
return PriceGenericValue::byPriceGeneric($id)->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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace App\Repositories\Shop;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Yajra\DataTables\DataTables;
use App\Models\Shop\Article;
use App\Models\Shop\Price;
class Prices
{
public static function getByArticle($id)
{
$prices = Article::byArticle($id)->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);
}
}