206 lines
6.1 KiB
PHP
206 lines
6.1 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',
|
|
'email' => 'required|email:rfc,dns,spoof|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é !');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(int $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, int $id)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'email' => 'required|email:rfc,dns,spoof|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(int $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.");
|
|
}
|
|
$shop->delete();
|
|
return redirect()->route('bo.magasins.index');
|
|
}
|
|
|
|
/**
|
|
* bo
|
|
* ajx : affichage d'infos sur un magasin
|
|
*
|
|
* @param integer $id
|
|
* @param string|null $mode
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function getMagasinInfo(int $id, string $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'));
|
|
}
|
|
|
|
/**
|
|
* Save collect exceptions
|
|
*
|
|
* @param Request $request
|
|
* @return array
|
|
*/
|
|
public function saveExceptions(Request $request)
|
|
{
|
|
$collecteException = $request->collecte_exception;
|
|
|
|
$looplimit = count($collecteException);
|
|
for ($i = 0; $i < $looplimit; $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.
|
|
$this->arraySortByColumn($collecteException, "date");
|
|
return $collecteException;
|
|
}
|
|
|
|
/**
|
|
* helper function
|
|
*
|
|
* @param array $array
|
|
* @param string $column
|
|
* @param integer $direction
|
|
* @return void
|
|
*/
|
|
public function arraySortByColumn(array &$array, string $column, int $direction = SORT_ASC)
|
|
{
|
|
$reference_array = array();
|
|
|
|
foreach ($array as $key => $row) {
|
|
$reference_array[$key] = $row[$column];
|
|
}
|
|
|
|
array_multisort($reference_array, $direction, $array);
|
|
}
|
|
|
|
/**
|
|
* delete collecte exception
|
|
*
|
|
* @param Request $request
|
|
* @return string
|
|
*/
|
|
public function destroyExceptions(Request $request)
|
|
{
|
|
$collecteException = CollecteException::findOrFail($request->id);
|
|
$collecteException->delete();
|
|
|
|
return "Suppression terminée !";
|
|
}
|
|
}
|