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:
efrilm
2026-09-06 23:03:31 +07:00
co-authored by Claude Opus 5
parent 9777f63426
commit 1505a83dd3
72 changed files with 5388 additions and 584 deletions
@@ -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,
);
}
+48 -16
View File
@@ -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,
);
}
+1
View File
@@ -3,6 +3,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';
import '../../common/api/api_failure.dart';
import '../../common/types/split_type.dart';
import '../../common/extension/extension.dart';
import '../product/product.dart';
part 'order.freezed.dart';
+163 -9
View File
@@ -1007,6 +1007,12 @@ mixin _$OrderItem {
String get categoryId => throw _privateConstructorUsedError;
String get categoryName => throw _privateConstructorUsedError;
/// Angka timbangan baris ini. `null` untuk produk cacah.
double? get weight => throw _privateConstructorUsedError;
String get unitId => throw _privateConstructorUsedError;
String get unitName => throw _privateConstructorUsedError;
String get unitAbbreviation => throw _privateConstructorUsedError;
/// Create a copy of OrderItem
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -1039,6 +1045,10 @@ abstract class $OrderItemCopyWith<$Res> {
int paidQuantity,
String categoryId,
String categoryName,
double? weight,
String unitId,
String unitName,
String unitAbbreviation,
});
}
@@ -1076,6 +1086,10 @@ class _$OrderItemCopyWithImpl<$Res, $Val extends OrderItem>
Object? paidQuantity = null,
Object? categoryId = null,
Object? categoryName = null,
Object? weight = freezed,
Object? unitId = null,
Object? unitName = null,
Object? unitAbbreviation = null,
}) {
return _then(
_value.copyWith(
@@ -1155,6 +1169,22 @@ class _$OrderItemCopyWithImpl<$Res, $Val extends OrderItem>
? _value.categoryName
: categoryName // ignore: cast_nullable_to_non_nullable
as String,
weight: freezed == weight
? _value.weight
: weight // ignore: cast_nullable_to_non_nullable
as double?,
unitId: null == unitId
? _value.unitId
: unitId // ignore: cast_nullable_to_non_nullable
as String,
unitName: null == unitName
? _value.unitName
: unitName // ignore: cast_nullable_to_non_nullable
as String,
unitAbbreviation: null == unitAbbreviation
? _value.unitAbbreviation
: unitAbbreviation // ignore: cast_nullable_to_non_nullable
as String,
)
as $Val,
);
@@ -1190,6 +1220,10 @@ abstract class _$$OrderItemImplCopyWith<$Res>
int paidQuantity,
String categoryId,
String categoryName,
double? weight,
String unitId,
String unitName,
String unitAbbreviation,
});
}
@@ -1226,6 +1260,10 @@ class __$$OrderItemImplCopyWithImpl<$Res>
Object? paidQuantity = null,
Object? categoryId = null,
Object? categoryName = null,
Object? weight = freezed,
Object? unitId = null,
Object? unitName = null,
Object? unitAbbreviation = null,
}) {
return _then(
_$OrderItemImpl(
@@ -1305,6 +1343,22 @@ class __$$OrderItemImplCopyWithImpl<$Res>
? _value.categoryName
: categoryName // ignore: cast_nullable_to_non_nullable
as String,
weight: freezed == weight
? _value.weight
: weight // ignore: cast_nullable_to_non_nullable
as double?,
unitId: null == unitId
? _value.unitId
: unitId // ignore: cast_nullable_to_non_nullable
as String,
unitName: null == unitName
? _value.unitName
: unitName // ignore: cast_nullable_to_non_nullable
as String,
unitAbbreviation: null == unitAbbreviation
? _value.unitAbbreviation
: unitAbbreviation // ignore: cast_nullable_to_non_nullable
as String,
),
);
}
@@ -1312,7 +1366,7 @@ class __$$OrderItemImplCopyWithImpl<$Res>
/// @nodoc
class _$OrderItemImpl implements _OrderItem {
class _$OrderItemImpl extends _OrderItem {
const _$OrderItemImpl({
required this.id,
required this.orderId,
@@ -1333,7 +1387,12 @@ class _$OrderItemImpl implements _OrderItem {
required this.paidQuantity,
required this.categoryId,
required this.categoryName,
}) : _modifiers = modifiers;
this.weight,
this.unitId = '',
this.unitName = '',
this.unitAbbreviation = '',
}) : _modifiers = modifiers,
super._();
@override
final String id;
@@ -1380,9 +1439,22 @@ class _$OrderItemImpl implements _OrderItem {
@override
final String categoryName;
/// Angka timbangan baris ini. `null` untuk produk cacah.
@override
final double? weight;
@override
@JsonKey()
final String unitId;
@override
@JsonKey()
final String unitName;
@override
@JsonKey()
final String unitAbbreviation;
@override
String toString() {
return 'OrderItem(id: $id, orderId: $orderId, productId: $productId, productName: $productName, productVariantId: $productVariantId, productVariantName: $productVariantName, quantity: $quantity, unitPrice: $unitPrice, totalPrice: $totalPrice, modifiers: $modifiers, notes: $notes, status: $status, createdAt: $createdAt, updatedAt: $updatedAt, printerType: $printerType, printToChecker: $printToChecker, paidQuantity: $paidQuantity, categoryId: $categoryId, categoryName: $categoryName)';
return 'OrderItem(id: $id, orderId: $orderId, productId: $productId, productName: $productName, productVariantId: $productVariantId, productVariantName: $productVariantName, quantity: $quantity, unitPrice: $unitPrice, totalPrice: $totalPrice, modifiers: $modifiers, notes: $notes, status: $status, createdAt: $createdAt, updatedAt: $updatedAt, printerType: $printerType, printToChecker: $printToChecker, paidQuantity: $paidQuantity, categoryId: $categoryId, categoryName: $categoryName, weight: $weight, unitId: $unitId, unitName: $unitName, unitAbbreviation: $unitAbbreviation)';
}
@override
@@ -1425,7 +1497,13 @@ class _$OrderItemImpl implements _OrderItem {
(identical(other.categoryId, categoryId) ||
other.categoryId == categoryId) &&
(identical(other.categoryName, categoryName) ||
other.categoryName == categoryName));
other.categoryName == categoryName) &&
(identical(other.weight, weight) || other.weight == weight) &&
(identical(other.unitId, unitId) || other.unitId == unitId) &&
(identical(other.unitName, unitName) ||
other.unitName == unitName) &&
(identical(other.unitAbbreviation, unitAbbreviation) ||
other.unitAbbreviation == unitAbbreviation));
}
@override
@@ -1450,6 +1528,10 @@ class _$OrderItemImpl implements _OrderItem {
paidQuantity,
categoryId,
categoryName,
weight,
unitId,
unitName,
unitAbbreviation,
]);
/// Create a copy of OrderItem
@@ -1461,7 +1543,7 @@ class _$OrderItemImpl implements _OrderItem {
__$$OrderItemImplCopyWithImpl<_$OrderItemImpl>(this, _$identity);
}
abstract class _OrderItem implements OrderItem {
abstract class _OrderItem extends OrderItem {
const factory _OrderItem({
required final String id,
required final String orderId,
@@ -1482,7 +1564,12 @@ abstract class _OrderItem implements OrderItem {
required final int paidQuantity,
required final String categoryId,
required final String categoryName,
final double? weight,
final String unitId,
final String unitName,
final String unitAbbreviation,
}) = _$OrderItemImpl;
const _OrderItem._() : super._();
@override
String get id;
@@ -1523,6 +1610,16 @@ abstract class _OrderItem implements OrderItem {
@override
String get categoryName;
/// Angka timbangan baris ini. `null` untuk produk cacah.
@override
double? get weight;
@override
String get unitId;
@override
String get unitName;
@override
String get unitAbbreviation;
/// Create a copy of OrderItem
/// with the given fields replaced by the non-null parameter values.
@override
@@ -1847,6 +1944,10 @@ mixin _$OrderItemRequest {
int get unitPrice => throw _privateConstructorUsedError;
String get notes => throw _privateConstructorUsedError;
/// Hanya diisi untuk produk `sell_by: "weight"`; produk cacah tidak boleh
/// mengirim berat sama sekali.
double? get weight => throw _privateConstructorUsedError;
/// Create a copy of OrderItemRequest
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -1867,6 +1968,7 @@ abstract class $OrderItemRequestCopyWith<$Res> {
int quantity,
int unitPrice,
String notes,
double? weight,
});
}
@@ -1890,6 +1992,7 @@ class _$OrderItemRequestCopyWithImpl<$Res, $Val extends OrderItemRequest>
Object? quantity = null,
Object? unitPrice = null,
Object? notes = null,
Object? weight = freezed,
}) {
return _then(
_value.copyWith(
@@ -1913,6 +2016,10 @@ class _$OrderItemRequestCopyWithImpl<$Res, $Val extends OrderItemRequest>
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as String,
weight: freezed == weight
? _value.weight
: weight // ignore: cast_nullable_to_non_nullable
as double?,
)
as $Val,
);
@@ -1934,6 +2041,7 @@ abstract class _$$OrderItemRequestImplCopyWith<$Res>
int quantity,
int unitPrice,
String notes,
double? weight,
});
}
@@ -1956,6 +2064,7 @@ class __$$OrderItemRequestImplCopyWithImpl<$Res>
Object? quantity = null,
Object? unitPrice = null,
Object? notes = null,
Object? weight = freezed,
}) {
return _then(
_$OrderItemRequestImpl(
@@ -1979,6 +2088,10 @@ class __$$OrderItemRequestImplCopyWithImpl<$Res>
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as String,
weight: freezed == weight
? _value.weight
: weight // ignore: cast_nullable_to_non_nullable
as double?,
),
);
}
@@ -1993,6 +2106,7 @@ class _$OrderItemRequestImpl implements _OrderItemRequest {
required this.quantity,
required this.unitPrice,
required this.notes,
this.weight,
});
@override
@@ -2006,9 +2120,14 @@ class _$OrderItemRequestImpl implements _OrderItemRequest {
@override
final String notes;
/// Hanya diisi untuk produk `sell_by: "weight"`; produk cacah tidak boleh
/// mengirim berat sama sekali.
@override
final double? weight;
@override
String toString() {
return 'OrderItemRequest(productId: $productId, productVariantId: $productVariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes)';
return 'OrderItemRequest(productId: $productId, productVariantId: $productVariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes, weight: $weight)';
}
@override
@@ -2024,7 +2143,8 @@ class _$OrderItemRequestImpl implements _OrderItemRequest {
other.quantity == quantity) &&
(identical(other.unitPrice, unitPrice) ||
other.unitPrice == unitPrice) &&
(identical(other.notes, notes) || other.notes == notes));
(identical(other.notes, notes) || other.notes == notes) &&
(identical(other.weight, weight) || other.weight == weight));
}
@override
@@ -2035,6 +2155,7 @@ class _$OrderItemRequestImpl implements _OrderItemRequest {
quantity,
unitPrice,
notes,
weight,
);
/// Create a copy of OrderItemRequest
@@ -2056,6 +2177,7 @@ abstract class _OrderItemRequest implements OrderItemRequest {
required final int quantity,
required final int unitPrice,
required final String notes,
final double? weight,
}) = _$OrderItemRequestImpl;
@override
@@ -2069,6 +2191,11 @@ abstract class _OrderItemRequest implements OrderItemRequest {
@override
String get notes;
/// Hanya diisi untuk produk `sell_by: "weight"`; produk cacah tidak boleh
/// mengirim berat sama sekali.
@override
double? get weight;
/// Create a copy of OrderItemRequest
/// with the given fields replaced by the non-null parameter values.
@override
@@ -3448,6 +3575,9 @@ mixin _$AddItemOrderRequest {
int get unitPrice => throw _privateConstructorUsedError;
String get notes => throw _privateConstructorUsedError;
/// Hanya diisi untuk produk `sell_by: "weight"`.
double? get weight => throw _privateConstructorUsedError;
/// Create a copy of AddItemOrderRequest
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -3468,6 +3598,7 @@ abstract class $AddItemOrderRequestCopyWith<$Res> {
int quantity,
int unitPrice,
String notes,
double? weight,
});
}
@@ -3491,6 +3622,7 @@ class _$AddItemOrderRequestCopyWithImpl<$Res, $Val extends AddItemOrderRequest>
Object? quantity = null,
Object? unitPrice = null,
Object? notes = null,
Object? weight = freezed,
}) {
return _then(
_value.copyWith(
@@ -3514,6 +3646,10 @@ class _$AddItemOrderRequestCopyWithImpl<$Res, $Val extends AddItemOrderRequest>
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as String,
weight: freezed == weight
? _value.weight
: weight // ignore: cast_nullable_to_non_nullable
as double?,
)
as $Val,
);
@@ -3535,6 +3671,7 @@ abstract class _$$AddItemOrderRequestImplCopyWith<$Res>
int quantity,
int unitPrice,
String notes,
double? weight,
});
}
@@ -3557,6 +3694,7 @@ class __$$AddItemOrderRequestImplCopyWithImpl<$Res>
Object? quantity = null,
Object? unitPrice = null,
Object? notes = null,
Object? weight = freezed,
}) {
return _then(
_$AddItemOrderRequestImpl(
@@ -3580,6 +3718,10 @@ class __$$AddItemOrderRequestImplCopyWithImpl<$Res>
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as String,
weight: freezed == weight
? _value.weight
: weight // ignore: cast_nullable_to_non_nullable
as double?,
),
);
}
@@ -3594,6 +3736,7 @@ class _$AddItemOrderRequestImpl implements _AddItemOrderRequest {
required this.quantity,
required this.unitPrice,
required this.notes,
this.weight,
});
@override
@@ -3607,9 +3750,13 @@ class _$AddItemOrderRequestImpl implements _AddItemOrderRequest {
@override
final String notes;
/// Hanya diisi untuk produk `sell_by: "weight"`.
@override
final double? weight;
@override
String toString() {
return 'AddItemOrderRequest(productId: $productId, productVariantId: $productVariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes)';
return 'AddItemOrderRequest(productId: $productId, productVariantId: $productVariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes, weight: $weight)';
}
@override
@@ -3625,7 +3772,8 @@ class _$AddItemOrderRequestImpl implements _AddItemOrderRequest {
other.quantity == quantity) &&
(identical(other.unitPrice, unitPrice) ||
other.unitPrice == unitPrice) &&
(identical(other.notes, notes) || other.notes == notes));
(identical(other.notes, notes) || other.notes == notes) &&
(identical(other.weight, weight) || other.weight == weight));
}
@override
@@ -3636,6 +3784,7 @@ class _$AddItemOrderRequestImpl implements _AddItemOrderRequest {
quantity,
unitPrice,
notes,
weight,
);
/// Create a copy of AddItemOrderRequest
@@ -3657,6 +3806,7 @@ abstract class _AddItemOrderRequest implements AddItemOrderRequest {
required final int quantity,
required final int unitPrice,
required final String notes,
final double? weight,
}) = _$AddItemOrderRequestImpl;
@override
@@ -3670,6 +3820,10 @@ abstract class _AddItemOrderRequest implements AddItemOrderRequest {
@override
String get notes;
/// Hanya diisi untuk produk `sell_by: "weight"`.
@override
double? get weight;
/// Create a copy of AddItemOrderRequest
/// with the given fields replaced by the non-null parameter values.
@override