This commit is contained in:
2026-05-08 12:28:14 +02:00
parent 9793ba8348
commit 42a9506f02
24 changed files with 1266 additions and 959 deletions

View File

@@ -0,0 +1,270 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/features/operations/data/operations_repository.dart';
import 'package:flux/features/operations/models/operation_model.dart';
import 'package:get_it/get_it.dart';
import 'package:uuid/uuid.dart';
part 'operation_form_state.dart';
class OperationFormCubit extends Cubit<OperationFormState> {
final OperationsRepository _repository = GetIt.I<OperationsRepository>();
final SessionCubit _sessionCubit = GetIt.I<SessionCubit>();
final Uuid _uuid = const Uuid();
OperationFormCubit()
: super(
OperationFormState(
// Inizializziamo con un modello vuoto di sicurezza
operation: OperationModel(
storeId: '',
companyId: '',
reference: '',
status: OperationStatus.draft,
createdAt: DateTime.now(),
),
),
);
Future<void> initForm({
OperationModel? existingOperation,
String? operationId,
}) async {
emit(state.copyWith(status: OperationFormStatus.loading));
try {
if (existingOperation != null) {
emit(
state.copyWith(
operation: existingOperation,
status: OperationFormStatus.ready,
),
);
} else if (operationId != null) {
// Avendo separato i cubit, se ci passano solo l'ID lo scarichiamo dal DB
final operation = await _repository.fetchOperationById(operationId);
emit(
state.copyWith(
operation: operation,
status: OperationFormStatus.ready,
),
);
} else {
// NUOVA PRATICA: Creiamo un nuovo Batch UUID
emit(
state.copyWith(
operation: OperationModel(
storeId: _sessionCubit.state.currentStore?.id ?? '',
reference: '',
createdAt: DateTime.now(),
companyId: _sessionCubit.state.company!.id!,
status: OperationStatus.draft,
batchUuid: _uuid.v4(),
),
status: OperationFormStatus.ready,
),
);
}
} catch (e) {
emit(
state.copyWith(
status: OperationFormStatus.failure,
errorMessage: "Errore inizializzazione form: $e",
),
);
}
}
// --- LOGICA BATCH ---
void _prepareNextOperationInBatch() {
final current = state.operation;
emit(
state.copyWith(
status: OperationFormStatus.ready, // Torna ready per il nuovo form
operation: OperationModel(
companyId: current.companyId,
storeId: current.storeId,
storeDisplayName: current.storeDisplayName,
batchUuid: current.batchUuid, // MANTIENE IL COLLEGAMENTO
customerId: current.customerId, // MANTIENE IL CLIENTE
customerDisplayName: current.customerDisplayName,
status: OperationStatus.draft,
createdAt: DateTime.now(),
),
),
);
}
// --- SALVATAGGIO ---
Future<void> saveOperation({
required OperationStatus targetStatus,
required bool keepAdding,
}) async {
emit(
state.copyWith(status: OperationFormStatus.saving, errorMessage: null),
);
try {
final operationToSave = state.operation.copyWith(status: targetStatus);
final savedOperation = await _repository.saveFullOperation(
operation: operationToSave,
);
if (keepAdding) {
// Salviamo nella "memoria" del batch le pratiche create finora
final updatedBatchList = List<OperationModel>.from(
state.savedBatchOperations,
)..add(savedOperation);
emit(
state.copyWith(
status: OperationFormStatus.successAndAddAnother,
savedBatchOperations: updatedBatchList,
),
);
// Pulisce i campi per la prossima operazione
_prepareNextOperationInBatch();
} else {
emit(
state.copyWith(
status: OperationFormStatus.success,
operation: savedOperation, // Aggiorniamo con l'ID restituito dal DB
),
);
}
} catch (e) {
emit(
state.copyWith(
status: OperationFormStatus.failure,
errorMessage: e.toString(),
),
);
}
}
Future<String?> saveOperationDraft() async {
try {
final operationToSave = state.operation;
if (operationToSave.customerId == null ||
operationToSave.customerId!.isEmpty) {
throw Exception('Seleziona un cliente prima di poter usare il QR');
}
final savedOperation = await _repository.saveFullOperation(
operation: operationToSave,
);
emit(
state.copyWith(
operation: savedOperation,
status: OperationFormStatus.ready,
),
);
return savedOperation.id;
} catch (e) {
emit(
state.copyWith(
status: OperationFormStatus.failure,
errorMessage: e.toString(),
),
);
return null;
}
}
// --- GESTIONE DEI CAMPI IN TEMPO REALE ---
void updateFields({
String? customerId,
String? customerDisplayName,
String? reference,
String? note,
String? type,
String? providerId,
String? providerDisplayName,
String? subtype,
String? description,
DateTime? expirationDate,
int? quantity,
String? modelId,
String? modelDisplayName,
String? staffId,
String? staffDisplayName,
OperationStatus? status,
bool clearProvider = false,
bool clearType = false,
bool clearSubtype = false,
bool clearDescription = false,
bool clearExpiration = false,
bool clearQuantity = false,
bool clearModel = false,
}) {
final current = state.operation;
int? newQuantity;
if (clearQuantity) newQuantity = 1;
if (quantity != null && quantity <= 0) newQuantity = 0;
if (quantity != null && quantity > 0) newQuantity = quantity;
final updated = current.copyWith(
customerId:
customerId ??
current.customerId, // Se non passo customerId, tengo il vecchio
customerDisplayName: customerDisplayName ?? current.customerDisplayName,
reference: reference ?? current.reference,
note: note ?? current.note,
providerId: clearProvider ? null : (providerId ?? current.providerId),
providerDisplayName: clearProvider
? null
: (providerDisplayName ?? current.providerDisplayName),
quantity: newQuantity ?? current.quantity,
type: clearType ? null : (type ?? current.type),
description: clearDescription
? null
: (description ?? current.description),
subtype: clearSubtype ? null : (subtype ?? current.subtype),
expirationDate: clearExpiration
? null
: (expirationDate ?? current.expirationDate),
modelId: clearModel ? null : (modelId ?? current.modelId),
modelDisplayName: clearModel
? null
: (modelDisplayName ?? current.modelDisplayName),
staffId: staffId ?? current.staffId,
staffDisplayName: staffDisplayName ?? current.staffDisplayName,
status: status ?? current.status,
);
emit(state.copyWith(operation: updated));
}
// --- UTILS ---
void setTypeWithSmartDefault(String type) {
DateTime? defaultDate;
final now = DateTime.now();
if (type == 'Energy') {
defaultDate = DateTime(now.year, now.month + 24, now.day);
}
if (type == 'Fin') {
defaultDate = DateTime(now.year, now.month + 30, now.day);
}
if (type == 'Entertainment') {
defaultDate = DateTime(now.year, now.month + 12, now.day);
}
updateFields(
type: type,
expirationDate: defaultDate,
clearProvider: true,
clearSubtype: true,
clearModel: true,
clearQuantity: true,
);
}
}

