private void UpdateStatementItem(StatementItemRecord item, string line, StatementItemsPositions positions) { item.CisloProtiuctu = GetValue(line, positions.CisloProtiuctu).Trim().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).First(); item.VS = GetValue(line, positions.VS).Trim(); item.KS = GetValue(line, positions.KS).Trim(); ParseSS(item, line, positions); item.ZpravaProPrijemce = string.Empty; }
private decimal ParseAmount(string line, StatementItemsPositions positions) { var amount = GetValue(line, positions.Castka).Trim(); return(ParseTools.FromTextToDecimal(amount.Trim()) ?? throw new ApplicationException( $"Amount on line '{line}' on position {positions.Castka} could not be read ({amount})")); }
private void ParseSS(StatementItemRecord item, string line, StatementItemsPositions positions) { var ss = GetValue(line, positions.SS); if (!string.IsNullOrEmpty(ss) && !ss.StartsWith(" ")) { item.SS = ss.IndexOf(" ", SC) > 0 ? ss.Substring(0, ss.IndexOf(" ", SC)) : ss; } }
private StatementItemRecord CreateStatementItem(string line, StatementItemsPositions positions) { return(new StatementItemRecord { AddId = GetValue(line, positions.AddId).Trim(), Datum = GetValue(line, positions.Datum).Trim(), PopisTransakce = GetValue(line, positions.PopisTransakce).Trim(), NazevProtiuctu = GetValue(line, positions.NazevProtiuctu).Trim(), Castka = ParseAmount(line, positions) }); }
private void DefineItemsPositionsForSecondLine(StatementItemsPositions positions, string line) { positions.CisloProtiuctu = new PositionIndex(line.IndexOf("Protiúčet nebo poznámka", SC), 50); positions.SS = new PositionIndex(line.IndexOf("SS", SC), default(int?)); if (line.IndexOf("KS", SC) > 0) { positions.KS = new PositionIndex(line.IndexOf("KS", SC), 4); } var vsPosition = line.IndexOf("VS", SC); positions.VS = new PositionIndex(vsPosition, positions.KS.Start - vsPosition - 1); }
private void DefineItemsPositionsForFirstLine(StatementItemsPositions positions, string line) { if (line.IndexOf("Identifikace", SC) > 0) { positions.AddId = new PositionIndex(line.IndexOf("Identifikace", SC), 14); } else if (line.IndexOf("Reference banky", SC) > 0) { positions.AddId = new PositionIndex(line.IndexOf("Reference banky", SC), 20); } else if (line.IndexOf("Sekv.", SC) > 0) { positions.AddId = new PositionIndex(line.IndexOf("Sekv.", SC), 5); } var protiucetPosition = line.IndexOf("Název protiúčtu", SC); positions.NazevProtiuctu = new PositionIndex(protiucetPosition, 25); var popisPosition = Math.Max(line.IndexOf("Označení platby", SC), line.IndexOf("Označení operace", SC)); positions.PopisTransakce = new PositionIndex(popisPosition, protiucetPosition - popisPosition - 1); positions.Castka = new PositionIndex(line.IndexOf("Částka", SC), 15); positions.KS = new PositionIndex(line.IndexOf("KS", SC), 4); }
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); }