static TxCoreV2 doTxnBlockQif(string block, string file, TxMoneySrc txMoneySrc) { try { var ls = block.Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); var rv = new TxCoreV2 { Id = --_cntr, CreatedAt = /**/ _batchTimeNow, FitId = /**/ ls.FirstOrDefault(r => r[0] == 'N').Substring(1), TxDate = /**/ parseTxnDate(ls.FirstOrDefault(r => r[0] == 'D').Substring(1)), TxAmount = /**/ -parseTA(ls.FirstOrDefault(r => r[0] == 'T').Substring(1)), TxDetail = /**/ ls.FirstOrDefault(r => r[0] == 'P').Substring(1), MemoPP = /**/ string.Join(", ", ls.Where(r => r[0] == 'A' && r.Length > 1).Select(r => r.Substring(1))) + ". ", TxCategoryIdTxt = /**/ PreSet.__UnKn, TxMoneySrc = /**/ txMoneySrc, TxMoneySrcId = /**/ txMoneySrc.Id, // inferTxMoneySrcId(file), SrcFile = /**/ Path.GetFileNameWithoutExtension(file) }; Debug.WriteLine(rv); return(rv); } catch (Exception ex) { ex.Log(); } return(null); }
static BalAmtHist doBalAmtBlock(string block, TxMoneySrc txMoneySrc, string tagBgn, string balTpe) { var start = block.IndexOf(tagBgn); if (start < 0) { return(null); } var dp = getElCont(block, "<DTASOF>"); // as of date var ta = getElCont(block, "<BALAMT>"); var rv = new BalAmtHist { Id = --_cntr, CreatedAt = _batchTimeNow, AsOfDate = parseTxnDate(dp), BalAmt = -parseTA(ta), BalTpe = balTpe, TxMoneySrc = txMoneySrc, TxMoneySrcId = txMoneySrc.Id // inferTxMoneySrcId(file), // fix this Jan 30 2019 }; return(rv); }
static TxCoreV2 doTxnBlock(string block, string file, TxMoneySrc txMoneySrc) { var start = block.IndexOf("<STMTTRN>"); if (start < 0) { return(null); } //Debug.WriteLine(block.Substring(33).Replace("<TRNAMT>", "\t").Replace("<FITID>", "\t").Replace("<NAME>", "\t\t").Replace("<MEMO>", "\t\t").Replace("<CHECKNUM>", "\t\t").Replace("<DTUSER>", "\t\t").Replace("<SIC>", "\t\t")); //var tt = getElCont(block, "<TRNTYPE>"); var dp = getElCont(block, "<DTPOSTED>"); // posted date var ta = getElCont(block, "<TRNAMT>"); var fi = getElCont(block, "<FITID>"); var dt = getElCont(block, "<NAME>") ?? "°"; // required db field var mm = getElCont(block, "<MEMO>"); // CV only: details of $US txn. var cs = getElCont(block, "<CHECKNUM>"); // TD only: cheque number var td = getElCont(block, "<DTUSER>"); // MC only: tx date var si = getElCont(block, "<SIC>"); // MC only: 4-digits like 0000, 4816, ... if (td != null) { td = string.Format("txDate:{0}, ", parseTxnDate(td)); } if (si != null) { si = string.Format("sic:{0}", si); } var rv = new TxCoreV2 { Id = --_cntr, CreatedAt = _batchTimeNow, FitId = fi, TxDate = parseTxnDate(dp), TxAmount = -parseTA(ta), TxDetail = dt, MemoPP = mm + cs + td + si, TxCategoryIdTxt = PreSet.__UnKn, TxMoneySrc = txMoneySrc, TxMoneySrcId = txMoneySrc.Id, // inferTxMoneySrcId(file), SrcFile = Path.GetFileNameWithoutExtension(file) }; //Debug.WriteLine(rv); return(rv); }
public static List <BalAmtHist> ReadBAH(string file, TxMoneySrc txMoneySrc, string balTpe = LedgerBal) // for all files, or AVAILBAL for VIsa, TD only. { var rv = new List <BalAmtHist>(); var tagBgn = string.Format("<{0}>", balTpe); var tagEnd = string.Format("</{0}>", balTpe); var blocks = File.ReadAllText(file).Split(new string[] { tagEnd }, StringSplitOptions.RemoveEmptyEntries); foreach (var block in blocks) { if (block.Contains(tagBgn)) { rv.Add(doBalAmtBlock(block.Replace("\n", "").Replace("\r", ""), txMoneySrc, tagBgn, balTpe)); } } return(rv); }
public static List <TxCoreV2> ReadTxs(A0DbContext db, string file, out string fla_acntDir, out TxMoneySrc txMoneySrc) { Debug.WriteLine(Path.GetFileName(file), "\n\n"); var txns = new List <TxCoreV2>(); txMoneySrc = null; try { if (string.Equals(Path.GetExtension(file), ".csv", StringComparison.OrdinalIgnoreCase)) // PC since 2019 { var lines = File.ReadAllLines(file); if (lines.Length < 2) { fla_acntDir = $"Is empty file: {file}"; return(txns); } if (!lines[0].Equals(_header)) { fla_acntDir = $"Is not a known fin file: {file}"; return(txns); } var acntId = lines[1].Split(new[] { _separtor }, StringSplitOptions.RemoveEmptyEntries)[1].Replace("*", ""); txMoneySrc = getCreateTxMoneySrc(db, out fla_acntDir, acntId, Path.GetFileName(file)); foreach (var line in lines) { if (line.Equals(_header)) { continue; } if (string.IsNullOrEmpty(line)) { continue; } var cells = line.Split(new[] { _separtor }, StringSplitOptions.RemoveEmptyEntries); if (cells.Length < 3) { continue; } txns.Add(new TxCoreV2 { Id = --_cntr, CreatedAt = _batchTimeNow, FitId = line, TxDate = parseTxnDate(cells[2], cells[3]), TxAmount = parseTA(cells[4].Replace("\"", "")), TxDetail = cells[0].Substring(1), //MemoPP = mm + cs + td + si, TxCategoryIdTxt = PreSet.__UnKn, TxMoneySrc = txMoneySrc, TxMoneySrcId = txMoneySrc.Id, SrcFile = Path.GetFileNameWithoutExtension(file), Notes = $"{file}" // 2021-01 }); } } else if (string.Equals(Path.GetExtension(file), ".qif", StringComparison.OrdinalIgnoreCase)) // amazon / chase qif file format { var blocks = File.ReadAllText(file).Split(new string[] { "^" }, StringSplitOptions.RemoveEmptyEntries); var acntId = "amzn"; //only for amazon since no other formats available; note: no CC # in qif ==> use for amazon only. txMoneySrc = getCreateTxMoneySrc(db, out fla_acntDir, acntId, Path.GetFileName(file)); foreach (var block in blocks) { if (block.Contains("\nD")) { txns.Add(doTxnBlockQif(block, file, txMoneySrc)); } } } else // .ofx .qfx .aso { var blocks = File.ReadAllText(file).Split(new string[] { "</STMTTRN>" }, StringSplitOptions.RemoveEmptyEntries); var acntId = blocks[0].Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(r => r.Contains(PreSet.AcntIdTAg)); if (acntId?.Length > 20) // Jun2017: elements not always are one per line. { acntId = getElCont(acntId, PreSet.AcntIdTAg); } txMoneySrc = getCreateTxMoneySrc(db, out fla_acntDir, acntId, Path.GetFileName(file)); foreach (var block in blocks) { if (block.Contains("<STMTTRN>")) { txns.Add(doTxnBlock(block.Replace("\n", "").Replace("\r", ""), file, txMoneySrc)); } } } } catch (Exception ex) { ex.Log(); fla_acntDir = "Exceptioned"; } return(txns); }