This commit is contained in:
Phuoc Nguyen
2025-10-10 16:38:07 +07:00
parent e5b247d622
commit b94c158004
177 changed files with 25080 additions and 152 deletions

View File

@@ -0,0 +1,123 @@
import 'package:hive_ce/hive.dart';
import 'package:retail/core/constants/storage_constants.dart';
import 'package:retail/features/home/data/models/cart_item_model.dart';
part 'transaction_model.g.dart';
/// Transaction model with Hive CE type adapter
@HiveType(typeId: StorageConstants.transactionTypeId)
class TransactionModel extends HiveObject {
/// Unique transaction identifier
@HiveField(0)
final String id;
/// List of cart items in this transaction
@HiveField(1)
final List<CartItemModel> items;
/// Subtotal amount (before tax and discount)
@HiveField(2)
final double subtotal;
/// Tax amount
@HiveField(3)
final double tax;
/// Discount amount
@HiveField(4)
final double discount;
/// Total amount (subtotal + tax - discount)
@HiveField(5)
final double total;
/// Transaction completion timestamp
@HiveField(6)
final DateTime completedAt;
/// Payment method used (e.g., 'cash', 'card', 'digital')
@HiveField(7)
final String paymentMethod;
TransactionModel({
required this.id,
required this.items,
required this.subtotal,
required this.tax,
required this.discount,
required this.total,
required this.completedAt,
required this.paymentMethod,
});
/// Get total number of items in transaction
int get totalItems => items.fold(0, (sum, item) => sum + item.quantity);
/// Create a copy with updated fields
TransactionModel copyWith({
String? id,
List<CartItemModel>? items,
double? subtotal,
double? tax,
double? discount,
double? total,
DateTime? completedAt,
String? paymentMethod,
}) {
return TransactionModel(
id: id ?? this.id,
items: items ?? this.items,
subtotal: subtotal ?? this.subtotal,
tax: tax ?? this.tax,
discount: discount ?? this.discount,
total: total ?? this.total,
completedAt: completedAt ?? this.completedAt,
paymentMethod: paymentMethod ?? this.paymentMethod,
);
}
/// Convert to JSON
Map<String, dynamic> toJson() {
return {
'id': id,
'items': items.map((item) => item.toJson()).toList(),
'subtotal': subtotal,
'tax': tax,
'discount': discount,
'total': total,
'completedAt': completedAt.toIso8601String(),
'paymentMethod': paymentMethod,
};
}
/// Create from JSON
factory TransactionModel.fromJson(Map<String, dynamic> json) {
return TransactionModel(
id: json['id'] as String,
items: (json['items'] as List)
.map((item) => CartItemModel.fromJson(item as Map<String, dynamic>))
.toList(),
subtotal: (json['subtotal'] as num).toDouble(),
tax: (json['tax'] as num).toDouble(),
discount: (json['discount'] as num).toDouble(),
total: (json['total'] as num).toDouble(),
completedAt: DateTime.parse(json['completedAt'] as String),
paymentMethod: json['paymentMethod'] as String,
);
}
@override
String toString() {
return 'TransactionModel(id: $id, total: $total, items: ${items.length}, method: $paymentMethod)';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is TransactionModel && other.id == id;
}
@override
int get hashCode => id.hashCode;
}