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.

598 lines
31 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 System.Configuration;
  13. using System.IO;
  14. using System.Xml.Linq;
  15. using ManagementSystem.Utility;
  16. namespace ManagementSystem
  17. {
  18. public partial class AccountingOpeningEntry : Form
  19. {
  20. //程式內共用物件
  21. string strKey = ""; //程式內加密的Key值
  22. string strActiveUserID = ""; //取得登入作用的使用者帳號
  23. string strAccountingBookID = ""; //取得作用中的帳本
  24. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  25. SqlTransaction sqlTran;
  26. SqlDataAdapter sqlAdapter = new SqlDataAdapter();
  27. SqlCommand sqlCmd = new SqlCommand();
  28. DataSet sdInsurance = new System.Data.DataSet();
  29. public AccountingOpeningEntry()
  30. {
  31. InitializeComponent();
  32. }
  33. #region 自定義程式
  34. private void SetupStatus() //畫面載入設定
  35. {
  36. try
  37. {
  38. strActiveUserID = MainForm.strActiveUserID;
  39. strAccountingBookID = MainForm.strAccountingBookID;
  40. strKey = MainForm.strKey;
  41. string strCheckSQL = "Select * From OTB_FNC_AccountingBookStatus Where AccountingBookID = '" + strAccountingBookID + "' And AccountingYear = Year(GetDate()) And IsOpen = 'Y'";
  42. if (UtilityClass.IsExist(strCheckSQL))
  43. {
  44. txtAccountingYear.Text = DateTime.Now.Year.ToString();
  45. btnOpen.Enabled = false;
  46. }
  47. else
  48. {
  49. txtAccountingYear.Text = (DateTime.Now.Year - 1).ToString();
  50. btnOpen.Enabled = true;
  51. }
  52. #region DEL
  53. //string strCheckSQL = "Select IsOpen From OTB_FNC_AccountingBookStatus Where AccountingBookID = '" + strAccountingBookID + "' And AccountingYear = '" + txtAccountingYear.Text.Trim() + "'";
  54. //using (DataTable dtTemp = UtilityClass.GetSQLResult(strCheckSQL).Tables["Result"])
  55. //{
  56. // if (dtTemp.Rows.Count > 0)
  57. // {
  58. // if (dtTemp.Rows[0]["IsOpen"].ToString() == "N")
  59. // {
  60. // btnClose.Enabled = false;
  61. // btnOpen.Enabled = true;
  62. // }
  63. // else
  64. // {
  65. // btnClose.Enabled = true;
  66. // btnOpen.Enabled = false;
  67. // }
  68. // }
  69. // else
  70. // {
  71. // btnClose.Enabled = false;
  72. // btnOpen.Enabled = true;
  73. // }
  74. //}
  75. #endregion
  76. }
  77. catch (Exception ex)
  78. {
  79. ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex);
  80. }
  81. }
  82. private void AccountingCloseEntryProcess()
  83. {
  84. //關帳作業
  85. string strSQL = "";
  86. string strCredit = "";
  87. string strDebit = "";
  88. double dbValue = 0;
  89. double dbIncomeSum = 0;
  90. double dbExpendSum = 0;
  91. StringBuilder strAccSQL = new StringBuilder();
  92. string strCurrentProfitAndLossSubID = ""; //本期損益科目編號
  93. string strCurrentProfitAndLossSubName = ""; //本期損益科目名稱
  94. string strCumulativeProfitAndLossSubID = ""; //累計盈虧科目編號
  95. string strCumulativeProfitAndLossSubName = ""; //累計盈虧科目名稱
  96. //載入本期損益科目
  97. string strXMLPathCurrent = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + "AccountingOpeningEntry.xml";
  98. if (File.Exists(strXMLPathCurrent))
  99. {
  100. XDocument xmlContent = XDocument.Load(strXMLPathCurrent);
  101. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  102. {
  103. strCurrentProfitAndLossSubID = xmlData.Element("SubAccounting").Value.ToString();
  104. strCurrentProfitAndLossSubName = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  105. break;
  106. }
  107. }
  108. //載入累計盈虧科目
  109. string strXMLPathCumulative = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + "AccountingOpeningEntry_Cumulative.xml";
  110. if (File.Exists(strXMLPathCumulative))
  111. {
  112. XDocument xmlContent = XDocument.Load(strXMLPathCumulative);
  113. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  114. {
  115. strCumulativeProfitAndLossSubID = xmlData.Element("SubAccounting").Value.ToString();
  116. strCumulativeProfitAndLossSubName = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  117. break;
  118. }
  119. }
  120. # region 結清收入會計科目
  121. try
  122. {
  123. //結帳作業
  124. // 1. 結清虛帳戶(收入、費用)
  125. // 1.1. 結清收入會計科目
  126. strSQL = "Select * From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'I' And DCClass = 'C' Order By AccountingSubID";
  127. AccountingEntries acItemForm = new AccountingEntries();
  128. acItemForm.strOwnerForm = "AccountingOpeningEntry";
  129. acItemForm.Owner = this;
  130. acItemForm.StartPosition = FormStartPosition.CenterParent;
  131. acItemForm.strFrmStatus = "CLOSE";
  132. acItemForm.strKey = strKey;
  133. acItemForm.strAccountingBookID = strAccountingBookID;
  134. acItemForm.strActiveUserID = strActiveUserID;
  135. acItemForm.strAccountingYear = txtAccountingYear.Text.Trim();
  136. acItemForm.dpAccountingDate.Text = txtAccountingYear.Text.Trim() + "/12/31 23:59:59";
  137. acItemForm.Text = "收入結算分錄";
  138. using (DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"])
  139. {
  140. pbProcessedAccounting.Maximum = dtTemp.Rows.Count;
  141. foreach (DataRow drAcc in dtTemp.Rows)
  142. {
  143. dbValue = 0;
  144. strAccSQL.Clear();
  145. strAccSQL.Append("Select * From OTB_FNC_AccountingJournal Where AccountingBookID = '" + strAccountingBookID + "' ");
  146. 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() + "%'");
  147. using (DataTable dtAccSum = UtilityClass.GetSQLResult(strAccSQL.ToString()).Tables["Result"])
  148. {
  149. if (dtAccSum.Rows.Count > 0)
  150. {
  151. foreach (DataRow drAccSum in dtAccSum.Rows)
  152. {
  153. strCredit = UtilityClass.DecryptDES(drAccSum["Credit"].ToString(), strKey).Replace(",", "");
  154. strDebit = UtilityClass.DecryptDES(drAccSum["Debit"].ToString(), strKey).Replace(",", "");
  155. dbValue += Convert.ToDouble(string.IsNullOrEmpty(strCredit) ? "0" : strCredit);
  156. dbValue -= Convert.ToDouble(string.IsNullOrEmpty(strDebit) ? "0" : strDebit);
  157. }
  158. dbIncomeSum += dbValue; //計算本期損益總額
  159. if (dbValue > 0)
  160. {
  161. DataGridViewRow dgvRow = new DataGridViewRow();
  162. dgvRow.CreateCells(acItemForm.dgvDataMaintain);
  163. dgvRow.Cells[1].Value = drAcc["AccountingSubID"].ToString();
  164. dgvRow.Cells[2].Value = drAcc["AccountingSubName"].ToString();
  165. dgvRow.Cells[3].Value = "結轉分錄";
  166. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue);
  167. dgvRow.Cells[9].Value = "Y";
  168. acItemForm.dgvDataMaintain.Rows.Add(dgvRow);
  169. }
  170. }
  171. }
  172. pbProcessedAccounting.Value += 1;
  173. }
  174. }
  175. if (dbIncomeSum > 0)
  176. {
  177. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + ".xml";
  178. if (File.Exists(strXMLPath))
  179. {
  180. XDocument xmlContent = XDocument.Load(strXMLPath);
  181. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  182. {
  183. DataGridViewRow dgvRow = new DataGridViewRow();
  184. dgvRow.CreateCells(acItemForm.dgvDataMaintain);
  185. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  186. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  187. dgvRow.Cells[3].Value = "結轉分錄";
  188. dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbIncomeSum);
  189. dgvRow.Cells[9].Value = "Y";
  190. acItemForm.dgvDataMaintain.Rows.Add(dgvRow);
  191. break;
  192. }
  193. }
  194. acItemForm.SaveEvent();
  195. }
  196. }
  197. catch (Exception ex)
  198. {
  199. ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex);
  200. MessageBox.Show("收入資料結算失敗", "提示");
  201. }
  202. #endregion
  203. #region 結清費用會計科目
  204. try
  205. {
  206. //結帳作業
  207. // 1. 結清虛帳戶(收入、費用)
  208. // 1.2. 結清費用會計科目
  209. dbValue = 0;
  210. strSQL = "Select * From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'E' And DCClass = 'D' Order By AccountingSubID";
  211. AccountingEntries acItemForm = new AccountingEntries();
  212. acItemForm.strOwnerForm = "AccountingOpeningEntry";
  213. acItemForm.Owner = this;
  214. acItemForm.StartPosition = FormStartPosition.CenterParent;
  215. acItemForm.strFrmStatus = "CLOSE";
  216. acItemForm.strKey = strKey;
  217. acItemForm.strAccountingBookID = strAccountingBookID;
  218. acItemForm.strActiveUserID = strActiveUserID;
  219. acItemForm.strAccountingYear = txtAccountingYear.Text.Trim();
  220. acItemForm.dpAccountingDate.Text = txtAccountingYear.Text.Trim() + "/12/31 23:59:59";
  221. acItemForm.Text = "費用結算分錄";
  222. using (DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"])
  223. {
  224. pbProcessedAccounting.Value = 0;
  225. pbProcessedAccounting.Maximum = dtTemp.Rows.Count;
  226. foreach (DataRow drAcc in dtTemp.Rows)
  227. {
  228. dbValue = 0;
  229. strAccSQL.Clear();
  230. strAccSQL.Append("Select * From OTB_FNC_AccountingJournal Where AccountingBookID = '" + strAccountingBookID + "' ");
  231. 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() + "%'");
  232. using (DataTable dtAccSum = UtilityClass.GetSQLResult(strAccSQL.ToString()).Tables["Result"])
  233. {
  234. if (dtAccSum.Rows.Count > 0)
  235. {
  236. foreach (DataRow drAccSum in dtAccSum.Rows)
  237. {
  238. strCredit = UtilityClass.DecryptDES(drAccSum["Credit"].ToString(), strKey).Replace(",", "");
  239. strDebit = UtilityClass.DecryptDES(drAccSum["Debit"].ToString(), strKey).Replace(",", "");
  240. dbValue += Convert.ToDouble(string.IsNullOrEmpty(strDebit) ? "0" : strDebit);
  241. dbValue -= Convert.ToDouble(string.IsNullOrEmpty(strCredit) ? "0" : strCredit);
  242. }
  243. dbExpendSum += dbValue; //計算本期損益總額
  244. if (dbValue > 0)
  245. {
  246. DataGridViewRow dgvRow = new DataGridViewRow();
  247. dgvRow.CreateCells(acItemForm.dgvDataMaintain);
  248. dgvRow.Cells[1].Value = drAcc["AccountingSubID"].ToString();
  249. dgvRow.Cells[2].Value = drAcc["AccountingSubName"].ToString();
  250. dgvRow.Cells[3].Value = "結轉分錄";
  251. dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue);
  252. dgvRow.Cells[9].Value = "Y";
  253. acItemForm.dgvDataMaintain.Rows.Add(dgvRow);
  254. }
  255. }
  256. }
  257. pbProcessedAccounting.Value += 1;
  258. }
  259. }
  260. if (dbExpendSum > 0)
  261. {
  262. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + ".xml";
  263. if (File.Exists(strXMLPath))
  264. {
  265. XDocument xmlContent = XDocument.Load(strXMLPath);
  266. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  267. {
  268. DataGridViewRow dgvRow = new DataGridViewRow();
  269. dgvRow.CreateCells(acItemForm.dgvDataMaintain);
  270. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  271. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  272. dgvRow.Cells[3].Value = "結轉分錄";
  273. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(dbExpendSum);
  274. dgvRow.Cells[9].Value = "Y";
  275. acItemForm.dgvDataMaintain.Rows.Add(dgvRow);
  276. break;
  277. }
  278. }
  279. acItemForm.SaveEvent();
  280. }
  281. }
  282. catch (Exception ex)
  283. {
  284. ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex);
  285. MessageBox.Show("費用資料結算失敗", "提示");
  286. }
  287. #endregion
  288. #region 本期損益轉保留盈餘
  289. //就會計的做法,非必要執行
  290. #endregion
  291. #region 進行實帳戶結清
  292. try
  293. {
  294. //結帳作業
  295. // 2. 結轉實帳戶(A:資產、L:負債、O:股東權限)
  296. strSQL = "Select * From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass IN ('A','L','O') Order By AccountingClass, AccountingSubID";
  297. //資產結算分錄
  298. AccountingEntries acItemForm = new AccountingEntries();
  299. acItemForm.strOwnerForm = "AccountingOpeningEntry";
  300. acItemForm.Owner = this;
  301. acItemForm.StartPosition = FormStartPosition.CenterParent;
  302. acItemForm.strFrmStatus = "CLOSE";
  303. acItemForm.strKey = strKey;
  304. acItemForm.strAccountingBookID = strAccountingBookID;
  305. acItemForm.strActiveUserID = strActiveUserID;
  306. acItemForm.strAccountingYear = txtAccountingYear.Text.Trim();
  307. acItemForm.Text = "資產結算分錄";
  308. //結清本期損益
  309. AccountingEntries acItemCloseForm = new AccountingEntries();
  310. acItemCloseForm.strOwnerForm = "AccountingOpeningEntry";
  311. acItemCloseForm.Owner = this;
  312. acItemCloseForm.StartPosition = FormStartPosition.CenterParent;
  313. acItemCloseForm.strFrmStatus = "CLOSE";
  314. acItemCloseForm.strKey = strKey;
  315. acItemCloseForm.strAccountingBookID = strAccountingBookID;
  316. acItemCloseForm.strActiveUserID = strActiveUserID;
  317. acItemCloseForm.strAccountingYear = txtAccountingYear.Text.Trim();
  318. acItemCloseForm.Text = "本期損益結算分錄";
  319. //期初分錄
  320. AccountingEntries acNewItemForm = new AccountingEntries();
  321. acNewItemForm.strOwnerForm = "AccountingOpeningEntry";
  322. acNewItemForm.Owner = this;
  323. acNewItemForm.StartPosition = FormStartPosition.CenterParent;
  324. acNewItemForm.strFrmStatus = "ADD";
  325. acNewItemForm.strKey = strKey;
  326. acNewItemForm.strAccountingBookID = strAccountingBookID;
  327. acNewItemForm.strActiveUserID = strActiveUserID;
  328. acNewItemForm.Text = "期初轉入";
  329. using (DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"])
  330. {
  331. pbProcessedAccounting.Value = 0;
  332. pbProcessedAccounting.Maximum = dtTemp.Rows.Count;
  333. foreach (DataRow drAcc in dtTemp.Rows)
  334. {
  335. dbValue = 0;
  336. strAccSQL.Clear();
  337. strAccSQL.Append("Select * From OTB_FNC_AccountingJournal Where AccountingBookID = '" + strAccountingBookID + "' ");
  338. 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() + "%'");
  339. using (DataTable dtAccSum = UtilityClass.GetSQLResult(strAccSQL.ToString()).Tables["Result"])
  340. {
  341. if (dtAccSum.Rows.Count > 0)
  342. {
  343. foreach (DataRow drAccSum in dtAccSum.Rows)
  344. {
  345. strCredit = UtilityClass.DecryptDES(drAccSum["Credit"].ToString(), strKey).Replace(",", ""); //貸方
  346. strDebit = UtilityClass.DecryptDES(drAccSum["Debit"].ToString(), strKey).Replace(",", ""); //借方
  347. dbValue += Convert.ToDouble(string.IsNullOrEmpty(strDebit) ? "0" : strDebit);
  348. dbValue -= Convert.ToDouble(string.IsNullOrEmpty(strCredit) ? "0" : strCredit);
  349. }
  350. dbExpendSum += dbValue; //計算本期損益總額
  351. DataGridViewRow dgvRow = new DataGridViewRow();
  352. dgvRow.CreateCells(acItemForm.dgvDataMaintain);
  353. dgvRow.Cells[1].Value = drAcc["AccountingSubID"].ToString();
  354. dgvRow.Cells[2].Value = drAcc["AccountingSubName"].ToString();
  355. dgvRow.Cells[3].Value = "結轉分錄";
  356. dgvRow.Cells[9].Value = "Y";
  357. //結清本期損益 Start
  358. if (drAcc["AccountingSubID"].ToString() == strCurrentProfitAndLossSubID)
  359. {
  360. DataGridViewRow dgvRowCurrProfitAndLoss = new DataGridViewRow();
  361. dgvRowCurrProfitAndLoss.CreateCells(acItemCloseForm.dgvDataMaintain);
  362. dgvRowCurrProfitAndLoss.Cells[1].Value = strCurrentProfitAndLossSubID;
  363. dgvRowCurrProfitAndLoss.Cells[2].Value = strCurrentProfitAndLossSubName;
  364. dgvRowCurrProfitAndLoss.Cells[3].Value = "結轉分錄";
  365. dgvRowCurrProfitAndLoss.Cells[9].Value = "Y";
  366. if (drAcc["DCClass"].ToString() == "D")
  367. {
  368. if (dbValue > 0)
  369. {
  370. dgvRowCurrProfitAndLoss.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄
  371. }
  372. else
  373. {
  374. dgvRowCurrProfitAndLoss.Cells[4].Value = UtilityClass.MarkNumber(dbValue * -1); //結轉分錄
  375. }
  376. }
  377. else
  378. {
  379. if (dbValue > 0)
  380. {
  381. dgvRowCurrProfitAndLoss.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄
  382. }
  383. else
  384. {
  385. dgvRowCurrProfitAndLoss.Cells[4].Value = UtilityClass.MarkNumber(dbValue * -1); //結轉分錄
  386. }
  387. }
  388. acItemCloseForm.dgvDataMaintain.Rows.Add(dgvRowCurrProfitAndLoss);
  389. DataGridViewRow dgvRowCumuProfitAndLoss = new DataGridViewRow();
  390. dgvRowCumuProfitAndLoss.CreateCells(acItemCloseForm.dgvDataMaintain);
  391. dgvRowCumuProfitAndLoss.Cells[1].Value = strCumulativeProfitAndLossSubID;
  392. dgvRowCumuProfitAndLoss.Cells[2].Value = strCumulativeProfitAndLossSubName;
  393. dgvRowCumuProfitAndLoss.Cells[3].Value = "結轉分錄";
  394. dgvRowCumuProfitAndLoss.Cells[9].Value = "Y";
  395. if (drAcc["DCClass"].ToString() == "D")
  396. {
  397. if (dbValue > 0)
  398. {
  399. dgvRowCumuProfitAndLoss.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄
  400. }
  401. else
  402. {
  403. dgvRowCumuProfitAndLoss.Cells[4].Value = UtilityClass.MarkNumber(dbValue * -1); //結轉分錄
  404. }
  405. }
  406. else
  407. {
  408. if (dbValue > 0)
  409. {
  410. dgvRowCumuProfitAndLoss.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄
  411. }
  412. else
  413. {
  414. dgvRowCumuProfitAndLoss.Cells[5].Value = UtilityClass.MarkNumber(dbValue * -1); //結轉分錄
  415. }
  416. }
  417. acItemCloseForm.dgvDataMaintain.Rows.Add(dgvRowCumuProfitAndLoss);
  418. }
  419. //結清本期損益 End
  420. DataGridViewRow dgvNewRow = new DataGridViewRow();
  421. dgvNewRow.CreateCells(acNewItemForm.dgvDataMaintain);
  422. dgvNewRow.Cells[1].Value = drAcc["AccountingSubID"].ToString();
  423. dgvNewRow.Cells[2].Value = drAcc["AccountingSubName"].ToString();
  424. dgvNewRow.Cells[3].Value = "期初轉入";
  425. if (drAcc["DCClass"].ToString() == "D")
  426. {
  427. if (dbValue > 0)
  428. {
  429. dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄
  430. dgvNewRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //期初轉入
  431. }
  432. else
  433. {
  434. dbValue = dbValue * -1;
  435. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄
  436. dgvNewRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //期初轉入
  437. }
  438. }
  439. else
  440. {
  441. if (dbValue > 0)
  442. {
  443. dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄
  444. dgvNewRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //期初轉入
  445. }
  446. else
  447. {
  448. dbValue = dbValue * -1;
  449. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(dbValue); //結轉分錄
  450. dgvNewRow.Cells[5].Value = UtilityClass.MarkNumber(dbValue); //期初轉入
  451. }
  452. }
  453. strCredit = string.IsNullOrEmpty((string)dgvRow.Cells[4].Value) ? "0" : dgvRow.Cells[4].Value.ToString();
  454. strDebit = string.IsNullOrEmpty((string)dgvRow.Cells[5].Value) ? "0" : dgvRow.Cells[5].Value.ToString();
  455. if (strCredit != "0" || strDebit != "0")
  456. {
  457. acItemForm.dgvDataMaintain.Rows.Add(dgvRow);
  458. }
  459. strCredit = string.IsNullOrEmpty((string)dgvNewRow.Cells[4].Value) ? "0" : dgvNewRow.Cells[4].Value.ToString();
  460. strDebit = string.IsNullOrEmpty((string)dgvNewRow.Cells[5].Value) ? "0" : dgvNewRow.Cells[5].Value.ToString();
  461. if (strCredit != "0" || strDebit != "0")
  462. {
  463. acNewItemForm.dgvDataMaintain.Rows.Add(dgvNewRow);
  464. }
  465. }
  466. }
  467. pbProcessedAccounting.Value += 1;
  468. }
  469. }
  470. acItemForm.SaveEvent();
  471. acItemCloseForm.SaveEvent();
  472. acNewItemForm.SaveEvent();
  473. //acItemForm.ShowDialog();
  474. //acItemCloseForm.ShowDialog();
  475. //acNewItemForm.ShowDialog();
  476. }
  477. catch (Exception ex)
  478. {
  479. ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex);
  480. MessageBox.Show("實帳戶結算失敗", "提示");
  481. }
  482. #endregion
  483. string strCloseAccBookSQL = "Update OTB_FNC_AccountingBookStatus Set IsOpen = 'N' Where AccountingBookID ='" + strAccountingBookID + "' And AccountingYear = '" + txtAccountingYear.Text.Trim() + "'";
  484. UtilityClass.RunSQLNonReturn(strCloseAccBookSQL); //執行關帳
  485. MessageBox.Show("關帳成功!", "提示");
  486. this.Close();
  487. }
  488. private void AccountingOpenEntryProcess()
  489. {
  490. //開帳作業
  491. try
  492. {
  493. 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 + "')";
  494. UtilityClass.RunSQLNonReturn(strSQL);
  495. this.Close();
  496. }
  497. catch (Exception ex)
  498. {
  499. ErrorHandler.WriteErrorLog("AccountingOpeningEntry.cs", ex);
  500. MessageBox.Show("開帳失敗", "提示");
  501. }
  502. }
  503. private bool CheckCloseForm()
  504. {
  505. if (txtAccountingYear.Text.Trim() == "")
  506. {
  507. MessageBox.Show("會計年度為必填欄位", "提示");
  508. txtAccountingYear.Focus();
  509. return false;
  510. }
  511. return true;
  512. }
  513. private bool CheckOpenForm()
  514. {
  515. return true;
  516. }
  517. #endregion
  518. #region 事件觸發及問題處理
  519. private void AccountingOpeningEntry_Load(object sender, EventArgs e)
  520. {
  521. SetupStatus();
  522. }
  523. private void btnOpen_Click(object sender, EventArgs e)
  524. {
  525. if (MessageBox.Show("請問是否要進行關帳及開帳作業?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  526. {
  527. if (CheckCloseForm())
  528. {
  529. AccountingOpenEntryProcess();
  530. AccountingCloseEntryProcess();
  531. }
  532. }
  533. }
  534. private void tsbSetup_Click(object sender, EventArgs e)
  535. {
  536. SetOpenAccountingSubID actForm = new SetOpenAccountingSubID();
  537. actForm.StartPosition = FormStartPosition.CenterParent;
  538. actForm.ShowDialog();
  539. this.Close();
  540. }
  541. private void txtAccountingYear_Leave(object sender, EventArgs e)
  542. {
  543. SetupStatus();
  544. }
  545. #endregion
  546. }
  547. }