-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormEmployee.cs
More file actions
178 lines (159 loc) · 6.73 KB
/
FormEmployee.cs
File metadata and controls
178 lines (159 loc) · 6.73 KB
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
using CSEmployeeAttendance25.Data;
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 CSEmployeeAttendance25
{
public partial class FormEmployee : Form
{
private EmployeeDBHelper _employeeHelper;
private readonly long? _employeeId;
public FormEmployee(long? employeeId = null)
{
InitializeComponent();
_employeeHelper = new EmployeeDBHelper();
_employeeId = employeeId;
if (_employeeId.HasValue)
{
LoadEmployeeData();
}
}
private void LoadEmployeeData()
{
EmployeeDTO employee = _employeeHelper.GetEmployeeById(_employeeId.Value);
if (employee != null)
{
textBoxEmployeeCode.Text = employee.EmployeeCode;
textBoxBMEmployeId.Text = employee.BMEmployeeId.ToString();
dtpJoiningDate.Value = employee.JoiningDate;
dtpBirthDate.Value = employee.BirthDate;
textBoxEmployeeName.Text = employee.EmployeeName;
textBoxAddress.Text = employee.Address;
textBoxEmailId.Text = employee.EmailId;
textBoxContactNo1.Text = employee.ContactNumber1;
textBoxContactNo2.Text = employee.ContactNumber2;
textBoxDepartmentName.Text = employee.Department;
textBoxDesignation.Text = employee.Designation;
numericMonthlySalary.Value = employee.MonthlySalary;
numericHourlySalary.Value = employee.HourlySalary;
dtpShiftStart.Value = DateTime.Today.Add(employee.ShiftStart);
dtpShiftEnd.Value = DateTime.Today.Add(employee.ShiftEnd);
checkBoxIsActive.Checked = employee.IsActive;
}
}
private void FormEmployee_Load(object sender, EventArgs e)
{
}
private void buttonSave_Click(object sender, EventArgs e)
{
ValidateData();
int.TryParse(textBoxBMEmployeId.Text.Trim(), out int bmEmployeeId);
EmployeeDTO employee = new EmployeeDTO
{
EmployeeCode = textBoxEmployeeCode.Text.Trim(),
BMEmployeeId = bmEmployeeId,
JoiningDate = dtpJoiningDate.Value,
BirthDate = dtpBirthDate.Value,
EmployeeName = textBoxEmployeeName.Text.Trim(),
Address = textBoxAddress.Text.Trim(),
EmailId = textBoxEmailId.Text.Trim(),
ContactNumber1 = textBoxContactNo1.Text.Trim(),
ContactNumber2 = textBoxContactNo2.Text.Trim(),
Department = textBoxDepartmentName.Text.Trim(),
Designation = textBoxDesignation.Text.Trim(),
MonthlySalary = numericMonthlySalary.Value,
HourlySalary = numericHourlySalary.Value,
ShiftStart = dtpShiftStart.Value.TimeOfDay,
ShiftEnd = dtpShiftEnd.Value.TimeOfDay,
IsActive = checkBoxIsActive.Checked
};
bool success;
if (_employeeId.HasValue)
{
employee.EmployeeId = _employeeId.Value;
success = _employeeHelper.UpdateEmployee(employee);
}
else
{
success = _employeeHelper.AddEmployee(employee);
}
if (success)
{
MessageBox.Show("Employee saved successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("Error saving employee data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ValidateData()
{
// Validate required fields
if (string.IsNullOrWhiteSpace(textBoxEmployeeCode.Text))
{
MessageBox.Show("Employee Code is required.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBoxEmployeeName.Focus();
return;
}
if (string.IsNullOrWhiteSpace(textBoxBMEmployeId.Text))
{
MessageBox.Show("BM Employe Id is required.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBoxEmployeeName.Focus();
return;
}
if (string.IsNullOrWhiteSpace(textBoxEmployeeName.Text))
{
MessageBox.Show("Employee Name is required.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBoxEmployeeName.Focus();
return;
}
if (string.IsNullOrWhiteSpace(textBoxContactNo1.Text))
{
MessageBox.Show("Contact Number(1) is required.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBoxContactNo1.Focus();
return;
}
// Validate Monthly Salary (ensure it's a decimal)
if (numericMonthlySalary.Value <= 0)
{
MessageBox.Show("Please enter a valid Monthly Salary.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
numericMonthlySalary.Focus();
return;
}
if (numericHourlySalary.Value <= 0)
{
MessageBox.Show("Please enter a valid Hourly Salary.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
numericHourlySalary.Focus();
return;
}
// Validate Shift Start and Shift End (ensure they are selected)
if (dtpShiftStart.Value == null || dtpShiftEnd.Value == null)
{
MessageBox.Show("Shift Start and Shift End are required.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Ensure Shift Start is before Shift End
//if (dtpShiftStart.Value.TimeOfDay >= dtpShiftEnd.Value.TimeOfDay)
//{
// MessageBox.Show("Shift Start must be before Shift End.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// return;
//}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}