ajout champ TVA dans le BO

This commit is contained in:
Olivier Legat 2021-04-21 11:55:51 +02:00
parent 9af3852583
commit 4eea725335
3 changed files with 112 additions and 81 deletions

View file

@ -8,6 +8,7 @@ use App\Models\FoodProduct;
use App\Models\Product;
use App\Models\Range;
use App\Models\Tag;
use App\Models\TaxRate;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
@ -21,12 +22,12 @@ class ProductsController extends Controller
$ranges = Range::all();
$filter_range = $request->query('filter_range');
$filter_name = $request->query('filter_name');
$filter_name = $request->query('filter_name');
$products = new Product();
if (!empty($filter_name)) {
$products = $products->where('name', 'like', '%'.$filter_name.'%');
$products = $products->where('name', 'like', '%' . $filter_name . '%');
}
if (!empty($filter_range)) {
$products = $products->where('range_id', $filter_range);
@ -35,17 +36,17 @@ class ProductsController extends Controller
//ici récupération des infos additionnelles pour affichage dans la liste
// @todo : parametrer l'occurence de ces affichages car ça n'aura aucun sens selon le type de produit
foreach($products as &$product) {
foreach ($products as &$product) {
$product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type);
if ($product->additionnalProductInfo === null) {
//n'existe pas on renseigne un vide
$product_type = $product->product_type;
$modelClass = 'App\Models\\' . ucfirst($product_type);
$product_type = $product->product_type;
$modelClass = 'App\Models\\' . ucfirst($product_type);
$product->$product_type = new $modelClass();
}
}
unset($product);
return view('bo.products.index', compact('products','ranges','filter_name','filter_range'));
return view('bo.products.index', compact('products', 'ranges', 'filter_name', 'filter_range'));
}
/**
@ -57,11 +58,12 @@ class ProductsController extends Controller
abort(404, "Cette page n'existe pas.");
//return redirect()->route('bo.products.index');
}
$modelClass = 'App\Models\\' . ucfirst($product_type);
$product = new Product();
$modelClass = 'App\Models\\' . ucfirst($product_type);
$product = new Product();
$product->$product_type = new $modelClass();
$ranges = Range::all();
return view('bo.products.create', compact('product_type', 'product', 'ranges'));
$ranges = Range::all();
$taxes = TaxRate::all();
return view('bo.products.create', compact('product_type', 'product', 'ranges', 'taxes'));
}
/**
@ -72,21 +74,22 @@ class ProductsController extends Controller
public function store(Request $request)
{
//products fields
$name = $request->name;
$description = $request->description;
$picture = $request->picture;
$name = $request->name;
$description = $request->description;
$picture = $request->picture;
$picture_large = $request->picture_large;
$price = $request->price;
$priceBuy = $request->price_buy;
$stock = $request->stock;
$reference = $request->reference;
$sell_unit = $request->sell_unit;
$weight = $request->weight;
$weight_unit = $request->weight_unit;
$status = $request->status === null ? 0 : 1;
$star = $request->star === null ? 0 : 1;
$slug = Str::slug($name);
$rangeId = $request->range_id;
$price = $request->price;
$priceBuy = $request->price_buy;
$stock = $request->stock;
$reference = $request->reference;
$sell_unit = $request->sell_unit;
$weight = $request->weight;
$weight_unit = $request->weight_unit;
$status = $request->status === null ? 0 : 1;
$star = $request->star === null ? 0 : 1;
$slug = Str::slug($name);
$rangeId = $request->range_id;
$taxRateId = $request->tax_rate_id;
$request->validate([
'name' => 'required|string|max:255',
@ -110,20 +113,21 @@ class ProductsController extends Controller
"\" éxiste déjà. Veuillez changer le nom du produit.");
}
$product = new Product();
$product->name = $name;
$product = new Product();
$product->name = $name;
$product->description = $description;
$product->price = $price;
$product->price_buy = $priceBuy;
$product->stock = $stock;
$product->reference = $reference;
$product->sell_unit = $sell_unit;
$product->weight = $weight;
$product->price = $price;
$product->price_buy = $priceBuy;
$product->stock = $stock;
$product->reference = $reference;
$product->sell_unit = $sell_unit;
$product->weight = $weight;
$product->weight_unit = $weight_unit;
$product->slug = $slug;
$product->status = $status;
$product->star = $star;
$product->range_id = $rangeId;
$product->slug = $slug;
$product->status = $status;
$product->star = $star;
$product->range_id = $rangeId;
$product->tax_rate_id = $taxRateId;
$product->save();
// Check si le slug existe dans la table old_slugs, auquel cas il est attribué a ce nouveau produit
@ -173,20 +177,21 @@ class ProductsController extends Controller
*/
public function edit($id)
{
$product = Product::findOrFail($id);
$product = Product::findOrFail($id);
$product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type);
//above lines equivalent to :
//$product = Product::with('additionnalProductInfo')->findOrFail($id);
if ($product->additionnalProductInfo === null) {
//n'existe pas on renseigne un vide
$product_type = $product->product_type;
$modelClass = 'App\Models\\' . ucfirst($product_type);
$product_type = $product->product_type;
$modelClass = 'App\Models\\' . ucfirst($product_type);
$product->$product_type = new $modelClass();
}
$ranges = Range::all();
$ranges = Range::all();
$taxes = TaxRate::all();
$existingTags = Tag::all();
$selectedtags = $product->tags;
@ -194,7 +199,7 @@ class ProductsController extends Controller
$selectedProds = $product->products;
$existingProds = $existingProds->diff($selectedProds);
return view('bo.products.edit', compact('product', 'ranges','existingTags', 'selectedtags', 'existingProds', 'selectedProds'));
return view('bo.products.edit', compact('product', 'ranges', 'taxes', 'existingTags', 'selectedtags', 'existingProds', 'selectedProds'));
}
/**
@ -206,22 +211,23 @@ class ProductsController extends Controller
$product = Product::findOrFail($id);
$name = $request->name;
$description = $request->description;
$picture = $request->picture;
$name = $request->name;
$description = $request->description;
$picture = $request->picture;
$picture_large = $request->picture_large;
$price = $request->price;
$priceBuy = $request->price_buy;
$stock = $request->stock;
$reference = $request->reference;
$sell_unit = $request->sell_unit;
$weight = $request->weight;
$weight_unit = $request->weight_unit;
$status = $request->status === null ? 0 : 1;
$star = $request->star === null ? 0 : 1;
$slug = Str::slug($name);
$rangeId = $request->range_id;
$tags = $request->tags;
$price = $request->price;
$priceBuy = $request->price_buy;
$stock = $request->stock;
$reference = $request->reference;
$sell_unit = $request->sell_unit;
$weight = $request->weight;
$weight_unit = $request->weight_unit;
$status = $request->status === null ? 0 : 1;
$star = $request->star === null ? 0 : 1;
$slug = Str::slug($name);
$rangeId = $request->range_id;
$taxRateId = $request->tax_rate_id;
$tags = $request->tags;
$request->validate([
'name' => 'required|string|max:255',
@ -274,25 +280,26 @@ class ProductsController extends Controller
if ($product->slug != $slug) {
$exist = OldSlug::where('slug', $product->slug)->first();
if ($exist === null) {
$oldSlug = new OldSlug();
$oldSlug = new OldSlug();
$oldSlug->product_id = $product->id;
$oldSlug->slug = $product->slug;
$oldSlug->slug = $product->slug;
$oldSlug->save();
}
}
$product->description = $description;
$product->price = $price;
$product->price_buy = $priceBuy;
$product->stock = $stock;
$product->reference = $reference;
$product->sell_unit = $sell_unit;
$product->weight = $weight;
$product->price = $price;
$product->price_buy = $priceBuy;
$product->stock = $stock;
$product->reference = $reference;
$product->sell_unit = $sell_unit;
$product->weight = $weight;
$product->weight_unit = $weight_unit;
$product->slug = $slug;
$product->status = $status;
$product->star = $star;
$product->range_id = $rangeId;
$product->slug = $slug;
$product->status = $status;
$product->star = $star;
$product->range_id = $rangeId;
$product->tax_rate_id = $taxRateId;
$product->save();
// Check si le slug existe dans la table old_slug, auquel cas il est attribué a ce nouveau produit
@ -311,11 +318,11 @@ class ProductsController extends Controller
}
//tags associés
if(!empty($tags)) {
if (!empty($tags)) {
$arTagIds = array();
foreach($tags as $tag) {
foreach ($tags as $tag) {
$oTag = json_decode($tag);
if(empty($oTag->id)) {
if (empty($oTag->id)) {
//we need a new tag
$arTagIds[] = $this->storeTag($oTag);
} else {
@ -327,7 +334,7 @@ class ProductsController extends Controller
}
//produits associés
if(!empty($request->prods)) {
if (!empty($request->prods)) {
$productIds = explode(",", $request->prods);
//prise en charge de la liaison avec les produits
$product->products()->sync($productIds);
@ -343,12 +350,12 @@ class ProductsController extends Controller
public function storeTag($oTag)
{
$slug = Str::slug($oTag->label);
$tag = Tag::where('slug', $slug)->first();
if($tag === null) {
$tag = Tag::where('slug', $slug)->first();
if ($tag === null) {
//si on pas encore ce tag on le crée
$tag = new Tag();
$tag = new Tag();
$tag->label = $oTag->label;
$tag->slug = Str::slug($oTag->label);
$tag->slug = Str::slug($oTag->label);
$tag->save();
}
return $tag->id;
@ -385,8 +392,8 @@ class ProductsController extends Controller
'product_id' => $product_id,
]);
if(!empty($request->no_art)) {
$foodProduct->no_art = $request->no_art;
if (!empty($request->no_art)) {
$foodProduct->no_art = $request->no_art;
}
$foodProduct->ingredients = $request->ingredients;
$foodProduct->preparation = $request->preparation;
@ -428,7 +435,6 @@ class ProductsController extends Controller
$foodProduct->save();
}
/**
@ -481,7 +487,6 @@ class ProductsController extends Controller
$foodProduct->save();
}
/**
@ -497,7 +502,7 @@ class ProductsController extends Controller
*/
public function starred()
{
$products = Product::where('star',1)->paginate(10);
$products = Product::where('star', 1)->paginate(10);
return view('bo.products.starred', compact('products'));
}
}

