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 System.Configuration; using System.IO; using System.Xml.Linq; using ManagementSystem.Utility; namespace ManagementSystem { public partial class AccountingOpeningEntry : Form { //程式內共用物件 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 AccountingOpeningEntry() { InitializeComponent(); } #region 自定義程式 private void SetupStatus() //畫面載入設定 { try { strActiveUserID = MainForm.strActiveUserID; strAccountingBookID = MainForm.strAccountingBookID; strKey = MainForm.strKey; string strCheckSQL = "Select * From OTB_FNC_AccountingBookStatus Where AccountingBookID = '" + strAccountingBookID + "' And AccountingYear = Year(GetDate()) And IsOpen = 'Y'"; if (UtilityClass.IsExist(strCheckSQL)) { txtAccountingYear.Text = DateTime.Now.Year.ToString(); btnOpen.Enabled = false; } else { txtAccountingYear.Text = (DateTime.Now.Year - 1).ToString(); btnOpen.Enabled = true; } #region DEL //string strCheckSQL = "Select IsOpen From OTB_FNC_AccountingBookStatus Where AccountingBookID = '" + strAccountingBookID + "' And AccountingYear = '" + txtAccountingYear.Text.Trim() + "'"; //using (DataTable dtTemp = UtilityClass.GetSQLResult(strCheckSQL).Tables["Result"]) //{ // if (dtTemp.Rows.Count > 0) // { // if (dtTemp.Rows[0]["IsOpen"].ToString() == "N") // { // btnClose.Enabled = false; // btnOpen.Enabled = true; // } // else // { // btnClose.Enabled = true; // btnOpen.Enabled = false; // } // } // else // { // btnClose.Enabled = false; // btnOpen.Enabled = true; // } //} #endregion } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex); } } private void AccountingCloseEntryProcess() { //關帳作業 string strSQL = ""; string strCredit = ""; string strDebit = ""; double dbValue = 0; double dbIncomeSum = 0; double dbExpendSum = 0; StringBuilder strAccSQL = new StringBuilder(); string strCurrentProfitAndLossSubID = ""; //本期損益科目編號 string strCurrentProfitAndLossSubName = ""; //本期損益科目名稱 string strCumulativeProfitAndLossSubID = ""; //累計盈虧科目編號 string strCumulativeProfitAndLossSubName = ""; //累計盈虧科目名稱 //載入本期損益科目 string strXMLPathCurrent = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + "AccountingOpeningEntry.xml"; if (File.Exists(strXMLPathCurrent)) { XDocument xmlContent = XDocument.Load(strXMLPathCurrent); foreach (XElement xmlData in xmlContent.Descendants("Accounting")) { strCurrentProfitAndLossSubID = xmlData.Element("SubAccounting").Value.ToString(); strCurrentProfitAndLossSubName = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString(); break; } } //載入累計盈虧科目 string strXMLPathCumulative = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + "AccountingOpeningEntry_Cumulative.xml"; if (File.Exists(strXMLPathCumulative)) { XDocument xmlContent = XDocument.Load(strXMLPathCumulative); foreach (XElement xmlData in xmlContent.Descendants("Accounting")) { strCumulativeProfitAndLossSubID = xmlData.Element("SubAccounting").Value.ToString(); strCumulativeProfitAndLossSubName = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString(); break; } } # region 結清收入會計科目 try { //結帳作業 // 1. 結清虛帳戶(收入、費用) // 1.1. 結清收入會計科目 strSQL = "Select * From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'I' And DCClass = 'C' Order By AccountingSubID"; AccountingEntries acItemForm = new AccountingEntries(); acItemForm.strOwnerForm = "AccountingOpeningEntry"; acItemForm.Owner = this; acItemForm.StartPosition = FormStartPosition.CenterParent; acItemForm.strFrmStatus = "CLOSE"; acItemForm.strKey = strKey; acItemForm.strAccountingBookID = strAccountingBookID; acItemForm.strActiveUserID = strActiveUserID; acItemForm.strAccountingYear = txtAccountingYear.Text.Trim(); acItemForm.dpAccountingDate.Text = txtAccountingYear.Text.Trim() + "/12/31 23:59:59"; acItemForm.Text = "收入結算分錄"; using (DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"]) { pbProcessedAccounting.Maximum = dtTemp.Rows.Count; foreach (DataRow drAcc in dtTemp.Rows) { dbValue = 0; strAccSQL.Clear(); strAccSQL.Append("Select * From OTB_FNC_AccountingJournal Where AccountingBookID = '" + strAccountingBookID + "' "); strAccSQL.Append("And AccountingSubID IN (Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'I' And DCClass = 'C') And AccountingSubID =" + drAcc["AccountingSubID"].ToString() + " And AccountingID Like '" + txtAccountingYear.Text.Trim() + "%'"); using (DataTable dtAccSum = UtilityClass.GetSQLResult(strAccSQL.ToString()).Tables["Result"]) { if (dtAccSum.Rows.Count > 0) { foreach (DataRow drAccSum in dtAccSum.Rows) { strCredit = UtilityClass.DecryptDES(drAccSum["Credit"].ToString(), strKey).Replace(",", ""); strDebit = UtilityClass.DecryptDES(drAccSum["Debit"].ToString(), strKey).Replace(",", ""); dbValue += Convert.ToDouble(string.IsNullOrEmpty(strCredit) ? "0" : strCredit); dbValue -= Convert.ToDouble(string.IsNullOrEmpty(strDebit) ? "0" : strDebit); } dbIncomeSum += dbValue; //計算本期損益總額 if (dbValue > 0) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(acItemForm.dgvDataMaintain); dgvRow.Cells[1].Value = drAcc["AccountingSubID"].ToString(); dgvRow.Cells[2].Value = drAcc["AccountingSubName"].ToString(); dgvRow.Cells[3].Value = "結轉分錄"; dgvRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); dgvRow.Cells[9].Value = "Y"; acItemForm.dgvDataMaintain.Rows.Add(dgvRow); } } } pbProcessedAccounting.Value += 1; } } if (dbIncomeSum > 0) { string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + ".xml"; if (File.Exists(strXMLPath)) { XDocument xmlContent = XDocument.Load(strXMLPath); foreach (XElement xmlData in xmlContent.Descendants("Accounting")) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(acItemForm.dgvDataMaintain); dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString(); dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString(); dgvRow.Cells[3].Value = "結轉分錄"; dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbIncomeSum); dgvRow.Cells[9].Value = "Y"; acItemForm.dgvDataMaintain.Rows.Add(dgvRow); break; } } acItemForm.SaveEvent(); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex); MessageBox.Show("收入資料結算失敗", "提示"); } #endregion #region 結清費用會計科目 try { //結帳作業 // 1. 結清虛帳戶(收入、費用) // 1.2. 結清費用會計科目 dbValue = 0; strSQL = "Select * From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'E' And DCClass = 'D' Order By AccountingSubID"; AccountingEntries acItemForm = new AccountingEntries(); acItemForm.strOwnerForm = "AccountingOpeningEntry"; acItemForm.Owner = this; acItemForm.StartPosition = FormStartPosition.CenterParent; acItemForm.strFrmStatus = "CLOSE"; acItemForm.strKey = strKey; acItemForm.strAccountingBookID = strAccountingBookID; acItemForm.strActiveUserID = strActiveUserID; acItemForm.strAccountingYear = txtAccountingYear.Text.Trim(); acItemForm.dpAccountingDate.Text = txtAccountingYear.Text.Trim() + "/12/31 23:59:59"; acItemForm.Text = "費用結算分錄"; using (DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"]) { pbProcessedAccounting.Value = 0; pbProcessedAccounting.Maximum = dtTemp.Rows.Count; foreach (DataRow drAcc in dtTemp.Rows) { dbValue = 0; strAccSQL.Clear(); strAccSQL.Append("Select * From OTB_FNC_AccountingJournal Where AccountingBookID = '" + strAccountingBookID + "' "); strAccSQL.Append("And AccountingSubID IN (Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'E' And DCClass = 'D') And AccountingSubID =" + drAcc["AccountingSubID"].ToString() + " And AccountingID Like '" + txtAccountingYear.Text.Trim() + "%'"); using (DataTable dtAccSum = UtilityClass.GetSQLResult(strAccSQL.ToString()).Tables["Result"]) { if (dtAccSum.Rows.Count > 0) { foreach (DataRow drAccSum in dtAccSum.Rows) { strCredit = UtilityClass.DecryptDES(drAccSum["Credit"].ToString(), strKey).Replace(",", ""); strDebit = UtilityClass.DecryptDES(drAccSum["Debit"].ToString(), strKey).Replace(",", ""); dbValue += Convert.ToDouble(string.IsNullOrEmpty(strDebit) ? "0" : strDebit); dbValue -= Convert.ToDouble(string.IsNullOrEmpty(strCredit) ? "0" : strCredit); } dbExpendSum += dbValue; //計算本期損益總額 if (dbValue > 0) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(acItemForm.dgvDataMaintain); dgvRow.Cells[1].Value = drAcc["AccountingSubID"].ToString(); dgvRow.Cells[2].Value = drAcc["AccountingSubName"].ToString(); dgvRow.Cells[3].Value = "結轉分錄"; dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); dgvRow.Cells[9].Value = "Y"; acItemForm.dgvDataMaintain.Rows.Add(dgvRow); } } } pbProcessedAccounting.Value += 1; } } if (dbExpendSum > 0) { string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + ".xml"; if (File.Exists(strXMLPath)) { XDocument xmlContent = XDocument.Load(strXMLPath); foreach (XElement xmlData in xmlContent.Descendants("Accounting")) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(acItemForm.dgvDataMaintain); dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString(); dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString(); dgvRow.Cells[3].Value = "結轉分錄"; dgvRow.Cells[4].Value = UtilityClass.MarkNumber(dbExpendSum); dgvRow.Cells[9].Value = "Y"; acItemForm.dgvDataMaintain.Rows.Add(dgvRow); break; } } acItemForm.SaveEvent(); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex); MessageBox.Show("費用資料結算失敗", "提示"); } #endregion #region 本期損益轉保留盈餘 //就會計的做法,非必要執行 #endregion #region 進行實帳戶結清 try { //結帳作業 // 2. 結轉實帳戶(A:資產、L:負債、O:股東權限) strSQL = "Select * From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass IN ('A','L','O') Order By AccountingClass, AccountingSubID"; //資產結算分錄 AccountingEntries acItemForm = new AccountingEntries(); acItemForm.strOwnerForm = "AccountingOpeningEntry"; acItemForm.Owner = this; acItemForm.StartPosition = FormStartPosition.CenterParent; acItemForm.strFrmStatus = "CLOSE"; acItemForm.strKey = strKey; acItemForm.strAccountingBookID = strAccountingBookID; acItemForm.strActiveUserID = strActiveUserID; acItemForm.strAccountingYear = txtAccountingYear.Text.Trim(); acItemForm.Text = "資產結算分錄"; //結清本期損益 AccountingEntries acItemCloseForm = new AccountingEntries(); acItemCloseForm.strOwnerForm = "AccountingOpeningEntry"; acItemCloseForm.Owner = this; acItemCloseForm.StartPosition = FormStartPosition.CenterParent; acItemCloseForm.strFrmStatus = "CLOSE"; acItemCloseForm.strKey = strKey; acItemCloseForm.strAccountingBookID = strAccountingBookID; acItemCloseForm.strActiveUserID = strActiveUserID; acItemCloseForm.strAccountingYear = txtAccountingYear.Text.Trim(); acItemCloseForm.Text = "本期損益結算分錄"; //期初分錄 AccountingEntries acNewItemForm = new AccountingEntries(); acNewItemForm.strOwnerForm = "AccountingOpeningEntry"; acNewItemForm.Owner = this; acNewItemForm.StartPosition = FormStartPosition.CenterParent; acNewItemForm.strFrmStatus = "ADD"; acNewItemForm.strKey = strKey; acNewItemForm.strAccountingBookID = strAccountingBookID; acNewItemForm.strActiveUserID = strActiveUserID; acNewItemForm.Text = "期初轉入"; using (DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"]) { pbProcessedAccounting.Value = 0; pbProcessedAccounting.Maximum = dtTemp.Rows.Count; foreach (DataRow drAcc in dtTemp.Rows) { dbValue = 0; strAccSQL.Clear(); strAccSQL.Append("Select * From OTB_FNC_AccountingJournal Where AccountingBookID = '" + strAccountingBookID + "' "); strAccSQL.Append("And AccountingSubID IN (Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID +"' And AccountingClass IN ('A','L','O')) And AccountingSubID =" + drAcc["AccountingSubID"].ToString() + " And AccountingID Like '" + txtAccountingYear.Text.Trim() + "%'"); using (DataTable dtAccSum = UtilityClass.GetSQLResult(strAccSQL.ToString()).Tables["Result"]) { if (dtAccSum.Rows.Count > 0) { foreach (DataRow drAccSum in dtAccSum.Rows) { strCredit = UtilityClass.DecryptDES(drAccSum["Credit"].ToString(), strKey).Replace(",", ""); //貸方 strDebit = UtilityClass.DecryptDES(drAccSum["Debit"].ToString(), strKey).Replace(",", ""); //借方 dbValue += Convert.ToDouble(string.IsNullOrEmpty(strDebit) ? "0" : strDebit); dbValue -= Convert.ToDouble(string.IsNullOrEmpty(strCredit) ? "0" : strCredit); } dbExpendSum += dbValue; //計算本期損益總額 DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(acItemForm.dgvDataMaintain); dgvRow.Cells[1].Value = drAcc["AccountingSubID"].ToString(); dgvRow.Cells[2].Value = drAcc["AccountingSubName"].ToString(); dgvRow.Cells[3].Value = "結轉分錄"; dgvRow.Cells[9].Value = "Y"; //結清本期損益 Start if (drAcc["AccountingSubID"].ToString() == strCurrentProfitAndLossSubID) { DataGridViewRow dgvRowCurrProfitAndLoss = new DataGridViewRow(); dgvRowCurrProfitAndLoss.CreateCells(acItemCloseForm.dgvDataMaintain); dgvRowCurrProfitAndLoss.Cells[1].Value = strCurrentProfitAndLossSubID; dgvRowCurrProfitAndLoss.Cells[2].Value = strCurrentProfitAndLossSubName; dgvRowCurrProfitAndLoss.Cells[3].Value = "結轉分錄"; dgvRowCurrProfitAndLoss.Cells[9].Value = "Y"; if (drAcc["DCClass"].ToString() == "D") { if (dbValue > 0) { dgvRowCurrProfitAndLoss.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄 } else { dgvRowCurrProfitAndLoss.Cells[4].Value = UtilityClass.MarkNumber(dbValue * -1); //結轉分錄 } } else { if (dbValue > 0) { dgvRowCurrProfitAndLoss.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄 } else { dgvRowCurrProfitAndLoss.Cells[4].Value = UtilityClass.MarkNumber(dbValue * -1); //結轉分錄 } } acItemCloseForm.dgvDataMaintain.Rows.Add(dgvRowCurrProfitAndLoss); DataGridViewRow dgvRowCumuProfitAndLoss = new DataGridViewRow(); dgvRowCumuProfitAndLoss.CreateCells(acItemCloseForm.dgvDataMaintain); dgvRowCumuProfitAndLoss.Cells[1].Value = strCumulativeProfitAndLossSubID; dgvRowCumuProfitAndLoss.Cells[2].Value = strCumulativeProfitAndLossSubName; dgvRowCumuProfitAndLoss.Cells[3].Value = "結轉分錄"; dgvRowCumuProfitAndLoss.Cells[9].Value = "Y"; if (drAcc["DCClass"].ToString() == "D") { if (dbValue > 0) { dgvRowCumuProfitAndLoss.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄 } else { dgvRowCumuProfitAndLoss.Cells[4].Value = UtilityClass.MarkNumber(dbValue * -1); //結轉分錄 } } else { if (dbValue > 0) { dgvRowCumuProfitAndLoss.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄 } else { dgvRowCumuProfitAndLoss.Cells[5].Value = UtilityClass.MarkNumber(dbValue * -1); //結轉分錄 } } acItemCloseForm.dgvDataMaintain.Rows.Add(dgvRowCumuProfitAndLoss); } //結清本期損益 End DataGridViewRow dgvNewRow = new DataGridViewRow(); dgvNewRow.CreateCells(acNewItemForm.dgvDataMaintain); dgvNewRow.Cells[1].Value = drAcc["AccountingSubID"].ToString(); dgvNewRow.Cells[2].Value = drAcc["AccountingSubName"].ToString(); dgvNewRow.Cells[3].Value = "期初轉入"; if (drAcc["DCClass"].ToString() == "D") { if (dbValue > 0) { dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄 dgvNewRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //期初轉入 } else { dbValue = dbValue * -1; dgvRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄 dgvNewRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //期初轉入 } } else { if (dbValue > 0) { dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄 dgvNewRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //期初轉入 } else { dbValue = dbValue * -1; dgvRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄 dgvNewRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //期初轉入 } } strCredit = string.IsNullOrEmpty((string)dgvRow.Cells[4].Value) ? "0" : dgvRow.Cells[4].Value.ToString(); strDebit = string.IsNullOrEmpty((string)dgvRow.Cells[5].Value) ? "0" : dgvRow.Cells[5].Value.ToString(); if (strCredit != "0" || strDebit != "0") { acItemForm.dgvDataMaintain.Rows.Add(dgvRow); } strCredit = string.IsNullOrEmpty((string)dgvNewRow.Cells[4].Value) ? "0" : dgvNewRow.Cells[4].Value.ToString(); strDebit = string.IsNullOrEmpty((string)dgvNewRow.Cells[5].Value) ? "0" : dgvNewRow.Cells[5].Value.ToString(); if (strCredit != "0" || strDebit != "0") { acNewItemForm.dgvDataMaintain.Rows.Add(dgvNewRow); } } } pbProcessedAccounting.Value += 1; } } acItemForm.SaveEvent(); acItemCloseForm.SaveEvent(); acNewItemForm.SaveEvent(); //acItemForm.ShowDialog(); //acItemCloseForm.ShowDialog(); //acNewItemForm.ShowDialog(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex); MessageBox.Show("實帳戶結算失敗", "提示"); } #endregion string strCloseAccBookSQL = "Update OTB_FNC_AccountingBookStatus Set IsOpen = 'N' Where AccountingBookID ='" + strAccountingBookID + "' And AccountingYear = '" + txtAccountingYear.Text.Trim() + "'"; UtilityClass.RunSQLNonReturn(strCloseAccBookSQL); //執行關帳 MessageBox.Show("關帳成功!", "提示"); this.Close(); } private void AccountingOpenEntryProcess() { //開帳作業 try { string strSQL = "Insert Into OTB_FNC_AccountingBookStatus(AccountingBookID, AccountingYear, IsOpen, CreateDate, CreateUser, ModifyDate, ModifyUser) Values ('" + strAccountingBookID + "', '" + DateTime.Now.Year.ToString() + "', 'Y', Getdate(), '" + strActiveUserID + "', Getdate(), '" + strActiveUserID + "')"; UtilityClass.RunSQLNonReturn(strSQL); this.Close(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex); MessageBox.Show("開帳失敗", "提示"); } } private bool CheckCloseForm() { if (txtAccountingYear.Text.Trim() == "") { MessageBox.Show("會計年度為必填欄位", "提示"); txtAccountingYear.Focus(); return false; } return true; } private bool CheckOpenForm() { return true; } #endregion #region 事件觸發及問題處理 private void AccountingOpeningEntry_Load(object sender, EventArgs e) { SetupStatus(); } private void btnOpen_Click(object sender, EventArgs e) { if (MessageBox.Show("請問是否要進行關帳及開帳作業?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (CheckCloseForm()) { AccountingOpenEntryProcess(); AccountingCloseEntryProcess(); } } } private void tsbSetup_Click(object sender, EventArgs e) { SetOpenAccountingSubID actForm = new SetOpenAccountingSubID(); actForm.StartPosition = FormStartPosition.CenterParent; actForm.ShowDialog(); this.Close(); } private void txtAccountingYear_Leave(object sender, EventArgs e) { SetupStatus(); } #endregion } }