feat: enhance ProductPickerField and ParsedReceiptItem to support category filtering in receipt import

This commit is contained in:
Nils-Johan Gynther
2026-05-01 02:05:53 +02:00
parent 47801935e9
commit 84dfbc4a52
4 changed files with 35 additions and 9 deletions
@@ -15,6 +15,7 @@ class ParsedReceiptItem {
// AI-kategorisuggestion (premium)
final String? categorySuggestionName;
final String? categorySuggestionPath;
final int? categorySuggestionId;
ParsedReceiptItem({
required this.rawName,
@@ -29,6 +30,7 @@ class ParsedReceiptItem {
this.suggestedProductName,
this.categorySuggestionName,
this.categorySuggestionPath,
this.categorySuggestionId,
});
factory ParsedReceiptItem.fromJson(Map<String, dynamic> json) {
@@ -46,6 +48,7 @@ class ParsedReceiptItem {
suggestedProductName: json['suggestedProductName'] as String?,
categorySuggestionName: cat?['categoryName'] as String?,
categorySuggestionPath: cat?['path'] as String?,
categorySuggestionId: (cat?['categoryId'] as num?)?.toInt(),
);
}
}
@@ -98,14 +98,16 @@ class _EditDialogState extends State<_EditDialog> {
_productName = item.suggestedProductName;
});
} else if (aiCategory != null) {
// Öppna pickern med råtexten från kvittot som sökord — kategorinamnet
// matchar inte produktnamn, men rawName gör det troligtvis
// Öppna pickern filtrerad på AI-föreslagen kategori (categoryId).
// Visar bara produkter i den kategorin (eller rawName-sökning om kategorin är tom).
final catId = item.categorySuggestionId;
ProductPickerField.showSheet(
context,
products: widget.products,
value: _productId,
label: 'Produkt',
initialQuery: item.rawName,
categoryFilter: catId != null ? {catId} : null,
).then((id) {
if (id != null && mounted) {
setState(() {
@@ -276,7 +278,7 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
setState(() {
_products = list
.cast<Map<String, dynamic>>()
.map((e) => (id: e['id'] as int, name: e['name'] as String))
.map((e) => (id: e['id'] as int, name: e['name'] as String, categoryId: (e['categoryId'] as num?)?.toInt()))
.toList();
_loadingProducts = false;
});