View file

@ -43,6 +43,17 @@
<label for="price_buy">Prix d'achat en €</label>
<input type="number" id="price_buy" name="price_buy" min="0" step=".01" class="form-control" value="{{ old('price_buy') }}">
</div>
<div class="form-group col">
<label for="tax_rate_id">TVA Applicable</label>
<select class="custom-select" id="tax_rate_id" name="tax_rate_id">
@foreach($taxes as $taxe)
<option value="{{$taxe->id}}">{{$taxe->label}}</option>
@endforeach
</select>
</div>
<div class="form-group col"></div>
</div>
<div class="form-row">
<div class="form-group col">
<label for="stock">Stock</label>
<input type="number" id="stock" name="stock" class="form-control" min="0" value="{{ old('stock') }}">
@ -51,6 +62,8 @@
<label for="reference">Référence</label>
<input type="text" id="reference" name="reference" class="form-control" value="{{ old('reference') }}">
</div>
<div class="form-group col"></div>
<div class="form-group col"></div>
</div>
<div class="form-row">
<div class="form-group col">

View file

@ -67,6 +67,17 @@
<label for="price_buy">Prix d'achat en €</label>
<input type="number" id="price_buy" name="price_buy" min="0" step=".01" class="form-control" value="{{ old('price_buy', $product->price_buy)}}">
</div>
<div class="form-group col">
<label for="tax_rate_id">TVA Applicable</label>
<select class="custom-select" id="tax_rate_id" name="tax_rate_id">
@foreach($taxes as $taxe)
<option value="{{$taxe->id}}" @if ($taxe->id === old('tax_rate_id',$product->tax_rate_id)) selected @endif>{{$taxe->label}}</option>
@endforeach
</select>
</div>
<div class="form-group col"></div>
</div>
<div class="form-row">
<div class="form-group col">
<label for="stock">Stock</label>
<input type="number" id="stock" name="stock" class="form-control" min="0" value="{{ old('stock', $product->stock)}}">
@ -75,6 +86,8 @@
<label for="reference">Référence</label>
<input type="text" id="reference" name="reference" class="form-control" value="{{ old('reference', $product->reference)}}">
</div>
<div class="form-group col"></div>
<div class="form-group col"></div>
</div>
<div class="form-row">
<div class="form-group col">