Can extract date of expenses
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		@@ -5,9 +5,10 @@ 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 total amount from invoice image
 | 
			
		||||
Future<double?> extractTotalFromBill(Uint8List imgBuff) async {
 | 
			
		||||
/// Attempt to extract information from invoice image
 | 
			
		||||
Future<BaseExpenseInfo?> extractInfoFromBill(Uint8List imgBuff) async {
 | 
			
		||||
  final decodedImage = await decodeImageFromList(imgBuff);
 | 
			
		||||
 | 
			
		||||
  final byteData = await decodedImage.toByteData(
 | 
			
		||||
@@ -25,20 +26,54 @@ Future<double?> extractTotalFromBill(Uint8List imgBuff) async {
 | 
			
		||||
 | 
			
		||||
  Logger.root.fine("Expense text: ${extractionResult.text}");
 | 
			
		||||
 | 
			
		||||
  // Check for highest amount on invoice
 | 
			
		||||
  final regexp = RegExp(
 | 
			
		||||
  // 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 highest = 0.0;
 | 
			
		||||
  for (final match in regexp.allMatches(extractionResult.text)) {
 | 
			
		||||
  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(",", ".");
 | 
			
		||||
    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: true,
 | 
			
		||||
    caseSensitive: false,
 | 
			
		||||
  );
 | 
			
		||||
  final currDate = DateTime.now();
 | 
			
		||||
  DateTime? newest;
 | 
			
		||||
  for (final match in dateRegexp.allMatches(extractionResult.text)) {
 | 
			
		||||
    if (match.groupCount < 5) continue;
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      final date = DateTime(
 | 
			
		||||
        int.tryParse(match.group(5)!) ?? currDate.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: newest?.isBefore(currDate) ?? false ? newest! : currDate,
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user