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.

416 lines
15 KiB

2 years ago
  1. using ManagementSystem.Utility;
  2. using System;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Drawing;
  8. namespace ManagementSystem
  9. {
  10. public partial class AccountingBookMaintain : Form
  11. {
  12. //程式內共用物件
  13. public string strFrmStatus = ""; //表單狀態
  14. public string strPKey = ""; //程式內的Key值
  15. string strKey = ""; //程式內加密的Key值
  16. string strActiveUserID = ""; //取得登入作用的使用者帳號
  17. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  18. SqlTransaction sqlTran;
  19. SqlDataAdapter sqlAdapter = new SqlDataAdapter();
  20. SqlCommand sqlCmd = new SqlCommand();
  21. DataSet sdInsurance = new System.Data.DataSet();
  22. public AccountingBookMaintain()
  23. {
  24. InitializeComponent();
  25. }
  26. #region 自定義程式
  27. private void StatusChange(string strStatus) //變更主畫面狀態
  28. {
  29. strActiveUserID = MainForm.strActiveUserID;
  30. strKey = MainForm.strKey;
  31. switch (strStatus.ToUpper())
  32. {
  33. case "NONE":
  34. strFrmStatus = "";
  35. CleanToolbar();
  36. LockForm();
  37. CleanForm();
  38. dgvAccountingBook.ReadOnly = false;
  39. break;
  40. case "SEARCH":
  41. CleanToolbar();
  42. UnLockForm();
  43. tsbOK.Visible = true;
  44. tsbCancel.Visible = true;
  45. tsbSearch.Visible = false;
  46. tsbAdd.Enabled = false;
  47. tsbEdit.Enabled = false;
  48. tsbDelete.Enabled = false;
  49. txtAccountingBookID.Focus();
  50. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  51. break;
  52. case "ADD":
  53. CleanToolbar();
  54. CleanForm();
  55. UnLockForm();
  56. tsbSave.Visible = true;
  57. tsbCancel.Visible = true;
  58. tsbAdd.Visible = false;
  59. tsbSearch.Enabled = false;
  60. tsbEdit.Enabled = false;
  61. tsbDelete.Enabled = false;
  62. txtAccountingBookID.Focus();
  63. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  64. strFrmStatus = "ADD";
  65. break;
  66. case "MODIFY":
  67. CleanToolbar();
  68. UnLockForm();
  69. txtAccountingBookID.ReadOnly = true;
  70. tsbSave.Visible = true;
  71. tsbCancel.Visible = true;
  72. tsbSearch.Enabled = false;
  73. tsbAdd.Enabled = false;
  74. tsbEdit.Visible = false;
  75. tsbDelete.Enabled = false;
  76. txtAccountingBookID.Focus();
  77. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  78. strFrmStatus = "MODIFY";
  79. break;
  80. case "DEL":
  81. try
  82. {
  83. if (MessageBox.Show("請問您確定要刪除本資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  84. {
  85. this.DelEven();
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex);
  91. }
  92. strFrmStatus = "DEL";
  93. break;
  94. }
  95. }
  96. private void CleanToolbar() //清除工具列
  97. {
  98. //設定Toolbar狀態
  99. tsbSearch.Enabled = true;
  100. tsbSearch.Visible = true;
  101. tsbAdd.Enabled = true;
  102. tsbAdd.Visible = true;
  103. tsbEdit.Enabled = false;
  104. tsbEdit.Visible = false;
  105. tsbDelete.Enabled = true;
  106. tsbDelete.Visible = true;
  107. tsbSave.Visible = false;
  108. tsbOK.Visible = false;
  109. tsbCancel.Visible = false;
  110. }
  111. private void GetAccountingBookInfoByID(string strAccountingBookID)
  112. {
  113. try
  114. {
  115. //排除空值
  116. if (strAccountingBookID.Trim() == "")
  117. {
  118. return;
  119. }
  120. if (strAccountingBookID == null)
  121. {
  122. return;
  123. }
  124. strPKey = strAccountingBookID;
  125. StringBuilder strSQL = new StringBuilder("");
  126. strSQL.Append("Select * ");
  127. strSQL.Append("From OTB_FNC_AccountingBookInfo ");
  128. strSQL.Append("Where AccountingBookID = '" + strPKey + "'");
  129. //進行查詢
  130. DataTable dtTemp = (DataTable)UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"];
  131. //進行資料顯示
  132. txtAccountingBookID.Text = dtTemp.Rows[0]["AccountingBookID"].ToString();
  133. txtAccountingBookName.Text = dtTemp.Rows[0]["AccountingBookName"].ToString();
  134. cbBelongOrg.SelectedValue = dtTemp.Rows[0]["BelongOrg"].ToString();
  135. cbEffective.Checked = (dtTemp.Rows[0]["Effective"].ToString() == "Y");
  136. ((MainForm)ParentForm).SsStatus.Items["tsslModifyUser"].Text = dtTemp.Rows[0]["ModifyUser"].ToString().Trim();
  137. ((MainForm)ParentForm).SsStatus.Items["tsslModifyDate"].Text = dtTemp.Rows[0]["ModifyDate"].ToString().Trim();
  138. }
  139. catch (Exception ex)
  140. {
  141. ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex);
  142. }
  143. }
  144. private void CleanForm() //清除畫面
  145. {
  146. string strSQL = "Select AccountingBookID, AccountingBookName From OTB_FNC_AccountingBookInfo";
  147. dgvAccountingBook.DataSource = UtilityClass.GetSQLResult(strSQL).Tables["Result"]; ;
  148. cbBelongOrg.DataSource = UtilityClass.GetOrgList().Tables["Organizations"]; //取得清單列表
  149. cbBelongOrg.ValueMember = "OrganizationID";
  150. cbBelongOrg.DisplayMember = "OrganizationName";
  151. txtAccountingBookID.Text = "";
  152. txtAccountingBookName.Text = "";
  153. cbBelongOrg.SelectedIndex = 0;
  154. cbEffective.Checked = true;
  155. }
  156. private bool CheckForm() //確認資料是否填寫正確
  157. {
  158. if (txtAccountingBookID.Text.Trim() == "")
  159. {
  160. MessageBox.Show("請填寫帳本編號", "提示");
  161. txtAccountingBookID.Focus();
  162. return false;
  163. }
  164. if (txtAccountingBookID.Text.Trim() == "")
  165. {
  166. MessageBox.Show("請填寫帳本名稱", "提示");
  167. txtAccountingBookName.Focus();
  168. return false;
  169. }
  170. return true;
  171. }
  172. private void UnLockForm() //解除限制唯讀物件
  173. {
  174. txtAccountingBookID.ReadOnly = false;
  175. txtAccountingBookName.ReadOnly = false;
  176. cbBelongOrg.Enabled = true;
  177. }
  178. private void LockForm() //限制唯讀物件
  179. {
  180. txtAccountingBookID.ReadOnly = true;
  181. txtAccountingBookName.ReadOnly = true;
  182. cbBelongOrg.Enabled = false;
  183. }
  184. private void DelEven() //刪除事件
  185. {
  186. StringBuilder sbSQL = new StringBuilder();
  187. try
  188. {
  189. //執行Delete命令
  190. //執行命令
  191. string strCheckSQL = string.Format("Select AccountingBookID From OTB_FNC_AccountingMaxNumber Where AccountingBookID ='{0}'",strPKey);
  192. if (UtilityClass.IsExist(strCheckSQL))
  193. {
  194. //帳號資料有被使用
  195. MessageBox.Show("該帳本已被開帳使用,無法刪除!", "提示");
  196. }
  197. else
  198. {
  199. sbSQL.Append("Delete OTB_FNC_AccountingBookInfo Where AccountingBookID = @AccountingBookID");
  200. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  201. {
  202. //添加參數
  203. sqlAdapter.DeleteCommand = new SqlCommand();
  204. sqlAdapter.DeleteCommand.Connection = sqlConn;
  205. sqlAdapter.DeleteCommand.CommandText = sbSQL.ToString();
  206. sqlAdapter.DeleteCommand.Parameters.AddRange
  207. (
  208. new SqlParameter[]
  209. {
  210. new SqlParameter("@AccountingBookID",txtAccountingBookID.Text.Trim()),
  211. }
  212. );
  213. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  214. {
  215. sqlConn.Open();
  216. }
  217. sqlAdapter.DeleteCommand.ExecuteNonQuery();
  218. GetAccountingBookInfoByID(txtAccountingBookID.Text.Trim()); //顯示該帳本的值到畫面上
  219. }
  220. StatusChange("None");
  221. LockForm();
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex);
  227. }
  228. }
  229. private void AddEvent() //新增事件
  230. {
  231. try
  232. {
  233. if (CheckForm())
  234. {
  235. //進行資料新增
  236. StringBuilder strSQL = new StringBuilder();
  237. strSQL.Clear();
  238. strSQL.Append("Insert Into OTB_FNC_AccountingBookInfo(AccountingBookID, AccountingBookName, BelongOrg, Effective, CreateDate, CreateUser, ModifyDate, ModifyUser)");
  239. strSQL.Append("Values (@AccountingBookID, @AccountingBookName, @BelongOrg, @Effective, Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "') ");
  240. //添加參數
  241. sqlAdapter.InsertCommand = new SqlCommand();
  242. sqlAdapter.InsertCommand.Connection = sqlConn;
  243. sqlAdapter.InsertCommand.CommandText = strSQL.ToString();
  244. sqlAdapter.InsertCommand.Transaction = sqlTran;
  245. sqlAdapter.InsertCommand.Parameters.AddRange
  246. (
  247. new SqlParameter[]
  248. {
  249. new SqlParameter("@AccountingBookID",txtAccountingBookID.Text.Trim()),
  250. new SqlParameter("@AccountingBookName",txtAccountingBookName.Text.Trim()),
  251. new SqlParameter("@BelongOrg",cbBelongOrg.SelectedValue.ToString()),
  252. new SqlParameter("@Effective",cbEffective.Checked ? "Y" : "N")
  253. }
  254. );
  255. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  256. {
  257. sqlConn.Open();
  258. }
  259. sqlAdapter.InsertCommand.ExecuteNonQuery();
  260. MessageBox.Show("資料新增成功", "提示");
  261. dgvAccountingBook.ReadOnly = false;
  262. }
  263. }
  264. catch (Exception ex)
  265. {
  266. MessageBox.Show("資料新增失敗!", "提示");
  267. ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex);
  268. }
  269. }
  270. private void EditEvent() //修改事件
  271. {
  272. try
  273. {
  274. if (CheckForm())
  275. {
  276. //進行資料維護
  277. MessageBox.Show("資料修改成功", "提示");
  278. dgvAccountingBook.ReadOnly = false;
  279. }
  280. }
  281. catch (Exception ex)
  282. {
  283. MessageBox.Show("資料修改失敗!", "提示");
  284. ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex);
  285. }
  286. }
  287. private void GoEvent() //執行事件
  288. {
  289. switch (strFrmStatus)
  290. {
  291. case "ADD": //新增事件
  292. AddEvent();
  293. StatusChange("NONE");
  294. break;
  295. case "MODIFY": //修改事件
  296. EditEvent();
  297. StatusChange("NONE");
  298. break;
  299. }
  300. }
  301. #endregion
  302. #region 事件觸發及問題處理
  303. private void AccountingBookMaintain_Load(object sender, EventArgs e)
  304. {
  305. StatusChange("NONE");
  306. }
  307. private void tsbClean_Click(object sender, EventArgs e)
  308. {
  309. CleanForm();
  310. }
  311. private void tsbSearch_Click(object sender, EventArgs e)
  312. {
  313. StatusChange("SEARCH");
  314. }
  315. private void tsbAdd_Click(object sender, EventArgs e)
  316. {
  317. dgvAccountingBook.ReadOnly = true;
  318. StatusChange("ADD");
  319. }
  320. private void tsbExit_Click(object sender, EventArgs e)
  321. {
  322. this.Close();
  323. }
  324. private void tsbCancel_Click(object sender, EventArgs e)
  325. {
  326. StatusChange("NONE");
  327. }
  328. private void tsbEdit_Click(object sender, EventArgs e)
  329. {
  330. dgvAccountingBook.ReadOnly = true;
  331. StatusChange("MODIFY");
  332. }
  333. private void dgvAccountingBook_CellEnter(object sender, DataGridViewCellEventArgs e)
  334. {
  335. GetAccountingBookInfoByID(dgvAccountingBook.CurrentRow.Cells[0].Value.ToString()); //顯示該帳本的值到畫面上
  336. }
  337. private void tsbSave_Click(object sender, EventArgs e)
  338. {
  339. GoEvent(); //執行儲存動作
  340. }
  341. private void tsbDelete_Click(object sender, EventArgs e)
  342. {
  343. try
  344. {
  345. if (MessageBox.Show("請問您確定要刪除本帳本?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  346. {
  347. this.DelEven();
  348. }
  349. }
  350. catch (Exception ex)
  351. {
  352. ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex);
  353. }
  354. }
  355. #endregion
  356. }
  357. }