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)

View File

@@ -1,28 +0,0 @@
@extends('layout.index', [
'title' => __('article_price_generics.title'),
'subtitle' => __('article_price_generics.create.title'),
'breadcrumb' => [__('article_price_generics.title'), __('article_price_generics.create.title')]
])
@include('boilerplate::load.fileinput')
@section('content')
{{ Form::open(['route' => 'Shop.Admin.ArticlePriceGenerics.store', 'id' => 'article-price-generic-form', 'autocomplete' => 'off', 'files' => true]) }}
<div class="row">
<div class="col-sm-12 mbl">
<a href="{{ route("Shop.Admin.ArticlePriceGenerics.index") }}" class="btn btn-default">
{{ __('article_price_generics.list.title') }}
</a>
<span class="btn-group pull-right">
@include('components.button-save')
</span>
</div>
</div>
@include('Shop.Admin.ArticlePriceGenerics.form')
</form>
@endsection

View File

@@ -1,14 +0,0 @@
@extends('layout.index', [
'title' => __('article_price_generics.title'),
'subtitle' => __('article_price_generics.edit.title'),
'breadcrumb' => [__('article_price_generics.title'), __('article_price_generics.edit.title')]
])
@section('content')
{{ Form::open(['route' => 'Shop.Admin.ArticlePriceGenerics.update', 'id' => 'article-price-generic-form', 'autocomplete' => 'off', 'files' => true]) }}
<input type="hidden" name="id" value="{{ $generic['id'] }}">
@include('Shop.Admin.ArticlePriceGenerics.form')
</form>
@endsection

View File

@@ -1,64 +0,0 @@
<div class="row">
<div class="col-md-4">
{{ Form::label('name', 'Nom') }}
@include('components.input', ['name' => 'name', 'value' => $generic['name'] ?? null, 'required' => true])
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="row">
<div class="col-4">
{{ Form::label('tax_id', 'TVA') }}
@include('components.select', ['name' => 'tax_id', 'value' => $generic['tax_id'] ?? null, 'list' => $taxes ?? null, 'class' => 'tva', 'required' => true])
</div>
<div class="col-4">
{{ Form::label('price', 'Prix HT') }}
@include('components.input', ['name' => 'price', 'value' => $generic['price'] ?? null, 'class' => 'price', 'required' => true])
</div>
<div class="col-4">
{{ Form::label('price_taxed', 'Prix TTC') }}
@include('components.input', ['name' => 'price_taxed', 'value' => $generic['price_taxed'] ?? null, 'class' => 'price_taxed', 'required' => true])
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="float-right mt-3">
@include('components.button-save')
</div>
</div>
</div>
@push('js')
<script>
function set_price(item) {
$row = $(item).parent();
tva = $row.find('.tva').val();
price_taxed = $row.find('.price_taxed').val();
console.log(tva);
console.log(price_taxed);
}
function set_price_taxed(item) {
$row = $(item).parent();
tva = $row.find('.tva').val();
price = $row.find('.price').val();
console.log(tva);
console.log(price);
}
$('.price_taxed').change(function() {
set_price(this);
});
$('.price').change(function() {
set_price_taxed(this);
});
</script>
@endpush

View File

@@ -1,8 +0,0 @@
@extends('layout.index', [
'title' => __('article_price_generics.title'),
'subtitle' => __('article_price_generics.list.title'),
'breadcrumb' => [__('article_price_generics.title')]
])
@section('content')
@include('components.datatable', ['route' => route('Shop.Admin.ArticlePriceGenerics.index'), 'model' => 'article-price-generics'])
@endsection

View File

@@ -0,0 +1,26 @@
@extends('layout.index', [
'title' => __('price_generics.title'),
'subtitle' => __('price_generics.create.title'),
'breadcrumb' => [__('price_generics.title'), __('price_generics.create.title')]
])
@section('content')
{{ Form::open(['route' => 'Shop.Admin.PriceGenerics.store', 'id' => 'price-generic-form', 'autocomplete' => 'off']) }}
<div class="row">
<div class="col-sm-12 mbl">
<a href="{{ route("Shop.Admin.PriceGenerics.index") }}" class="btn btn-default">
{{ __('price_generics.list.title') }}
</a>
<span class="btn-group pull-right">
@include('components.button-save')
</span>
</div>
</div>
@include('Shop.Admin.PriceGenerics.form')
</form>
@endsection

View File

@@ -0,0 +1,14 @@
@extends('layout.index', [
'title' => __('price_generics.title'),
'subtitle' => __('price_generics.edit.title'),
'breadcrumb' => [__('price_generics.title'), __('price_generics.edit.title')]
])
@section('content')
{{ Form::open(['route' => 'Shop.Admin.PriceGenerics.store', 'id' => 'price-generic-form', 'autocomplete' => 'off']) }}
<input type="hidden" name="id" value="{{ $generic['id'] }}">
@include('Shop.Admin.PriceGenerics.form')
</form>
@endsection

