Exemplo n.º 1
0
        private void CreateRandomTransactions(List <SampleTransaction> list, double inflation)
        {
            // Now pick randomly from the list to mix things up nicely and spread across 10 year range.
            Random   rand             = new Random();
            DateTime today            = DateTime.Today;
            DateTime first            = today.AddYears(-Years);
            DateTime start            = new DateTime(first.Year, 1, 1); // start in January
            TimeSpan span             = today - first;
            int      totalDays        = (int)span.TotalDays;
            double   monthlyInflation = (inflation / 12);

            money.BeginUpdate();
            Transactions transactions = money.Transactions;
            Accounts     accounts     = money.Accounts;
            Payees       payees       = money.Payees;
            Categories   categories   = money.Categories;

            while (list.Count > 0)
            {
                int i = rand.Next(0, list.Count);
                SampleTransaction st = list[i];
                list.RemoveAt(i);

                SampleAccount  sa = st.Account;
                Account        a  = accounts.FindAccount(sa.Name);
                Payee          p  = payees.FindPayee(st.Payee.Name, true);
                SampleCategory sc = st.Category;
                Category       c  = money.Categories.GetOrCreateCategory(sc.Name, sc.Type);
                if (c.Type == CategoryType.None)
                {
                    c.Type = sc.Type;
                }
                if (c.Root.Type == CategoryType.None)
                {
                    c.Root.Type = sc.Type;
                }

                int      daysFromStart = rand.Next(0, totalDays);
                DateTime date          = start + TimeSpan.FromDays(daysFromStart);

                // spread evenly around the average
                decimal amount = 0;
                if (rand.Next(2) == 1)
                {
                    // above average
                    amount = (decimal)rand.Next(sc.Average * 100, sc.Max * 100) / 100;
                }
                else
                {
                    // below average
                    amount = (decimal)rand.Next(sc.Min * 100, sc.Average * 100) / 100;
                }

                // add inflation
                amount = Inflate(amount, (int)(daysFromStart / 30), (decimal)monthlyInflation);

                Transaction t = transactions.NewTransaction(a);
                t.Payee    = p;
                t.Category = c;
                t.Date     = date;
                t.Amount   = amount;
                transactions.AddTransaction(t);
            }
            money.EndUpdate();

            // now trigger UI update
            foreach (Account a in money.Accounts.GetAccounts())
            {
                money.Rebalance(a);
            }

            // only have to do this because we hid all update events earlier by doing BeginUpdate/EndUpdate on money object.
            // trigger payee update
            money.Payees.BeginUpdate(false);
            money.Payees.EndUpdate();

            // trigger category update
            money.Categories.BeginUpdate(false);
            money.Categories.EndUpdate();
        }
Exemplo n.º 2
0
        public void Export(string path)
        {
            SampleData           data     = new SampleData();
            List <SampleAccount> accounts = data.Accounts = new List <SampleAccount>();
            Dictionary <Account, SampleAccount> accountMap = new Dictionary <Account, SampleAccount>();

            foreach (Account a in money.Accounts.GetAccounts())
            {
                if (!a.IsClosed)
                {
                    var sa = new SampleAccount()
                    {
                        Name = a.Name, Type = a.Type
                    };
                    accounts.Add(sa);
                    accountMap[a] = sa;
                }
            }

            List <SamplePayee> payees = data.Payees = new List <SamplePayee>();
            Dictionary <Payee, SamplePayee> payeeMap = new Dictionary <Payee, SamplePayee>();

            foreach (Transaction t in money.Transactions.GetAllTransactions())
            {
                SampleAccount sa;
                if (!accountMap.TryGetValue(t.Account, out sa) || t.Account.Type == AccountType.Investment || t.Payee == null || t.IsSplit || t.Category == null || t.Transfer != null)
                {
                    continue;
                }

                Category c       = t.Category;
                string   catName = c.Name;
                if (catName.Contains("Microsoft") || catName.Contains("Mitzvah") || catName.Contains("Love") || catName.Contains("Loans") || catName.Contains("Chanukah") || catName.Contains("Unknown"))
                {
                    continue;
                }
                catName = catName.Replace("Woodinville", string.Empty).Replace("Redmond", string.Empty).Trim();

                sa.Frequency++;
                Payee  p         = t.Payee;
                string payeeName = p.Name.Replace("Woodinville", string.Empty).Replace("Redmond", string.Empty).Trim();
                if (payeeName.Contains("Microsoft") || payeeName.Contains("ATM Withdrawal"))
                {
                    continue;
                }
                SamplePayee sp = null;
                if (!payeeMap.TryGetValue(p, out sp))
                {
                    sp = new SamplePayee()
                    {
                        Name        = payeeName,
                        Categories  = new List <SampleCategory>(),
                        CategoryMap = new Dictionary <Category, SampleCategory>()
                    };
                    payees.Add(sp);
                    payeeMap[p] = sp;
                }
                sp.Frequency++;

                SampleCategory sc;
                if (!sp.CategoryMap.TryGetValue(c, out sc))
                {
                    sc = new SampleCategory()
                    {
                        Name = catName, Type = c.Root.Type
                    };
                    sp.CategoryMap[c] = sc;
                    sp.Categories.Add(sc);
                }
                decimal amount = t.Amount;
                sc.TotalAmount += amount;
                if (sc.Frequency == 0)
                {
                    sc.Min = sc.Max = (int)amount;
                }
                else
                {
                    if (sc.Min > amount)
                    {
                        sc.Min = (int)amount;
                    }
                    if (sc.Max < amount)
                    {
                        sc.Max = (int)amount;
                    }
                }
                sc.Frequency++;
            }

            // remove low frequency stuff
            foreach (SampleAccount sa in new List <SampleAccount>(accounts))
            {
                if (sa.Frequency < 10)
                {
                    accounts.Remove(sa);
                }
            }

            foreach (SamplePayee sp in new List <SamplePayee>(payees))
            {
                if (sp.Frequency < 10)
                {
                    payees.Remove(sp);
                }
                else
                {
                    foreach (SampleCategory sc in sp.Categories)
                    {
                        sc.Average = (int)(sc.TotalAmount / sc.Frequency);
                    }
                }
            }

            XmlSerializer     s        = new XmlSerializer(typeof(SampleData));
            XmlWriterSettings settings = new XmlWriterSettings()
            {
                Indent = true
            };

            using (XmlWriter writer = XmlWriter.Create(path, settings))
            {
                s.Serialize(writer, data);
            }
        }
