150 lines
4.3 KiB
PHP
150 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\DiscountType;
|
|
use App\Models\Promotion;
|
|
use App\Models\Product;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PromotionsController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$promotions = Promotion::all();
|
|
return view('bo.promotions.index', compact('promotions'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$discTypes = DiscountType::all();
|
|
|
|
$existingProds = Product::all();
|
|
$promotion = new Promotion();
|
|
$selectedProds = $promotion->products;
|
|
// $existingProds = $existingProds->diff($selectedProds);
|
|
return view('bo.promotions.create', compact('discTypes', 'existingProds', 'selectedProds'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
|
|
$request->validate([
|
|
'product_id' => 'required',
|
|
]);
|
|
|
|
$productIds = explode(",", $request->product_id);
|
|
$discType = $request->discount_type_id;
|
|
$value = $request->discount_value;
|
|
$date_start = $request->date_start_date . " " . $request->date_start_time;
|
|
$date_end = $request->date_end_date . " " . $request->date_end_time;
|
|
|
|
$promotion = new Promotion();
|
|
$promotion->discount_type_id = $discType;
|
|
$promotion->discount_value = $value;
|
|
$promotion->date_start = $date_start;
|
|
$promotion->date_end = $date_end;
|
|
$promotion->save();
|
|
|
|
// prise en charge de la liaison avec les produits
|
|
$promotion->products()->sync($productIds);
|
|
|
|
return redirect()->route('bo.promotions.index');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$promotion = Promotion::findOrFail($id);
|
|
$discTypes = DiscountType::all();
|
|
|
|
$existingProds = Product::all();
|
|
$selectedProds = $promotion->products;
|
|
$existingProds = $existingProds->diff($selectedProds);
|
|
|
|
return view('bo.promotions.edit', compact('promotion', 'discTypes', 'existingProds', 'selectedProds'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$promotion = Promotion::findOrFail($id);
|
|
|
|
$request->validate([
|
|
'product_id' => 'required',
|
|
]);
|
|
|
|
$productIds = explode(",", $request->product_id);
|
|
$discType = $request->discount_type_id;
|
|
$value = $request->discount_value;
|
|
$date_start = $request->date_start_date . " " . $request->date_start_time;
|
|
$date_end = $request->date_end_date . " " . $request->date_end_time;
|
|
|
|
$promotion->discount_type_id = $discType;
|
|
$promotion->discount_value = $value;
|
|
$promotion->date_start = $date_start;
|
|
$promotion->date_end = $date_end;
|
|
$promotion->save();
|
|
|
|
// prise en charge de la liaison avec les produits
|
|
$promotion->products()->sync($productIds);
|
|
|
|
return redirect()->route('bo.promotions.edit', $promotion->id)->with('success', 'Modifications enregistrés !');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$p = Promotion::findOrFail($id);
|
|
|
|
// prise en charge de la liaison avec les produits
|
|
$p->products()->detach();
|
|
|
|
$p->delete();
|
|
|
|
return redirect()->route('bo.promotions.index');
|
|
}
|
|
}
|