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:
@@ -5,6 +5,7 @@ import '../../../application/checkout/checkout_form/checkout_form_bloc.dart';
|
||||
import '../../../common/extension/extension.dart';
|
||||
import '../../../common/theme/theme.dart';
|
||||
import '../../../domain/product/product.dart';
|
||||
import '../dialog/dialog.dart';
|
||||
import '../image/image.dart';
|
||||
import '../spaces/space.dart';
|
||||
|
||||
@@ -26,8 +27,8 @@ class _OrderMenuState extends State<OrderMenu> {
|
||||
|
||||
_controller.addListener(() {
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.updateItemNotes(
|
||||
widget.data.product,
|
||||
CheckoutFormEvent.updateLineNotes(
|
||||
widget.data.lineId,
|
||||
_controller.text,
|
||||
),
|
||||
);
|
||||
@@ -40,6 +41,29 @@ class _OrderMenuState extends State<OrderMenu> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _removeLine() {
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.removeLine(widget.data.lineId),
|
||||
);
|
||||
}
|
||||
|
||||
/// Koreksi salah timbang: ubah angka pada baris yang sama, bukan menambah
|
||||
/// baris baru.
|
||||
void _editWeight() {
|
||||
final bloc = context.read<CheckoutFormBloc>();
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => WeightInputDialog(
|
||||
product: widget.data.product,
|
||||
variant: widget.data.variant,
|
||||
initialWeight: widget.data.weight,
|
||||
onSubmit: (weight) => bloc.add(
|
||||
CheckoutFormEvent.updateItemWeight(widget.data.lineId, weight),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
@@ -77,9 +101,10 @@ class _OrderMenuState extends State<OrderMenu> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
(widget.data.product.price +
|
||||
(widget.data.variant?.priceModifier ?? 0))
|
||||
.currencyFormatRp,
|
||||
widget.data.isWeighted &&
|
||||
widget.data.product.unitLabel.isNotEmpty
|
||||
? '${widget.data.unitPrice.currencyFormatRp} / ${widget.data.product.unitLabel}'
|
||||
: widget.data.unitPrice.currencyFormatRp,
|
||||
),
|
||||
if (widget.data.variant != null)
|
||||
Text(widget.data.variant?.name ?? ""),
|
||||
@@ -87,60 +112,15 @@ class _OrderMenuState extends State<OrderMenu> {
|
||||
),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.removeItem(
|
||||
widget.data.product,
|
||||
widget.data.variant,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Container(
|
||||
width: 30,
|
||||
height: 30,
|
||||
color: AppColor.white,
|
||||
child: const Icon(
|
||||
Icons.remove_circle,
|
||||
color: AppColor.primary,
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: 30.0,
|
||||
child: Center(child: Text(widget.data.quantity.toString())),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.addItem(
|
||||
widget.data.product,
|
||||
widget.data.variant,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Container(
|
||||
width: 30,
|
||||
height: 30,
|
||||
color: AppColor.white,
|
||||
child: const Icon(
|
||||
Icons.add_circle,
|
||||
color: AppColor.primary,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (widget.data.isWeighted)
|
||||
_buildWeightControl()
|
||||
else
|
||||
_buildQuantityStepper(),
|
||||
const SpaceWidth(8),
|
||||
SizedBox(
|
||||
width: 80.0,
|
||||
child: Text(
|
||||
((widget.data.product.price +
|
||||
(widget.data.variant?.priceModifier ?? 0)) *
|
||||
widget.data.quantity)
|
||||
.currencyFormatRp,
|
||||
widget.data.lineTotal.currencyFormatRp,
|
||||
textAlign: TextAlign.right,
|
||||
style: const TextStyle(
|
||||
color: AppColor.primary,
|
||||
@@ -167,14 +147,7 @@ class _OrderMenuState extends State<OrderMenu> {
|
||||
),
|
||||
const SpaceWidth(16.0),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.removeItem(
|
||||
widget.data.product,
|
||||
widget.data.variant,
|
||||
),
|
||||
);
|
||||
},
|
||||
onTap: _removeLine,
|
||||
child: Container(
|
||||
height: 40,
|
||||
width: 40,
|
||||
@@ -194,4 +167,77 @@ class _OrderMenuState extends State<OrderMenu> {
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// Baris berbobot tidak punya stepper — nilainya bukan cacah, dan angka 1
|
||||
/// yang tersimpan di `quantity` akan terbaca seperti "satu ons".
|
||||
Widget _buildWeightControl() {
|
||||
return GestureDetector(
|
||||
onTap: _editWeight,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColor.primary.withOpacity(0.08),
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
border: Border.all(color: AppColor.primary.withOpacity(0.4)),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
widget.data.quantityLabel,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColor.primary,
|
||||
),
|
||||
),
|
||||
const SpaceWidth(4),
|
||||
const Icon(Icons.edit_outlined, size: 14, color: AppColor.primary),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildQuantityStepper() {
|
||||
return Row(
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.removeItem(
|
||||
widget.data.product,
|
||||
widget.data.variant,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Container(
|
||||
width: 30,
|
||||
height: 30,
|
||||
color: AppColor.white,
|
||||
child: const Icon(Icons.remove_circle, color: AppColor.primary),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: 30.0,
|
||||
child: Center(child: Text(widget.data.quantity.toString())),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.addItem(
|
||||
widget.data.product,
|
||||
widget.data.variant,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Container(
|
||||
width: 30,
|
||||
height: 30,
|
||||
color: AppColor.white,
|
||||
child: const Icon(Icons.add_circle, color: AppColor.primary),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,18 +17,35 @@ class ProductCard extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
if (product.isActive == true) {
|
||||
if (product.variants.isEmpty) {
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.addItem(product, null),
|
||||
);
|
||||
} else {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => VariantDialog(product: product),
|
||||
);
|
||||
}
|
||||
if (product.isActive != true) return;
|
||||
|
||||
if (product.variants.isNotEmpty) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => VariantDialog(product: product),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Produk timbangan tidak bisa langsung masuk keranjang — kasir harus
|
||||
// menimbang dulu, dan tiap penimbangan jadi barisnya sendiri.
|
||||
if (product.isSoldByWeight) {
|
||||
final bloc = context.read<CheckoutFormBloc>();
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => WeightInputDialog(
|
||||
product: product,
|
||||
onSubmit: (weight) => bloc.add(
|
||||
CheckoutFormEvent.addWeightedItem(product, null, weight),
|
||||
),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
context.read<CheckoutFormBloc>().add(
|
||||
CheckoutFormEvent.addItem(product, null),
|
||||
);
|
||||
},
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
@@ -75,7 +92,11 @@ class ProductCard extends StatelessWidget {
|
||||
Align(
|
||||
alignment: Alignment.center,
|
||||
child: Text(
|
||||
product.price.currencyFormatRp,
|
||||
product.isSoldByWeight
|
||||
? '${product.price.currencyFormatRp} ${product.priceUnitLabel}'
|
||||
.trim()
|
||||
: product.price.currencyFormatRp,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12,
|
||||
@@ -89,31 +110,39 @@ class ProductCard extends StatelessWidget {
|
||||
|
||||
BlocBuilder<CheckoutFormBloc, CheckoutFormState>(
|
||||
builder: (context, state) {
|
||||
final totalQuantity = state.items
|
||||
final lines = state.items
|
||||
.where((item) => item.product.id == product.id)
|
||||
.map((item) => item.quantity)
|
||||
.fold(0, (sum, qty) => sum + qty);
|
||||
.toList();
|
||||
|
||||
if (lines.isEmpty) return const SizedBox.shrink();
|
||||
|
||||
// Untuk produk timbangan, jumlah baris tidak berarti apa-apa
|
||||
// (selalu 1 per penimbangan) — yang informatif adalah total
|
||||
// berat yang sudah masuk keranjang.
|
||||
final label = product.isSoldByWeight
|
||||
? '${lines.fold<double>(0, (sum, item) => sum + (item.weight ?? 0)).weightFormat} ${product.unitLabel}'
|
||||
.trim()
|
||||
: lines
|
||||
.fold<int>(0, (sum, item) => sum + item.quantity)
|
||||
.toString();
|
||||
|
||||
if (totalQuantity == 0) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return Positioned(
|
||||
top: 4,
|
||||
right: 4,
|
||||
child: Container(
|
||||
width: 40,
|
||||
constraints: const BoxConstraints(minWidth: 40),
|
||||
height: 40,
|
||||
padding: const EdgeInsets.all(6),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6),
|
||||
decoration: const BoxDecoration(
|
||||
borderRadius: BorderRadius.all(Radius.circular(9.0)),
|
||||
color: AppColor.primary,
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
totalQuantity.toString(),
|
||||
style: const TextStyle(
|
||||
label,
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
fontSize: product.isSoldByWeight ? 13 : 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user