Exemplo n.º 3
0
        private void CreateRandomTransactions(List <SampleTransaction> list, double inflation)
        {
            // Now pick randomly from the list to mix things up nicely and spread across 10 year range.
            Random   rand             = new Random();
            DateTime today            = DateTime.Today;
            DateTime first            = today.AddYears(-Years);
            DateTime start            = new DateTime(first.Year, 1, 1); // start in January
            TimeSpan span             = today - first;
            int      totalDays        = (int)span.TotalDays;
            double   monthlyInflation = (inflation / 12);

            money.BeginUpdate(this);
            Transactions transactions = money.Transactions;
            Accounts     accounts     = money.Accounts;
            Payees       payees       = money.Payees;
            Categories   categories   = money.Categories;
            int          nextCheck    = 2800;

            while (list.Count > 0)
            {
                int i = rand.Next(0, list.Count);
                SampleTransaction st = list[i];
                list.RemoveAt(i);

                SampleAccount  sa = st.Account;
                Account        a  = accounts.FindAccount(sa.Name);
                Payee          p  = payees.FindPayee(st.Payee.Name, true);
                SampleCategory sc = st.Category;
                Category       c  = money.Categories.GetOrCreateCategory(sc.Name, sc.Type);
                if (c.Type == CategoryType.None)
                {
                    c.Type = sc.Type;
                }
                if (c.Root.Type == CategoryType.None)
                {
                    c.Root.Type = sc.Type;
                }

                int      daysFromStart = rand.Next(0, totalDays);
                DateTime date          = start + TimeSpan.FromDays(daysFromStart);

                // spread evenly around the average
                decimal amount = 0;
                if (rand.Next(2) == 1)
                {
                    // above average
                    amount = (decimal)rand.Next(sc.Average * 100, sc.Max * 100) / 100;
                }
                else
                {
                    // below average
                    amount = (decimal)rand.Next(sc.Min * 100, sc.Average * 100) / 100;
                }

                // add inflation
                amount = Inflate(amount, (int)(daysFromStart / 30), (decimal)monthlyInflation);

                Transaction t = transactions.NewTransaction(a);
                t.Payee    = p;
                t.Category = c;
                t.Date     = date;
                t.Amount   = RoundCents(amount);
                if (st.Payee.Type == PaymentType.Check)
                {
                    t.Number = nextCheck.ToString();
                    nextCheck++;
                }
                transactions.AddTransaction(t);
            }

            // now pay the credit cards once a month from a checking account.
            Account checking = this.checking;

            if (checking != null)
            {
                foreach (var acct in accounts.GetAccounts())
                {
                    if (acct.Type == AccountType.Credit)
                    {
                        // here we know transactions are sorted by date.
                        DateTime endOfMonth = start.AddMonths(1);
                        decimal  balance    = 0;
                        foreach (var t in money.Transactions.GetTransactionsFrom(acct))
                        {
                            balance += t.Amount;
                            if (t.Date >= endOfMonth)
                            {
                                if (balance != 0)
                                {
                                    Transaction payment = transactions.NewTransaction(checking);
                                    payment.Date   = endOfMonth;
                                    payment.Amount = RoundCents(balance);
                                    transactions.AddTransaction(payment);
                                    money.Transfer(payment, acct);
                                    balance    = 0;
                                    endOfMonth = endOfMonth.AddMonths(1);
                                }
                            }
                        }
                    }
                }
            }

            money.EndUpdate();
        }