public void Create() { string path = Path.Combine(Path.GetTempPath(), "SampleData.xml"); ProcessHelper.ExtractEmbeddedResourceAsFile("Walkabout.Database.SampleData.xml", path); SampleDatabaseOptions options = new SampleDatabaseOptions(); options.Owner = Application.Current.MainWindow; options.SampleData = path; if (options.ShowDialog() == false) { return; } path = options.SampleData; double inflation = options.Inflation; SampleData data = null; XmlSerializer s = new XmlSerializer(typeof(SampleData)); using (XmlReader reader = XmlReader.Create(path)) { data = (SampleData)s.Deserialize(reader); } int totalFrequency = data.GetTotalFrequency(); List <SampleTransaction> list = new List <SampleTransaction>(); foreach (SampleAccount sa in data.Accounts) { // Create all the accounts. Accounts accounts = money.Accounts; Account a = accounts.FindAccount(sa.Name); if (a == null) { a = accounts.AddAccount(sa.Name); } a.Type = sa.Type; a.IsBudgeted = true; if (a.Type == AccountType.Checking) { this.checking = a; } // Create this many transactions int count = sa.Frequency; // by scaling the payee frequencies to match the above desired count. double ratio = (double)count / (double)totalFrequency; if (a.Type == AccountType.Brokerage || a.Type == AccountType.Retirement) { CreateInvestmentSamples(data, a); } else { // create flat list of all payees to choose from so it fits the histogram List <SamplePayee> payees = new List <SamplePayee>(); foreach (SamplePayee payee in data.Payees) { foreach (SampleCategory sc in payee.Categories) { int newFrequency = (int)(sc.Frequency * ratio); for (int i = 0; i < newFrequency; i++) { list.Add(new SampleTransaction() { Account = sa, Payee = payee, Category = sc }); } } } } } CreateRandomTransactions(list, inflation); AddPaychecks(options.Employer, options.PayCheck, inflation); }
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.Brokerage || 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); } }
private void CreateInvestmentSamples(SampleData data, Account a) { money.BeginUpdate(this); Transactions transactions = money.Transactions; for (int year = DateTime.Now.Year - 10; year <= DateTime.Now.Year; year++) { foreach (SampleSecurity ss in data.Securities) { // Get or Create the security Security stock = money.Securities.FindSecurity(ss.Name, true); stock.SecurityType = ss.SecurityType; stock.Symbol = ss.Symbol; // keep track of how much we own so we never go negative. decimal owned = 0; IList <int> selectedDays = GetRandomDaysInTheYearForTransactions(); foreach (var day in selectedDays) { // Create a new Transaction Transaction t = money.Transactions.NewTransaction(a); // the date the purchase or sale was done t.Date = new DateTime(year, 1, 1).AddDays(day); //----------------------------------------------------- // Create the matching Investment transaction Investment i = t.GetOrCreateInvestment(); i.Transaction = t; i.Security = stock; // Is this a BUY or SELL i.Type = InvestmentType.Buy; // How many unit bought or sold i.Units = rand.Next(1000); if (owned > 0 && rand.Next(2) == 1) { i.Type = InvestmentType.Sell; i.Units = rand.Next((int)owned); // don't sell more than we currently own. owned -= i.Units; } else { owned += i.Units; } // What price i.UnitPrice = Convert.ToDecimal(rand.Next(ss.PriceRangeLow, ss.PriceRangeHight)); // add some pennies (decimal value) decimal penies = rand.Next(99); penies /= 100; i.UnitPrice = i.UnitPrice + penies; // Calculate the Payment or Deposit amount t.Amount = i.Units * i.UnitPrice * (i.Type == InvestmentType.Buy ? -1 : 1); t.Payee = money.Payees.FindPayee(ss.Name, true); t.Category = money.Categories.InvestmentStocks; if (i.Type == InvestmentType.Sell) { t.Amount = t.Amount * 1.10M; // Improve the outcome of profit, sell at the 10% higher random } //----------------------------------------------------- // Finally add the new transaction money.Transactions.AddTransaction(t); } } } money.EndUpdate(); }