-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPurchaseListWindow.cs
177 lines (145 loc) · 6.18 KB
/
PurchaseListWindow.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GroceryExpenseTracker
{
public partial class PurchaseListWindow : Form
{
private Application mainApp;
private Model.PurchaseList originalPurchaseList = null;
private bool isEditing = false;
private void ActivateNewListMode()
{
tbMarketName.Visible = true;
lblMarketName.Visible = false;
lblGrandTotal.Visible = false;
btnSaveList.Enabled = true;
dgPurchaseList.Columns[3].Visible = false;
lblDate.Text = DateTime.Now.ToString();
}
private void DisplayPurchaseList(Model.PurchaseList purchaseList)
{
float grandTotal = 0;
for(int i=0; i < purchaseList.ProductList.Count; i++)
{
Model.Product product = purchaseList.ProductList[i];
float productTotalPrice = product.QTY * product.UnitPrice;
if(dgPurchaseList.Rows.GetRowCount(DataGridViewElementStates.Displayed) - 1 != purchaseList.ProductList.Count)
dgPurchaseList.Rows.Add();
var row = dgPurchaseList.Rows[i];
row.Cells[0].Value = product.Name;
row.Cells[1].Value = product.QTY.ToString();
row.Cells[2].Value = product.UnitPrice.ToString();
row.Cells[3].Value = productTotalPrice.ToString();
grandTotal += productTotalPrice;
}
lblMarketName.Text = purchaseList.MarketName;
lblDate.Text = purchaseList.Date.ToString();
lblGrandTotal.Text = "Grand Total: " + grandTotal.ToString() + SettingsUtil.GetCurrencySymbol();
}
public PurchaseListWindow(Application mainApp, Model.PurchaseList purchaseList = null)
{
this.mainApp = mainApp;
this.mainApp.Hide();
InitializeComponent();
MinimumSize = Size;
if(purchaseList == null) ActivateNewListMode();
else
{
isEditing = true;
originalPurchaseList = purchaseList;
btnSaveList.Text = "Update";
btnDelete.Visible = true;
DisplayPurchaseList(purchaseList);
}
}
private void btnSaveList_Click(object sender, EventArgs e)
{
bool exception = false;
Model.PurchaseList purchaseList = new Model.PurchaseList
{
MarketName = isEditing ? originalPurchaseList.MarketName : tbMarketName.Text,
Date = isEditing ? originalPurchaseList.Date : DateTime.Now
};
for(int i=0; i < dgPurchaseList.RowCount - 1; i++)
{
try
{
var row = dgPurchaseList.Rows[i];
Model.Product product = new Model.Product
{
Name = row.Cells[0].Value.ToString(),
QTY = Convert.ToUInt32(row.Cells[1].Value),
UnitPrice = Convert.ToSingle(row.Cells[2].Value.ToString().Replace('.', ','))
};
purchaseList.ProductList.Add(product);
}
catch(Exception ex)
{
exception = true;
MessageBox.Show("An error occured. Please see below for more details:\n"+ex.Message, "Error", MessageBoxButtons.OK);
break;
}
}
if(exception) return;
if(isEditing)
{
for(int i=0; i < mainApp.purchaseHistory.History.Count; i++)
{
if(originalPurchaseList == mainApp.purchaseHistory.History[i])
mainApp.purchaseHistory.History[i] = purchaseList;
originalPurchaseList = purchaseList;
}
}
else mainApp.purchaseHistory.History.Insert(0, purchaseList);
mainApp.purchaseHistory.Save(mainApp.purchaseDataFile);
MessageBox.Show(isEditing ? "Purchase list has been updated." : "Purchase list has been saved.", "Info", MessageBoxButtons.OK);
if(!isEditing)
{
mainApp.LoadPurchaseData();
Close();
}
else
{
btnSaveList.Enabled = false;
DisplayPurchaseList(originalPurchaseList);
}
}
private void PurchaseListWindow_FormClosed(object sender, FormClosedEventArgs e)
{
mainApp.Show();
}
private void dgPurchaseList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
btnSaveList.Enabled = true;
// QTY || Unit Price
if(e.ColumnIndex == 1 || e.ColumnIndex == 2)
{
dgPurchaseList.Rows[e.RowIndex].Cells[3].Value = "-";
lblGrandTotal.Text = "Grand Total: -";
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if(isEditing
&& MessageBox.Show("Are you sure to delete this purchase list?", "Info", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
for(int i=0; i < mainApp.purchaseHistory.History.Count; i++)
{
if(originalPurchaseList == mainApp.purchaseHistory.History[i])
mainApp.purchaseHistory.History.RemoveAt(i);
}
mainApp.purchaseHistory.Save(mainApp.purchaseDataFile);
mainApp.LoadPurchaseData();
MessageBox.Show("Purchase list has been deleted.", "Info", MessageBoxButtons.OK);
Close();
}
}
}
}