@@ -1,200 +1,200 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/utils/extensions.dart';
|
||||
import 'package:flux/features/operations/models/energy_operation_model.dart';
|
||||
import 'package:flux/features/operations/models/entertainment_operation_model.dart';
|
||||
import 'package:flux/features/operations/models/fin_operation_model.dart';
|
||||
import 'package:flux/features/operations/models/operation_file_model.dart'; // <-- Aggiunto Import
|
||||
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');
|
||||
|
||||
static OperationStatus fromString(String value) {
|
||||
final normalizedValue = value.replaceAll('_', '').toLowerCase();
|
||||
return OperationStatus.values.firstWhere(
|
||||
(e) => e.name.toLowerCase() == normalizedValue,
|
||||
);
|
||||
}
|
||||
|
||||
final String supabaseName;
|
||||
|
||||
const OperationStatus(this.supabaseName);
|
||||
}
|
||||
|
||||
class OperationModel extends Equatable {
|
||||
final String? id;
|
||||
final DateTime? createdAt;
|
||||
final String storeId;
|
||||
final String? employeeId;
|
||||
final String? customerId;
|
||||
final String number;
|
||||
final bool isBozza;
|
||||
final String type;
|
||||
final String? providerId;
|
||||
final String? modelId;
|
||||
final String? description;
|
||||
final DateTime? expirationDate;
|
||||
final String note;
|
||||
final bool resultOk;
|
||||
final String? customerDisplayName;
|
||||
final bool showInDashboard;
|
||||
final String batchUuid;
|
||||
final String companyId;
|
||||
|
||||
// Telefonia
|
||||
final int al;
|
||||
final int mnp;
|
||||
final int nip;
|
||||
final int unica;
|
||||
final int telepass;
|
||||
|
||||
// Moduli (Liste)
|
||||
final List<EnergyOperationModel> energyOperations;
|
||||
final List<FinOperationModel> finOperations;
|
||||
final List<EntertainmentOperationModel> entertainmentOperations;
|
||||
final String storeId;
|
||||
final int quantity;
|
||||
final String? staffId;
|
||||
final String staffDisplayName;
|
||||
final String? lastCampaignId;
|
||||
final OperationStatus status;
|
||||
final String? customerId;
|
||||
final String customerDisplayName;
|
||||
final String reference;
|
||||
|
||||
// ALLEGATI (Aggiunto)
|
||||
final List<OperationFileModel> files;
|
||||
final List<AttachmentModel> attachments;
|
||||
|
||||
const OperationModel({
|
||||
this.id,
|
||||
this.createdAt,
|
||||
required this.storeId,
|
||||
this.employeeId,
|
||||
this.customerId,
|
||||
required this.number,
|
||||
this.isBozza = true,
|
||||
this.type = '',
|
||||
this.providerId,
|
||||
this.modelId,
|
||||
this.description,
|
||||
this.expirationDate,
|
||||
this.note = '',
|
||||
this.resultOk = true,
|
||||
this.al = 0,
|
||||
this.mnp = 0,
|
||||
this.nip = 0,
|
||||
this.unica = 0,
|
||||
this.telepass = 0,
|
||||
this.energyOperations = const [],
|
||||
this.finOperations = const [],
|
||||
this.entertainmentOperations = const [],
|
||||
this.files = const [], // <-- Aggiunto default vuoto
|
||||
this.customerDisplayName,
|
||||
this.showInDashboard = true,
|
||||
this.batchUuid = '',
|
||||
required this.companyId,
|
||||
this.storeId = '',
|
||||
this.quantity = 1,
|
||||
this.staffId,
|
||||
this.staffDisplayName = '',
|
||||
this.lastCampaignId,
|
||||
this.status = OperationStatus.draft,
|
||||
this.customerId,
|
||||
this.customerDisplayName = '',
|
||||
this.reference = '',
|
||||
this.attachments = const [],
|
||||
});
|
||||
|
||||
OperationModel copyWith({
|
||||
String? id,
|
||||
DateTime? createdAt,
|
||||
String? storeId,
|
||||
String? employeeId,
|
||||
String? customerId,
|
||||
String? number,
|
||||
bool? isBozza,
|
||||
String? type,
|
||||
String? providerId,
|
||||
String? modelId,
|
||||
String? description,
|
||||
DateTime? expirationDate,
|
||||
String? note,
|
||||
bool? resultOk,
|
||||
int? al,
|
||||
int? mnp,
|
||||
int? nip,
|
||||
int? unica,
|
||||
int? telepass,
|
||||
List<EnergyOperationModel>? energyOperations,
|
||||
List<FinOperationModel>? finOperations,
|
||||
List<EntertainmentOperationModel>? entertainmentOperations,
|
||||
List<OperationFileModel>? files, // <-- Aggiunto
|
||||
String? customerDisplayName,
|
||||
bool? showInDashboard,
|
||||
String? batchUuid,
|
||||
String? companyId,
|
||||
}) {
|
||||
return OperationModel(
|
||||
id: id ?? this.id,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
storeId: storeId ?? this.storeId,
|
||||
employeeId: employeeId ?? this.employeeId,
|
||||
customerId: customerId ?? this.customerId,
|
||||
number: number ?? this.number,
|
||||
isBozza: isBozza ?? this.isBozza,
|
||||
note: note ?? this.note,
|
||||
resultOk: resultOk ?? this.resultOk,
|
||||
al: al ?? this.al,
|
||||
mnp: mnp ?? this.mnp,
|
||||
nip: nip ?? this.nip,
|
||||
unica: unica ?? this.unica,
|
||||
telepass: telepass ?? this.telepass,
|
||||
energyOperations: energyOperations ?? this.energyOperations,
|
||||
finOperations: finOperations ?? this.finOperations,
|
||||
entertainmentOperations:
|
||||
entertainmentOperations ?? this.entertainmentOperations,
|
||||
files: files ?? this.files, // <-- Aggiunto
|
||||
customerDisplayName: customerDisplayName ?? this.customerDisplayName,
|
||||
companyId: companyId ?? this.companyId,
|
||||
);
|
||||
}
|
||||
String? storeId,
|
||||
int? quantity,
|
||||
String? staffId,
|
||||
String? staffDisplayName,
|
||||
String? lastCampaignId,
|
||||
OperationStatus? status,
|
||||
String? customerId,
|
||||
String? customerDisplayName,
|
||||
String? reference,
|
||||
List<AttachmentModel>? attachments,
|
||||
}) => OperationModel(
|
||||
id: id ?? this.id,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
type: type ?? this.type,
|
||||
providerId: providerId ?? this.providerId,
|
||||
modelId: modelId ?? this.modelId,
|
||||
description: description ?? this.description,
|
||||
expirationDate: expirationDate ?? this.expirationDate,
|
||||
note: note ?? this.note,
|
||||
showInDashboard: showInDashboard ?? this.showInDashboard,
|
||||
batchUuid: batchUuid ?? this.batchUuid,
|
||||
companyId: companyId ?? this.companyId,
|
||||
storeId: storeId ?? this.storeId,
|
||||
quantity: quantity ?? this.quantity,
|
||||
staffId: staffId ?? this.staffId,
|
||||
staffDisplayName: staffDisplayName ?? this.staffDisplayName,
|
||||
lastCampaignId: lastCampaignId ?? this.lastCampaignId,
|
||||
status: status ?? this.status,
|
||||
customerId: customerId ?? this.customerId,
|
||||
customerDisplayName: customerDisplayName ?? this.customerDisplayName,
|
||||
reference: reference ?? this.reference,
|
||||
attachments: attachments ?? this.attachments,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
id,
|
||||
createdAt,
|
||||
storeId,
|
||||
employeeId,
|
||||
customerId,
|
||||
number,
|
||||
isBozza,
|
||||
type,
|
||||
providerId,
|
||||
modelId,
|
||||
description,
|
||||
expirationDate,
|
||||
note,
|
||||
resultOk,
|
||||
al,
|
||||
mnp,
|
||||
nip,
|
||||
unica,
|
||||
telepass,
|
||||
energyOperations,
|
||||
finOperations,
|
||||
entertainmentOperations,
|
||||
files, // <-- Aggiunto
|
||||
customerDisplayName,
|
||||
showInDashboard,
|
||||
batchUuid,
|
||||
companyId,
|
||||
storeId,
|
||||
quantity,
|
||||
staffId,
|
||||
staffDisplayName,
|
||||
lastCampaignId,
|
||||
status,
|
||||
customerId,
|
||||
customerDisplayName,
|
||||
reference,
|
||||
attachments,
|
||||
];
|
||||
|
||||
factory OperationModel.empty({required String companyId}) {
|
||||
return OperationModel(id: null, createdAt: null, companyId: companyId);
|
||||
}
|
||||
|
||||
factory OperationModel.fromMap(Map<String, dynamic> map) {
|
||||
return OperationModel(
|
||||
id: map['id'].toString(),
|
||||
id: map['id'],
|
||||
createdAt: map['created_at'] != null
|
||||
? DateTime.parse(map['created_at'])
|
||||
: DateTime.now(),
|
||||
storeId: map['store_id'] ?? '',
|
||||
employeeId: map['employee_id']?.toString(),
|
||||
customerId: map['customer_id']?.toString(),
|
||||
number: map['number']?.toString() ?? '',
|
||||
isBozza: map['bozza'] ?? true,
|
||||
note: map['note'] ?? '',
|
||||
resultOk: map['result_ok'] ?? true,
|
||||
al: map['al'] ?? 0,
|
||||
mnp: map['mnp'] ?? 0,
|
||||
nip: map['nip'] ?? 0,
|
||||
unica: map['unica'] ?? 0,
|
||||
telepass: map['telepass'] ?? 0,
|
||||
|
||||
// Estrazione sicura liste collegate
|
||||
energyOperations:
|
||||
(map['energy_operation'] as List?)
|
||||
?.map((x) => EnergyOperationModel.fromMap(x))
|
||||
.toList() ??
|
||||
const [],
|
||||
finOperations:
|
||||
(map['fin_operation'] as List?)
|
||||
?.map((x) => FinOperationModel.fromMap(x))
|
||||
.toList() ??
|
||||
const [],
|
||||
entertainmentOperations:
|
||||
(map['entertainment_operation'] as List?)
|
||||
?.map((x) => EntertainmentOperationModel.fromMap(x))
|
||||
.toList() ??
|
||||
const [],
|
||||
|
||||
// I FILE! (Assicurati che la foreign key su Supabase usi esattamente questo nome)
|
||||
files:
|
||||
(map['operation_file'] as List?)
|
||||
?.map((x) => OperationFileModel.fromMap(x))
|
||||
.toList() ??
|
||||
const [],
|
||||
|
||||
// Display name del cliente con fallback
|
||||
customerDisplayName: map['customer'] != null
|
||||
? "${map['customer']['nome'] ?? ''}".myFormat()
|
||||
: "Cliente non assegnato",
|
||||
: null,
|
||||
type: map['type'] as String? ?? '',
|
||||
providerId: map['provider_id'] as String? ?? '',
|
||||
modelId: map['model_id'] as String? ?? '',
|
||||
description: map['description'] as String? ?? '',
|
||||
expirationDate: map['expiration_date'] != null
|
||||
? DateTime.parse(map['expiration_date'])
|
||||
: null,
|
||||
note: map['note'] as String? ?? '',
|
||||
showInDashboard: map['show_in_dashboard'] as bool,
|
||||
batchUuid: map['batch_uuid'] as String,
|
||||
companyId: map['company_id'] as String,
|
||||
storeId: map['store_id'] as String? ?? '',
|
||||
quantity: map['quantity'] is int
|
||||
? map['quantity']
|
||||
: int.tryParse(map['quantity']?.toString() ?? '0') ?? 0,
|
||||
staffId: map['staff_id'] as String? ?? '',
|
||||
lastCampaignId: map['last_campaign_id'] as String? ?? '',
|
||||
status: OperationStatus.fromString(map['status']),
|
||||
customerId: map['customer_id'] as String? ?? '',
|
||||
reference: map['reference'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'store_id': storeId,
|
||||
'employee_id': employeeId,
|
||||
'customer_id': customerId,
|
||||
'number': number,
|
||||
'bozza': isBozza,
|
||||
'type': type,
|
||||
'provider_id': providerId,
|
||||
'model_id': modelId,
|
||||
'description': description,
|
||||
if (expirationDate != null)
|
||||
'expiration_date': expirationDate!.toIso8601String(),
|
||||
'note': note,
|
||||
'result_ok': resultOk,
|
||||
'al': al,
|
||||
'mnp': mnp,
|
||||
'nip': nip,
|
||||
'unica': unica,
|
||||
'telepass': telepass,
|
||||
'show_in_dashboard': showInDashboard,
|
||||
'batch_uuid': batchUuid,
|
||||
'company_id': companyId,
|
||||
// Le liste non le mettiamo qui perché vanno in tabelle diverse!
|
||||
'store_id': storeId,
|
||||
'quantity': quantity,
|
||||
if (staffId != null) 'staff_id': staffId,
|
||||
if (lastCampaignId != null) 'last_campaign_id': lastCampaignId,
|
||||
'status': status.supabaseName,
|
||||
if (customerId != null) 'customer_id': customerId,
|
||||
'reference': reference,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user