View File

@@ -0,0 +1,23 @@
<div class="row">
<div class="col-md-6">
{{ Form::label('name', 'Catégorie') }}
@include('components.select', ['name' => 'category_id', 'list' => $categories, 'value' => $generic['category_id'] ?? null, 'required' => true])
</div>
</div>
<div class="row mb-3">
<div class="col-md-6">
{{ Form::label('name', 'Nom') }}
@include('components.input', ['name' => 'name', 'value' => $generic['name'] ?? null, 'required' => true])
</div>
</div>
@include('Shop.Admin.PriceGenerics.partials.prices', ['prices' => $generic['values'] ?? null ])
<div class="row">
<div class="col-md-8">
<div class="float-right mt-3">
@include('components.button-save')
</div>
</div>
</div>

View File

@@ -0,0 +1,8 @@
@extends('layout.index', [
'title' => __('price_generics.title'),
'subtitle' => __('price_generics.list.title'),
'breadcrumb' => [__('price_generics.title')]
])
@section('content')
@include('components.datatable', ['route' => route('Shop.Admin.PriceGenerics.index'), 'model' => 'price-generics'])
@endsection

View File

@@ -0,0 +1,42 @@
<div class="col-6 row-price">
<input type="hidden" name="prices[{{ $key }}][id]" value="@if (isset($price['id'])){{ $price['id'] }}@endif" class="price_id">
<div class="card card-light">
<div class="card-body pt-2">
<div class="row">
<div class="col-2">
{{ Form::label('quantity', 'Qté.') }}<br/>
@include('components.number', ['name' => "prices[$key][quantity]", 'value' => $price['quantity'] ?? 1, 'required' => true, 'class' => 'form-control-sm'])
</div>
<div class="col-3">
{{ Form::label('tax_id', 'TVA') }}<br/>
@include('components.select', ['name' => "prices[$key][tax_id]", 'value' => $price['tax_id'] ?? null, 'list' => $taxes ?? null, 'required' => true, 'class' => 'form-control-sm'])
</div>
<div class="col-3">
{{ Form::label('price', 'Prix HT') }}
@include('components.money', ['name' => "prices[$key][price]", 'value' => $price['price'] ?? 0, 'required' => true, 'class' => 'form-control-sm price-item'])
</div>
<div class="col-3">
{{ Form::label('price_taxed', 'Prix TTC') }}
@include('components.money', ['name' => "prices[$key][price_taxed]", 'value' => $price['price_taxed'] ?? 0, 'required' => true, 'class' => 'form-control-sm price-taxed-item'])
</div>
<div class="col-1 text-right">
<br/>
<button type="button" class="btn btn-xs btn-danger delete-price-btn mt-2" data-card-widget="collapse" data-toggle="tooltip" title="supprimer">
<i class="fas fa-trash"></i>
</button>
</div>
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,43 @@
<div class="col-6 row-new-price row-price">
<input type="hidden" name="prices[][id]" value="">
<div class="card card-light">
<div class="card-body pt-2">
<div class="row">
<div class="col-2">
{{ Form::label('quantity', 'Quantité') }}<br/>
@include('components.number', ['name' => 'prices[0][quantity]', 'value' => $quantity ?? 1, 'required' => true, 'class' => 'form-control-sm'])
</div>
<div class="col-3">
{{ Form::label('tax_id', 'TVA') }}<br/>
@include('components.select', ['name' => 'prices[0][tax_id]', 'value' => $tax_id ?? null, 'list' => $taxes ?? null, 'required' => true, 'class' => 'form-control-sm'])
</div>
<div class="col-3">
{{ Form::label('price', 'Prix HT') }}
@include('components.money', ['name' => 'prices[0][price]', 'value' => $price ?? 0, 'required' => true, 'class' => 'form-control-sm price-item'])
</div>
<div class="col-3">
{{ Form::label('price_taxed', 'Prix TTC') }}
@include('components.money', ['name' => 'prices[0][price_taxed]', 'value' => $price_taxed ?? 0, 'required' => true, 'class' => 'form-control-sm price-taxed-item'])
</div>
<div class="col-1 text-right">
<br/>
<button type="button" class="btn btn-xs btn-danger delete-new-price-btn mt-2" data-card-widget="collapse" data-toggle="tooltip" title="supprimer">
<i class="fas fa-trash"></i>
</button>
</div>
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,16 @@
@if (isset($prices) && (count($prices)))
@for ($i = 0; $i < count($prices); $i++)
@include('Shop.Admin.PriceGenerics.partials.block_price', ['key' => $i, 'price' => $prices[$i]])
@endfor
@endif
@push('js')
<script>
$(function () {
handle_delete_price();
handle_prices();
handle_prices_taxed();
});
</script>
@endpush

View File

