Files
opensem/app/Repositories/Shop/ArticleNatures.php

115 lines
2.7 KiB
PHP

<?php
namespace App\Repositories\Shop;
use App\Models\Shop\ArticleNature;
use App\Models\Shop\Article;
use App\Models\Botanic\Specie;
use App\Models\Botanic\Variety;
use App\Models\Shop\Merchandise;
class ArticleNatures
{
public static function getOptions()
{
return ArticleNature::get()->pluck('name', 'id')->toArray();
}
public static function getProductType($id)
{
$type = self::get($id)->product_type ?? false;
return $type ? self::getProductTypes()[$type] : false;
}
public static function getProductTypeName($type)
{
return self::getProductTypes()[$type] ?? false;
}
public static function getProductTypeByModel($model)
{
switch ($model) {
case Specie::class:
$type = 1;
break;
case Variety::class:
$type = 1;
break;
case Merchandise::class:
$type = 2;
break;
}
return $type ?? false;
}
public static function getProductTypes()
{
return ['', 'botanic', 'merchandise'];
}
public static function getOptionsByBotanic()
{
return self::getOptionsByProductType(1);
}
public static function getOptionsByMerchandise()
{
return self::getOptionsByProductType(2);
}
public static function getOptionsByProductTypeModel($model)
{
$type = self::getProductTypeByModel($model);
return self::getOptionsByProductType($type);
}
public static function getOptionsByProductType($type)
{
return self::getByProductType($type)->pluck('name', 'id')->toArray();
}
public static function getByProductType($type)
{
return ArticleNature::byProductType($type)->get();
}
public static function getByCategory($category_id)
{
return Article::byCategory($category_id)->select('article_nature_id')->distinct()->get();
}
public static function getAll()
{
return ArticleNature::orderBy('name', 'asc')->get();
}
public static function get($id)
{
return ArticleNature::find($id);
}
public static function store($data)
{
$item = ($data['id'] ?? false) ? self::update($data) : self::create($data);
return $item->id;
}
public static function create($data)
{
return ArticleNature::create($data);
}
public static function update($data, $id = false)
{
$id = $id ? $id : $data['id'];
$item = self::get($id);
$ret = $item->update($data);
return $item;
}
public static function destroy($id)
{
return ArticleNature::destroy($id);
}
}