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.

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