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.

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