public IEnumerable <FieldResult> Aggregate(InvoiceTemplate template, InvoiceField field, IEnumerable <FieldResult> results)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            if (field?.Key == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (results == null)
            {
                throw new ArgumentNullException(nameof(results));
            }

            var allItems = results.ToArray();

            if (allItems.Length > 0)
            {
                foreach (var item in allItems)
                {
                    if (!GetValue(template, item, out var date))
                    {
                        continue;
                    }

                    yield return(new FieldResult(field.Key, date.ToShortDateString()));

                    break;
                }
            }
        }
        public IEnumerable <FieldResult> Extract(InvoiceField field, Document document)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var handler = inner.FirstOrDefault(item => item.CanHandle(field));

            if (handler == null)
            {
                yield break;
            }

            var result = handler.Extract(field, document);

            foreach (var item in result)
            {
                yield return(item);
            }
        }
Example #3
0
 public void SetUp()
 {
     first    = new Mock <IFieldExtractor>();
     second   = new Mock <IFieldExtractor>();
     document = new Document("Test");
     field    = new InvoiceField();
     instance = CreateCombinedFieldExtractor();
 }
        public bool CanHandle(InvoiceField field)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            return(inner.Any(item => item.CanHandle(field)));
        }
Example #5
0
        public bool CanHandle(InvoiceField field)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            return(true);
        }
        public bool CanHandle(InvoiceField field)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            return(field.Key.StartsWith("date", StringComparison.OrdinalIgnoreCase) || field.Key.EndsWith("date", StringComparison.OrdinalIgnoreCase));
        }
Example #7
0
        public void AggregateError()
        {
            var field = new InvoiceField {
                Key = "date"
            };
            var result = instance.Aggregate(template, field, new[] { new FieldResult("1", "x"), new FieldResult("1", "a") }).ToArray();

            Assert.AreEqual(0, result.Length);
        }
Example #8
0
        public void AggregateSum()
        {
            var field = new InvoiceField {
                Key = "sum_amount"
            };
            var result = instance.Aggregate(template, field, new[] { new FieldResult("1", "2"), new FieldResult("1", "3") }).ToArray();

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual("5", result[0].Value);
        }
Example #9
0
 public void SetUp()
 {
     document           = new Document("Test document");
     staticField        = new InvoiceField();
     staticField.Key    = "static_invoice";
     staticField.Values = new[] { "Origin" };
     otherField         = new InvoiceField();
     otherField.Key     = "invoice";
     otherField.Values  = new[] { "Origin" };
     instance           = CreateStaticFieldExtractor();
 }
Example #10
0
        public void Aggregate()
        {
            var field = new InvoiceField {
                Key = "date"
            };

            template.Options             = new InvoiceTemplateOptions();
            template.Options.DateFormats = new[] { "yyyy-MM-dd" };
            var result = instance.Aggregate(template, field, new[] { new FieldResult("1", "2015-10-01"), new FieldResult("1", "2015-12-01") }).ToArray();

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(new DateTime(2015, 10, 01).ToShortDateString(), result[0].Value);
        }
Example #11
0
        public void AggregateWithSeparator()
        {
            var field = new InvoiceField {
                Key = "amount"
            };

            template.Options = new InvoiceTemplateOptions();
            template.Options.DecimalSeparator = ":";
            var result = instance.Aggregate(template, field, new[] { new FieldResult("1", "2:3") }).ToArray();

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual("2.3", result[0].Value);
        }
Example #12
0
        public IEnumerable <FieldResult> Aggregate(InvoiceTemplate template, InvoiceField field, IEnumerable <FieldResult> results)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            if (field?.Key == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (results == null)
            {
                throw new ArgumentNullException(nameof(results));
            }

            double value    = 0;
            var    allItems = results.ToArray();
            bool   found    = false;

            if (allItems.Length > 0)
            {
                if (field.Key.StartsWith("sum_amount", StringComparison.OrdinalIgnoreCase))
                {
                    foreach (var item in allItems)
                    {
                        if (GetValue(template, item, out var calculated))
                        {
                            value += calculated;
                            found  = true;
                        }
                    }
                }
                else
                {
                    if (GetValue(template, allItems[0], out value))
                    {
                        found = true;
                    }
                }
            }

            if (found)
            {
                yield return(new FieldResult(field.Key, value.ToString(CultureInfo.CurrentCulture)));
            }
        }
Example #13
0
        public IEnumerable <FieldResult> Extract(InvoiceField field, Document document)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            foreach (var value in field.Values)
            {
                var match = Regex.Matches(document.Text, value, RegexOptions.IgnoreCase);
                foreach (Match matchGroup in match)
                {
                    yield return(new FieldResult(field.Key, matchGroup.Groups[matchGroup.Groups.Count - 1].Value));
                }
            }
        }
Example #14
0
        public IEnumerable <FieldResult> Extract(InvoiceField field, Document document)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (!CanHandle(field))
            {
                yield break;
            }

            var key = field.Key.Substring(7);

            foreach (var value in field.Values)
            {
                yield return(new FieldResult(key, value));
            }
        }
Example #15
0
        private IEnumerable <FieldResult> ProcessField(InvoiceTemplate template, Document document, InvoiceField field)
        {
            var result = extractor.Extract(field, document).ToArray();

            foreach (var aggregator in aggregators)
            {
                if (!aggregator.CanHandle(field))
                {
                    continue;
                }

                IEnumerable <FieldResult> aggregated = aggregator.Aggregate(template, field, result);
                foreach (FieldResult item in aggregated)
                {
                    yield return(item);
                }

                yield break;
            }

            foreach (var fieldResult in result)
            {
                yield return(fieldResult);
            }
        }