2026-04-16 11:50:29 +02:00
|
|
|
import 'package:equatable/equatable.dart';
|
2026-04-17 15:37:14 +02:00
|
|
|
import 'package:flux/features/master_data/store/models/store_model.dart';
|
2026-04-16 11:50:29 +02:00
|
|
|
|
|
|
|
|
class ProviderModel extends Equatable {
|
2026-04-17 10:34:23 +02:00
|
|
|
final String? id;
|
2026-05-03 12:05:47 +02:00
|
|
|
final String name;
|
|
|
|
|
final bool landline;
|
|
|
|
|
final bool mobile;
|
|
|
|
|
final bool energy;
|
|
|
|
|
final bool insurance;
|
|
|
|
|
final bool entertainment;
|
|
|
|
|
final bool financing;
|
2026-05-03 10:08:57 +02:00
|
|
|
final bool telepass;
|
2026-05-03 12:05:47 +02:00
|
|
|
final bool other;
|
2026-04-16 11:50:29 +02:00
|
|
|
final bool isActive;
|
|
|
|
|
final String companyId;
|
2026-04-17 15:37:14 +02:00
|
|
|
final List<StoreModel> associatedStores;
|
2026-04-16 11:50:29 +02:00
|
|
|
|
|
|
|
|
const ProviderModel({
|
2026-04-17 10:34:23 +02:00
|
|
|
this.id,
|
2026-05-03 12:05:47 +02:00
|
|
|
required this.name,
|
|
|
|
|
required this.landline,
|
|
|
|
|
required this.mobile,
|
|
|
|
|
required this.energy,
|
|
|
|
|
required this.insurance,
|
|
|
|
|
required this.entertainment,
|
|
|
|
|
required this.financing,
|
2026-05-03 10:08:57 +02:00
|
|
|
required this.telepass,
|
2026-05-03 12:05:47 +02:00
|
|
|
required this.other,
|
2026-04-16 11:50:29 +02:00
|
|
|
required this.isActive,
|
|
|
|
|
required this.companyId,
|
2026-04-17 15:37:14 +02:00
|
|
|
this.associatedStores = const [],
|
2026-04-16 11:50:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
factory ProviderModel.fromMap(Map<String, dynamic> map) {
|
2026-04-17 15:37:14 +02:00
|
|
|
// Estraiamo la lista dalla pivot e poi prendiamo l'oggetto 'store' annidato
|
|
|
|
|
final pivotList = map['associated_stores'] as List?;
|
|
|
|
|
List<StoreModel> stores = [];
|
|
|
|
|
if (pivotList != null) {
|
|
|
|
|
stores = pivotList
|
|
|
|
|
.where((item) => item['store'] != null) // Sicurezza
|
|
|
|
|
.map(
|
|
|
|
|
(item) => StoreModel.fromMap(item['store'] as Map<String, dynamic>),
|
|
|
|
|
)
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
2026-04-16 11:50:29 +02:00
|
|
|
return ProviderModel(
|
|
|
|
|
id: map['id'],
|
2026-05-03 12:05:47 +02:00
|
|
|
name: map['name'],
|
|
|
|
|
landline: map['landline'] ?? false,
|
|
|
|
|
mobile: map['mobile'] ?? false,
|
|
|
|
|
energy: map['energy'] ?? false,
|
|
|
|
|
insurance: map['insurance'] ?? false,
|
|
|
|
|
entertainment: map['entertainment'] ?? false,
|
|
|
|
|
financing: map['financing'] ?? false,
|
2026-05-03 10:08:57 +02:00
|
|
|
telepass: map['telepass'] ?? false,
|
2026-05-03 12:05:47 +02:00
|
|
|
other: map['other'] ?? false,
|
2026-04-16 11:50:29 +02:00
|
|
|
isActive: map['is_active'] ?? true,
|
|
|
|
|
companyId: map['company_id'],
|
2026-04-17 15:37:14 +02:00
|
|
|
associatedStores: stores,
|
2026-04-16 11:50:29 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, dynamic> toMap() {
|
2026-04-17 10:34:23 +02:00
|
|
|
final map = {
|
2026-05-03 12:05:47 +02:00
|
|
|
'name': name,
|
|
|
|
|
'landline': landline,
|
|
|
|
|
'mobile': mobile,
|
|
|
|
|
'energy': energy,
|
|
|
|
|
'insurance': insurance,
|
|
|
|
|
'entertainment': entertainment,
|
|
|
|
|
'financing': financing,
|
2026-05-03 10:08:57 +02:00
|
|
|
'telepass': telepass,
|
2026-05-03 12:05:47 +02:00
|
|
|
'other': other,
|
2026-04-16 11:50:29 +02:00
|
|
|
'is_active': isActive,
|
|
|
|
|
'company_id': companyId,
|
|
|
|
|
};
|
2026-04-17 10:34:23 +02:00
|
|
|
// AGGIUNGIAMO L'ID SOLO SE NON È NULLO
|
|
|
|
|
// Senza questo, l'upsert non sa dove andare a parare
|
|
|
|
|
if (id != null) {
|
|
|
|
|
map['id'] = id!;
|
|
|
|
|
}
|
|
|
|
|
return map;
|
2026-04-16 11:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
List<Object?> get props => [
|
|
|
|
|
id,
|
2026-05-03 12:05:47 +02:00
|
|
|
name,
|
|
|
|
|
landline,
|
|
|
|
|
mobile,
|
|
|
|
|
energy,
|
|
|
|
|
insurance,
|
|
|
|
|
entertainment,
|
|
|
|
|
financing,
|
2026-05-03 10:08:57 +02:00
|
|
|
telepass,
|
2026-05-03 12:05:47 +02:00
|
|
|
other,
|
2026-04-16 11:50:29 +02:00
|
|
|
isActive,
|
|
|
|
|
companyId,
|
2026-04-17 15:37:14 +02:00
|
|
|
associatedStores,
|
2026-04-16 11:50:29 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
ProviderModel copyWith({
|
|
|
|
|
String? id,
|
2026-05-03 12:05:47 +02:00
|
|
|
String? name,
|
|
|
|
|
bool? landline,
|
|
|
|
|
bool? mobile,
|
|
|
|
|
bool? energy,
|
|
|
|
|
bool? insurance,
|
|
|
|
|
bool? entertainment,
|
|
|
|
|
bool? financing,
|
2026-05-03 10:08:57 +02:00
|
|
|
bool? telepass,
|
2026-05-03 12:05:47 +02:00
|
|
|
bool? other,
|
2026-04-16 11:50:29 +02:00
|
|
|
bool? isActive,
|
|
|
|
|
String? companyId,
|
2026-04-17 15:37:14 +02:00
|
|
|
List<StoreModel>? associatedStores,
|
2026-04-16 11:50:29 +02:00
|
|
|
}) {
|
|
|
|
|
return ProviderModel(
|
|
|
|
|
id: id ?? this.id,
|
2026-05-03 12:05:47 +02:00
|
|
|
name: name ?? this.name,
|
|
|
|
|
landline: landline ?? this.landline,
|
|
|
|
|
mobile: mobile ?? this.mobile,
|
|
|
|
|
energy: energy ?? this.energy,
|
|
|
|
|
insurance: insurance ?? this.insurance,
|
|
|
|
|
entertainment: entertainment ?? this.entertainment,
|
|
|
|
|
financing: financing ?? this.financing,
|
2026-05-03 10:08:57 +02:00
|
|
|
telepass: telepass ?? this.telepass,
|
2026-05-03 12:05:47 +02:00
|
|
|
other: other ?? this.other,
|
2026-04-16 11:50:29 +02:00
|
|
|
isActive: isActive ?? this.isActive,
|
|
|
|
|
companyId: companyId ?? this.companyId,
|
2026-04-17 15:37:14 +02:00
|
|
|
associatedStores: associatedStores ?? this.associatedStores,
|
2026-04-16 11:50:29 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|