feat: dukung penjualan produk per timbangan
Produk kini punya cara jual `unit` (cacah) atau `weight` (timbangan), mengikuti kontrak backend di docs/integration-weight-based-products.md. - tipe SellBy, domain/infrastructure unit baru, dan endpoint /api/v1/units - dialog input berat saat produk timbangan ditambahkan ke keranjang - tiap penimbangan jadi baris keranjang sendiri; total baris dihitung berat x harga satuan, sedangkan produk cacah tetap digabung - request ke backend mengirim quantity 1 dan membawa berat - migrasi DB v3: kolom sell_by, unit_id, unit_name, unit_abbreviation di tabel products supaya satuan tetap tampil saat offline - tampilan keranjang, struk, split bill, void, dan refund memakai berat - test/weight_based_products_test.dart menutup perilaku di atas (9 test) Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,9 @@ class AddItemOrderRequest with _$AddItemOrderRequest {
|
||||
required int quantity,
|
||||
required int unitPrice,
|
||||
required String notes,
|
||||
|
||||
/// Hanya diisi untuk produk `sell_by: "weight"`.
|
||||
double? weight,
|
||||
}) = _AddItemOrderRequest;
|
||||
|
||||
factory AddItemOrderRequest.empty() => const AddItemOrderRequest(
|
||||
@@ -17,4 +20,14 @@ class AddItemOrderRequest with _$AddItemOrderRequest {
|
||||
unitPrice: 0,
|
||||
notes: '',
|
||||
);
|
||||
|
||||
factory AddItemOrderRequest.fromProductQuantity(ProductQuantity item) =>
|
||||
AddItemOrderRequest(
|
||||
productId: item.product.id,
|
||||
productVariantId: item.variant?.id ?? '',
|
||||
quantity: item.isWeighted ? 1 : item.quantity,
|
||||
unitPrice: item.product.price.toInt(),
|
||||
notes: item.notes,
|
||||
weight: item.weight,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -197,19 +197,19 @@ class Order with _$Order {
|
||||
);
|
||||
|
||||
// For Add Item Order
|
||||
factory Order.fromLastOrder(Order order, List<ProductQuantity> products) =>
|
||||
order.copyWith(
|
||||
orderItems: products
|
||||
.map((e) => OrderItem.fromProductQuantity(e))
|
||||
.toList(),
|
||||
totalAmount: products
|
||||
.map((e) => e.product.price.toInt() * e.quantity)
|
||||
.reduce((value, element) => value + element),
|
||||
subtotal: products
|
||||
.map((e) => e.product.price.toInt() * e.quantity)
|
||||
.reduce((value, element) => value + element),
|
||||
metadata: order.metadata,
|
||||
);
|
||||
factory Order.fromLastOrder(Order order, List<ProductQuantity> products) {
|
||||
final subtotal = products.fold<int>(
|
||||
0,
|
||||
(sum, item) => sum + item.lineTotal.toInt(),
|
||||
);
|
||||
|
||||
return order.copyWith(
|
||||
orderItems: products.map(OrderItem.fromProductQuantity).toList(),
|
||||
totalAmount: subtotal,
|
||||
subtotal: subtotal,
|
||||
metadata: order.metadata,
|
||||
);
|
||||
}
|
||||
|
||||
List<OrderItem> get orderItemNotCancelled =>
|
||||
orderItems.where((item) => item.status != 'cancelled').toList();
|
||||
@@ -217,6 +217,8 @@ class Order with _$Order {
|
||||
|
||||
@freezed
|
||||
class OrderItem with _$OrderItem {
|
||||
const OrderItem._();
|
||||
|
||||
const factory OrderItem({
|
||||
required String id,
|
||||
required String orderId,
|
||||
@@ -237,6 +239,12 @@ class OrderItem with _$OrderItem {
|
||||
required int paidQuantity,
|
||||
required String categoryId,
|
||||
required String categoryName,
|
||||
|
||||
/// Angka timbangan baris ini. `null` untuk produk cacah.
|
||||
double? weight,
|
||||
@Default('') String unitId,
|
||||
@Default('') String unitName,
|
||||
@Default('') String unitAbbreviation,
|
||||
}) = _OrderItem;
|
||||
|
||||
factory OrderItem.empty() => OrderItem(
|
||||
@@ -269,10 +277,9 @@ class OrderItem with _$OrderItem {
|
||||
productName: productQuantity.product.name,
|
||||
productVariantId: productQuantity.variant?.id ?? '',
|
||||
productVariantName: productQuantity.variant?.name ?? '',
|
||||
quantity: productQuantity.quantity,
|
||||
quantity: productQuantity.isWeighted ? 1 : productQuantity.quantity,
|
||||
unitPrice: productQuantity.product.price.toInt(),
|
||||
totalPrice: (productQuantity.product.price * productQuantity.quantity)
|
||||
.toInt(),
|
||||
totalPrice: productQuantity.lineTotal.toInt(),
|
||||
modifiers: [],
|
||||
notes: productQuantity.notes,
|
||||
status: 'pending',
|
||||
@@ -283,5 +290,30 @@ class OrderItem with _$OrderItem {
|
||||
paidQuantity: 0,
|
||||
categoryId: '',
|
||||
categoryName: '',
|
||||
weight: productQuantity.weight,
|
||||
unitId: productQuantity.product.unitId,
|
||||
unitName: productQuantity.product.unitName,
|
||||
unitAbbreviation: productQuantity.product.unitAbbreviation,
|
||||
);
|
||||
|
||||
/// Baris hasil penimbangan — utuh, tidak bisa dipecah sebagian.
|
||||
bool get isWeighted => weight != null && weight! > 0;
|
||||
|
||||
/// Singkatan satuan untuk ditampilkan; jatuh ke nama panjang bila kosong.
|
||||
String get unitLabel =>
|
||||
unitAbbreviation.isNotEmpty ? unitAbbreviation : unitName;
|
||||
|
||||
/// `4,2 ons` untuk baris berbobot, `2` untuk baris biasa.
|
||||
///
|
||||
/// Jangan menampilkan [quantity] mentah pada baris berbobot — nilainya selalu
|
||||
/// 1 dan terbaca seperti "satu ons".
|
||||
String get quantityLabel {
|
||||
if (!isWeighted) return quantity.toString();
|
||||
final value = weight!.weightFormat;
|
||||
return unitLabel.isEmpty ? value : '$value $unitLabel';
|
||||
}
|
||||
|
||||
/// `4,2 ons x Rp 4.500` atau `2 x Rp 25.000`.
|
||||
String get quantityPriceLabel =>
|
||||
'$quantityLabel x ${unitPrice.currencyFormatRpV2}';
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ class OrderItemRequest with _$OrderItemRequest {
|
||||
required int quantity,
|
||||
required int unitPrice,
|
||||
required String notes,
|
||||
|
||||
/// Hanya diisi untuk produk `sell_by: "weight"`; produk cacah tidak boleh
|
||||
/// mengirim berat sama sekali.
|
||||
double? weight,
|
||||
}) = _OrderItemRequest;
|
||||
|
||||
factory OrderItemRequest.empty() => const OrderItemRequest(
|
||||
@@ -42,4 +46,16 @@ class OrderItemRequest with _$OrderItemRequest {
|
||||
unitPrice: 0,
|
||||
notes: '',
|
||||
);
|
||||
|
||||
/// Bentuk baris dari isi keranjang: produk timbangan selalu `quantity: 1`
|
||||
/// ditambah `weight`, produk cacah tetap seperti sebelumnya.
|
||||
factory OrderItemRequest.fromProductQuantity(ProductQuantity item) =>
|
||||
OrderItemRequest(
|
||||
productId: item.product.id,
|
||||
productVariantId: item.variant?.id ?? '',
|
||||
quantity: item.isWeighted ? 1 : item.quantity,
|
||||
unitPrice: item.product.price.toInt(),
|
||||
notes: item.notes,
|
||||
weight: item.weight,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user