/// <summary> /// Save xml string as camt052 file /// </summary> public static string Save(string Account, string UMS, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; string dir = Helper.GetProgramBaseDir(); dir = Path.Combine(dir, "camt052"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string filename = Path.Combine(dir, Helper.MakeFilenameValid(Account + "_" + DateTime.Now + "-" + Guid.NewGuid() + ".camt052")); // camt052 if (!File.Exists(filename)) { using (File.Create(filename)) { }; } File.AppendAllText(filename, UMS, encoding); return(filename); }
/// <summary> /// Trace /// </summary> /// <param name="Message"></param> public static void Write(string Message) { if (Enabled) { // Directory var dir = Helper.GetProgramBaseDir(); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // Tracefile dir = Path.Combine(dir, "TRACE"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string file = Path.Combine(dir, "Trace.txt"); if (!File.Exists(file)) { using (File.Create(Path.Combine(dir, "Trace.txt"))) { }; } if (MaxFileSize > 0) { var sizeMB = (double)new FileInfo(file).Length / (double)1000000; if (sizeMB > MaxFileSize) { File.WriteAllText(file, string.Empty); } } if (Formatted) { var formatted = string.Empty; var matches = Regex.Matches(Message, "[A-Z]{5}[^']*'+"); foreach (Match match in matches) { formatted += match.Value + Environment.NewLine; } File.AppendAllText(file, "[" + DateTime.Now + "]" + Environment.NewLine + formatted + Environment.NewLine); } else { File.AppendAllText(file, "[" + DateTime.Now + "]" + " " + Message + Environment.NewLine); } } }
/// <summary> /// Log /// </summary> /// <param name="Message"></param> public static void Write(string Message) { if (Enabled) { // Directory var dir = Helper.GetProgramBaseDir(); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // Logfile dir = Path.Combine(dir, "LOG"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string file = Path.Combine(dir, "Log.txt"); if (!File.Exists(file)) { using (File.Create(file)) { }; } if (MaxFileSize > 0) { var sizeMB = (double)new FileInfo(file).Length / (double)1000000; if (sizeMB > MaxFileSize) { File.WriteAllText(file, string.Empty); } } File.AppendAllText(file, "[" + DateTime.Now + "]" + " " + Message + Environment.NewLine); } }
public static List <SWIFTStatement> Serialize(string STA, string Account, bool writeToFile = false) { int LineCounter = 0; string swiftTag = ""; string swiftData = ""; SWIFTStatements = new List <SWIFTStatement>(); SWIFTStatement = null; if (STA == null || STA.Length == 0) { return(SWIFTStatements); } string dir = null; if (writeToFile) { dir = Helper.GetProgramBaseDir(); dir = Path.Combine(dir, "STA"); string filename = Path.Combine(dir, Helper.MakeFilenameValid(Account + "_" + DateTime.Now + ".STA")); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // STA if (!File.Exists(filename)) { using (File.Create(filename)) { }; File.AppendAllText(filename, STA); } else { File.AppendAllText(filename, STA); } } while (STA.Length > 0) { string line = Read(ref STA); LineCounter++; if (line.StartsWith("-")) // end of block { // Process previously read swift chunk if (swiftTag.Length > 0) { Data(swiftTag, swiftData); } swiftTag = string.Empty; swiftData = string.Empty; continue; } if (line.Length > 0) { // A swift chunk starts with a swiftTag, which is between colons if (Regex.IsMatch(line, @"^:[\w]+:")) { // Process previously read swift chunk if (swiftTag.Length > 0) { Data(swiftTag, swiftData); } int posColon = line.IndexOf(":", 2); swiftTag = line.Substring(1, posColon - 1); swiftData = line.Substring(posColon + 1); } else { // The swift chunk is spread over several lines swiftData = swiftData + line; } } } if (swiftTag.Length > 0) { Data(swiftTag, swiftData); } // If there are remaining unprocessed statements - add them if (SWIFTStatement != null) { SWIFTStatements.Add(SWIFTStatement); // Process missing input dates foreach (var tx in SWIFTStatement.SWIFTTransactions) { if (tx.inputDate == default) { tx.inputDate = SWIFTStatement.date; } } SWIFTStatement = null; } // Parse SEPA purposes foreach (var stmt in SWIFTStatements) { foreach (var tx in stmt.SWIFTTransactions) { if (string.IsNullOrWhiteSpace(tx.description)) { continue; } // Collect all occuring SEPA purposes ordered by their position List <Tuple <int, SWIFTTransaction.SEPAPurpose> > indices = new List <Tuple <int, SWIFTTransaction.SEPAPurpose> >(); foreach (SWIFTTransaction.SEPAPurpose sepaPurpose in Enum.GetValues(typeof(SWIFTTransaction.SEPAPurpose))) { string prefix = $"{sepaPurpose}+"; var idx = tx.description.IndexOf(prefix); if (idx >= 0) { indices.Add(Tuple.Create(idx, sepaPurpose)); } } indices = indices.OrderBy(v => v.Item1).ToList(); // Then get the values for (int i = 0; i < indices.Count; i++) { var beginIdx = indices[i].Item1 + $"{indices[i].Item2}+".Length; var endIdx = i < indices.Count - 1 ? indices[i + 1].Item1 : tx.description.Length; var value = tx.description.Substring(beginIdx, endIdx - beginIdx); tx.SEPAPurposes[indices[i].Item2] = value; } } } if (Trace.Enabled) { foreach (SWIFTStatement statement in SWIFTStatements) { var ID = statement.id; var Date = statement.date.ToShortDateString(); var AccountCode = statement.accountCode; var BanksortCode = statement.bankCode; var Currency = statement.currency; var StartBalance = statement.startBalance.ToString(); var EndBalance = statement.endBalance.ToString(); foreach (SWIFTTransaction transaction in statement.SWIFTTransactions) { var PartnerName = transaction.partnerName; var AccountCode_ = transaction.accountCode; var BankCode = transaction.bankCode; var Description = transaction.description; var Text = transaction.text; var TypeCode = transaction.typecode; var Amount = transaction.amount.ToString(); var UMS = "++STARTUMS++" + "ID: " + ID + " ' " + "Date: " + Date + " ' " + "AccountCode: " + AccountCode + " ' " + "BanksortCode: " + BanksortCode + " ' " + "Currency: " + Currency + " ' " + "StartBalance: " + StartBalance + " ' " + "EndBalance: " + EndBalance + " ' " + "PartnerName: " + PartnerName + " ' " + "BankCode: " + BankCode + " ' " + "Description: " + Description + " ' " + "Text: " + Text + " ' " + "TypeCode: " + TypeCode + " ' " + "Amount: " + Amount + " ' " + "++ENDUMS++"; dir = Helper.GetProgramBaseDir(); dir = Path.Combine(dir, "MT940"); string filename_ = Path.Combine(dir, Helper.MakeFilenameValid(Account + "_" + DateTime.Now + ".MT940")); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // MT940 if (!File.Exists(filename_)) { using (File.Create(filename_)) { }; File.AppendAllText(filename_, UMS); } else { File.AppendAllText(filename_, UMS); } } } } return(SWIFTStatements); }