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:
@@ -2,4 +2,5 @@ class LocalStorageKey {
|
||||
static const String token = 'token';
|
||||
static const String user = 'user';
|
||||
static const String outlet = 'outlet';
|
||||
static const String units = 'units';
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class DatabaseHelper {
|
||||
|
||||
return await openDatabase(
|
||||
path,
|
||||
version: 2,
|
||||
version: 3,
|
||||
onCreate: _onCreate,
|
||||
onUpgrade: _onUpgrade,
|
||||
);
|
||||
@@ -42,7 +42,11 @@ class DatabaseHelper {
|
||||
metadata TEXT,
|
||||
is_active INTEGER,
|
||||
created_at TEXT,
|
||||
updated_at TEXT
|
||||
updated_at TEXT,
|
||||
sell_by TEXT DEFAULT 'unit',
|
||||
unit_id TEXT,
|
||||
unit_name TEXT,
|
||||
unit_abbreviation TEXT
|
||||
)
|
||||
''');
|
||||
|
||||
@@ -114,6 +118,17 @@ class DatabaseHelper {
|
||||
'ALTER TABLE products ADD COLUMN print_to_checker INTEGER DEFAULT 0',
|
||||
);
|
||||
}
|
||||
|
||||
// Produk timbangan: cara jual dan satuan ikut disimpan lokal supaya POS
|
||||
// tetap bisa menampilkan "4,2 ons" saat offline.
|
||||
if (oldVersion < 3) {
|
||||
await db.execute(
|
||||
"ALTER TABLE products ADD COLUMN sell_by TEXT DEFAULT 'unit'",
|
||||
);
|
||||
await db.execute('ALTER TABLE products ADD COLUMN unit_id TEXT');
|
||||
await db.execute('ALTER TABLE products ADD COLUMN unit_name TEXT');
|
||||
await db.execute('ALTER TABLE products ADD COLUMN unit_abbreviation TEXT');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> close() async {
|
||||
|
||||
@@ -13,3 +13,10 @@ extension DoubleExt on double {
|
||||
decimalDigits: 0,
|
||||
).format(this);
|
||||
}
|
||||
|
||||
extension WeightExt on double {
|
||||
/// Format berat gaya Indonesia: koma desimal, tanpa nol di belakang.
|
||||
/// `4.2` → `4,2`, `4.237` → `4,237`, `5.0` → `5`.
|
||||
String get weightFormat =>
|
||||
NumberFormat('#,##0.###', 'id').format(this);
|
||||
}
|
||||
|
||||
@@ -75,3 +75,25 @@ int generateRandomNumber() {
|
||||
final random = Random();
|
||||
return 1 + random.nextInt(10000);
|
||||
}
|
||||
|
||||
/// Ambil pesan validasi dari amplop error standar backend.
|
||||
///
|
||||
/// Untuk produk timbangan pesannya menyebut nama produk — misalnya
|
||||
/// "product Ikan Tude is sold by weight and requires a weight" — jadi layak
|
||||
/// ditampilkan apa adanya ke kasir alih-alih diganti pesan generik.
|
||||
String? extractApiErrorCause(dynamic responseData) {
|
||||
if (responseData is! Map) return null;
|
||||
|
||||
final errors = responseData['errors'];
|
||||
if (errors is! List || errors.isEmpty) return null;
|
||||
|
||||
final causes = errors
|
||||
.whereType<Map>()
|
||||
.map((e) => (e['cause'] ?? e['message'])?.toString())
|
||||
.where((cause) => cause != null && cause.isNotEmpty)
|
||||
.cast<String>()
|
||||
.toList();
|
||||
|
||||
if (causes.isEmpty) return null;
|
||||
return causes.join('\n');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
import '../../domain/unit/unit.dart';
|
||||
|
||||
/// Kamus satuan yang dipakai untuk menerjemahkan `unit_id` sebuah produk
|
||||
/// menjadi singkatan yang dicetak di keranjang dan struk (`4,2 ons`).
|
||||
///
|
||||
/// Response produk hanya dijamin membawa `unit_id`; nama dan singkatannya
|
||||
/// datang dari `GET /api/v1/units`. Registry ini menjembatani keduanya tanpa
|
||||
/// memaksa setiap pemanggil menunggu request.
|
||||
@lazySingleton
|
||||
class UnitRegistry {
|
||||
final IUnitRepository _repository;
|
||||
final _logName = 'UnitRegistry';
|
||||
|
||||
Map<String, ProductUnit> _unitsById = {};
|
||||
|
||||
UnitRegistry(this._repository);
|
||||
|
||||
bool get isEmpty => _unitsById.isEmpty;
|
||||
|
||||
ProductUnit? byId(String? id) {
|
||||
if (id == null || id.isEmpty) return null;
|
||||
return _unitsById[id];
|
||||
}
|
||||
|
||||
String abbreviationOf(String? id) => byId(id)?.abbreviation ?? '';
|
||||
|
||||
String nameOf(String? id) => byId(id)?.name ?? '';
|
||||
|
||||
/// Muat satuan ke memori. Aman dipanggil berulang; [forceRefresh] menembak
|
||||
/// server ulang saat pengguna melakukan sinkronisasi manual.
|
||||
Future<void> load({bool forceRefresh = false}) async {
|
||||
final result = await _repository.getUnits(forceRefresh: forceRefresh);
|
||||
|
||||
result.fold(
|
||||
(failure) => log('⚠️ Gagal memuat satuan: $failure', name: _logName),
|
||||
(units) {
|
||||
_unitsById = {for (final unit in units) unit.id: unit};
|
||||
log('📏 ${units.length} satuan siap dipakai', name: _logName);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void clear() => _unitsById = {};
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/// Cara sebuah produk dijual.
|
||||
///
|
||||
/// Lihat `docs/integration-weight-based-products.md` — backend hanya menerima
|
||||
/// dua nilai ini, dan produk lama otomatis dianggap [SellBy.unit].
|
||||
class SellBy {
|
||||
const SellBy._();
|
||||
|
||||
/// Dijual per cacah. `quantity` integer, `weight` tidak dikirim.
|
||||
static const String unit = 'unit';
|
||||
|
||||
/// Dijual per timbangan. `quantity` selalu 1, `weight` wajib.
|
||||
static const String weight = 'weight';
|
||||
|
||||
static bool isWeight(String? value) => value == weight;
|
||||
|
||||
/// Nilai apa pun di luar dua yang dikenal diperlakukan sebagai [unit],
|
||||
/// sama seperti perilaku backend untuk produk lama.
|
||||
static String normalize(String? value) => value == weight ? weight : unit;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ class ApiPath {
|
||||
static const String outlets = '/api/v1/outlets';
|
||||
static const String categories = '/api/v1/categories';
|
||||
static const String products = '/api/v1/products';
|
||||
static const String units = '/api/v1/units';
|
||||
static const String tables = '/api/v1/tables';
|
||||
static const String customers = '/api/v1/customers';
|
||||
static const String paymentMethods = '/api/v1/payment-methods';
|
||||
|
||||
Reference in New Issue
Block a user