Add Providers Management to Master Data Hub and Enhance Provider Model
This commit is contained in:
@@ -108,7 +108,10 @@ class ProvidersCubit extends Cubit<ProvidersState> {
|
||||
Future<void> saveProvider(ProviderModel provider) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
await _repository.saveProvider(provider);
|
||||
final providerWithCompanyId = provider.copyWith(
|
||||
companyId: _sessionBloc.state.company!.id,
|
||||
);
|
||||
await _repository.saveProvider(providerWithCompanyId);
|
||||
// Ricarichiamo la lista per vedere le modifiche
|
||||
await loadProviders(null);
|
||||
} catch (e) {
|
||||
|
||||
@@ -40,14 +40,29 @@ class ProviderRepository {
|
||||
// Recupera tutti i provider di una company (per la lista generale)
|
||||
Future<List<ProviderModel>> fetchAllCompanyProviders(String companyId) async {
|
||||
try {
|
||||
// La magia è qui: selezioniamo tutto e chiediamo il conteggio (count)
|
||||
// della tabella pivot providers_in_stores
|
||||
final response = await _supabase
|
||||
.from('provider')
|
||||
.select()
|
||||
.eq('company_id', companyId);
|
||||
.select('''
|
||||
*,
|
||||
providers_in_stores(count)
|
||||
''')
|
||||
.eq('company_id', companyId)
|
||||
.order('nome');
|
||||
|
||||
return (response as List).map((m) => ProviderModel.fromMap(m)).toList();
|
||||
return (response as List).map((m) {
|
||||
// Estraiamo il conteggio dalla struttura restituita da Supabase
|
||||
// La risposta per ogni riga sarà tipo: { "id": "...", "providers_in_stores": [{"count": 5}] }
|
||||
final storesList = m['providers_in_stores'] as List?;
|
||||
final count = (storesList != null && storesList.isNotEmpty)
|
||||
? storesList[0]['count'] as int
|
||||
: 0;
|
||||
|
||||
return ProviderModel.fromMap(m).copyWith(storesCount: count);
|
||||
}).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Errore fetch provider: $e');
|
||||
throw Exception('Errore fetch all providers: $e');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ class ProviderModel extends Equatable {
|
||||
final bool altro;
|
||||
final bool isActive;
|
||||
final String companyId;
|
||||
final int storesCount;
|
||||
|
||||
const ProviderModel({
|
||||
this.id,
|
||||
@@ -23,6 +24,7 @@ class ProviderModel extends Equatable {
|
||||
required this.altro,
|
||||
required this.isActive,
|
||||
required this.companyId,
|
||||
this.storesCount = 0, // Numero di store associati, default a 0
|
||||
});
|
||||
|
||||
factory ProviderModel.fromMap(Map<String, dynamic> map) {
|
||||
@@ -37,6 +39,11 @@ class ProviderModel extends Equatable {
|
||||
altro: map['altro'] ?? false,
|
||||
isActive: map['is_active'] ?? true,
|
||||
companyId: map['company_id'],
|
||||
storesCount:
|
||||
map['providers_in_stores'] != null &&
|
||||
map['providers_in_stores'].isNotEmpty
|
||||
? map['providers_in_stores'][0]['count'] as int
|
||||
: 0, // Assumiamo che l'API possa restituire questo campo
|
||||
);
|
||||
}
|
||||
|
||||
@@ -72,6 +79,7 @@ class ProviderModel extends Equatable {
|
||||
altro,
|
||||
isActive,
|
||||
companyId,
|
||||
storesCount,
|
||||
];
|
||||
|
||||
ProviderModel copyWith({
|
||||
@@ -85,6 +93,7 @@ class ProviderModel extends Equatable {
|
||||
bool? altro,
|
||||
bool? isActive,
|
||||
String? companyId,
|
||||
int? storesCount,
|
||||
}) {
|
||||
return ProviderModel(
|
||||
id: id ?? this.id,
|
||||
@@ -97,6 +106,7 @@ class ProviderModel extends Equatable {
|
||||
altro: altro ?? this.altro,
|
||||
isActive: isActive ?? this.isActive,
|
||||
companyId: companyId ?? this.companyId,
|
||||
storesCount: storesCount ?? this.storesCount,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,54 @@ class _ProvidersMasterDataScreenState extends State<ProvidersMasterDataScreen> {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (state.allProviders.isEmpty) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// Un'icona grande e stilizzata
|
||||
Icon(
|
||||
Icons.handshake_outlined,
|
||||
size: 80,
|
||||
color: Colors.indigo.withValues(alpha: 0.3),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text(
|
||||
"Nessun Provider configurato",
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
"Aggiungi i partner con cui collabori (es. Enel, WindTre, ecc.) per poter gestire i servizi e i mandati nei tuoi negozi.",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
// Un bel bottone centrato per chi non vuole usare il FAB in basso
|
||||
ElevatedButton.icon(
|
||||
onPressed: () => _showProviderForm(context, null),
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text("AGGIUNGI IL PRIMO PROVIDER"),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.indigo,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24,
|
||||
vertical: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
itemCount: state.allProviders.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
@@ -49,7 +97,9 @@ class _ProvidersMasterDataScreenState extends State<ProvidersMasterDataScreen> {
|
||||
provider.nome,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: _buildProviderBadges(provider),
|
||||
subtitle: _buildCardSubtitle(
|
||||
provider,
|
||||
), // Una funzione che costruisce il sottotitolo con i badge
|
||||
trailing: const Icon(Icons.edit_outlined),
|
||||
onTap: () => _showProviderForm(context, provider),
|
||||
);
|
||||
@@ -64,6 +114,30 @@ class _ProvidersMasterDataScreenState extends State<ProvidersMasterDataScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCardSubtitle(ProviderModel provider) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildProviderBadges(provider), // I badge che abbiamo fatto prima
|
||||
const SizedBox(height: 4),
|
||||
BlocBuilder<ProvidersCubit, ProvidersState>(
|
||||
builder: (context, state) {
|
||||
// Un piccolo testo che indica il numero di store associati
|
||||
// Nota: Dovrai assicurarti che il Cubit carichi queste info
|
||||
return Text(
|
||||
"Disponibile in ${provider.storesCount} negozi",
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.indigo.withValues(alpha: 0.7),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
_buildProviderBadges(provider),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// Visualizza i servizi abilitati per quel provider nella lista
|
||||
Widget _buildProviderBadges(ProviderModel p) {
|
||||
return Wrap(
|
||||
|
||||
Reference in New Issue
Block a user