View File

@@ -1,39 +1,48 @@
import 'package:equatable/equatable.dart';
import 'package:flux/features/operations/models/operation_model.dart';
part of 'operation_form_cubit.dart';
enum OperationFormStatus {
initial,
ready,
loading,
ready,
saving,
success,
successAndAddAnother,
successAndAddAnother, // Nuovo stato in stile Ticket!
failure,
}
class OperationFormState extends Equatable {
final OperationModel operation;
final OperationFormStatus status;
final OperationModel operation;
final String? errorMessage;
// Teniamo traccia delle operazioni salvate in questa sessione (per UI riepilogo)
final List<OperationModel> savedBatchOperations;
const OperationFormState({
required this.operation,
this.status = OperationFormStatus.initial,
required this.operation,
this.errorMessage,
this.savedBatchOperations = const [],
});
@override
List<Object?> get props => [operation, status, errorMessage];
OperationFormState copyWith({
OperationModel? operation,
OperationFormStatus? status,
OperationModel? operation,
String? errorMessage,
List<OperationModel>? savedBatchOperations,
}) {
return OperationFormState(
operation: operation ?? this.operation,
status: status ?? this.status,
operation: operation ?? this.operation,
errorMessage: errorMessage,
savedBatchOperations: savedBatchOperations ?? this.savedBatchOperations,
);
}
@override
List<Object?> get props => [
status,
operation,
errorMessage,
savedBatchOperations,
];
}

View File

@@ -0,0 +1,81 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/features/operations/data/operations_repository.dart';
import 'package:flux/features/operations/models/operation_model.dart';
import 'package:get_it/get_it.dart';
part 'operation_list_state.dart';
class OperationListCubit extends Cubit<OperationListState> {
final OperationsRepository _repository = GetIt.I<OperationsRepository>();
final SessionCubit _sessionCubit = GetIt.I<SessionCubit>();
OperationListCubit() : super(const OperationListState());
Future<void> loadOperations({bool refresh = false}) async {
if (state.status == OperationListStatus.loading) return;
if (!refresh && state.hasReachedMax) return;
emit(
state.copyWith(
status: OperationListStatus.loading,
errorMessage: null,
operations: refresh ? [] : state.operations,
hasReachedMax: refresh ? false : state.hasReachedMax,
),
);
try {
final currentOffset = refresh ? 0 : state.operations.length;
final companyId = _sessionCubit.state.company?.id;
if (companyId == null) {
throw Exception("Company ID non trovato nella sessione");
}
final newOperations = await _repository.fetchOperations(
companyId: companyId,
offset: currentOffset,
limit: 50,
searchTerm: state.query,
dateRange: state.dateRange,
);
final bool reachedMax = newOperations.length < 50;
emit(
state.copyWith(
status: OperationListStatus.success,
operations: refresh
? newOperations
: [...state.operations, ...newOperations],
hasReachedMax: reachedMax,
),
);
} catch (e) {
emit(
state.copyWith(
status: OperationListStatus.failure,
errorMessage: "Errore nel caricamento operazioni: $e",
),
);
}
}
void updateFilters({String? query, DateTimeRange? range}) {
emit(
state.copyWith(
query: query ?? state.query,
dateRange: range ?? state.dateRange,
),
);
loadOperations(refresh: true);
}
void clearFilters() {
emit(const OperationListState()); // Resetta tutto allo stato iniziale
loadOperations(refresh: true);
}
}

View File

@@ -0,0 +1,49 @@
part of 'operation_list_cubit.dart';
enum OperationListStatus { initial, loading, success, failure }
class OperationListState extends Equatable {
final OperationListStatus status;
final List<OperationModel> operations;
final bool hasReachedMax;
final String? errorMessage;
final String query;
final DateTimeRange? dateRange;
const OperationListState({
this.status = OperationListStatus.initial,
this.operations = const [],
this.hasReachedMax = false,
this.errorMessage,
this.query = '',
this.dateRange,
});
OperationListState copyWith({
OperationListStatus? status,
List<OperationModel>? operations,
bool? hasReachedMax,
String? errorMessage,
String? query,
DateTimeRange? dateRange,
}) {
return OperationListState(
status: status ?? this.status,
operations: operations ?? this.operations,
hasReachedMax: hasReachedMax ?? this.hasReachedMax,
errorMessage: errorMessage,
query: query ?? this.query,
dateRange: dateRange ?? this.dateRange,
);
}
@override
List<Object?> get props => [
status,
operations,
hasReachedMax,
errorMessage,
query,
dateRange,
];
}

