ldc-legaulois/app/Http/Controllers/Bo/TraceabilityController.php

78 lines
2.3 KiB
PHP
Raw Normal View History

2020-10-05 14:59:44 +02:00
<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Traceability;
use Carbon\Carbon;
class TraceabilityController extends Controller
{
2021-03-20 14:38:15 +01:00
2021-03-21 22:51:19 +01:00
private $responseType = [
1 => "Produit trouvé",
2 => "Produit non trouvé",
3 => "Produit élaboré",
4 => "Eleveur non trouvé",
5 => "CP Eleveur manquant"
];
2020-10-05 14:59:44 +02:00
2021-03-20 14:38:15 +01:00
public function index()
{
$t = Traceability::orderBy('created_at', 'DESC')->paginate(40);
2020-10-05 14:59:44 +02:00
$c = $this->countRequest();
$dateDebut = null;
$dateFin = null;
2021-03-21 22:51:19 +01:00
return view(
'bo.traceability.index',
compact('t', 'c', 'dateDebut', 'dateFin')
)->with('responseType', $this->responseType);
2020-10-05 14:59:44 +02:00
}
2021-03-20 14:38:15 +01:00
public function filter(Request $request)
{
2020-10-05 14:59:44 +02:00
$dateDebut = $request->date_debut;
$dateFin = $request->date_fin;
$cDateDebut = null;
$cDateFin = null;
2021-03-20 14:38:15 +01:00
if ($dateDebut !== null) {
2020-10-05 14:59:44 +02:00
$cDateDebut = Carbon::createFromFormat('Y-m-d', $dateDebut);
2021-03-20 14:38:15 +01:00
}
if ($dateFin !== null) {
2020-10-05 14:59:44 +02:00
$cDateFin = Carbon::createFromFormat('Y-m-d', $dateFin);
2021-03-20 14:38:15 +01:00
}
2020-10-05 14:59:44 +02:00
if ($dateDebut !== null && $dateFin !== null) {
$t = Traceability::whereBetween('created_at', [$cDateDebut, $cDateFin])->paginate(40);
} elseif ($dateDebut !== null) {
$t = Traceability::whereDate('created_at', '>=', $cDateDebut)->paginate(40);
} elseif ($dateFin !== null) {
$t = Traceability::whereDate('created_at', '<=', $cDateFin)->paginate(40);
} else {
$t = Traceability::all();
}
$c = $this->countRequest();
2021-03-21 22:51:19 +01:00
return view(
'bo.traceability.index',
compact('t', 'c', 'dateDebut', 'dateFin')
)->with('responseType', $this->responseType);
2020-10-05 14:59:44 +02:00
}
2021-03-20 14:38:15 +01:00
public function countRequest()
{
2020-10-05 14:59:44 +02:00
$counts = [];
$counts[1] = Traceability::where('response_type', 1)->count();
$counts[2] = Traceability::where('response_type', 2)->count();
$counts[3] = Traceability::where('response_type', 3)->count();
$counts[4] = Traceability::where('response_type', 4)->count();
$counts[5] = Traceability::where('response_type', 5)->count();
return $counts;
}
}