-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm4.cs
379 lines (299 loc) · 14.9 KB
/
Form4.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Group_11___COSC_31112
{
public partial class frmAddNewSubT : Form
{
//create list for the items
List<string> AccountNames = new List<string>();
public frmAddNewSubT()
{
InitializeComponent();
}
private void frmAddNewSubT_Load(object sender, EventArgs e)
{
string connectionString = "Data Source=(localdb)\\Local;Initial Catalog=projectDatabase;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string sqlquery = "SELECT Account_name FROM Account";
using (SqlCommand command = new SqlCommand(sqlquery, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
// Load data into a DataTable
DataTable dataTable = new DataTable();
dataTable.Load(reader);
// Set the DataSource property of the ComboBox to the DataTable
cmbAccounts.DataSource = dataTable;
cmbAccounts.DisplayMember = "Account_name";
cmbAccounts.ValueMember = "Account_name";
}
}
connection.Close(); // Close the connection with the database
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void subNewSubT_Click_1(object sender, EventArgs e)
{
if (txtNewSubT.Text == "" || txtNewSubDesc.Text == "" || cmbSubTCat.SelectedItem == null || txtAmount.Text == "")
{
MessageBox.Show("Please fill all items", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
DataRowView selectedRow = (DataRowView)cmbAccounts.SelectedItem;
string accountName = selectedRow["Account_name"].ToString();
string subtask_name = txtNewSubT.Text;
string subtask_desc = txtNewSubDesc.Text;
string subtask_cat = cmbSubTCat.SelectedItem.ToString();
int acc_id = GetAccountId(accountName);
decimal amount_money = decimal.Parse(txtAmount.Text);
/* string some = cmbAccounts.SelectedItem.ToString();
MessageBox.Show("Account Name :" + some);*/
string connectionString = "Data Source=(localdb)\\Local;Initial Catalog=projectDatabase;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
if (cmbSubTCat.SelectedItem == "Income")
{
string sqlquery = "INSERT INTO Subtask (Subtask_name, Subtask_expence, Subtask_income, Account_id, Subtask_description, Subtask_catagory) VALUES (@Subtask_name, @Subtask_expence, @Subtask_income, @Account_id, @Subtask_description, @Subtask_catagory)";
using (SqlCommand command = new SqlCommand(sqlquery, connection))
{
command.Parameters.AddWithValue("@Subtask_name", subtask_name);
command.Parameters.AddWithValue("@Subtask_description", subtask_desc);
command.Parameters.AddWithValue("@Subtask_catagory", subtask_cat);
command.Parameters.AddWithValue("@Subtask_income", amount_money);
command.Parameters.AddWithValue("@Subtask_expence", "0.00");
command.Parameters.AddWithValue("@Account_id", acc_id);
command.ExecuteNonQuery();
MessageBox.Show("Data inserted to the Subtask successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Clear input fields after successful submission if desired
txtNewSubT.Text = string.Empty;
txtNewSubDesc.Text = string.Empty;
txtAmount.Text = string.Empty;
cmbSubTCat.SelectedItem = null;
connection.Close();//end the connection with database
}
}
else
{
string sqlquery = "INSERT INTO Subtask (Subtask_name, Subtask_expence, Subtask_income, Account_id, Subtask_description, Subtask_catagory) VALUES (@Subtask_name, @Subtask_expence, @Subtask_income, @Account_id, @Subtask_description, @Subtask_catagory)";
using (SqlCommand command = new SqlCommand(sqlquery, connection))
{
command.Parameters.AddWithValue("@Subtask_name", subtask_name);
command.Parameters.AddWithValue("@Subtask_description", subtask_desc);
command.Parameters.AddWithValue("@Subtask_catagory", subtask_cat);
command.Parameters.AddWithValue("@Subtask_income", "0.00");
command.Parameters.AddWithValue("@Subtask_expence", amount_money);
command.Parameters.AddWithValue("@Account_id", acc_id);
command.ExecuteNonQuery();
MessageBox.Show("Data inserted to the Subtask successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Clear input fields after successful submission if desired
txtNewSubT.Text = string.Empty;
txtNewSubDesc.Text = string.Empty;
txtAmount.Text = string.Empty;
cmbSubTCat.SelectedItem = null;
connection.Close();//end the connection with database
}
}
connection.Open();
// Get current account balance
decimal currentIncome = GetAccountIncome(connection, acc_id);
decimal currentExpence = GetAccountExpence(connection, acc_id);
decimal currentBalance = GetCurrentAccountBalance(connection, acc_id);
// Calculate new balance after adding the subtask amount
decimal newBalance = subtask_cat == "Income" ? currentBalance + amount_money : currentBalance - amount_money;
decimal newIncome = subtask_cat == "Income" ? currentIncome + amount_money : currentIncome + 0;
decimal newExpence = subtask_cat == "Expence" ? currentExpence + amount_money : currentExpence + 0;
// MessageBox.Show("currentIn :" + currentIncome + "\n curE " + currentExpence + "\n CURRBAL :" + currentBalance + "\n" + newIncome + "\n" + newExpence + "\n" + newBalance);
// Update the Account table with the new balance
UpdateAccountBalance(connection, acc_id, newBalance, newIncome, newExpence);
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error in Database(Subtask) Ocured " + ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private int GetAccountId(string accountName)
{
int accId = 0;
string connectionString = "Data Source=(localdb)\\Local;Initial Catalog=projectDatabase;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Account_id FROM Account WHERE Account_name = @accountName", connection))
{
cmd.Parameters.AddWithValue("@accountName", accountName);
object result = cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
accId = Convert.ToInt32(result);
}
}
}
//MessageBox.Show("The Account Id is" + accId);
return accId;
}
private void home_Click(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.Show();
}
private void editAcc_Click(object sender, EventArgs e)
{
}
private void newAcc_Click(object sender, EventArgs e)
{
this.Hide();
addNewAcc form3 = new addNewAcc();
form3.Show();
}
private decimal GetCurrentAccountBalance(SqlConnection connection, int accountId)
{
try
{
string query = "SELECT Balance FROM Account WHERE Account_id = @accountId";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@accountId", accountId);
object result = cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
return Convert.ToDecimal(result);
}
}
}
catch (Exception e)
{
MessageBox.Show("Error occured in GetAccountbalance() method : " + e.ToString());
}
return 0.00m;
}
private decimal GetAccountIncome(SqlConnection connection, int accountId)
{
try
{
string query = "SELECT Income FROM Account WHERE Account_id=@accountID";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@accountId", accountId);
object result = cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
return Convert.ToDecimal(result);
}
}
}
catch (Exception e)
{
MessageBox.Show("Error occured in GetAccountIncome() method : " + e.Message.ToString());
}
return 0.00m;
}
private decimal GetAccountExpence(SqlConnection connection, int accountId)
{
try
{
string query = "SELECT Expence FROM Account WHERE Account_id=@accountID";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@accountId", accountId);
object result = cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
return Convert.ToDecimal(result);
}
}
}
catch (Exception e)
{
MessageBox.Show("Error occured in GetAccountExpence() method : " + e.Message.ToString());
}
return 0.00m;
}
private void UpdateAccountBalance(SqlConnection connection, int accountId, decimal newBalance, decimal newIncome, decimal newExpence)
{
SqlTransaction transaction = null;
try
{
transaction = connection.BeginTransaction();
string queryBalance = "UPDATE Account SET Balance = @newBalance, Income = @newIncome, Expence = @newExpence WHERE Account_id = @accountId";
using (SqlCommand cmd = new SqlCommand(queryBalance, connection, transaction))
{
cmd.Parameters.AddWithValue("@newBalance", newBalance);
cmd.Parameters.AddWithValue("@newIncome", newIncome);
cmd.Parameters.AddWithValue("@newExpence", newExpence);
cmd.Parameters.AddWithValue("@accountId", accountId);
cmd.ExecuteNonQuery();
}
transaction.Commit();
MessageBox.Show("Account balance updated successfully.");
}
catch (Exception e)
{
transaction?.Rollback();
MessageBox.Show("Error occured in UpdateAccountbalance() method : " + e.ToString());
}
finally
{
transaction?.Dispose();
}
}
private void deleteAccountToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
deleteAcc form5 = new deleteAcc();
form5.Show();
}
private void deleteSubtaskToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form6 form6 = new Form6();
form6.Show();
}
private void getDetailedReportToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.Show();
}
private void btnAddNewSubT_Click(object sender, EventArgs e)
{
txtNewSubT.Text = null;
txtAmount.Text = null;
txtNewSubDesc.Text = null;
}
private void frmAddNewSubT_Load_1(object sender, EventArgs e)
{
}
private void getDetailedReportToolStripMenuItem_Click_1(object sender, EventArgs e)
{
}
private void btnAddNewSubT_Click_1(object sender, EventArgs e)
{
this.Refresh();
}
}
}