You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

513 lines
18 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.Sql;
  6. using System.Data.SqlClient;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using ManagementSystem.Utility;
  13. namespace ManagementSystem
  14. {
  15. public partial class HRDataCorrect : Form
  16. {
  17. //程式內共用物件
  18. string strFrmStatus = ""; //表單狀態
  19. string strActiveUserID = ""; //取得登入作用的使用者帳號
  20. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  21. SqlCommand sqlCmd = new SqlCommand();
  22. string strPKey = ""; //程式內的Key值
  23. string strKey = ""; //程式內加密的Key值
  24. public HRDataCorrect()
  25. {
  26. InitializeComponent();
  27. }
  28. #region 自定義程式
  29. private string CheckForm() //確認畫面必填欄位
  30. {
  31. string strResult = "";
  32. return strResult;
  33. }
  34. private void SetupStatus() //畫面載入設定
  35. {
  36. try
  37. {
  38. strActiveUserID = MainForm.strActiveUserID;
  39. strKey = MainForm.strKey;
  40. if (MainForm.strKey == "")
  41. {
  42. //設定Toolbar初始狀態
  43. tsbSave.Enabled = false;
  44. tsbDelete.Enabled = false;
  45. }
  46. //設定畫面初始狀態
  47. GetMemberData();
  48. CleanForm();
  49. CleanToolbar();
  50. LockForm();
  51. }
  52. catch (Exception ex)
  53. {
  54. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  55. }
  56. }
  57. private void GetMemberData() //取得成員資料
  58. {
  59. try
  60. {
  61. string strSQL = "Select MemberID, MemberName, IsOutside From OTB_SYS_Members Where Effective = 'Y' And IsNull(IsOutside, 'N') = 'N'";
  62. //進行查詢
  63. dgvHRItem.DataSource = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
  64. Application.DoEvents();
  65. }
  66. catch (Exception ex)
  67. {
  68. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  69. }
  70. }
  71. private void LockForm() //限制唯讀物件
  72. {
  73. }
  74. private void UnLockForm() //解除限制唯讀物件
  75. {
  76. }
  77. private void StatusChange(string strStatus) //變更主畫面狀態
  78. {
  79. switch (strStatus.ToUpper())
  80. {
  81. case "NONE":
  82. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  83. strFrmStatus = "";
  84. CleanForm();
  85. CleanToolbar();
  86. dgvHRItem.ReadOnly = false;
  87. break;
  88. case "SEARCH":
  89. tsbSearch.Visible = false;
  90. tsbEdit.Enabled = false;
  91. tsbDelete.Enabled = false;
  92. tsbOK.Visible = true;
  93. tsbCancel.Visible = true;
  94. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  95. strFrmStatus = "SEARCH";
  96. break;
  97. case "ADD":
  98. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  99. strFrmStatus = "ADD";
  100. break;
  101. case "MODIFY":
  102. tsbSearch.Enabled = false;
  103. tsbEdit.Visible = false;
  104. tsbSave.Visible = true;
  105. tsbDelete.Enabled = false;
  106. tsbCancel.Visible = true;
  107. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  108. strFrmStatus = "MODIFY";
  109. break;
  110. case "DEL":
  111. tsbDelete.Visible = false;
  112. tsbSearch.Enabled = false;
  113. tsbEdit.Enabled = false;
  114. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  115. strFrmStatus = "DEL";
  116. break;
  117. }
  118. }
  119. private void CleanToolbar() //清除工具列
  120. {
  121. //設定Toolbar狀態
  122. tsbSearch.Enabled = false;
  123. tsbSearch.Visible = false;
  124. tsbEdit.Enabled = false;
  125. tsbEdit.Visible = false;
  126. tsbDelete.Enabled = false;
  127. tsbDelete.Visible = false;
  128. tsbSave.Visible = false;
  129. tsbOK.Visible = false;
  130. tsbCancel.Visible = false;
  131. }
  132. private void CleanForm() //清除畫面
  133. {
  134. //GetMemberData();
  135. cbWorkType.Checked = false;
  136. cbEndDate.Checked = false;
  137. cbSubject.Checked = false;
  138. txtSubject.Text = "";
  139. txtDescription.Text = "";
  140. cbProjectNumber.Checked = false;
  141. txtProjectNumber.Text = "";
  142. txtEventID.Text = "";
  143. Application.DoEvents();
  144. }
  145. private void AddEven() //新增事件
  146. {
  147. try
  148. {
  149. //本功能無新增功能
  150. }
  151. catch (Exception ex)
  152. {
  153. throw ex;
  154. }
  155. }
  156. private void SaveEven() //儲存事件
  157. {
  158. try
  159. {
  160. StringBuilder sbSQL = new StringBuilder();
  161. switch (tbPage.SelectedTab.Name.ToString())
  162. {
  163. case "tbPage1": //更正工作資料及狀態
  164. string strEventID = txtEventID.Text.Trim();
  165. if (cbWorkType.Checked)
  166. sbSQL.AppendLine("Update OTB_MNG_DayWork Set Worktype = 'Prj' Where EventID = '" + strEventID + "';");
  167. if (cbProjectNumber.Checked)
  168. sbSQL.AppendLine("Update OTB_MNG_DayWork Set ProjectNumber = '" + txtProjectNumber.Text.Trim() + "' Where EventID = '" + strEventID + "';");
  169. if(cbSubject.Checked)
  170. sbSQL.AppendLine("Update OTB_MNG_DayWork Set Subject = '" + txtSubject.Text.Trim() + "', description = '" + txtDescription.Text.Trim() + "' Where EventID = '" + strEventID + "';");
  171. if(cbEndDate.Checked)
  172. sbSQL.AppendLine("Update OTB_MNG_DayWork Set EndDate = '" + dtPickEnd.Text.Trim() + "' Where EventID = '"+ strEventID + "';");
  173. break;
  174. case "tbPage2": //更正加班資料
  175. sbSQL.AppendLine("Declare @EventID char(36);");
  176. foreach (DataGridViewRow row in dgvDetail1.Rows)
  177. {
  178. if (!UtilityClass.IsExist("Select * from OTB_MNG_DayWork Where UserID = '" + row.Cells["cMemberIDDetail"].Value.ToString() + "' And EndDate ='" + row.Cells["cSignOutTime"].Value.ToString() + "'"))
  179. {
  180. if (row.Cells["cSelected"].Value != null)
  181. {
  182. //查詢卻變更的EventID
  183. 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;");
  184. //進行變更
  185. sbSQL.AppendLine(" Insert Into OTB_MNG_DayWork(OrganizationID, UserID, EventID, Subject, Description, StartDate, EndDate, IsPublic, Skin, Memo, CreateUser, CreateDate, ProjectNumber, Worktype, Comment, WeekPlanID)");
  186. 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");
  187. sbSQL.AppendLine(" From OTB_MNG_DayWork Where EventID = @EventID;");
  188. }
  189. }
  190. }
  191. break;
  192. }
  193. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  194. {
  195. sqlAdapter.UpdateCommand = new SqlCommand();
  196. sqlAdapter.UpdateCommand.Connection = sqlConn;
  197. sqlAdapter.UpdateCommand.CommandText = sbSQL.ToString();
  198. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  199. {
  200. sqlConn.Open();
  201. }
  202. sqlAdapter.UpdateCommand.ExecuteNonQuery();
  203. }
  204. MessageBox.Show("儲存成功", "提示");
  205. StatusChange("None");
  206. LockForm();
  207. }
  208. catch (Exception ex)
  209. {
  210. MessageBox.Show("儲存失敗","提示");
  211. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  212. }
  213. }
  214. private void DelEven() //刪除事件
  215. {
  216. StringBuilder sbSQL = new StringBuilder();
  217. try
  218. {
  219. //執行Delete命令
  220. //執行命令
  221. StatusChange("None");
  222. LockForm();
  223. }
  224. catch (Exception ex)
  225. {
  226. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  227. }
  228. }
  229. private void GoEvent() //執行事件
  230. {
  231. try
  232. {
  233. StringBuilder strSQL = new StringBuilder("");
  234. switch (strFrmStatus.ToString())
  235. {
  236. case "SEARCH": //搜尋
  237. break;
  238. }
  239. StatusChange("None");
  240. LockForm();
  241. }
  242. catch (Exception ex)
  243. {
  244. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  245. }
  246. }
  247. #endregion
  248. #region 事件觸發及問題處理
  249. private void tsbAdd_Click(object sender, EventArgs e)
  250. {
  251. try
  252. {
  253. CleanToolbar();
  254. tsbSearch.Enabled = false;
  255. tsbEdit.Enabled = false;
  256. tsbSave.Visible = true;
  257. CleanForm();
  258. UnLockForm();
  259. StatusChange("ADD");
  260. }
  261. catch (Exception ex)
  262. {
  263. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  264. }
  265. }
  266. private void tsbEdit_Click(object sender, EventArgs e)
  267. {
  268. try
  269. {
  270. if (strPKey == "")
  271. {
  272. MessageBox.Show("請先選擇資料", "提示");
  273. return;
  274. }
  275. CleanToolbar();
  276. UnLockForm();
  277. StatusChange("Modify");
  278. }
  279. catch (Exception ex)
  280. {
  281. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  282. }
  283. }
  284. private void tsbSave_Click(object sender, EventArgs e)
  285. {
  286. try
  287. {
  288. string strMessage = CheckForm();
  289. if (strMessage == "")
  290. {
  291. this.SaveEven();
  292. //GetMemberData(strPKey);
  293. }
  294. else
  295. {
  296. MessageBox.Show(strMessage,"提示");
  297. }
  298. }
  299. catch (Exception ex)
  300. {
  301. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  302. }
  303. }
  304. private void tsbDelete_Click(object sender, EventArgs e)
  305. {
  306. try
  307. {
  308. if (MessageBox.Show("請問您確定要刪除本資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  309. {
  310. this.DelEven();
  311. }
  312. }
  313. catch (Exception ex)
  314. {
  315. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  316. }
  317. }
  318. private void tsbSearch_Click(object sender, EventArgs e)
  319. {
  320. try
  321. {
  322. //設定Toolbar狀態
  323. CleanToolbar();
  324. //開放查詢條件
  325. UnLockForm();
  326. //設定畫面物件狀態
  327. CleanForm();
  328. StatusChange("Search");
  329. }
  330. catch (Exception ex)
  331. {
  332. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  333. }
  334. }
  335. private void tsbOK_Click(object sender, EventArgs e)
  336. {
  337. try
  338. {
  339. GoEvent();
  340. //還原Toolbar狀態
  341. tsbSearch.Enabled = true;
  342. tsbOK.Visible = false;
  343. //關閉查詢條件
  344. LockForm();
  345. StatusChange("Search");
  346. }
  347. catch (Exception ex)
  348. {
  349. ErrorHandler.WriteErrorLog("HRDataCorrect.cs", ex);
  350. }
  351. }
  352. private void tsbCancel_Click(object sender, EventArgs e)
  353. {
  354. CleanForm();
  355. CleanToolbar();
  356. LockForm();
  357. StatusChange("None");
  358. }
  359. private void HRDataCorrect_Load(object sender, EventArgs e)
  360. {
  361. SetupStatus(); //設定畫面狀態
  362. }
  363. private void tsbExit_Click(object sender, EventArgs e)
  364. {
  365. this.Close();
  366. }
  367. private void tsbClean_Click(object sender, EventArgs e)
  368. {
  369. CleanForm();
  370. }
  371. #endregion
  372. private void dgvHRItem_CellClick(object sender, DataGridViewCellEventArgs e)
  373. {
  374. txtEventID.Text = "";
  375. GetCurrentData();
  376. }
  377. private void GetCurrentData()
  378. {
  379. StringBuilder strSQL = new StringBuilder();
  380. switch (tbPage.SelectedTab.Name.ToString())
  381. {
  382. case "tbPage1":
  383. //更新工作資料及狀態
  384. 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 + "' ");
  385. strSQL.Append(" And StartDate Between '" + dpStart.Text.ToString() + " 00:00:00' And '" + dpEnd.Text.ToString() + " 23:59:59'");
  386. strSQL.Append(" Order by StartDate");
  387. //進行查詢
  388. dgvDetail.DataSource = UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"];
  389. Application.DoEvents();
  390. break;
  391. case "tbPage2":
  392. //更正加班資料
  393. 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("-", "") + "'");
  394. strSQL.Append(" And MemberID = '" + (string)dgvHRItem.CurrentRow.Cells["cMemberID"].Value + "' And SignOutTime is not null And Convert(varchar(50), SignOutTime,108) >= '19:00:00'");
  395. 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))"); //未填寫加班匯報
  396. dgvDetail1.DataSource = UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"];
  397. Application.DoEvents();
  398. break;
  399. }
  400. }
  401. private void btnUpdate_Click(object sender, EventArgs e)
  402. {
  403. SaveEven();
  404. int intNowRow = dgvDetail.Rows.IndexOf(dgvDetail.CurrentRow);
  405. CleanForm();
  406. GetCurrentData();
  407. dgvDetail.Rows[intNowRow].Selected = true;
  408. }
  409. private void btnUpdate1_Click(object sender, EventArgs e)
  410. {
  411. int intSelectedCount = 0;
  412. foreach (DataGridViewRow row in dgvDetail1.Rows)
  413. {
  414. if (row.Cells["cSelected"].Value != null)
  415. {
  416. if ((bool)row.Cells["cSelected"].Value == true)
  417. {
  418. intSelectedCount += 1;
  419. }
  420. }
  421. }
  422. if (intSelectedCount > 0)
  423. {
  424. SaveEven();
  425. CleanForm();
  426. GetCurrentData();
  427. }
  428. }
  429. private void dgvDetail_CellClick(object sender, DataGridViewCellEventArgs e)
  430. {
  431. txtEventID.Text = (string)dgvDetail.CurrentRow.Cells["cEventID"].Value;
  432. dtPickEnd.Value = (DateTime)dgvDetail.CurrentRow.Cells["cEndDate"].Value;
  433. txtProjectNumber.Text = (string)dgvDetail.CurrentRow.Cells["cProjectNumber"].Value;
  434. txtSubject.Text = (string)dgvDetail.CurrentRow.Cells["cSubject"].Value;
  435. txtDescription.Text = (string)dgvDetail.CurrentRow.Cells["cDescription"].Value;
  436. }
  437. private void btnPickProject_Click(object sender, EventArgs e)
  438. {
  439. PickProject pkItemForm = new PickProject();
  440. pkItemForm.txtProjectNumber.Text = txtProjectNumber.Text.Trim();
  441. pkItemForm.strOwnerForm = "HRDataCorrect";
  442. pkItemForm.Owner = this;
  443. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  444. pkItemForm.ShowDialog();
  445. }
  446. public void ReturnProjectInfo(string[] strProjectInfo) //回傳專案資料
  447. {
  448. txtProjectNumber.Text = strProjectInfo[0].ToString();
  449. }
  450. }
  451. }