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.

442 lines
21 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 IncomeStatement : 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 IncomeStatement()
  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. LockForm();
  42. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  43. strFrmStatus = "";
  44. tsbSearch.Enabled = true;
  45. break;
  46. case "SEARCH":
  47. UnLockForm();
  48. Application.DoEvents();
  49. tsbSearch.Enabled = false;
  50. tsbOK.Visible = true;
  51. tsbCancel.Visible = true;
  52. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  53. strFrmStatus = "SEARCH";
  54. break;
  55. case "ADD":
  56. //本功能不提供新增
  57. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  58. strFrmStatus = "ADD";
  59. break;
  60. case "MODIFY":
  61. //本功能不提供修改
  62. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  63. strFrmStatus = "MODIFY";
  64. break;
  65. case "DEL":
  66. //本功能不提供刪除
  67. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  68. strFrmStatus = "DEL";
  69. break;
  70. }
  71. }
  72. private void CleanForm()
  73. {
  74. dpAccStart.Text = dpAccStart.Text = DateTime.Now.Year.ToString() + "/1/1";
  75. dpAccEnd.Text = DateTime.Now.ToShortDateString();
  76. lbExpense.Text = "";
  77. lbIncome.Text = "";
  78. lbNetAfterTax.Text = "";
  79. crNet.Series["Series1"].Points.Clear();
  80. crIncomeList.Series["Series1"].Points.Clear();
  81. crExpendList.Series["Series1"].Points.Clear();
  82. dgvExpense.Rows.Clear();
  83. dgvIncome.Rows.Clear();
  84. tbCharts.SelectedTab = tabNet;
  85. }
  86. private void CleanToolbar()
  87. {
  88. tsbSearch.Enabled = true;
  89. tsbClean.Enabled = true;
  90. tsbExit.Enabled = true;
  91. tsbOK.Visible = false;
  92. tsbCancel.Visible = false;
  93. }
  94. private void LockForm()
  95. {
  96. dpAccEnd.Enabled = false;
  97. dpAccStart.Enabled = false;
  98. }
  99. private void UnLockForm()
  100. {
  101. dpAccEnd.Enabled = true;
  102. dpAccStart.Enabled = true;
  103. }
  104. private void IncomeCount() //計算收入總額
  105. {
  106. StringBuilder sbSQL = new StringBuilder();
  107. string strAccountingSubID = "";
  108. string strAccountingSubName = "";
  109. string strAccountingAmount = "0";
  110. string strCredit = "";
  111. string strDebit = "";
  112. string strIncomeCount = "0";
  113. try
  114. {
  115. sbSQL.Append(" Select J.AccountingBookID, J.AccountingID, J.AccountingDate, J.AccountingSubID, J.AccountingSubName,J.Debit, J.Credit, A.DCClass ");
  116. sbSQL.Append(" From OTB_FNC_AccountingJournal J, OTB_FNC_AccountingSubjects A ");
  117. sbSQL.Append(" Where J.AccountingSubID = A.AccountingSubID And A.AccountingBookID = '" + strAccountingBookID + "'");
  118. sbSQL.Append(" And ISNULL(J.ClosedAccounting,'N') != 'Y'");
  119. sbSQL.Append(" And J.AccountingBookID ='" + strAccountingBookID + "' ");
  120. sbSQL.Append(" And J.AccountingDate Between '" + dpAccStart.Text + " 00:00:00' And '" + dpAccEnd.Text + " 23:59:59' ");
  121. sbSQL.Append(" And J.AccountingSubID IN (Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'I') ");
  122. sbSQL.Append(" Order by J.AccountingSubID ");
  123. using (DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  124. {
  125. if (dtTemp.Rows.Count > 0)
  126. {
  127. foreach (DataRow drIncomeData in dtTemp.Rows)
  128. {
  129. if (strAccountingSubID == drIncomeData["AccountingSubID"].ToString())
  130. {
  131. strCredit = string.IsNullOrEmpty(drIncomeData["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drIncomeData["Credit"].ToString(),strKey).Replace(",", "");
  132. strDebit = string.IsNullOrEmpty(drIncomeData["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drIncomeData["Debit"].ToString(), strKey).Replace(",", "");
  133. if (drIncomeData["DCClass"].ToString() == "C")
  134. {
  135. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) + Convert.ToInt32(strCredit) - Convert.ToInt32(strDebit)).ToString();
  136. }
  137. else
  138. {
  139. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  140. }
  141. }
  142. else
  143. {
  144. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  145. {
  146. DataGridViewRow dgvRow = new DataGridViewRow();
  147. dgvRow.CreateCells(dgvIncome);
  148. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  149. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  150. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  151. dgvIncome.Rows.Add(dgvRow);
  152. strIncomeCount = (Convert.ToInt32(strIncomeCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  153. strAccountingAmount = "0";
  154. }
  155. //清空數值
  156. strAccountingSubID = drIncomeData["AccountingSubID"].ToString();
  157. strAccountingSubName = drIncomeData["AccountingSubName"].ToString();
  158. strCredit = string.IsNullOrEmpty(drIncomeData["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drIncomeData["Credit"].ToString(), strKey).Replace(",", "");
  159. strDebit = string.IsNullOrEmpty(drIncomeData["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drIncomeData["Debit"].ToString(), strKey).Replace(",", "");
  160. if (drIncomeData["DCClass"].ToString() == "C")
  161. {
  162. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) + Convert.ToInt32(strCredit) - Convert.ToInt32(strDebit)).ToString();
  163. }
  164. else
  165. {
  166. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  167. }
  168. }
  169. }
  170. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  171. {
  172. DataGridViewRow dgvRow = new DataGridViewRow();
  173. dgvRow.CreateCells(dgvIncome);
  174. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  175. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  176. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  177. dgvIncome.Rows.Add(dgvRow);
  178. strIncomeCount = (Convert.ToInt32(strIncomeCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  179. }
  180. }
  181. lbIncome.Text = UtilityClass.MarkNumber(strIncomeCount);
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. ErrorHandler.WriteErrorLog("IncomeStatement.cs", ex);
  187. }
  188. }
  189. private void ExpenseCount() //計算費用總額
  190. {
  191. StringBuilder sbSQL = new StringBuilder();
  192. string strAccountingSubID = "";
  193. string strAccountingSubName = "";
  194. string strAccountingAmount = "0";
  195. string strCredit = "";
  196. string strDebit = "";
  197. string strExpenseCount = "0";
  198. try
  199. {
  200. sbSQL.Append(" Select J.AccountingBookID, J.AccountingID, J.AccountingDate, J.AccountingSubID, J.AccountingSubName,J.Debit, J.Credit, A.DCClass ");
  201. sbSQL.Append(" From OTB_FNC_AccountingJournal J, OTB_FNC_AccountingSubjects A ");
  202. sbSQL.Append(" Where J.AccountingSubID = A.AccountingSubID And A.AccountingBookID = '" + strAccountingBookID + "'");
  203. sbSQL.Append(" And ISNULL(J.ClosedAccounting,'N') != 'Y'");
  204. sbSQL.Append(" And J.AccountingBookID ='" + strAccountingBookID + "' ");
  205. sbSQL.Append(" And J.AccountingDate Between '" + dpAccStart.Text + " 00:00:00' And '" + dpAccEnd.Text + " 23:59:59' ");
  206. sbSQL.Append(" And J.AccountingSubID IN (Select AccountingSubID From OTB_FNC_AccountingSubjects Where AccountingBookID = '" + strAccountingBookID + "' And AccountingClass = 'E') ");
  207. sbSQL.Append(" Order by J.AccountingSubID ");
  208. using (DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  209. {
  210. if (dtTemp.Rows.Count > 0)
  211. {
  212. foreach (DataRow drExpenseData in dtTemp.Rows)
  213. {
  214. if (strAccountingSubID == drExpenseData["AccountingSubID"].ToString())
  215. {
  216. strCredit = string.IsNullOrEmpty(drExpenseData["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drExpenseData["Credit"].ToString(), strKey).Replace(",", "");
  217. strDebit = string.IsNullOrEmpty(drExpenseData["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drExpenseData["Debit"].ToString(), strKey).Replace(",", "");
  218. if (drExpenseData["DCClass"].ToString() == "D")
  219. {
  220. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  221. }
  222. else
  223. {
  224. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) + Convert.ToInt32(strCredit) - Convert.ToInt32(strDebit)).ToString();
  225. }
  226. }
  227. else
  228. {
  229. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  230. {
  231. DataGridViewRow dgvRow = new DataGridViewRow();
  232. dgvRow.CreateCells(dgvExpense);
  233. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  234. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  235. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  236. dgvExpense.Rows.Add(dgvRow);
  237. strExpenseCount = (Convert.ToInt32(strExpenseCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  238. strAccountingAmount = "0";
  239. }
  240. //清空數值
  241. strAccountingSubID = drExpenseData["AccountingSubID"].ToString();
  242. strAccountingSubName = drExpenseData["AccountingSubName"].ToString();
  243. strCredit = string.IsNullOrEmpty(drExpenseData["Credit"].ToString()) ? "0" : UtilityClass.DecryptDES(drExpenseData["Credit"].ToString(), strKey).Replace(",", "");
  244. strDebit = string.IsNullOrEmpty(drExpenseData["Debit"].ToString()) ? "0" : UtilityClass.DecryptDES(drExpenseData["Debit"].ToString(), strKey).Replace(",", "");
  245. if (drExpenseData["DCClass"].ToString() == "D")
  246. {
  247. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) - Convert.ToInt32(strCredit) + Convert.ToInt32(strDebit)).ToString();
  248. }
  249. else
  250. {
  251. strAccountingAmount = (Convert.ToInt32(strAccountingAmount) + Convert.ToInt32(strCredit) - Convert.ToInt32(strDebit)).ToString();
  252. }
  253. }
  254. }
  255. if (!string.IsNullOrEmpty(strAccountingSubID) && strAccountingAmount != "0")
  256. {
  257. DataGridViewRow dgvRow = new DataGridViewRow();
  258. dgvRow.CreateCells(dgvExpense);
  259. dgvRow.Cells[0].Value = strAccountingSubID; //科目代號
  260. dgvRow.Cells[1].Value = strAccountingSubName; //科目名稱
  261. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strAccountingAmount); //金額
  262. dgvExpense.Rows.Add(dgvRow);
  263. strExpenseCount = (Convert.ToInt32(strExpenseCount) + Convert.ToInt32(strAccountingAmount)).ToString();
  264. }
  265. }
  266. lbExpense.Text = UtilityClass.MarkNumber(strExpenseCount);
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. ErrorHandler.WriteErrorLog("IncomeStatement.cs", ex);
  272. }
  273. }
  274. private void GoEvent()
  275. {
  276. IncomeCount();
  277. ExpenseCount();
  278. UtilityClass.SetGridColor(dgvIncome);
  279. UtilityClass.SetGridColor(dgvExpense);
  280. //計算稅前淨利
  281. string strIncome = lbIncome.Text.ToString();
  282. string strExpenxe = lbExpense.Text.ToString();
  283. strIncome = string.IsNullOrEmpty(strIncome) ? "0" : strIncome.Replace(",", "");
  284. strExpenxe = string.IsNullOrEmpty(strExpenxe) ? "0" : strExpenxe.Replace(",", "");
  285. lbNetAfterTax.Text = UtilityClass.MarkNumber(Convert.ToInt32(strIncome) - Convert.ToInt32(strExpenxe));
  286. ShowIncomeExpendChar();
  287. ShowIncomeListChar();
  288. ShowExpendListChar();
  289. }
  290. private void ShowIncomeExpendChar()
  291. {
  292. string[] strItems = {"收入總計", "費用總計" };
  293. int[] intValues = { Convert.ToInt32(lbIncome.Text.Replace(",","")), Convert.ToInt32(lbExpense.Text.Replace(",","")) };
  294. crNet.Series["Series1"]["PieLabelStyle"] = "Inside";
  295. crNet.Series["Series1"].Points.DataBindXY(strItems, intValues);
  296. crNet.Series["Series1"].IsValueShownAsLabel = true;
  297. }
  298. private void ShowIncomeListChar()
  299. {
  300. string[] strItems = new string[dgvIncome.Rows.Count];
  301. int[] intValues = new int[dgvIncome.Rows.Count];
  302. for (int intCount = 0; intCount < dgvIncome.Rows.Count; intCount++ )
  303. {
  304. strItems[intCount] = dgvIncome.Rows[intCount].Cells["cInAccountingSubject"].Value.ToString();
  305. intValues[intCount] = Convert.ToInt32(dgvIncome.Rows[intCount].Cells["cInAccountingAmount"].Value.ToString().Replace(",",""));
  306. }
  307. crIncomeList.Series["Series1"].Points.DataBindXY(strItems, intValues);
  308. crIncomeList.Series["Series1"].IsValueShownAsLabel = true;
  309. }
  310. private void ShowExpendListChar()
  311. {
  312. string[] strItems = new string[dgvExpense.Rows.Count];
  313. int[] intValues = new int[dgvExpense.Rows.Count];
  314. for (int intCount = 0; intCount < dgvExpense.Rows.Count; intCount++)
  315. {
  316. strItems[intCount] = dgvExpense.Rows[intCount].Cells["cExAccountingSubject"].Value.ToString();
  317. intValues[intCount] = Convert.ToInt32(dgvExpense.Rows[intCount].Cells["cExAccountingAmount"].Value.ToString().Replace(",", ""));
  318. }
  319. crExpendList.Series["Series1"].Points.DataBindXY(strItems, intValues);
  320. crExpendList.Series["Series1"].IsValueShownAsLabel = true;
  321. }
  322. #endregion
  323. #region 事件觸發及問題處理
  324. private void IncomeStatement_Load(object sender, EventArgs e)//畫面載入設定
  325. {
  326. try
  327. {
  328. strActiveUserID = MainForm.strActiveUserID;
  329. strAccountingBookID = MainForm.strAccountingBookID;
  330. strKey = MainForm.strKey;
  331. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + ".xml";
  332. string strDC = "";
  333. if (File.Exists(strXMLPath))
  334. {
  335. XDocument xmlContent = XDocument.Load(strXMLPath);
  336. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  337. {
  338. strDC = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString();
  339. switch (strDC)
  340. {
  341. case "D":
  342. strTaxAccountingID = xmlData.Element("SubAccounting").Value.ToString();
  343. break;
  344. case "C":
  345. break;
  346. }
  347. }
  348. }
  349. if (MainForm.strKey == "")
  350. {
  351. //設定Toolbar初始狀態
  352. tsbSearch.Enabled = false;
  353. }
  354. //設定畫面初始狀態
  355. StatusChange("NONE");
  356. GoEvent();
  357. }
  358. catch (Exception ex)
  359. {
  360. ErrorHandler.WriteErrorLog("IncomeStatement.cs", ex);
  361. }
  362. }
  363. private void tsbSearch_Click(object sender, EventArgs e)
  364. {
  365. CleanForm();
  366. tsbSearch.Visible = false;
  367. tsbOK.Visible = true;
  368. tsbCancel.Visible = true;
  369. dpAccStart.Enabled = true;
  370. dpAccEnd.Enabled = true;
  371. }
  372. private void tsbCancel_Click(object sender, EventArgs e)
  373. {
  374. tsbSearch.Visible = true;
  375. tsbOK.Visible = false;
  376. tsbCancel.Visible = false;
  377. LockForm();
  378. }
  379. private void tsbOK_Click(object sender, EventArgs e)
  380. {
  381. GoEvent();
  382. tsbSearch.Visible = true;
  383. tsbOK.Visible = false;
  384. tsbCancel.Visible = false;
  385. LockForm();
  386. }
  387. private void tsbExit_Click(object sender, EventArgs e)
  388. {
  389. this.Close();
  390. }
  391. private void tsbSetup_Click(object sender, EventArgs e)
  392. {
  393. XMLSetting frmSettingForm = new XMLSetting();
  394. frmSettingForm.Owner = this;
  395. frmSettingForm.strOwnerForm = this.Name.ToString();
  396. frmSettingForm.strXMLName = this.Name.ToString();
  397. frmSettingForm.StartPosition = FormStartPosition.CenterParent;
  398. frmSettingForm.ShowDialog();
  399. }
  400. private void tsbClean_Click(object sender, EventArgs e)
  401. {
  402. CleanForm();
  403. }
  404. #endregion
  405. }
  406. }