a
This commit is contained in:
1 parent
c6e3594850
commit
9154a16fe1
2 files changed
+240
-112
No files matched your search
@@ -2,10 +2,9 @@ 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/customers/models/customer_model.dart';
|
||||
import 'package:flux/features/master_data/providers/models/provider_model.dart';
|
||||
import 'package:flux/features/master_data/providers/models/provider_model_extensions.dart';
|
||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||
import 'package:flux/features/operations/data/operations_repository.dart';
|
||||
import 'package:flux/features/operations/models/operation_detail_model.dart';
|
||||
import 'package:flux/features/operations/models/operation_model.dart';
|
||||
import 'package:flux/service_locator.dart';
|
||||
|
||||
@@ -62,7 +61,7 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// NUOVA PRATICA: Creiamo un nuovo Batch UUID
|
||||
// NUOVA PRATICA
|
||||
final currentStore = _sessionCubit.state.currentStore;
|
||||
final newOperation = await _repository.createNewOperation(
|
||||
currentStore: currentStore!,
|
||||
@@ -147,17 +146,6 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
emit(state.copyWith(operation: updated));
|
||||
}
|
||||
|
||||
void updateProvider(ProviderModel? newProvider) {
|
||||
final current = state.operation;
|
||||
|
||||
final updatedOperation = current.copyWith(
|
||||
// Se newProvider è null, passiamo una funzione che ritorna null per sbiancare i campi!
|
||||
provider: () => newProvider,
|
||||
);
|
||||
|
||||
emit(state.copyWith(operation: updatedOperation));
|
||||
}
|
||||
|
||||
void updateCustomer(CustomerModel customer) {
|
||||
final bool isBusiness = customer.isBusiness;
|
||||
final updatedOperation = state.operation.copyWith(
|
||||
@@ -168,103 +156,9 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
emit(state.copyWith(operation: updatedOperation));
|
||||
}
|
||||
|
||||
// --- UTILS ---
|
||||
|
||||
void updateOperationType(
|
||||
String newType, {
|
||||
required List<ProviderModel> allProviders,
|
||||
String? defaultProviderId,
|
||||
}) {
|
||||
// 1. Aggiorniamo il tipo nel modello in canna
|
||||
// (Presumo tu abbia un metodo copyWith o simile)
|
||||
|
||||
// 2. LA LOGICA DI DEFAULT
|
||||
if (defaultProviderId != null) {
|
||||
// Troviamo il provider di default nella lista
|
||||
final defaultProvider = allProviders
|
||||
.where((p) => p.id == defaultProviderId)
|
||||
.firstOrNull;
|
||||
|
||||
if (defaultProvider != null) {
|
||||
// Usiamo l'extension appena creata!
|
||||
if (defaultProvider.supportsOperation(newType)) {
|
||||
updateProvider(defaultProvider);
|
||||
} else {
|
||||
// Se cambi tipo (es. da Mobile a Luce) e il default non lo supporta, sbianchiamo
|
||||
updateProvider(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setTypeWithSmartDefaults({
|
||||
required String newType,
|
||||
required List<ProviderModel> allProviders,
|
||||
String? defaultProviderId,
|
||||
}) {
|
||||
final currentOp = state.operation;
|
||||
|
||||
// -----------------------------------------
|
||||
// 1. SMART DATES: Calcolo Scadenze Default (Invariato)
|
||||
// -----------------------------------------
|
||||
DateTime? defaultDate;
|
||||
final now = DateTime.now();
|
||||
|
||||
if (newType == 'Energy') {
|
||||
defaultDate = DateTime(now.year, now.month + 24, now.day);
|
||||
}
|
||||
if (newType == 'Fin') {
|
||||
defaultDate = DateTime(now.year, now.month + 30, now.day);
|
||||
}
|
||||
if (newType == 'Entertainment') {
|
||||
defaultDate = DateTime(now.year, now.month + 12, now.day);
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
// 2. SMART PROVIDER: Filtro e Auto-Selezione ad Oggetti
|
||||
// -----------------------------------------
|
||||
// Pescatore direttamente l'oggetto dal modello corrente
|
||||
ProviderModel? targetProvider = currentOp.provider;
|
||||
|
||||
// A) Il provider attuale è ancora compatibile col nuovo tipo scelto?
|
||||
if (targetProvider != null && !targetProvider.supportsOperation(newType)) {
|
||||
// Non è più compatibile (es. da TIM fisso passo a Energy). Lo sbianchiamo!
|
||||
targetProvider = null;
|
||||
}
|
||||
|
||||
// B) Se non c'è un provider selezionato, proviamo ad auto-inserire quello di default del negozio
|
||||
if (targetProvider == null && defaultProviderId != null) {
|
||||
final defaultProvider = allProviders
|
||||
.where((p) => p.id == defaultProviderId)
|
||||
.firstOrNull;
|
||||
|
||||
// Controlliamo che il default del negozio supporti questa specifica operazione
|
||||
if (defaultProvider != null &&
|
||||
defaultProvider.supportsOperation(newType)) {
|
||||
targetProvider = defaultProvider;
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
// 3. EMISSIONE DELLO STATO PULITO
|
||||
// -----------------------------------------
|
||||
emit(
|
||||
state.copyWith(
|
||||
operation: currentOp.copyWith(
|
||||
type: newType,
|
||||
subType: '', // Resettiamo il sottotipo per evitare incongruenze
|
||||
expirationDate:
|
||||
defaultDate, // Impostiamo la scadenza di default se calcolata
|
||||
// 🥷 APPLICHIAMO IL TRUCCO NINJA DELLE FUNZIONI
|
||||
// Se targetProvider è null, le funzioni ritorneranno null sbiancando il DB!
|
||||
provider: () => targetProvider,
|
||||
|
||||
// Nota: Per azzerare davvero questi due, ricordati in futuro di applicare
|
||||
// il trucco delle funzioni anche a modelId e modelDisplayName nel modello!
|
||||
modelId: null,
|
||||
modelDisplayName: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
void updateOperationDetailsList(List<OperationDetailModel> details) {
|
||||
final current = state.operation;
|
||||
final updated = current.copyWith(details: details);
|
||||
emit(state.copyWith(operation: updated));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/operations/blocs/operation_details_list_cubit.dart';
|
||||
import 'package:flux/features/operations/blocs/operation_form_cubit.dart';
|
||||
import 'package:flux/features/operations/models/operation_detail_model.dart';
|
||||
import 'package:flux/features/operations/models/operation_model.dart';
|
||||
|
||||
class OperationFormNewScreen extends StatefulWidget {
|
||||
final String? operationId;
|
||||
final OperationModel? operation;
|
||||
|
||||
const OperationFormNewScreen({super.key, this.operationId, this.operation});
|
||||
|
||||
@override
|
||||
State<OperationFormNewScreen> createState() => _OperationFormNewScreenState();
|
||||
}
|
||||
|
||||
class _OperationFormNewScreenState extends State<OperationFormNewScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
bool _isInitialized = false;
|
||||
bool _isClosingIntent = false;
|
||||
|
||||
final _referenceController = TextEditingController();
|
||||
final _notesController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// 1. Lanciamo l'inizializzazione sincrona/asincrona
|
||||
context.read<OperationFormCubit>().initForm(
|
||||
existingOperation: widget.operation,
|
||||
operationId: widget.operationId,
|
||||
);
|
||||
|
||||
// 2. Lettura immediata dello stato (come fatto per il customer!)
|
||||
final currentState = context.read<OperationFormCubit>().state;
|
||||
if (currentState.status == OperationFormStatus.ready && !_isInitialized) {
|
||||
_syncTextControllers(currentState.operation);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_referenceController.dispose();
|
||||
_notesController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _syncTextControllers(OperationModel model) {
|
||||
if (_referenceController.text.isEmpty) {
|
||||
_referenceController.text = model.reference;
|
||||
}
|
||||
if (_notesController.text.isEmpty) {
|
||||
_notesController.text = model.note;
|
||||
}
|
||||
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
void _flushControllersToCubit() {
|
||||
context.read<OperationFormCubit>().updateFields(
|
||||
reference: _referenceController.text,
|
||||
note: _notesController.text,
|
||||
);
|
||||
}
|
||||
|
||||
void _saveOperation({
|
||||
required OperationStatus targetStatus,
|
||||
required bool isClosingIntent,
|
||||
}) {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
_isClosingIntent = isClosingIntent;
|
||||
|
||||
_flushControllersToCubit();
|
||||
context.read<OperationFormCubit>().updateFields(status: targetStatus);
|
||||
context.read<OperationFormCubit>().saveOperation(
|
||||
targetStatus: targetStatus,
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
List<OperationDetailModel> details = context
|
||||
.watch<OperationDetailsListCubit>()
|
||||
.state
|
||||
.details;
|
||||
return BlocConsumer<OperationFormCubit, OperationFormState>(
|
||||
listener: (context, state) {
|
||||
if (state.status == OperationFormStatus.ready && !_isInitialized) {
|
||||
_syncTextControllers(state.operation);
|
||||
}
|
||||
if (state.status == OperationFormStatus.success && _isClosingIntent) {
|
||||
Navigator.of(context).pop();
|
||||
} else if (state.status == OperationFormStatus.failure) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Si è verificato un errore. ${state.errorMessage}'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
if (!_isInitialized &&
|
||||
(widget.operationId != null || widget.operation != null) &&
|
||||
state.status == OperationFormStatus.loading) {
|
||||
return const Scaffold(
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
//TODO controlla se l'utente non ha ancora inserito dati rilevanti e nel caso cancella l'operazione e torna all'home page
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
title: Text(
|
||||
'Addetto selezionato: ${state.operation.staffDisplayName}',
|
||||
),
|
||||
actions: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 10),
|
||||
child: Chip(
|
||||
label: Text(
|
||||
state.operation.status.displayName,
|
||||
style: const TextStyle(color: Colors.white, fontSize: 12),
|
||||
),
|
||||
backgroundColor: _getStatusColor(state.operation.status),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: FocusTraversalGroup(
|
||||
policy: WidgetOrderTraversalPolicy(),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final isUltraWide = constraints.maxWidth > 1400;
|
||||
final isDesktop = constraints.maxWidth > 900;
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: isUltraWide
|
||||
? 1600
|
||||
: isDesktop
|
||||
? 1200
|
||||
: 800,
|
||||
),
|
||||
child: _buildResponsiveLayout(
|
||||
isUltraWide,
|
||||
isDesktop,
|
||||
state,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
bottomNavigationBar: SafeArea(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
decoration: BoxDecoration(
|
||||
color: theme.scaffoldBackgroundColor,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: 0.05),
|
||||
blurRadius: 10,
|
||||
offset: const Offset(0, -3),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: _getStatusColor(
|
||||
state.operation.status,
|
||||
),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
onPressed: () => _saveOperation(
|
||||
targetStatus: state.operation.status,
|
||||
isClosingIntent: true,
|
||||
),
|
||||
child: state.status == OperationFormStatus.saving
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
color: Colors.white,
|
||||
strokeWidth: 2,
|
||||
),
|
||||
)
|
||||
: Text('Salva e chiudi'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user