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
@@ -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.