Files
apskel-pos-flutter-v2/lib/common/function/app_function.dart
T
efrilmandClaude Opus 5 1505a83dd3 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>
2026-09-06 23:03:31 +07:00

100 lines
3.1 KiB
Dart

import 'dart:developer';
import 'dart:math' hide log;
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../injection.dart';
import '../constant/local_storage_key.dart';
void dismissKeyboard(BuildContext context) {
final currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
FocusManager.instance.primaryFocus?.unfocus();
}
}
Map<String, dynamic> getAuthorizationHeader() {
return {
'Authorization':
'Bearer ${getIt<SharedPreferences>().getString(LocalStorageKey.token)}',
};
}
Map<String, int> getChairDistribution(int capacity) {
if (capacity == 1) {
return {'top': 0, 'bottom': 0, 'left': 1, 'right': 0};
} else if (capacity == 2) {
return {'top': 0, 'bottom': 0, 'left': 1, 'right': 1};
} else if (capacity == 3) {
return {'top': 1, 'bottom': 0, 'left': 1, 'right': 1};
} else if (capacity == 4) {
return {'top': 1, 'bottom': 1, 'left': 1, 'right': 1};
} else if (capacity == 5) {
return {'top': 2, 'bottom': 1, 'left': 1, 'right': 1};
} else if (capacity == 6) {
return {'top': 2, 'bottom': 2, 'left': 1, 'right': 1};
} else if (capacity == 7) {
return {'top': 3, 'bottom': 2, 'left': 1, 'right': 1};
} else if (capacity == 8) {
return {'top': 3, 'bottom': 3, 'left': 1, 'right': 1};
} else if (capacity == 9) {
return {'top': 4, 'bottom': 3, 'left': 1, 'right': 1};
} else if (capacity == 10) {
return {'top': 4, 'bottom': 4, 'left': 1, 'right': 1};
} else {
int side = ((capacity - 2) / 2).floor();
return {'top': side, 'bottom': side, 'left': 1, 'right': 1};
}
}
double safeDouble(double value) {
if (value.isNaN || value.isInfinite) return 0.0;
return value;
}
int safeRound(double value) {
if (value.isNaN || value.isInfinite) return 0;
return value.round();
}
Future<void> loadPermissionBluetooth() async {
var status2 = await Permission.bluetoothScan.status;
log("Permission: $status2");
if (status2.isDenied) {
await Permission.bluetoothScan.request();
}
var status = await Permission.bluetoothConnect.status;
if (status.isDenied) {
await Permission.bluetoothConnect.request();
}
}
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');
}