Ejemplo n.º 1
0
        public static int FindMatch(AmazonTransaction transaction, List <Transaction> checkingList, int windowDays)
        {
            for (int i = 0; i < checkingList.Count; i++)
            {
                Transaction searchEntry = checkingList[i];
                double      myAmount    = transaction.Amount;
                double      theirAmount = searchEntry.CheckingAmount == 0 ? (searchEntry.SavingsAmount == 0 ? searchEntry.CreditAmount : searchEntry.SavingsAmount) : searchEntry.CheckingAmount;
                double      daysDiff    = Math.Abs((searchEntry.Date - transaction.Date).TotalDays);

                if (searchEntry.Accounts.Count == 1 &&
                    (Math.Abs(theirAmount + myAmount) < 0.01 || Math.Abs(myAmount - theirAmount) < 0.01) &&
                    daysDiff < windowDays
                    )
                {
                    //Console.WriteLine("   Combining {0}", this);
                    //Console.WriteLine("        into {0}", searchEntry);
                    return(i);
                }
            }

            Console.WriteLine("Failed to find match for entry: {0}", transaction);
            return(-1);
        }
        public static AmazonStatement ReadAmazonCSV(string filepath)
        {
            AmazonStatement ret = new AmazonStatement(filepath);

            string[] lines = File.ReadAllText(filepath).Split('\n');

            AmazonTransaction prevEntry = null;

            foreach (string line in lines)
            {
                List <string> parts = FileHelpers.ParseCSVLine(line);

                if (parts.Count < 30)
                {
                    continue;
                }

                DateTime?date = TryToScanDate(parts[0], null, null);

                if (!date.HasValue)
                {
                    continue;
                }

                string amountStr = parts[29].Substring(1);
                double.TryParse(amountStr, out double amount);

                //Build an entry for this line by itself
                AmazonTransaction subEntry = new AmazonTransaction
                {
                    Date        = date.Value,
                    Amount      = -1 * amount,
                    Description = parts[2],
                    Category    = parts[3],
                    OrderNumber = parts[1]
                };

                //Special code here so we can combine multiple transactions with the same order number into one final transaction with sub-transactions

                AmazonTransaction wrapper = null;
                if (prevEntry != null)
                {
                    //If we had an entry in progress and this entry shares the same order number, add this entry as a sub-transaction
                    if (prevEntry.Items[0].OrderNumber.Equals(subEntry.OrderNumber))
                    {
                        wrapper              = prevEntry;
                        wrapper.Amount      += subEntry.Amount;
                        wrapper.Category    += ";" + subEntry.Category;
                        wrapper.Description += ";" + subEntry.Description;
                    }
                    else
                    {
                        //Commit the previous entry, the new entry starts a new order
                        ret.Transactions.Add(prevEntry);
                        prevEntry = null;
                    }
                }

                if (prevEntry == null)
                {
                    //Create a new wrapper to hold the sub-transactions
                    wrapper = new AmazonTransaction
                    {
                        Date        = date.Value,
                        Amount      = -1 * amount,
                        Description = parts[2],
                        Category    = parts[3],
                        OrderNumber = parts[1],
                    };
                }

                wrapper.Items.Add(subEntry);
                prevEntry = wrapper;
            }

            if (prevEntry != null)
            {
                ret.Transactions.Add(prevEntry);
            }

            return(ret);
        }