feat: Aggiungi funzionalità di eliminazione per clienti e preventivi con gestione delle dipendenze
This commit is contained in:
@@ -31,6 +31,16 @@ class Cliente extends Model
|
||||
return $this->hasMany(Preventivo::class);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::deleting(function (Cliente $cliente): void {
|
||||
// Elimina di conseguenza i preventivi del cliente (con i relativi file)
|
||||
// e revoca i token API usati per l'accesso al configuratore.
|
||||
$cliente->preventivi()->get()->each->delete();
|
||||
$cliente->tokens()->delete();
|
||||
});
|
||||
}
|
||||
|
||||
public function getNominativoAttribute(): string
|
||||
{
|
||||
if ($this->ragione_sociale) {
|
||||
@@ -41,4 +51,11 @@ class Cliente extends Model
|
||||
|
||||
return $nome !== '' ? $nome : $this->username;
|
||||
}
|
||||
|
||||
public function getNomeCompletoAttribute(): string
|
||||
{
|
||||
$nome = trim("{$this->nome} {$this->cognome}");
|
||||
|
||||
return $nome !== '' ? $nome : $this->username;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class Preventivo extends Model
|
||||
{
|
||||
@@ -26,6 +27,24 @@ class Preventivo extends Model
|
||||
'chiuso' => 'Chiuso',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::deleting(function (Preventivo $preventivo): void {
|
||||
// Elimina immagini degli item (disk 'public') e PDF (disk 'local') prima della cancellazione.
|
||||
foreach ($preventivo->items()->get() as $item) {
|
||||
if ($item->immagine_path && Storage::disk('public')->exists($item->immagine_path)) {
|
||||
Storage::disk('public')->delete($item->immagine_path);
|
||||
}
|
||||
|
||||
$item->delete();
|
||||
}
|
||||
|
||||
if ($preventivo->pdf_path && Storage::disk('local')->exists($preventivo->pdf_path)) {
|
||||
Storage::disk('local')->delete($preventivo->pdf_path);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function cliente(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Cliente::class);
|
||||
|
||||
Reference in New Issue
Block a user