private static void ConvertOfxFileToFineAntsStatementFile(FileInfo ofxFile, FileInfo statementFile) { Ofx.Document file = new Ofx.Document(ofxFile.FullName, "../../../external/SgmlReader/TestSuite/ofx160.dtd"); FineAntsCore.Statement statement = file.ConvertToFineAntsStatement(); FineAntsCore.Statement.SerialiseStatement(statement, statementFile.FullName); }
private static void ConvertOfxFileToFineAntsStatementFile(FileInfo ofxFile, FileInfo statementFile) { Ofx.Document file = new Ofx.Document(ofxFile.FullName, "ofx160.dtd"); FineAntsCore.Statement statement = file.ConvertToFineAntsStatement(); FineAntsCore.Statement.SerialiseStatementJSON(statement, statementFile.FullName); }
static FineAntsCore.Statement statementFromFileName(string fileName) { FileInfo info = new FileInfo(fileName); if (String.Compare(info.Extension, ".ofx", true) == 0) { Ofx.Document file = new Ofx.Document(fileName, "ofx160.dtd"); return file.ConvertToFineAntsStatement(); } else if (String.Compare(info.Extension, ".statement", true) == 0) { return FineAntsCore.Statement.DeserialiseStatement(fileName); } else if (String.Compare(info.Extension, ".statementjson", true) == 0) { return FineAntsCore.Statement.DeserialiseStatementJSON(fileName); } throw new Exception("Not a statement file."); }
private static void CleanAmexOFX(string path) { // Create a reader to read the broken file System.IO.TextReader reader = new System.IO.StreamReader(path); // Read the whole file into a string string content = reader.ReadToEnd(); // Close the reader stream reader.Close(); // Remove the non-standard bits content = System.Text.RegularExpressions.Regex.Replace(content, "<AMEX.UNIVID>[^<]*<", "<"); content = System.Text.RegularExpressions.Regex.Replace(content, "<ORIGIN.ID>[^<]*<", "<"); content = System.Text.RegularExpressions.Regex.Replace(content, "<START.TIME>[^<]*<", "<"); content = System.Text.RegularExpressions.Regex.Replace(content, "<CYCLECUT.INDICATOR>[^<]*<", "<"); content = System.Text.RegularExpressions.Regex.Replace(content, "<PURGE.INDICATOR>[^<]*<", "<"); content = System.Text.RegularExpressions.Regex.Replace(content, "<INTL.INDICATOR>[^<]*<", "<"); // Append '.broken' to the path of the file, which we will rename the old file to string newPath = path + ".broken"; // Rename the old, broken file System.IO.File.Move(path, newPath); // Create a writer to write to the fixed file System.IO.TextWriter writer = new System.IO.StreamWriter(path); // Write the fixed content to the new stream writer.Write(content); // Close the write stream writer.Close(); // Open up the fixed file again since there are some more things to do to it Ofx.Document document = new Ofx.Document(path, "ofx160.dtd"); bool containsPayment = false; int previousPayment = 0; // Loop through all the transactions foreach (SimpleOfx.BankTranListTypeSTMTTRN transaction in document.transactions) { // Add decimal places to the amount field // 5.4 => 5.40, 20 => 20.00 double amount = double.Parse(transaction.TRNAMT); transaction.TRNAMT = amount.ToString("F", System.Globalization.CultureInfo.InvariantCulture); // Remember if it was a payment of the previous statement - we'll use this in a minute to fix the closing balance if (transaction.NAME == "PAYMENT RECEIVED - THANK YOU") { containsPayment = true; previousPayment = Ofx.Document.moneyInPenceFromString(transaction.TRNAMT); } } // Calculate the closing balance date document.calculateClosingBalanceDetails(); // Recalculate the closing balance if(containsPayment) { // Work out the actual closing balance - will be the sum of transactions other than the first payment, since this is equal to the opening balance int statementTotal = document.sumOfTransactions(); int closingBalance = statementTotal - previousPayment; document.closingBalance = closingBalance; } document.Save(); }
static void Main(string[] args) { System.Collections.Generic.List<Ofx.Document> documents = new System.Collections.Generic.List<Ofx.Document>(); string outputDirectory; // check if a directory has been supplied if ((System.IO.File.GetAttributes(args[0]) & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) { outputDirectory = args[0]; // load each file in the directory if it is an ofx file foreach (string filename in System.IO.Directory.GetFiles(args[0])) { System.IO.FileInfo info = new System.IO.FileInfo(filename); if (System.String.Compare(info.Extension, ".ofx", true) == 0) { Ofx.Document file = new Ofx.Document(filename, "../../../external/SgmlReader/TestSuite/ofx160.dtd"); documents.Add(file); } } } else { System.IO.FileInfo inputFileInfo = new System.IO.FileInfo(args[0]); outputDirectory = inputFileInfo.DirectoryName; // load each argument as an ofx file foreach (string filename in args) { Ofx.Document file = new Ofx.Document(filename, "../../../external/SgmlReader/TestSuite/ofx160.dtd"); documents.Add(file); } } documents.Sort(delegate(Ofx.Document a, Ofx.Document b) { return a.startDate.CompareTo(b.startDate); }); // create merged statement object with properties of first file in files Ofx.Document merged = new Ofx.Document(); merged.usePropertiesFrom(documents[0]); merged.startDate = documents[0].startDate; merged.endDate = documents[documents.Count - 1].endDate; merged.closingBalanceDate = documents[documents.Count - 1].closingBalanceDate; merged.closingBalance = documents[documents.Count - 1].closingBalance; // add all the transactions from the first document to the merged document foreach (SimpleOfx.BankTranListTypeSTMTTRN transaction in documents[0].transactions) { // add to transactions of merged statement merged.transactions.Add(transaction); } System.Collections.Generic.List<string> warnings = new System.Collections.Generic.List<string>(); for (int index = 1; index < documents.Count; ++index) { int value = System.DateTime.Compare(documents[index].startDate, documents[index-1].endDate.AddDays(1)); if (value < 0) { // todo: address duplicate transactions warnings.Add("overlapping date range between documents " + (documents[index - 1].m_fileName) + " and " + documents[index].m_fileName); } else if(value > 0) { warnings.Add("gap in date range between documents " + (documents[index - 1].m_fileName) + " and " + documents[index].m_fileName); } int laterClosingBalance = documents[index].closingBalance; int earlierClosingBalance = documents[index - 1].closingBalance; int transactionsInBetween = documents[index].sumOfTransactions(); if (earlierClosingBalance + transactionsInBetween != laterClosingBalance) { warnings.Add("Document " + documents[index].m_fileName + " has a closing balance of " + earlierClosingBalance + " which is inconsistent with its transactions (totalling " + transactionsInBetween + ") and the closing balance of " + laterClosingBalance + " in " + (documents[index - 1].m_fileName)); } // add all the transactions to the merged document foreach (SimpleOfx.BankTranListTypeSTMTTRN transaction in documents[index].transactions) { // add to transactions of merged statement merged.transactions.Add(transaction); } } if (warnings.Count > 0) { foreach (string warning in warnings) { System.Console.WriteLine(warning); } System.Console.ReadKey(); } // write merged file string outputFileName = string.Format("{0} - {1}.ofx", merged.startDate.ToString("yyyy-MM-dd"), merged.endDate.ToString("yyyy-MM-dd")); merged.Save(outputDirectory + "/" + outputFileName); }
private void loadFile() { System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName); if (fileInfo.Extension == ".ofx") { Ofx.Document document = new Ofx.Document(fileInfo.FullName, "ofx160.dtd"); statement = document.ConvertToFineAntsStatement(); } else if (fileInfo.Extension == ".statement") { statement = FineAntsCore.Statement.DeserialiseStatement(fileInfo.FullName); } else if (fileInfo.Extension == ".statementjson") { statement = FineAntsCore.Statement.DeserialiseStatementJSON(fileInfo.FullName); } else { throw new Exception("Unsupported file type"); } bindControlsToDocument(); }
private void loadFile() { System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName); if (fileInfo.Extension == ".ofx") { Ofx.Document document = new Ofx.Document(fileInfo.FullName, "../../../external/SgmlReader/TestSuite/ofx160.dtd"); statement = document.ConvertToFineAntsStatement(); } else if (fileInfo.Extension == ".statement") { statement = FineAntsCore.Statement.DeserialiseStatement(fileInfo.FullName); } bindControlsToDocument(); }