diff --git a/flutter/lib/features/import/presentation/receipt_import_tab.dart b/flutter/lib/features/import/presentation/receipt_import_tab.dart index 596bf1bd..705f270e 100644 --- a/flutter/lib/features/import/presentation/receipt_import_tab.dart +++ b/flutter/lib/features/import/presentation/receipt_import_tab.dart @@ -63,7 +63,23 @@ bool _isPackageLikeUnit(String? unit) { required double? quantity, required String? unit, }) { - if (quantity == null || unit == null) { + final normalizedUnit = unit?.trim().toLowerCase(); + final safeCount = (quantity != null && quantity > 0) ? quantity : 1.0; + final extracted = _extractPackageSizeFromRawName(rawName); + + // If the receipt name contains size (e.g. "5dl"), prefer it when unit is + // missing/unknown or when OCR reports package-like count units (st/pkt/etc). + if (extracted != null && (normalizedUnit == null || normalizedUnit.isEmpty || _isPackageLikeUnit(normalizedUnit))) { + return ( + packQuantity: extracted.packQuantity, + packUnit: extracted.packUnit, + packageCount: safeCount, + totalQuantity: extracted.packQuantity * safeCount, + totalUnit: extracted.packUnit, + ); + } + + if (quantity == null || normalizedUnit == null || normalizedUnit.isEmpty) { return ( packQuantity: null, packUnit: null, @@ -73,8 +89,7 @@ bool _isPackageLikeUnit(String? unit) { ); } - final looksLikePackage = _isPackageLikeUnit(unit); - final extracted = _extractPackageSizeFromRawName(rawName); + final looksLikePackage = _isPackageLikeUnit(normalizedUnit); if (looksLikePackage && extracted != null) { return ( @@ -88,10 +103,10 @@ bool _isPackageLikeUnit(String? unit) { return ( packQuantity: quantity, - packUnit: unit, + packUnit: normalizedUnit, packageCount: 1, totalQuantity: quantity, - totalUnit: unit, + totalUnit: normalizedUnit, ); }