feat-insert-service (#5)
Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/5 Co-authored-by: mark-cachy <marco@catelli.it> Co-committed-by: mark-cachy <marco@catelli.it>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_bloc.dart';
|
||||
@@ -10,9 +11,9 @@ part 'product_state.dart';
|
||||
|
||||
class ProductCubit extends Cubit<ProductState> {
|
||||
final ProductRepository _repository = GetIt.I<ProductRepository>();
|
||||
final SessionBloc _sessionBloc;
|
||||
final SessionBloc _sessionBloc = GetIt.I<SessionBloc>();
|
||||
|
||||
ProductCubit(this._sessionBloc) : super(const ProductState());
|
||||
ProductCubit() : super(const ProductState());
|
||||
|
||||
// Caricamento iniziale dei Brand
|
||||
Future<void> loadBrands() async {
|
||||
@@ -102,4 +103,55 @@ class ProductCubit extends Cubit<ProductState> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> searchModels(String query) async {
|
||||
if (query.isEmpty) {
|
||||
// Se cancella tutto, potresti voler ricaricare i modelli del brand selezionato
|
||||
// o svuotare la lista. Scegliamo di svuotare per pulizia.
|
||||
emit(state.copyWith(models: []));
|
||||
return;
|
||||
}
|
||||
|
||||
// Opzionale: emetti loading solo se vuoi mostrare una barretta nella UI
|
||||
// emit(state.copyWith(status: ProductStatus.loading));
|
||||
|
||||
try {
|
||||
final results = await _repository.searchModels(query);
|
||||
emit(state.copyWith(status: ProductStatus.success, models: results));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(status: ProductStatus.error, errorMessage: e.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<ModelModel?> quickCreateProduct({
|
||||
required String brandName,
|
||||
required String modelName,
|
||||
}) async {
|
||||
try {
|
||||
await loadBrands();
|
||||
BrandModel? brand = state.brands.firstWhereOrNull(
|
||||
(b) => b.name.toLowerCase() == brandName.toLowerCase(),
|
||||
);
|
||||
// 1. Cerchiamo o creiamo il Brand
|
||||
// (Usa una funzione upsert o una ricerca rapida nel repository)
|
||||
brand ??= await _repository.upsertBrand(
|
||||
BrandModel(name: brandName, companyId: _sessionBloc.state.company!.id),
|
||||
);
|
||||
|
||||
// 2. Creiamo il Modello legato al Brand
|
||||
final newModel = await _repository.upsertModel(
|
||||
ModelModel(brandId: brand.id!, name: modelName),
|
||||
);
|
||||
|
||||
// 3. Aggiorniamo lo stato locale così la lista modelli lo vede subito
|
||||
emit(state.copyWith(models: [newModel, ...state.models]));
|
||||
|
||||
return newModel;
|
||||
} catch (e) {
|
||||
emit(state.copyWith(errorMessage: "Errore creazione rapida: $e"));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,14 @@ import '../models/brand_model.dart';
|
||||
import '../models/model_model.dart';
|
||||
|
||||
class ProductRepository {
|
||||
final SupabaseClient _client = GetIt.I<SupabaseClient>();
|
||||
final SupabaseClient _supabase = GetIt.I<SupabaseClient>();
|
||||
|
||||
// --- BRAND ---
|
||||
|
||||
/// Recupera tutti i brand dell'azienda
|
||||
Future<List<BrandModel>> getBrands(String companyId) async {
|
||||
try {
|
||||
final response = await _client
|
||||
final response = await _supabase
|
||||
.from('brand')
|
||||
.select()
|
||||
.eq('company_id', companyId)
|
||||
@@ -27,7 +27,7 @@ class ProductRepository {
|
||||
/// Crea o aggiorna un brand
|
||||
Future<BrandModel> upsertBrand(BrandModel brand) async {
|
||||
try {
|
||||
final response = await _client
|
||||
final response = await _supabase
|
||||
.from('brand')
|
||||
.upsert(brand.toJson())
|
||||
.select()
|
||||
@@ -44,7 +44,7 @@ class ProductRepository {
|
||||
/// Recupera i modelli di un brand specifico
|
||||
Future<List<ModelModel>> getModelsByBrand(String brandId) async {
|
||||
try {
|
||||
final response = await _client
|
||||
final response = await _supabase
|
||||
.from('model')
|
||||
.select()
|
||||
.eq('brand_id', brandId)
|
||||
@@ -61,7 +61,7 @@ class ProductRepository {
|
||||
/// NOTA: name_with_brand verrà gestito dal trigger SQL che hai lanciato!
|
||||
Future<ModelModel> upsertModel(ModelModel model) async {
|
||||
try {
|
||||
final response = await _client
|
||||
final response = await _supabase
|
||||
.from('model')
|
||||
.upsert(model.toJson())
|
||||
.select()
|
||||
@@ -78,9 +78,24 @@ class ProductRepository {
|
||||
/// Disattiva un brand o un modello (Soft Delete per non rompere le FK delle operazioni passate)
|
||||
Future<void> toggleActiveStatus(String table, String id, bool status) async {
|
||||
try {
|
||||
await _client.from(table).update({'is_active': status}).eq('id', id);
|
||||
await _supabase.from(table).update({'is_active': status}).eq('id', id);
|
||||
} catch (e) {
|
||||
throw 'Errore durante la modifica dello stato';
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<ModelModel>> searchModels(String query) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('model')
|
||||
.select()
|
||||
.ilike('name_with_brand', '%$query%') // Cerca ovunque nel nome
|
||||
.eq('is_active', true)
|
||||
.limit(10); // Non esageriamo con i risultati
|
||||
|
||||
return (response as List).map((m) => ModelModel.fromJson(m)).toList();
|
||||
} catch (e) {
|
||||
throw 'Errore durante la ricerca: $e';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ class ModelModel extends Equatable {
|
||||
const ModelModel({
|
||||
this.id,
|
||||
required this.name,
|
||||
required this.nameWithBrand,
|
||||
this.nameWithBrand = '',
|
||||
required this.brandId,
|
||||
this.isActive = true,
|
||||
this.createdAt,
|
||||
|
||||
111
lib/features/master_data/products/ui/quick_product_dialog.dart
Normal file
111
lib/features/master_data/products/ui/quick_product_dialog.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
|
||||
import 'package:flux/features/master_data/products/models/brand_model.dart';
|
||||
|
||||
class QuickProductDialog extends StatefulWidget {
|
||||
final List<BrandModel> existingBrands;
|
||||
|
||||
const QuickProductDialog({super.key, required this.existingBrands});
|
||||
|
||||
@override
|
||||
State<QuickProductDialog> createState() => _QuickProductDialogState();
|
||||
}
|
||||
|
||||
class _QuickProductDialogState extends State<QuickProductDialog> {
|
||||
final _modelCtrl = TextEditingController();
|
||||
String _selectedBrandName = "";
|
||||
bool _isLoading = false;
|
||||
|
||||
Future<void> _save() async {
|
||||
final NavigatorState navigator = Navigator.of(context);
|
||||
if (_selectedBrandName.isEmpty || _modelCtrl.text.isEmpty) return;
|
||||
|
||||
setState(() => _isLoading = true);
|
||||
|
||||
final newModel = await context.read<ProductCubit>().quickCreateProduct(
|
||||
brandName: _selectedBrandName.trim(),
|
||||
modelName: _modelCtrl.text.trim(),
|
||||
);
|
||||
|
||||
setState(() => _isLoading = false);
|
||||
|
||||
if (context.mounted) {
|
||||
navigator.pop(newModel); // Restituiamo il modello creato
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text("Nuovo Dispositivo"),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// AUTOCOMPLETE PER IL BRAND LOCALE
|
||||
Autocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) {
|
||||
if (textEditingValue.text.isEmpty) {
|
||||
return const Iterable<String>.empty();
|
||||
}
|
||||
final query = textEditingValue.text.toLowerCase();
|
||||
// Filtriamo i brand che contengono la stringa cercata
|
||||
return widget.existingBrands
|
||||
.map((b) => b.name)
|
||||
.where((name) => name.toLowerCase().contains(query));
|
||||
},
|
||||
onSelected: (String selection) {
|
||||
_selectedBrandName = selection;
|
||||
},
|
||||
fieldViewBuilder:
|
||||
(
|
||||
context,
|
||||
textEditingController,
|
||||
focusNode,
|
||||
onFieldSubmitted,
|
||||
) {
|
||||
return TextField(
|
||||
controller: textEditingController,
|
||||
focusNode: focusNode,
|
||||
autofocus: true,
|
||||
decoration: const InputDecoration(
|
||||
labelText: "Marca (es: Apple, Samsung)",
|
||||
hintText: "Inizia a scrivere...",
|
||||
),
|
||||
onChanged: (val) => _selectedBrandName = val,
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _modelCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: "Modello (es: iPhone 15 Pro)",
|
||||
),
|
||||
textInputAction: TextInputAction.done,
|
||||
onSubmitted: (_) => _save(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text("Annulla"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _save,
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text("Crea"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,15 +7,17 @@ import 'package:get_it/get_it.dart';
|
||||
import '../models/provider_model.dart';
|
||||
|
||||
class ProvidersState extends Equatable {
|
||||
final List<ProviderModel> allProviders; // Tutti i provider della company
|
||||
final List<String>
|
||||
associatedIds; // ID dei provider attivi nello store selezionato
|
||||
final List<ProviderModel> allProviders;
|
||||
final List<String> associatedIds;
|
||||
// NUOVO CAMPO: Lista dei provider pronti per essere usati nel form pratiche
|
||||
final List<ProviderModel> activeProviders;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const ProvidersState({
|
||||
this.allProviders = const [],
|
||||
this.associatedIds = const [],
|
||||
this.activeProviders = const [], // Inizializza
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
@@ -23,14 +25,18 @@ class ProvidersState extends Equatable {
|
||||
ProvidersState copyWith({
|
||||
List<ProviderModel>? allProviders,
|
||||
List<String>? associatedIds,
|
||||
List<ProviderModel>? activeProviders, // Aggiungi qui
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return ProvidersState(
|
||||
allProviders: allProviders ?? this.allProviders,
|
||||
associatedIds: associatedIds ?? this.associatedIds,
|
||||
activeProviders: activeProviders ?? this.activeProviders, // Aggiungi qui
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage,
|
||||
errorMessage:
|
||||
errorMessage ??
|
||||
this.errorMessage, // Correzione bug: mancava "?? this.errorMessage" nel tuo originale
|
||||
);
|
||||
}
|
||||
|
||||
@@ -38,6 +44,7 @@ class ProvidersState extends Equatable {
|
||||
List<Object?> get props => [
|
||||
allProviders,
|
||||
associatedIds,
|
||||
activeProviders, // Aggiungi qui
|
||||
isLoading,
|
||||
errorMessage,
|
||||
];
|
||||
@@ -45,9 +52,9 @@ class ProvidersState extends Equatable {
|
||||
|
||||
class ProvidersCubit extends Cubit<ProvidersState> {
|
||||
final ProviderRepository _repository = GetIt.I<ProviderRepository>();
|
||||
final SessionBloc _sessionBloc;
|
||||
final SessionBloc _sessionBloc = GetIt.I<SessionBloc>();
|
||||
|
||||
ProvidersCubit(this._sessionBloc) : super(const ProvidersState());
|
||||
ProvidersCubit() : super(const ProvidersState());
|
||||
|
||||
// Carica i provider della company e quelli associati a uno store specifico
|
||||
Future<void> loadProviders(StoreModel? store) async {
|
||||
@@ -74,6 +81,23 @@ class ProvidersCubit extends Cubit<ProvidersState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadActiveProvidersForStore(String storeId) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
final activeList = await _repository.fetchActiveProvidersForStore(
|
||||
storeId,
|
||||
);
|
||||
emit(state.copyWith(activeProviders: activeList, isLoading: false));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: "Errore caricamento gestori: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Aggiunge o rimuove l'associazione con lo store
|
||||
Future<void> toggleProviderAssociation({
|
||||
required String providerId,
|
||||
|
||||
@@ -9,6 +9,7 @@ class ProviderModel extends Equatable {
|
||||
final bool energia;
|
||||
final bool assicurazioni;
|
||||
final bool intrattenimento;
|
||||
final bool finanziamenti;
|
||||
final bool altro;
|
||||
final bool isActive;
|
||||
final String companyId;
|
||||
@@ -22,6 +23,7 @@ class ProviderModel extends Equatable {
|
||||
required this.energia,
|
||||
required this.assicurazioni,
|
||||
required this.intrattenimento,
|
||||
required this.finanziamenti,
|
||||
required this.altro,
|
||||
required this.isActive,
|
||||
required this.companyId,
|
||||
@@ -48,6 +50,7 @@ class ProviderModel extends Equatable {
|
||||
energia: map['energia'] ?? false,
|
||||
assicurazioni: map['assicurazioni'] ?? false,
|
||||
intrattenimento: map['intrattenimento'] ?? false,
|
||||
finanziamenti: map['finanziamenti'] ?? false,
|
||||
altro: map['altro'] ?? false,
|
||||
isActive: map['is_active'] ?? true,
|
||||
companyId: map['company_id'],
|
||||
@@ -63,6 +66,7 @@ class ProviderModel extends Equatable {
|
||||
'energia': energia,
|
||||
'assicurazioni': assicurazioni,
|
||||
'intrattenimento': intrattenimento,
|
||||
'finanziamenti': finanziamenti,
|
||||
'altro': altro,
|
||||
'is_active': isActive,
|
||||
'company_id': companyId,
|
||||
@@ -84,6 +88,7 @@ class ProviderModel extends Equatable {
|
||||
energia,
|
||||
assicurazioni,
|
||||
intrattenimento,
|
||||
finanziamenti,
|
||||
altro,
|
||||
isActive,
|
||||
companyId,
|
||||
@@ -98,6 +103,7 @@ class ProviderModel extends Equatable {
|
||||
bool? energia,
|
||||
bool? assicurazioni,
|
||||
bool? intrattenimento,
|
||||
bool? finanziamenti,
|
||||
bool? altro,
|
||||
bool? isActive,
|
||||
String? companyId,
|
||||
@@ -111,6 +117,7 @@ class ProviderModel extends Equatable {
|
||||
energia: energia ?? this.energia,
|
||||
assicurazioni: assicurazioni ?? this.assicurazioni,
|
||||
intrattenimento: intrattenimento ?? this.intrattenimento,
|
||||
finanziamenti: finanziamenti ?? this.finanziamenti,
|
||||
altro: altro ?? this.altro,
|
||||
isActive: isActive ?? this.isActive,
|
||||
companyId: companyId ?? this.companyId,
|
||||
|
||||
@@ -20,6 +20,7 @@ class _ProviderFormSheetState extends State<ProviderFormSheet> {
|
||||
late bool _energia;
|
||||
late bool _assicurazioni;
|
||||
late bool _intrattenimento;
|
||||
late bool _finanziamenti;
|
||||
late bool _altro;
|
||||
late bool _isActive;
|
||||
final List<String> _tempSelectedStoreIds =
|
||||
@@ -38,6 +39,7 @@ class _ProviderFormSheetState extends State<ProviderFormSheet> {
|
||||
_energia = p?.energia ?? false;
|
||||
_assicurazioni = p?.assicurazioni ?? false;
|
||||
_intrattenimento = p?.intrattenimento ?? false;
|
||||
_finanziamenti = p?.finanziamenti ?? false;
|
||||
_altro = p?.altro ?? false;
|
||||
_isActive = p?.isActive ?? true;
|
||||
}
|
||||
@@ -61,6 +63,7 @@ class _ProviderFormSheetState extends State<ProviderFormSheet> {
|
||||
energia: _energia,
|
||||
assicurazioni: _assicurazioni,
|
||||
intrattenimento: _intrattenimento,
|
||||
finanziamenti: _finanziamenti,
|
||||
altro: _altro,
|
||||
isActive: _isActive,
|
||||
companyId:
|
||||
@@ -130,6 +133,11 @@ class _ProviderFormSheetState extends State<ProviderFormSheet> {
|
||||
_intrattenimento,
|
||||
(v) => setState(() => _intrattenimento = v),
|
||||
),
|
||||
_buildSwitch(
|
||||
"Finanziamenti",
|
||||
_finanziamenti,
|
||||
(v) => setState(() => _finanziamenti = v),
|
||||
),
|
||||
_buildSwitch(
|
||||
"Altro/Accessori",
|
||||
_altro,
|
||||
|
||||
@@ -10,9 +10,9 @@ part 'staff_state.dart';
|
||||
|
||||
class StaffCubit extends Cubit<StaffState> {
|
||||
final StaffRepository _repository = GetIt.I.get<StaffRepository>();
|
||||
final SessionBloc _sessionBloc;
|
||||
final SessionBloc _sessionBloc = GetIt.I<SessionBloc>();
|
||||
|
||||
StaffCubit(this._sessionBloc) : super(const StaffState());
|
||||
StaffCubit() : super(const StaffState());
|
||||
|
||||
// Carica tutto lo staff della compagnia
|
||||
Future<void> loadAllStaff() async {
|
||||
|
||||
@@ -13,9 +13,9 @@ part 'store_state.dart';
|
||||
class StoreCubit extends Cubit<StoreState> {
|
||||
final StoreRepository _repository = GetIt.I<StoreRepository>();
|
||||
final StaffRepository _staffRepository = GetIt.I<StaffRepository>();
|
||||
final SessionBloc _sessionBloc;
|
||||
final SessionBloc _sessionBloc = GetIt.I<SessionBloc>();
|
||||
|
||||
StoreCubit(this._sessionBloc) : super(const StoreState(stores: []));
|
||||
StoreCubit() : super(const StoreState(stores: []));
|
||||
|
||||
Future<void> createStore(final StoreModel store) async {
|
||||
emit(state.copyWith(status: StoreStatus.loading));
|
||||
|
||||
Reference in New Issue
Block a user