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 AccountingLedger : Form { //程式內共用物件 public string strFrmStatus = ""; //表單狀態 public string strPKey = ""; //程式內的Key值 string strKey = ""; //程式內加密的Key值 string strActiveUserID = ""; //取得登入作用的使用者帳號 string strAccountingBookID = ""; //取得作用中的帳本 SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID); SqlTransaction sqlTran; SqlDataAdapter sqlAdapter = new SqlDataAdapter(); SqlCommand sqlCmd = new SqlCommand(); DataSet sdInsurance = new System.Data.DataSet(); public AccountingLedger() { InitializeComponent(); } #region 自定義程式 private void StatusChange(string strStatus) //變更主畫面狀態 { strActiveUserID = MainForm.strActiveUserID; strAccountingBookID = MainForm.strAccountingBookID; strKey = MainForm.strKey; switch (strStatus.ToUpper()) { case "NONE": //本功能無None狀態 ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = ""; strFrmStatus = ""; CleanToolbar(); CleanForm(); LockForm(); tsbSearch.Enabled = true; tsbOK.Visible = false; tsbCancel.Visible = false; break; case "SEARCH": //查詢功能 ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋"; tsbSearch.Visible = false; tsbOK.Visible = true; tsbCancel.Visible = true; UnLockForm(); 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 GoEvent() //執行動作 { //本程式只有查詢結果 if (txtAccountingSubID.Text.Trim() == "") { MessageBox.Show("請輸入科目編號", "提示"); } else { dgvAccountingLedger.Rows.Clear(); SearchAccountingLedger(txtAccountingSubID.Text.Trim()); StatusChange("NONE"); UtilityClass.SetGridColor(dgvAccountingLedger); } } private void CleanForm() //清除畫面 { //設定畫面物件狀態 //清除GridView cbAccountingYear.DataSource = UtilityClass.GetAccountingYears(5); cbAccountingYear.SelectedIndex = 0; txtAccountingSubID.Text = ""; txtAccountingSubName.Text = ""; dpAccStart.Text = DateTime.Now.Year.ToString() + "/1/1"; dpAccEnd.Text = DateTime.Now.ToShortDateString(); } private void LockForm() //鎖定物件 { txtAccountingSubID.ReadOnly = true; txtAccountingSubName.ReadOnly = true; cbAccountingYear.Enabled = false; dpAccStart.Enabled = false; dpAccEnd.Enabled = false; btnGetSubAccID.Enabled = false; } private void UnLockForm() //物件解鎖 { txtAccountingSubID.ReadOnly = false; txtAccountingSubName.ReadOnly = false; cbAccountingYear.Enabled = true; dpAccStart.Enabled = true; dpAccEnd.Enabled = true; btnGetSubAccID.Enabled = true; } private void CleanToolbar() //清除工具列 { //設定Toolbar狀態 tsbSearch.Visible = true; tsbAdd.Visible = false; tsbEdit.Visible = false; tsbDelete.Visible = false; tsbSave.Visible = false; tsbOK.Visible = false; tsbCancel.Visible = false; } public void ReturnAccountList(string strAccountList) //取得資料 { //strAccList[0] : IndexID //strAccList[1] : 科目代碼 //strAccList[2] : 科目名稱 string[] strAccList = strAccountList.Split('|'); txtAccountingSubID.Text = strAccList[1]; txtAccountingSubName.Text = strAccList[2]; } private DataTable GetAccountingSubjectByID(string strAccountingSubID) { try { DataTable dtTemp = new DataTable(); if (strAccountingSubID.Trim() != "") { //宣告物件 string strSQL = string.Format("Select AccountingSubID, AccountingClass, DCClass, AccountingSubName From OTB_FNC_AccountingSubjects Where AccountingSubID = '{0}' And AccountingBookID = '{1}'", strAccountingSubID, strAccountingBookID); using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.SelectCommand = new SqlCommand(); sqlAdapter.SelectCommand.Connection = sqlConn; sqlAdapter.SelectCommand.CommandText = strSQL; if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } //進行查詢 dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"]; } } return dtTemp; } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex); return null; } } private DataTable GetAccountingSubjectByName(string strAccountingSubName) { try { DataTable dtTemp = new DataTable(); if (strAccountingSubName.Trim() != "") { //宣告物件 string strSQL = string.Format("Select AccountingSubID, AccountingClass, DCClass, AccountingSubName From OTB_FNC_AccountingSubjects Where AccountingSubName = '{0}' And AccountingBookID ='{1}'", strAccountingSubName, strAccountingBookID); using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.SelectCommand = new SqlCommand(); sqlAdapter.SelectCommand.Connection = sqlConn; sqlAdapter.SelectCommand.CommandText = strSQL; if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } //進行查詢 dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"]; } } return dtTemp; } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex); return null; } } private void SearchAccountingLedger(string strAccountingSubID) { try { DataTable dtTemp = new DataTable(); if (strAccountingSubID.Trim() != "") { //宣告物件 string strDebit = ""; string strCredit = ""; double dbBalance = 0; string strSQL = string.Format("Select * From OTB_FNC_AccountingJournal Where 1 = 1 And AccountingBookID = '{0}' And AccountingSubID = '{1}' And AccountingDate Between '{2} 00:00:00' And '{3} 23:59:59' Order By AccountingDate", strAccountingBookID, strAccountingSubID, dpAccStart.Text.Trim(), dpAccEnd.Text.Trim()); using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.SelectCommand = new SqlCommand(); sqlAdapter.SelectCommand.Connection = sqlConn; sqlAdapter.SelectCommand.CommandText = strSQL; if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } //進行查詢 dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"]; if (dtTemp.Rows.Count > 0) { foreach (DataRow drData in dtTemp.Rows) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(dgvAccountingLedger); dgvRow.Cells[0].Value = drData["AccountingID"].ToString(); //傳票編號 dgvRow.Cells[1].Value = drData["AccountingDate"].ToString(); //傳票日期 dgvRow.Cells[2].Value = drData["Memo"].ToString(); //摘要 strDebit = (string)UtilityClass.DecryptDES(drData["Debit"].ToString(), strKey); strCredit = UtilityClass.DecryptDES(drData["Credit"].ToString(), strKey); dgvRow.Cells[3].Value = UtilityClass.MarkNumber(strDebit); //借方金額 dgvRow.Cells[4].Value = UtilityClass.MarkNumber(strCredit); //貸方金額 dbBalance += Convert.ToDouble(string.IsNullOrEmpty(strDebit) ? "0" : strDebit) - Convert.ToDouble(string.IsNullOrEmpty(strCredit) ? "0" : strCredit); //計算餘額 dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbBalance); //餘額 dgvAccountingLedger.Rows.Add(dgvRow); } //將資料移到最下面 dgvAccountingLedger.CurrentCell = dgvAccountingLedger.Rows[dtTemp.Rows.Count - 1].Cells[0]; } } } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex); } } #endregion #region 事件觸發及問題處理 private void AccountingLedger_Load(object sender, EventArgs e) { StatusChange("NONE"); } private void tsbSearch_Click(object sender, EventArgs e) { StatusChange("SEARCH"); } private void tsbClean_Click(object sender, EventArgs e) { CleanForm(); lbAccountingName.Text = ""; dgvAccountingLedger.Rows.Clear(); } private void tsbCancel_Click(object sender, EventArgs e) { StatusChange("NONE"); lbAccountingName.Text = ""; } private void tsbExit_Click(object sender, EventArgs e) { this.Close(); } private void btnGetSubAccID_Click(object sender, EventArgs e) { PickAccountingSubject pkItemForm = new PickAccountingSubject(); string strAccountingSubID = txtAccountingSubID.Text.Trim(); string strAccountingSubName = txtAccountingSubName.Text.Trim(); if(strAccountingSubID != "") { pkItemForm.txtAccountingSubID.Text = strAccountingSubID; } if (strAccountingSubName != "") { pkItemForm.txtAccountingSubName.Text = strAccountingSubName; } pkItemForm.Owner = this; pkItemForm.strOwnerForm = "AccountingLedger"; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } private void txtAccountingSubID_Leave(object sender, EventArgs e) { //物件宣告 string strAccountingSubID = ""; string strAccountingSubName = ""; strAccountingSubID = txtAccountingSubID.Text.Trim(); if (string.IsNullOrEmpty(strAccountingSubID)) { return; } else { string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubID = '{0}' And AccountingBookID = '{1}'", strAccountingSubID, strAccountingBookID); if (UtilityClass.IsExist(strCheckSQL)) { strAccountingSubName = GetAccountingSubjectByID(strAccountingSubID).Rows[0]["AccountingSubName"].ToString(); txtAccountingSubName.Text = strAccountingSubName; } } } private void txtAccountingSubName_Leave(object sender, EventArgs e) { //物件宣告 string strAccountingSubID = ""; string strAccountingSubName = ""; strAccountingSubName = txtAccountingSubName.Text.Trim(); if (string.IsNullOrEmpty(strAccountingSubName)) { return; } else { string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubName = '{0}' And AccountingBookID = '{1}'", strAccountingSubName, strAccountingBookID); if (UtilityClass.IsExist(strCheckSQL)) { strAccountingSubID = GetAccountingSubjectByName(strAccountingSubName).Rows[0]["AccountingSubID"].ToString(); txtAccountingSubID.Text = strAccountingSubID; } } } private void tsbOK_Click(object sender, EventArgs e) { lbAccountingName.Text = txtAccountingSubName.Text.Trim(); GoEvent(); } private void cbAccountingYear_SelectedIndexChanged(object sender, EventArgs e) { if (cbAccountingYear.SelectedValue.ToString() == DateTime.Now.Year.ToString()) { dpAccStart.Text = DateTime.Now.Year.ToString() + "/1/1"; dpAccEnd.Text = DateTime.Now.ToShortDateString(); } else { dpAccStart.Text = cbAccountingYear.SelectedValue.ToString() + "/1/1"; dpAccEnd.Text = cbAccountingYear.SelectedValue.ToString() + "/12/31"; } } #endregion } }