View File

@@ -1,304 +0,0 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/features/operations/data/operations_repository.dart';
import 'package:flux/features/operations/models/operation_model.dart';
import 'package:get_it/get_it.dart';
import 'package:collection/collection.dart';
import 'package:uuid/uuid.dart';
part 'operations_state.dart';
class OperationsCubit extends Cubit<OperationsState> {
final OperationsRepository _repository = GetIt.I<OperationsRepository>();
final SessionCubit _sessionCubit = GetIt.I<SessionCubit>();
final Uuid _uuid = const Uuid(); // Generatore di UUID per il batch
OperationsCubit()
: super(const OperationsState(status: OperationsStatus.initial));
// --- CARICAMENTO E PAGINAZIONE ---
Future<void> loadOperations({bool refresh = false}) async {
if (state.status == OperationsStatus.loading) return;
if (!refresh && state.hasReachedMax) return;
emit(
state.copyWith(
status: OperationsStatus.loading,
errorMessage: null,
allOperations: refresh ? [] : state.allOperations,
hasReachedMax: refresh ? false : state.hasReachedMax,
),
);
try {
final currentOffset = refresh ? 0 : state.allOperations.length;
final companyId = _sessionCubit.state.company?.id;
if (companyId == null) {
throw Exception("Company ID non trovato nella sessione");
}
final newOperations = await _repository.fetchOperations(
companyId: companyId,
offset: currentOffset,
limit: 50,
searchTerm: state.query,
dateRange: state.dateRange,
);
final bool reachedMax = newOperations.length < 50;
emit(
state.copyWith(
status: OperationsStatus.ready,
allOperations: refresh
? newOperations
: [...state.allOperations, ...newOperations],
hasReachedMax: reachedMax,
),
);
} catch (e) {
emit(
state.copyWith(
status: OperationsStatus.failure,
errorMessage: "Errore nel caricamento operazioni: $e",
),
);
}
}
// --- GESTIONE FILTRI ---
void updateFilters({String? query, DateTimeRange? range}) {
emit(
state.copyWith(
query: query ?? state.query,
dateRange: range ?? state.dateRange,
),
);
loadOperations(refresh: true);
}
void clearFilters() {
emit(state.copyWith(query: '', dateRange: null));
loadOperations(refresh: true);
}
void initOperationForm({
OperationModel? existingOperation,
String? operationId,
String? staffId,
String? staffDisplayName,
}) async {
if (existingOperation != null) {
emit(
state.copyWith(
currentOperation: existingOperation,
status: OperationsStatus.ready,
),
);
} else if (operationId != null) {
OperationModel? operationModel = state.allOperations.firstWhereOrNull(
(s) => s.id == operationId,
);
operationModel ??= await _repository.fetchOperationById(operationId);
emit(
state.copyWith(
currentOperation: operationModel,
status: OperationsStatus.ready,
),
);
} else {
// NUOVA PRATICA: Creiamo un nuovo Batch UUID
emit(
state.copyWith(
currentOperation: OperationModel(
storeId: _sessionCubit.state.currentStore?.id ?? '',
reference: '',
createdAt: DateTime.now(),
companyId: _sessionCubit.state.company!.id!,
status: OperationStatus.draft,
batchUuid: _uuid.v4(), // <-- GENERIAMO IL BATCH UNIVOCO
),
status: OperationsStatus.ready,
),
);
}
}
/// MAGIA PURA: Prepara il form per inserire un altro servizio nella stessa pratica.
/// Mantiene il Cliente, il Batch e lo Store, ma svuota il resto.
void prepareNextOperationInBatch() {
if (state.currentOperation == null) return;
final current = state.currentOperation!;
emit(
state.copyWith(
status: OperationsStatus.ready,
currentOperation: OperationModel(
companyId: current.companyId,
storeId: current.storeId,
storeDisplayName: current.storeDisplayName,
batchUuid: current.batchUuid, // <-- MANTIENE IL COLLEGAMENTO
customerId: current.customerId, // <-- MANTIENE IL CLIENTE
customerDisplayName: current.customerDisplayName,
status: OperationStatus.draft,
createdAt: DateTime.now(),
),
),
);
}
// --- PERSISTENZA ---
Future<void> saveCurrentOperation({
required OperationStatus targetStatus,
bool shouldPop = true,
}) async {
if (state.currentOperation == null) return;
emit(state.copyWith(status: OperationsStatus.saving, errorMessage: null));
try {
final operationToSave = state.currentOperation!.copyWith(
status: targetStatus,
);
final updatedOperation = await _repository.saveFullOperation(
operation: operationToSave,
);
emit(
state.copyWith(
// Se non facciamo Pop (es. l'utente vuole aggiungere un altro servizio), non killiamo l'operazione corrente
status: shouldPop
? OperationsStatus.saved
: OperationsStatus.savedNoPop,
currentOperation: shouldPop ? null : updatedOperation,
),
);
// Ricarica in background per la dashboard
loadOperations(refresh: true);
} catch (e) {
emit(
state.copyWith(
status: OperationsStatus.failure,
errorMessage: e.toString(),
),
);
}
}
// --- RECUPERO OPERAZIONI DELLO STESSO BATCH (Per UI di riepilogo) ---
/// Puoi usare questa funzione se nella UI vuoi mostrare "Hai inserito 3 servizi in questa pratica"
List<OperationModel> getOperationsInCurrentBatch() {
if (state.currentOperation == null) return [];
final currentBatch = state.currentOperation!.batchUuid;
// Filtriamo dalla lista caricata (o potresti fare una query diretta a Supabase se preferisci)
return state.allOperations
.where(
(op) =>
op.batchUuid == currentBatch &&
op.id != state.currentOperation!.id,
)
.toList();
}
// --- GESTIONE DELLO STATO DEL FORM IN TEMPO REALE ---
void updateOperationFields({
String? customerId,
String? customerDisplayName,
String? type,
String? providerId,
String? providerDisplayName,
String? subtype,
String? description,
DateTime? expirationDate,
int? quantity,
String? modelId,
String? modelDisplayName,
String? staffId,
String? staffDisplayName,
// Aggiungiamo questi flag per forzare la pulizia dei campi quando cambi tipo
bool clearProvider = false,
bool clearType = false,
bool clearSubtype = false,
bool clearDescription = false,
bool clearExpiration = false,
bool clearQuantity = false,
bool clearModel = false,
}) {
if (state.currentOperation == null) return;
final current = state.currentOperation!;
// Creiamo il modello aggiornato
// ATTENZIONE: adatta questa logica in base a come è scritto il tuo copyWith!
int? newQuantity;
if (clearQuantity) {
newQuantity = 1;
}
if (quantity != null && quantity <= 0) {
newQuantity = 0;
}
if (quantity != null && quantity > 0) {
newQuantity = quantity;
}
final updated = current.copyWith(
customerId: customerId,
customerDisplayName: customerDisplayName,
// Se clearProvider è true, forziamo una stringa vuota (o null se il tuo modello lo supporta)
providerId: clearProvider ? null : (providerId ?? current.providerId),
providerDisplayName: clearProvider
? null
: (providerDisplayName ?? current.providerDisplayName),
quantity: newQuantity,
type: clearType ? null : (type ?? current.type),
description: clearDescription
? null
: (description ?? current.description),
subtype: clearSubtype ? null : (subtype ?? current.subtype),
expirationDate: clearExpiration
? null
: (expirationDate ?? current.expirationDate),
modelId: clearModel ? null : (modelId ?? current.modelId),
modelDisplayName: clearModel
? null
: (modelDisplayName ?? current.modelDisplayName),
staffId: staffId ?? current.staffId,
staffDisplayName: staffDisplayName ?? current.staffDisplayName,
);
emit(state.copyWith(currentOperation: updated));
}
// Metodo di utilità per calcolare la data X mesi da oggi
DateTime _calculateMonths(int months) {
final now = DateTime.now();
return DateTime(now.year, now.month + months, now.day);
}
// Quando l'utente seleziona un tipo, impostiamo il default
void setTypeWithSmartDefault(String type) {
DateTime? defaultDate;
if (type == 'Energy') defaultDate = _calculateMonths(24);
if (type == 'Fin') defaultDate = _calculateMonths(30);
if (type == 'Entertainment') defaultDate = _calculateMonths(12);
updateOperationFields(
type: type,
expirationDate: defaultDate,
clearProvider: true,
clearSubtype: true,
clearModel: true,
clearQuantity: true,
);
}
}

