feat: Aggiungi funzionalità di eliminazione per clienti e preventivi con gestione delle dipendenze
This commit is contained in:
@@ -12,6 +12,7 @@ use App\Support\ConfigurazioneFormatter;
|
||||
use App\Support\PreventivoPdf;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -122,8 +123,21 @@ class CarrelloController extends Controller
|
||||
'note' => ['nullable', 'string', 'max:2000'],
|
||||
]);
|
||||
|
||||
DB::transaction(function () use ($preventivo, $dati) {
|
||||
$cliente = Cliente::create($dati);
|
||||
// Se la richiesta arriva da un cliente loggato nel configuratore (token Sanctum
|
||||
// valido), il preventivo va agganciato al suo record esistente — aggiornando
|
||||
// l'anagrafica con i dati confermati nel form — e NON a un nuovo cliente.
|
||||
// Il guard Sanctum legge il token Bearer direttamente dalla richiesta:
|
||||
// per i guest (o token assenti/scaduti/non validi) restituisce null.
|
||||
$clienteLoggato = Auth::guard('sanctum')->user();
|
||||
|
||||
DB::transaction(function () use ($preventivo, $dati, $clienteLoggato) {
|
||||
$cliente = $clienteLoggato;
|
||||
|
||||
if ($cliente) {
|
||||
$cliente->update($dati);
|
||||
} else {
|
||||
$cliente = Cliente::create($dati);
|
||||
}
|
||||
|
||||
$preventivo->cliente_id = $cliente->id;
|
||||
$preventivo->stato = 'inviato';
|
||||
|
||||
Reference in New Issue
Block a user