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.

498 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 AccountsPayable : Form
  19. {
  20. //程式內共用物件
  21. public bool blIsAdded = true; //判斷分錄是否寫入成功
  22. string strFrmStatus = ""; //表單狀態
  23. string strActiveUserID = ""; //取得登入作用的使用者帳號
  24. string strAccountingBookID = ""; //取得作用中的帳本
  25. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  26. SqlTransaction sqlTran;
  27. SqlCommand sqlCmd = new SqlCommand();
  28. DataSet sdInsurance = new System.Data.DataSet();
  29. string strKey = ""; //程式內加密的Key值
  30. public AccountsPayable()
  31. {
  32. InitializeComponent();
  33. }
  34. #region 自定義程式
  35. private void SetupStatus() //畫面載入設定
  36. {
  37. try
  38. {
  39. strActiveUserID = MainForm.strActiveUserID;
  40. strAccountingBookID = MainForm.strAccountingBookID;
  41. strKey = MainForm.strKey;
  42. if (MainForm.strKey == "")
  43. {
  44. //設定Toolbar初始狀態
  45. tsbAdd.Enabled = false;
  46. tsbSave.Enabled = false;
  47. tsbDelete.Enabled = false;
  48. }
  49. //設定畫面初始狀態
  50. StatusChange("NONE");
  51. }
  52. catch (Exception ex)
  53. {
  54. ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex);
  55. }
  56. }
  57. private void CleanForm()
  58. {
  59. txtCustomerName.Text = "";
  60. txtProjectName.Text = "";
  61. txtPaiedDateStart.Text = "";
  62. txtPaiedDateEnd.Text = "";
  63. txtInvoiceNo.Text = "";
  64. cbHistory.Checked = false;
  65. }
  66. private void CleanToolbar() //清除工具列
  67. {
  68. //設定Toolbar狀態
  69. tsbSearch.Visible = true;
  70. tsbSearch.Enabled = true;
  71. tsbAdd.Visible = false; //本程式不存在新功能
  72. tsbEdit.Visible = false;
  73. tsbEdit.Enabled = false;
  74. tsbDelete.Visible = false;
  75. tsbDelete.Enabled = false;
  76. tsbSave.Visible = false;
  77. tsbOK.Visible = false;
  78. tsbCancel.Visible = false;
  79. }
  80. private void LockForm() //限制唯讀物件
  81. {
  82. txtCustomerName.ReadOnly = true;
  83. txtProjectName.ReadOnly = true;
  84. txtPaiedDateEnd.ReadOnly = true;
  85. txtPaiedDateStart.ReadOnly = true;
  86. txtInvoiceNo.ReadOnly = true;
  87. cbHistory.Enabled = false;
  88. }
  89. private void UnLockForm() //解除限制唯讀物件
  90. {
  91. txtCustomerName.ReadOnly = false;
  92. txtProjectName.ReadOnly = false;
  93. txtPaiedDateEnd.ReadOnly = false;
  94. txtPaiedDateStart.ReadOnly = false;
  95. txtInvoiceNo.ReadOnly = false;
  96. cbHistory.Enabled = true;
  97. }
  98. private bool CheckForm()
  99. {
  100. if (!UtilityClass.IsDate(txtPaiedDateStart.Text.Trim()) && txtPaiedDateStart.Text.Trim() != "")
  101. {
  102. MessageBox.Show("起始時間格式有誤!", "提示");
  103. txtPaiedDateStart.Focus();
  104. return false;
  105. }
  106. if (!UtilityClass.IsDate(txtPaiedDateEnd.Text.Trim()) && txtPaiedDateEnd.Text.Trim() != "")
  107. {
  108. MessageBox.Show("結束時間格式有誤!", "提示");
  109. txtPaiedDateEnd.Focus();
  110. return false;
  111. }
  112. return true;
  113. }
  114. public void StatusChange(string strStatus) //變更主畫面狀態
  115. {
  116. try
  117. {
  118. switch (strStatus.ToUpper())
  119. {
  120. case "NONE":
  121. CleanForm();
  122. CleanToolbar();
  123. LockForm();
  124. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  125. strFrmStatus = "";
  126. tsbSearch.Enabled = true;
  127. break;
  128. case "SEARCH":
  129. UnLockForm();
  130. CleanForm();
  131. tsbSearch.Enabled = false;
  132. tsbEdit.Enabled = false;
  133. tsbDelete.Enabled = false;
  134. tsbSave.Visible = false;
  135. tsbOK.Visible = true;
  136. tsbCancel.Visible = true;
  137. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  138. strFrmStatus = "SEARCH";
  139. break;
  140. case "ADD":
  141. //本程式不提供新增功能
  142. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  143. strFrmStatus = "ADD";
  144. break;
  145. case "MODIFY":
  146. //本程式不提供修改功能
  147. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  148. strFrmStatus = "MODIFY";
  149. break;
  150. case "DEL":
  151. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  152. strFrmStatus = "DEL";
  153. break;
  154. }
  155. UtilityClass.SetGridColor(dgvAPManagement);
  156. }
  157. catch (Exception ex)
  158. {
  159. ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex);
  160. }
  161. }
  162. public void GoEvent() //執行事件
  163. {
  164. try
  165. {
  166. //物件宣告
  167. StringBuilder sbSQL = new StringBuilder();
  168. string strSalesOrderNo = ""; //訂單編號
  169. string strProjectCName = ""; //專案中文名稱
  170. string strProjectNumber = ""; //專案編號
  171. string strInvoiceNo = ""; //統一編號
  172. string strCustomerID = ""; //客戶編號
  173. string strCustomerName = ""; //客戶名稱
  174. string strActualPaiedDate = ""; //付款日期
  175. string strPayAmount = ""; //付款金額
  176. string strPayStatus = ""; //付款狀態
  177. string strMemo = ""; //備註
  178. switch (strFrmStatus)
  179. {
  180. case "SEARCH":
  181. dgvAPManagement.Rows.Clear();
  182. sbSQL.Clear();
  183. sbSQL.Append("Select SalesOrderNo, ProjectCName, ProjectNumber, InvoiceNo, CustomerID, CustomerName, ActualPaiedDate , PayAmount, PayStatus, Memo From OTB_FNC_AccountPayable ");
  184. sbSQL.Append(" Where AccountingBookID ='" + strAccountingBookID + "' ");
  185. if (txtInvoiceNo.Text.Trim() != "")
  186. {
  187. sbSQL.Append("AND InvoiceNo like '" + txtInvoiceNo.Text.Trim() + "' ");
  188. }
  189. if (txtCustomerName.Text.Trim() != "")
  190. {
  191. sbSQL.Append("AND CustomerName like '" + txtCustomerName.Text.Trim() + "' ");
  192. }
  193. if (txtProjectName.Text.Trim() != "")
  194. {
  195. sbSQL.Append("AND ProjectName like '" + txtProjectName.Text.Trim() + "' ");
  196. }
  197. if (txtPaiedDateStart.Text.Trim() != "")
  198. {
  199. sbSQL.Append("AND ActualPaiedDate >= '" + txtPaiedDateStart.Text.Trim() + "' ");
  200. }
  201. if (txtPaiedDateEnd.Text.Trim() != "")
  202. {
  203. sbSQL.Append("AND ActualPaiedDate <= '" + txtPaiedDateEnd.Text.Trim() + "' ");
  204. }
  205. if (cbHistory.Checked == true)
  206. {
  207. sbSQL.Append("And PayStatus = 'Y' ");
  208. }
  209. else
  210. {
  211. sbSQL.Append("And PayStatus = 'N' ");
  212. }
  213. DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"];
  214. foreach (DataRow drData in dtTemp.Rows)
  215. {
  216. DataGridViewRow dgvRow = new DataGridViewRow();
  217. dgvRow.CreateCells(dgvAPManagement);
  218. //逐行產生資料
  219. strSalesOrderNo = (string)drData["SalesOrderNo"];
  220. strProjectCName = (string)drData["ProjectCName"];
  221. strProjectNumber = (string)drData["ProjectNumber"];
  222. strInvoiceNo = (string)drData["InvoiceNo"];
  223. strCustomerID = (string)drData["CustomerID"];
  224. strCustomerName = (string)drData["CustomerName"];
  225. if (drData["ActualPaiedDate"] != DBNull.Value)
  226. {
  227. strActualPaiedDate = Convert.ToDateTime(drData["ActualPaiedDate"]).ToShortDateString();
  228. }
  229. strPayAmount = (string)drData["PayAmount"];
  230. strPayStatus = (string)drData["PayStatus"];
  231. strMemo = (string)drData["Memo"];
  232. //產生Row
  233. dgvRow.Cells[1].Value = string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo;
  234. dgvRow.Cells[2].Value = string.IsNullOrEmpty(strProjectCName) ? "" : strProjectCName;
  235. dgvRow.Cells[3].Value = string.IsNullOrEmpty(strProjectNumber) ? "" : strProjectNumber;
  236. dgvRow.Cells[4].Value = string.IsNullOrEmpty(strCustomerID) ? "" : strCustomerID;
  237. dgvRow.Cells[5].Value = string.IsNullOrEmpty(strCustomerName) ? "" : strCustomerName;
  238. dgvRow.Cells[6].Value = string.IsNullOrEmpty(strInvoiceNo) ? "" : strInvoiceNo;
  239. dgvRow.Cells[7].Value = strActualPaiedDate;
  240. dgvRow.Cells[8].Value = string.IsNullOrEmpty(strPayAmount) ? "" : UtilityClass.DecryptDES(strPayAmount, strKey);
  241. dgvRow.Cells[9].Value = string.IsNullOrEmpty(strMemo) ? "" : strMemo;
  242. dgvRow.Cells[10].Value = string.IsNullOrEmpty(strPayStatus) ? "" : strPayStatus;
  243. if (drData["PayStatus"].ToString() == "Y")
  244. {
  245. dgvRow.ReadOnly = true;
  246. }
  247. dgvAPManagement.Rows.Add(dgvRow);
  248. }
  249. StatusChange("NONE");
  250. break;
  251. case "MODIFY":
  252. //本程式不提供維護功能
  253. break;
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex);
  259. sqlTran.Rollback();
  260. }
  261. }
  262. public void UpdateAP(DataGridViewCellEventArgs e) //更新應收帳款記錄
  263. {
  264. try
  265. {
  266. //物件宣告
  267. string strInvoiceNo = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  268. string strPaiedDate = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value;
  269. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  270. {
  271. string strSQL = "Update OTB_FNC_AccountPayable Set ActualPaiedDate = @ActualPaiedDate , PayStatus = 'Y' , ModifyDate = GetDate(), ModifyUser = @ModifyUser Where AccountingBookID = @AccountingBookID And InvoiceNo = @InvoiceNo ";
  272. strSQL += "UPdate OTB_SAL_PayPlan Set ActualPayDate = @ActualPaiedDate, ModifyDate = GetDate(), ModifyUser = @ModifyUser Where AccountingBookID = @AccountingBookID And InvoiceNo = @InvoiceNo ";
  273. //添加參數
  274. sqlAdapter.UpdateCommand = new SqlCommand();
  275. sqlAdapter.UpdateCommand.Connection = sqlConn;
  276. sqlAdapter.UpdateCommand.Transaction = sqlTran;
  277. sqlAdapter.UpdateCommand.CommandText = strSQL;
  278. sqlAdapter.UpdateCommand.Parameters.AddRange
  279. (
  280. new SqlParameter[]
  281. {
  282. new SqlParameter("@AccountingBookID",string.IsNullOrEmpty(strAccountingBookID) ? "" : strAccountingBookID),
  283. new SqlParameter("@InvoiceNo",string.IsNullOrEmpty(strInvoiceNo) ? "" : strInvoiceNo),
  284. new SqlParameter("@ModifyUser",strActiveUserID)
  285. }
  286. );
  287. SqlParameter spActualPaiedDate = new SqlParameter("@ActualPaiedDate", SqlDbType.DateTime);
  288. spActualPaiedDate.Value = strPaiedDate;
  289. sqlAdapter.UpdateCommand.Parameters.Add(spActualPaiedDate);
  290. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  291. {
  292. sqlConn.Open();
  293. }
  294. sqlAdapter.UpdateCommand.ExecuteNonQuery();
  295. MessageBox.Show("核銷成功", "提示");
  296. }
  297. }
  298. catch (Exception ex)
  299. {
  300. throw ex;
  301. }
  302. }
  303. #endregion
  304. #region 事件觸發及問題處理
  305. private void AccountsPayable_Load(object sender, EventArgs e)
  306. {
  307. SetupStatus();
  308. StatusChange("SEARCH");
  309. GoEvent();
  310. }
  311. private void tsbClean_Click(object sender, EventArgs e)
  312. {
  313. CleanForm();
  314. }
  315. private void tsbCancel_Click(object sender, EventArgs e)
  316. {
  317. CleanToolbar();
  318. StatusChange("NONE");
  319. }
  320. private void tsbOK_Click(object sender, EventArgs e)
  321. {
  322. try
  323. {
  324. if (CheckForm())
  325. {
  326. dgvAPManagement.Rows.Clear();
  327. GoEvent();
  328. //還原Toolbar狀態
  329. CleanToolbar();
  330. //關閉查詢條件
  331. LockForm();
  332. StatusChange("NONE");
  333. }
  334. }
  335. catch (Exception ex)
  336. {
  337. ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex);
  338. }
  339. }
  340. private void tsbSearch_Click(object sender, EventArgs e)
  341. {
  342. UnLockForm();
  343. StatusChange("SEARCH");
  344. txtCustomerName.Focus();
  345. tsbOK.Visible = true;
  346. tsbCancel.Visible = true;
  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 dgvAPManagement_CellContentClick(object sender, DataGridViewCellEventArgs e)
  358. {
  359. //物件宣告
  360. string strDC = "";
  361. string strInvoiceNo = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  362. string strPayAmount = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cPayAmount"].Value;
  363. string strProjectName = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cProjectName"].Value;
  364. string strProjectNumber = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cProjectNumber"].Value;
  365. try
  366. {
  367. //當GridView中的Button被按下
  368. var SenderGrid = (DataGridView)sender;
  369. if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
  370. {
  371. if (SenderGrid.Rows[e.RowIndex].ReadOnly == true) //判定唯讀則不處理
  372. {
  373. return;
  374. }
  375. //Form設定
  376. AccountingEntries acItemForm = new AccountingEntries();
  377. acItemForm.strOwnerForm = "AccountsPay";
  378. acItemForm.Owner = this;
  379. acItemForm.StartPosition = FormStartPosition.CenterParent;
  380. acItemForm.strFrmStatus = "ADD";
  381. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + ".xml";
  382. if (File.Exists(strXMLPath))
  383. {
  384. XDocument xmlContent = XDocument.Load(strXMLPath);
  385. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  386. {
  387. DataGridViewRow dgvRow = new DataGridViewRow();
  388. dgvRow.CreateCells(acItemForm.dgvDataMaintain);
  389. strDC = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString();
  390. switch (strDC)
  391. {
  392. case "D":
  393. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  394. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  395. dgvRow.Cells[3].Value = "發票號碼:" + (string)dgvAPManagement.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  396. dgvRow.Cells[4].Value = strPayAmount;
  397. break;
  398. case "C":
  399. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  400. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  401. dgvRow.Cells[3].Value = "發票號碼:" + (string)dgvAPManagement.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  402. dgvRow.Cells[5].Value = strPayAmount;
  403. break;
  404. }
  405. blIsAdded = false;
  406. if (string.IsNullOrEmpty((string)dgvAPManagement.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value))
  407. {
  408. dgvAPManagement.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value = DateTime.Now.ToString("yyyy/MM/dd");
  409. }
  410. acItemForm.dgvDataMaintain.Rows.Add(dgvRow);
  411. }
  412. acItemForm.txtProjectName.Text = strProjectName;
  413. acItemForm.txtProjectNumber.Text = strProjectNumber;
  414. acItemForm.dgeArgs = e;
  415. }
  416. acItemForm.ShowDialog();
  417. }
  418. }
  419. catch (Exception ex)
  420. {
  421. ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex);
  422. }
  423. }
  424. private void dgvAPManagement_RowValidated(object sender, DataGridViewCellEventArgs e)
  425. {
  426. dgvAPManagement.Rows[e.RowIndex].ErrorText = "";
  427. }
  428. private void dgvAPManagement_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  429. {
  430. string strActualPaiedDate = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value;
  431. if (!string.IsNullOrEmpty(strActualPaiedDate))
  432. {
  433. if (!UtilityClass.IsDate(strActualPaiedDate))
  434. {
  435. dgvAPManagement.Rows[e.RowIndex].ErrorText = "日期格式不正確!";
  436. e.Cancel = true;
  437. }
  438. }
  439. }
  440. private void tsbExit_Click(object sender, EventArgs e)
  441. {
  442. this.Close();
  443. }
  444. #endregion
  445. }
  446. }