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.

505 lines
20 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. using ManagementSystem.Utility;
  12. namespace ManagementSystem
  13. {
  14. public partial class InsuranceMaintain : Form
  15. {
  16. //程式內共用物件
  17. string strFrmStatus = ""; //表單狀態
  18. string strActiveUserID = ""; //取得登入作用的使用者帳號
  19. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  20. SqlCommand sqlCmd = new SqlCommand();
  21. DataSet sdInsurance = new System.Data.DataSet();
  22. string strPKey = ""; //程式內的Key值
  23. string strKey = ""; //程式內加密的Key值
  24. int intStartCount = 0;
  25. public InsuranceMaintain()
  26. {
  27. InitializeComponent();
  28. }
  29. #region 自定義程式
  30. private void SetupStatus() //畫面載入設定
  31. {
  32. try
  33. {
  34. strActiveUserID = MainForm.strActiveUserID;
  35. strKey = MainForm.strKey;
  36. if (MainForm.strKey == "")
  37. {
  38. //設定Toolbar初始狀態
  39. tsbAdd.Enabled = false;
  40. tsbSave.Enabled = false;
  41. tsbDelete.Enabled = false;
  42. }
  43. //設定畫面初始狀態
  44. StatusChange("NONE");
  45. }
  46. catch (Exception ex)
  47. {
  48. ErrorHandler.WriteErrorLog("InsuranceMaintain.cs", ex);
  49. }
  50. }
  51. private void CleanForm() //清除畫面
  52. {
  53. //設定畫面物件狀態
  54. //清除GridView
  55. string strSQL = "Select GUIDCode,InsuranceLevel,InsuranceAmountStart,InsuranceAmountEnd,C_LaborProtection,S_LaborProtection,C_HealthInsuranceAmount,S_HealthInsuranceAmount,RetirementAmount From OTB_HR_Insurance Order By InsuranceLevel";
  56. sdInsurance =UtilityClass.GetSQLResult(strSQL);
  57. dgvDataMaintain.DataSource = sdInsurance.Tables["Result"];
  58. Application.DoEvents();
  59. }
  60. private void CleanToolbar() //清除工具列
  61. {
  62. //設定Toolbar狀態
  63. tsbAdd.Visible = true;
  64. tsbAdd.Enabled = true;
  65. tsbEdit.Visible = true;
  66. tsbEdit.Enabled = true;
  67. tsbDelete.Visible = true;
  68. tsbDelete.Enabled = true;
  69. tsbSave.Visible = false;
  70. tsbOK.Visible = false;
  71. tsbCancel.Visible = false;
  72. }
  73. private void UnLockForm() //解除限制唯讀物件
  74. {
  75. dgvDataMaintain.Columns["cGUIDCODE"].ReadOnly = false;
  76. dgvDataMaintain.Columns["cInsuranceLevel"].ReadOnly = false;
  77. dgvDataMaintain.Columns["cInsuranceAmountStart"].ReadOnly = false;
  78. dgvDataMaintain.Columns["cInsuranceAmountEnd"].ReadOnly = false;
  79. dgvDataMaintain.Columns["cC_LaborProtection"].ReadOnly = false;
  80. dgvDataMaintain.Columns["cS_LaborProtection"].ReadOnly = false;
  81. dgvDataMaintain.Columns["cC_HealthInsuranceAmount"].ReadOnly = false;
  82. dgvDataMaintain.Columns["cS_HealthInsuranceAmount"].ReadOnly = false;
  83. dgvDataMaintain.Columns["cRetirementAmount"].ReadOnly = false;
  84. }
  85. private void LockForm() //限制唯讀物件
  86. {
  87. dgvDataMaintain.AllowUserToAddRows = false;
  88. dgvDataMaintain.Columns["cDelColumn"].ReadOnly = true;
  89. dgvDataMaintain.Columns["cGUIDCODE"].ReadOnly = true;
  90. dgvDataMaintain.Columns["cInsuranceLevel"].ReadOnly = true;
  91. dgvDataMaintain.Columns["cInsuranceAmountStart"].ReadOnly = true;
  92. dgvDataMaintain.Columns["cInsuranceAmountEnd"].ReadOnly = true;
  93. dgvDataMaintain.Columns["cC_LaborProtection"].ReadOnly = true;
  94. dgvDataMaintain.Columns["cS_LaborProtection"].ReadOnly = true;
  95. dgvDataMaintain.Columns["cC_HealthInsuranceAmount"].ReadOnly = true;
  96. dgvDataMaintain.Columns["cS_HealthInsuranceAmount"].ReadOnly = true;
  97. dgvDataMaintain.Columns["cRetirementAmount"].ReadOnly = true;
  98. }
  99. private void StatusChange(string strStatus) //變更主畫面狀態
  100. {
  101. switch (strStatus.ToUpper())
  102. {
  103. case "NONE":
  104. CleanForm();
  105. CleanToolbar();
  106. LockForm();
  107. dgvDataMaintain.AllowUserToAddRows = false;
  108. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  109. strFrmStatus = "";
  110. intStartCount = dgvDataMaintain.Rows.Count;
  111. break;
  112. case "SEARCH":
  113. UnLockForm();
  114. dgvDataMaintain.DataSource = null;
  115. Application.DoEvents();
  116. tsbSearch.Visible = false;
  117. tsbOK.Visible = true;
  118. tsbCancel.Visible = true;
  119. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  120. strFrmStatus = "SEARCH";
  121. break;
  122. case "ADD":
  123. UnLockForm();
  124. dgvDataMaintain.AllowUserToAddRows = true;
  125. tsbAdd.Visible = false;
  126. tsbEdit.Enabled = false;
  127. tsbDelete.Enabled = false;
  128. tsbSave.Visible = true;
  129. tsbCancel.Visible = true;
  130. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  131. strFrmStatus = "ADD";
  132. break;
  133. case "MODIFY":
  134. UnLockForm();
  135. tsbEdit.Visible = false;
  136. tsbAdd.Enabled = false;
  137. tsbDelete.Enabled = false;
  138. tsbSave.Visible = true;
  139. tsbCancel.Visible = true;
  140. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  141. strFrmStatus = "MODIFY";
  142. break;
  143. case "DEL":
  144. dgvDataMaintain.Columns["cDelColumn"].ReadOnly = false;
  145. tsbAdd.Enabled = false;
  146. tsbEdit.Enabled = false;
  147. tsbOK.Visible = true;
  148. tsbCancel.Visible = true;
  149. tsbDelete.Visible = false;
  150. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  151. strFrmStatus = "DEL";
  152. break;
  153. }
  154. UtilityClass.SetGridColor(dgvDataMaintain);
  155. }
  156. private bool CheckDel() //確認是否有資料被勾選刪除
  157. {
  158. foreach (DataGridViewRow row in dgvDataMaintain.Rows)
  159. {
  160. DataGridViewCheckBoxCell Delcell = (DataGridViewCheckBoxCell)row.Cells["cDelColumn"]; //刪除狀態列
  161. DataGridViewTextBoxCell Keycell = (DataGridViewTextBoxCell)row.Cells["cGUIDCode"]; //需刪除的Key值
  162. if (Delcell.Value != null)
  163. {
  164. if (Delcell.Value == Delcell.TrueValue)
  165. {
  166. return true;
  167. }
  168. }
  169. }
  170. return false;
  171. }
  172. private void DelEven() //刪除事件
  173. {
  174. try
  175. {
  176. //宣告物件
  177. string strDelItem = "";
  178. if (CheckDel())
  179. {
  180. if (MessageBox.Show("請問您確定要刪除本資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  181. {
  182. foreach (DataGridViewRow row in dgvDataMaintain.Rows)
  183. {
  184. DataGridViewCheckBoxCell Delcell = (DataGridViewCheckBoxCell)row.Cells["cDelColumn"]; //刪除狀態列
  185. DataGridViewTextBoxCell Keycell = (DataGridViewTextBoxCell)row.Cells["cGUIDCode"]; //需刪除的Key值
  186. if (Delcell.Value != null)
  187. {
  188. if (Delcell.Value == Delcell.TrueValue)
  189. {
  190. if (strDelItem == "")
  191. { strDelItem = "'" + Keycell.Value.ToString() + "'"; }
  192. else
  193. { strDelItem += ",'" + Keycell.Value.ToString() + "'"; }
  194. }
  195. }
  196. }
  197. string strDelSQL = string.Format("Delete OTB_HR_Insurance Where GUIDCode in ({0})", strDelItem);
  198. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  199. {
  200. //添加參數
  201. sqlAdapter.DeleteCommand = new SqlCommand();
  202. sqlAdapter.DeleteCommand.Connection = sqlConn;
  203. sqlAdapter.DeleteCommand.CommandText = strDelSQL.ToString();
  204. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  205. {
  206. sqlConn.Open();
  207. }
  208. sqlAdapter.DeleteCommand.ExecuteNonQuery();
  209. MessageBox.Show("資料刪除成功", "提示");
  210. }
  211. }
  212. }
  213. else
  214. {
  215. //未選取要刪除的資料
  216. MessageBox.Show("請先選取欲刪除的資料", "提示");
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. ErrorHandler.WriteErrorLog("InsuranceMaintain.cs", ex);
  222. }
  223. }
  224. private void AddEven() //新增事件
  225. {
  226. try
  227. {
  228. //宣告物件
  229. int intDataCount = dgvDataMaintain.Rows.Count - 1;
  230. StringBuilder strSQL = new StringBuilder();
  231. for (int intCount = intStartCount; intCount < intDataCount; intCount++)
  232. {
  233. if (!dgvDataMaintain.Rows[intCount].IsNewRow)
  234. {
  235. strSQL.Append("Insert Into OTB_HR_Insurance(GUIDCode,InsuranceLevel,InsuranceAmountStart,InsuranceAmountEnd,C_LaborProtection,S_LaborProtection,C_HealthInsuranceAmount,S_HealthInsuranceAmount,RetirementAmount,CreateDate,CreateUser,ModifyDate,ModifyUser)");
  236. strSQL.Append("Values (NEWID(),");
  237. strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["cInsuranceLevel"].Value.ToString() + ",");
  238. strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["cInsuranceAmountStart"].Value.ToString() + ",");
  239. strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["cInsuranceAmountEnd"].Value.ToString() + ",");
  240. strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["cC_LaborProtection"].Value.ToString() + ",");
  241. strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["cS_LaborProtection"].Value.ToString() + ",");
  242. strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["cC_HealthInsuranceAmount"].Value.ToString() + ",");
  243. strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["cS_HealthInsuranceAmount"].Value.ToString() + ",");
  244. strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["cRetirementAmount"].Value.ToString() + ",");
  245. strSQL.Append("Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "') ");
  246. }
  247. }
  248. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  249. {
  250. //添加參數
  251. sqlAdapter.InsertCommand = new SqlCommand();
  252. sqlAdapter.InsertCommand.Connection = sqlConn;
  253. sqlAdapter.InsertCommand.CommandText = strSQL.ToString();
  254. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  255. {
  256. sqlConn.Open();
  257. }
  258. sqlAdapter.InsertCommand.ExecuteNonQuery();
  259. MessageBox.Show("資料新增成功", "提示");
  260. StatusChange("NONE");
  261. }
  262. }
  263. catch (Exception ex)
  264. {
  265. MessageBox.Show("資料新增失敗", "提示");
  266. ErrorHandler.WriteErrorLog("InsuranceMaintain.cs", ex);
  267. }
  268. }
  269. private void EditEven() //修改事件
  270. {
  271. try
  272. {
  273. //宣告物件
  274. StringBuilder sbSQL = new StringBuilder();
  275. foreach (DataGridViewRow row in dgvDataMaintain.Rows)
  276. {
  277. DataGridViewCheckBoxCell Updcell = (DataGridViewCheckBoxCell)row.Cells["cUPDColumn"]; //修改狀態列
  278. DataGridViewTextBoxCell Keycell = (DataGridViewTextBoxCell)row.Cells["cGUIDCode"]; //需修改的Key值
  279. if (Updcell.Value != null)
  280. {
  281. if (Updcell.Value == Updcell.TrueValue)
  282. {
  283. sbSQL.Append(" Update OTB_HR_Insurance Set ");
  284. sbSQL.Append(" InsuranceLevel = " + row.Cells["cInsuranceLevel"].Value.ToString());
  285. sbSQL.Append(" ,InsuranceAmountStart = " + row.Cells["cInsuranceAmountStart"].Value.ToString());
  286. sbSQL.Append(" ,InsuranceAmountEnd = " + row.Cells["cInsuranceAmountEnd"].Value.ToString());
  287. sbSQL.Append(" ,C_LaborProtection = " + row.Cells["cC_LaborProtection"].Value.ToString());
  288. sbSQL.Append(" ,S_LaborProtection = " + row.Cells["cS_LaborProtection"].Value.ToString());
  289. sbSQL.Append(" ,C_HealthInsuranceAmount = " + row.Cells["cC_HealthInsuranceAmount"].Value.ToString());
  290. sbSQL.Append(" ,S_HealthInsuranceAmount = " + row.Cells["cS_HealthInsuranceAmount"].Value.ToString());
  291. sbSQL.Append(" ,RetirementAmount = " + row.Cells["cRetirementAmount"].Value.ToString());
  292. sbSQL.Append(" ,ModifyDate = Getdate(), ModifyUser ='" + strActiveUserID + "'");
  293. sbSQL.Append(" Where GUIDCode ='" + Keycell.Value.ToString() + "'");
  294. }
  295. }
  296. }
  297. if (sbSQL.ToString().Trim() != "")
  298. {
  299. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  300. {
  301. //添加參數
  302. sqlAdapter.UpdateCommand = new SqlCommand();
  303. sqlAdapter.UpdateCommand.Connection = sqlConn;
  304. sqlAdapter.UpdateCommand.CommandText = sbSQL.ToString();
  305. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  306. {
  307. sqlConn.Open();
  308. }
  309. sqlAdapter.UpdateCommand.ExecuteNonQuery();
  310. StatusChange("NONE");
  311. }
  312. MessageBox.Show("資料修改成功", "提示");
  313. }
  314. }
  315. catch (Exception ex)
  316. {
  317. MessageBox.Show("資料修改失敗", "提示");
  318. ErrorHandler.WriteErrorLog("InsuranceMaintain.cs", ex);
  319. }
  320. }
  321. private void ShowUpdateDate(string strGUID) // 呈現最新修改人
  322. {
  323. //排除空值
  324. if (strGUID.Trim() == "")
  325. {
  326. return;
  327. }
  328. if (strGUID == null)
  329. {
  330. return;
  331. }
  332. strPKey = strGUID;
  333. string strSQL = "Select ModifyDate, ModifyUser From OTB_HR_Insurance Where GUIDCode = '" + strPKey + "'";
  334. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  335. {
  336. //添加參數
  337. sqlAdapter.SelectCommand = new SqlCommand();
  338. sqlAdapter.SelectCommand.Connection = sqlConn;
  339. sqlAdapter.SelectCommand.CommandText = strSQL;
  340. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  341. {
  342. sqlConn.Open();
  343. }
  344. //進行查詢
  345. DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
  346. ((MainForm)ParentForm).SsStatus.Items["tsslModifyUser"].Text = dtTemp.Rows[0]["ModifyUser"].ToString().Trim();
  347. ((MainForm)ParentForm).SsStatus.Items["tsslModifyDate"].Text = dtTemp.Rows[0]["ModifyDate"].ToString().Trim();
  348. }
  349. UtilityClass.SetGridColor(dgvDataMaintain);
  350. }
  351. #endregion
  352. #region 事件觸發及問題處理
  353. private void InsuranceMaintain_Load(object sender, EventArgs e)
  354. {
  355. SetupStatus();
  356. }
  357. private void tsbEdit_Click(object sender, EventArgs e)
  358. {
  359. StatusChange("NONE");
  360. StatusChange("MODIFY");
  361. }
  362. private void tsbCancel_Click(object sender, EventArgs e)
  363. {
  364. StatusChange("NONE");
  365. }
  366. private void tsbAdd_Click(object sender, EventArgs e)
  367. {
  368. StatusChange("NONE");
  369. StatusChange("ADD");
  370. }
  371. private void tsbDelete_Click(object sender, EventArgs e)
  372. {
  373. StatusChange("DEL");
  374. }
  375. private void tsbOK_Click(object sender, EventArgs e)
  376. {
  377. try
  378. {
  379. StringBuilder strSQL = new StringBuilder("");
  380. switch (strFrmStatus.ToString())
  381. {
  382. case "DEL": //刪除
  383. DelEven();
  384. StatusChange("NONE");
  385. break;
  386. }
  387. LockForm();
  388. }
  389. catch (Exception ex)
  390. {
  391. ErrorHandler.WriteErrorLog("InsuranceMaintain.cs", ex);
  392. }
  393. }
  394. private void tsbSearch_Click(object sender, EventArgs e)
  395. {
  396. StatusChange("SEARCH");
  397. }
  398. private void tsbSave_Click(object sender, EventArgs e)
  399. {
  400. try
  401. {
  402. StringBuilder strSQL = new StringBuilder("");
  403. switch (strFrmStatus.ToString())
  404. {
  405. case "ADD": //新增
  406. AddEven();
  407. break;
  408. case "MODIFY": //修改
  409. EditEven();
  410. break;
  411. }
  412. LockForm();
  413. }
  414. catch (Exception ex)
  415. {
  416. ErrorHandler.WriteErrorLog("InsuranceMaintain.cs", ex);
  417. }
  418. }
  419. private void dgvDataMaintain_CellClick(object sender, DataGridViewCellEventArgs e)
  420. {
  421. ShowUpdateDate(dgvDataMaintain.CurrentRow.Cells["cGUIDCode"].Value.ToString()); //顯示最新修改資料
  422. }
  423. private void dgvDataMaintain_CellEnter(object sender, DataGridViewCellEventArgs e)
  424. {
  425. ShowUpdateDate(dgvDataMaintain.CurrentRow.Cells["cGUIDCode"].Value.ToString()); //顯示最新修改資料
  426. }
  427. private void tsbExit_Click(object sender, EventArgs e)
  428. {
  429. this.Close();
  430. }
  431. private void dgvDataMaintain_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  432. {
  433. if (dgvDataMaintain.CurrentRow != null)
  434. {
  435. DataGridViewCheckBoxCell UPDcell = (DataGridViewCheckBoxCell)dgvDataMaintain.CurrentRow.Cells["cUPDColumn"]; //被修改的Key值
  436. UPDcell.Value = UPDcell.TrueValue;
  437. }
  438. }
  439. private void tsbClean_Click(object sender, EventArgs e)
  440. {
  441. CleanForm();
  442. }
  443. #endregion
  444. }
  445. }