Exemplo n.º 1
0
        private void ValidateParsedItems(HashSet <IBankovniPolozka> polozky, StatementOverview overview)
        {
            var currentCredit = polozky.Sum(p => p.Castka > 0 ? p.Castka : 0);

            if (overview.CreditSum != currentCredit)
            {
                throw new ApplicationException(
                          $"Invalid total credit (expected {overview.CreditSum}, found {currentCredit}) - {Ucet.Url}");
            }

            var currentDebit = polozky.Sum(p => p.Castka < 0 ? p.Castka : 0);

            if (overview.DebitSum != currentDebit)
            {
                throw new ApplicationException(
                          $"Invalid total debit (expected {overview.DebitSum}, found {currentDebit}) - {Ucet.Url}");
            }

            var currentFinalBalance = overview.OpeningBalance + currentCredit + currentDebit;

            if (overview.FinalBalance != currentFinalBalance)
            {
                throw new ApplicationException(
                          $"Invalid final balance (expected {overview.FinalBalance}, found {currentFinalBalance}) - {Ucet.Url}");
            }
        }
Exemplo n.º 2
0
        private static void ValidateParsedItems(string sourceUrl, StatementOverview overview, List <IBankovniPolozka> statementItems)
        {
            var currentCreditItems = statementItems.Count(i => i.Castka > 0);

            if (overview.CreditItems != currentCreditItems)
            {
                throw new ApplicationException(
                          $"Invalid count of credit items (expected {overview.CreditItems}, found {currentCreditItems}) - {sourceUrl}");
            }
            var currentDebitItems = statementItems.Count(i => i.Castka < 0);

            if (overview.DebitItems != currentDebitItems)
            {
                throw new ApplicationException(
                          $"Invalid count of debit items (expected {overview.DebitItems}, found {currentDebitItems}) - {sourceUrl}");
            }
            var currentFinalBalance = overview.OpeningBalance + statementItems.Sum(i => i.Castka);

            if (overview.FinalBalance != currentFinalBalance)
            {
                throw new ApplicationException(
                          $"Invalid final balance (expected {overview.FinalBalance}, found {currentFinalBalance}) - {sourceUrl}");
            }
        }
Exemplo n.º 3
0
        private List <IBankovniPolozka> ParseStatement(string textFile, DateTime date, string sourceUrl)
        {
            var statementItems = new List <IBankovniPolozka>();
            var data           = File.ReadAllLines(textFile);
            var item           = (StatementItemRecord)null;
            var overview       = new StatementOverview();
            var positions      = new StatementItemsPositions();

            foreach (var line in data)
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }
                if (line.Contains("Změna úrokové sazby"))
                {
                    continue;
                }

                if (line.StartsWith("Počet kreditních položek"))
                {
                    overview.CreditItems = int.Parse(ItemsPattern.Match(GetValue(line, CreditItemsPosition)).Groups[1].Value);
                    if (!line.Contains(OpeningBalanceText))
                    {
                        continue;
                    }
                }
                if (line.StartsWith("Počet debetních položek"))
                {
                    overview.DebitItems = int.Parse(ItemsPattern.Match(GetValue(line, DebitItemsPosition)).Groups[1].Value);
                    continue;
                }
                if (line.Contains(OpeningBalanceText))
                {
                    overview.OpeningBalance = ParseTools.FromTextToDecimal(GetValue(line, new PositionIndex(line.IndexOf(OpeningBalanceText, SC) + OpeningBalanceText.Length + 1, default(int?)))) ?? 0;
                    continue;
                }
                if (line.Trim().StartsWith(FinalBalanceText))
                {
                    overview.FinalBalance = ParseTools.FromTextToDecimal(GetValue(line, new PositionIndex(line.IndexOf(FinalBalanceText, SC) + FinalBalanceText.Length + 1, default(int?)))) ?? 0;
                    continue;
                }

                if (line.StartsWith("Datum"))
                {
                    DefineItemsPositionsForFirstLine(positions, line);
                    continue;
                }
                if (line.StartsWith("Valuta"))
                {
                    DefineItemsPositionsForSecondLine(positions, line);
                    continue;
                }

                if (item != null && (line[0] != ' ' || line.Trim().StartsWith("Převádí se") || line.Trim().StartsWith("Konec výpisu")))
                {
                    statementItems.Add(item.ToBankPolozka(date, Ucet, sourceUrl));
                    item = null;
                }

                if (StatementItemPattern.IsMatch(line))
                {
                    item = CreateStatementItem(line, positions);
                }
                else if (item != null && string.IsNullOrEmpty(item.ZpravaProPrijemce) && AccountNumberPattern.IsMatch(line))
                {
                    UpdateStatementItem(item, line, positions);
                }
                else if (item != null)
                {
                    item.ZpravaProPrijemce += (string.IsNullOrEmpty(item.ZpravaProPrijemce)
                                                                                                  ? string.Empty
                                                                                                  : Environment.NewLine) + line.Trim();
                }
            }

            ValidateParsedItems(sourceUrl, overview, statementItems);

            return(statementItems);
        }
