validate([ 'api_key' => 'required|string', 'soggetti' => 'required|array', 'soggetti.*.codice_fiscale' => 'nullable|string|max:16', 'soggetti.*.partita_iva' => 'nullable|string|max:11', 'soggetti.*.nome' => 'nullable|string|max:255', 'soggetti.*.cognome' => 'nullable|string|max:255', 'soggetti.*.ragione_sociale' => 'nullable|string|max:255', 'soggetti.*.email' => 'nullable|email|max:255', 'soggetti.*.telefono' => 'nullable|string|max:20', 'soggetti.*.indirizzo' => 'nullable|string|max:500', 'soggetti.*.cap' => 'nullable|string|max:5', 'soggetti.*.citta' => 'nullable|string|max:255', 'soggetti.*.provincia' => 'nullable|string|max:2', 'soggetti.*.tipo' => ['required', Rule::in(['persona_fisica', 'persona_giuridica'])], 'soggetti.*.old_id' => 'nullable|integer' ]); // Verifica API Key if ($validated['api_key'] !== config('netgescon.api_key')) { return response()->json([ 'success' => false, 'message' => 'API Key non valida', 'code' => 'INVALID_API_KEY' ], 401); } $risultati = [ 'inseriti' => 0, 'aggiornati' => 0, 'errori' => [] ]; foreach ($validated['soggetti'] as $index => $soggettoData) { try { // Cerca se esiste giĆ  (per codice fiscale, partita iva o old_id) $soggetto = null; if (!empty($soggettoData['old_id'])) { $soggetto = Soggetto::where('old_id', $soggettoData['old_id'])->first(); } if (!$soggetto && !empty($soggettoData['codice_fiscale'])) { $soggetto = Soggetto::where('codice_fiscale', $soggettoData['codice_fiscale'])->first(); } if (!$soggetto && !empty($soggettoData['partita_iva'])) { $soggetto = Soggetto::where('partita_iva', $soggettoData['partita_iva'])->first(); } if ($soggetto) { // Aggiorna esistente $soggetto->update($soggettoData); $risultati['aggiornati']++; } else { // Crea nuovo Soggetto::create($soggettoData); $risultati['inseriti']++; } } catch (\Exception $e) { $risultati['errori'][] = [ 'indice' => $index, 'errore' => $e->getMessage(), 'dati' => $soggettoData ]; } } return response()->json([ 'success' => true, 'message' => 'Sincronizzazione completata', 'risultati' => $risultati ]); } catch (\Illuminate\Validation\ValidationException $e) { return response()->json([ 'success' => false, 'message' => 'Errori di validazione', 'errors' => $e->errors() ], 422); } catch (\Exception $e) { return response()->json([ 'success' => false, 'message' => 'Errore interno del server', 'error' => $e->getMessage() ], 500); } } /** * Invia dati soggetti al gestionale esterno * * @param Request $request * @return JsonResponse */ public function sendToGestionale(Request $request): JsonResponse { try { $validated = $request->validate([ 'api_key' => 'required|string', 'since' => 'nullable|date', 'tipo' => 'nullable|in:persona_fisica,persona_giuridica', 'limit' => 'nullable|integer|min:1|max:1000' ]); // Verifica API Key if ($validated['api_key'] !== config('netgescon.api_key')) { return response()->json([ 'success' => false, 'message' => 'API Key non valida' ], 401); } $query = Soggetto::query(); // Filtri opzionali if (!empty($validated['since'])) { $query->where('updated_at', '>=', $validated['since']); } if (!empty($validated['tipo'])) { $query->where('tipo', $validated['tipo']); } $limit = $validated['limit'] ?? 100; $soggetti = $query->limit($limit)->get(); return response()->json([ 'success' => true, 'count' => $soggetti->count(), 'soggetti' => $soggetti->map(function ($soggetto) { return [ 'id' => $soggetto->id, 'old_id' => $soggetto->old_id, 'codice_fiscale' => $soggetto->codice_fiscale, 'partita_iva' => $soggetto->partita_iva, 'nome' => $soggetto->nome, 'cognome' => $soggetto->cognome, 'ragione_sociale' => $soggetto->ragione_sociale, 'email' => $soggetto->email, 'telefono' => $soggetto->telefono, 'indirizzo' => $soggetto->indirizzo, 'cap' => $soggetto->cap, 'citta' => $soggetto->citta, 'provincia' => $soggetto->provincia, 'tipo' => $soggetto->tipo, 'codice_univoco' => $soggetto->codice_univoco, 'created_at' => $soggetto->created_at, 'updated_at' => $soggetto->updated_at ]; }) ]); } catch (\Exception $e) { return response()->json([ 'success' => false, 'message' => 'Errore interno del server', 'error' => $e->getMessage() ], 500); } } /** * Stato sincronizzazione */ public function status(Request $request): JsonResponse { $validated = $request->validate([ 'api_key' => 'required|string' ]); if ($validated['api_key'] !== config('netgescon.api_key')) { return response()->json([ 'success' => false, 'message' => 'API Key non valida' ], 401); } $stats = [ 'totale_soggetti' => Soggetto::count(), 'persone_fisiche' => Soggetto::where('tipo', 'persona_fisica')->count(), 'persone_giuridiche' => Soggetto::where('tipo', 'persona_giuridica')->count(), 'con_email' => Soggetto::whereNotNull('email')->count(), 'con_telefono' => Soggetto::whereNotNull('telefono')->count(), 'ultimo_aggiornamento' => Soggetto::latest('updated_at')->first()?->updated_at, 'version' => '1.0.0', 'timestamp' => now() ]; return response()->json([ 'success' => true, 'sistema' => 'NetGescon API', 'statistiche' => $stats ]); } }