Files
flux/lib/features/operations/models/operation_model.dart
T
brontomark 9374f4ab46
Build and Release FLUX (Multi-Platform) / build-android (push) Successful in 4m30s
Build and Release FLUX (Multi-Platform) / build-windows (push) Successful in 9m21s
Build and Release FLUX (Multi-Platform) / build-linux (push) Successful in 2m29s
Build and Release FLUX (Multi-Platform) / build-web (push) Successful in 2m17s
nuova strategia per le liste delle operazioni e correzione bug indirizzo predefinito di spedizione dei provider
2026-08-18 19:24:53 +02:00

193 lines
5.4 KiB
Dart

import 'package:equatable/equatable.dart';
import 'package:flux/core/enums_and_consts/consts.dart';
import 'package:flux/core/utils/extensions.dart';
import 'package:flux/features/attachments/models/attachment_model.dart';
import 'package:flux/features/customers/models/customer_model.dart';
import 'package:flux/features/operations/models/operation_detail_model.dart';
enum OperationStatus {
success('success', 'OK'),
waitingForAction('waiting_for_action', 'In attesa di azione'),
waitingForSupport('waiting_for_support', 'In attesa di supporto'),
failure('failure', 'KO'),
draft('draft', 'Bozza');
static OperationStatus fromString(String value) {
final normalizedValue = value.replaceAll('_', '').toLowerCase();
return OperationStatus.values.firstWhere(
(e) => e.name.toLowerCase() == normalizedValue,
);
}
final String supabaseName;
final String displayName;
const OperationStatus(this.supabaseName, this.displayName);
}
class OperationModel extends Equatable {
final String? id;
final DateTime? createdAt;
final String note;
final bool showInDashboard;
final String companyId;
final String storeId;
final String? storeDisplayName;
final String? staffId;
final String? staffDisplayName;
final OperationStatus status;
final String? customerId;
final CustomerModel? customer;
final String reference;
final bool isBusiness;
// ALLEGATI (Aggiunto)
final List<AttachmentModel> attachments;
// DETTAGLI OPERAZIONE (Aggiunto)
final List<OperationDetailModel> details;
const OperationModel({
this.id,
this.createdAt,
this.note = '',
this.showInDashboard = true,
required this.companyId,
this.storeId = '',
this.storeDisplayName,
this.staffId,
this.staffDisplayName,
this.status = OperationStatus.success,
this.customerId,
this.customer,
this.reference = '',
this.attachments = const [],
this.details = const [],
this.isBusiness = false,
});
OperationModel copyWith({
String? id,
DateTime? createdAt,
String? note,
bool? showInDashboard,
String? companyId,
String? storeId,
String? storeDisplayName,
String? staffId,
String? staffDisplayName,
OperationStatus? status,
String? customerId,
CustomerModel? customer,
String? reference,
List<AttachmentModel>? attachments,
List<OperationDetailModel>? details,
bool? isBusiness,
}) => OperationModel(
id: id ?? this.id,
createdAt: createdAt ?? this.createdAt,
note: note ?? this.note,
showInDashboard: showInDashboard ?? this.showInDashboard,
companyId: companyId ?? this.companyId,
storeId: storeId ?? this.storeId,
storeDisplayName: storeDisplayName ?? this.storeDisplayName,
staffId: staffId ?? this.staffId,
staffDisplayName: staffDisplayName ?? this.staffDisplayName,
status: status ?? this.status,
customerId: customerId ?? this.customerId,
customer: customer ?? this.customer,
reference: reference ?? this.reference,
attachments: attachments ?? this.attachments,
details: details ?? this.details,
isBusiness: isBusiness ?? this.isBusiness,
);
@override
List<Object?> get props => [
id,
createdAt,
note,
showInDashboard,
companyId,
storeId,
storeDisplayName,
staffId,
staffDisplayName,
status,
customerId,
customer,
reference,
attachments,
details,
isBusiness,
];
factory OperationModel.empty() {
return OperationModel(
id: null,
createdAt: null,
showInDashboard: false,
companyId: '',
details: const [],
);
}
factory OperationModel.fromMap(Map<String, dynamic> map) {
return OperationModel(
id: map['id'] as String?,
createdAt: map['created_at'] != null
? DateTime.parse(map['created_at'])
: null,
note: map['note'] as String? ?? '',
showInDashboard: map['show_in_dashboard'] as bool? ?? true,
companyId: map['company_id'] as String,
storeId:
map['store_id'] as String? ??
'', // Questo è non-nullable nella tua classe
storeDisplayName: (map[Tables.stores]?['name'] as String?)?.myFormat(),
staffId: map['staff_id'] as String?,
staffDisplayName: (map[Tables.staffMembers]?['name'] as String?)
?.myFormat(),
status: OperationStatus.fromString(map['status'] ?? 'draft'),
customerId: map['customer_id'] as String?,
customer: map[Tables.customers] != null
? CustomerModel.fromMap(map[Tables.customers] as Map<String, dynamic>)
: null,
attachments:
(map[Tables.attachments] as List?)
?.map((x) => AttachmentModel.fromMap(x))
.toList() ??
const [],
details:
(map[Tables.operationDetails] as List?)
?.map(
(x) => OperationDetailModel.fromMap(x as Map<String, dynamic>),
)
.toList() ??
const [],
reference: map['reference'] as String? ?? '',
isBusiness: map['is_business'] as bool? ?? false,
);
}
Map<String, dynamic> toMap() {
return {
if (id != null) 'id': id,
'note': note,
'show_in_dashboard': showInDashboard,
'company_id': companyId,
'store_id': storeId,
if (staffId != null) 'staff_id': staffId,
'status': status.supabaseName,
if (customerId != null) 'customer_id': customerId,
'reference': reference,
'is_business': isBusiness,
};
}
}