Exemplo n.º 4
0
        private IEnumerable <IBankovniPolozka> ParseStatement(string url)
        {
            var polozky = new HashSet <IBankovniPolozka>();

            using (var net = new Devmasters.Net.HttpClient.URLContent(url))
            {
                net.IgnoreHttpErrors = true;
                var content = net.GetContent(Encoding.UTF8).Text;
                if (content.Contains("Některé pohyby nemusí být zobrazeny. Zmenšete datumový rozsah."))
                {
                    throw new StatementTooLongException();
                }
                var doc = new Devmasters.XPath(content);

                var xoverviewRows = "//div[contains(@class, 'pohybySum')]/table/tbody/tr";
                var overviewRows  = doc.GetNodes(xoverviewRows)?.Count ?? 0;
                if (overviewRows == 0)
                {
                    TULogger.Warning($"FIO: Account statement page was not found for account {Ucet.CisloUctu}. Account has been probably canceled. Url: {url}");
                    return(new List <IBankovniPolozka>());
                }

                var overview = new StatementOverview
                {
                    OpeningBalance = parseAmount(doc.GetNodeText(xoverviewRows + "/td[1]")),
                    FinalBalance   = parseAmount(doc.GetNodeText(xoverviewRows + "/td[2]")),
                    CreditSum      = parseAmount(doc.GetNodeText(xoverviewRows + "/td[3]")),
                    DebitSum       = parseAmount(doc.GetNodeText(xoverviewRows + "/td[4]"))
                };

                var xrows = "//table[@class='table' and starts-with(@id,'id')]/tbody/tr";
                var rows  = doc.GetNodes(xrows)?.Count ?? 0;
                for (var row = 1; row <= rows; row++)
                {
                    var xroot = xrows + "[" + row + "]";

                    var p = new SimpleBankovniPolozka
                    {
                        CisloUctu         = Ucet.CisloUctu,
                        Datum             = Devmasters.DT.Util.ToDateTime(doc.GetNodeText(xroot + "/td[1]"), "dd.MM.yyyy").Value,
                        Castka            = parseAmount(System.Net.WebUtility.HtmlDecode(doc.GetNodeText(xroot + "/td[2]"))),
                        PopisTransakce    = System.Net.WebUtility.HtmlDecode(doc.GetNodeText(xroot + "/td[3]")),
                        NazevProtiuctu    = System.Net.WebUtility.HtmlDecode(doc.GetNodeText(xroot + "/td[4]")),
                        ZpravaProPrijemce = Devmasters.TextUtil.NormalizeToBlockText(
                            System.Net.WebUtility.HtmlDecode(doc.GetNodeHtml(xroot + "/td[5]"))
                            ?.Replace("<br>", " \n")
                            )
                    };

                    var poznamka = Devmasters.TextUtil.NormalizeToBlockText(
                        System.Net.WebUtility.HtmlDecode(doc.GetNodeHtml(xroot + "/td[9]"))
                        ?.Replace("<br>", " \n")
                        );

                    if (poznamka != p.ZpravaProPrijemce)
                    {
                        p.ZpravaProPrijemce += " " + poznamka;
                    }

                    p.KS       = doc.GetNodeText(xroot + "/td[6]");
                    p.VS       = doc.GetNodeText(xroot + "/td[7]");
                    p.SS       = doc.GetNodeText(xroot + "/td[8]");
                    p.ZdrojUrl = net.Url;


                    p.CisloProtiuctu = ""; //neni k dispozici

                    if (!polozky.Contains(p))
                    {
                        polozky.Add(p);
                    }
                }

                ValidateParsedItems(polozky, overview);
            }

            return(polozky);
        }