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.

472 lines
24 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 BalanceSheet : Form
  19. {
  20. //程式內共用物件
  21. string strFrmStatus = ""; //表單狀態
  22. string strActiveUserID = ""; //取得登入作用的使用者帳號
  23. string strAccountingBookID = ""; //取得作用中的帳本
  24. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  25. SqlCommand sqlCmd = new SqlCommand();
  26. DataSet sdInsurance = new System.Data.DataSet();
  27. string strKey = ""; //程式內加密的Key值
  28. string strTaxAccountingID = ""; //所得稅科目ID
  29. public BalanceSheet()
  30. {
  31. InitializeComponent();
  32. }
  33. #region 自定義程式
  34. public void StatusChange(string strStatus) //變更主畫面狀態
  35. {
  36. switch (strStatus.ToUpper())
  37. {
  38. case "NONE":
  39. CleanForm();
  40. CleanToolbar();
  41. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  42. strFrmStatus = "";
  43. tsbSearch.Enabled = true;
  44. break;
  45. case "SEARCH":
  46. Application.DoEvents();
  47. tsbSearch.Enabled = false;
  48. tsbOK.Visible = true;
  49. tsbCancel.Visible = true;
  50. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  51. strFrmStatus = "SEARCH";
  52. break;
  53. case "ADD":
  54. //本功能不提供新增
  55. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  56. strFrmStatus = "ADD";
  57. break;
  58. case "MODIFY":
  59. //本功能不提供修改
  60. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  61. strFrmStatus = "MODIFY";
  62. break;
  63. case "DEL":
  64. //本功能不提供刪除
  65. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  66. strFrmStatus = "DEL";
  67. break;
  68. }
  69. }
  70. private void CleanForm()
  71. {
  72. cbAccountingYear.DataSource = UtilityClass.GetAccountingYears(5);
  73. cbAccountingYear.SelectedIndex = 0;
  74. lbLiabilities.Text = "";
  75. lbAssets.Text = "";
  76. lbOwnerRight.Text = "";
  77. crBalanceSheet.Series["Series1"].Points.Clear();
  78. dgvLiabilities.Rows.Clear();
  79. dgvAsset.Rows.Clear();
  80. dgvOwnerRight.Rows.Clear();
  81. tbCharts.SelectedTab = tabBalanceSheet;
  82. }
  83. private void CleanToolbar()
  84. {
  85. tsbSearch.Enabled = true;
  86. tsbClean.Enabled = true;
  87. tsbExit.Enabled = true;
  88. tsbOK.Visible = false;
  89. tsbCancel.Visible = false;
  90. }
  91. private void AssetsCount() //計算資產總額
  92. {
  93. StringBuilder sbSQL = new StringBuilder();
  94. string strAccountingSubID = "";
  95. string strAccountingSubName = "";
  96. string strAccountingAmount = "0";
  97. string strCredit = "";
  98. string strDebit = "";
  99. string strAssetsCount = "0";
  100. try
  101. {
  102. sbSQL.Append(" Select J.AccountingBookID, J.AccountingID, J.AccountingDate, J.AccountingSubID, J.AccountingSubName,J.Debit, J.Credit, A.DCClass ");
  103. sbSQL.Append(" From OTB_FNC_AccountingJournal J, OTB_FNC_AccountingSubjects A ");
  104. sbSQL.Append(" Where J.AccountingSubID = A.AccountingSubID And A.AccountingBookID = '" + strAccountingBookID + "'");
  105. sbSQL.Append(" And ISNULL(J.ClosedAccounting,'N') != 'Y' ");
  106. sbSQL.Append(" And J.AccountingBookID ='" + strAccountingBookID + "' ");
  107. sbSQL.Append(" And Year(J.AccountingDate) = '" + cbAccountingYear.SelectedValue.ToString() + "'");
  108. sbSQL.Append(" And J.AccountingSubID IN (Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'A') ");
  109. sbSQL.Append(" Order by J.AccountingSubID ");
  110. using (DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  111. {
  112. if (dtTemp.Rows.Count > 0)
  113. {
  114. foreach (DataRow drAssetsData in dtTemp.Rows)
  115. {
  116. if (strAccountingSubID == drAssetsData["AccountingSubID"].ToString())
  117. {
  118. strCredit = string.IsNullOrEmpty(drAssetsData["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drAssetsData["Credit"].ToString(),strKey).Replace(",", "");
  119. strDebit = string.IsNullOrEmpty(drAssetsData["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drAssetsData["Debit"].ToString(), strKey).Replace(",", "");
  120. if (drAssetsData["DCClass"].ToString() == "C")
  121. {
  122. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) + Convert.ToInt32(strCredit) - Convert.ToInt32(strDebit)).ToString();
  123. }
  124. else
  125. {
  126. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  127. }
  128. }
  129. else
  130. {
  131. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  132. {
  133. DataGridViewRow dgvRow = new DataGridViewRow();
  134. dgvRow.CreateCells(dgvAsset);
  135. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  136. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  137. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  138. dgvAsset.Rows.Add(dgvRow);
  139. strAssetsCount = (Convert.ToInt32(strAssetsCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  140. strAccountingAmount = "0";
  141. }
  142. //清空數值
  143. strAccountingSubID = drAssetsData["AccountingSubID"].ToString();
  144. strAccountingSubName = drAssetsData["AccountingSubName"].ToString();
  145. strCredit = string.IsNullOrEmpty(drAssetsData["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drAssetsData["Credit"].ToString(), strKey).Replace(",", "");
  146. strDebit = string.IsNullOrEmpty(drAssetsData["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drAssetsData["Debit"].ToString(), strKey).Replace(",", "");
  147. if (drAssetsData["DCClass"].ToString() == "C")
  148. {
  149. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) + Convert.ToInt32(strCredit) - Convert.ToInt32(strDebit)).ToString();
  150. }
  151. else
  152. {
  153. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  154. }
  155. }
  156. }
  157. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  158. {
  159. DataGridViewRow dgvRow = new DataGridViewRow();
  160. dgvRow.CreateCells(dgvAsset);
  161. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  162. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  163. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  164. dgvAsset.Rows.Add(dgvRow);
  165. strAssetsCount = (Convert.ToInt32(strAssetsCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  166. }
  167. }
  168. lbAssets.Text = UtilityClass.MarkNumber(strAssetsCount);
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. ErrorHandler.WriteErrorLog("BalanceSheet.cs", ex);
  174. }
  175. }
  176. private void LiabilitiesCount() //計算負債總額
  177. {
  178. StringBuilder sbSQL = new StringBuilder();
  179. string strAccountingSubID = "";
  180. string strAccountingSubName = "";
  181. string strAccountingAmount = "0";
  182. string strCredit = "";
  183. string strDebit = "";
  184. string strExpenseCount = "0";
  185. try
  186. {
  187. sbSQL.Append(" Select J.AccountingBookID, J.AccountingID, J.AccountingDate, J.AccountingSubID, J.AccountingSubName,J.Debit, J.Credit, A.DCClass ");
  188. sbSQL.Append(" From OTB_FNC_AccountingJournal J, OTB_FNC_AccountingSubjects A ");
  189. sbSQL.Append(" Where J.AccountingSubID = A.AccountingSubID And A.AccountingBookID = '" + strAccountingBookID + "'");
  190. sbSQL.Append(" And ISNULL(J.ClosedAccounting,'N') != 'Y' ");
  191. sbSQL.Append(" And J.AccountingBookID ='" + strAccountingBookID + "' ");
  192. sbSQL.Append(" And Year(J.AccountingDate) = '" + cbAccountingYear.SelectedValue.ToString() + "'");
  193. sbSQL.Append(" And J.AccountingSubID IN (Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'L') ");
  194. sbSQL.Append(" Order by J.AccountingSubID ");
  195. using (DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  196. {
  197. if (dtTemp.Rows.Count > 0)
  198. {
  199. foreach (DataRow drLiabilities in dtTemp.Rows)
  200. {
  201. if (strAccountingSubID == drLiabilities["AccountingSubID"].ToString())
  202. {
  203. strCredit = string.IsNullOrEmpty(drLiabilities["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drLiabilities["Credit"].ToString(), strKey).Replace(",", "");
  204. strDebit = string.IsNullOrEmpty(drLiabilities["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drLiabilities["Debit"].ToString(), strKey).Replace(",", "");
  205. if (drLiabilities["DCClass"].ToString() == "D")
  206. {
  207. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  208. }
  209. else
  210. {
  211. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) + Convert.ToInt32(strCredit) - Convert.ToInt32(strDebit)).ToString();
  212. }
  213. }
  214. else
  215. {
  216. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  217. {
  218. DataGridViewRow dgvRow = new DataGridViewRow();
  219. dgvRow.CreateCells(dgvLiabilities);
  220. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  221. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  222. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  223. dgvLiabilities.Rows.Add(dgvRow);
  224. strExpenseCount = (Convert.ToInt32(strExpenseCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  225. strAccountingAmount = "0";
  226. }
  227. //清空數值
  228. strAccountingSubID = drLiabilities["AccountingSubID"].ToString();
  229. strAccountingSubName = drLiabilities["AccountingSubName"].ToString();
  230. strCredit = string.IsNullOrEmpty(drLiabilities["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drLiabilities["Credit"].ToString(), strKey).Replace(",", "");
  231. strDebit = string.IsNullOrEmpty(drLiabilities["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drLiabilities["Debit"].ToString(), strKey).Replace(",", "");
  232. if (drLiabilities["DCClass"].ToString() == "D")
  233. {
  234. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  235. }
  236. else
  237. {
  238. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) + Convert.ToInt32(strCredit) - Convert.ToInt32(strDebit)).ToString();
  239. }
  240. }
  241. }
  242. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  243. {
  244. DataGridViewRow dgvRow = new DataGridViewRow();
  245. dgvRow.CreateCells(dgvLiabilities);
  246. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  247. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  248. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  249. dgvLiabilities.Rows.Add(dgvRow);
  250. strExpenseCount = (Convert.ToInt32(strExpenseCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  251. }
  252. }
  253. lbLiabilities.Text = UtilityClass.MarkNumber(strExpenseCount);
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. ErrorHandler.WriteErrorLog("BalanceSheet.cs", ex);
  259. }
  260. }
  261. private void OwnerRightCount() //計算業主權益總額
  262. {
  263. StringBuilder sbSQL = new StringBuilder();
  264. string strAccountingSubID = "";
  265. string strAccountingSubName = "";
  266. string strAccountingAmount = "0";
  267. string strCredit = "";
  268. string strDebit = "";
  269. string strOwnerRightCount = "0";
  270. try
  271. {
  272. sbSQL.Append(" Select J.AccountingBookID, J.AccountingID, J.AccountingDate, J.AccountingSubID, J.AccountingSubName,J.Debit, J.Credit, A.DCClass ");
  273. sbSQL.Append(" From OTB_FNC_AccountingJournal J, OTB_FNC_AccountingSubjects A ");
  274. sbSQL.Append(" Where J.AccountingSubID = A.AccountingSubID And A.AccountingBookID = '" + strAccountingBookID + "'");
  275. sbSQL.Append(" And ISNULL(J.ClosedAccounting,'N') != 'Y' ");
  276. sbSQL.Append(" And J.AccountingBookID ='" + strAccountingBookID + "' ");
  277. sbSQL.Append(" And Year(J.AccountingDate) = '" + cbAccountingYear.SelectedValue.ToString() + "'");
  278. sbSQL.Append(" And J.AccountingSubID IN (Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'O') ");
  279. sbSQL.Append(" Order by J.AccountingSubID, J.AccountingID ");
  280. using (DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  281. {
  282. if (dtTemp.Rows.Count > 0)
  283. {
  284. foreach (DataRow drOwnerRightData in dtTemp.Rows)
  285. {
  286. if (strAccountingSubID == drOwnerRightData["AccountingSubID"].ToString())
  287. {
  288. strDebit = string.IsNullOrEmpty(drOwnerRightData["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drOwnerRightData["Debit"].ToString(), strKey).Replace(",", "");
  289. strCredit = string.IsNullOrEmpty(drOwnerRightData["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drOwnerRightData["Credit"].ToString(), strKey).Replace(",", "");
  290. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  291. }
  292. else
  293. {
  294. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  295. {
  296. DataGridViewRow dgvRow = new DataGridViewRow();
  297. dgvRow.CreateCells(dgvOwnerRight);
  298. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  299. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  300. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  301. dgvOwnerRight.Rows.Add(dgvRow);
  302. strOwnerRightCount = (Convert.ToInt32(strOwnerRightCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  303. strAccountingAmount = "0";
  304. }
  305. //清空數值
  306. strAccountingSubID = drOwnerRightData["AccountingSubID"].ToString();
  307. strAccountingSubName = drOwnerRightData["AccountingSubName"].ToString();
  308. strDebit = string.IsNullOrEmpty(drOwnerRightData["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drOwnerRightData["Debit"].ToString(), strKey).Replace(",", "");
  309. strCredit = string.IsNullOrEmpty(drOwnerRightData["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drOwnerRightData["Credit"].ToString(), strKey).Replace(",", "");
  310. if (drOwnerRightData["DCClass"].ToString() == "D")
  311. {
  312. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  313. }
  314. else
  315. {
  316. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  317. }
  318. }
  319. }
  320. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  321. {
  322. DataGridViewRow dgvRow = new DataGridViewRow();
  323. dgvRow.CreateCells(dgvOwnerRight);
  324. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  325. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  326. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  327. dgvOwnerRight.Rows.Add(dgvRow);
  328. strOwnerRightCount = (Convert.ToInt32(strOwnerRightCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  329. }
  330. }
  331. lbOwnerRight.Text = UtilityClass.MarkNumber(strOwnerRightCount);
  332. }
  333. }
  334. catch (Exception ex)
  335. {
  336. ErrorHandler.WriteErrorLog("BalanceSheet.cs", ex);
  337. }
  338. }
  339. private void GoEvent()
  340. {
  341. AssetsCount();
  342. LiabilitiesCount();
  343. OwnerRightCount();
  344. UtilityClass.SetGridColor(dgvAsset);
  345. UtilityClass.SetGridColor(dgvLiabilities);
  346. UtilityClass.SetGridColor(dgvOwnerRight);
  347. //計算稅前淨利
  348. string strAssets = lbAssets.Text.ToString();
  349. string strLiabilities = lbLiabilities.Text.ToString();
  350. string strOwnerRight = lbOwnerRight.Text.ToString();
  351. strAssets = string.IsNullOrEmpty(strAssets) ? "0" : strAssets.Replace(",", "");
  352. strLiabilities = string.IsNullOrEmpty(strLiabilities) ? "0" : strLiabilities.Replace(",", "");
  353. strOwnerRight = string.IsNullOrEmpty(strOwnerRight) ? "0" : strOwnerRight.Replace(",", "");
  354. ShowBalanceSheetChar();
  355. }
  356. private void ShowBalanceSheetChar()
  357. {
  358. string[] strItems = {"資產總計", "負債總計", "業主權益總計" };
  359. int[] intValues = { Convert.ToInt32(lbAssets.Text.Replace(",", "")), Convert.ToInt32(lbLiabilities.Text.Replace(",", "")), Convert.ToInt32(lbOwnerRight.Text.Replace(",", "")) };
  360. crBalanceSheet.Series["Series1"]["PieLabelStyle"] = "Inside";
  361. crBalanceSheet.Series["Series1"].Points.DataBindXY(strItems, intValues);
  362. crBalanceSheet.Series["Series1"].IsValueShownAsLabel = true;
  363. }
  364. #endregion
  365. #region 事件觸發及問題處理
  366. private void BalanceSheet_Load(object sender, EventArgs e)//畫面載入設定
  367. {
  368. try
  369. {
  370. strActiveUserID = MainForm.strActiveUserID;
  371. strAccountingBookID = MainForm.strAccountingBookID;
  372. strKey = MainForm.strKey;
  373. if (MainForm.strKey == "")
  374. {
  375. //設定Toolbar初始狀態
  376. tsbSearch.Enabled = false;
  377. }
  378. //設定畫面初始狀態
  379. StatusChange("NONE");
  380. GoEvent();
  381. }
  382. catch (Exception ex)
  383. {
  384. ErrorHandler.WriteErrorLog("BalanceSheet.cs", ex);
  385. }
  386. }
  387. private void tsbSearch_Click(object sender, EventArgs e)
  388. {
  389. CleanForm();
  390. tsbSearch.Visible = false;
  391. tsbOK.Visible = true;
  392. tsbCancel.Visible = true;
  393. }
  394. private void tsbCancel_Click(object sender, EventArgs e)
  395. {
  396. tsbSearch.Visible = true;
  397. tsbOK.Visible = false;
  398. tsbCancel.Visible = false;
  399. }
  400. private void tsbOK_Click(object sender, EventArgs e)
  401. {
  402. GoEvent();
  403. tsbSearch.Visible = true;
  404. tsbOK.Visible = false;
  405. tsbCancel.Visible = false;
  406. }
  407. private void tsbExit_Click(object sender, EventArgs e)
  408. {
  409. this.Close();
  410. }
  411. private void tsbSetup_Click(object sender, EventArgs e)
  412. {
  413. XMLSetting frmSettingForm = new XMLSetting();
  414. frmSettingForm.Owner = this;
  415. frmSettingForm.strOwnerForm = this.Name.ToString();
  416. frmSettingForm.strXMLName = this.Name.ToString();
  417. frmSettingForm.StartPosition = FormStartPosition.CenterParent;
  418. frmSettingForm.ShowDialog();
  419. }
  420. private void tsbClean_Click(object sender, EventArgs e)
  421. {
  422. CleanForm();
  423. }
  424. #endregion
  425. }
  426. }