Files
opensem/app/Repositories/Shop/ArticlePrices.php
Ludovic CANDELLIER 8ec6f24df4 Fix
2021-04-07 22:56:15 +02:00

99 lines
2.2 KiB
PHP

<?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\ArticlePrice;
class ArticlePrices
{
public static function getByArticle($id)
{
return ArticlePrice::byArticle($id)->get();
}
public static function getByArticleWithAttribute($id)
{
return ArticlePrice::with('article_attribute.attribute_value')->byArticle($id)->get();
}
public static function getDatatable()
{
$model = ArticlePrice::orderBy('name');
return Datatables::of($model)->make(true);
}
public static function getAll()
{
return ArticlePrice::orderBy('name','asc')->get();
}
public static function get($id)
{
return ArticlePrice::find($id);
}
public static function storePrices($article_id, $prices)
{
// dump($article_id);
// dump($prices);
// exit;
if ($prices) {
foreach ($prices as $key => $price) {
$price['article_attribute']['article_attribute_value_id'] = $price['attribute']['attribute_value_id'];
$prices[$key]['article_attribute_id'] = ArticleAttributes::storeAttribute($article_id, $price['article_attribute']);
unset($prices[$key]['article_attribute']);
unset($prices[$key]['attribute']);
self::store($prices[$key]);
}
} else {
return false;
}
}
public static function store($data)
{
$attributes = isset($data['attributes']) ? $data['attributes'] : false;
unset($data['attributes']);
$id = isset($data['id']) ? $data['id'] : false;
$price = $id ? self::update($data) : self::create($data);
$ret = $attributes ? self::storeAttributes($price->id, $attributes) : false;
return $price->id;
}
public static function storeAttributes($article_price_id,$attributes)
{
return ArticleAttributes::storeAttribute($article_price_id, $attributes);
}
public static function create($data)
{
return ArticlePrice::create($data);
}
public static function update($data, $id = false)
{
$id = isset($data['id']) ? $data['id'] : false;
$article = ArticlePrice::find($id);
$article->update($data);
return $article;
}
public static function destroy($id)
{
return ArticlePrice::destroy($id);
}
}