using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ManagementSystem.Utility; using ManagementSystem; namespace ManagementSystem { public partial class AccountingEntries : Form { //程式內共用物件 public string strFrmStatus = ""; //表單狀態 public string strPKey = ""; //程式內的Key值 public string strOwnerForm = ""; //呼叫的Form Name public string strKey = ""; //程式內加密的Key值 public string strActiveUserID = ""; //取得登入作用的使用者帳號 public string strAccountingBookID = ""; //取得作用中的帳本 public string strAccountingYear = ""; //取得分錄年份 SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID); SqlTransaction sqlTran; SqlDataAdapter sqlAdapter = new SqlDataAdapter(); SqlCommand sqlCmd = new SqlCommand(); DataSet sdInsurance = new System.Data.DataSet(); public DataGridViewCellEventArgs dgeArgs; public AccountingEntries() { InitializeComponent(); } #region 自定義程式 private void ReSum() //重新統計借貸方的總額 { string strAddedDebit = "0"; string strAddedCredit = "0"; string strdDebit = "0"; string strCredit = "0"; foreach (DataGridViewRow drAccountingItem in dgvDataMaintain.Rows) { if (drAccountingItem.Cells["cDebit"].Value != null) { strdDebit = string.IsNullOrEmpty(drAccountingItem.Cells["cDebit"].Value.ToString()) ? "0" : drAccountingItem.Cells["cDebit"].Value.ToString().Replace(",",""); strAddedDebit = (Convert.ToInt32(strAddedDebit) + Convert.ToInt32(strdDebit)).ToString(); } if (drAccountingItem.Cells["cCredit"].Value != null) { strCredit = string.IsNullOrEmpty(drAccountingItem.Cells["cCredit"].Value.ToString()) ? "0" : drAccountingItem.Cells["cCredit"].Value.ToString().Replace(",", ""); strAddedCredit = (Convert.ToInt32(strAddedCredit) + Convert.ToInt32(strCredit)).ToString(); } } lbDebit.Text = UtilityClass.MarkNumber(strAddedDebit); lbCredit.Text = UtilityClass.MarkNumber(strAddedCredit); } private void StatusChange(string strStatus) //變更主畫面狀態 { strActiveUserID = MainForm.strActiveUserID; strAccountingBookID = MainForm.strAccountingBookID; strKey = MainForm.strKey; switch (strStatus.ToUpper()) { case "NONE": //本功能無None狀態 strFrmStatus = ""; break; case "SEARCH": //本程式並無查詢功能 Application.DoEvents(); break; case "ADD": UnLockForm(); txtAccountingID.Text = ""; dpAccountingDate.Value = DateTime.Now; dpAccountingDate.Focus(); strFrmStatus = "ADD"; break; case "MODIFY": UnLockForm(); txtAccountingID.Text = strPKey; //先取得該分錄的基本資料 GetAccountingListByID(strPKey); dpAccountingDate.Focus(); strFrmStatus = "MODIFY"; break; case "CLOSE": UnLockForm(); strFrmStatus = "CLOSE"; break; case "DEL": btnSave.Text = "確認刪除"; txtAccountingID.Text = strPKey; //應先取得該分錄的基本資料 GetAccountingListByID(strPKey); dpAccountingDate.Enabled = false; dgvDataMaintain.AllowUserToAddRows = false; dgvDataMaintain.ReadOnly = true; btnCancle.Enabled = false; btnClose.Focus(); strFrmStatus = "DEL"; break; } } private bool CheckForm() //確認分錄是否正確 { string strCheckResult = ""; string strDebit = ""; string strCredit = ""; double dbDebit = 0; //借方總額 double dbCredit = 0; //借方總額 //確認借貸是否平衡 foreach (DataGridViewRow dgRow in dgvDataMaintain.Rows) { strDebit = (string)dgRow.Cells["cDebit"].Value; strCredit = (string)dgRow.Cells["cCredit"].Value; if(!string.IsNullOrEmpty(strDebit)) { dbDebit += Convert.ToDouble(strDebit.Replace(",","")); } if (!string.IsNullOrEmpty(strCredit)) { dbCredit += Convert.ToDouble(strCredit.Replace(",", "")); } } if (dbDebit != dbCredit) { strCheckResult = "借貸不平衡"; } //確認是否有開帳 string strChackDate = dpAccountingDate.Text == "" ? DateTime.Now.ToString() : dpAccountingDate.Text; string strCheckSQL = "Select * From OTB_FNC_AccountingBookStatus Where IsOpen = 'Y' And AccountingYear = Year(Convert(DateTime,'" + Convert.ToDateTime(strChackDate).ToString("yyyy/MM/dd HH:mm:ss") + "'))"; if (!UtilityClass.IsExist(strCheckSQL)) { strCheckResult = "該會計年度尚未開帳! 請先至開帳功能進行開帳動作"; } if (strCheckResult != "") { MessageBox.Show(strCheckResult); return false; } return true; } private void CleanForm() //清除畫面 { //設定畫面物件狀態 //清除GridView StatusChange(strFrmStatus); } private void UnLockForm() //解除限制唯讀物件 { dpAccountingDate.Enabled = true; } private void LockForm() //限制唯讀物件 { dpAccountingDate.Enabled = false; } private void GetAccountingListByID(string strAccountingSubID) { try { string strDebit = ""; string strCredit = ""; DataTable dtTemp = new DataTable(); if (strAccountingSubID.Trim() != "") { //宣告物件 string strSQL = string.Format("Select AccountingSubID, AccountingSubName, AccountingDate, Memo, Debit, Credit, ProjectNumber, ProjectCName From OTB_FNC_AccountingJournal Where AccountingID = '{0}' And AccountingBookID ='{1}' Order by AccountingOrder", 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"]; if (dtTemp.Rows.Count > 0) { //strAccountingDate = Convert.ToDateTime(dtTemp.Rows[0]["AccountingDate"].ToString()).ToString("yyyy/MM/dd HH:mm:ss"); dpAccountingDate.Text = Convert.ToDateTime(dtTemp.Rows[0]["AccountingDate"].ToString()).ToString("yyyy/MM/dd HH:mm:ss"); foreach (DataRow drAccounting in dtTemp.Rows) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(dgvDataMaintain); dgvRow.Cells[1].Value = drAccounting["AccountingSubID"].ToString(); //科目代號 dgvRow.Cells[2].Value = drAccounting["AccountingSubName"].ToString(); //科目名稱 dgvRow.Cells[3].Value = drAccounting["Memo"].ToString(); //摘要 strDebit = UtilityClass.DecryptDES(drAccounting["Debit"].ToString(), strKey); strCredit = UtilityClass.DecryptDES(drAccounting["Credit"].ToString(), strKey); dgvRow.Cells[4].Value = UtilityClass.MarkNumber(strDebit); //借方金額 dgvRow.Cells[5].Value = UtilityClass.MarkNumber(strCredit); //貸方金額 dgvRow.Cells[7].Value = drAccounting["ProjectCName"].ToString(); //專案名稱 dgvRow.Cells[8].Value = drAccounting["ProjectNumber"].ToString(); //專案代碼 dgvDataMaintain.Rows.Add(dgvRow); } } } } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex); } } public void ReturnAccountList(string strAccountList) { string[] strAccList = strAccountList.Split('|'); dgvDataMaintain.Rows[Convert.ToInt32(strAccList[0])].Cells["cAccountingSubID"].Value = strAccList[1].ToString(); dgvDataMaintain.Rows[Convert.ToInt32(strAccList[0])].Cells["cAccountingSubName"].Value = strAccList[2].ToString(); } public string GetNewAccountID() { //宣告物件 string strNewAccountID = ""; int intMaxID = 0; //確認當日是否有取過傳票編號 StringBuilder sbCheckNewAccountID = new StringBuilder(); StringBuilder sbUpdateNewAccountID = new StringBuilder(); sbCheckNewAccountID.Append("Select * From OTB_FNC_AccountingMaxNumber Where AccountingBookID = '"+ strAccountingBookID + "' "); sbCheckNewAccountID.Append("And CountYear = '" + DateTime.Now.Year.ToString() + "' "); sbCheckNewAccountID.Append("And CountMonth = '" + DateTime.Now.Month.ToString() + "' "); sbCheckNewAccountID.Append("And CountDay = '" + DateTime.Now.Day.ToString() + "' "); DataSet dsTemp = UtilityClass.GetSQLResult(sbCheckNewAccountID.ToString()); if (dsTemp.Tables["Result"].Rows.Count > 0) { //當日已經有取得傳票編號 intMaxID = Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountMax"].ToString()) + 1; strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountYear"]).ToString("0000"); //年碼 strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountMonth"]).ToString("00"); //月碼 strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountDay"]).ToString("00"); //日碼 strNewAccountID += intMaxID.ToString("000"); //流水碼 //更新傳票編號 sbUpdateNewAccountID.Append("Update OTB_FNC_AccountingMaxNumber Set CountMax = CountMax + 1 "); sbUpdateNewAccountID.Append(", ModifyUser = '" + strActiveUserID + "', ModifyDate = Getdate() "); sbUpdateNewAccountID.Append("Where AccountingBookID = '" + strAccountingBookID + "' "); sbUpdateNewAccountID.Append("And CountYear = '" + dsTemp.Tables["Result"].Rows[0]["CountYear"].ToString() + "'"); sbUpdateNewAccountID.Append("And CountMonth = '" + dsTemp.Tables["Result"].Rows[0]["CountMonth"].ToString() + "'"); sbUpdateNewAccountID.Append("And CountDay = '" + dsTemp.Tables["Result"].Rows[0]["CountDay"].ToString() + "'"); UtilityClass.RunSQLNonReturn(sbUpdateNewAccountID.ToString()); } else { //當日未取過傳票編號 intMaxID = 1; strNewAccountID += DateTime.Now.Year.ToString("0000"); //年碼 strNewAccountID += DateTime.Now.Month.ToString("00"); //月碼 strNewAccountID += DateTime.Now.Day.ToString("00"); //日碼 strNewAccountID += intMaxID.ToString("000"); //流水碼 //新增傳票編號 sbUpdateNewAccountID.Append("Insert into OTB_FNC_AccountingMaxNumber(AccountingBookID, CountYear, CountMonth, CountDay, CountMax, Memo,CreateUser, CreateDate, ModifyUser, ModifyDate) "); sbUpdateNewAccountID.Append("Values ('" + strAccountingBookID + "',Year(getdate()),Month(getdate()),day(getdate()),1,null,'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "',Getdate())"); UtilityClass.RunSQLNonReturn(sbUpdateNewAccountID.ToString()); } return strNewAccountID; } public string GetNewAccountID(string strYear) { //宣告物件 string strNewAccountID = ""; int intMaxID = 0; //確認當日是否有取過傳票編號 StringBuilder sbCheckNewAccountID = new StringBuilder(); StringBuilder sbUpdateNewAccountID = new StringBuilder(); sbCheckNewAccountID.Append("Select TOP 1 * From OTB_FNC_AccountingMaxNumber Where AccountingBookID = '" + strAccountingBookID + "' "); sbCheckNewAccountID.Append("And CountYear = '" + strYear + "' "); sbCheckNewAccountID.Append("Order By CountMonth DESC,CountDay DESC"); DataSet dsTemp = UtilityClass.GetSQLResult(sbCheckNewAccountID.ToString()); if (dsTemp.Tables["Result"].Rows.Count > 0) { //當日已經有取得傳票編號 intMaxID = Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountMax"].ToString()) + 1; strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountYear"]).ToString("0000"); //年碼 strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountMonth"]).ToString("00"); //月碼 strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountDay"]).ToString("00"); //日碼 strNewAccountID += intMaxID.ToString("000"); //流水碼 //更新傳票編號 sbUpdateNewAccountID.Append("Update OTB_FNC_AccountingMaxNumber Set CountMax = CountMax + 1 "); sbUpdateNewAccountID.Append(", ModifyUser = '" + strActiveUserID + "', ModifyDate = Getdate() "); sbUpdateNewAccountID.Append("Where AccountingBookID = '" + strAccountingBookID + "' "); sbUpdateNewAccountID.Append("And CountYear = '" + dsTemp.Tables["Result"].Rows[0]["CountYear"].ToString() + "'"); sbUpdateNewAccountID.Append("And CountMonth = '" + dsTemp.Tables["Result"].Rows[0]["CountMonth"].ToString() + "'"); sbUpdateNewAccountID.Append("And CountDay = '" + dsTemp.Tables["Result"].Rows[0]["CountDay"].ToString() + "'"); UtilityClass.RunSQLNonReturn(sbUpdateNewAccountID.ToString()); } else { //當日未取過傳票編號 intMaxID = 1; strNewAccountID += DateTime.Now.Year.ToString("0000"); //年碼 strNewAccountID += DateTime.Now.Month.ToString("00"); //月碼 strNewAccountID += DateTime.Now.Day.ToString("00"); //日碼 strNewAccountID += intMaxID.ToString("000"); //流水碼 //新增傳票編號 sbUpdateNewAccountID.Append("Insert into OTB_FNC_AccountingMaxNumber(AccountingBookID, CountYear, CountMonth, CountDay, CountMax, Memo,CreateUser, CreateDate, ModifyUser, ModifyDate) "); sbUpdateNewAccountID.Append("Values ('" + strAccountingBookID + "',Year(getdate()),Month(getdate()),day(getdate()),1,null,'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "',Getdate())"); UtilityClass.RunSQLNonReturn(sbUpdateNewAccountID.ToString()); } return strNewAccountID; } private void AddEvent(bool isNew) //新增事件 { try { //宣告物件 string strNewAccountingID = ""; if(isNew) //判斷是否為 { strNewAccountingID = GetNewAccountID(); } else { strNewAccountingID = strPKey; } int intDataCount = dgvDataMaintain.Rows.Count - 1; StringBuilder strSQL = new StringBuilder(); int intOrder = 0; //分錄排序欄位 string strAccountingSubID = ""; //科目代號 string strAccountingSubName = ""; //科目名稱 string strMemo = ""; //備註 string strDebit = ""; //借方金額 string strCredit = ""; //貸方金額 string strProjectNumber = ""; //專案代碼 string strProjectCName = ""; //專案名稱 string strClosedAccounting = ""; //是否為關帳分錄 foreach(DataGridViewRow dgRow in dgvDataMaintain.Rows) { //設定初始值 intOrder++; strAccountingSubID = ((string)dgRow.Cells["cAccountingSubID"].Value); //科目代號 strAccountingSubName = ((string)dgRow.Cells["cAccountingSubName"].Value); //科目名稱 strMemo = ((string)dgRow.Cells["cMemo"].Value); //備註 strDebit = (string)dgRow.Cells["cDebit"].Value; //借方金額 strCredit = (string)dgRow.Cells["cCredit"].Value; //貸方金額 strProjectNumber = (string)dgRow.Cells["cProjectID"].Value; //專案代碼 strProjectCName = (string)dgRow.Cells["cProjectName"].Value; //專案代碼 strClosedAccounting = (string)dgRow.Cells["cClosedAccounting"].Value; //關帳分錄 if (!string.IsNullOrEmpty(strAccountingSubID)) { string strChackDate = dpAccountingDate.Text == "" ? DateTime.Now.ToString() : dpAccountingDate.Text; if (string.IsNullOrEmpty(strDebit)) { strDebit = "";} //處理借方金額為null值 if (string.IsNullOrEmpty(strCredit)) { strCredit = ""; } //處理貸方金額為null值 strSQL.Clear(); strSQL.Append("Insert Into OTB_FNC_AccountingJournal(AccountingBookID, AccountingID, AccountingDate, AccountingOrder, AccountingSubID, AccountingSubName, ProjectNumber, ProjectCName, Memo, Debit, Credit, ClosedAccounting, CreateDate, CreateUser, ModifyDate, ModifyUser)"); strSQL.Append(" Values ('" + strAccountingBookID + "','" + strNewAccountingID + "'," ); strSQL.Append("Convert(DateTime,'" + Convert.ToDateTime(strChackDate).ToString("yyyy/MM/dd HH:mm:ss") + "'),"); strSQL.Append(intOrder.ToString() + ","); strSQL.Append("'" + strAccountingSubID + "',"); strSQL.Append("'" + strAccountingSubName + "',"); strSQL.Append("'" + strProjectNumber + "',"); strSQL.Append("'" + strProjectCName + "',"); strSQL.Append("'" + strMemo + "',"); strSQL.Append("'" + (strDebit == "" ? "" : UtilityClass.EncryptDES(strDebit.Replace(",",""), strKey)) + "',"); strSQL.Append("'" + (strCredit == "" ? "" : UtilityClass.EncryptDES(strCredit.Replace(",", ""), strKey)) + "',"); strSQL.Append("'" + (strClosedAccounting == "" ? "" : strClosedAccounting + "',")); strSQL.Append("Convert(DateTime,'" + Convert.ToDateTime(strChackDate).ToString("yyyy/MM/dd HH:mm:ss") + "'),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "') "); //添加參數 sqlAdapter.InsertCommand = new SqlCommand(); sqlAdapter.InsertCommand.Connection = sqlConn; sqlAdapter.InsertCommand.CommandText = strSQL.ToString(); sqlAdapter.InsertCommand.Transaction = sqlTran; if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.InsertCommand.ExecuteNonQuery(); } } } catch (Exception ex) { throw ex; } } private void AddEvent(string strYear) //新增某一個年度的分錄 { try { //宣告物件 string strNewAccountingID = ""; if (strYear == "") //判斷是否為 { strNewAccountingID = GetNewAccountID(); } else { strNewAccountingID = GetNewAccountID(strYear); } int intDataCount = dgvDataMaintain.Rows.Count - 1; StringBuilder strSQL = new StringBuilder(); int intOrder = 0; //分錄排序欄位 string strAccountingSubID = ""; //科目代號 string strAccountingSubName = ""; //科目名稱 string strMemo = ""; //備註 string strDebit = ""; //借方金額 string strCredit = ""; //貸方金額 string strProjectNumber = ""; //專案代碼 string strProjectCName = ""; //專案名稱 string strClosedAccounting = ""; //是否為關帳分錄 foreach (DataGridViewRow dgRow in dgvDataMaintain.Rows) { //設定初始值 intOrder++; strAccountingSubID = ((string)dgRow.Cells["cAccountingSubID"].Value); //科目代號 strAccountingSubName = ((string)dgRow.Cells["cAccountingSubName"].Value); //科目名稱 strMemo = ((string)dgRow.Cells["cMemo"].Value); //備註 strDebit = (string)dgRow.Cells["cDebit"].Value; //借方金額 strCredit = (string)dgRow.Cells["cCredit"].Value; //貸方金額 strProjectNumber = (string)dgRow.Cells["cProjectID"].Value; //專案代碼 strProjectCName = (string)dgRow.Cells["cProjectName"].Value; //專案代碼 strClosedAccounting = (string)dgRow.Cells["cClosedAccounting"].Value; //關帳分錄 if (!string.IsNullOrEmpty(strAccountingSubID)) { string strChackDate = dpAccountingDate.Text == "" ? DateTime.Now.ToString() : dpAccountingDate.Text; if (string.IsNullOrEmpty(strDebit)) { strDebit = ""; } //處理借方金額為null值 if (string.IsNullOrEmpty(strCredit)) { strCredit = ""; } //處理貸方金額為null值 strSQL.Clear(); strSQL.Append("Insert Into OTB_FNC_AccountingJournal(AccountingBookID, AccountingID, AccountingDate, AccountingOrder, AccountingSubID, AccountingSubName, ProjectNumber, ProjectCName, Memo, Debit, Credit, ClosedAccounting, CreateDate, CreateUser, ModifyDate, ModifyUser)"); strSQL.Append(" Values ('" + strAccountingBookID + "','" + strNewAccountingID + "',"); strSQL.Append("Convert(DateTime,'" + Convert.ToDateTime(strNewAccountingID.Substring(0, 4) + "/" + strNewAccountingID.Substring(4, 2) + "/" + strNewAccountingID.Substring(6, 2) + " 23:59:59").ToString("yyyy/MM/dd HH:mm:ss") + "'),"); strSQL.Append(intOrder.ToString() + ","); strSQL.Append("'" + strAccountingSubID + "',"); strSQL.Append("'" + strAccountingSubName + "',"); strSQL.Append("'" + strProjectNumber + "',"); strSQL.Append("'" + strProjectCName + "',"); strSQL.Append("'" + strMemo + "',"); strSQL.Append("'" + (strDebit == "" ? "" : UtilityClass.EncryptDES(strDebit.Replace(",", ""), strKey)) + "',"); strSQL.Append("'" + (strCredit == "" ? "" : UtilityClass.EncryptDES(strCredit.Replace(",", ""), strKey)) + "',"); strSQL.Append("'" + (strClosedAccounting == "" ? "" : strClosedAccounting + "',")); strSQL.Append("Convert(DateTime,'" + Convert.ToDateTime(strChackDate).ToString("yyyy/MM/dd HH:mm:ss") + "'),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "') "); //添加參數 sqlAdapter.InsertCommand = new SqlCommand(); sqlAdapter.InsertCommand.Connection = sqlConn; sqlAdapter.InsertCommand.CommandText = strSQL.ToString(); sqlAdapter.InsertCommand.Transaction = sqlTran; if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.InsertCommand.ExecuteNonQuery(); } } } catch (Exception ex) { throw ex; } } private void DelEvent() //刪除事件 { try { //宣告物件 string strSQL = string.Format("Delete OTB_FNC_AccountingJournal Where AccountingID = '{0}' And AccountingBookID = '{1}'", strPKey,strAccountingBookID); //添加參數 sqlAdapter.DeleteCommand = new SqlCommand(); sqlAdapter.DeleteCommand.Connection = sqlConn; sqlAdapter.DeleteCommand.CommandText = strSQL.ToString(); sqlAdapter.DeleteCommand.Transaction = sqlTran; if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.DeleteCommand.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } } public void SaveEvent() //儲存事件 { //進行資料整理 switch (strFrmStatus) { case "ADD": //新增事件 try { if (CheckForm()) //資料確認 { if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } if(sqlTran == null) { sqlTran = sqlConn.BeginTransaction(); //開始交易 } AddEvent(true); //進行資料的新增(取新傳票編號) StatusChange("NONE"); switch (strOwnerForm) { case "AccountingJournalMaintain": sqlTran.Commit(); ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態 ((AccountingJournalMaintain)this.Owner).GoEvent(); //進行母Form查詢功能 break; case "AccountingJournalMaintain_Reversal": sqlTran.Commit(); ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態 ((AccountingJournalMaintain)this.Owner).GoEvent(); //進行母Form查詢功能 break; case "PayPlanManagement_Receive": ((PayPlanManagement)this.Owner).blIsAdded = true; ((PayPlanManagement)this.Owner).CreateAR(dgeArgs); //增加應收帳款記錄 ((PayPlanManagement)this.Owner).GoEvent(); //進行母Form儲存功能 sqlTran.Commit(); break; case "PayPlanManagement_Pay": ((PayPlanManagement)this.Owner).blIsAdded = true; ((PayPlanManagement)this.Owner).CreateAP(dgeArgs); //增加應付帳款記錄 ((PayPlanManagement)this.Owner).GoEvent(); //進行母Form儲存功能 sqlTran.Commit(); break; case "AccountsReceivable": ((AccountsReceivable)this.Owner).blIsAdded = true; ((AccountsReceivable)this.Owner).UpdateAR(dgeArgs); //更新母Form的收款狀況 ((AccountsReceivable)this.Owner).StatusChange("SEARCH"); ((AccountsReceivable)this.Owner).GoEvent(); //進行母Form儲存功能 sqlTran.Commit(); break; case "AccountsPay": ((AccountsPayable)this.Owner).blIsAdded = true; ((AccountsPayable)this.Owner).UpdateAP(dgeArgs); //更新母Form的付款狀況 ((AccountsPayable)this.Owner).StatusChange("SEARCH"); ((AccountsPayable)this.Owner).GoEvent(); //進行母Form儲存功能 sqlTran.Commit(); break; case "AccountingOpeningEntry": sqlTran.Commit(); break; } this.Close(); } } catch (Exception ex) { sqlTran.Rollback(); MessageBox.Show("資料新增失敗", "提示"); ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex); } break; case "CLOSE": //關帳事件 try { if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } if (sqlTran == null) { sqlTran = sqlConn.BeginTransaction(); //開始交易 } AddEvent(strAccountingYear); //進行資料的新增(取新傳票編號) //AddEvent(true); StatusChange("NONE"); switch (strOwnerForm) { case "AccountingOpeningEntry": sqlTran.Commit(); break; } this.Close(); } catch (Exception ex) { sqlTran.Rollback(); MessageBox.Show("關帳失敗", "提示"); ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex); } break; case "MODIFY": //修改事件 try { if (CheckForm()) //資料確認 { if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlTran = sqlConn.BeginTransaction(); //開始交易 DelEvent(); //先將資料進行刪除 AddEvent(false); //將資料重新新增(但不取新傳票編號) sqlTran.Commit(); MessageBox.Show("資料修改成功", "提示"); StatusChange("NONE"); ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態 ((AccountingJournalMaintain)this.Owner).GoEvent(); //進行母Form查詢功能 this.Close(); } } catch (Exception ex) { sqlTran.Rollback(); MessageBox.Show("資料修改失敗", "提示"); ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex); } break; case "DEL": //刪除事件 try { if (MessageBox.Show("請問您是否確認刪除本分錄資料", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlTran = sqlConn.BeginTransaction(); //開始交易 DelEvent(); //將資料進行刪除 sqlTran.Commit(); MessageBox.Show("資料刪除成功", "提示"); StatusChange("NONE"); ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態 ((AccountingJournalMaintain)this.Owner).GoEvent(); //進行母Form查詢功能 this.Close(); } } catch (Exception ex) { sqlTran.Rollback(); MessageBox.Show("資料刪除失敗", "提示"); ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex); } break; } } public void ReturnProjectInfo(string[] strProjectInfo) { txtProjectName.Text = strProjectInfo[1].ToString(); txtProjectNumber.Text = strProjectInfo[0].ToString(); } public void ReturnProjectInfo(int intIndexID, string[] strProjectInfo) { dgvDataMaintain.Rows[intIndexID].Cells["cProjectID"].Value = strProjectInfo[0].ToString(); dgvDataMaintain.Rows[intIndexID].Cells["cProjectName"].Value = strProjectInfo[1].ToString(); } #endregion #region 事件觸發及問題處理 private void btnSave_Click(object sender, EventArgs e) { SaveEvent(); } private void btnCancle_Click(object sender, EventArgs e) { CleanForm(); } private void btnClose_Click(object sender, EventArgs e) { ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態 this.Close(); } private void AccountingEntries_Load(object sender, EventArgs e) { CleanForm(); switch (strOwnerForm) { case "AccountingJournalMaintain": label4.Visible = false; txtProjectName.Visible = false; txtProjectNumber.Visible = false; btnGetProject.Visible = false; break; } } private void dgvDataMaintain_CellEndEdit(object sender, DataGridViewCellEventArgs e) { //物件宣告 string strAccountingSubID = ""; string strAccountingSubName = ""; string strDebit = ""; string strCredit = ""; string strProjectName = ""; //Cell[0] : 選窗Button //Cell[1] : 科目代碼(cAccountingSubID) //Cell[2] : 科目名稱(cAccountingSubName) //Cell[3] : 摘要(cMemo) //Cell[4] : 借方金額(cDebit) //Cell[5] : 貸方金額(cCredit) //Cell[6] : 選窗Button //Cell[7] : 專案名稱(cProjectName) //Cell[8] : 專案代碼(cProjectID) //科目代碼變動 if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cAccountingSubID") { strAccountingSubID = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value; 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 = UtilityClass.GetAccountingSubjectByID(strAccountingSubID, strAccountingBookID).Rows[0]["AccountingSubName"].ToString(); dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value = strAccountingSubName; } else { PickAccountingSubject pkItemForm = new PickAccountingSubject(); pkItemForm.txtAccountingSubID.Text = strAccountingSubID; pkItemForm.intIndex = e.RowIndex; pkItemForm.strOwnerForm = "AccountingEntries"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } } } //科目名稱變動 if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cAccountingSubName") { strAccountingSubName = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value; 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 = UtilityClass.GetAccountingSubjectByName(strAccountingSubName).Rows[0]["AccountingSubID"].ToString(); dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value = strAccountingSubID; } else { PickAccountingSubject pkItemForm = new PickAccountingSubject(); pkItemForm.txtAccountingSubName.Text = strAccountingSubName; pkItemForm.intIndex = e.RowIndex; pkItemForm.strOwnerForm = "AccountingEntries"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } } } //數字呈現方式調整 if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cDebit") { strDebit = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value; if (string.IsNullOrEmpty(strDebit)) { return; } else { strDebit = strDebit.Replace(",", ""); dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value = UtilityClass.MarkNumber(strDebit); } } if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cCredit") { strCredit = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value; if (string.IsNullOrEmpty(strCredit)) { return; } else { strCredit = strCredit.Replace(",", ""); dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value = UtilityClass.MarkNumber(strCredit); } } if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cProjectName") { strProjectName = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cProjectName"].Value; if (string.IsNullOrEmpty(strProjectName)) { dgvDataMaintain.Rows[e.RowIndex].Cells["cProjectID"].Value = ""; } } } private void dgvDataMaintain_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { //物件宣告 string strErrorMsg = ""; //Cell[0] : 選窗Button //Cell[1] : 科目代碼(cAccountingSubID) //Cell[2] : 科目名稱(cAccountingSubName) //Cell[3] : 摘要(cMemo) //Cell[4] : 借方金額(cDebit) //Cell[5] : 貸方金額(cCredit) if (! ( string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value) & string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value) & string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value) & string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value)) ) //判斷是否所有條件均為未填 { if (string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value)) { strErrorMsg = "科目代碼不得為空白"; } if (string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value)) { strErrorMsg = "科目名稱不得為空白"; } if (string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value) & string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value)) { strErrorMsg = "借貸方不得均為空白"; } } if (strErrorMsg != "") { dgvDataMaintain.Rows[e.RowIndex].ErrorText = strErrorMsg; e.Cancel = true; } } private void dgvDataMaintain_CellValidated(object sender, DataGridViewCellEventArgs e) { dgvDataMaintain.Rows[e.RowIndex].ErrorText = ""; ReSum(); } private void dgvDataMaintain_CellContentClick(object sender, DataGridViewCellEventArgs e) { //當GridView中的Button被按下 var SenderGrid = (DataGridView)sender; if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && SenderGrid.Columns[e.ColumnIndex].Name == "cAdd" && e.RowIndex >= 0) { PickAccountingSubject pkItemForm = new PickAccountingSubject(); pkItemForm.intIndex = e.RowIndex; pkItemForm.strOwnerForm = "AccountingEntries"; pkItemForm.Owner = this; pkItemForm.strAccountingBookID = strAccountingBookID; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && SenderGrid.Columns[e.ColumnIndex].Name == "cAddProject" && e.RowIndex >= 0) { PickProject pkItemForm = new PickProject(); pkItemForm.intIndex = e.RowIndex; pkItemForm.strOwnerForm = "AccountingEntries_Detail"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } } private void dgvDataMaintain_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { //物件宣告 string strDebit = ""; string strCredit = ""; //Cell[0] : 選窗Button //Cell[1] : 科目代碼(cAccountingSubID) //Cell[2] : 科目名稱(cAccountingSubName) //Cell[3] : 摘要(cMemo) //Cell[4] : 借方金額(cDebit) //Cell[5] : 貸方金額(cCredit) //數字呈現方式調整 if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cDebit") { strDebit = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value; if (string.IsNullOrEmpty(strDebit)) { return; } else { dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value = strDebit.Replace(",", ""); } } if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cCredit") { strCredit = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value; if (string.IsNullOrEmpty(strCredit)) { return; } else { dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value = strCredit.Replace(",", ""); } } } private void btnGetProject_Click(object sender, EventArgs e) { PickProject pkItemForm = new PickProject(); pkItemForm.txtProjectName.Text = txtProjectName.Text.Trim(); pkItemForm.strOwnerForm = "AccountingEntries"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } #endregion } }