Compare commits
6 Commits
28f61a3099
...
1.0.5
Author | SHA1 | Date | |
---|---|---|---|
23cc189e53 | |||
3098d12e8a | |||
0943104cc8 | |||
3beaba806a | |||
1788e7f184 | |||
71d32d72ef |
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:moneymgr_mobile/services/storage/expenses.dart';
|
import 'package:moneymgr_mobile/services/storage/expenses.dart';
|
||||||
|
import 'package:moneymgr_mobile/services/storage/prefs.dart';
|
||||||
import 'package:moneymgr_mobile/utils/ocr_utils.dart';
|
import 'package:moneymgr_mobile/utils/ocr_utils.dart';
|
||||||
import 'package:moneymgr_mobile/utils/pdf_utils.dart';
|
import 'package:moneymgr_mobile/utils/pdf_utils.dart';
|
||||||
import 'package:moneymgr_mobile/widgets/expense_editor.dart';
|
import 'package:moneymgr_mobile/widgets/expense_editor.dart';
|
||||||
@@ -13,10 +14,15 @@ part 'scan_screen.g.dart';
|
|||||||
|
|
||||||
/// Scan a document & return generated PDF as byte file
|
/// Scan a document & return generated PDF as byte file
|
||||||
@riverpod
|
@riverpod
|
||||||
Future<(Uint8List?, double?)> _scanDocument(Ref ref) async {
|
Future<(Uint8List?, BaseExpenseInfo?)> _scanDocument(Ref ref) async {
|
||||||
|
final prefs = ref.watch(prefsProvider).requireValue;
|
||||||
|
|
||||||
final pdf = await scanDocAsPDF();
|
final pdf = await scanDocAsPDF();
|
||||||
final img = await renderPdf(pdfBytes: pdf);
|
final img = await renderPdf(pdfBytes: pdf);
|
||||||
final amount = await extractTotalFromBill(img);
|
final amount = await extractInfoFromBill(
|
||||||
|
imgBuff: img,
|
||||||
|
extractDates: !prefs.disableExtractDates(),
|
||||||
|
);
|
||||||
return (pdf, amount);
|
return (pdf, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,11 +48,7 @@ class ScanScreen extends HookConsumerWidget {
|
|||||||
child: switch (scanDocProvider) {
|
child: switch (scanDocProvider) {
|
||||||
AsyncData(:final value) when value.$1 != null => ExpenseEditor(
|
AsyncData(:final value) when value.$1 != null => ExpenseEditor(
|
||||||
file: value.$1!,
|
file: value.$1!,
|
||||||
initialData: BaseExpenseInfo(
|
initialData: value.$2,
|
||||||
label: null,
|
|
||||||
cost: value.$2 ?? 0.0,
|
|
||||||
time: DateTime.now(),
|
|
||||||
),
|
|
||||||
onFinished: (expense) async {
|
onFinished: (expense) async {
|
||||||
await expenses.add(
|
await expenses.add(
|
||||||
info: expense,
|
info: expense,
|
||||||
|
@@ -22,6 +22,11 @@ class SettingsScreen extends ConsumerWidget {
|
|||||||
ref.invalidate(prefsProvider);
|
ref.invalidate(prefsProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleToggleDisableExtractDate(v) async {
|
||||||
|
await prefs.setDisableExtractDates(v);
|
||||||
|
ref.invalidate(prefsProvider);
|
||||||
|
}
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text('Settings')),
|
appBar: AppBar(title: const Text('Settings')),
|
||||||
body: ListView(
|
body: ListView(
|
||||||
@@ -40,6 +45,14 @@ class SettingsScreen extends ConsumerWidget {
|
|||||||
"Do not start camera automatically on application startup",
|
"Do not start camera automatically on application startup",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
SwitchListTile(
|
||||||
|
value: prefs.disableExtractDates(),
|
||||||
|
onChanged: handleToggleDisableExtractDate,
|
||||||
|
title: Text("Do not extract dates"),
|
||||||
|
subtitle: Text(
|
||||||
|
"Do not attempt to extract dates from scanned expenses",
|
||||||
|
),
|
||||||
|
),
|
||||||
const Divider(),
|
const Divider(),
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.info_outline),
|
leading: const Icon(Icons.info_outline),
|
||||||
|
@@ -23,6 +23,14 @@ extension MoneyMgrSharedPreferences on SharedPreferencesWithCache {
|
|||||||
await setBool("startOnScansListScreen", start);
|
await setBool("startOnScansListScreen", start);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool disableExtractDates() {
|
||||||
|
return getBool("disableExtractDates") ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> setDisableExtractDates(bool disable) async {
|
||||||
|
await setBool("disableExtractDates", disable);
|
||||||
|
}
|
||||||
|
|
||||||
ServerConfig? serverConfig() {
|
ServerConfig? serverConfig() {
|
||||||
final json = getString("serverConfig");
|
final json = getString("serverConfig");
|
||||||
if (json != null) return ServerConfig.fromJson(jsonDecode(json));
|
if (json != null) return ServerConfig.fromJson(jsonDecode(json));
|
||||||
|
@@ -5,9 +5,13 @@ import 'dart:ui' as ui;
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
|
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:moneymgr_mobile/services/storage/expenses.dart';
|
||||||
|
|
||||||
/// Attempt to extract total amount from invoice image
|
/// Attempt to extract information from invoice image
|
||||||
Future<double?> extractTotalFromBill(Uint8List imgBuff) async {
|
Future<BaseExpenseInfo?> extractInfoFromBill({
|
||||||
|
required Uint8List imgBuff,
|
||||||
|
required bool extractDates,
|
||||||
|
}) async {
|
||||||
final decodedImage = await decodeImageFromList(imgBuff);
|
final decodedImage = await decodeImageFromList(imgBuff);
|
||||||
|
|
||||||
final byteData = await decodedImage.toByteData(
|
final byteData = await decodedImage.toByteData(
|
||||||
@@ -25,20 +29,58 @@ Future<double?> extractTotalFromBill(Uint8List imgBuff) async {
|
|||||||
|
|
||||||
Logger.root.fine("Expense text: ${extractionResult.text}");
|
Logger.root.fine("Expense text: ${extractionResult.text}");
|
||||||
|
|
||||||
// Check for highest amount on invoice
|
// Check for highestCost amount on invoice
|
||||||
final regexp = RegExp(
|
final costRegexp = RegExp(
|
||||||
r'([0-9]+([ ]*(\\.|,)[ ]*[0-9]{1,2}){0,1})([ \\t\\n]*(EUR|eur|€)|E)',
|
r'([0-9]+([ ]*(\\.|,)[ ]*[0-9]{1,2}){0,1})([ \\t\\n]*(EUR|eur|€)|E)',
|
||||||
multiLine: true,
|
multiLine: true,
|
||||||
caseSensitive: false,
|
caseSensitive: false,
|
||||||
);
|
);
|
||||||
var highest = 0.0;
|
var highestCost = 0.0;
|
||||||
for (final match in regexp.allMatches(extractionResult.text)) {
|
for (final match in costRegexp.allMatches(extractionResult.text)) {
|
||||||
if (match.groupCount == 0) continue;
|
if (match.groupCount == 0) continue;
|
||||||
|
|
||||||
// Process only numeric value
|
// Process only numeric value
|
||||||
final value = (match.group(1) ?? "").replaceAll(",", ".");
|
final value = (match.group(1) ?? "").replaceAll(",", ".");
|
||||||
highest = max(highest, double.tryParse(value) ?? 0.0);
|
highestCost = max(highestCost, double.tryParse(value) ?? 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return highest == 0.0 ? null : highest;
|
// Check for highestCost amount on invoice
|
||||||
|
final dateRegexp = RegExp(
|
||||||
|
r'([0-3][0-9])(\/|-)([0-1][0-9])(\/|-)((20|)[0-9]{2})',
|
||||||
|
multiLine: false,
|
||||||
|
caseSensitive: false,
|
||||||
|
);
|
||||||
|
final currDate = DateTime.now();
|
||||||
|
DateTime? newest;
|
||||||
|
for (final match in dateRegexp.allMatches(extractionResult.text)) {
|
||||||
|
if (match.groupCount < 6) continue;
|
||||||
|
|
||||||
|
int year = int.tryParse(match.group(5)!) ?? currDate.year;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final date = DateTime(
|
||||||
|
year > 99 ? year : (2000 + year),
|
||||||
|
int.tryParse(match.group(3)!) ?? currDate.month,
|
||||||
|
int.tryParse(match.group(1)!) ?? currDate.day,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (newest == null) {
|
||||||
|
newest = date;
|
||||||
|
} else {
|
||||||
|
newest = DateTime.fromMillisecondsSinceEpoch(
|
||||||
|
max(newest.millisecondsSinceEpoch, date.millisecondsSinceEpoch),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e, s) {
|
||||||
|
Logger.root.warning("Failed to parse date! $e$s");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return BaseExpenseInfo(
|
||||||
|
label: null,
|
||||||
|
cost: highestCost,
|
||||||
|
time: extractDates && (newest?.isBefore(currDate) ?? false)
|
||||||
|
? newest!
|
||||||
|
: currDate,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@@ -40,6 +40,11 @@ class ExpenseEditor extends HookConsumerWidget {
|
|||||||
|
|
||||||
final (:pending, :snapshot, :hasError) = useAsyncTask();
|
final (:pending, :snapshot, :hasError) = useAsyncTask();
|
||||||
|
|
||||||
|
// Clear cost value
|
||||||
|
handleClearCost() {
|
||||||
|
costController.text = "";
|
||||||
|
}
|
||||||
|
|
||||||
// Pick a new date
|
// Pick a new date
|
||||||
handlePickDate() async {
|
handlePickDate() async {
|
||||||
final date = await showDatePicker(
|
final date = await showDatePicker(
|
||||||
@@ -94,6 +99,19 @@ class ExpenseEditor extends HookConsumerWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Open invoice in full screen
|
||||||
|
handleFullScreenInvoice() {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (c) => Scaffold(
|
||||||
|
appBar: AppBar(title: Text("Expense")),
|
||||||
|
body: SingleChildScrollView(
|
||||||
|
child: PDFViewer(pdfBytes: file, fit: BoxFit.fitWidth),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text("Expense info"),
|
title: Text("Expense info"),
|
||||||
@@ -125,7 +143,10 @@ class ExpenseEditor extends HookConsumerWidget {
|
|||||||
children: [
|
children: [
|
||||||
// Expense preview
|
// Expense preview
|
||||||
Expanded(
|
Expanded(
|
||||||
child: PDFViewer(pdfBytes: file, fit: BoxFit.contain),
|
child: GestureDetector(
|
||||||
|
onTap: handleFullScreenInvoice,
|
||||||
|
child: PDFViewer(pdfBytes: file, fit: BoxFit.contain),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
SizedBox(height: 10),
|
SizedBox(height: 10),
|
||||||
@@ -137,7 +158,13 @@ class ExpenseEditor extends HookConsumerWidget {
|
|||||||
decimal: true,
|
decimal: true,
|
||||||
signed: false,
|
signed: false,
|
||||||
),
|
),
|
||||||
decoration: const InputDecoration(labelText: 'Cost'),
|
decoration: InputDecoration(
|
||||||
|
labelText: 'Cost',
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
onPressed: handleClearCost,
|
||||||
|
icon: const Icon(Icons.clear),
|
||||||
|
),
|
||||||
|
),
|
||||||
textInputAction: TextInputAction.done,
|
textInputAction: TextInputAction.done,
|
||||||
),
|
),
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user