private static void GenerateMergedStatement(List<KeyValuePair<string, FineAntsCore.Statement>> statements, out FineAntsCore.Statement merged, out List<string> warnings) { statements.Sort(delegate(KeyValuePair<string, FineAntsCore.Statement> a, KeyValuePair<string, FineAntsCore.Statement> b) { return a.Value.StartDate.CompareTo(b.Value.StartDate); }); // create merged statement object with properties of first file in files merged = new FineAntsCore.Statement(); merged.StartDate = statements[0].Value.StartDate; merged.EndDate = statements[statements.Count - 1].Value.EndDate; merged.ClosingBalance = statements[statements.Count - 1].Value.ClosingBalance; // add all the transactions from the first document to the merged document foreach (var transaction in statements[0].Value.Transactions) { // add to transactions of merged statement merged.Transactions.Add(transaction); } warnings = new List<string>(); for (int index = 1; index < statements.Count; ++index) { int dateComparison = DateTime.Compare(statements[index].Value.StartDate, statements[index - 1].Value.EndDate.AddDays(1)); if (dateComparison < 0) { // todo: address duplicate transactions warnings.Add("overlapping date range between documents " + (statements[index - 1].Key) + " and " + statements[index].Key); } else if (dateComparison > 0) { warnings.Add("gap in date range between documents " + (statements[index - 1].Key) + " and " + statements[index].Key); } // Detect balance inaccuracies. int laterClosingBalance = statements[index].Value.ClosingBalance; int earlierClosingBalance = statements[index - 1].Value.ClosingBalance; int transactionsInBetween = sumOfTransactions(statements[index].Value); if (earlierClosingBalance + transactionsInBetween != laterClosingBalance) { warnings.Add("Document " + statements[index].Key + " has a closing balance of " + earlierClosingBalance + " which is inconsistent with its transactions (totalling " + transactionsInBetween + ") and the closing balance of " + laterClosingBalance + " in " + (statements[index - 1].Key) ); } // add all the transactions to the merged document foreach (var transaction in statements[index].Value.Transactions) { // add to transactions of merged statement merged.Transactions.Add(transaction); } } merged.Transactions.Sort(delegate(FineAntsCore.Transaction a, FineAntsCore.Transaction b) { return a.Date.CompareTo(b.Date); }); }
private static int sumOfTransactions(FineAntsCore.Statement statement) { int total = 0; foreach (var transaction in statement.Transactions) { total += transaction.Amount; } return total; }