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
@@ -21,6 +21,8 @@ class ListProduct with _$ListProduct {
@freezed
class Product with _$Product {
const Product._();
const factory Product({
required String id,
required String organizationId,
@@ -39,6 +41,11 @@ class Product with _$Product {
required String createdAt,
required String updatedAt,
required List<ProductVariant> variants,
/// `unit` (default) atau `weight`. Lihat [SellBy].
@Default(SellBy.unit) String sellBy,
@Default('') String unitId,
@Default('') String unitName,
@Default('') String unitAbbreviation,
}) = _Product;
factory Product.empty() => const Product(
@@ -60,6 +67,18 @@ class Product with _$Product {
updatedAt: '',
variants: [],
);
/// Produk yang dijual per timbangan: `quantity` selalu 1, `weight` wajib.
bool get isSoldByWeight => SellBy.isWeight(sellBy);
/// Singkatan satuan untuk ditampilkan — `ons`. Jatuh ke nama panjang bila
/// backend hanya mengirim itu.
String get unitLabel =>
unitAbbreviation.isNotEmpty ? unitAbbreviation : unitName;
/// Label harga mengikuti satuan: "Harga per ons".
String get priceUnitLabel =>
isSoldByWeight && unitLabel.isNotEmpty ? 'per $unitLabel' : '';
}
@freezed
@@ -1,14 +1,53 @@
part of '../product.dart';
/// Satu baris keranjang.
///
/// Untuk produk timbangan, **satu penimbangan = satu baris**: dua kali menimbang
/// Ikan Tude menghasilkan dua baris terpisah dengan [quantity] 1 dan [weight]
/// masing-masing. Karena itu baris dibedakan lewat [lineId], bukan lewat
/// kombinasi produk + varian.
@freezed
class ProductQuantity with _$ProductQuantity {
const ProductQuantity._();
const factory ProductQuantity({
required Product product,
ProductVariant? variant,
required int quantity,
required String notes,
@Default('') String lineId,
/// Angka timbangan dalam satuan produk. `null` untuk produk cacah.
double? weight,
}) = _ProductQuantity;
factory ProductQuantity.empty() =>
ProductQuantity(product: Product.empty(), quantity: 0, notes: '');
bool get isWeighted => weight != null;
double get unitPrice => product.price + (variant?.priceModifier ?? 0);
/// Harga baris: `weight × unit_price` untuk timbangan, `quantity × unit_price`
/// untuk produk cacah.
double get lineTotal =>
isWeighted ? unitPrice * weight! : unitPrice * quantity;
/// `4,2 ons` atau `2` — dipakai di kolom Qty keranjang.
String get quantityLabel {
if (!isWeighted) return quantity.toString();
final label = product.unitLabel;
final value = weight!.weightFormat;
return label.isEmpty ? value : '$value $label';
}
}
extension ProductQuantityListExt on List<ProductQuantity> {
/// Total keranjang: baris berbobot pakai `weight × harga`, baris cacah pakai
/// `quantity × harga`.
double get cartTotal => fold<double>(0, (sum, item) => sum + item.lineTotal);
/// Baris berbobot dihitung satu — satu penimbangan, satu barang.
int get cartItemCount =>
fold<int>(0, (sum, item) => sum + (item.isWeighted ? 1 : item.quantity));
}
+2
View File
@@ -3,6 +3,8 @@ import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import '../../common/api/api_failure.dart';
import '../../common/extension/extension.dart';
import '../../common/types/sell_by.dart';
part 'product.freezed.dart';
+176 -18
View File
@@ -286,6 +286,12 @@ mixin _$Product {
String get updatedAt => throw _privateConstructorUsedError;
List<ProductVariant> get variants => throw _privateConstructorUsedError;
/// `unit` (default) atau `weight`. Lihat [SellBy].
String get sellBy => throw _privateConstructorUsedError;
String get unitId => throw _privateConstructorUsedError;
String get unitName => throw _privateConstructorUsedError;
String get unitAbbreviation => throw _privateConstructorUsedError;
/// Create a copy of Product
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -315,6 +321,10 @@ abstract class $ProductCopyWith<$Res> {
String createdAt,
String updatedAt,
List<ProductVariant> variants,
String sellBy,
String unitId,
String unitName,
String unitAbbreviation,
});
}
@@ -350,6 +360,10 @@ class _$ProductCopyWithImpl<$Res, $Val extends Product>
Object? createdAt = null,
Object? updatedAt = null,
Object? variants = null,
Object? sellBy = null,
Object? unitId = null,
Object? unitName = null,
Object? unitAbbreviation = null,
}) {
return _then(
_value.copyWith(
@@ -421,6 +435,22 @@ class _$ProductCopyWithImpl<$Res, $Val extends Product>
? _value.variants
: variants // ignore: cast_nullable_to_non_nullable
as List<ProductVariant>,
sellBy: null == sellBy
? _value.sellBy
: sellBy // ignore: cast_nullable_to_non_nullable
as String,
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,
);
@@ -453,6 +483,10 @@ abstract class _$$ProductImplCopyWith<$Res> implements $ProductCopyWith<$Res> {
String createdAt,
String updatedAt,
List<ProductVariant> variants,
String sellBy,
String unitId,
String unitName,
String unitAbbreviation,
});
}
@@ -487,6 +521,10 @@ class __$$ProductImplCopyWithImpl<$Res>
Object? createdAt = null,
Object? updatedAt = null,
Object? variants = null,
Object? sellBy = null,
Object? unitId = null,
Object? unitName = null,
Object? unitAbbreviation = null,
}) {
return _then(
_$ProductImpl(
@@ -558,6 +596,22 @@ class __$$ProductImplCopyWithImpl<$Res>
? _value._variants
: variants // ignore: cast_nullable_to_non_nullable
as List<ProductVariant>,
sellBy: null == sellBy
? _value.sellBy
: sellBy // ignore: cast_nullable_to_non_nullable
as String,
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,
),
);
}
@@ -565,7 +619,7 @@ class __$$ProductImplCopyWithImpl<$Res>
/// @nodoc
class _$ProductImpl with DiagnosticableTreeMixin implements _Product {
class _$ProductImpl extends _Product with DiagnosticableTreeMixin {
const _$ProductImpl({
required this.id,
required this.organizationId,
@@ -584,8 +638,13 @@ class _$ProductImpl with DiagnosticableTreeMixin implements _Product {
required this.createdAt,
required this.updatedAt,
required final List<ProductVariant> variants,
this.sellBy = SellBy.unit,
this.unitId = '',
this.unitName = '',
this.unitAbbreviation = '',
}) : _metadata = metadata,
_variants = variants;
_variants = variants,
super._();
@override
final String id;
@@ -633,9 +692,23 @@ class _$ProductImpl with DiagnosticableTreeMixin implements _Product {
return EqualUnmodifiableListView(_variants);
}
/// `unit` (default) atau `weight`. Lihat [SellBy].
@override
@JsonKey()
final String sellBy;
@override
@JsonKey()
final String unitId;
@override
@JsonKey()
final String unitName;
@override
@JsonKey()
final String unitAbbreviation;
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return 'Product(id: $id, organizationId: $organizationId, categoryId: $categoryId, sku: $sku, name: $name, description: $description, price: $price, cost: $cost, businessType: $businessType, imageUrl: $imageUrl, printerType: $printerType, printToChecker: $printToChecker, metadata: $metadata, isActive: $isActive, createdAt: $createdAt, updatedAt: $updatedAt, variants: $variants)';
return 'Product(id: $id, organizationId: $organizationId, categoryId: $categoryId, sku: $sku, name: $name, description: $description, price: $price, cost: $cost, businessType: $businessType, imageUrl: $imageUrl, printerType: $printerType, printToChecker: $printToChecker, metadata: $metadata, isActive: $isActive, createdAt: $createdAt, updatedAt: $updatedAt, variants: $variants, sellBy: $sellBy, unitId: $unitId, unitName: $unitName, unitAbbreviation: $unitAbbreviation)';
}
@override
@@ -659,7 +732,11 @@ class _$ProductImpl with DiagnosticableTreeMixin implements _Product {
..add(DiagnosticsProperty('isActive', isActive))
..add(DiagnosticsProperty('createdAt', createdAt))
..add(DiagnosticsProperty('updatedAt', updatedAt))
..add(DiagnosticsProperty('variants', variants));
..add(DiagnosticsProperty('variants', variants))
..add(DiagnosticsProperty('sellBy', sellBy))
..add(DiagnosticsProperty('unitId', unitId))
..add(DiagnosticsProperty('unitName', unitName))
..add(DiagnosticsProperty('unitAbbreviation', unitAbbreviation));
}
@override
@@ -693,11 +770,17 @@ class _$ProductImpl with DiagnosticableTreeMixin implements _Product {
other.createdAt == createdAt) &&
(identical(other.updatedAt, updatedAt) ||
other.updatedAt == updatedAt) &&
const DeepCollectionEquality().equals(other._variants, _variants));
const DeepCollectionEquality().equals(other._variants, _variants) &&
(identical(other.sellBy, sellBy) || other.sellBy == sellBy) &&
(identical(other.unitId, unitId) || other.unitId == unitId) &&
(identical(other.unitName, unitName) ||
other.unitName == unitName) &&
(identical(other.unitAbbreviation, unitAbbreviation) ||
other.unitAbbreviation == unitAbbreviation));
}
@override
int get hashCode => Object.hash(
int get hashCode => Object.hashAll([
runtimeType,
id,
organizationId,
@@ -716,7 +799,11 @@ class _$ProductImpl with DiagnosticableTreeMixin implements _Product {
createdAt,
updatedAt,
const DeepCollectionEquality().hash(_variants),
);
sellBy,
unitId,
unitName,
unitAbbreviation,
]);
/// Create a copy of Product
/// with the given fields replaced by the non-null parameter values.
@@ -727,7 +814,7 @@ class _$ProductImpl with DiagnosticableTreeMixin implements _Product {
__$$ProductImplCopyWithImpl<_$ProductImpl>(this, _$identity);
}
abstract class _Product implements Product {
abstract class _Product extends Product {
const factory _Product({
required final String id,
required final String organizationId,
@@ -746,7 +833,12 @@ abstract class _Product implements Product {
required final String createdAt,
required final String updatedAt,
required final List<ProductVariant> variants,
final String sellBy,
final String unitId,
final String unitName,
final String unitAbbreviation,
}) = _$ProductImpl;
const _Product._() : super._();
@override
String get id;
@@ -783,6 +875,16 @@ abstract class _Product implements Product {
@override
List<ProductVariant> get variants;
/// `unit` (default) atau `weight`. Lihat [SellBy].
@override
String get sellBy;
@override
String get unitId;
@override
String get unitName;
@override
String get unitAbbreviation;
/// Create a copy of Product
/// with the given fields replaced by the non-null parameter values.
@override
@@ -1121,6 +1223,10 @@ mixin _$ProductQuantity {
ProductVariant? get variant => throw _privateConstructorUsedError;
int get quantity => throw _privateConstructorUsedError;
String get notes => throw _privateConstructorUsedError;
String get lineId => throw _privateConstructorUsedError;
/// Angka timbangan dalam satuan produk. `null` untuk produk cacah.
double? get weight => throw _privateConstructorUsedError;
/// Create a copy of ProductQuantity
/// with the given fields replaced by the non-null parameter values.
@@ -1141,6 +1247,8 @@ abstract class $ProductQuantityCopyWith<$Res> {
ProductVariant? variant,
int quantity,
String notes,
String lineId,
double? weight,
});
$ProductCopyWith<$Res> get product;
@@ -1166,6 +1274,8 @@ class _$ProductQuantityCopyWithImpl<$Res, $Val extends ProductQuantity>
Object? variant = freezed,
Object? quantity = null,
Object? notes = null,
Object? lineId = null,
Object? weight = freezed,
}) {
return _then(
_value.copyWith(
@@ -1185,6 +1295,14 @@ class _$ProductQuantityCopyWithImpl<$Res, $Val extends ProductQuantity>
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as String,
lineId: null == lineId
? _value.lineId
: lineId // ignore: cast_nullable_to_non_nullable
as String,
weight: freezed == weight
? _value.weight
: weight // ignore: cast_nullable_to_non_nullable
as double?,
)
as $Val,
);
@@ -1229,6 +1347,8 @@ abstract class _$$ProductQuantityImplCopyWith<$Res>
ProductVariant? variant,
int quantity,
String notes,
String lineId,
double? weight,
});
@override
@@ -1255,6 +1375,8 @@ class __$$ProductQuantityImplCopyWithImpl<$Res>
Object? variant = freezed,
Object? quantity = null,
Object? notes = null,
Object? lineId = null,
Object? weight = freezed,
}) {
return _then(
_$ProductQuantityImpl(
@@ -1274,6 +1396,14 @@ class __$$ProductQuantityImplCopyWithImpl<$Res>
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as String,
lineId: null == lineId
? _value.lineId
: lineId // ignore: cast_nullable_to_non_nullable
as String,
weight: freezed == weight
? _value.weight
: weight // ignore: cast_nullable_to_non_nullable
as double?,
),
);
}
@@ -1281,15 +1411,16 @@ class __$$ProductQuantityImplCopyWithImpl<$Res>
/// @nodoc
class _$ProductQuantityImpl
with DiagnosticableTreeMixin
implements _ProductQuantity {
class _$ProductQuantityImpl extends _ProductQuantity
with DiagnosticableTreeMixin {
const _$ProductQuantityImpl({
required this.product,
this.variant,
required this.quantity,
required this.notes,
});
this.lineId = '',
this.weight,
}) : super._();
@override
final Product product;
@@ -1299,10 +1430,17 @@ class _$ProductQuantityImpl
final int quantity;
@override
final String notes;
@override
@JsonKey()
final String lineId;
/// Angka timbangan dalam satuan produk. `null` untuk produk cacah.
@override
final double? weight;
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return 'ProductQuantity(product: $product, variant: $variant, quantity: $quantity, notes: $notes)';
return 'ProductQuantity(product: $product, variant: $variant, quantity: $quantity, notes: $notes, lineId: $lineId, weight: $weight)';
}
@override
@@ -1313,7 +1451,9 @@ class _$ProductQuantityImpl
..add(DiagnosticsProperty('product', product))
..add(DiagnosticsProperty('variant', variant))
..add(DiagnosticsProperty('quantity', quantity))
..add(DiagnosticsProperty('notes', notes));
..add(DiagnosticsProperty('notes', notes))
..add(DiagnosticsProperty('lineId', lineId))
..add(DiagnosticsProperty('weight', weight));
}
@override
@@ -1325,12 +1465,21 @@ class _$ProductQuantityImpl
(identical(other.variant, variant) || other.variant == variant) &&
(identical(other.quantity, quantity) ||
other.quantity == quantity) &&
(identical(other.notes, notes) || other.notes == notes));
(identical(other.notes, notes) || other.notes == notes) &&
(identical(other.lineId, lineId) || other.lineId == lineId) &&
(identical(other.weight, weight) || other.weight == weight));
}
@override
int get hashCode =>
Object.hash(runtimeType, product, variant, quantity, notes);
int get hashCode => Object.hash(
runtimeType,
product,
variant,
quantity,
notes,
lineId,
weight,
);
/// Create a copy of ProductQuantity
/// with the given fields replaced by the non-null parameter values.
@@ -1344,13 +1493,16 @@ class _$ProductQuantityImpl
);
}
abstract class _ProductQuantity implements ProductQuantity {
abstract class _ProductQuantity extends ProductQuantity {
const factory _ProductQuantity({
required final Product product,
final ProductVariant? variant,
required final int quantity,
required final String notes,
final String lineId,
final double? weight,
}) = _$ProductQuantityImpl;
const _ProductQuantity._() : super._();
@override
Product get product;
@@ -1360,6 +1512,12 @@ abstract class _ProductQuantity implements ProductQuantity {
int get quantity;
@override
String get notes;
@override
String get lineId;
/// Angka timbangan dalam satuan produk. `null` untuk produk cacah.
@override
double? get weight;
/// Create a copy of ProductQuantity
/// with the given fields replaced by the non-null parameter values.
+23
View File
@@ -0,0 +1,23 @@
part of '../unit.dart';
/// Satuan timbangan milik organisasi — ons, kg, gram.
///
/// [abbreviation] yang dipakai POS untuk mencetak `4,2 ons` di struk.
@freezed
class ProductUnit with _$ProductUnit {
const factory ProductUnit({
required String id,
required String organizationId,
required String name,
required String abbreviation,
required bool isActive,
}) = _ProductUnit;
factory ProductUnit.empty() => const ProductUnit(
id: '',
organizationId: '',
name: '',
abbreviation: '',
isActive: false,
);
}
@@ -0,0 +1,12 @@
part of '../unit.dart';
@freezed
sealed class UnitFailure with _$UnitFailure {
const factory UnitFailure.serverError(ApiFailure failure) = _ServerError;
const factory UnitFailure.unexpectedError() = _UnexpectedError;
const factory UnitFailure.empty() = _Empty;
const factory UnitFailure.localStorageError(String erroMessage) =
_LocalStorageError;
const factory UnitFailure.dynamicErrorMessage(String erroMessage) =
_DynamicErrorMessage;
}
@@ -0,0 +1,12 @@
part of '../unit.dart';
abstract class IUnitRepository {
/// Ambil satuan dari server lalu simpan ke cache lokal.
/// Bila server tidak terjangkau, kembalikan isi cache supaya POS tetap bisa
/// mencetak `4,2 ons` saat offline.
Future<Either<UnitFailure, List<ProductUnit>>> getUnits({
bool forceRefresh = false,
});
Future<List<ProductUnit>> cachedUnits();
}
+10
View File
@@ -0,0 +1,10 @@
import 'package:dartz/dartz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import '../../common/api/api_failure.dart';
part 'unit.freezed.dart';
part 'entities/unit_entity.dart';
part 'failures/unit_failure.dart';
part 'repositories/i_unit_repository.dart';
File diff suppressed because it is too large Load Diff