update cart
This commit is contained in:
@@ -11,16 +11,30 @@ import 'package:worker/features/products/domain/entities/product.dart';
|
||||
class CartItemData {
|
||||
final Product product;
|
||||
final double quantity;
|
||||
final double quantityConverted; // Rounded-up quantity for actual billing
|
||||
final int boxes; // Number of tiles/boxes needed
|
||||
|
||||
const CartItemData({required this.product, required this.quantity});
|
||||
const CartItemData({
|
||||
required this.product,
|
||||
required this.quantity,
|
||||
required this.quantityConverted,
|
||||
required this.boxes,
|
||||
});
|
||||
|
||||
/// Calculate line total
|
||||
double get lineTotal => product.basePrice * quantity;
|
||||
/// Calculate line total using CONVERTED quantity (important for accurate billing)
|
||||
double get lineTotal => product.basePrice * quantityConverted;
|
||||
|
||||
CartItemData copyWith({Product? product, double? quantity}) {
|
||||
CartItemData copyWith({
|
||||
Product? product,
|
||||
double? quantity,
|
||||
double? quantityConverted,
|
||||
int? boxes,
|
||||
}) {
|
||||
return CartItemData(
|
||||
product: product ?? this.product,
|
||||
quantity: quantity ?? this.quantity,
|
||||
quantityConverted: quantityConverted ?? this.quantityConverted,
|
||||
boxes: boxes ?? this.boxes,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -30,6 +44,7 @@ class CartItemData {
|
||||
/// Represents the complete state of the shopping cart.
|
||||
class CartState {
|
||||
final List<CartItemData> items;
|
||||
final Map<String, bool> selectedItems; // productId -> isSelected
|
||||
final String selectedWarehouse;
|
||||
final String? discountCode;
|
||||
final bool discountCodeApplied;
|
||||
@@ -39,9 +54,12 @@ class CartState {
|
||||
final double memberDiscount;
|
||||
final double shippingFee;
|
||||
final double total;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const CartState({
|
||||
required this.items,
|
||||
required this.selectedItems,
|
||||
required this.selectedWarehouse,
|
||||
this.discountCode,
|
||||
required this.discountCodeApplied,
|
||||
@@ -51,11 +69,14 @@ class CartState {
|
||||
required this.memberDiscount,
|
||||
required this.shippingFee,
|
||||
required this.total,
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
factory CartState.initial() {
|
||||
return const CartState(
|
||||
items: [],
|
||||
selectedItems: {},
|
||||
selectedWarehouse: 'Kho Hà Nội - Nguyễn Trãi',
|
||||
discountCode: null,
|
||||
discountCodeApplied: false,
|
||||
@@ -77,8 +98,30 @@ class CartState {
|
||||
return items.fold<double>(0.0, (sum, item) => sum + item.quantity);
|
||||
}
|
||||
|
||||
/// Get number of selected items
|
||||
int get selectedCount {
|
||||
return selectedItems.values.where((selected) => selected).length;
|
||||
}
|
||||
|
||||
/// Check if all items are selected
|
||||
bool get isAllSelected {
|
||||
return items.isNotEmpty && selectedCount == items.length;
|
||||
}
|
||||
|
||||
/// Get total of selected items only
|
||||
double get selectedTotal {
|
||||
double total = 0.0;
|
||||
for (final item in items) {
|
||||
if (selectedItems[item.product.productId] == true) {
|
||||
total += item.lineTotal;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
CartState copyWith({
|
||||
List<CartItemData>? items,
|
||||
Map<String, bool>? selectedItems,
|
||||
String? selectedWarehouse,
|
||||
String? discountCode,
|
||||
bool? discountCodeApplied,
|
||||
@@ -88,9 +131,12 @@ class CartState {
|
||||
double? memberDiscount,
|
||||
double? shippingFee,
|
||||
double? total,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return CartState(
|
||||
items: items ?? this.items,
|
||||
selectedItems: selectedItems ?? this.selectedItems,
|
||||
selectedWarehouse: selectedWarehouse ?? this.selectedWarehouse,
|
||||
discountCode: discountCode ?? this.discountCode,
|
||||
discountCodeApplied: discountCodeApplied ?? this.discountCodeApplied,
|
||||
@@ -101,6 +147,8 @@ class CartState {
|
||||
memberDiscount: memberDiscount ?? this.memberDiscount,
|
||||
shippingFee: shippingFee ?? this.shippingFee,
|
||||
total: total ?? this.total,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user