-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paycheck.xaml.cs
146 lines (138 loc) · 5.11 KB
/
Paycheck.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Goalie.Lib;
using Goalie.Lib.Data;
using Goalie.Lib.Models;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace Goalie
{
/// <summary>
/// Interaction logic for Paycheck.xaml
/// </summary>
public partial class Paycheck : Window, IShouldSave
{
public Profile Profile { get; set; }
public List<Account> Accounts { get; set; }
public bool ShouldSave { get; set; }
private List<AccountListView> AccountList { get; set; }
public Paycheck(Profile profile)
{
InitializeComponent();
ShouldSave = false;
Profile = profile;
List<Account> accounts = profile.Accounts
.Where(account => account.Type == AccountType.Goal &&
account.SavingsType != GoalSavingsType.Manual).ToList();
AccountList = new List<AccountListView>();
foreach(Account account in accounts)
{
AccountList.Add(new AccountListView(account));
}
SelectGoalCheckboxes.ItemsSource = AccountList;
NetPay.Focus();
DoSummary();
}
private void SomethingChecked(object sender, RoutedEventArgs e)
{
DoSummary();
}
private async void Done_Click(object sender, RoutedEventArgs e)
{
if(await Process())
{
ShouldSave = true;
Close();
}
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void DoSummary()
{
List<Account> selectedAccounts = new List<Account>();
foreach (var account in AccountList.Where(item => item.IsSelected))
selectedAccounts.Add(account);
decimal minimumForGoals = PaycheckDistributor.MinimumRequired(selectedAccounts);
Minimum.Text = $"Minimum: {minimumForGoals:C}";
try
{
decimal income = decimal.Parse(NetPay.Text, NumberStyles.Currency);
if(income < minimumForGoals)
{
DisableSave();
SummaryLabel.Text = $"Minimum not met (Required: {minimumForGoals:C}, provided: {income:C})";
return;
}
try
{
var distribution = PaycheckDistributor.DistributePaycheck(selectedAccounts, income);
decimal distributionSum = 0;
foreach(var item in distribution)
{
distributionSum += item.Value;
}
EnableSave();
SummaryLabel.Text = $"Distributing {distributionSum:C} towards goals and {income - distributionSum:C} to general savings";
}
catch (PaycheckDistributionError err)
{
DisableSave();
SummaryLabel.Text = $"Error: {err.Message}";
}
}
catch
{
DisableSave();
SummaryLabel.Text = "Please input a valid currency value";
}
}
private void DisableSave()
{
Done.IsEnabled = false;
}
private void EnableSave()
{
Done.IsEnabled = true;
}
private void NetPay_TextChanged(object sender, TextChangedEventArgs e)
{
DoSummary();
}
private async Task<bool> Process()
{
// We don't need to validate anything here because the user can't click the
// button unless everything is already valid
var selectedAccounts = new List<Account>();
foreach (var account in AccountList.Where(item => item.IsSelected))
selectedAccounts.Add(account);
decimal income = decimal.Parse(NetPay.Text, NumberStyles.Currency);
if(income <= 0)
{
MessageBox.Show("Income must be positive and non-zero", "Not Enough Income",
MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
decimal totalPaid = 0;
string description = Description.Text;
// Create transactions
foreach (var record in PaycheckDistributor.DistributePaycheck(selectedAccounts, income))
{
Transaction txn = new Transaction();
txn.NewID();
txn.Amount = record.Value;
txn.Description = description;
Profile.GetAccountByID(record.Key.ID).Balance += record.Value;
totalPaid += record.Value;
await record.Key.RecordTransactionAsync(Profile, txn);
}
Profile.GeneralAccount.Balance += income - totalPaid;
// Save profile
await ProfileService.WriteAsync(Profile);
return true;
}
}
}