- Added new API path for unit mappings in `api_paths.dart`. - Implemented `upsertUnitMapping` method in `ImportRepository` to handle unit mapping creation. - Updated `ReceiptImportTab` to learn and save unit mappings during receipt import. - Created DTO for unit mapping with validation in `create-unit-mapping.dto.ts`. - Added SQL migration for `UnitMapping` table creation with necessary constraints.
This commit is contained in:
@@ -464,8 +464,10 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
|
||||
int pantryAdded = 0;
|
||||
int pantrySkipped = 0;
|
||||
int aliasesLearned = 0;
|
||||
int unitMappingsLearned = 0;
|
||||
try {
|
||||
final token = await ref.read(authStateProvider.future);
|
||||
final repo = ref.read(importRepositoryProvider);
|
||||
final invRepo = ref.read(inventoryRepositoryProvider);
|
||||
final pantryRepo = ref.read(pantryRepositoryProvider);
|
||||
final adminRepo = ref.read(adminRepositoryProvider);
|
||||
@@ -497,6 +499,8 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
|
||||
: (edit.quantity ?? inferred.totalQuantity ?? item.quantity ?? 1.0);
|
||||
final unit = packUnit;
|
||||
final existing = _inventoryByProduct[pid];
|
||||
final originalUnit = (item.unit ?? '').trim();
|
||||
final preferredUnitForLearning = (existing?.unit ?? unit).trim();
|
||||
final qtyInExistingUnit = existing == null
|
||||
? null
|
||||
: convertQuantity(qty, unit, existing.unit);
|
||||
@@ -516,6 +520,23 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
|
||||
}, token: token);
|
||||
created++;
|
||||
}
|
||||
|
||||
if (originalUnit.isNotEmpty && preferredUnitForLearning.isNotEmpty) {
|
||||
try {
|
||||
await repo.upsertUnitMapping(
|
||||
productId: pid,
|
||||
originalUnit: originalUnit,
|
||||
preferredUnit: preferredUnitForLearning,
|
||||
token: token,
|
||||
);
|
||||
if (originalUnit.toLowerCase().trim() != preferredUnitForLearning.toLowerCase().trim()) {
|
||||
unitMappingsLearned++;
|
||||
}
|
||||
} catch (e, st) {
|
||||
debugPrint('ReceiptImportTab unit mapping upsert failed: $e');
|
||||
debugPrintStack(stackTrace: st);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final normalizedReceiptName = item.rawName.trim().toLowerCase();
|
||||
@@ -544,6 +565,7 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
|
||||
if (pantryAdded > 0) '$pantryAdded tillagd${pantryAdded == 1 ? '' : 'a'} i baslager',
|
||||
if (pantrySkipped > 0) '$pantrySkipped fanns redan i baslager',
|
||||
if (aliasesLearned > 0) '$aliasesLearned alias inlärda',
|
||||
if (unitMappingsLearned > 0) '$unitMappingsLearned enhetsmappningar inlärda',
|
||||
];
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(parts.join(', ') + '.')),
|
||||
|
||||
Reference in New Issue
Block a user