ldc-supportweb/app/Http/Controllers/Bo/Credit/EstimationController.php

123 lines
4.6 KiB
PHP
Raw Normal View History

2022-05-31 14:06:07 +02:00
<?php
namespace App\Http\Controllers\Bo\Credit;
2022-05-31 14:06:07 +02:00
use App\Enums\Credit\EstimationStatusEnum;
use App\Enums\Tickets\TicketStatusEnum;
2022-05-31 14:06:07 +02:00
use App\Events\NotifyAdmin;
use App\Events\NotifySubscriptions;
use App\Events\TicketStatusChanged;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Credit\AnswerEstimateRequest;
use App\Http\Requests\Bo\Credit\CreateEstimateRequest;
2022-05-31 14:06:07 +02:00
use App\Models\Credit\Estimation;
use App\Models\Ticket;
use Illuminate\Support\Facades\DB;
class EstimationController extends Controller
{
/**
* Show estimate Form
*
* @param \App\Models\Ticket $ticket
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
public function estimate(Ticket $ticket)
{
$this->authorize('estimate', $ticket);
return view('estimations.create', \compact('ticket'));
}
/**
* Create Ticket estimation
*
* @param \App\Http\Requests\Bo\Credit\CreateEstimateRequest $request
* @param \App\Models\Ticket $ticket
2022-05-31 14:06:07 +02:00
* @return \Illuminate\Http\RedirectResponse
*/
public function estimateSubmit(CreateEstimateRequest $request, Ticket $ticket)
{
return DB::transaction(function () use ($request, $ticket) {
/** @var \App\Models\User */
$user = auth()->user();
$estimate = new Estimation($request->validated());
$estimate->ticket()->associate($ticket);
$estimate->estimator()->associate($user);
$ticket->status = TicketStatusEnum::waitingValidation;
2022-05-31 14:06:07 +02:00
$ticket->saveQuietly();
if ($estimate->saveOrFail()) {
$message = sprintf(
'<b>%s</b> a modifié le statut en <b>%s</b>, Une estimation de %s jours à été ajoutée',
$user->name,
$ticket->status->label(),
2022-05-31 14:06:07 +02:00
\number_format(\floatval($request->amount), 1, ',', '')
);
event(new TicketStatusChanged($ticket, $ticket->status, $user));
event(new NotifySubscriptions($ticket, $message));
event(new NotifyAdmin($ticket, $message));
return response()->redirectToRoute('bo.ticket.show', $ticket)
2022-05-31 14:06:07 +02:00
->with('info', trans('L\'estimation à bien été ajouté'));
}
return \back();
});
}
/**
* Answer ticket estimation
*
* @param \App\Http\Requests\Bo\Credit\AnswerEstimateRequest $request
* @param \App\Models\Ticket $ticket
2022-05-31 14:06:07 +02:00
* @return \Illuminate\Http\RedirectResponse
*/
public function estimatePendingSubmit(AnswerEstimateRequest $request, Ticket $ticket)
{
/** @var \App\Models\User */
$user = auth()->user();
/** @var \App\Models\Credit\Estimation */
$estimation = $ticket->estimation;
if ($request->answer) {
$ticket->status = TicketStatusEnum::takenInAccount;
$estimation->status = EstimationStatusEnum::accepted;
2022-05-31 14:06:07 +02:00
} else {
$ticket->status = TicketStatusEnum::archived;
$estimation->status = EstimationStatusEnum::refused;
2022-05-31 14:06:07 +02:00
}
$estimation->validator()->associate($user);
if ($estimation->saveOrFail() and $ticket->saveOrFail()) {
$message = sprintf(
'Une réponse à été fourni par %s pour l\'estimation du ticket n°%d : %s',
$user->name,
$ticket->id,
$estimation->status->label()
2022-05-31 14:06:07 +02:00
);
event(new NotifySubscriptions($ticket, $message));
event(new NotifyAdmin($ticket, $message));
return response()->redirectToRoute('bo.ticket.show', $ticket)
2022-05-31 14:06:07 +02:00
->with('info', trans('Votre réponse à été enregistrée'));
}
return \back();
}
/**
* Handle submit and cancel an estimate on ticket
*
* @param \App\Models\Ticket $ticket
* @return \Illuminate\Http\RedirectResponse
*/
public function estimateCancelSubmit(Ticket $ticket)
{
$this->authorize('cancelEstimate', $ticket);
/** @var \App\Models\Credit\Estimation */
$estimation = $ticket->estimation;
$ticket->status = TicketStatusEnum::estimateTodo;
if ($estimation->deleteOrFail() and $ticket->saveOrFail()) {
return response()->redirectToRoute('bo.ticket.show', $ticket)
->with('info', trans('L\'estimation a été supprimée.'));
}
return \back();
}
2022-05-31 14:06:07 +02:00
}