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.

623 lines
24 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.Sql;
  11. using System.Data.SqlClient;
  12. using ManagementSystem.Utility;
  13. namespace ManagementSystem
  14. {
  15. public partial class AccountingSubjectMaintain : Form
  16. {
  17. //程式內共用物件
  18. string strFrmStatus = ""; //表單狀態
  19. string strActiveUserID = ""; //取得登入作用的使用者帳號
  20. string strAccountingBookID = ""; //取得作用中的帳本
  21. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  22. SqlCommand sqlCmd = new SqlCommand();
  23. string strPKey = ""; //程式內的Key值
  24. public AccountingSubjectMaintain()
  25. {
  26. InitializeComponent();
  27. }
  28. #region 自定義程式
  29. private void SetupStatus() //畫面載入設定
  30. {
  31. try
  32. {
  33. strActiveUserID = MainForm.strActiveUserID;
  34. strAccountingBookID = MainForm.strAccountingBookID;
  35. if (MainForm.strKey == "")
  36. {
  37. //設定Toolbar初始狀態
  38. tsbAdd.Enabled = false;
  39. tsbSave.Enabled = false;
  40. tsbDelete.Enabled = false;
  41. }
  42. //設定畫面初始狀態
  43. CleanForm();
  44. CleanToolbar();
  45. LockForm();
  46. }
  47. catch (Exception ex)
  48. {
  49. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  50. }
  51. }
  52. private string CheckForm() //確認畫面必填欄位
  53. {
  54. string strResult = "";
  55. if (txtAccountingSubID.Text.Trim() == "")
  56. {
  57. strResult = "請輸入科目編號";
  58. txtAccountingSubID.Focus();
  59. }
  60. if (txtAccountingSubName.Text.Trim() == "")
  61. {
  62. strResult = "請輸入會計科目";
  63. txtAccountingSubName.Focus();
  64. }
  65. if (cbAccountingClass.SelectedValue.ToString().Trim() == "")
  66. {
  67. strResult = "請選擇會計類別";
  68. cbAccountingClass.Focus();
  69. }
  70. if (cbDCClass.SelectedValue.ToString().Trim() == "")
  71. {
  72. strResult = "請選擇借/貸方類別";
  73. cbDCClass.Focus();
  74. }
  75. if (strFrmStatus.ToUpper() == "ADD")
  76. {
  77. string strCHKSQL = "Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingSubID ='" + txtAccountingSubID.Text.Trim() + "'";
  78. if (UtilityClass.IsExist(strCHKSQL))
  79. {
  80. strResult = "存在重覆科目資料";
  81. txtAccountingSubID.Focus();
  82. }
  83. }
  84. return strResult;
  85. }
  86. private void CleanForm() //清除畫面
  87. {
  88. //設定科目類別
  89. cbAccountingClass.DataSource = UtilityClass.GetSystemArgument("ACCClass", "zh-TW").Tables["Arguments"];
  90. cbAccountingClass.DisplayMember = "ArgumentValue";
  91. cbAccountingClass.ValueMember = "ArgumentID";
  92. //設定借/貸方類別
  93. cbDCClass.DataSource = UtilityClass.GetSystemArgument("DCClass", "zh-TW").Tables["Arguments"];
  94. cbDCClass.DisplayMember = "ArgumentValue";
  95. cbDCClass.ValueMember = "ArgumentID";
  96. //清除GridView
  97. string strSQL = "Select AccountingSubID, AccountingSubName From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' Order by AccountingSubID";
  98. dgvAccountingSubjects.DataSource = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
  99. //清除畫面資料
  100. txtAccountingSubID.Text = "";
  101. txtAccountingSubName.Text = "";
  102. cbAccountingClass.SelectedIndex = -1;
  103. cbDCClass.SelectedIndex = -1;
  104. cbToCarry.Checked = false;
  105. Application.DoEvents();
  106. }
  107. private void LockForm() //限制唯讀物件
  108. {
  109. txtAccountingSubID.ReadOnly = true;
  110. txtAccountingSubName.ReadOnly = true;
  111. cbDCClass.Enabled = false;
  112. cbAccountingClass.Enabled = false;
  113. cbToCarry.Enabled = false;
  114. }
  115. private void UnLockForm() //解除限制唯讀物件
  116. {
  117. txtAccountingSubID.ReadOnly = false;
  118. txtAccountingSubName.ReadOnly = false;
  119. cbDCClass.Enabled = true;
  120. cbAccountingClass.Enabled = true;
  121. cbToCarry.Enabled = true;
  122. }
  123. private void CleanToolbar() //清除工具列
  124. {
  125. //設定Toolbar狀態
  126. tsbSearch.Enabled = true;
  127. tsbSearch.Visible = true;
  128. tsbAdd.Enabled = true;
  129. tsbAdd.Visible = true;
  130. tsbEdit.Enabled = true;
  131. tsbEdit.Visible = true;
  132. tsbDelete.Enabled = true;
  133. tsbDelete.Visible = true;
  134. tsbSave.Visible = false;
  135. tsbOK.Visible = false;
  136. tsbCancel.Visible = false;
  137. }
  138. private void StatusChange(string strStatus) //變更主畫面狀態
  139. {
  140. switch (strStatus.ToUpper())
  141. {
  142. case "NONE":
  143. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  144. strFrmStatus = "";
  145. CleanForm();
  146. CleanToolbar();
  147. dgvAccountingSubjects.ReadOnly = false;
  148. break;
  149. case "SEARCH":
  150. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  151. strFrmStatus = "SEARCH";
  152. break;
  153. case "ADD":
  154. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  155. strFrmStatus = "ADD";
  156. break;
  157. case "MODIFY":
  158. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  159. strFrmStatus = "MODIFY";
  160. break;
  161. case "DEL":
  162. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  163. strFrmStatus = "DEL";
  164. break;
  165. }
  166. }
  167. private void GetAccountData(string strAccountingSubID) //取得會計科目資料
  168. {
  169. //排除空值
  170. if (strAccountingSubID.Trim() == "")
  171. {
  172. return;
  173. }
  174. if (strAccountingSubID == null)
  175. {
  176. return;
  177. }
  178. strPKey = strAccountingSubID;
  179. StringBuilder strSQL = new StringBuilder("");
  180. strSQL.Append("Select * ");
  181. strSQL.Append("From OTB_FNC_AccountingSubjects ");
  182. strSQL.Append("Where AccountingBookID = '" + strAccountingBookID + "' And AccountingSubID = '" + strPKey + "'");
  183. //進行查詢
  184. DataTable dtTemp = (DataTable)UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"];
  185. //進行資料顯示
  186. txtAccountingSubID.Text = dtTemp.Rows[0]["AccountingSubID"].ToString();
  187. txtAccountingSubName.Text = dtTemp.Rows[0]["AccountingSubName"].ToString();
  188. txtMemo.Text = dtTemp.Rows[0]["Memo"].ToString();
  189. cbAccountingClass.SelectedValue = dtTemp.Rows[0]["AccountingClass"].ToString();
  190. cbDCClass.SelectedValue = dtTemp.Rows[0]["DCClass"].ToString();
  191. cbToCarry.Checked = (dtTemp.Rows[0]["ToCarry"].ToString() == "Y");
  192. ((MainForm)ParentForm).SsStatus.Items["tsslModifyUser"].Text = dtTemp.Rows[0]["ModifyUser"].ToString().Trim();
  193. ((MainForm)ParentForm).SsStatus.Items["tsslModifyDate"].Text = dtTemp.Rows[0]["ModifyDate"].ToString().Trim();
  194. }
  195. private void SaveEven() //儲存事件
  196. {
  197. try
  198. {
  199. switch (strFrmStatus.ToString())
  200. {
  201. case "ADD": //新增
  202. StringBuilder sbADDSQL = new StringBuilder();
  203. sbADDSQL.Append("Insert Into OTB_FNC_AccountingSubjects(AccountingBookID,AccountingSubID,AccountingClass,DCClass,AccountingSubName,ToCarry,CreateDate,CreateUser,ModifyDate,ModifyUser,Memo) ");
  204. sbADDSQL.Append("Values(@AccountingBookID,@AccountingSubID,@AccountingClass,@DCClass,@AccountingSubName,@ToCarry,Getdate(),@CreateUser,Getdate(),@ModifyUser,@Memo)");
  205. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  206. {
  207. //添加參數
  208. sqlAdapter.InsertCommand = new SqlCommand();
  209. sqlAdapter.InsertCommand.Connection = sqlConn;
  210. sqlAdapter.InsertCommand.CommandText = sbADDSQL.ToString();
  211. sqlAdapter.InsertCommand.Parameters.AddRange
  212. (
  213. new SqlParameter[]
  214. {
  215. new SqlParameter("@AccountingBookID",strAccountingBookID),
  216. new SqlParameter("@AccountingSubID",txtAccountingSubID.Text.Trim()),
  217. new SqlParameter("@AccountingSubName",txtAccountingSubName.Text.Trim()),
  218. new SqlParameter("@AccountingClass",cbAccountingClass.SelectedValue.ToString()),
  219. new SqlParameter("@DCClass",cbDCClass.SelectedValue.ToString()),
  220. new SqlParameter("@ToCarry",cbToCarry.Checked ? "Y" : "N"),
  221. new SqlParameter("@CreateUser",strActiveUserID),
  222. new SqlParameter("@ModifyUser",strActiveUserID),
  223. new SqlParameter("@Memo",txtMemo.Text.ToString()),
  224. }
  225. );
  226. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  227. {
  228. sqlConn.Open();
  229. }
  230. sqlAdapter.InsertCommand.ExecuteNonQuery();
  231. }
  232. break;
  233. case "MODIFY": //修改
  234. StringBuilder sbMDFSQL = new StringBuilder();
  235. sbMDFSQL.Append("Update OTB_FNC_AccountingSubjects Set ");
  236. sbMDFSQL.Append("AccountingClass = @AccountingClass, ");
  237. sbMDFSQL.Append("DCClass = @DCClass, ");
  238. sbMDFSQL.Append("AccountingSubName = @AccountingSubName, ");
  239. sbMDFSQL.Append("ToCarry = @ToCarry, ");
  240. sbMDFSQL.Append("ModifyDate = GetDate(), ");
  241. sbMDFSQL.Append("ModifyUser = @ModifyUser, ");
  242. sbMDFSQL.Append("Memo = @Memo ");
  243. sbMDFSQL.Append("Where AccountingBookID = @AccountingBookID And AccountingSubID = @AccountingSubID");
  244. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  245. {
  246. //添加參數
  247. sqlAdapter.UpdateCommand = new SqlCommand();
  248. sqlAdapter.UpdateCommand.Connection = sqlConn;
  249. sqlAdapter.UpdateCommand.CommandText = sbMDFSQL.ToString();
  250. sqlAdapter.UpdateCommand.Parameters.AddRange
  251. (
  252. new SqlParameter[]
  253. {
  254. new SqlParameter("@AccountingBookID",strAccountingBookID),
  255. new SqlParameter("@AccountingSubID",txtAccountingSubID.Text.Trim()),
  256. new SqlParameter("@AccountingSubName",txtAccountingSubName.Text.Trim()),
  257. new SqlParameter("@AccountingClass",cbAccountingClass.SelectedValue.ToString()),
  258. new SqlParameter("@ToCarry",cbToCarry.Checked ? "Y" : "N"),
  259. new SqlParameter("@DCClass",cbDCClass.SelectedValue.ToString()),
  260. new SqlParameter("@ModifyUser",strActiveUserID),
  261. new SqlParameter("@Memo",txtMemo.Text.ToString()),
  262. }
  263. );
  264. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  265. {
  266. sqlConn.Open();
  267. }
  268. sqlAdapter.UpdateCommand.ExecuteNonQuery();
  269. }
  270. break;
  271. }
  272. MessageBox.Show("儲存成功", "提示");
  273. StatusChange("None");
  274. LockForm();
  275. }
  276. catch (Exception ex)
  277. {
  278. MessageBox.Show("儲存失敗", "提示");
  279. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  280. }
  281. }
  282. private void DelEven() //刪除事件
  283. {
  284. StringBuilder sbSQL = new StringBuilder();
  285. try
  286. {
  287. //執行Delete命令
  288. //執行命令
  289. sbSQL.Append("Delete OTB_FNC_AccountingSubjects Where AccountingBookID = @AccountingBookID And AccountingSubID = @AccountingSubID");
  290. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  291. {
  292. //添加參數
  293. sqlAdapter.DeleteCommand = new SqlCommand();
  294. sqlAdapter.DeleteCommand.Connection = sqlConn;
  295. sqlAdapter.DeleteCommand.CommandText = sbSQL.ToString();
  296. sqlAdapter.DeleteCommand.Parameters.AddRange
  297. (
  298. new SqlParameter[]
  299. {
  300. new SqlParameter("@AccountingBookID",strAccountingBookID),
  301. new SqlParameter("@AccountingSubID",txtAccountingSubID.Text.Trim()),
  302. }
  303. );
  304. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  305. {
  306. sqlConn.Open();
  307. }
  308. sqlAdapter.DeleteCommand.ExecuteNonQuery();
  309. }
  310. StatusChange("NONE");
  311. }
  312. catch (Exception ex)
  313. {
  314. MessageBox.Show("刪除錯誤","錯誤");
  315. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  316. }
  317. }
  318. private void AddEven() //新增事件
  319. {
  320. try
  321. {
  322. //本功能無新增功能
  323. }
  324. catch (Exception ex)
  325. {
  326. throw ex;
  327. }
  328. }
  329. private void GoEvent() //執行事件
  330. {
  331. try
  332. {
  333. StringBuilder strSQL = new StringBuilder("");
  334. switch (strFrmStatus.ToString())
  335. {
  336. case "SEARCH": //搜尋
  337. strSQL.Append("Select * ");
  338. strSQL.Append("From OTB_FNC_AccountingSubjects ");
  339. strSQL.Append("Where AccountingBookID = '" + strAccountingBookID + "' ");
  340. if (txtAccountingSubID.Text.Trim() != "") //科目編號
  341. {
  342. strSQL.Append("And AccountingSubID like '" + txtAccountingSubID.Text.Trim() + "' ");
  343. }
  344. if (txtAccountingSubName.Text.Trim() != "") //會計稱名科目
  345. {
  346. strSQL.Append("And AccountingSubName like '" + txtAccountingSubName.Text.Trim() + "' ");
  347. }
  348. if (cbAccountingClass.SelectedValue != null) //會計科目類別
  349. {
  350. if (cbAccountingClass.SelectedValue.ToString().Trim() != "")
  351. {
  352. strSQL.Append("And AccountingClass = '" + cbAccountingClass.SelectedValue.ToString() + "' ");
  353. }
  354. }
  355. if (cbDCClass.SelectedValue != null) //借貸方類別
  356. {
  357. if (cbDCClass.SelectedValue.ToString() != "")
  358. {
  359. strSQL.Append("And DCClass = '" + cbDCClass.SelectedValue.ToString() + "' ");
  360. }
  361. }
  362. if (cbToCarry.Checked) //是否結轉
  363. {
  364. if (cbDCClass.SelectedValue.ToString() != "")
  365. {
  366. strSQL.Append("And ToCarry = 'Y' ");
  367. }
  368. }
  369. //進行查詢
  370. dgvAccountingSubjects.DataSource = UtilityClass.GetSQLResult(strSQL.ToString()).Tables[0];
  371. break;
  372. }
  373. LockForm();
  374. }
  375. catch (Exception ex)
  376. {
  377. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  378. }
  379. }
  380. #endregion
  381. #region 事件觸發及問題處理
  382. private void AccountingSubjectMaintain_Load(object sender, EventArgs e)
  383. {
  384. SetupStatus(); //設定畫面狀態
  385. }
  386. private void tsbSearch_Click(object sender, EventArgs e)
  387. {
  388. try
  389. {
  390. //設定Toolbar狀態
  391. CleanToolbar();
  392. tsbSearch.Visible = false;
  393. tsbAdd.Enabled = false;
  394. tsbEdit.Enabled = false;
  395. tsbDelete.Enabled = false;
  396. tsbOK.Visible = true;
  397. tsbCancel.Visible = true;
  398. //開放查詢條件
  399. UnLockForm();
  400. //設定畫面物件狀態
  401. txtAccountingSubID.Text = "";
  402. txtAccountingSubName.Text = "";
  403. txtMemo.Text = "";
  404. cbToCarry.Checked = false;
  405. cbAccountingClass.SelectedIndex = -1;
  406. cbDCClass.SelectedIndex = -1;
  407. StatusChange("Search");
  408. }
  409. catch (Exception ex)
  410. {
  411. MessageBox.Show("查詢錯誤", "提示");
  412. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  413. }
  414. }
  415. private void dgvAccountingSubject_CellEnter(object sender, DataGridViewCellEventArgs e)
  416. {
  417. string strAccountingSubID = (string)dgvAccountingSubjects.CurrentRow.Cells["cAccountingSubID"].Value;
  418. if (!string.IsNullOrEmpty(strAccountingSubID))
  419. {
  420. GetAccountData(strAccountingSubID); //顯示該科目的值到畫面上
  421. }
  422. }
  423. private void tsbAdd_Click(object sender, EventArgs e)
  424. {
  425. try
  426. {
  427. CleanToolbar();
  428. tsbSearch.Enabled = false;
  429. tsbAdd.Visible = false;
  430. tsbEdit.Enabled = false;
  431. tsbDelete.Enabled = false;
  432. tsbSave.Visible = true;
  433. tsbCancel.Visible = true;
  434. CleanForm();
  435. UnLockForm();
  436. txtAccountingSubID.Focus();
  437. StatusChange("ADD");
  438. dgvAccountingSubjects.ReadOnly = true;
  439. }
  440. catch (Exception ex)
  441. {
  442. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  443. }
  444. }
  445. private void tsbEdit_Click(object sender, EventArgs e)
  446. {
  447. try
  448. {
  449. if (strPKey == "")
  450. {
  451. MessageBox.Show("請先選擇資料", "提示");
  452. return;
  453. }
  454. CleanToolbar();
  455. tsbSearch.Enabled = false;
  456. tsbAdd.Enabled = false;
  457. tsbEdit.Visible = false;
  458. tsbDelete.Enabled = false;
  459. tsbSave.Visible = true;
  460. tsbCancel.Visible = true;
  461. UnLockForm();
  462. txtAccountingSubID.ReadOnly = true;
  463. txtAccountingSubName.Focus();
  464. StatusChange("Modify");
  465. dgvAccountingSubjects.ReadOnly = true;
  466. }
  467. catch (Exception ex)
  468. {
  469. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  470. }
  471. }
  472. private void tsbDelete_Click(object sender, EventArgs e)
  473. {
  474. try
  475. {
  476. if (MessageBox.Show("請問您確定要刪除本資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  477. {
  478. this.DelEven();
  479. }
  480. }
  481. catch (Exception ex)
  482. {
  483. MessageBox.Show("刪除錯誤!", "錯誤");
  484. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  485. }
  486. }
  487. private void tsbSave_Click(object sender, EventArgs e)
  488. {
  489. try
  490. {
  491. string strMessage = CheckForm();
  492. if (strMessage == "")
  493. {
  494. this.SaveEven();
  495. GetAccountData(strPKey);
  496. }
  497. else
  498. {
  499. MessageBox.Show(strMessage, "提示");
  500. }
  501. }
  502. catch (Exception ex)
  503. {
  504. MessageBox.Show("儲存錯誤!", "錯誤");
  505. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  506. }
  507. }
  508. private void tsbOK_Click(object sender, EventArgs e)
  509. {
  510. try
  511. {
  512. GoEvent();
  513. //還原Toolbar狀態
  514. tsbSearch.Enabled = true;
  515. tsbOK.Visible = false;
  516. tsbCancel.Visible = false;
  517. //關閉查詢條件
  518. LockForm();
  519. CleanToolbar();
  520. }
  521. catch (Exception ex)
  522. {
  523. MessageBox.Show("發生錯誤", "錯誤");
  524. ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex);
  525. }
  526. }
  527. private void tsbCancel_Click(object sender, EventArgs e)
  528. {
  529. CleanForm();
  530. CleanToolbar();
  531. LockForm();
  532. StatusChange("None");
  533. }
  534. private void tsbExit_Click(object sender, EventArgs e)
  535. {
  536. this.Close();
  537. }
  538. private void tsbClean_Click(object sender, EventArgs e)
  539. {
  540. CleanForm();
  541. }
  542. private void cbAccountingClass_SelectedIndexChanged(object sender, EventArgs e)
  543. {
  544. if (cbAccountingClass.SelectedValue != null)
  545. {
  546. if (cbAccountingClass.SelectedValue.ToString() == "E" | cbAccountingClass.SelectedValue.ToString() == "L")
  547. {
  548. cbDCClass.SelectedValue = "C";
  549. }
  550. else
  551. {
  552. cbDCClass.SelectedValue = "D";
  553. }
  554. }
  555. }
  556. #endregion
  557. }
  558. }