Add categories for generic prices

This commit is contained in:
Ludovic CANDELLIER
2020-08-31 00:44:29 +02:00
parent 3b6108b449
commit c9198de890
26 changed files with 471 additions and 242 deletions

View File

@@ -1,30 +0,0 @@
<?php
namespace App\DataTables\Shop;
use Yajra\DataTables\Html\Column;
use App\DataTables\ParentDataTable as DataTable;
use App\Models\Shop\ArticlePriceGeneric;
class ArticlePriceGenericsDataTable extends DataTable
{
public $model_name = 'article-price-generics';
public function query(ArticlePriceGeneric $model)
{
$model = $model::withCount('article_prices');
return self::buildQuery($model);
}
protected function getColumns()
{
return [
Column::make('name')->title('Nom'),
Column::make('price')->title('Prix HT')->class('text-right'),
Column::make('price_taxed')->title('Prix TTC')->class('text-right'),
Column::make('article_prices_count')->title('Nb de tarifs')->class('text-right'),
self::makeColumnButtons(),
];
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\DataTables\Shop;
use Yajra\DataTables\Html\Column;
use App\DataTables\ParentDataTable as DataTable;
use App\Models\Shop\PriceGeneric;
class PriceGenericsDataTable extends DataTable
{
public $model_name = 'price-generics';
public function query(PriceGeneric $model)
{
$model = $model::with(['category','priceByUnit'])->withCount('prices');
return self::buildQuery($model);
}
protected function getColumns()
{
return [
Column::make('category.name')->title('Catégorie'),
Column::make('name')->title('Nom'),
Column::make('price_by_unit.price')->title('Prix HT')->class('text-right'),
Column::make('price_by_unit.price_taxed')->title('Prix TTC')->class('text-right'),
Column::make('prices_count')->title('Nb articles')->class('text-right'),
self::makeColumnButtons(),
];
}
}

View File

@@ -1,59 +0,0 @@
<?php
namespace App\Http\Controllers\Shop\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\ArticlePriceGeneric;
use App\Repositories\Shop\ArticlePriceGenerics;
use App\Repositories\Shop\Taxes;
use App\DataTables\Shop\ArticlePriceGenericsDataTable;
class ArticlePriceGenericController extends Controller
{
public function index(ArticlePriceGenericsDataTable $dataTable)
{
return $dataTable->render('Shop.Admin.ArticlePriceGenerics.list');
}
public function getDatatable(Request $request)
{
return ArticlePriceGenerics::getTables($request->all());
}
public function create()
{
$data['taxes'] = Taxes::getOptions();
return view('Shop.Admin.ArticlePriceGenerics.create',$data);
}
public function store(Request $request)
{
$ret = ArticlePriceGenerics::store($request->all());
return redirect()->route('Shop.Admin.ArticlePriceGenerics.index');
}
public function show($id)
{
$data = ArticlePriceGenerics::get($id);
return view('Shop.Admin.ArticlePriceGenerics.view', $data);
}
public function edit($id)
{
$data['generic'] = ArticlePriceGenerics::get($id);
$data['taxes'] = Taxes::getOptions();
return view('Shop.Admin.ArticlePriceGenerics.edit', $data);
}
public function update(Request $request)
{
//
}
public function destroy($id)
{
return ArticlePriceGenerics::destroy($id);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Http\Controllers\Shop\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\PriceGeneric;
use App\Repositories\Shop\PriceGenerics;
use App\Repositories\Shop\PriceGenericCategories;
use App\Repositories\Shop\Taxes;
use App\DataTables\Shop\PriceGenericsDataTable;
class PriceGenericController extends Controller
{
public function index(PriceGenericsDataTable $dataTable)
{
return $dataTable->render('Shop.Admin.PriceGenerics.list');
}
public function getDatatable(Request $request)
{
return PriceGenerics::getTables($request->all());
}
public function create()
{
$data['taxes'] = Taxes::getOptions();
$data['categories'] = PriceGenericCategories::getOptions();
return view('Shop.Admin.PriceGenerics.create',$data);
}
public function edit($id)
{
$data['generic'] = PriceGenerics::get($id)->toArray();
$data['taxes'] = Taxes::getOptions();
$data['categories'] = PriceGenericCategories::getOptions();
return view('Shop.Admin.PriceGenerics.edit', $data);
}
public function store(Request $request)
{
$ret = PriceGenerics::store($request->all());
return redirect()->route('Shop.Admin.PriceGenerics.index');
}
public function show($id)
{
$data = PriceGenerics::get($id);
return view('Shop.Admin.PriceGenerics.view', $data);
}
public function destroy($id)
{
return PriceGenerics::destroy($id);
}
}

View File

@@ -34,8 +34,8 @@ class Shop
$menu->addTo('shop', 'Stock', [ 'route' => 'Shop.Admin.ArticleAttributeValues.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Shop.Admin.ArticleAttributeValues.*'])->order(9);
*/
$menu->addTo('shop', 'Prix génériques', [ 'route' => 'Shop.Admin.ArticlePriceGenerics.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Shop.Admin.ArticlePriceGenerics.*'])->order(10);
$menu->addTo('shop', 'Prix génériques', [ 'route' => 'Shop.Admin.PriceGenerics.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Shop.Admin.PriceGenerics.*'])->order(10);
}

View File

@@ -12,9 +12,14 @@ class PriceGeneric extends Model
protected $guarded = ['id'];
protected $table = 'shop_price_generics';
public function category()
{
return $this->hasOne('App\Models\Shop\PriceGenericCategory','id','category_id');
}
public function prices()
{
return $this->hasMany('App\Models\Shop\Price');
return $this->hasMany('App\Models\Shop\Price','price_id')->where('price_type','App\Models\Shop\PriceGeneric');
}
public function values()
@@ -22,9 +27,19 @@ class PriceGeneric extends Model
return $this->hasMany('App\Models\Shop\PriceGenericValue');
}
public function priceByUnit()
{
return $this->hasOne('App\Models\Shop\PriceGenericValue')->orderBy('quantity','asc');
}
public function articles()
{
return $this->hasManyThrough('App\Models\Shop\Article','App\Models\Shop\Price');
}
public function scopeByCategory($query, $id)
{
return $query->where('category_id', $id);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class PriceGenericCategory extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_price_generic_categories';
public function price_generics()
{
return $this->hasMany('App\Models\Shop\PriceGeneric','category_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\PriceGenericCategory;
class PriceGenericCategories
{
public static function getDatatable()
{
$model = PriceGenericCategory::orderBy('name');
return Datatables::of($model)->make(true);
}
public static function getAll()
{
return PriceGenericCategory::orderBy('name','asc')->get();
}
public static function get($id)
{
return PriceGenericCategory::find($id);
}
public static function getOptions()
{
return PriceGenericCategory::get()->pluck('name','id')->toArray();
}
public static function store($data)
{
$id = isset($data['id']) ? $data['id'] : false;
$category = $id ? self::update($data) : self::create($data);
return $category->id;
}
public static function create($data)
{
return PriceGenericCategory::create($data);
}
public static function update($data, $id = false)
{
$id = isset($data['id']) ? $data['id'] : false;
$category = PriceGenericCategory::find($id);
$category->update($data);
return $category;
}
public static function destroy($id)
{
return PriceGenericCategory::destroy($id);
}
}

View File

@@ -33,6 +33,15 @@ class PriceGenericValues
return PriceGenericValue::find($id);
}
public static function storePrices($generic_id, $values)
{
foreach ($values as $value)
{
$value['price_generic_id'] = $generic_id;
self::store($value);
}
}
public static function store($data)
{
$id = isset($data['id']) ? $data['id'] : false;

View File

@@ -36,14 +36,17 @@ class PriceGenerics
public static function get($id)
{
return PriceGeneric::find($id);
return PriceGeneric::with("values")->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;
$prices = isset($data['prices']) ? $data['prices'] : false;
unset($data['prices']);
$generic = $id ? self::update($data) : self::create($data);
PriceGenericValues::storePrices($generic->id, $prices);
return $generic->id;
}
public static function create($data)
@@ -54,9 +57,9 @@ class PriceGenerics
public static function update($data, $id = false)
{
$id = isset($data['id']) ? $data['id'] : false;
$article = PriceGeneric::find($id);
$article->update($data);
return $article;
$generic = PriceGeneric::find($id);
$generic->update($data);
return $generic;
}
public static function destroy($id)