public void Save(Document document) { var invoiceProcessingContext = new InvoiceProcessingContext(); invoiceProcessingContext.Text = document.Text; invoiceProcessingContext.Invoice = new Invoice() { Id = document.Id }; var tasks = new List <IInvoiceProcessingTask>(); tasks.Add(new AnalyzeEntities()); tasks.Add(new DetermineInvoiceAmountDueTask()); tasks.Add(new DetermineInvoiceTotalAmountTask()); tasks.Add(new DetermineInvoiceCurrency()); tasks.Add(new DetermineInvoiceDate()); InvoiceProcessingTaskExecutionHelper.ExecuteTasks(invoiceProcessingContext, tasks); if (invoiceProcessingContext.Invoice.AmountDue == 0) { invoiceProcessingContext.Invoice.ProcessingStatus = ProcessingStatus.Paid; } _invoiceDatasource.Save(invoiceProcessingContext.Invoice); }
public void Execute(InvoiceProcessingContext invoiceProcessingContext) { if (invoiceProcessingContext.AnalyzeEntitiesResponse == null) { return; } var currencies = invoiceProcessingContext.AnalyzeEntitiesResponse.Entities.Where(u => Convert.ToInt32(u.Type) == 13 && u.Metadata.ContainsKey("currency")); if (currencies.Any()) { var containedCurrencies = new List <string>(); foreach (var currency in _supportedCurrencies) { if (invoiceProcessingContext.Text.Contains(currency)) { containedCurrencies.Add(currency); } } if (containedCurrencies.Count > 1) { invoiceProcessingContext.Invoice.Currency = currencies.GroupBy(u => u.Metadata["currency"]).OrderByDescending(v => v.Count()) .FirstOrDefault().Key; } else { invoiceProcessingContext.Invoice.Currency = containedCurrencies.FirstOrDefault(); } } }
public static void ExecuteTasks(InvoiceProcessingContext invoiceProcessingContext, IList <IInvoiceProcessingTask> tasks) { if (tasks == null || tasks.Count == 0) { return; } foreach (var task in tasks) { task.Execute(invoiceProcessingContext); } }
public void Execute(InvoiceProcessingContext invoiceProcessingContext) { if (invoiceProcessingContext.AnalyzeEntitiesResponse == null) { return; } var dates = invoiceProcessingContext.AnalyzeEntitiesResponse.Entities.Where(u => Convert.ToInt32(u.Type) == 11 && u.Metadata.ContainsKey("year") && u.Metadata.ContainsKey("month") && u.Metadata.ContainsKey("day")); if (dates.Any()) { var invoiceDates = new List <DateTime>(); dates.ToList().ForEach(u => { invoiceDates.Add(new DateTime(Convert.ToInt32(u.Metadata["year"]), Convert.ToInt32(u.Metadata["month"]), Convert.ToInt32(u.Metadata["day"]))); }); invoiceProcessingContext.Invoice.InvoiceDate = invoiceDates.Min(); } }
public void Execute(InvoiceProcessingContext invoiceProcessingContext) { decimal returnValue = 0; MatchCollection matches = Regex.Matches(invoiceProcessingContext.Text, @"((\+|\-)?[$€£])\s*(\d[.\d,]*)"); var amounts = new List <decimal>(); var amountDictionary = new Dictionary <decimal, List <int> >(); for (int i = 0; i < matches.Count; i++) { var str = matches[i].ToString(); var num = 0; var formattedAmountAsString = str.Replace("$", "").Replace("£", ""); decimal amount = 0; if (decimal.TryParse(formattedAmountAsString, out amount)) { amounts.Add(amount); } BuildAmountDictionary(i, amount, amountDictionary); } SetIndexForAmountsToWhichTheyCanBeFormedByAddingOtherAmountsConsecutively(amounts, amountDictionary); int maxIndex = 0; foreach (var item in amountDictionary) { foreach (var index in item.Value) { if (index > maxIndex) { maxIndex = index; returnValue = item.Key; } } } SetAmount(invoiceProcessingContext, returnValue); }
protected override void SetAmount(InvoiceProcessingContext invoiceProcessingContext, decimal amount) { invoiceProcessingContext.Invoice.AmountDue = amount; }
protected virtual void SetAmount(InvoiceProcessingContext invoiceProcessingContext, decimal amount) { throw new NotImplementedException(); }