96 lines
3.2 KiB
PHP
96 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\SearchPoultryTraceaRequest;
|
|
use App\Models\TraceaPoultry;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Str;
|
|
|
|
class TraceaPoultryController extends Controller
|
|
{
|
|
/**
|
|
* Show search form
|
|
*
|
|
* @param SearchPoultryTraceaRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function searchSubmit(SearchPoultryTraceaRequest $request)
|
|
{
|
|
/** @var \App\Models\TraceaPoultry|null */
|
|
$traceaPoultry = TraceaPoultry::whereExternalCodeLot($request->codelot)
|
|
->where(function ($q) use ($request) {
|
|
if ($request->eancode[0] === "0") {
|
|
$code = Str::substr($request->eancode, 1);
|
|
$q->where('barcode_price', 'LIKE', Str::limit($code, 6, '') . '%')
|
|
->orWhere('barcode_weight', 'LIKE', Str::limit($code, 6, '') . '%');
|
|
} else {
|
|
$q->where('barcode_price', 'LIKE', Str::limit($request->eancode, 7, '') . '%')
|
|
->orWhere('barcode_weight', 'LIKE', Str::limit($request->eancode, 7, '') . '%');
|
|
}
|
|
})->first();
|
|
|
|
if (!$traceaPoultry) {
|
|
return back()->with(
|
|
'warning',
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
trans('Aucun produit n\'a pu être trouvé avec ces informations. Vérifiez bien les numéros saisis avec les indications comme sur l\'image ci-dessous.')
|
|
);
|
|
}
|
|
|
|
Session::put('poultrySearch', [
|
|
'model' => $traceaPoultry,
|
|
'time' => now()
|
|
]);
|
|
|
|
// * Flash session with age max show result page to 6
|
|
return response()->redirectToRoute('poultry.show');
|
|
}
|
|
|
|
/**
|
|
* Show Traceapoultry result
|
|
*
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function show()
|
|
{
|
|
// * If result too old or not found
|
|
/** @var \App\Models\TraceaPoultry|null */
|
|
if (
|
|
!($traceaPoultry = Session::get('poultrySearch.model')) or
|
|
!\is_object(Session::get('poultrySearch.time')) or
|
|
\get_class(Session::get('poultrySearch.time')) !== Carbon::class
|
|
) {
|
|
Session::forget('poultrySearch');
|
|
\redirect()->route('poultry.search')->throwResponse();
|
|
}
|
|
|
|
$distributionPicture = $this->getDistributionPicture($traceaPoultry->sign);
|
|
|
|
return view('front.poultry.show', compact(
|
|
'traceaPoultry',
|
|
'distributionPicture'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Get distribution sign picture name if exists.
|
|
*
|
|
* @param string|null $sign
|
|
* @return string|null
|
|
*/
|
|
private function getDistributionPicture(?string $sign): ?string
|
|
{
|
|
if (\is_null($sign) or empty($sign)) {
|
|
return null;
|
|
}
|
|
if (File::exists(\public_path("assets/images/signs/$sign.png"))) {
|
|
return "$sign.png";
|
|
}
|
|
report(new \RuntimeException("$sign is missing in assets/images/signs"));
|
|
return null;
|
|
}
|
|
}
|