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.

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