using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ManagementSystem.Utility; namespace ManagementSystem { public partial class HRDataCorrect : Form { //程式內共用物件 string strFrmStatus = ""; //表單狀態 string strActiveUserID = ""; //取得登入作用的使用者帳號 SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID); SqlCommand sqlCmd = new SqlCommand(); string strPKey = ""; //程式內的Key值 string strKey = ""; //程式內加密的Key值 public HRDataCorrect() { InitializeComponent(); } #region 自定義程式 private string CheckForm() //確認畫面必填欄位 { string strResult = ""; return strResult; } private void SetupStatus() //畫面載入設定 { try { strActiveUserID = MainForm.strActiveUserID; strKey = MainForm.strKey; if (MainForm.strKey == "") { //設定Toolbar初始狀態 tsbSave.Enabled = false; tsbDelete.Enabled = false; } //設定畫面初始狀態 GetMemberData(); CleanForm(); CleanToolbar(); LockForm(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void GetMemberData() //取得成員資料 { try { string strSQL = "Select MemberID, MemberName, IsOutside From OTB_SYS_Members Where Effective = 'Y' And IsNull(IsOutside, 'N') = 'N'"; //進行查詢 dgvHRItem.DataSource = UtilityClass.GetSQLResult(strSQL).Tables["Result"]; Application.DoEvents(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void LockForm() //限制唯讀物件 { } private void UnLockForm() //解除限制唯讀物件 { } private void StatusChange(string strStatus) //變更主畫面狀態 { switch (strStatus.ToUpper()) { case "NONE": ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = ""; strFrmStatus = ""; CleanForm(); CleanToolbar(); dgvHRItem.ReadOnly = false; break; case "SEARCH": tsbSearch.Visible = false; tsbEdit.Enabled = false; tsbDelete.Enabled = false; tsbOK.Visible = true; tsbCancel.Visible = true; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋"; strFrmStatus = "SEARCH"; break; case "ADD": ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增"; strFrmStatus = "ADD"; break; case "MODIFY": tsbSearch.Enabled = false; tsbEdit.Visible = false; tsbSave.Visible = true; tsbDelete.Enabled = false; tsbCancel.Visible = true; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改"; strFrmStatus = "MODIFY"; break; case "DEL": tsbDelete.Visible = false; tsbSearch.Enabled = false; tsbEdit.Enabled = false; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除"; strFrmStatus = "DEL"; break; } } private void CleanToolbar() //清除工具列 { //設定Toolbar狀態 tsbSearch.Enabled = false; tsbSearch.Visible = false; tsbEdit.Enabled = false; tsbEdit.Visible = false; tsbDelete.Enabled = false; tsbDelete.Visible = false; tsbSave.Visible = false; tsbOK.Visible = false; tsbCancel.Visible = false; } private void CleanForm() //清除畫面 { //GetMemberData(); cbWorkType.Checked = false; cbEndDate.Checked = false; cbSubject.Checked = false; txtSubject.Text = ""; txtDescription.Text = ""; cbProjectNumber.Checked = false; txtProjectNumber.Text = ""; txtEventID.Text = ""; Application.DoEvents(); } private void AddEven() //新增事件 { try { //本功能無新增功能 } catch (Exception ex) { throw ex; } } private void SaveEven() //儲存事件 { try { StringBuilder sbSQL = new StringBuilder(); switch (tbPage.SelectedTab.Name.ToString()) { case "tbPage1": //更正工作資料及狀態 string strEventID = txtEventID.Text.Trim(); if (cbWorkType.Checked) sbSQL.AppendLine("Update OTB_MNG_DayWork Set Worktype = 'Prj' Where EventID = '" + strEventID + "';"); if (cbProjectNumber.Checked) sbSQL.AppendLine("Update OTB_MNG_DayWork Set ProjectNumber = '" + txtProjectNumber.Text.Trim() + "' Where EventID = '" + strEventID + "';"); if(cbSubject.Checked) sbSQL.AppendLine("Update OTB_MNG_DayWork Set Subject = '" + txtSubject.Text.Trim() + "', description = '" + txtDescription.Text.Trim() + "' Where EventID = '" + strEventID + "';"); if(cbEndDate.Checked) sbSQL.AppendLine("Update OTB_MNG_DayWork Set EndDate = '" + dtPickEnd.Text.Trim() + "' Where EventID = '"+ strEventID + "';"); break; case "tbPage2": //更正加班資料 sbSQL.AppendLine("Declare @EventID char(36);"); foreach (DataGridViewRow row in dgvDetail1.Rows) { if (!UtilityClass.IsExist("Select * from OTB_MNG_DayWork Where UserID = '" + row.Cells["cMemberIDDetail"].Value.ToString() + "' And EndDate ='" + row.Cells["cSignOutTime"].Value.ToString() + "'")) { if (row.Cells["cSelected"].Value != null) { //查詢卻變更的EventID sbSQL.AppendLine(" Select Top 1 @EventID = EventID From OTB_MNG_DayWork Where UserID = '" + row.Cells["cMemberIDDetail"].Value.ToString() + "' And convert(varchar,EndDate,23) = '" + row.Cells["cSignOutTime"].Value.ToString().Substring(0, 10) + "' Order By EndDate DESC;"); //進行變更 sbSQL.AppendLine(" Insert Into OTB_MNG_DayWork(OrganizationID, UserID, EventID, Subject, Description, StartDate, EndDate, IsPublic, Skin, Memo, CreateUser, CreateDate, ProjectNumber, Worktype, Comment, WeekPlanID)"); sbSQL.AppendLine(" Select OrganizationID, UserID, NewID() AS EventID, Subject, Description, EndDate AS StartDate, '" + row.Cells["cSignOutTime"].Value.ToString() + "' AS EndDate, IsPublic, Skin, '加班' as Memo, CreateUser,CreateDate, ProjectNumber, Worktype ,Comment, WeekPlanID"); sbSQL.AppendLine(" From OTB_MNG_DayWork Where EventID = @EventID;"); } } } break; } using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { sqlAdapter.UpdateCommand = new SqlCommand(); sqlAdapter.UpdateCommand.Connection = sqlConn; sqlAdapter.UpdateCommand.CommandText = sbSQL.ToString(); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.UpdateCommand.ExecuteNonQuery(); } MessageBox.Show("儲存成功", "提示"); StatusChange("None"); LockForm(); } catch (Exception ex) { MessageBox.Show("儲存失敗","提示"); ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void DelEven() //刪除事件 { StringBuilder sbSQL = new StringBuilder(); try { //執行Delete命令 //執行命令 StatusChange("None"); LockForm(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void GoEvent() //執行事件 { try { StringBuilder strSQL = new StringBuilder(""); switch (strFrmStatus.ToString()) { case "SEARCH": //搜尋 break; } StatusChange("None"); LockForm(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } #endregion #region 事件觸發及問題處理 private void tsbAdd_Click(object sender, EventArgs e) { try { CleanToolbar(); tsbSearch.Enabled = false; tsbEdit.Enabled = false; tsbSave.Visible = true; CleanForm(); UnLockForm(); StatusChange("ADD"); } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void tsbEdit_Click(object sender, EventArgs e) { try { if (strPKey == "") { MessageBox.Show("請先選擇資料", "提示"); return; } CleanToolbar(); UnLockForm(); StatusChange("Modify"); } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void tsbSave_Click(object sender, EventArgs e) { try { string strMessage = CheckForm(); if (strMessage == "") { this.SaveEven(); //GetMemberData(strPKey); } else { MessageBox.Show(strMessage,"提示"); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void tsbDelete_Click(object sender, EventArgs e) { try { if (MessageBox.Show("請問您確定要刪除本資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { this.DelEven(); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void tsbSearch_Click(object sender, EventArgs e) { try { //設定Toolbar狀態 CleanToolbar(); //開放查詢條件 UnLockForm(); //設定畫面物件狀態 CleanForm(); StatusChange("Search"); } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void tsbOK_Click(object sender, EventArgs e) { try { GoEvent(); //還原Toolbar狀態 tsbSearch.Enabled = true; tsbOK.Visible = false; //關閉查詢條件 LockForm(); StatusChange("Search"); } catch (Exception ex) { ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex); } } private void tsbCancel_Click(object sender, EventArgs e) { CleanForm(); CleanToolbar(); LockForm(); StatusChange("None"); } private void HRDataCorrect_Load(object sender, EventArgs e) { SetupStatus(); //設定畫面狀態 } private void tsbExit_Click(object sender, EventArgs e) { this.Close(); } private void tsbClean_Click(object sender, EventArgs e) { CleanForm(); } #endregion private void dgvHRItem_CellClick(object sender, DataGridViewCellEventArgs e) { txtEventID.Text = ""; GetCurrentData(); } private void GetCurrentData() { StringBuilder strSQL = new StringBuilder(); switch (tbPage.SelectedTab.Name.ToString()) { case "tbPage1": //更新工作資料及狀態 strSQL.Append(" Select EventID,Subject, description, StartDate, EndDate, ProjectNumber, Worktype From OTB_MNG_DayWork Where 1 = 1 and UserID = '" + (string)dgvHRItem.CurrentRow.Cells["cMemberID"].Value + "' "); strSQL.Append(" And StartDate Between '" + dpStart.Text.ToString() + " 00:00:00' And '" + dpEnd.Text.ToString() + " 23:59:59'"); strSQL.Append(" Order by StartDate"); //進行查詢 dgvDetail.DataSource = UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"]; Application.DoEvents(); break; case "tbPage2": //更正加班資料 strSQL.Append(" Select Trim(MemberID) MemberID, convert(varchar,SignTime,120) SignTime, convert(varchar,SignOutTime,120) SignOutTime From OTB_HR_CheckAttendance Where SignID Between '" + dpStart.Text.ToString().Replace("-","") +"' And '" + dpEnd.Text.ToString().Replace("-", "") + "'"); strSQL.Append(" And MemberID = '" + (string)dgvHRItem.CurrentRow.Cells["cMemberID"].Value + "' And SignOutTime is not null And Convert(varchar(50), SignOutTime,108) >= '19:00:00'"); strSQL.Append(" And Not Exists(Select * From OTB_MNG_DayWork Where UserID = '" + (string)dgvHRItem.CurrentRow.Cells["cMemberID"].Value + "' And EndDate > '" + dpEnd.Text.ToString() + " 19:00:00' And convert(varchar, EndDate, 23) = convert(varchar, " + dpEnd.Text.ToString() + ", 23))"); //未填寫加班匯報 dgvDetail1.DataSource = UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"]; Application.DoEvents(); break; } } private void btnUpdate_Click(object sender, EventArgs e) { SaveEven(); int intNowRow = dgvDetail.Rows.IndexOf(dgvDetail.CurrentRow); CleanForm(); GetCurrentData(); dgvDetail.Rows[intNowRow].Selected = true; } private void btnUpdate1_Click(object sender, EventArgs e) { int intSelectedCount = 0; foreach (DataGridViewRow row in dgvDetail1.Rows) { if (row.Cells["cSelected"].Value != null) { if ((bool)row.Cells["cSelected"].Value == true) { intSelectedCount += 1; } } } if (intSelectedCount > 0) { SaveEven(); CleanForm(); GetCurrentData(); } } private void dgvDetail_CellClick(object sender, DataGridViewCellEventArgs e) { txtEventID.Text = (string)dgvDetail.CurrentRow.Cells["cEventID"].Value; dtPickEnd.Value = (DateTime)dgvDetail.CurrentRow.Cells["cEndDate"].Value; txtProjectNumber.Text = (string)dgvDetail.CurrentRow.Cells["cProjectNumber"].Value; txtSubject.Text = (string)dgvDetail.CurrentRow.Cells["cSubject"].Value; txtDescription.Text = (string)dgvDetail.CurrentRow.Cells["cDescription"].Value; } private void btnPickProject_Click(object sender, EventArgs e) { PickProject pkItemForm = new PickProject(); pkItemForm.txtProjectNumber.Text = txtProjectNumber.Text.Trim(); pkItemForm.strOwnerForm = "HRDataCorrect"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } public void ReturnProjectInfo(string[] strProjectInfo) //回傳專案資料 { txtProjectNumber.Text = strProjectInfo[0].ToString(); } } }