View File

@@ -1,68 +0,0 @@
part of 'operations_cubit.dart';
enum OperationsStatus {
initial,
loading,
ready,
saving,
saved,
savedNoPop,
success,
failure,
}
class OperationsState extends Equatable {
final OperationsStatus status;
final List<OperationModel> allOperations;
final OperationModel? currentOperation; // La bozza che stiamo editando
final String? errorMessage;
final String query;
final DateTimeRange? dateRange;
final bool hasReachedMax;
final bool isSavingDraft;
const OperationsState({
required this.status,
this.allOperations = const [],
this.currentOperation,
this.errorMessage,
this.query = '',
this.dateRange,
this.hasReachedMax = false,
this.isSavingDraft = false,
});
OperationsState copyWith({
OperationsStatus? status,
List<OperationModel>? allOperations,
OperationModel? currentOperation,
String? errorMessage,
String? query,
DateTimeRange? dateRange,
bool? hasReachedMax,
bool? isSavingDraft,
}) {
return OperationsState(
status: status ?? this.status,
allOperations: allOperations ?? this.allOperations,
currentOperation: currentOperation ?? this.currentOperation,
errorMessage: errorMessage,
query: query ?? this.query,
dateRange: dateRange ?? this.dateRange,
hasReachedMax: hasReachedMax ?? this.hasReachedMax,
isSavingDraft: isSavingDraft ?? this.isSavingDraft,
);
}
@override
List<Object?> get props => [
status,
allOperations,
currentOperation,
errorMessage,
query,
dateRange,
hasReachedMax,
isSavingDraft,
];
}

View File