@@ -0,0 +1,67 @@
@include('Shop.Admin.PriceGenerics.partials.block_price_new')
<div id="append_price" class="row">
@include('Shop.Admin.PriceGenerics.partials.list-prices')
</div>
<button type="button" class="btn btn-sm btn-primary add-new-price">Ajouter un tarif <i class="fa fa-plus"></i></button>
@push('js')
<script>
function append_price() {
handle_prices();
handle_prices_taxed();
// handle_append_attribute();
// $('.select2').select2();
}
$("#append_price").appender({
rowSection: '.row-new-price',
type: '.row-price',
addBtn: '.add-new-price',
appendEffect: 'slide',
addClass: 'animated bounceInLeft',
rowNumber: '.row-price-number',
deleteBtn: '.delete-new-price-btn',
callback: append_price,
rowNumberStart: 2,
hideSection: true
});
function handle_delete_price() {
$('.delete-price-btn').click(function() {
var $selector = $(this).parents('.row-price');
var id = $selector.find('.price_id').val();
confirm_delete(id, laroute.route('Shop.Admin.PriceGenericValues.destroy', {id : id}), function() {
$selector.remove();
});
});
}
function handle_prices() {
$('.price-item').change(function() {
$col_tax = $(this).parent().prev();
tax_selected = $col_tax.find('select option:selected').text();
price_taxed = $(this).val() * (1 + (tax_selected / 100));
price_taxed = price_taxed.toFixed(2);
$(this).parent().parent().find('.price-taxed-item').val(price_taxed);
})
}
function handle_prices_taxed() {
$('.price-taxed-item').change(function() {
$col_tax = $(this).parent().prev().prev();
tax_selected = $col_tax.find('select option:selected').text();
price = $(this).val() / (1 + (tax_selected / 100));
price = price.toFixed(2);
$(this).parent().parent().find('.price-item').val(price);
})
}
</script>
@endpush

View File

@@ -1,12 +0,0 @@
<?php
Route::prefix('ArticlePriceGenerics')->name('ArticlePriceGenerics.')->group(function () {
Route::get('', 'ArticlePriceGenericController@index')->name('index');
Route::get('create', 'ArticlePriceGenericController@create')->name('create');
Route::delete('destroy', 'ArticlePriceGenericController@destroy')->name('destroy');
Route::post('update', 'ArticlePriceGenericController@update')->name('update');
Route::post('store', 'ArticlePriceGenericController@store')->name('store');
Route::get('edit/{id}', 'ArticlePriceGenericController@edit')->name('edit');
});

View File

@@ -0,0 +1,12 @@
<?php
Route::prefix('PriceGenerics')->name('PriceGenerics.')->group(function () {
Route::get('', 'PriceGenericController@index')->name('index');
Route::get('create', 'PriceGenericController@create')->name('create');
Route::delete('destroy', 'PriceGenericController@destroy')->name('destroy');
Route::post('update', 'PriceGenericController@update')->name('update');
Route::post('store', 'PriceGenericController@store')->name('store');
Route::get('edit/{id}', 'PriceGenericController@edit')->name('edit');
});

View File

@@ -2,22 +2,21 @@
Route::middleware('auth')->prefix('Admin')->namespace('Admin')->name('Admin.')->group(function () {
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
include( __DIR__ . '/ArticleAttributeFamilies.php');
include( __DIR__ . '/ArticleAttributeValues.php');
include( __DIR__ . '/ArticleAttributes.php');
include( __DIR__ . '/ArticleFamilies.php');
include( __DIR__ . '/ArticlePriceGenerics.php');
include( __DIR__ . '/ArticlePrices.php');
include( __DIR__ . '/Articles.php');
include( __DIR__ . '/Categories.php');
include( __DIR__ . '/Customers.php');
include( __DIR__ . '/Inventories.php');
include( __DIR__ . '/InventoryPlaces.php');
include( __DIR__ . '/InvoiceItems.php');
include( __DIR__ . '/Invoices.php');
include( __DIR__ . '/OrderPayments.php');
include( __DIR__ . '/Orders.php');
include( __DIR__ . '/Tags.php');
include( __DIR__ . '/TagGroups.php');
include __DIR__ . '/ArticleAttributeFamilies.php';
include __DIR__ . '/ArticleAttributeValues.php';
include __DIR__ . '/ArticleAttributes.php';
include __DIR__ . '/ArticleFamilies.php';
include __DIR__ . '/ArticlePrices.php';
include __DIR__ . '/Articles.php';
include __DIR__ . '/Categories.php';
include __DIR__ . '/Customers.php';
include __DIR__ . '/Inventories.php';
include __DIR__ . '/InventoryPlaces.php';
include __DIR__ . '/InvoiceItems.php';
include __DIR__ . '/Invoices.php';
include __DIR__ . '/OrderPayments.php';
include __DIR__ . '/Orders.php';
include __DIR__ . '/PriceGenerics.php';
include __DIR__ . '/Tags.php';
include __DIR__ . '/TagGroups.php';
});