Compare commits
1 Commits
1.0.5
...
c779c8dc9a
Author | SHA1 | Date | |
---|---|---|---|
c779c8dc9a |
@@ -12,7 +12,7 @@ steps:
|
|||||||
|
|
||||||
# Frontend
|
# Frontend
|
||||||
- name: web_build
|
- name: web_build
|
||||||
image: node:23
|
image: node:24
|
||||||
depends_on:
|
depends_on:
|
||||||
- fetch
|
- fetch
|
||||||
volumes:
|
volumes:
|
||||||
|
4
moneymgr_backend/Cargo.lock
generated
4
moneymgr_backend/Cargo.lock
generated
@@ -3232,9 +3232,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.141"
|
version = "1.0.140"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "30b9eff21ebe718216c6ec64e1d9ac57087aad11efc64e32002bce4a0d4c03d3"
|
checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"memchr",
|
"memchr",
|
||||||
|
@@ -22,7 +22,7 @@ rust-s3 = "0.36.0-beta.2"
|
|||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
tokio = "1.45.1"
|
tokio = "1.45.1"
|
||||||
futures-util = "0.3.31"
|
futures-util = "0.3.31"
|
||||||
serde_json = "1.0.141"
|
serde_json = "1.0.140"
|
||||||
light-openid = "1.0.4"
|
light-openid = "1.0.4"
|
||||||
rand = "0.9.1"
|
rand = "0.9.1"
|
||||||
ipnet = { version = "2.11.0", features = ["serde"] }
|
ipnet = { version = "2.11.0", features = ["serde"] }
|
||||||
|
@@ -1,29 +1,45 @@
|
|||||||
|
import 'dart:io';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
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/pdf_utils.dart';
|
|
||||||
import 'package:moneymgr_mobile/widgets/expense_editor.dart';
|
import 'package:moneymgr_mobile/widgets/expense_editor.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
|
import 'package:scanbot_sdk/scanbot_sdk.dart';
|
||||||
|
import 'package:scanbot_sdk/scanbot_sdk_ui_v2.dart' hide IconButton, EdgeInsets;
|
||||||
|
|
||||||
part 'scan_screen.g.dart';
|
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?, BaseExpenseInfo?)> _scanDocument(Ref ref) async {
|
Future<Uint8List?> _scanDocument(Ref ref) async {
|
||||||
final prefs = ref.watch(prefsProvider).requireValue;
|
var configuration = DocumentScanningFlow(
|
||||||
|
appearance: DocumentFlowAppearanceConfiguration(
|
||||||
final pdf = await scanDocAsPDF();
|
statusBarMode: StatusBarMode.DARK,
|
||||||
final img = await renderPdf(pdfBytes: pdf);
|
),
|
||||||
final amount = await extractInfoFromBill(
|
cleanScanningSession: true,
|
||||||
imgBuff: img,
|
outputSettings: DocumentScannerOutputSettings(pagesScanLimit: 1),
|
||||||
extractDates: !prefs.disableExtractDates(),
|
screens: DocumentScannerScreens(
|
||||||
|
review: ReviewScreenConfiguration(enabled: false),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
return (pdf, amount);
|
var documentResult = await ScanbotSdkUiV2.startDocumentScanner(configuration);
|
||||||
|
|
||||||
|
if (documentResult.status != OperationStatus.OK) {
|
||||||
|
throw Exception("Scanner failed with status ${documentResult.status}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert result to PDF
|
||||||
|
var result = await ScanbotSdk.document.createPDFForDocument(
|
||||||
|
PDFFromDocumentParams(
|
||||||
|
documentID: documentResult.data!.uuid,
|
||||||
|
pdfConfiguration: PdfConfiguration(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final pdfPath = result.pdfFileUri.replaceFirst("file://", "");
|
||||||
|
return File(pdfPath).readAsBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
class ScanScreen extends HookConsumerWidget {
|
class ScanScreen extends HookConsumerWidget {
|
||||||
@@ -46,13 +62,12 @@ class ScanScreen extends HookConsumerWidget {
|
|||||||
return Padding(
|
return Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
child: switch (scanDocProvider) {
|
child: switch (scanDocProvider) {
|
||||||
AsyncData(:final value) when value.$1 != null => ExpenseEditor(
|
AsyncData(:final value) when value != null => ExpenseEditor(
|
||||||
file: value.$1!,
|
file: value,
|
||||||
initialData: value.$2,
|
|
||||||
onFinished: (expense) async {
|
onFinished: (expense) async {
|
||||||
await expenses.add(
|
await expenses.add(
|
||||||
info: expense,
|
info: expense,
|
||||||
fileContent: value.$1!,
|
fileContent: value,
|
||||||
fileMimeType: "application/pdf",
|
fileMimeType: "application/pdf",
|
||||||
);
|
);
|
||||||
restartScan();
|
restartScan();
|
||||||
@@ -61,7 +76,7 @@ class ScanScreen extends HookConsumerWidget {
|
|||||||
),
|
),
|
||||||
|
|
||||||
// No data
|
// No data
|
||||||
AsyncData(:final value) when value.$1 == null => ScanErrorScreen(
|
AsyncData(:final value) when value == null => ScanErrorScreen(
|
||||||
message: "No document scanned!",
|
message: "No document scanned!",
|
||||||
onTryAgain: restartScan,
|
onTryAgain: restartScan,
|
||||||
),
|
),
|
||||||
|
@@ -22,11 +22,6 @@ 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(
|
||||||
@@ -45,14 +40,6 @@ 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,14 +23,6 @@ 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));
|
||||||
|
@@ -1,86 +0,0 @@
|
|||||||
import 'dart:math';
|
|
||||||
import 'dart:typed_data';
|
|
||||||
import 'dart:ui' as ui;
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
|
|
||||||
import 'package:logging/logging.dart';
|
|
||||||
import 'package:moneymgr_mobile/services/storage/expenses.dart';
|
|
||||||
|
|
||||||
/// Attempt to extract information from invoice image
|
|
||||||
Future<BaseExpenseInfo?> extractInfoFromBill({
|
|
||||||
required Uint8List imgBuff,
|
|
||||||
required bool extractDates,
|
|
||||||
}) async {
|
|
||||||
final decodedImage = await decodeImageFromList(imgBuff);
|
|
||||||
|
|
||||||
final byteData = await decodedImage.toByteData(
|
|
||||||
format: ui.ImageByteFormat.rawRgba,
|
|
||||||
);
|
|
||||||
|
|
||||||
final image = InputImage.fromBitmap(
|
|
||||||
bitmap: byteData!.buffer.asUint8List(),
|
|
||||||
width: decodedImage.width,
|
|
||||||
height: decodedImage.height,
|
|
||||||
);
|
|
||||||
|
|
||||||
final textRecognizer = TextRecognizer(script: TextRecognitionScript.latin);
|
|
||||||
final extractionResult = await textRecognizer.processImage(image);
|
|
||||||
|
|
||||||
Logger.root.fine("Expense text: ${extractionResult.text}");
|
|
||||||
|
|
||||||
// Check for highestCost amount on invoice
|
|
||||||
final costRegexp = RegExp(
|
|
||||||
r'([0-9]+([ ]*(\\.|,)[ ]*[0-9]{1,2}){0,1})([ \\t\\n]*(EUR|eur|€)|E)',
|
|
||||||
multiLine: true,
|
|
||||||
caseSensitive: false,
|
|
||||||
);
|
|
||||||
var highestCost = 0.0;
|
|
||||||
for (final match in costRegexp.allMatches(extractionResult.text)) {
|
|
||||||
if (match.groupCount == 0) continue;
|
|
||||||
|
|
||||||
// Process only numeric value
|
|
||||||
final value = (match.group(1) ?? "").replaceAll(",", ".");
|
|
||||||
highestCost = max(highestCost, double.tryParse(value) ?? 0.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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,
|
|
||||||
);
|
|
||||||
}
|
|
@@ -6,40 +6,14 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:pdf_image_renderer/pdf_image_renderer.dart';
|
import 'package:pdf_image_renderer/pdf_image_renderer.dart';
|
||||||
import 'package:scanbot_sdk/scanbot_sdk.dart';
|
|
||||||
import 'package:scanbot_sdk/scanbot_sdk_ui_v2.dart' hide IconButton, EdgeInsets;
|
|
||||||
|
|
||||||
/// Scan document as PDF
|
|
||||||
Future<Uint8List> scanDocAsPDF() async {
|
|
||||||
var configuration = DocumentScanningFlow(
|
|
||||||
appearance: DocumentFlowAppearanceConfiguration(
|
|
||||||
statusBarMode: StatusBarMode.DARK,
|
|
||||||
),
|
|
||||||
cleanScanningSession: true,
|
|
||||||
outputSettings: DocumentScannerOutputSettings(pagesScanLimit: 1),
|
|
||||||
screens: DocumentScannerScreens(
|
|
||||||
review: ReviewScreenConfiguration(enabled: false),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
var documentResult = await ScanbotSdkUiV2.startDocumentScanner(configuration);
|
|
||||||
|
|
||||||
if (documentResult.status != OperationStatus.OK) {
|
|
||||||
throw Exception("Scanner failed with status ${documentResult.status}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert result to PDF
|
|
||||||
var result = await ScanbotSdk.document.createPDFForDocument(
|
|
||||||
PDFFromDocumentParams(
|
|
||||||
documentID: documentResult.data!.uuid,
|
|
||||||
pdfConfiguration: PdfConfiguration(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
final pdfPath = result.pdfFileUri.replaceFirst("file://", "");
|
|
||||||
return File(pdfPath).readAsBytes();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Render PDF to image bits
|
/// Render PDF to image bits
|
||||||
Future<Uint8List> renderPdf({String? path, Uint8List? pdfBytes}) async {
|
Future<Uint8List> renderPdf(
|
||||||
|
{
|
||||||
|
String? path,
|
||||||
|
Uint8List? pdfBytes,
|
||||||
|
}) async {
|
||||||
assert(path != null || pdfBytes != null);
|
assert(path != null || pdfBytes != null);
|
||||||
|
|
||||||
// Create temporary file if required
|
// Create temporary file if required
|
||||||
@@ -88,4 +62,4 @@ Future<Uint8List> renderPdf({String? path, Uint8List? pdfBytes}) async {
|
|||||||
await File(path).delete();
|
await File(path).delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -40,11 +40,6 @@ 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(
|
||||||
@@ -99,19 +94,6 @@ 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"),
|
||||||
@@ -143,10 +125,7 @@ class ExpenseEditor extends HookConsumerWidget {
|
|||||||
children: [
|
children: [
|
||||||
// Expense preview
|
// Expense preview
|
||||||
Expanded(
|
Expanded(
|
||||||
child: GestureDetector(
|
child: PDFViewer(pdfBytes: file, fit: BoxFit.contain),
|
||||||
onTap: handleFullScreenInvoice,
|
|
||||||
child: PDFViewer(pdfBytes: file, fit: BoxFit.contain),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
|
||||||
SizedBox(height: 10),
|
SizedBox(height: 10),
|
||||||
@@ -158,13 +137,7 @@ class ExpenseEditor extends HookConsumerWidget {
|
|||||||
decimal: true,
|
decimal: true,
|
||||||
signed: false,
|
signed: false,
|
||||||
),
|
),
|
||||||
decoration: InputDecoration(
|
decoration: const InputDecoration(labelText: 'Cost'),
|
||||||
labelText: 'Cost',
|
|
||||||
suffixIcon: IconButton(
|
|
||||||
onPressed: handleClearCost,
|
|
||||||
icon: const Icon(Icons.clear),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
textInputAction: TextInputAction.done,
|
textInputAction: TextInputAction.done,
|
||||||
),
|
),
|
||||||
|
|
||||||
|
@@ -488,22 +488,6 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "16.0.0"
|
version: "16.0.0"
|
||||||
google_mlkit_commons:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: google_mlkit_commons
|
|
||||||
sha256: "8f40fbac10685cad4715d11e6a0d86837d9ad7168684dfcad29610282a88e67a"
|
|
||||||
url: "https://pub.dev"
|
|
||||||
source: hosted
|
|
||||||
version: "0.11.0"
|
|
||||||
google_mlkit_text_recognition:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: google_mlkit_text_recognition
|
|
||||||
sha256: "96173ad4dd7fd06c660e22ac3f9e9f1798a517fe7e48bee68eeec83853224224"
|
|
||||||
url: "https://pub.dev"
|
|
||||||
source: hosted
|
|
||||||
version: "0.15.0"
|
|
||||||
graphs:
|
graphs:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@@ -93,9 +93,6 @@ dependencies:
|
|||||||
# PDF renderer
|
# PDF renderer
|
||||||
pdf_image_renderer: ^1.0.1
|
pdf_image_renderer: ^1.0.1
|
||||||
|
|
||||||
# Text extraction
|
|
||||||
google_mlkit_text_recognition: ^0.15.0
|
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
Reference in New Issue
Block a user