-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPayrollUI.cs
272 lines (241 loc) · 9.46 KB
/
PayrollUI.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using custom_alert_notifications;
using MetroFramework;
namespace Kimathi_Construction
{
public partial class PayrollUI : UserControl
{
private static PayrollUI _instance;
private Bitmap value;
public static PayrollUI Instance
{
get
{
if (_instance == null)
{
_instance = new PayrollUI();
}
return _instance;
}
}
public PayrollUI()
{
InitializeComponent();
}
public void Global_PayrollUI_Load()
{
GridInitializer(DateTime.Now);
}
private void PayrollUI_Load(object sender, EventArgs e)
{
//UI code
SetToolTip(owner, "Follow twitter @Job_Getabu ;)");
GridInitializer(DateTime.Now);
}
private void SetToolTip(Control ctl, string message)
{
ToolTip toolTip1 = new ToolTip();
toolTip1.UseFading = true;
toolTip1.UseAnimation = true;
toolTip1.IsBalloon = true;
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
toolTip1.ShowAlways = true;
toolTip1.SetToolTip(ctl, message);
}
private void owner_Click(object sender, EventArgs e)
{
//UI code
SetToolTip(owner, "Follow at twitter @Job_Getabu ;)");
string fbPage = "https://twitter.com/job_getabu";
try
{
System.Diagnostics.ProcessStartInfo sInfo = new System.Diagnostics.ProcessStartInfo(fbPage);
System.Diagnostics.Process.Start(sInfo);
}
catch (Exception) { }
}
private void gData_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
GridIconPicker(this.gData.Rows[e.RowIndex].Cells[6], this.gData.Rows[e.RowIndex].Cells[5], e);
}
private void GridIconPicker(DataGridViewCell rPic, DataGridViewCell pay, DataGridViewRowsAddedEventArgs e)
{
string test = (string)pay.Value;
string t1 = test.Remove(0, 4);
string t2 = t1.Replace(",", "");
int cAmount; //might use it later
int.TryParse(t2, out cAmount);
//styling
this.gData.Rows[e.RowIndex].Cells[3].Style = new DataGridViewCellStyle { ForeColor = Color.DeepPink };
this.gData.Rows[e.RowIndex].Cells[4].Style = new DataGridViewCellStyle { ForeColor = Color.Red };
this.gData.Rows[e.RowIndex].Cells[5].Style = new DataGridViewCellStyle { ForeColor = Color.Blue };
//add the
//if (cAmount == 0)
//{
// rPic.Value = Res._1pending;
//}
//else
//{
// rPic.Value = Res._1pending;
//}
this.gData.Rows[e.RowIndex].Cells[0].Value = Res.ok80;
this.gData.Rows[e.RowIndex].Cells[7].Value = Res.Trash_Can_50px;
rPic.Value = value;
}
private async void gData_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewImageColumn &&
e.RowIndex >= 0)
{
if (e.ColumnIndex == 7)
{
if ((await GridDelImageAsync(e.RowIndex)) != null)
{
using (var context = new KimathiEntities())
{
try
{
context.Entry<Work>(await GridDelImageAsync(e.RowIndex)).State = EntityState.Deleted;
context.SaveChanges();
//short Custom Notification
alert.Show("Deleted", alert.AlertType.warnig);
gData.Rows[e.RowIndex].Visible = false;
//Load the grid again
GridInitializer(dTimePickerGrid.Value);
}
catch (Exception exp)
{
MessageBox.Show("Something went wrong" + exp.Message, "Unsuccessful");
}
}
}
}
if (e.ColumnIndex == 0)
{
if (MetroMessageBox.Show(this, $"Hi do you want to pay {this.gData.Rows[e.RowIndex].Cells[2].Value.ToString()} {this.gData.Rows[e.RowIndex].Cells[5].Value.ToString()}", "Payment", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
{
//this employee wages have been payed
//get instance of employee and pay
Work xx = (await GridDelImageAsync(e.RowIndex));
xx.Payed = 1;
try
{
using (var context = new KimathiEntities())
{
context.Entry<Work>(xx).State = EntityState.Modified;
context.SaveChanges();
//short Custom Notification
alert.Show("Payment Successfull", alert.AlertType.info);
//Load the grid again
GridInitializer(dTimePickerGrid.Value);
}
}
catch (Exception) { }
}
}
}
}
private async void GridInitializer(DateTime leo)
{
//change color of INX to green
//gData.Columns[0].DefaultCellStyle.ForeColor = Color.Blue;
var bb = leo.Date.ToShortDateString();
var workListAsync = await Task.Factory.StartNew(() =>
{
using (var context = new KimathiEntities())
{
return context.Works
.OrderBy(x => x.Id)
//.Where(t => t.TimeIn.Value.Date.ToShortDateString() == bb)
.ToList();
}
});
gData.Rows.Clear();
try
{
foreach (var item in workListAsync)
{
if (item.TimeIn.Value.Date.ToShortDateString().Equals(bb))
{
var hrs = item.TimeOut.Value - item.TimeIn.Value;
var pay = hrs.TotalHours * int.Parse(tbRate.Text);
value = item.Payed == 0 ? Res._1pending : Res._1paid;
gData.Rows.Add(new string[]
{
null,
$"{empFound(item.IdNum_fk).IdNum}",
$"{empFound(item.IdNum_fk).Name}",
$"{item.TimeIn.Value.ToShortTimeString()}",
$"{item.TimeOut.Value.ToShortTimeString()}",
$"KES {String.Format("{0:0,0}", pay)}",
null,
null
});
}
}
}
catch (Exception)
{
throw;
}
}
private Employee empFound(long idnum)
{
using (var context = new KimathiEntities())
{
var userList = context.Employees.ToList();
foreach (var ss in userList.Where(a => a.IdNum == (idnum)))
{
if (ss.IdNum == idnum)
{
return ss;
}
}
return null;
}
}
private void dTimePickerGrid_ValueChanged(object sender, EventArgs e)
{
//set up the GridInitilizer
var dt = dTimePickerGrid.Value;
GridInitializer(dt);
}
private async Task<Work> GridDelImageAsync(int rowIndex)
{
List<Work> wkList = await Task.Factory.StartNew(() =>
{
using (var context = new KimathiEntities())
{
return context.Works.ToList();
}
});
string details;
details = $"{(string)this.gData.Rows[rowIndex].Cells[1].Value} {(string)this.gData.Rows[rowIndex].Cells[3].Value} {(string)this.gData.Rows[rowIndex].Cells[4].Value}";
foreach (var fi in wkList)
{
string id = $"{fi.IdNum_fk} {fi.TimeIn.Value.ToShortTimeString()} {fi.TimeOut.Value.ToShortTimeString()}";
if (id.Equals(details))
{
return fi;
}
}
return null;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
//Load the grid again
GridInitializer(dTimePickerGrid.Value);
}
}
}