ldc-jeusselin/app/Http/Controllers/Bo/ObjectivesController.php

177 lines
5.8 KiB
PHP
Raw Normal View History

2021-03-12 14:42:15 +01:00
<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Models\Objectives;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
2021-05-17 12:22:53 +02:00
use Illuminate\Support\Facades\Session;
2021-03-12 14:42:15 +01:00
class ObjectivesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$filter_tc_id = $request->query('filter_tc_id');
2021-05-26 11:59:45 +02:00
2021-05-17 12:22:53 +02:00
$objectives = $this->filterObjectives($request);
$objectives->with(['customer', 'status']);
$objectives = $this->paginate($objectives);
2021-05-17 12:22:53 +02:00
return view('bo.objectives.index', compact('objectives', 'filter_tc_id'));
2021-05-17 12:22:53 +02:00
}
protected function filterObjectives(Request $request)
{
2021-05-26 11:59:45 +02:00
2021-05-17 12:22:53 +02:00
$objectives = Objectives::query();
2021-05-26 11:59:45 +02:00
if ($request->unfilter) {
Session::remove('objectives.filter_tc_id');
}
2021-05-17 12:22:53 +02:00
if ($request->rst) {
Session::remove('objectives.sort_col');
Session::remove('objectives.sort_way');
}
if ($request->sort_col) {
Session::put('objectives.sort_col', $request->sort_col);
Session::save();
}
if ($request->sort_way) {
Session::put('objectives.sort_way', $request->sort_way);
Session::save();
}
if ($request->filter_tc_id) {
2021-05-26 11:59:45 +02:00
Session::put('objectives.filter_tc_id', $request->filter_tc_id);
Session::save();
}
2021-05-17 12:22:53 +02:00
if (Session::has('objectives.sort_col') && Session::has('objectives.sort_way')) {
2021-05-17 12:22:53 +02:00
switch (Session::get('objectives.sort_col')) {
case 'produit':
$objectives = $objectives->orderBy('produit', Session::get('objectives.sort_way'));
break;
case 'qualite':
$objectives = $objectives->orderBy('qualite', Session::get('objectives.sort_way'));
break;
case 'campagne':
$objectives = $objectives->orderBy('campagne', Session::get('objectives.sort_way'));
break;
case 'quantite':
$objectives = $objectives->orderBy('quantite', Session::get('objectives.sort_way'));
break;
case 'prix':
$objectives = $objectives->orderBy('prix', Session::get('objectives.sort_way'));
break;
case 'echeance':
$objectives = $objectives->orderBy('echeance', Session::get('objectives.sort_way'));
break;
case 'status_id':
$objectives = $objectives->orderBy('status_id', Session::get('objectives.sort_way'));
break;
case 'customer_id':
$objectives = $objectives->orderBy('customer_id', Session::get('objectives.sort_way'));
2021-05-17 12:22:53 +02:00
break;
case 'tc_id':
$objectives = $objectives->select('objectives.*');
$objectives = $objectives->leftJoin('customers', 'customers.id', '=', 'objectives.customer_id');
$objectives = $objectives->orderBy('customers.tc_id', Session::get('objectives.sort_way'));
break;
}//end switch
2021-05-26 11:59:45 +02:00
if (Session::has('objectives.filter_tc_id')) {
if (
Session::has('objectives.sort_col')
&& Session::has('objectives.sort_way')
&& Session::get('objectives.sort_col') == 'tc_id'
) {
$objectives = $objectives->where('customers.tc_id', '=', Session::get('objectives.filter_tc_id'));
} else {
$objectives = $objectives->select('objectives.*');
$objectives = $objectives->leftJoin('customers', 'customers.id', '=', 'objectives.customer_id');
$objectives = $objectives->where('customers.tc_id', '=', Session::get('objectives.filter_tc_id'));
}
2021-05-26 11:59:45 +02:00
}
}//end if
2021-05-17 12:22:53 +02:00
return $objectives;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('bo.objectives.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
return redirect()->route('bo.objectives.index');
}
/**
* Display the specified resource.
*
* @param integer $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
2021-05-17 12:22:53 +02:00
$objectif = Objectives::findOrFail($id);
$objectif->load(['customer', 'status']);
2021-05-17 12:22:53 +02:00
return view('bo.objectives.show', compact('objectif'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param integer $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
return redirect()->route('bo.objectives.index');
}
/**
* Remove the specified resource from storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request)
{
$deleteOk = 0;
if ($request->id !== null) {
$objectif = Objectives::findOrFail($request->id);
if ($objectif->customer_id == Auth::guard('frontend')->user()->id) {
$objectif->delete();
} else {
$deleteOk = 3;
}
} else {
$deleteOk = 2;
}
return $deleteOk;
}
2021-03-12 14:42:15 +01:00
}