-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
229 lines (198 loc) · 7.8 KB
/
Form1.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Configuration;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//these are declared here so that dataRefresh and dataUpdate can both access them
SqlDataAdapter sdaexp;
SqlCommandBuilder scbexp;
DataTable dtexp;
SqlDataAdapter sdainc;
SqlCommandBuilder scbinc;
DataTable dtinc;
SqlDataAdapter sdabal;
SqlCommandBuilder scbbal;
DataTable dtbal;
//connects to the database and then fills the grids accordingly
private void dataRefresh()
{
try
{
//string expenseDate = null;
SqlConnection con = new SqlConnection("Data Source=.\\SQLBIRD;AttachDbFilename=C:\\Users\\Richardbi\\Documents\\ambr.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
sdaexp = new SqlDataAdapter(@"SELECT billName AS Name, billAmount as Amount, billDescription as Description, billId as ID FROM expenses" , con);
dtexp = new DataTable();
sdaexp.Fill(dtexp);
expenseGrid.DataSource = dtexp;
sdainc = new SqlDataAdapter(@"SELECT incomeName as Name, incomeAmount as Amount, CONVERT(DECIMAL(10,2),incomeTax) as Tax, CONVERT(DECIMAL(10,2),incomeAmount-(incomeAmount * isnull(incomeTax, 0.00))/100) as NetAmount ,incomeId as ID FROM income", con);
dtinc = new DataTable();
sdainc.Fill(dtinc);
incomeGrid.DataSource = dtinc;
sdabal = new SqlDataAdapter(@"SELECT balanceAccountName as Account, balanceAmount as Amount, balanceDescription as Description, balanceId as ID FROM balance", con);
dtbal = new DataTable();
sdabal.Fill(dtbal);
balanceGrid.DataSource = dtbal;
// Get the saved value from the application's settings
primaryAccount.Text = Properties.Settings.Default.primaryAccount;
// Refresh the total panel
updateCalculations(null, null);
}
catch
{
Console.WriteLine("Something failed when getting data");
}
}
//updates the database with user entered data in the grids
private void dataUpdate()
{
try
{
scbexp = new SqlCommandBuilder(sdaexp);
sdaexp.Update(dtexp);
scbinc = new SqlCommandBuilder(sdainc);
sdainc.Update(dtinc);
scbbal = new SqlCommandBuilder(sdabal);
sdabal.Update(dtbal);
updateCalculations(null, null);
}
catch
{
Console.WriteLine("Something failed during data update");
}
}
public Form1()
{
InitializeComponent();
dataRefresh();
}
//This is the brain... (a simple brain) (for now)
private void updateCalculations(object sender, EventArgs e)
{
decimal expenseSum = 0;
decimal incomeSum = 0;
decimal incomeAftTax = 0;
decimal currBalance = 0;
// string primaryAccountS = Properties.Settings.Default.primaryAccount;
try
{
//Expense loop
for (int i = 0; i < expenseGrid.Rows.Count; ++i)
{
expenseSum += Convert.ToDecimal(expenseGrid.Rows[i].Cells[1].Value);
}
//Income loop
for (int i = 0; i < incomeGrid.Rows.Count; ++i)
{
incomeSum += Convert.ToDecimal(incomeGrid.Rows[i].Cells[1].Value);
incomeAftTax += Convert.ToDecimal(incomeGrid.Rows[i].Cells[3].Value);
}
}
catch
{
Console.WriteLine(expenseSum);
}
try
{
for (int i = 0; i < balanceGrid.Rows.Count; ++i)
{
if (primaryAccount.Text == "")
{
currBalance += Convert.ToDecimal(balanceGrid.Rows[i].Cells[1].Value);
}
else
{
if (Convert.ToInt32(balanceGrid.Rows[i].Cells[3].Value) == Convert.ToInt32(primaryAccount.Text))
{
currBalance = Convert.ToDecimal(balanceGrid.Rows[i].Cells[1].Value);
}
}
}
}
catch
{
Console.WriteLine("Balance Grid failed for some reason");
}
decimal total;
try
{
total = currBalance - expenseSum + incomeAftTax;
expenseTotalV.Text = String.Format("{0,-10:C}", expenseSum);
incomeTotal.Text = String.Format("{0,-10:C}", incomeSum);
incomeAftTaxV.Text = String.Format("{0,-10:C}", incomeAftTax);
afterExpTotalV.Text = String.Format("{0,-10:C}", total);
currBalanceV.Text = String.Format("{0,-10:C}", currBalance);
}
catch
{
Console.WriteLine("Calculation update failed");
}
}
private void gridCellLeave(object sender, DataGridViewCellEventArgs e)
{
dataUpdate();
updateCalculations(null, null);
}
private void deleteRowUpdate(object sender, DataGridViewRowEventArgs e)
{
try
{
dataUpdate();
dataRefresh();
Console.WriteLine("Row deleted");
}
catch
{
Console.WriteLine("Delete Row Failed");
}
}
private void calculateTax(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLBIRD;AttachDbFilename=C:\\Users\\Richardbi\\Documents\\ambr.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
sdainc = new SqlDataAdapter(@"SELECT incomeName as Name, incomeAmount as Amount, CONVERT(DECIMAL(10,2),incomeTax) as Tax, CONVERT(DECIMAL(10,2),incomeAmount-(incomeAmount * isnull(incomeTax, 0.00))/100) as NetAmount ,incomeId as ID FROM income", con);
dtinc = new DataTable();
sdainc.Fill(dtinc);
incomeGrid.DataSource = dtinc;
updateCalculations(null,null);
Console.WriteLine("User Clicked Calculate Tax");
}
private void refreshButtonClick(object sender, EventArgs e)
{
try
{
dataRefresh();
updateCalculations(null, null);
Console.WriteLine("User clicked Refresh Records");
}
catch
{
Console.WriteLine("Refresh Button Failed");
}
}
private void doneCloseClick(object sender, EventArgs e)
{
try
{
// Save any un-commited changes
dataUpdate();
// Save the value in the text box in the application's settings
Properties.Settings.Default.primaryAccount = primaryAccount.Text;
// Tell the application to save the new values
Properties.Settings.Default.Save();
this.Close();
}
catch
{
Console.WriteLine("Update/Done Failed");
}
}
}
}