<?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)
    {
        $data['prices'] = Price::byArticle($id)->notGeneric()->get()->toArray();
        $data['generics'] = Price::byArticle($id)->generic()->with(['generic.prices', 'generic.category'])->get()->toArray();
        return $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, $id = false)
    {
        $id = $id ? $id : $data['id'];
        $item = self::get($id);
        $item->update($data);
        return $item;
    }

    public static function destroy($id)
    {
        return Price::destroy($id);
    }
}
