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.

576 lines
23 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.Xml.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Data.Sql;
  12. using System.Data.SqlClient;
  13. using System.Xml;
  14. using System.Configuration;
  15. using System.IO;
  16. using ManagementSystem.Utility;
  17. namespace ManagementSystem
  18. {
  19. public partial class CashFlowForecast : Form
  20. {
  21. //程式內共用物件
  22. public string strFrmStatus = ""; //表單狀態
  23. public string strPKey = ""; //程式內的Key值
  24. public string strOwnerForm = ""; //呼叫的Form Name
  25. public string strKey = ""; //程式內加密的Key值
  26. public string strActiveUserID = ""; //取得登入作用的使用者帳號
  27. public string strAccountingBookID = ""; //取得作用中的帳本
  28. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  29. SqlTransaction sqlTran;
  30. SqlDataAdapter sqlAdapter = new SqlDataAdapter();
  31. SqlCommand sqlCmd = new SqlCommand();
  32. int intStartCount = 0; //當下Gridview所有的資料筆數
  33. Int32 intAssetsCount = 0; //記錄本次資金餘額
  34. public CashFlowForecast()
  35. {
  36. InitializeComponent();
  37. }
  38. #region 自定義程式
  39. public void SetupStatus() //畫面載入設定
  40. {
  41. try
  42. {
  43. strActiveUserID = MainForm.strActiveUserID;
  44. strAccountingBookID = MainForm.strAccountingBookID;
  45. strKey = MainForm.strKey;
  46. if (MainForm.strKey == "")
  47. {
  48. //設定Toolbar初始狀態
  49. tsbAdd.Enabled = false;
  50. tsbSave.Enabled = false;
  51. tsbDelete.Enabled = false;
  52. }
  53. //設定畫面初始狀態
  54. StatusChange("NONE");
  55. LoadAssets();
  56. LoadRest();
  57. UtilityClass.SetGridColor(dgvCashFlow);
  58. }
  59. catch (Exception ex)
  60. {
  61. ErrorHandler.WriteErrorLog("CashFlowForecast.cs", ex);
  62. }
  63. }
  64. private void CleanForm() //清除畫面
  65. {
  66. dgvCashFlow.Rows.Clear();
  67. }
  68. private void CleanToolbar() //清除工具列
  69. {
  70. //設定Toolbar狀態
  71. tsbSearch.Visible = false;
  72. tsbAdd.Visible = true;
  73. tsbAdd.Enabled = true;
  74. tsbEdit.Visible = true;
  75. tsbEdit.Enabled = true;
  76. tsbDelete.Visible = false;
  77. tsbDelete.Enabled = false;
  78. tsbSave.Visible = false;
  79. tsbOK.Visible = false;
  80. tsbCancel.Visible = false;
  81. }
  82. private void UnLockForm() //解除限制唯讀物件
  83. {
  84. dgvCashFlow.ReadOnly = false;
  85. }
  86. private void LockForm() //限制唯讀物件
  87. {
  88. dgvCashFlow.ReadOnly = true;
  89. }
  90. private void SearchEvent() //查詢事件
  91. {
  92. }
  93. private void EditEven() //修改事件
  94. {
  95. }
  96. private void StatusChange(string strStatus) //變更主畫面狀態
  97. {
  98. switch (strStatus.ToUpper())
  99. {
  100. case "NONE":
  101. CleanForm();
  102. CleanToolbar();
  103. LockForm();
  104. dgvCashFlow.AllowUserToAddRows = false;
  105. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  106. strFrmStatus = "";
  107. intStartCount = dgvCashFlow.Rows.Count;
  108. break;
  109. case "SEARCH":
  110. UnLockForm();
  111. dgvCashFlow.DataSource = null;
  112. Application.DoEvents();
  113. tsbSearch.Visible = false;
  114. tsbAdd.Enabled = false;
  115. tsbEdit.Enabled = false;
  116. tsbDelete.Enabled = false;
  117. tsbOK.Visible = true;
  118. tsbCancel.Visible = true;
  119. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  120. strFrmStatus = "SEARCH";
  121. break;
  122. case "ADD":
  123. UnLockForm();
  124. dgvCashFlow.AllowUserToAddRows = true;
  125. tsbSearch.Enabled = false;
  126. tsbAdd.Visible = false;
  127. tsbEdit.Enabled = false;
  128. tsbSave.Visible = true;
  129. tsbCancel.Visible = true;
  130. tsbDelete.Enabled = false;
  131. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  132. strFrmStatus = "ADD";
  133. break;
  134. case "MODIFY":
  135. UnLockForm();
  136. tsbSearch.Enabled = false;
  137. tsbAdd.Enabled = false;
  138. tsbEdit.Visible = false;
  139. tsbSave.Visible = true;
  140. tsbCancel.Visible = true;
  141. tsbDelete.Enabled = false;
  142. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  143. strFrmStatus = "MODIFY";
  144. break;
  145. case "DEL":
  146. UnLockForm();
  147. tsbSearch.Enabled = false;
  148. tsbSave.Visible = true;
  149. tsbCancel.Visible = true;
  150. tsbDelete.Visible = false;
  151. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  152. strFrmStatus = "DEL";
  153. break;
  154. }
  155. UtilityClass.SetGridColor(dgvCashFlow);
  156. }
  157. private int CheckSelected()
  158. {
  159. int intCheckCount = 0;
  160. foreach (DataGridViewRow row in dgvCashFlow.Rows)
  161. {
  162. DataGridViewCheckBoxCell Selcell = (DataGridViewCheckBoxCell)row.Cells["cDel"]; //選擇狀態列
  163. if (Selcell.Value != null)
  164. {
  165. if (Selcell.Value == Selcell.TrueValue)
  166. {
  167. intCheckCount += 1;
  168. }
  169. }
  170. }
  171. return intCheckCount;
  172. }
  173. private void GoDel()
  174. {
  175. if (CheckSelected() == 0)
  176. {
  177. if (MessageBox.Show("刪除的資料將無法還原,請問您是否繼續刪除的動作?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  178. {
  179. //進行刪除的動作
  180. }
  181. }
  182. else
  183. {
  184. MessageBox.Show("請先選擇要刪除的資料!", "提示");
  185. }
  186. }
  187. private Int32 CountAssets(string strAccountingSubjectID)
  188. {
  189. try
  190. {
  191. Int32 intCount = 0;
  192. string strDebit = "";
  193. string strCredit = "";
  194. string strAssetsList = "Select AccountingID, AccountingDate, AccountingOrder, Debit, Credit, ProjectNumber, ProjectCName, Memo From OTB_FNC_AccountingJournal Where AccountingBookID = '" + strAccountingBookID + "' And AccountingID like '" + DateTime.Now.Year.ToString() + "%' And AccountingSubID ='" + strAccountingSubjectID + "' Order by AccountingID ASC";
  195. DataTable dtTemp = UtilityClass.GetSQLResult(strAssetsList).Tables["Result"];
  196. foreach (DataRow dr in dtTemp.Rows)
  197. {
  198. strDebit = UtilityClass.DecryptDES(dr["Debit"].ToString(),strKey).Replace(",","");
  199. if (strDebit == "") strDebit = "0";
  200. strCredit = UtilityClass.DecryptDES(dr["Credit"].ToString(),strKey).Replace(",","");
  201. if (strCredit == "") strCredit = "0";
  202. intCount = intCount + Convert.ToInt32(strDebit) - Convert.ToInt32(strCredit);
  203. }
  204. return intCount;
  205. }
  206. catch
  207. {
  208. return 0;
  209. }
  210. }
  211. private void LoadAssets()
  212. {
  213. try
  214. {
  215. intAssetsCount = 0;
  216. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + ".xml";
  217. if (File.Exists(strXMLPath))
  218. {
  219. XDocument xmlContent = XDocument.Load(strXMLPath);
  220. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  221. {
  222. intAssetsCount += CountAssets(xmlData.Element("SubAccounting").Value.ToString());
  223. }
  224. }
  225. //建立最新餘額資料
  226. DataGridViewRow dgvRow = new DataGridViewRow();
  227. dgvRow.CreateCells(dgvCashFlow);
  228. dgvRow.ReadOnly = true;
  229. dgvRow.Cells[1].Value = DateTime.Now.ToShortDateString(); //預估日期
  230. dgvRow.Cells[4].Value = "實際餘額";
  231. dgvRow.Cells[7].Value = UtilityClass.MarkNumber(intAssetsCount.ToString()); //實際餘額
  232. dgvCashFlow.Rows.Add(dgvRow);
  233. }
  234. catch (Exception ex)
  235. {
  236. ErrorHandler.WriteErrorLog("CashFlowForecast.cs", ex);
  237. }
  238. }
  239. private void LoadRest() //整理餘額資料
  240. {
  241. try
  242. {
  243. string strIncome = "";
  244. string strExpand = "";
  245. string strSQL = "Select * From OTB_FNC_CashFlowForecast Where AccountingBookID = '" + strAccountingBookID + "' Order by ForecastDate";
  246. DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
  247. if (dtTemp.Rows.Count > 0)
  248. {
  249. foreach (DataRow dr in dtTemp.Rows)
  250. {
  251. strIncome = "";
  252. strExpand = "";
  253. DataGridViewRow dgvForcastRow = new DataGridViewRow();
  254. dgvForcastRow.CreateCells(dgvCashFlow);
  255. dgvForcastRow.Cells[1].Value = DateTime.Parse(dr["ForecastDate"].ToString()).ToShortDateString(); ; //預估日期
  256. dgvForcastRow.Cells[2].Value = dr["CustomerName"].ToString(); //客戶名稱
  257. dgvForcastRow.Cells[3].Value = dr["InvoiceNo"].ToString(); //發票號碼
  258. dgvForcastRow.Cells[4].Value = dr["Memo"].ToString(); //備註
  259. strIncome = string.IsNullOrEmpty(dr["ForecastIncome"].ToString()) ? "" : UtilityClass.DecryptDES(dr["ForecastIncome"].ToString(), strKey); //預估收入
  260. strExpand = string.IsNullOrEmpty(dr["ForecastExpand"].ToString()) ? "" : UtilityClass.DecryptDES(dr["ForecastExpand"].ToString(), strKey); //預估收入
  261. dgvForcastRow.Cells[5].Value = strIncome; //預估收入
  262. dgvForcastRow.Cells[6].Value = strExpand; //預估支出
  263. intAssetsCount = intAssetsCount + Convert.ToInt32(string.IsNullOrEmpty(strIncome) ? "0" : strIncome.Replace(",", "")) - Convert.ToInt32(string.IsNullOrEmpty(strExpand) ? "0" : strExpand.Replace(",", ""));
  264. dgvForcastRow.Cells[7].Value = UtilityClass.MarkNumber(intAssetsCount.ToString()); //實際餘額
  265. dgvForcastRow.Cells[8].Value = dr["ItemNo"].ToString(); //專案名稱
  266. dgvForcastRow.Cells[10].Value = dr["ProjectCName"].ToString(); //專案名稱
  267. dgvCashFlow.Rows.Add(dgvForcastRow);
  268. }
  269. }
  270. }
  271. catch (Exception ex)
  272. {
  273. ErrorHandler.WriteErrorLog("CashFlowForecast.cs", ex);
  274. }
  275. }
  276. private void ReLoadRest() //重新整理餘額資料
  277. {
  278. string strIncome = "";
  279. string strExpand = "";
  280. string strAmount = "";
  281. try
  282. {
  283. if (dgvCashFlow.Rows.Count > 0)
  284. {
  285. foreach (DataGridViewRow dr in dgvCashFlow.Rows)
  286. {
  287. if (dr.Index == 0)
  288. {
  289. strAmount = (string)dr.Cells[7].Value;
  290. }
  291. else
  292. {
  293. strIncome = "";
  294. strExpand = "";
  295. strAmount = (string)dgvCashFlow.Rows[dr.Index - 1].Cells[7].Value;
  296. strIncome = (string)dr.Cells[5].Value; //預估收入
  297. strExpand = (string)dr.Cells[6].Value; //預估支出
  298. intAssetsCount = Convert.ToInt32(string.IsNullOrEmpty(strAmount) ? "0" : strAmount.Replace(",", "")) + Convert.ToInt32(string.IsNullOrEmpty(strIncome) ? "0" : strIncome.Replace(",", "")) - Convert.ToInt32(string.IsNullOrEmpty(strExpand) ? "0" : strExpand.Replace(",", ""));
  299. dr.Cells[7].Value = UtilityClass.MarkNumber(intAssetsCount.ToString()); //實際餘額
  300. }
  301. }
  302. }
  303. }
  304. catch (Exception ex)
  305. {
  306. ErrorHandler.WriteErrorLog("CashFlowForecast.cs", ex);
  307. }
  308. }
  309. #endregion
  310. #region 事件觸發及問題處理
  311. private void CashFlowForecast_Load(object sender, EventArgs e)
  312. {
  313. SetupStatus();
  314. }
  315. private void tsbExit_Click(object sender, EventArgs e)
  316. {
  317. this.Close();
  318. }
  319. private void tsbClean_Click(object sender, EventArgs e)
  320. {
  321. SetupStatus();
  322. }
  323. private void tsbSearch_Click(object sender, EventArgs e)
  324. {
  325. }
  326. private void tsbCancel_Click(object sender, EventArgs e)
  327. {
  328. SetupStatus();
  329. }
  330. private void tsbAdd_Click(object sender, EventArgs e)
  331. {
  332. StatusChange("Add");
  333. CashFlowForecastEdit actForm = new CashFlowForecastEdit();
  334. actForm.strFrmStatus = "ADD";
  335. actForm.Owner = this;
  336. actForm.StartPosition = FormStartPosition.CenterParent;
  337. actForm.ShowDialog();
  338. }
  339. private void tsbEdit_Click(object sender, EventArgs e)
  340. {
  341. StatusChange("Modify");
  342. UnLockForm();
  343. }
  344. private void tsbDelete_Click(object sender, EventArgs e)
  345. {
  346. StatusChange("Del");
  347. }
  348. private void tsbSetup_Click(object sender, EventArgs e)
  349. {
  350. XMLSetting frmSettingForm = new XMLSetting();
  351. frmSettingForm.Owner = this;
  352. frmSettingForm.strOwnerForm = this.Name.ToString();
  353. frmSettingForm.strXMLName = this.Name.ToString();
  354. frmSettingForm.StartPosition = FormStartPosition.CenterParent;
  355. frmSettingForm.ShowDialog();
  356. }
  357. private void dgvCashFlow_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  358. {
  359. string strValue = "";
  360. if (dgvCashFlow.Columns[e.ColumnIndex].Name.ToString() == "cIncome")
  361. {
  362. strValue = (string)dgvCashFlow.Rows[e.RowIndex].Cells["cIncome"].Value;
  363. if (string.IsNullOrEmpty(strValue))
  364. {
  365. return;
  366. }
  367. else
  368. {
  369. dgvCashFlow.Rows[e.RowIndex].Cells["cIncome"].Value = strValue.Replace(",", "");
  370. }
  371. }
  372. if (dgvCashFlow.Columns[e.ColumnIndex].Name.ToString() == "cExpand")
  373. {
  374. strValue = (string)dgvCashFlow.Rows[e.RowIndex].Cells["cExpand"].Value;
  375. if (string.IsNullOrEmpty(strValue))
  376. {
  377. return;
  378. }
  379. else
  380. {
  381. dgvCashFlow.Rows[e.RowIndex].Cells["cExpand"].Value = strValue.Replace(",", "");
  382. }
  383. }
  384. }
  385. private void dgvCashFlow_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  386. {
  387. //數字呈現方式調整
  388. string strValue = "";
  389. if (dgvCashFlow.Columns[e.ColumnIndex].Name.ToString() == "cIncome")
  390. {
  391. strValue = (string)dgvCashFlow.Rows[e.RowIndex].Cells["cIncome"].Value;
  392. if (string.IsNullOrEmpty(strValue))
  393. {
  394. return;
  395. }
  396. else
  397. {
  398. strValue = strValue.Replace(",", "");
  399. dgvCashFlow.Rows[e.RowIndex].Cells["cIncome"].Value = UtilityClass.MarkNumber(strValue);
  400. }
  401. }
  402. if (dgvCashFlow.Columns[e.ColumnIndex].Name.ToString() == "cExpand")
  403. {
  404. strValue = (string)dgvCashFlow.Rows[e.RowIndex].Cells["cExpand"].Value;
  405. if (string.IsNullOrEmpty(strValue))
  406. {
  407. return;
  408. }
  409. else
  410. {
  411. strValue = strValue.Replace(",", "");
  412. dgvCashFlow.Rows[e.RowIndex].Cells["cExpand"].Value = UtilityClass.MarkNumber(strValue);
  413. }
  414. }
  415. }
  416. private void tsbSave_Click(object sender, EventArgs e) //存在判斷刪除狀態的問題
  417. {
  418. string strItemNo = "";
  419. string strIncome = "";
  420. string strExpand = "";
  421. string strSQL = "";
  422. try
  423. {
  424. foreach (DataGridViewRow dr in dgvCashFlow.Rows)
  425. {
  426. strItemNo = (string)dr.Cells["cItemNo"].Value;
  427. DataGridViewCheckBoxCell Delcell = (DataGridViewCheckBoxCell)dr.Cells["cDel"]; //刪除狀態列
  428. if (Delcell.Value == Delcell.TrueValue)
  429. {
  430. //判斷刪除被選取
  431. if (!string.IsNullOrEmpty(strItemNo))
  432. {
  433. strSQL = "Delete OTB_FNC_CashFlowForecast Where ItemNo = '" + (string)dr.Cells["cItemNo"].Value + "'";
  434. UtilityClass.RunSQLNonReturn(strSQL);
  435. }
  436. }
  437. else
  438. {
  439. //進行更新
  440. if (!string.IsNullOrEmpty(strItemNo))
  441. {
  442. strIncome = (string)dr.Cells["cIncome"].Value;
  443. strExpand = (string)dr.Cells["cExpand"].Value;
  444. strSQL = "Update OTB_FNC_CashFlowForecast Set ";
  445. strSQL += " ForecastDate = '" + dr.Cells["cForecastDate"].Value.ToString() + "',";
  446. strSQL += " ForecastIncome = '" + (string.IsNullOrEmpty(strIncome) ? "" : UtilityClass.EncryptDES(strIncome, strKey)) + "',";
  447. strSQL += " ForecastExpand = '" + (string.IsNullOrEmpty(strExpand) ? "" : UtilityClass.EncryptDES(strExpand, strKey)) + "',";
  448. strSQL += " CustomerName = '" + (string)dr.Cells["cCustomerName"].Value + "',";
  449. strSQL += " InvoiceNo = '" + (string)dr.Cells["cInvoiceNo"].Value + "',";
  450. strSQL += " Memo = '" + (string)dr.Cells["cMemo"].Value + "',";
  451. strSQL += " ProjectNumber = '" + (string)dr.Cells["cProjectNumber"].Value + "',";
  452. strSQL += " ProjectCName = '" + (string)dr.Cells["cProjectCName"].Value + "'";
  453. strSQL += " Where ItemNo = '" + (string)dr.Cells["cItemNo"].Value + "'";
  454. UtilityClass.RunSQLNonReturn(strSQL);
  455. }
  456. }
  457. }
  458. MessageBox.Show("儲存成功", "提示");
  459. SetupStatus(); //截入初始狀態
  460. }
  461. catch (Exception ex)
  462. {
  463. MessageBox.Show("儲存有誤", "提示");
  464. ErrorHandler.WriteErrorLog("CashFlowForecast.cs", ex);
  465. }
  466. }
  467. private void tsbOK_Click(object sender, EventArgs e)
  468. {
  469. switch (strFrmStatus.ToString())
  470. {
  471. case "MODIFY": //修改
  472. EditEven();
  473. break;
  474. }
  475. }
  476. private void dgvCashFlow_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  477. {
  478. string strErrorMsg = "";
  479. try
  480. {
  481. //進行資料檢查
  482. if (string.IsNullOrEmpty((string)dgvCashFlow.Rows[e.RowIndex].Cells["cForecastDate"].Value))
  483. {
  484. strErrorMsg = "請輸入預估日期";
  485. }
  486. else
  487. {
  488. if (!UtilityClass.IsDate((string)dgvCashFlow.Rows[e.RowIndex].Cells["cForecastDate"].Value))
  489. {
  490. strErrorMsg = "預估日期格式不對";
  491. }
  492. else if(Convert.ToDateTime((string)dgvCashFlow.Rows[e.RowIndex].Cells["cForecastDate"].Value) < Convert.ToDateTime(DateTime.Now.ToShortDateString()))
  493. {
  494. strErrorMsg = "預估日期應大於今天";
  495. }
  496. }
  497. if ((string)dgvCashFlow.Rows[e.RowIndex].Cells["cMemo"].Value == "")
  498. {
  499. strErrorMsg = "備註不能等於空白";
  500. }
  501. if (string.IsNullOrEmpty((string)dgvCashFlow.Rows[e.RowIndex].Cells["cIncome"].Value) && string.IsNullOrEmpty((string)dgvCashFlow.Rows[e.RowIndex].Cells["cExpand"].Value) && e.RowIndex != 0)
  502. {
  503. strErrorMsg = "估預收入及支出不能同時等於空白";
  504. }
  505. if (strErrorMsg != "")
  506. {
  507. dgvCashFlow.Rows[e.RowIndex].ErrorText = strErrorMsg;
  508. e.Cancel = true;
  509. }
  510. }
  511. catch (Exception ex)
  512. {
  513. ErrorHandler.WriteErrorLog("CashFlowForecast.cs", ex);
  514. }
  515. }
  516. private void dgvCashFlow_RowValidated(object sender, DataGridViewCellEventArgs e)
  517. {
  518. dgvCashFlow.Rows[e.RowIndex].ErrorText = "";
  519. ReLoadRest();
  520. }
  521. #endregion
  522. }
  523. }