using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.Sql; using System.Data.SqlClient; using ManagementSystem.Utility; namespace ManagementSystem { public partial class AccountingSubjectMaintain : Form { //程式內共用物件 string strFrmStatus = ""; //表單狀態 string strActiveUserID = ""; //取得登入作用的使用者帳號 string strAccountingBookID = ""; //取得作用中的帳本 SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID); SqlCommand sqlCmd = new SqlCommand(); string strPKey = ""; //程式內的Key值 public AccountingSubjectMaintain() { InitializeComponent(); } #region 自定義程式 private void SetupStatus() //畫面載入設定 { try { strActiveUserID = MainForm.strActiveUserID; strAccountingBookID = MainForm.strAccountingBookID; if (MainForm.strKey == "") { //設定Toolbar初始狀態 tsbAdd.Enabled = false; tsbSave.Enabled = false; tsbDelete.Enabled = false; } //設定畫面初始狀態 CleanForm(); CleanToolbar(); LockForm(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private string CheckForm() //確認畫面必填欄位 { string strResult = ""; if (txtAccountingSubID.Text.Trim() == "") { strResult = "請輸入科目編號"; txtAccountingSubID.Focus(); } if (txtAccountingSubName.Text.Trim() == "") { strResult = "請輸入會計科目"; txtAccountingSubName.Focus(); } if (cbAccountingClass.SelectedValue.ToString().Trim() == "") { strResult = "請選擇會計類別"; cbAccountingClass.Focus(); } if (cbDCClass.SelectedValue.ToString().Trim() == "") { strResult = "請選擇借/貸方類別"; cbDCClass.Focus(); } if (strFrmStatus.ToUpper() == "ADD") { string strCHKSQL = "Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingSubID ='" + txtAccountingSubID.Text.Trim() + "'"; if (UtilityClass.IsExist(strCHKSQL)) { strResult = "存在重覆科目資料"; txtAccountingSubID.Focus(); } } return strResult; } private void CleanForm() //清除畫面 { //設定科目類別 cbAccountingClass.DataSource = UtilityClass.GetSystemArgument("ACCClass", "zh-TW").Tables["Arguments"]; cbAccountingClass.DisplayMember = "ArgumentValue"; cbAccountingClass.ValueMember = "ArgumentID"; //設定借/貸方類別 cbDCClass.DataSource = UtilityClass.GetSystemArgument("DCClass", "zh-TW").Tables["Arguments"]; cbDCClass.DisplayMember = "ArgumentValue"; cbDCClass.ValueMember = "ArgumentID"; //清除GridView string strSQL = "Select AccountingSubID, AccountingSubName From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' Order by AccountingSubID"; dgvAccountingSubjects.DataSource = UtilityClass.GetSQLResult(strSQL).Tables["Result"]; //清除畫面資料 txtAccountingSubID.Text = ""; txtAccountingSubName.Text = ""; cbAccountingClass.SelectedIndex = -1; cbDCClass.SelectedIndex = -1; cbToCarry.Checked = false; Application.DoEvents(); } private void LockForm() //限制唯讀物件 { txtAccountingSubID.ReadOnly = true; txtAccountingSubName.ReadOnly = true; cbDCClass.Enabled = false; cbAccountingClass.Enabled = false; cbToCarry.Enabled = false; } private void UnLockForm() //解除限制唯讀物件 { txtAccountingSubID.ReadOnly = false; txtAccountingSubName.ReadOnly = false; cbDCClass.Enabled = true; cbAccountingClass.Enabled = true; cbToCarry.Enabled = true; } private void CleanToolbar() //清除工具列 { //設定Toolbar狀態 tsbSearch.Enabled = true; tsbSearch.Visible = true; tsbAdd.Enabled = true; tsbAdd.Visible = true; tsbEdit.Enabled = true; tsbEdit.Visible = true; tsbDelete.Enabled = true; tsbDelete.Visible = true; tsbSave.Visible = false; tsbOK.Visible = false; tsbCancel.Visible = false; } private void StatusChange(string strStatus) //變更主畫面狀態 { switch (strStatus.ToUpper()) { case "NONE": ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = ""; strFrmStatus = ""; CleanForm(); CleanToolbar(); dgvAccountingSubjects.ReadOnly = false; break; case "SEARCH": ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋"; strFrmStatus = "SEARCH"; break; case "ADD": ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增"; strFrmStatus = "ADD"; break; case "MODIFY": ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改"; strFrmStatus = "MODIFY"; break; case "DEL": ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除"; strFrmStatus = "DEL"; break; } } private void GetAccountData(string strAccountingSubID) //取得會計科目資料 { //排除空值 if (strAccountingSubID.Trim() == "") { return; } if (strAccountingSubID == null) { return; } strPKey = strAccountingSubID; StringBuilder strSQL = new StringBuilder(""); strSQL.Append("Select * "); strSQL.Append("From OTB_FNC_AccountingSubjects "); strSQL.Append("Where AccountingBookID = '" + strAccountingBookID + "' And AccountingSubID = '" + strPKey + "'"); //進行查詢 DataTable dtTemp = (DataTable)UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"]; //進行資料顯示 txtAccountingSubID.Text = dtTemp.Rows[0]["AccountingSubID"].ToString(); txtAccountingSubName.Text = dtTemp.Rows[0]["AccountingSubName"].ToString(); txtMemo.Text = dtTemp.Rows[0]["Memo"].ToString(); cbAccountingClass.SelectedValue = dtTemp.Rows[0]["AccountingClass"].ToString(); cbDCClass.SelectedValue = dtTemp.Rows[0]["DCClass"].ToString(); cbToCarry.Checked = (dtTemp.Rows[0]["ToCarry"].ToString() == "Y"); ((MainForm)ParentForm).SsStatus.Items["tsslModifyUser"].Text = dtTemp.Rows[0]["ModifyUser"].ToString().Trim(); ((MainForm)ParentForm).SsStatus.Items["tsslModifyDate"].Text = dtTemp.Rows[0]["ModifyDate"].ToString().Trim(); } private void SaveEven() //儲存事件 { try { switch (strFrmStatus.ToString()) { case "ADD": //新增 StringBuilder sbADDSQL = new StringBuilder(); sbADDSQL.Append("Insert Into OTB_FNC_AccountingSubjects(AccountingBookID,AccountingSubID,AccountingClass,DCClass,AccountingSubName,ToCarry,CreateDate,CreateUser,ModifyDate,ModifyUser,Memo) "); sbADDSQL.Append("Values(@AccountingBookID,@AccountingSubID,@AccountingClass,@DCClass,@AccountingSubName,@ToCarry,Getdate(),@CreateUser,Getdate(),@ModifyUser,@Memo)"); using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.InsertCommand = new SqlCommand(); sqlAdapter.InsertCommand.Connection = sqlConn; sqlAdapter.InsertCommand.CommandText = sbADDSQL.ToString(); sqlAdapter.InsertCommand.Parameters.AddRange ( new SqlParameter[] { new SqlParameter("@AccountingBookID",strAccountingBookID), new SqlParameter("@AccountingSubID",txtAccountingSubID.Text.Trim()), new SqlParameter("@AccountingSubName",txtAccountingSubName.Text.Trim()), new SqlParameter("@AccountingClass",cbAccountingClass.SelectedValue.ToString()), new SqlParameter("@DCClass",cbDCClass.SelectedValue.ToString()), new SqlParameter("@ToCarry",cbToCarry.Checked ? "Y" : "N"), new SqlParameter("@CreateUser",strActiveUserID), new SqlParameter("@ModifyUser",strActiveUserID), new SqlParameter("@Memo",txtMemo.Text.ToString()), } ); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.InsertCommand.ExecuteNonQuery(); } break; case "MODIFY": //修改 StringBuilder sbMDFSQL = new StringBuilder(); sbMDFSQL.Append("Update OTB_FNC_AccountingSubjects Set "); sbMDFSQL.Append("AccountingClass = @AccountingClass, "); sbMDFSQL.Append("DCClass = @DCClass, "); sbMDFSQL.Append("AccountingSubName = @AccountingSubName, "); sbMDFSQL.Append("ToCarry = @ToCarry, "); sbMDFSQL.Append("ModifyDate = GetDate(), "); sbMDFSQL.Append("ModifyUser = @ModifyUser, "); sbMDFSQL.Append("Memo = @Memo "); sbMDFSQL.Append("Where AccountingBookID = @AccountingBookID And AccountingSubID = @AccountingSubID"); using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.UpdateCommand = new SqlCommand(); sqlAdapter.UpdateCommand.Connection = sqlConn; sqlAdapter.UpdateCommand.CommandText = sbMDFSQL.ToString(); sqlAdapter.UpdateCommand.Parameters.AddRange ( new SqlParameter[] { new SqlParameter("@AccountingBookID",strAccountingBookID), new SqlParameter("@AccountingSubID",txtAccountingSubID.Text.Trim()), new SqlParameter("@AccountingSubName",txtAccountingSubName.Text.Trim()), new SqlParameter("@AccountingClass",cbAccountingClass.SelectedValue.ToString()), new SqlParameter("@ToCarry",cbToCarry.Checked ? "Y" : "N"), new SqlParameter("@DCClass",cbDCClass.SelectedValue.ToString()), new SqlParameter("@ModifyUser",strActiveUserID), new SqlParameter("@Memo",txtMemo.Text.ToString()), } ); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.UpdateCommand.ExecuteNonQuery(); } break; } MessageBox.Show("儲存成功", "提示"); StatusChange("None"); LockForm(); } catch (Exception ex) { MessageBox.Show("儲存失敗", "提示"); ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private void DelEven() //刪除事件 { StringBuilder sbSQL = new StringBuilder(); try { //執行Delete命令 //執行命令 sbSQL.Append("Delete OTB_FNC_AccountingSubjects Where AccountingBookID = @AccountingBookID And AccountingSubID = @AccountingSubID"); using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.DeleteCommand = new SqlCommand(); sqlAdapter.DeleteCommand.Connection = sqlConn; sqlAdapter.DeleteCommand.CommandText = sbSQL.ToString(); sqlAdapter.DeleteCommand.Parameters.AddRange ( new SqlParameter[] { new SqlParameter("@AccountingBookID",strAccountingBookID), new SqlParameter("@AccountingSubID",txtAccountingSubID.Text.Trim()), } ); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.DeleteCommand.ExecuteNonQuery(); } StatusChange("NONE"); } catch (Exception ex) { MessageBox.Show("刪除錯誤","錯誤"); ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private void AddEven() //新增事件 { try { //本功能無新增功能 } catch (Exception ex) { throw ex; } } private void GoEvent() //執行事件 { try { StringBuilder strSQL = new StringBuilder(""); switch (strFrmStatus.ToString()) { case "SEARCH": //搜尋 strSQL.Append("Select * "); strSQL.Append("From OTB_FNC_AccountingSubjects "); strSQL.Append("Where AccountingBookID = '" + strAccountingBookID + "' "); if (txtAccountingSubID.Text.Trim() != "") //科目編號 { strSQL.Append("And AccountingSubID like '" + txtAccountingSubID.Text.Trim() + "' "); } if (txtAccountingSubName.Text.Trim() != "") //會計稱名科目 { strSQL.Append("And AccountingSubName like '" + txtAccountingSubName.Text.Trim() + "' "); } if (cbAccountingClass.SelectedValue != null) //會計科目類別 { if (cbAccountingClass.SelectedValue.ToString().Trim() != "") { strSQL.Append("And AccountingClass = '" + cbAccountingClass.SelectedValue.ToString() + "' "); } } if (cbDCClass.SelectedValue != null) //借貸方類別 { if (cbDCClass.SelectedValue.ToString() != "") { strSQL.Append("And DCClass = '" + cbDCClass.SelectedValue.ToString() + "' "); } } if (cbToCarry.Checked) //是否結轉 { if (cbDCClass.SelectedValue.ToString() != "") { strSQL.Append("And ToCarry = 'Y' "); } } //進行查詢 dgvAccountingSubjects.DataSource = UtilityClass.GetSQLResult(strSQL.ToString()).Tables[0]; break; } LockForm(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } #endregion #region 事件觸發及問題處理 private void AccountingSubjectMaintain_Load(object sender, EventArgs e) { SetupStatus(); //設定畫面狀態 } private void tsbSearch_Click(object sender, EventArgs e) { try { //設定Toolbar狀態 CleanToolbar(); tsbSearch.Visible = false; tsbAdd.Enabled = false; tsbEdit.Enabled = false; tsbDelete.Enabled = false; tsbOK.Visible = true; tsbCancel.Visible = true; //開放查詢條件 UnLockForm(); //設定畫面物件狀態 txtAccountingSubID.Text = ""; txtAccountingSubName.Text = ""; txtMemo.Text = ""; cbToCarry.Checked = false; cbAccountingClass.SelectedIndex = -1; cbDCClass.SelectedIndex = -1; StatusChange("Search"); } catch (Exception ex) { MessageBox.Show("查詢錯誤", "提示"); ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private void dgvAccountingSubject_CellEnter(object sender, DataGridViewCellEventArgs e) { string strAccountingSubID = (string)dgvAccountingSubjects.CurrentRow.Cells["cAccountingSubID"].Value; if (!string.IsNullOrEmpty(strAccountingSubID)) { GetAccountData(strAccountingSubID); //顯示該科目的值到畫面上 } } private void tsbAdd_Click(object sender, EventArgs e) { try { CleanToolbar(); tsbSearch.Enabled = false; tsbAdd.Visible = false; tsbEdit.Enabled = false; tsbDelete.Enabled = false; tsbSave.Visible = true; tsbCancel.Visible = true; CleanForm(); UnLockForm(); txtAccountingSubID.Focus(); StatusChange("ADD"); dgvAccountingSubjects.ReadOnly = true; } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private void tsbEdit_Click(object sender, EventArgs e) { try { if (strPKey == "") { MessageBox.Show("請先選擇資料", "提示"); return; } CleanToolbar(); tsbSearch.Enabled = false; tsbAdd.Enabled = false; tsbEdit.Visible = false; tsbDelete.Enabled = false; tsbSave.Visible = true; tsbCancel.Visible = true; UnLockForm(); txtAccountingSubID.ReadOnly = true; txtAccountingSubName.Focus(); StatusChange("Modify"); dgvAccountingSubjects.ReadOnly = true; } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private void tsbDelete_Click(object sender, EventArgs e) { try { if (MessageBox.Show("請問您確定要刪除本資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { this.DelEven(); } } catch (Exception ex) { MessageBox.Show("刪除錯誤!", "錯誤"); ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private void tsbSave_Click(object sender, EventArgs e) { try { string strMessage = CheckForm(); if (strMessage == "") { this.SaveEven(); GetAccountData(strPKey); } else { MessageBox.Show(strMessage, "提示"); } } catch (Exception ex) { MessageBox.Show("儲存錯誤!", "錯誤"); ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private void tsbOK_Click(object sender, EventArgs e) { try { GoEvent(); //還原Toolbar狀態 tsbSearch.Enabled = true; tsbOK.Visible = false; tsbCancel.Visible = false; //關閉查詢條件 LockForm(); CleanToolbar(); } catch (Exception ex) { MessageBox.Show("發生錯誤", "錯誤"); ErrorHandler.WriteErrorLog("AccountingSubjectMaintain.cs", ex); } } private void tsbCancel_Click(object sender, EventArgs e) { CleanForm(); CleanToolbar(); LockForm(); StatusChange("None"); } private void tsbExit_Click(object sender, EventArgs e) { this.Close(); } private void tsbClean_Click(object sender, EventArgs e) { CleanForm(); } private void cbAccountingClass_SelectedIndexChanged(object sender, EventArgs e) { if (cbAccountingClass.SelectedValue != null) { if (cbAccountingClass.SelectedValue.ToString() == "E" | cbAccountingClass.SelectedValue.ToString() == "L") { cbDCClass.SelectedValue = "C"; } else { cbDCClass.SelectedValue = "D"; } } } #endregion } }