@@ -3,13 +3,11 @@ import 'package:flux/core/utils/extensions.dart';
import 'package:flux/features/attachments/models/attachment_model.dart';
enum OperationStatus {
ok('ok'),
waitingforaction('waiting_for_action'),
waitingforsupport('waiting_for_support'),
waitingfordeployment('waiting_for_deployment'),
ko('ko'),
draft('draft'),
canceled('canceled');
success('success', 'OK'),
waitingForAction('waiting_for_action', 'In attesa di azione'),
waitingForSupport('waiting_for_support', 'In attesa di supporto'),
failure('failure', 'KO'),
draft('draft', 'Bozza');
static OperationStatus fromString(String value) {
final normalizedValue = value.replaceAll('_', '').toLowerCase();
@@ -19,8 +17,9 @@ enum OperationStatus {
}
final String supabaseName;
final String displayName;
const OperationStatus(this.supabaseName);
const OperationStatus(this.supabaseName, this.displayName);
}
class OperationModel extends Equatable {
@@ -163,8 +162,8 @@ class OperationModel extends Equatable {
attachments,
];
factory OperationModel.empty({required String companyId}) {
return OperationModel(id: null, createdAt: null, companyId: companyId);
factory OperationModel.empty() {
return OperationModel(id: null, createdAt: null, companyId: '');
}
factory OperationModel.fromMap(Map<String, dynamic> map) {

View File

@@ -1,14 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/core/widgets/shared_forms/shared_files_section.dart';
import 'package:flux/features/attachments/blocs/attachments_bloc.dart';
import 'package:flux/features/operations/blocs/operations_cubit.dart';
import 'package:flux/features/operations/blocs/operation_form_cubit.dart';
import 'package:flux/features/operations/models/operation_model.dart';
import 'package:flux/core/widgets/shared_forms/customer_section.dart';
import 'package:flux/features/operations/ui/widgets/details_section.dart';
import 'package:flux/core/widgets/shared_forms/attachments_section.dart';
import 'package:flux/core/widgets/shared_forms/staff_section.dart';
import 'package:get_it/get_it.dart';
class OperationFormScreen extends StatefulWidget {
final String? operationId;
@@ -49,26 +48,10 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
@override
void initState() {
super.initState();
final cubit = context.read<OperationsCubit>();
final currentLoggedStaff = GetIt.I
.get<SessionCubit>()
.state
.currentStaffMember!;
// 1. Diciamo al Cubit di prepararsi
cubit.initOperationForm(
context.read<OperationFormCubit>().initForm(
existingOperation: widget.existingOperation,
operationId: widget.operationId,
staffId: currentLoggedStaff.id,
staffDisplayName: currentLoggedStaff.name,
);
// 2. IL TRUCCO MAGICO:
// Se abbiamo passato existingOperation, il Cubit si è appena aggiornato.
// Lo stato è già pronto, quindi sincronizziamo i controller SUBITO!
if (cubit.state.currentOperation != null) {
_syncTextControllers(cubit.state.currentOperation!);
}
}
@override
@@ -76,50 +59,83 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
_referenceController.dispose();
_noteController.dispose();
_freeTextSubtypeController.dispose();
_freeTextDescriptionController.dispose();
super.dispose();
}
void _syncTextControllers(OperationModel model) {
if (_referenceController.text.isEmpty && model.reference.isNotEmpty) {
if (_referenceController.text.isEmpty) {
_referenceController.text = model.reference;
}
if (_noteController.text.isEmpty && model.note.isNotEmpty) {
if (_noteController.text.isEmpty) {
_noteController.text = model.note;
}
if (_freeTextSubtypeController.text.isEmpty &&
model.subtype != null &&
model.subtype!.isNotEmpty) {
_freeTextSubtypeController.text = model.subtype!;
if (_freeTextSubtypeController.text.isEmpty) {
_freeTextSubtypeController.text = model.subtype ?? '';
}
if (_freeTextDescriptionController.text.isEmpty &&
model.description != null &&
model.description!.isNotEmpty) {
_freeTextDescriptionController.text = model.description!;
if (_freeTextDescriptionController.text.isEmpty) {
_freeTextDescriptionController.text = model.description ?? '';
}
// Se è una nuova pratica (draft), impostiamo di default il target su OK per comodità UI
if (model.id == null && model.status == OperationStatus.draft) {
// Usiamo addPostFrameCallback per non interferire con il build attuale
WidgetsBinding.instance.addPostFrameCallback((_) {
// Supponendo tu aggiunga la possibilità di aggiornare lo status nel metodo updateFields del Cubit
// context.read<OperationFormCubit>().updateFields(status: OperationStatus.ok);
});
}
_isInitialized = true;
}
void _saveOperation({required bool keepAdding}) {
void _flushControllersToCubit() {
context.read<OperationFormCubit>().updateFields(
reference: _referenceController.text,
note: _noteController.text,
subtype: _freeTextSubtypeController.text,
description: _freeTextDescriptionController.text,
);
}
void _saveOperation({
required OperationStatus targetStatus,
required bool keepAdding,
}) {
if (_formKey.currentState!.validate()) {
final cubit = context.read<OperationsCubit>();
final currentOperation = cubit.state.currentOperation!;
final operationToSave = currentOperation.copyWith(
reference: _referenceController.text,
note: _noteController.text,
subtype: ['Entertainment', 'Custom'].contains(currentOperation.type)
? _freeTextSubtypeController.text
: currentOperation.subtype,
description: ['Energy', 'Custom'].contains(currentOperation.type)
? _freeTextDescriptionController.text
: currentOperation.description,
_flushControllersToCubit();
context.read<OperationFormCubit>().saveOperation(
targetStatus: targetStatus,
keepAdding: keepAdding,
);
}
}
cubit.initOperationForm(existingOperation: operationToSave);
cubit.saveCurrentOperation(
targetStatus: OperationStatus.ok,
shouldPop: !keepAdding,
);
Future<String?> _generateIdForQr() async {
if (!_formKey.currentState!.validate()) return null;
_flushControllersToCubit();
final attachmentsBloc = context.read<AttachmentsBloc>();
// Presumo tu abbia creato il metodo saveOperationDraft() nel Cubit!
final newId = await context.read<OperationFormCubit>().saveOperationDraft();
if (newId != null && context.mounted) {
attachmentsBloc.add(ParentEntitySavedEvent(newId));
}
return newId;
}
// Helper per assegnare un colore agli stati
Color _getStatusColor(OperationStatus status) {
switch (status) {
case OperationStatus.success:
return Colors.green;
case OperationStatus.waitingForAction:
return Colors.orange;
case OperationStatus.waitingForSupport:
return Colors.blue;
case OperationStatus.failure:
return Colors.red;
case OperationStatus.draft:
return Colors.grey;
}
}
@@ -127,33 +143,27 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
Widget build(BuildContext context) {
final theme = Theme.of(context);
return BlocConsumer<OperationsCubit, OperationsState>(
listenWhen: (previous, current) =>
previous.status != current.status ||
previous.currentOperation?.id != current.currentOperation?.id,
return BlocConsumer<OperationFormCubit, OperationFormState>(
listenWhen: (previous, current) => previous.status != current.status,
listener: (context, state) {
if (state.status == OperationsStatus.ready &&
state.currentOperation != null &&
!_isInitialized) {
_syncTextControllers(state.currentOperation!);
if (state.status == OperationFormStatus.ready && !_isInitialized) {
_syncTextControllers(state.operation);
}
if (state.status == OperationsStatus.saved) {
if (state.status == OperationFormStatus.success) {
Navigator.of(context).pop();
} else if (state.status == OperationsStatus.savedNoPop) {
context.read<OperationsCubit>().prepareNextOperationInBatch();
} else if (state.status == OperationFormStatus.successAndAddAnother) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Servizio aggiunto! Inserisci il prossimo.'),
content: Text('Operazione salvata! Inserisci la prossima'),
),
);
_freeTextSubtypeController.clear();
_freeTextDescriptionController.clear();
} else if (state.status == OperationsStatus.failure) {
} else if (state.status == OperationFormStatus.failure) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(state.errorMessage ?? 'Errore'),
backgroundColor: theme.colorScheme.error,
content: Text(state.errorMessage ?? 'Errore di salvataggio'),
backgroundColor: Colors.red,
),
);
}
@@ -161,19 +171,45 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
builder: (context, state) {
if (!_isInitialized &&
(widget.operationId != null || widget.existingOperation != null) &&
state.status == OperationsStatus.loading) {
state.status == OperationFormStatus.loading) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
// Determiniamo lo stato da mostrare nel form.
// Se è una bozza appena creata, mostriamo visivamente "OK" come default per il salvataggio.
final displayStatus =
state.operation.status == OperationStatus.draft &&
state.operation.id == null
? OperationStatus.success
: state.operation.status;
return Scaffold(
appBar: AppBar(
title: Text(
state.currentOperation?.id == null
? 'Nuova Pratica'
: 'Modifica Pratica',
state.operation.id == null ? 'Nuova Pratica' : 'Modifica Pratica',
),
// Mettiamo un piccolo indicatore visivo anche nella AppBar se non è OK
actions:
displayStatus != OperationStatus.success &&
displayStatus != OperationStatus.draft
? [
Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Chip(
label: Text(
displayStatus.displayName,
style: const TextStyle(
color: Colors.white,
fontSize: 12,
),
),
backgroundColor: _getStatusColor(displayStatus),
),
),
]
: null,
),
body: Form(
key: _formKey,
@@ -182,26 +218,22 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
final isUltraWide = constraints.maxWidth > 1400;
final isDesktop = constraints.maxWidth > 900;
if (isUltraWide) {
// --- LAYOUT 3 COLONNE (Schermi giganti) ---
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 1. FORM PRINCIPALE (40%)
Expanded(
flex: 4,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
// Attenzione: devi togliere la sezione file dal _buildMainFormContent!
child: _buildMainFormContent(
theme,
state,
displayStatus,
showFiles: false,
),
),
),
VerticalDivider(width: 1, color: theme.dividerColor),
// 2. NOTE (30%)
Expanded(
flex: 3,
child: Padding(
@@ -210,15 +242,15 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
),
),
VerticalDivider(width: 1, color: theme.dividerColor),
// 3. FILE (30%)
Expanded(
flex: 3,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: SharedAttachmentsSection(
parentType: AttachmentParentType.operation,
parentId: state.currentOperation?.id,
child: SharedFilesSection(
titleNameForUpload:
state.operation.customerDisplayName ??
'Nuova operazione',
onGenerateIdForQr: _generateIdForQr,
),
),
),
@@ -232,7 +264,11 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
flex: 7,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: _buildMainFormContent(theme, state),
child: _buildMainFormContent(
theme,
state,
displayStatus,
),
),
),
VerticalDivider(width: 1, color: theme.dividerColor),
@@ -251,7 +287,7 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildMainFormContent(theme, state),
_buildMainFormContent(theme, state, displayStatus),
const Divider(height: 32),
_buildNotesSection(isDesktop: false),
const SizedBox(height: 80),
@@ -270,9 +306,13 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
Expanded(
flex: 1,
child: OutlinedButton(
onPressed: state.status == OperationsStatus.saving
onPressed: state.status == OperationFormStatus.saving
? null
: () => _saveOperation(keepAdding: true),
: () => _saveOperation(
keepAdding: true,
targetStatus:
displayStatus, // <-- Usiamo lo stato selezionato nel form!
),
child: const Text(
'Salva e Aggiungi Altro',
textAlign: TextAlign.center,
@@ -283,10 +323,27 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
Expanded(
flex: 1,
child: ElevatedButton(
onPressed: state.status == OperationsStatus.saving
style: ElevatedButton.styleFrom(
// Se c'è un KO o un blocco, cambiamo il colore del bottone principale per attirare l'attenzione
backgroundColor:
displayStatus != OperationStatus.success &&
displayStatus != OperationStatus.draft
? _getStatusColor(displayStatus)
: null,
foregroundColor:
displayStatus != OperationStatus.success &&
displayStatus != OperationStatus.draft
? Colors.white
: null,
),
onPressed: state.status == OperationFormStatus.saving
? null
: () => _saveOperation(keepAdding: false),
child: state.status == OperationsStatus.saving
: () => _saveOperation(
keepAdding: false,
targetStatus:
displayStatus, // <-- Usiamo lo stato selezionato nel form!
),
child: state.status == OperationFormStatus.saving
? const SizedBox(
width: 20,
height: 20,
@@ -309,32 +366,102 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
Widget _buildMainFormContent(
ThemeData theme,
OperationsState state, {
OperationFormState state,
OperationStatus displayStatus, {
bool showFiles = true,
}) {
final currentOp = state.currentOperation;
final currentType = currentOp?.type ?? 'AL';
final currentOp = state.operation;
final currentType = currentOp.type;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
StaffSection(
staffId: currentOp?.staffId,
staffName: currentOp?.staffDisplayName,
staffId: currentOp.staffId,
staffName: currentOp.staffDisplayName,
onStaffSelected: (staff) => {
context.read<OperationsCubit>().updateOperationFields(
context.read<OperationFormCubit>().updateFields(
staffId: staff.id,
staffDisplayName: staff.name,
),
},
),
const Divider(height: 50),
// --- SEZIONE STATO OPERAZIONE ---
_buildSectionTitle('Esito / Stato Operazione'),
Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
decoration: BoxDecoration(
color: _getStatusColor(displayStatus).withValues(alpha: 0.1),
border: Border.all(
color: _getStatusColor(displayStatus).withValues(alpha: 0.3),
),
borderRadius: BorderRadius.circular(12),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<OperationStatus>(
isExpanded: true,
value: displayStatus,
icon: Icon(
Icons.arrow_drop_down,
color: _getStatusColor(displayStatus),
),
items: OperationStatus.values
.where(
(s) => s != OperationStatus.draft,
) // Nascondiamo 'Bozza' dal menu
.map(
(status) => DropdownMenuItem(
value: status,
child: Row(
children: [
Icon(
status == OperationStatus.success
? Icons.check_circle
: Icons.error_outline,
color: _getStatusColor(status),
size: 20,
),
const SizedBox(width: 12),
Text(
status.displayName,
style: TextStyle(
fontWeight: FontWeight.w600,
color: _getStatusColor(status),
),
),
],
),
),
)
.toList(),
onChanged: (newStatus) {
if (newStatus != null) {
// Assicurati che il metodo updateFields nel tuo Cubit accetti anche 'status'
context.read<OperationFormCubit>().updateFields(
status: newStatus,
);
}
},
),
),
),
const SizedBox(height: 8),
Text(
displayStatus == OperationStatus.success
? 'Lascia OK se la pratica è stata caricata con successo.'
: 'Attenzione: la pratica verrà salvata come ${displayStatus.displayName}.',
style: TextStyle(fontSize: 12, color: Colors.grey.shade600),
),
const Divider(height: 32),
_buildSectionTitle('Cliente & Riferimento'),
SharedCustomerSection(
customerId: currentOp?.customerId,
customerName: currentOp?.customerDisplayName,
customerId: currentOp.customerId,
customerName: currentOp.customerDisplayName,
onCustomerSelected: (customer) {
context.read<OperationsCubit>().updateOperationFields(
context.read<OperationFormCubit>().updateFields(
customerId: customer.id,
customerDisplayName: customer.name,
);
@@ -360,7 +487,9 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
selected: currentType == type,
onSelected: (selected) {
if (selected) {
context.read<OperationsCubit>().setTypeWithSmartDefault(type);
context.read<OperationFormCubit>().setTypeWithSmartDefault(
type,
);
}
},
);
@@ -384,23 +513,23 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
IconButton(
icon: const Icon(Icons.remove),
onPressed: () {
final q = currentOp?.quantity ?? 1;
final q = currentOp.quantity;
if (q > 1) {
context.read<OperationsCubit>().updateOperationFields(
context.read<OperationFormCubit>().updateFields(
quantity: q - 1,
);
}
},
),
Text(
'${currentOp?.quantity ?? 1}',
'${currentOp.quantity}',
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
final q = currentOp?.quantity ?? 1;
context.read<OperationsCubit>().updateOperationFields(
final q = currentOp.quantity;
context.read<OperationFormCubit>().updateFields(
quantity: q + 1,
);
},
@@ -410,9 +539,14 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
const Divider(height: 32),
if (showFiles) ...[
SharedAttachmentsSection(
/* SharedAttachmentsSection(
parentType: AttachmentParentType.operation,
parentId: currentOp?.id,
parentId: currentOp.id,
), */
SharedFilesSection(
titleNameForUpload:
state.operation.customerDisplayName ?? 'Nuova pratica',
onGenerateIdForQr: _generateIdForQr,
),
],
],
@@ -444,7 +578,7 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
backgroundColor: Colors.blue.withValues(alpha: 0.05),
onPressed: () {
final now = DateTime.now();
context.read<OperationsCubit>().updateOperationFields(
context.read<OperationFormCubit>().updateFields(
expirationDate: DateTime(
now.year,
now.month + months,

View File

@@ -1,18 +1,18 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/features/operations/blocs/operations_cubit.dart';
import 'package:flux/features/operations/blocs/operation_list_cubit.dart';
import 'package:flux/features/operations/models/operation_model.dart';
import 'package:go_router/go_router.dart';
// Importa i tuoi modelli e cubit
class OperationsScreen extends StatefulWidget {
const OperationsScreen({super.key});
class OperationListScreen extends StatefulWidget {
const OperationListScreen({super.key});
@override
State<OperationsScreen> createState() => _OperationsScreenState();
State<OperationListScreen> createState() => _OperationListScreenState();
}
class _OperationsScreenState extends State<OperationsScreen> {
class _OperationListScreenState extends State<OperationListScreen> {
final ScrollController _scrollController = ScrollController();
@override
@@ -20,13 +20,11 @@ class _OperationsScreenState extends State<OperationsScreen> {
super.initState();
// Agganciamo il listener per la paginazione (Scroll Infinito)
_scrollController.addListener(_onScroll);
// Carichiamo i servizi iniziali
context.read<OperationsCubit>().loadOperations();
}
void _onScroll() {
if (_isBottom) {
context.read<OperationsCubit>().loadOperations();
context.read<OperationListCubit>().loadOperations();
}
}
@@ -59,16 +57,16 @@ class _OperationsScreenState extends State<OperationsScreen> {
),
],
),
body: BlocBuilder<OperationsCubit, OperationsState>(
body: BlocBuilder<OperationListCubit, OperationListState>(
builder: (context, state) {
// 1. Stato di caricamento iniziale
if (state.status == OperationsStatus.loading &&
state.allOperations.isEmpty) {
if (state.status == OperationListStatus.loading &&
state.operations.isEmpty) {
return const Center(child: CircularProgressIndicator());
}
// 2. Lista vuota
if (state.allOperations.isEmpty) {
if (state.operations.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
@@ -77,7 +75,7 @@ class _OperationsScreenState extends State<OperationsScreen> {
const SizedBox(height: 10),
ElevatedButton(
onPressed: () => context
.read<OperationsCubit>()
.read<OperationListCubit>()
.loadOperations(refresh: true),
child: const Text("Riprova"),
),
@@ -88,16 +86,17 @@ class _OperationsScreenState extends State<OperationsScreen> {
// 3. La Lista (con Pull-to-refresh)
return RefreshIndicator(
onRefresh: () =>
context.read<OperationsCubit>().loadOperations(refresh: true),
onRefresh: () => context.read<OperationListCubit>().loadOperations(
refresh: true,
),
child: ListView.builder(
controller: _scrollController,
padding: const EdgeInsets.only(bottom: 80), // Spazio per il FAB
itemCount: state.hasReachedMax
? state.allOperations.length
: state.allOperations.length + 1,
? state.operations.length
: state.operations.length + 1,
itemBuilder: (context, index) {
if (index >= state.allOperations.length) {
if (index >= state.operations.length) {
return const Center(
child: Padding(
padding: EdgeInsets.all(16.0),
@@ -106,7 +105,7 @@ class _OperationsScreenState extends State<OperationsScreen> {
);
}
final operation = state.allOperations[index];
final operation = state.operations[index];
return _buildOperationCard(context, operation);
},
),
@@ -173,17 +172,16 @@ class _OperationsScreenState extends State<OperationsScreen> {
Widget _buildOperationStatus(OperationStatus status) {
Color color;
switch (status) {
case OperationStatus.canceled || OperationStatus.ko:
case OperationStatus.failure:
color = Colors.grey.shade800;
break;
case OperationStatus.waitingforaction || OperationStatus.draft:
case OperationStatus.waitingForAction || OperationStatus.draft:
color = Colors.orange;
break;
case OperationStatus.ok:
case OperationStatus.success:
color = Colors.green;
break;
case OperationStatus.waitingfordeployment ||
OperationStatus.waitingforsupport:
case OperationStatus.waitingForSupport:
color = Colors.blue;
break;
}

View File

@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/widgets/shared_forms/model_section.dart';
import 'package:flux/features/master_data/providers/blocs/provider_cubit.dart';
import 'package:flux/features/operations/blocs/operations_cubit.dart';
import 'package:flux/features/operations/blocs/operation_form_cubit.dart';
import 'package:flux/features/operations/models/operation_model.dart';
class DetailsSection extends StatelessWidget {
@@ -117,12 +117,10 @@ class DetailsSection extends StatelessWidget {
),
),
onTap: () {
context
.read<OperationsCubit>()
.updateOperationFields(
providerId: provider.id,
providerDisplayName: provider.name,
);
context.read<OperationFormCubit>().updateFields(
providerId: provider.id,
providerDisplayName: provider.name,
);
Navigator.pop(modalContext);
},
);
@@ -190,9 +188,7 @@ class DetailsSection extends StatelessWidget {
].map((s) => DropdownMenuItem(value: s, child: Text(s))).toList(),
onChanged: (val) {
if (val != null) {
context.read<OperationsCubit>().updateOperationFields(
subtype: val,
);
context.read<OperationFormCubit>().updateFields(subtype: val);
}
},
),
@@ -215,7 +211,7 @@ class DetailsSection extends StatelessWidget {
modelId: currentOp?.modelId,
modelName: currentOp?.modelDisplayName,
onModelSelected: (id, name) {
context.read<OperationsCubit>().updateOperationFields(
context.read<OperationFormCubit>().updateFields(
modelId: id,
modelDisplayName: name,
);
@@ -271,7 +267,7 @@ class DetailsSection extends StatelessWidget {
lastDate: DateTime.now().add(const Duration(days: 3650)),
);
if (date != null && context.mounted) {
context.read<OperationsCubit>().updateOperationFields(
context.read<OperationFormCubit>().updateFields(
expirationDate: date,
);
}