-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEditGeneralLedger.cs
310 lines (275 loc) · 9.55 KB
/
EditGeneralLedger.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
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;
using Devart.Common;
using Devart.Data.PostgreSql;
namespace ShareTrading
{
public partial class EditGeneralLedger : Form
{
bool editing = false;
DBAccess.GLCodes currentRecord = new DBAccess.GLCodes();
public EditGeneralLedger()
{
InitializeComponent();
initialiseForm();
}
private void initialiseForm()
{
whileEditing(false);
cbxGLType.DataSource = System.Enum.GetNames(typeof(DBAccess.GLType));
// set up list of existing records
refreshGrid(0);
cbxGLType.Focus();
}
private void displayHighlightedRow()
{
try
{
int rowIdx = dgvGeneralLedger.CurrentCell.RowIndex;
currentRecord = ((List<DBAccess.GLCodes>)GLBindingSource.DataSource)[rowIdx];
tbxGLCode.Text = currentRecord.GLCode;
cbxGLType.SelectedIndex = ((List<string>)cbxGLType.DataSource).FindIndex(x => x == currentRecord.Type.ToString());
chbGSTApplies.Checked = currentRecord.GSTApplies;
}
catch { }
}
private void EditGeneralLedger_FormClosing(object sender, FormClosingEventArgs e)
{
if (editing)
{
MessageBox.Show("Please save or Cancel your changes", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Close();
}
private void whileEditing(bool state)
{
toolStripButtonClose.Enabled = !state;
toolStripButtonAdd.Enabled = !state;
toolStripButtonEdit.Enabled = !state;
toolStripButtonDelete.Enabled = !state;
toolStripButtonUnDelete.Enabled = false;
toolStripButtonNext.Enabled = !state;
toolStripButtonPrev.Enabled = !state;
toolStripButtonCancel.Enabled = state;
toolStripButtonSave.Enabled = state;
editing = state;
cbxGLType.Enabled = state;
tbxGLCode.Enabled = state;
chbGSTApplies.Enabled = state;
}
private void editingDeleted()
{
toolStripButtonClose.Enabled = true;
toolStripButtonAdd.Enabled = false;
toolStripButtonEdit.Enabled = false;
toolStripButtonDelete.Enabled = false;
toolStripButtonUnDelete.Enabled = true;
toolStripButtonNext.Enabled = true;
toolStripButtonPrev.Enabled = true;
toolStripButtonCancel.Enabled = false;
toolStripButtonSave.Enabled = false;
editing = false;
cbxGLType.Enabled = false;
tbxGLCode.Enabled = false;
chbGSTApplies.Enabled = false;
}
private void toolStripButtonAdd_Click(object sender, EventArgs e)
{
whileEditing(true);
cbxGLType.SelectedIndex = 0;
tbxGLCode.Text = string.Empty;
chbGSTApplies.Checked = false;
currentRecord = new DBAccess.GLCodes();
}
private DBAccess.GLType getType()
{
string txt = cbxGLType.SelectedValue.ToString();
DBAccess.GLType val = 0;
Enum.TryParse(txt, out val);
return val;
}
private void toolStripButtonSave_Click(object sender, EventArgs e)
{
if (!editing)
return;
// Adding a record so just make sure it's not a duplicate
List<DBAccess.GLCodes> list = new List<DBAccess.GLCodes>();
List<PgSqlParameter> paramList = new List<PgSqlParameter>();
paramList.Add(new PgSqlParameter("@P2", tbxGLCode.Text));
paramList.Add(new PgSqlParameter("@P3", (int) getType()));
if (currentRecord.ID == 0)
{
if (DBAccess.GetGLRecords(paramList, out list, DBAccess.GLFieldList, " AND gl_code = @P2 AND gl_type = @P3 " /* extraWhere */, string.Empty /* orderBy */))
{
MessageBox.Show("Duplicate Type / Code combination", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Time to add a record
currentRecord = new DBAccess.GLCodes();
currentRecord.GLCode = tbxGLCode.Text;
currentRecord.GSTApplies = chbGSTApplies.Checked;
currentRecord.Type = getType();
DBAccess.DBInsert(currentRecord, "glcodes", typeof(DBAccess.GLCodes));
paramList = new List<PgSqlParameter>();
paramList.Add(new PgSqlParameter("@P4", tbxGLCode.Text));
paramList.Add(new PgSqlParameter("@P5", (int)getType()));
if (DBAccess.GetGLRecords(paramList, out list, DBAccess.GLFieldList, " AND gl_code = @P4 AND gl_type = @P5 " /* extraWhere */, string.Empty /* orderBy */))
currentRecord.ID = list[0].ID;
}
else
{
// Update record
paramList.Add(new PgSqlParameter("@P4", currentRecord.ID));
if (DBAccess.GetGLRecords(paramList, out list, DBAccess.GLFieldList, " AND gl_code = @P2 AND gl_type = @P3 AND gl_id != @P4 " /* extraWhere */, string.Empty /* orderBy */))
{
MessageBox.Show("Duplicate Type / Code combination", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Time to add a record
currentRecord.GLCode = tbxGLCode.Text;
currentRecord.GSTApplies = chbGSTApplies.Checked;
currentRecord.Type = getType();
DBAccess.DBUpdate(currentRecord, "glcodes", typeof(DBAccess.GLCodes));
}
whileEditing(false);
// refresh the grid
refreshGrid(currentRecord.ID);
}
private void refreshGrid(int id)
{
GLBindingSource.DataSource = null;
List<DBAccess.GLCodes> list = new List<DBAccess.GLCodes>();
DBAccess.GetGLRecords(new List<PgSqlParameter>(), out list, DBAccess.GLFieldList, string.Empty /* extraWhere */, " ORDER BY gl_type ", chbIncDeleted.Checked);
GLBindingSource.DataSource = list;
if (GLBindingSource != null && GLBindingSource.Count > 0)
{
// display in data grid
dgvGeneralLedger.DataSource = GLBindingSource;
dgvGeneralLedger.Refresh();
highlightSelectedRow(id);
displayHighlightedRow();
}
else
dgvGeneralLedger.Enabled = false;
}
private void highlightSelectedRow(int id)
{
//
try
{
dgvGeneralLedger.ClearSelection();
if (id == 0)
dgvGeneralLedger.Rows[0].Selected = true;
else
{
foreach (DataGridViewRow row in dgvGeneralLedger.Rows)
{
if (row.Cells["ID"].Value.Equals(id))
{
dgvGeneralLedger.Rows[row.Index].Cells[0].Selected = true;
dgvGeneralLedger.CurrentCell = dgvGeneralLedger.Rows[row.Index].Cells[0];
break;
}
}
}
}
catch
{ }
}
private void dgvGeneralLedger_SelectionChanged(object sender, EventArgs e)
{
displayHighlightedRow();
if (DateTime.Compare(currentRecord.DateDeleted, DateTime.MinValue) == 0)
whileEditing(editing);
else
editingDeleted();
}
private void toolStripButtonEdit_Click(object sender, EventArgs e)
{
if (currentRecord.ID == 0)
{
MessageBox.Show("Please select row to edit", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
whileEditing(true);
}
private void toolStripButtonCancel_Click(object sender, EventArgs e)
{
// Cancel Insert or Update
displayHighlightedRow();
whileEditing(false);
}
private void toolStripButtonDelete_Click(object sender, EventArgs e)
{
//
currentRecord.DateDeleted = DateTime.Now;
DBAccess.DBUpdate(currentRecord, "glcodes", typeof(DBAccess.GLCodes));
refreshGrid(chbIncDeleted.Checked ? currentRecord.ID : 0);
displayHighlightedRow();
}
private void toolStripButtonClose_Click(object sender, EventArgs e)
{
if (editing)
{
MessageBox.Show("Please save or Cancel your changes", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
this.Close();
}
private void toolStripButtonPrev_Click(object sender, EventArgs e)
{
try
{
int rowIdx = dgvGeneralLedger.CurrentCell.RowIndex;
if (rowIdx <= 0)
return;
rowIdx -= 1;
dgvGeneralLedger.ClearSelection();
dgvGeneralLedger.Rows[rowIdx].Cells[0].Selected = true;
dgvGeneralLedger.CurrentCell = dgvGeneralLedger.Rows[rowIdx].Cells[0];
displayHighlightedRow();
}
catch { }
}
private void toolStripButtonNext_Click(object sender, EventArgs e)
{
try
{
int rowIdx = dgvGeneralLedger.CurrentCell.RowIndex;
if (rowIdx >= dgvGeneralLedger.RowCount - 1)
return;
rowIdx += 1;
dgvGeneralLedger.ClearSelection();
dgvGeneralLedger.Rows[rowIdx].Cells[0].Selected = true;
dgvGeneralLedger.CurrentCell = dgvGeneralLedger.Rows[rowIdx].Cells[0];
displayHighlightedRow();
}
catch { }
}
private void chbIncDeleted_CheckedChanged(object sender, EventArgs e)
{
refreshGrid(currentRecord.ID);
}
private void dgvGeneralLedger_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow row = dgvGeneralLedger.Rows[e.RowIndex];
if (DateTime.Compare(DateTime.Parse(row.Cells["DateDeleted"].Value.ToString()), DateTime.MinValue) != 0)
row.DefaultCellStyle.ForeColor = Color.Red;
}
private void toolStripButtonUnDelete_Click(object sender, EventArgs e)
{
currentRecord.DateDeleted = DateTime.MinValue;
DBAccess.DBUpdate(currentRecord, "glcodes", typeof(DBAccess.GLCodes));
refreshGrid(currentRecord.ID);
displayHighlightedRow();
}
}
}