Files
termanager2/app/Livewire/Assegnazioni/Rientra.php
Francesco Picone 358cbfc81c ++ rientro: avviso tempo di percorrenza elevato nel banner campagna
Se il territorio è rimasto fuori oltre la soglia "giorni_per_smarrito",
il banner della campagna mostra un avviso (non bloccante). Giorni e
avviso vengono salvati anche nell'evento "campagna_percorso".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 10:54:07 +02:00

135 lines
4.6 KiB
PHP

<?php
namespace App\Livewire\Assegnazioni;
use Livewire\Attributes\Locked;
use Livewire\Component;
use App\Models\Assegnazione;
use App\Models\Campagna;
class Rientra extends Component
{
public Assegnazione $assegnazione;
public string $returned_at = '';
public bool $showCampaignPrompt = false;
// Derived server-side by checkCampaign() and never bound in the view.
// Without #[Locked] the client can inject an arbitrary campaign id into
// the update payload and have the assignment counted toward a campaign it
// has no date overlap with (or hit an FK violation with a bogus id).
#[Locked]
public ?int $campagna_id = null;
public bool $counted_in_campaign = false;
public function mount(Assegnazione $assegnazione)
{
$this->assegnazione = $assegnazione->load(['territorio', 'proclamatore']);
if ($assegnazione->returned_at) {
abort(404, 'Assegnazione già rientrata.');
}
$this->returned_at = now()->format('Y-m-d');
$this->checkCampaign();
}
public function updatedReturnedAt()
{
$this->checkCampaign();
}
protected function checkCampaign()
{
if (!$this->returned_at) {
$this->showCampaignPrompt = false;
return;
}
$returnDate = \Carbon\Carbon::parse($this->returned_at);
$campagna = $this->assegnazione->campagnaApplicabile($returnDate);
if ($campagna) {
$this->showCampaignPrompt = true;
$this->campagna_id = $campagna->id;
} else {
$this->showCampaignPrompt = false;
$this->campagna_id = null;
$this->counted_in_campaign = false;
}
}
protected function rules(): array
{
return [
'returned_at' => 'required|date|after_or_equal:' . $this->assegnazione->assigned_at->format('Y-m-d') . '|before_or_equal:today',
];
}
public function save()
{
$this->validate();
// mount() rejects an already-returned assignment, but the component
// state persists across requests — two open tabs would both submit and
// the second would overwrite returned_at/returned_by and log a
// duplicate activity entry. Re-check against the current row.
abort_if($this->assegnazione->fresh()?->returned_at !== null, 409, 'Assegnazione già rientrata.');
$returnDate = \Carbon\Carbon::parse($this->returned_at);
$this->assegnazione->update([
'returned_at' => $returnDate,
'returned_by' => auth()->id(),
'campaign_id' => $this->counted_in_campaign ? $this->campagna_id : null,
'counted_in_campaign' => $this->counted_in_campaign,
]);
$territorio = $this->assegnazione->territorio;
$proclamatore = $this->assegnazione->proclamatore;
activity()->causedBy(auth()->user())
->performedOn($this->assegnazione)
->withProperties([
'territorio' => $territorio->numero,
'proclamatore' => $proclamatore->nome_completo,
'giorni' => $this->assegnazione->giorni,
'campagna' => $this->counted_in_campaign,
])
->log('returned');
// The operator answered the campaign prompt: record the choice
// (counted or not) as its own audit entry.
if ($this->showCampaignPrompt && $this->campagna_id) {
$this->assegnazione->logCampagnaPercorso(
Campagna::find($this->campagna_id),
$this->counted_in_campaign,
'Rientro'
);
}
session()->flash('success', "Territorio {$territorio->numero} rientrato da {$proclamatore->nome_completo} dopo {$this->assegnazione->giorni} giorni.");
return $this->redirect(route('territori.show', $territorio), navigate: true);
}
public function render()
{
$campagna = $this->campagna_id ? Campagna::find($this->campagna_id) : null;
// Warning shown inside the campaign prompt only: holding time up to
// the selected return date above the "da rientrare" threshold.
$giorniPercorrenza = null;
$sogliaPercorrenza = null;
if ($campagna && $this->returned_at) {
$giorniPercorrenza = $this->assegnazione->giorniPercorrenza(\Carbon\Carbon::parse($this->returned_at));
$sogliaPercorrenza = Assegnazione::sogliaPercorrenzaElevata();
}
return view('livewire.assegnazioni.rientra', [
'campagna' => $campagna,
'giorniPercorrenza' => $giorniPercorrenza,
'sogliaPercorrenza' => $sogliaPercorrenza,
]);
}
}