189 lines
5.7 KiB
PHP
189 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Magasin;
|
|
use App\Models\CollecteException;
|
|
use Illuminate\Http\Request;
|
|
|
|
class MagasinsController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$magasins = Magasin::all();
|
|
return view('bo.magasins.index', compact('magasins'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('bo.magasins.create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'address' => 'required|string|max:255',
|
|
'postcode' => 'required',
|
|
'city' => 'required|string|max:255',
|
|
]);
|
|
|
|
$shop = Magasin::create($request->except('_token'));
|
|
return redirect()->route('bo.magasins.edit', $shop->id)->with('success', 'Nouveau Magasin enregistré !');
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
$magasin = Magasin::findOrFail($id);
|
|
$exceptionsCollecte = $magasin->collecteExceptions()->orderBy('date')->get();
|
|
return view('bo.magasins.edit', compact('magasin', 'exceptionsCollecte'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'address' => 'required|string|max:255',
|
|
'postcode' => 'required',
|
|
'city' => 'required|string|max:255',
|
|
]);
|
|
|
|
$shop = Magasin::findOrFail($id);
|
|
$shop->fill($request->except('_token'))->save();
|
|
return redirect()->route('bo.magasins.edit', $shop->id)->with('success', 'Modifications enregistrés !');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$shop = Magasin::findOrFail($id);
|
|
|
|
$count = Magasin::count();
|
|
|
|
if ($count === 1) {
|
|
return redirect()->back()->withErrors("Suppression impossible : Vous devez conserver au moins 1 point de retrait.");
|
|
}
|
|
// @todo : controller les crenaux settings liés
|
|
$shop->delete();
|
|
return redirect()->route('bo.magasins.index');
|
|
}
|
|
|
|
/**
|
|
* bo
|
|
* ajx : affichage d'infos sur un magasin
|
|
*/
|
|
public function getMagasinInfo($id, $mode = null)
|
|
{
|
|
$magasin = Magasin::findOrFail($id);
|
|
if (!is_null($mode)) {
|
|
if ($mode == "tippy") {
|
|
return view('bo.magasins.tippy-card', compact('magasin'));
|
|
}
|
|
if ($mode == "json") {
|
|
return $magasin;
|
|
}
|
|
return view('bo.magasins.info-card', compact('magasin'));
|
|
}
|
|
return view('bo.magasins.info-popup', compact('magasin'));
|
|
}
|
|
|
|
public function saveExceptions(Request $request)
|
|
{
|
|
$collecteException = $request->collecte_exception;
|
|
|
|
for ($i = 0; $i < count($collecteException); $i++) {
|
|
$collecteExceptionToInsert = !empty($collecteException[$i]["date"]);
|
|
if (isset($collecteException[$i]["id"]) && $collecteException[$i]["id"] !== null && $collecteExceptionToInsert) {
|
|
$ce = CollecteException::findOrFail($collecteException[$i]["id"]);
|
|
$ce->date = $collecteException[$i]["date"];
|
|
$ce->comment = $collecteException[$i]["comment"];
|
|
$ce->magasin_id = $collecteException[$i]["magasin_id"];
|
|
$ce->save();
|
|
} else {
|
|
if ($collecteExceptionToInsert) {
|
|
$ce = CollecteException::updateOrCreate([
|
|
'date' => $collecteException[$i]["date"],
|
|
'comment' => $collecteException[$i]["comment"],
|
|
'magasin_id' => $collecteException[$i]["magasin_id"]
|
|
]);
|
|
$collecteException[$i]["id"] = $ce->id;
|
|
} else {
|
|
unset($collecteException[$i]);
|
|
$i--;
|
|
}
|
|
}
|
|
}//end for
|
|
|
|
// remet dans l'ordre avant de renvoyer
|
|
// $collecteException = CollecteException::where('magasin_id',)->orderBy('date')->get();
|
|
$this->arraySortByColumn($collecteException, "date");
|
|
return $collecteException;
|
|
}
|
|
|
|
/**
|
|
* helper function
|
|
*/
|
|
public function arraySortByColumn(&$array, $column, $direction = SORT_ASC)
|
|
{
|
|
$reference_array = array();
|
|
|
|
foreach ($array as $key => $row) {
|
|
$reference_array[$key] = $row[$column];
|
|
}
|
|
|
|
array_multisort($reference_array, $direction, $array);
|
|
}
|
|
|
|
public function destroyExceptions(Request $request)
|
|
{
|
|
$collecteException = CollecteException::findOrFail($request->id);
|
|
$collecteException->delete();
|
|
|
|
return "Suppression terminée !";
|
|
}
|
|
}
|