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.

3480 lines
197 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.Xml.Linq;
  14. using System.IO;
  15. using ManagementSystem.Utility;
  16. namespace ManagementSystem
  17. {
  18. public partial class PayPlanManagement : 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 PayPlanManagement()
  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. //設定畫面初始狀態
  43. StatusChange("NONE");
  44. }
  45. catch (Exception ex)
  46. {
  47. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  48. }
  49. }
  50. public void StatusChange(string strStatus) //變更主畫面狀態
  51. {
  52. try
  53. {
  54. switch (strStatus.ToUpper())
  55. {
  56. case "NONE":
  57. CleanForm();
  58. CleanToolbar();
  59. LockForm();
  60. dgvOrderContent.AllowUserToAddRows = false;
  61. dgvOutSourcingContent.AllowUserToAddRows = false;
  62. dgvReceiptPlan.AllowUserToAddRows = false;
  63. dgvOrderList.ReadOnly = true;
  64. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  65. strFrmStatus = "";
  66. tsbSearch.Enabled = true;
  67. break;
  68. case "SEARCH":
  69. UnLockFormHead();
  70. CleanForm();
  71. btnGetSalesOrderNo.Enabled = false;
  72. tsbSearch.Visible = false;
  73. tsbEdit.Enabled = false;
  74. tsbDelete.Enabled = false;
  75. tsbSave.Visible = false;
  76. tsbOK.Visible = true;
  77. tsbCancel.Visible = true;
  78. dgvOrderContent.AllowUserToAddRows = false;
  79. dgvOutSourcingContent.AllowUserToAddRows = false;
  80. dgvReceiptPlan.AllowUserToAddRows = false;
  81. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  82. strFrmStatus = "SEARCH";
  83. break;
  84. case "ADD":
  85. //本程式不提供新增功能
  86. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  87. strFrmStatus = "ADD";
  88. break;
  89. case "MODIFY":
  90. UnLockFormHead();
  91. UnLockFormDetail();
  92. UnLockFormSubDetail();
  93. tsbSearch.Enabled = false;
  94. tsbEdit.Visible = false;
  95. tsbDelete.Enabled = false;
  96. tsbSave.Visible = true;
  97. tsbCancel.Visible = true;
  98. dgvOrderContent.AllowUserToAddRows = true;
  99. dgvOutSourcingContent.AllowUserToAddRows = true;
  100. dgvReceiptPlan.AllowUserToAddRows = true;
  101. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  102. strFrmStatus = "MODIFY";
  103. break;
  104. case "DEL":
  105. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  106. strFrmStatus = "DEL";
  107. break;
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  113. }
  114. }
  115. private void CleanForm() //清除畫面
  116. {
  117. blIsAdded = true;
  118. CleanFormHead();
  119. CleanFormDetail();
  120. CleanFormSubDetail();
  121. }
  122. private void CleanFormHead() //清除畫面
  123. {
  124. //設定畫面物件狀態
  125. //清除GridView
  126. txtSalesOrderNo.Text = "";
  127. txtCustomerID.Text = "";
  128. txtCustomerName.Text = "";
  129. txtOrderSum.Text = "";
  130. txtOutSourcingSum.Text = "";
  131. rbPayType.Checked = false;
  132. rbPayType1.Checked = false;
  133. rbPayType2.Checked = false;
  134. rbPayType3.Checked = false;
  135. txtProjectName.Text = "";
  136. txtProjectNumber.Text = "";
  137. rbOrderStatusClose.Checked = false;
  138. rbOrderStatusOpen.Checked = false;
  139. }
  140. private void CleanFormDetail() //清除畫面
  141. {
  142. //設定畫面物件狀態
  143. try
  144. {
  145. if (dgvOrderContent.CurrentCell != null)
  146. {
  147. if (!dgvOrderContent.CurrentCell.IsInEditMode)
  148. {
  149. dgvOrderContent.Rows.Clear();
  150. }
  151. }
  152. dgvOutSourcingContent.Rows.Clear();
  153. }
  154. catch (Exception ex)
  155. {
  156. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  157. }
  158. }
  159. private void CleanFormSubDetail() //清除畫面
  160. {
  161. try
  162. {
  163. //設定畫面物件狀態
  164. if (dgvReceiptPlan.Rows.Count > 0) dgvReceiptPlan.Rows.Clear();
  165. if (dgvToPayPlan.Rows.Count > 0) dgvToPayPlan.Rows.Clear();
  166. }
  167. catch (Exception ex)
  168. {
  169. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  170. }
  171. }
  172. private void CleanToolbar() //清除工具列
  173. {
  174. //設定Toolbar狀態
  175. tsbSearch.Visible = true;
  176. tsbSearch.Enabled = true;
  177. tsbAdd.Visible = false; //本程式不存在新功能
  178. tsbEdit.Visible = true;
  179. tsbEdit.Enabled = true;
  180. tsbDelete.Visible = true;
  181. tsbDelete.Enabled = true;
  182. tsbSave.Visible = false;
  183. tsbOK.Visible = false;
  184. tsbCancel.Visible = false;
  185. }
  186. private void UnLockFormHead() //解除限制唯讀物件
  187. {
  188. txtSalesOrderNo.ReadOnly = false;
  189. txtCustomerName.ReadOnly = false;
  190. rbPayType.Enabled = true;
  191. rbPayType1.Enabled = true;
  192. rbPayType2.Enabled = true;
  193. rbPayType3.Enabled = true;
  194. rbOrderStatusClose.Enabled = true;
  195. rbOrderStatusOpen.Enabled = true;
  196. txtProjectName.ReadOnly = false;
  197. btnGetSalesOrderNo.Enabled = true;
  198. btnGetCustomer.Enabled = true;
  199. }
  200. private void UnLockFormDetail() //解除限制唯讀物件
  201. {
  202. dgvOrderContent.ReadOnly = false;
  203. dgvOutSourcingContent.ReadOnly = false;
  204. }
  205. private void UnLockFormSubDetail() //解除限制唯讀物件
  206. {
  207. try
  208. {
  209. if (dgvOrderContent.Rows.Count > 0)
  210. {
  211. btnArrangeProject.Enabled = true;
  212. dgvReceiptPlan.ReadOnly = false;
  213. }
  214. if (dgvOutSourcingContent.Rows.Count > 0)
  215. {
  216. btnArrangToPayProject.Enabled = true;
  217. dgvToPayPlan.ReadOnly = false;
  218. }
  219. }
  220. catch (Exception ex)
  221. {
  222. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  223. }
  224. }
  225. private void LockForm() //限制唯讀物件
  226. {
  227. txtSalesOrderNo.ReadOnly = true;
  228. txtCustomerName.ReadOnly = true;
  229. txtProjectName.ReadOnly = true;
  230. rbPayType.Enabled = false;
  231. rbPayType1.Enabled = false;
  232. rbPayType2.Enabled = false;
  233. rbPayType3.Enabled = false;
  234. dgvOrderContent.ReadOnly = true;
  235. dgvOutSourcingContent.ReadOnly = true;
  236. dgvReceiptPlan.ReadOnly = true;
  237. dgvToPayPlan.ReadOnly = true;
  238. rbOrderStatusOpen.Enabled = false;
  239. rbOrderStatusClose.Enabled = false;
  240. btnGetSalesOrderNo.Enabled = false;
  241. btnGetCustomer.Enabled = false;
  242. btnArrangeProject.Enabled = false;
  243. btnArrangToPayProject.Enabled = false;
  244. }
  245. private bool CheckForm() //檢查表單填寫狀況
  246. {
  247. try
  248. {
  249. //訂單表頭確認
  250. if (txtSalesOrderNo.Text.Trim() == "")
  251. {
  252. MessageBox.Show("請輸入訂單編號", "提示");
  253. txtSalesOrderNo.Focus();
  254. return false;
  255. }
  256. if (txtCustomerID.Text.Trim() == "")
  257. {
  258. MessageBox.Show("請輸入正確客戶名稱", "提示");
  259. txtCustomerID.Focus();
  260. return false;
  261. }
  262. if (rbPayType.Checked == false && rbPayType1.Checked == false && rbPayType2.Checked == false && rbPayType3.Checked == false)
  263. {
  264. MessageBox.Show("請選擇請款方式", "提示");
  265. rbPayType.Focus();
  266. return false;
  267. }
  268. //訂單內容與請款計劃確認
  269. if (dgvReceiptPlan.Rows.Count > 0 && dgvReceiptPlan.ReadOnly == false)
  270. {
  271. if (!dgvReceiptPlan.Rows[0].IsNewRow)
  272. {
  273. //物件宣告
  274. DataTable dtTemp = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  275. double dbOrderAmount = 0;
  276. double dbReceiptAmount = 0;
  277. string strOrderContent = "";
  278. string strReceiptItem = "";
  279. DataGridViewComboBoxCell cbCell = new DataGridViewComboBoxCell();
  280. foreach (DataRow drTemp in dtTemp.Rows) //取得訂單內容的金額總數
  281. {
  282. foreach (DataGridViewRow drOrderItem in dgvOrderContent.Rows)
  283. {
  284. cbCell = (DataGridViewComboBoxCell)drOrderItem.Cells["cOrderContent"];
  285. strOrderContent = (string)cbCell.Value;
  286. if (drTemp["ArgumentID"].ToString() == (string.IsNullOrEmpty(strOrderContent) ? "" : strOrderContent))
  287. {
  288. dbOrderAmount += Convert.ToDouble(drOrderItem.Cells["cAmount"].Value.ToString().Replace(",", ""));
  289. }
  290. }
  291. foreach (DataGridViewRow drReceiptItem in dgvReceiptPlan.Rows) //取得請款計劃的金額總數
  292. {
  293. cbCell = (DataGridViewComboBoxCell)drReceiptItem.Cells["cReceiptItem"];
  294. strReceiptItem = (string)cbCell.Value;
  295. if (drTemp["ArgumentID"].ToString() == (string.IsNullOrEmpty(strReceiptItem) ? "" : strReceiptItem))
  296. {
  297. dbReceiptAmount += Convert.ToDouble(drReceiptItem.Cells["cReceiptAmount"].Value.ToString().Replace(",", ""));
  298. }
  299. }
  300. if (dbOrderAmount != dbReceiptAmount)
  301. {
  302. MessageBox.Show("請款計劃" + drTemp["ArgumentValue"].ToString() + "累計金額有誤!", "提示");
  303. return false;
  304. }
  305. }
  306. }
  307. }
  308. //委外內容與付款計劃確認
  309. if (dgvToPayPlan.Rows.Count > 0 && dgvToPayPlan.ReadOnly == false)
  310. {
  311. if (!dgvToPayPlan.Rows[0].IsNewRow)
  312. {
  313. //物件宣告
  314. DataTable dtTemp = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  315. double dbOutSourceAmount = 0;
  316. double dbPayAmount = 0;
  317. string strOrderContent = "";
  318. string strPayItem = "";
  319. DataGridViewComboBoxCell cbCell = new DataGridViewComboBoxCell();
  320. foreach (DataRow drTemp in dtTemp.Rows) //取得訂單內容的金額總數
  321. {
  322. foreach (DataGridViewRow drPayItem in dgvOutSourcingContent.Rows)
  323. {
  324. cbCell = (DataGridViewComboBoxCell)drPayItem.Cells["cOutOrderContent"];
  325. strOrderContent = (string)cbCell.Value;
  326. if (drTemp["ArgumentID"].ToString() == (string.IsNullOrEmpty(strOrderContent) ? "" : strOrderContent))
  327. {
  328. dbOutSourceAmount += Convert.ToDouble(drPayItem.Cells["cOutAmount"].Value.ToString().Replace(",", ""));
  329. }
  330. }
  331. foreach (DataGridViewRow drPayItem in dgvToPayPlan.Rows) //取得請款計劃的金額總數
  332. {
  333. cbCell = (DataGridViewComboBoxCell)drPayItem.Cells["cPayItem"];
  334. strPayItem = (string)cbCell.Value;
  335. if (drTemp["ArgumentID"].ToString() == (string.IsNullOrEmpty(strPayItem) ? "" : strPayItem))
  336. {
  337. dbPayAmount += Convert.ToDouble(drPayItem.Cells["cPayAmount"].Value.ToString().Replace(",", ""));
  338. }
  339. }
  340. if (dbOutSourceAmount != dbPayAmount)
  341. {
  342. MessageBox.Show("付款計劃" + drTemp["ArgumentValue"].ToString() + "累計金額有誤!", "提示");
  343. return false;
  344. }
  345. }
  346. }
  347. }
  348. return true;
  349. }
  350. catch (Exception ex)
  351. {
  352. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  353. return false;
  354. }
  355. }
  356. private void GetOrderList() //取得專案列表
  357. {
  358. try
  359. {
  360. StringBuilder strSQL = new StringBuilder();
  361. strSQL.Append("Select * From OVW_SAL_ProjectOrderList Where ISNULL(CustomerName,'') <> '' And AccountingBookID = '" + strAccountingBookID + "' ");
  362. if (rbOrderStatusClose.Checked == true)
  363. {
  364. strSQL.Append("Union ");
  365. strSQL.Append("Select * From OVW_SAL_ProjectOrderList_Closed Where ISNULL(CustomerName,'') <> '' And AccountingBookID = '" + strAccountingBookID + "' ");
  366. }
  367. strSQL.Append("Order BY CustomerName");
  368. dgvOrderList.DataSource = UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"];
  369. }
  370. catch (Exception ex)
  371. {
  372. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  373. }
  374. }
  375. public void GoEvent() //執行事件
  376. {
  377. try
  378. {
  379. //物件宣告
  380. StringBuilder sbSQL = new StringBuilder();
  381. string strSalesOrderNo = ""; //訂單編號
  382. string strOrderItem = ""; //訂單項目
  383. string strOrderContent = ""; //採購項目
  384. string strUnitPrice = ""; //單價
  385. string strQuantity = ""; //數量
  386. string strTax = ""; //稅額
  387. string strMemo = ""; //訂單內容註解
  388. string strOutOrderItem = ""; //委外項目
  389. string strOutOrderContent = ""; //委外項目
  390. string strOutUnitPrice = ""; //委外單價
  391. string strOutQuantity = ""; //委外數量
  392. string strOutMemo = ""; //委外內容註解
  393. string strReceiptNo = ""; //請款單號
  394. string strItemNo = ""; //項次
  395. string strPhase = ""; //期數
  396. string strReceiptItem = ""; //請款項目
  397. string strReceiptUnitPrice = ""; //請款單價
  398. string strReceiptQuantity = ""; //請款數量
  399. string strRestReceipt = ""; //待請數量
  400. string strExpectedReceiptDate = ""; //預計請款日期
  401. string strActualReceiptDate = ""; //實際請款日期
  402. string strExpectedReceiptedDate = ""; //預計收款日期
  403. string strActualReceiptedDate = ""; //實際收款日期
  404. string strReceiptAmount = ""; //請款金額
  405. string strRestReceiptAmount = ""; //待請餘額
  406. string strInvoiceNo = ""; //付款單號
  407. string strPayItemNo = ""; //項次
  408. string strPayPhase = ""; //期數
  409. string strPayItem = ""; //付款項目
  410. string strPayUnitPrice = ""; //付款單價
  411. string strPayQuantity = ""; //付款數量
  412. string strRestPayQuantity = ""; //待付餘量
  413. string strExpectedPayDate = ""; //預計付款日期
  414. string strActualPayDate = ""; //實際請付日期
  415. string strPayAmount = ""; //付款止額
  416. string strRestPayAmount = ""; //待付餘額
  417. string strPayMemo = ""; //備註
  418. switch (strFrmStatus)
  419. {
  420. case "SEARCH":
  421. sbSQL.Append("Select * From OVW_SAL_ProjectOrderList ");
  422. sbSQL.Append(" Where ISNULL(CustomerName,'') <> '' ");
  423. if (txtSalesOrderNo.Text.Trim() != "")
  424. {
  425. sbSQL.Append("AND SalesOrderNo like '" + txtSalesOrderNo.Text.Trim() + "' ");
  426. }
  427. if (txtCustomerName.Text.Trim() != "")
  428. {
  429. sbSQL.Append("AND CustomerName like '" + txtCustomerName.Text.Trim() + "' ");
  430. }
  431. if (txtProjectName.Text.Trim() != "")
  432. {
  433. sbSQL.Append("AND ProjectCName like '" + txtProjectName.Text.Trim() + "' ");
  434. }
  435. if (rbOrderStatusClose.Checked == true)
  436. {
  437. sbSQL.Append("Union ");
  438. sbSQL.Append("Select * From OVW_SAL_ProjectOrderList_Closed ");
  439. sbSQL.Append(" Where ISNULL(CustomerName,'') <> '' ");
  440. if (txtSalesOrderNo.Text.Trim() != "")
  441. {
  442. sbSQL.Append("AND SalesOrderNo like '" + txtSalesOrderNo.Text.Trim() + "' ");
  443. }
  444. if (txtCustomerName.Text.Trim() != "")
  445. {
  446. sbSQL.Append("AND CustomerName like '" + txtCustomerName.Text.Trim() + "' ");
  447. }
  448. if (txtProjectName.Text.Trim() != "")
  449. {
  450. sbSQL.Append("AND ProjectCName like '" + txtProjectName.Text.Trim() + "' ");
  451. }
  452. }
  453. sbSQL.Append("Order BY CustomerName");
  454. DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"];
  455. dgvOrderList.DataSource = dtTemp;
  456. StatusChange("NONE");
  457. break;
  458. case "MODIFY":
  459. if (CheckForm() == true)
  460. {
  461. if (blIsAdded == false)
  462. {
  463. MessageBox.Show("您未完成分錄建立,請先完成分錄建立,再行儲存", "提示");
  464. return;
  465. }
  466. if (rbOrderStatusClose.Checked == true)
  467. {
  468. //處理合約結案相關事宜
  469. }
  470. //參數設定
  471. strSalesOrderNo = txtSalesOrderNo.Text.Trim(); //訂單編號
  472. string strPayType = ""; //設定請款方式
  473. if (rbPayType.Checked) strPayType = "P";
  474. if (rbPayType1.Checked) strPayType = "M";
  475. if (rbPayType2.Checked) strPayType = "D";
  476. if (rbPayType3.Checked) strPayType = "H";
  477. string strOrderStatus = ""; //設定訂單狀態
  478. if (rbOrderStatusClose.Checked) strOrderStatus = "C";
  479. if (rbOrderStatusOpen.Checked) strOrderStatus = "O";
  480. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  481. {
  482. sqlConn.Open();
  483. }
  484. string strCHKSQL = string.Format("Select * From OTB_SAL_OrderList Where SalesOrderNo = '{0}' And AccountingBookID = '{1}'", strSalesOrderNo, strAccountingBookID);
  485. sqlTran = sqlConn.BeginTransaction();
  486. if (UtilityClass.IsExist(strCHKSQL))
  487. {
  488. //資料存在,採用UPdate,基本資料不允許修改
  489. //清除訂單及委外內容
  490. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  491. {
  492. sbSQL.Clear();
  493. sbSQL.Append("Delete OTB_SAL_OrderContent Where SalesOrderNo = @SalesOrderNo ");
  494. sbSQL.Append("Delete OTB_SAL_OutSourcingContent Where SalesOrderNo = @SalesOrderNo ");
  495. sbSQL.Append("Delete OTB_SAL_ReceiptPlan Where SalesOrderNo = @SalesOrderNo ");
  496. sbSQL.Append("Delete OTB_SAL_PayPlan Where SalesOrderNo = @SalesOrderNo ");
  497. //添加參數
  498. sqlAdapter.DeleteCommand = new SqlCommand();
  499. sqlAdapter.DeleteCommand.Connection = sqlConn;
  500. sqlAdapter.DeleteCommand.Transaction = sqlTran;
  501. sqlAdapter.DeleteCommand.CommandText = sbSQL.ToString();
  502. sqlAdapter.DeleteCommand.Parameters.AddRange
  503. (
  504. new SqlParameter[]
  505. {
  506. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo)
  507. }
  508. );
  509. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  510. {
  511. sqlConn.Open();
  512. }
  513. sqlAdapter.DeleteCommand.ExecuteNonQuery();
  514. }
  515. //寫入訂單內容
  516. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  517. {
  518. sbSQL.Clear();
  519. sbSQL.Append("Insert Into OTB_SAL_OrderContent (SalesOrderNo, ItemNo, OrderContent, UnitPrice, Quantity, Tax, Memo, CreateDate, CreateUser, ModifyDate, ModifyUser) Values");
  520. sbSQL.Append("(@SalesOrderNo, @ItemNo, @OrderContent, @UnitPrice, @Quantity, @Tax, @Memo, GetDate(), @CreateUser, GetDate(), @ModifyUser)");
  521. foreach (DataGridViewRow drContent in dgvOrderContent.Rows)
  522. {
  523. strOrderContent = (string)drContent.Cells["cOrderContent"].Value;
  524. strOrderItem = (string)drContent.Cells["cOrderItem"].Value;
  525. strUnitPrice = (string)drContent.Cells["cUnitPrice"].Value;
  526. strQuantity = (string)drContent.Cells["cQuantity"].Value;
  527. strTax = (string)drContent.Cells["cTax"].Value;
  528. strMemo = (string)drContent.Cells["cOrderMemo"].Value;
  529. if (!drContent.IsNewRow)
  530. {
  531. //添加參數
  532. sqlAdapter.InsertCommand = new SqlCommand();
  533. sqlAdapter.InsertCommand.Connection = sqlConn;
  534. sqlAdapter.InsertCommand.Transaction = sqlTran;
  535. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  536. sqlAdapter.InsertCommand.Parameters.AddRange
  537. (
  538. new SqlParameter[]
  539. {
  540. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo),
  541. new SqlParameter("@ItemNo",string.IsNullOrEmpty(strOrderItem) ? "" : strOrderItem),
  542. new SqlParameter("@OrderContent",string.IsNullOrEmpty(strOrderContent) ? "" : strOrderContent),
  543. new SqlParameter("@UnitPrice",string.IsNullOrEmpty(strUnitPrice) ? "" : UtilityClass.EncryptDES(strUnitPrice,strKey)),
  544. new SqlParameter("@Quantity",string.IsNullOrEmpty(strQuantity) ? "" : UtilityClass.EncryptDES(strQuantity,strKey)),
  545. new SqlParameter("@Tax",string.IsNullOrEmpty(strTax) ? "" : UtilityClass.EncryptDES(strTax,strKey)),
  546. new SqlParameter("@Memo",string.IsNullOrEmpty(strMemo) ? "" : strMemo),
  547. new SqlParameter("@CreateUser",strActiveUserID),
  548. new SqlParameter("@ModifyUser",strActiveUserID)
  549. }
  550. );
  551. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  552. {
  553. sqlConn.Open();
  554. }
  555. sqlAdapter.InsertCommand.ExecuteNonQuery();
  556. }
  557. }
  558. }
  559. //寫入委外內容
  560. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  561. {
  562. sbSQL.Clear();
  563. sbSQL.Append("Insert Into OTB_SAL_OutSourcingContent (SalesOrderNo, ItemNo, OrderContent, UnitPrice, Quantity, Memo, CreateDate, CreateUser, ModifyDate, ModifyUser) Values");
  564. sbSQL.Append("(@SalesOrderNo, @ItemNo, @OrderContent, @UnitPrice, @Quantity, @Memo, GetDate(), @CreateUser, GetDate(), @ModifyUser)");
  565. foreach (DataGridViewRow drContent in dgvOutSourcingContent.Rows)
  566. {
  567. strOutOrderContent = (string)drContent.Cells["cOutOrderContent"].Value;
  568. strOutOrderItem = (string)drContent.Cells["cOutOrderItem"].Value;
  569. strOutUnitPrice = (string)drContent.Cells["cOutUnitPrice"].Value;
  570. strOutQuantity = (string)drContent.Cells["cOutQuantity"].Value;
  571. strOutMemo = (string)drContent.Cells["cOutOrderMemo"].Value;
  572. if (!drContent.IsNewRow)
  573. {
  574. //添加參數
  575. sqlAdapter.InsertCommand = new SqlCommand();
  576. sqlAdapter.InsertCommand.Connection = sqlConn;
  577. sqlAdapter.InsertCommand.Transaction = sqlTran;
  578. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  579. sqlAdapter.InsertCommand.Parameters.AddRange
  580. (
  581. new SqlParameter[]
  582. {
  583. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo),
  584. new SqlParameter("@ItemNo",string.IsNullOrEmpty(strOutOrderItem) ? "" : strOutOrderItem),
  585. new SqlParameter("@OrderContent",string.IsNullOrEmpty(strOutOrderContent) ? "" : strOutOrderContent),
  586. new SqlParameter("@UnitPrice",string.IsNullOrEmpty(strOutUnitPrice) ? "" : UtilityClass.EncryptDES(strOutUnitPrice,strKey)),
  587. new SqlParameter("@Quantity",string.IsNullOrEmpty(strOutQuantity) ? "" : UtilityClass.EncryptDES(strOutQuantity,strKey)),
  588. new SqlParameter("@Memo",string.IsNullOrEmpty(strOutMemo) ? "" : strOutMemo),
  589. new SqlParameter("@CreateUser",strActiveUserID),
  590. new SqlParameter("@ModifyUser",strActiveUserID)
  591. }
  592. );
  593. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  594. {
  595. sqlConn.Open();
  596. }
  597. sqlAdapter.InsertCommand.ExecuteNonQuery();
  598. }
  599. }
  600. }
  601. //寫入請款計劃
  602. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  603. {
  604. foreach (DataGridViewRow drContent in dgvReceiptPlan.Rows)
  605. {
  606. if (!drContent.IsNewRow)
  607. {
  608. strReceiptNo = (string)drContent.Cells["cReceiptNo"].Value; //請款單號
  609. strItemNo = (string)drContent.Cells["cItemNo"].Value; //項次
  610. strPhase = drContent.Cells["cPhase"].Value.ToString(); //期數
  611. strReceiptItem = (string)drContent.Cells["cReceiptItem"].Value; //請款項目
  612. strReceiptUnitPrice = (string)drContent.Cells["cReceiptUnitPrice"].Value; //請款單價
  613. strReceiptQuantity = (string)drContent.Cells["cReceiptQuantity"].Value; //請款數量
  614. strRestReceipt = (string)drContent.Cells["cRestReceipt"].Value; //待請數量
  615. strExpectedReceiptDate = (string)drContent.Cells["cExpectedReceiptDate"].Value; //預計請款日期
  616. strActualReceiptDate = (string)drContent.Cells["cActualReceiptDate"].Value; //實際請款日期
  617. strExpectedReceiptedDate = (string)drContent.Cells["cExpectedReceiptedDate"].Value; //預計收款日期
  618. strActualReceiptedDate = (string)drContent.Cells["cActualReceiptedDate"].Value; //實際收款日期
  619. strReceiptAmount = (string)drContent.Cells["cReceiptAmount"].Value;
  620. strTax = (string)drContent.Cells["cReceiptTax"].Value; //稅額
  621. strRestReceiptAmount = (string)drContent.Cells["cRestReceiptAmount"].Value; //待請餘額
  622. strMemo = (string)drContent.Cells["cMemo"].Value; //備註
  623. sbSQL.Clear();
  624. sbSQL.Append("Insert Into OTB_SAL_ReceiptPlan (AccountingBookID, SalesOrderNo, ReceiptNo, ItemNo, Phase, ReceiptItem, ReceiptUnitPrice, ReceiptQuantity, RestReceipt, ExpectedReceiptDate, ActualReceiptDate, ExpectedReceiptedDate, ActualReceiptedDate, ReceiptAmount, Tax, RestReceiptAmount, Memo , CreateDate, CreateUser, ModifyDate, ModifyUser) Values(");
  625. sbSQL.Append("'" + (string.IsNullOrEmpty(strAccountingBookID) ? "" : strAccountingBookID) + "', ");
  626. sbSQL.Append("'" + (string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo) + "', ");
  627. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptNo) ? "" : strReceiptNo) + "', ");
  628. sbSQL.Append("'" + (string.IsNullOrEmpty(strItemNo) ? "" : strItemNo) + "', ");
  629. sbSQL.Append((string.IsNullOrEmpty(strPhase) ? "" : strPhase) + ", ");
  630. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptItem) ? "" : strReceiptItem) + "', ");
  631. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptUnitPrice) ? "" : UtilityClass.EncryptDES(strReceiptUnitPrice, strKey)) + "', ");
  632. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptQuantity) ? "" : UtilityClass.EncryptDES(strReceiptQuantity, strKey)) + "', ");
  633. sbSQL.Append("'" + (string.IsNullOrEmpty(strRestReceipt) ? "" : UtilityClass.EncryptDES(strRestReceipt, strKey)) + "', ");
  634. sbSQL.Append(!UtilityClass.IsDate(strExpectedReceiptDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strExpectedReceiptDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  635. sbSQL.Append(!UtilityClass.IsDate(strActualReceiptDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strActualReceiptDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  636. sbSQL.Append(!UtilityClass.IsDate(strExpectedReceiptedDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strExpectedReceiptedDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  637. sbSQL.Append(!UtilityClass.IsDate(strActualReceiptedDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strActualReceiptedDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  638. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptAmount) ? "" : UtilityClass.EncryptDES(strReceiptAmount, strKey)) + "', ");
  639. sbSQL.Append("'" + (string.IsNullOrEmpty(strTax) ? "" : UtilityClass.EncryptDES(strTax, strKey)) + "', ");
  640. sbSQL.Append("'" + (string.IsNullOrEmpty(strRestReceiptAmount) ? "" : UtilityClass.EncryptDES(strRestReceiptAmount, strKey)) + "', ");
  641. sbSQL.Append("'" + (string.IsNullOrEmpty(strMemo) ? "" : strMemo) + "', ");
  642. sbSQL.Append("GetDate(), '" + strActiveUserID + "', GetDate(), '" + strActiveUserID + "')");
  643. sqlAdapter.InsertCommand = new SqlCommand();
  644. sqlAdapter.InsertCommand.Connection = sqlConn;
  645. sqlAdapter.InsertCommand.Transaction = sqlTran;
  646. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  647. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  648. {
  649. sqlConn.Open();
  650. }
  651. sqlAdapter.InsertCommand.ExecuteNonQuery();
  652. }
  653. }
  654. }
  655. //寫入付款計劃
  656. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  657. {
  658. foreach (DataGridViewRow drContent in dgvToPayPlan.Rows)
  659. {
  660. if (!drContent.IsNewRow)
  661. {
  662. strInvoiceNo = (string)drContent.Cells["cInvoiceNo"].Value; //付款單號
  663. strPayItemNo = (string)drContent.Cells["cPayItemNo"].Value; //項次
  664. strPayPhase = drContent.Cells["cPayPhase"].Value.ToString(); //期數
  665. strPayItem = (string)drContent.Cells["cPayItem"].Value; //付款項目
  666. strPayUnitPrice = (string)drContent.Cells["cPayUnitPrice"].Value; //付款單價
  667. strPayQuantity = (string)drContent.Cells["cPayQuantity"].Value; //付款數量
  668. strRestPayQuantity = (string)drContent.Cells["cRestPayQuantity"].Value; //待付數量
  669. strExpectedPayDate = (string)drContent.Cells["cExpectedPayDate"].Value; //預計付款日期
  670. strActualPayDate = (string)drContent.Cells["cActualPaiedDate"].Value; //實際付款日期
  671. strPayAmount = (string)drContent.Cells["cPayAmount"].Value; //付款金額
  672. strRestPayAmount = (string)drContent.Cells["cRestPayAmount"].Value; //待付餘額
  673. strPayMemo = (string)drContent.Cells["cPayMemo"].Value; //備註
  674. sbSQL.Clear();
  675. sbSQL.Append("Insert Into OTB_SAL_PayPlan (AccountingBookID, SalesOrderNo, ItemNo, PhaseNo, InvoiceNo, PayItem, PayUnitPrice, PayQuantity, RestPay, ExpectedPayDate, ActualPayDate, PayAmount,RestPayAmount ,Memo ,CreateDate, CreateUser, ModifyDate, ModifyUser) Values(");
  676. sbSQL.Append("'" + (string.IsNullOrEmpty(strAccountingBookID) ? "" : strAccountingBookID) + "', ");
  677. sbSQL.Append("'" + (string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo) + "', ");
  678. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayItemNo) ? "" : strPayItemNo) + "', ");
  679. sbSQL.Append((string.IsNullOrEmpty(strPayPhase) ? "" : strPayPhase) + ", ");
  680. sbSQL.Append("'" + (string.IsNullOrEmpty(strInvoiceNo) ? "" : strInvoiceNo) + "', ");
  681. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayItem) ? "" : strPayItem) + "', ");
  682. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayUnitPrice) ? "" : UtilityClass.EncryptDES(strPayUnitPrice, strKey)) + "', ");
  683. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayQuantity) ? "" : UtilityClass.EncryptDES(strPayQuantity, strKey)) + "', ");
  684. sbSQL.Append("'" + (string.IsNullOrEmpty(strRestPayQuantity) ? "" : UtilityClass.EncryptDES(strRestPayQuantity, strKey)) + "', ");
  685. sbSQL.Append(!UtilityClass.IsDate(strExpectedPayDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strExpectedPayDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  686. sbSQL.Append(!UtilityClass.IsDate(strActualPayDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strActualPayDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  687. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayAmount) ? "" : UtilityClass.EncryptDES(strPayAmount, strKey)) + "', ");
  688. sbSQL.Append("'" + (string.IsNullOrEmpty(strRestPayAmount) ? "" : UtilityClass.EncryptDES(strRestPayAmount, strKey)) + "', ");
  689. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayMemo) ? "" : strPayMemo) + "', ");
  690. sbSQL.Append("GetDate(), '" + strActiveUserID + "', GetDate(), '" + strActiveUserID + "')");
  691. sqlAdapter.InsertCommand = new SqlCommand();
  692. sqlAdapter.InsertCommand.Connection = sqlConn;
  693. sqlAdapter.InsertCommand.Transaction = sqlTran;
  694. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  695. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  696. {
  697. sqlConn.Open();
  698. }
  699. sqlAdapter.InsertCommand.ExecuteNonQuery();
  700. }
  701. }
  702. }
  703. sqlTran.Commit(); //資料完成寫入資料庫中
  704. dgvOrderList.ReadOnly = true;
  705. StatusChange("SEARCH");
  706. GoEvent();
  707. //定位在已存檔的位置
  708. string strColumnValue = "";
  709. foreach (DataGridViewRow dr in dgvOrderList.Rows)
  710. {
  711. strColumnValue = (string)dr.Cells["cOrderID"].Value;
  712. if (!string.IsNullOrEmpty(strColumnValue) && strColumnValue == strSalesOrderNo)
  713. {
  714. dgvOrderList.CurrentCell = dgvOrderList.Rows[dr.Index].Cells["cCustomerName"];
  715. }
  716. }
  717. MessageBox.Show("儲存成功", "提示");
  718. }
  719. else
  720. {
  721. # region 基本資料不存在,採用Insert
  722. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  723. {
  724. sbSQL.Clear();
  725. sbSQL.Append("Insert Into OTB_SAL_OrderList (AccountingBookID, SalesOrderNo, CustomerID, ProjectNumber, PayType, OrderStatus, CreateDate, CreateUser, ModifyDate, ModifyUser) Values");
  726. sbSQL.Append("(@AccountingBookID, @SalesOrderNo, @CustomerID, @ProjectNumber, @PayType, @OrderSatus, GetDate(), @CreateUser, GetDate(), @ModifyUser)");
  727. //添加參數
  728. sqlAdapter.InsertCommand = new SqlCommand();
  729. sqlAdapter.InsertCommand.Connection = sqlConn;
  730. sqlAdapter.InsertCommand.Transaction = sqlTran;
  731. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  732. sqlAdapter.InsertCommand.Parameters.AddRange
  733. (
  734. new SqlParameter[]
  735. {
  736. new SqlParameter("@AccountingBookID",strAccountingBookID),
  737. new SqlParameter("@SalesOrderNo",txtSalesOrderNo.Text.Trim()),
  738. new SqlParameter("@CustomerID",txtCustomerID.Text.Trim()),
  739. new SqlParameter("@ProjectNumber",txtProjectNumber.Text.Trim()),
  740. new SqlParameter("@PayType",strPayType),
  741. new SqlParameter("@OrderSatus",strOrderStatus),
  742. new SqlParameter("@CreateUser",strActiveUserID),
  743. new SqlParameter("@ModifyUser",strActiveUserID)
  744. }
  745. );
  746. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  747. {
  748. sqlConn.Open();
  749. }
  750. sqlAdapter.InsertCommand.ExecuteNonQuery();
  751. }
  752. //清除訂單與委外內容
  753. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  754. {
  755. sbSQL.Clear();
  756. sbSQL.Append("Delete OTB_SAL_OrderContent Where SalesOrderNo = @SalesOrderNo ");
  757. sbSQL.Append("Delete OTB_SAL_OutSourcingContent Where SalesOrderNo = @SalesOrderNo ");
  758. sbSQL.Append("Delete OTB_SAL_ReceiptPlan Where SalesOrderNo = @SalesOrderNo ");
  759. sbSQL.Append("Delete OTB_SAL_PayPlan Where SalesOrderNo = @SalesOrderNo ");
  760. //添加參數
  761. sqlAdapter.DeleteCommand = new SqlCommand();
  762. sqlAdapter.DeleteCommand.Connection = sqlConn;
  763. sqlAdapter.DeleteCommand.Transaction = sqlTran;
  764. sqlAdapter.DeleteCommand.CommandText = sbSQL.ToString();
  765. sqlAdapter.DeleteCommand.Parameters.AddRange
  766. (
  767. new SqlParameter[]
  768. {
  769. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo)
  770. }
  771. );
  772. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  773. {
  774. sqlConn.Open();
  775. }
  776. sqlAdapter.DeleteCommand.ExecuteNonQuery();
  777. }
  778. //寫入訂單內容
  779. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  780. {
  781. sbSQL.Clear();
  782. sbSQL.Append("Insert Into OTB_SAL_OrderContent (SalesOrderNo, ItemNo, OrderContent, UnitPrice, Quantity, Tax, Memo, CreateDate, CreateUser, ModifyDate, ModifyUser) Values");
  783. sbSQL.Append("(@SalesOrderNo, @ItemNo, @OrderContent, @UnitPrice, @Quantity, @Tax, @Memo, GetDate(), @CreateUser, GetDate(), @ModifyUser)");
  784. foreach (DataGridViewRow drContent in dgvOrderContent.Rows)
  785. {
  786. //檢查參數
  787. strOrderContent = (string)drContent.Cells["cOrderContent"].Value;
  788. strOrderItem = (string)drContent.Cells["cOrderItem"].Value;
  789. strUnitPrice = (string)drContent.Cells["cUnitPrice"].Value;
  790. strQuantity = (string)drContent.Cells["cQuantity"].Value;
  791. strTax = (string)drContent.Cells["cTax"].Value;
  792. strMemo = (string)drContent.Cells["cOrderMemo"].Value;
  793. if (!drContent.IsNewRow)
  794. {
  795. ////添加參數
  796. sqlAdapter.InsertCommand = new SqlCommand();
  797. sqlAdapter.InsertCommand.Connection = sqlConn;
  798. sqlAdapter.InsertCommand.Transaction = sqlTran;
  799. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  800. sqlAdapter.InsertCommand.Parameters.AddRange
  801. (
  802. new SqlParameter[]
  803. {
  804. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo),
  805. new SqlParameter("@ItemNo",string.IsNullOrEmpty(strOrderItem) ? "" : strOrderItem),
  806. new SqlParameter("@OrderContent",string.IsNullOrEmpty(strOrderContent) ? "" : strOrderContent),
  807. new SqlParameter("@UnitPrice",string.IsNullOrEmpty(strUnitPrice) ? "" : UtilityClass.EncryptDES(strUnitPrice,strKey)),
  808. new SqlParameter("@Quantity",string.IsNullOrEmpty(strQuantity) ? "" : UtilityClass.EncryptDES(strQuantity,strKey)),
  809. new SqlParameter("@Tax",string.IsNullOrEmpty(strTax) ? "" : UtilityClass.EncryptDES(strTax,strKey)),
  810. new SqlParameter("@Memo",string.IsNullOrEmpty(strMemo) ? "" : strMemo),
  811. new SqlParameter("@CreateUser",strActiveUserID),
  812. new SqlParameter("@ModifyUser",strActiveUserID)
  813. }
  814. );
  815. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  816. {
  817. sqlConn.Open();
  818. }
  819. sqlAdapter.InsertCommand.ExecuteNonQuery();
  820. }
  821. }
  822. }
  823. //寫入委外內容
  824. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  825. {
  826. sbSQL.Clear();
  827. sbSQL.Append("Insert Into OTB_SAL_OutSourcingContent (SalesOrderNo, ItemNo, OrderContent, UnitPrice, Quantity, Memo, CreateDate, CreateUser, ModifyDate, ModifyUser) Values");
  828. sbSQL.Append("(@SalesOrderNo, @ItemNo, @OrderContent, @UnitPrice, @Quantity, @Memo, GetDate(), @CreateUser, GetDate(), @ModifyUser)");
  829. foreach (DataGridViewRow drContent in dgvOutSourcingContent.Rows)
  830. {
  831. //檢查參數
  832. strOutOrderContent = (string)drContent.Cells["cOutOrderContent"].Value;
  833. strOutOrderItem = (string)drContent.Cells["cOutOrderItem"].Value;
  834. strOutUnitPrice = (string)drContent.Cells["cOutUnitPrice"].Value;
  835. strOutQuantity = (string)drContent.Cells["cOutQuantity"].Value;
  836. strOutMemo = (string)drContent.Cells["cOutOrderMemo"].Value;
  837. if (!drContent.IsNewRow)
  838. {
  839. ////添加參數
  840. sqlAdapter.InsertCommand = new SqlCommand();
  841. sqlAdapter.InsertCommand.Connection = sqlConn;
  842. sqlAdapter.InsertCommand.Transaction = sqlTran;
  843. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  844. sqlAdapter.InsertCommand.Parameters.AddRange
  845. (
  846. new SqlParameter[]
  847. {
  848. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo),
  849. new SqlParameter("@ItemNo",string.IsNullOrEmpty(strOutOrderItem) ? "" : strOutOrderItem),
  850. new SqlParameter("@OrderContent",string.IsNullOrEmpty(strOutOrderContent) ? "" : strOutOrderContent),
  851. new SqlParameter("@UnitPrice",string.IsNullOrEmpty(strOutUnitPrice) ? "" : UtilityClass.EncryptDES(strOutUnitPrice,strKey)),
  852. new SqlParameter("@Quantity",string.IsNullOrEmpty(strOutQuantity) ? "" : UtilityClass.EncryptDES(strOutQuantity,strKey)),
  853. new SqlParameter("@Memo",string.IsNullOrEmpty(strOutMemo) ? "" : strOutMemo),
  854. new SqlParameter("@CreateUser",strActiveUserID),
  855. new SqlParameter("@ModifyUser",strActiveUserID)
  856. }
  857. );
  858. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  859. {
  860. sqlConn.Open();
  861. }
  862. sqlAdapter.InsertCommand.ExecuteNonQuery();
  863. }
  864. }
  865. }
  866. //寫入請款計劃
  867. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  868. {
  869. foreach (DataGridViewRow drContent in dgvReceiptPlan.Rows)
  870. {
  871. if (!drContent.IsNewRow)
  872. {
  873. strReceiptNo = (string)drContent.Cells["cReceiptNo"].Value; //請款單號
  874. strItemNo = (string)drContent.Cells["cItemNo"].Value; //項次
  875. strPhase = drContent.Cells["cPhase"].Value.ToString(); //期數
  876. strReceiptItem = (string)drContent.Cells["cReceiptItem"].Value; //請款項目
  877. strReceiptUnitPrice = (string)drContent.Cells["cReceiptUnitPrice"].Value; //請款單價
  878. strReceiptQuantity = (string)drContent.Cells["cReceiptQuantity"].Value; //請款數量
  879. strRestReceipt = (string)drContent.Cells["cRestReceipt"].Value; //待請數量
  880. strExpectedReceiptDate = (string)drContent.Cells["cExpectedReceiptDate"].Value; //預計請款日期
  881. strActualReceiptDate = (string)drContent.Cells["cActualReceiptDate"].Value; //實際請款日期
  882. strExpectedReceiptedDate = (string)drContent.Cells["cExpectedReceiptedDate"].Value; //預計收款日期
  883. strActualReceiptedDate = (string)drContent.Cells["cActualReceiptedDate"].Value; //實際收款日期
  884. strReceiptAmount = (string)drContent.Cells["cReceiptAmount"].Value; //請款金額
  885. strTax = (string)drContent.Cells["cReceiptTax"].Value; //請款金額
  886. strRestReceiptAmount = (string)drContent.Cells["cRestReceiptAmount"].Value; //待請餘額
  887. strMemo = (string)drContent.Cells["cMemo"].Value; //備註
  888. sbSQL.Clear();
  889. sbSQL.Append("Insert Into OTB_SAL_ReceiptPlan (AccountingBookID, SalesOrderNo, ReceiptNo, ItemNo, Phase, ReceiptItem, ReceiptUnitPrice, ReceiptQuantity, RestReceipt, ExpectedReceiptDate, ActualReceiptDate, ExpectedReceiptedDate, ActualReceiptedDate, ReceiptAmount, Tax, RestReceiptAmount, Memo , CreateDate, CreateUser, ModifyDate, ModifyUser) Values(");
  890. sbSQL.Append("'" + (string.IsNullOrEmpty(strAccountingBookID) ? "" : strAccountingBookID) + "', ");
  891. sbSQL.Append("'" + (string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo) + "', ");
  892. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptNo) ? "" : strReceiptNo) + "', ");
  893. sbSQL.Append("'" + (string.IsNullOrEmpty(strItemNo) ? "" : strItemNo) + "', ");
  894. sbSQL.Append((string.IsNullOrEmpty(strPhase) ? "" : strPhase) + ", ");
  895. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptItem) ? "" : strReceiptItem) + "', ");
  896. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptUnitPrice) ? "" : UtilityClass.EncryptDES(strReceiptUnitPrice, strKey)) + "', ");
  897. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptQuantity) ? "" : UtilityClass.EncryptDES(strReceiptQuantity, strKey)) + "', ");
  898. sbSQL.Append("'" + (string.IsNullOrEmpty(strRestReceipt) ? "" : UtilityClass.EncryptDES(strRestReceipt, strKey)) + "', ");
  899. sbSQL.Append(!UtilityClass.IsDate(strExpectedReceiptDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strExpectedReceiptDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  900. sbSQL.Append(!UtilityClass.IsDate(strActualReceiptDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strActualReceiptDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  901. sbSQL.Append(!UtilityClass.IsDate(strExpectedReceiptedDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strExpectedReceiptedDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  902. sbSQL.Append(!UtilityClass.IsDate(strActualReceiptedDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strActualReceiptedDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  903. sbSQL.Append("'" + (string.IsNullOrEmpty(strReceiptAmount) ? "" : UtilityClass.EncryptDES(strReceiptAmount, strKey)) + "', ");
  904. sbSQL.Append("'" + (string.IsNullOrEmpty(strTax) ? "" : UtilityClass.EncryptDES(strTax, strKey)) + "', ");
  905. sbSQL.Append("'" + (string.IsNullOrEmpty(strRestReceiptAmount) ? "" : UtilityClass.EncryptDES(strRestReceiptAmount, strKey)) + "', ");
  906. sbSQL.Append("'" + (string.IsNullOrEmpty(strMemo) ? "" : strMemo) + "', ");
  907. sbSQL.Append("GetDate(), '" + strActiveUserID + "', GetDate(), '" + strActiveUserID + "')");
  908. sqlAdapter.InsertCommand = new SqlCommand();
  909. sqlAdapter.InsertCommand.Connection = sqlConn;
  910. sqlAdapter.InsertCommand.Transaction = sqlTran;
  911. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  912. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  913. {
  914. sqlConn.Open();
  915. }
  916. sqlAdapter.InsertCommand.ExecuteNonQuery();
  917. }
  918. }
  919. }
  920. //寫入付款計劃
  921. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  922. {
  923. foreach (DataGridViewRow drContent in dgvToPayPlan.Rows)
  924. {
  925. if (!drContent.IsNewRow)
  926. {
  927. strInvoiceNo = (string)drContent.Cells["cInvoiceNo"].Value; //付款單號
  928. strPayItemNo = (string)drContent.Cells["cPayItemNo"].Value; //項次
  929. strPayPhase = drContent.Cells["cPayPhase"].Value.ToString(); //期數
  930. strPayItem = (string)drContent.Cells["cPayItem"].Value; //付款項目
  931. strPayUnitPrice = (string)drContent.Cells["cPayUnitPrice"].Value; //付款單價
  932. strPayQuantity = (string)drContent.Cells["cPayQuantity"].Value; //付款數量
  933. strRestPayQuantity = (string)drContent.Cells["cRestPayQuantity"].Value; //待付數量
  934. strExpectedPayDate = (string)drContent.Cells["cExpectedPayDate"].Value; //預計付款日期
  935. strActualPayDate = (string)drContent.Cells["cActualPaiedDate"].Value; //實際請付日期
  936. strPayAmount = (string)drContent.Cells["cPayAmount"].Value; //付款金額
  937. strRestPayAmount = (string)drContent.Cells["cRestPayAmount"].Value; //待付餘額
  938. strPayMemo = (string)drContent.Cells["cPayMemo"].Value; //備註
  939. sbSQL.Clear();
  940. sbSQL.Append("Insert Into OTB_SAL_PayPlan (AccountingBookID, SalesOrderNo, ItemNo, PhaseNo, InvoiceNo, PayItem, PayUnitPrice, PayQuantity, RestPay, ExpectedPayDate, ActualPayDate, PayAmount,RestPayAmount ,Memo ,CreateDate, CreateUser, ModifyDate, ModifyUser) Values(");
  941. sbSQL.Append("'" + (string.IsNullOrEmpty(strAccountingBookID) ? "" : strAccountingBookID) + "', ");
  942. sbSQL.Append("'" + (string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo) + "', ");
  943. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayItemNo) ? "" : strPayItemNo) + "', ");
  944. sbSQL.Append((string.IsNullOrEmpty(strPayPhase) ? "" : strPayPhase) + ", ");
  945. sbSQL.Append("'" + (string.IsNullOrEmpty(strInvoiceNo) ? "" : strInvoiceNo) + "', ");
  946. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayItem) ? "" : strPayItem) + "', ");
  947. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayUnitPrice) ? "" : UtilityClass.EncryptDES(strPayUnitPrice, strKey)) + "', ");
  948. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayQuantity) ? "" : UtilityClass.EncryptDES(strPayQuantity, strKey)) + "', ");
  949. sbSQL.Append("'" + (string.IsNullOrEmpty(strRestPayQuantity) ? "" : UtilityClass.EncryptDES(strRestPayQuantity, strKey)) + "', ");
  950. sbSQL.Append(!UtilityClass.IsDate(strExpectedPayDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strExpectedPayDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  951. sbSQL.Append(!UtilityClass.IsDate(strActualPayDate) ? "null," : ("Convert(DateTime,'" + Convert.ToDateTime(strActualPayDate + " 00:00:00").ToString("yyyy/MM/dd HH:mm:ss") + "'), "));
  952. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayAmount) ? "" : UtilityClass.EncryptDES(strPayAmount, strKey)) + "', ");
  953. sbSQL.Append("'" + (string.IsNullOrEmpty(strRestPayAmount) ? "" : UtilityClass.EncryptDES(strRestPayAmount, strKey)) + "', ");
  954. sbSQL.Append("'" + (string.IsNullOrEmpty(strPayMemo) ? "" : strPayMemo) + "', ");
  955. sbSQL.Append("GetDate(), '" + strActiveUserID + "', GetDate(), '" + strActiveUserID + "')");
  956. sqlAdapter.InsertCommand = new SqlCommand();
  957. sqlAdapter.InsertCommand.Connection = sqlConn;
  958. sqlAdapter.InsertCommand.Transaction = sqlTran;
  959. sqlAdapter.InsertCommand.CommandText = sbSQL.ToString();
  960. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  961. {
  962. sqlConn.Open();
  963. }
  964. sqlAdapter.InsertCommand.ExecuteNonQuery();
  965. }
  966. }
  967. }
  968. sqlTran.Commit(); //資料完成寫入資料庫中
  969. dgvOrderList.ReadOnly = true;
  970. MessageBox.Show("儲存成功","提示");
  971. StatusChange("SEARCH");
  972. GoEvent();
  973. #endregion
  974. }
  975. }
  976. break;
  977. }
  978. }
  979. catch (Exception ex)
  980. {
  981. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  982. sqlTran.Rollback();
  983. }
  984. }
  985. private void ReArrangeReceipt(DataGridViewCellEventArgs e) //重新整理請款計劃
  986. {
  987. try
  988. {
  989. //物件宣告
  990. string strItemNo = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells[2].Value; //項次
  991. string strRestReceipt = ""; //請款數量
  992. string strReceiptClass = ""; //請款計算方式 (P:百分比、D:三位一撇)
  993. double dbSumRestReceipt = 0; //請款總量
  994. string strRestReceiptAmount = ""; //待請餘額
  995. double dbSumRestReceiptAmount = 0; //待請總餘額
  996. string strReceiptUnitPrice = ""; //請款單價
  997. string strReceiptQuantity = ""; //請款數量
  998. double dbReceiptQuantity = 0; //請款數量
  999. double dbTax = 0; //稅額
  1000. double dbReceiptAmount = 0; //請款金額
  1001. if (rbPayType.Checked == true)
  1002. {
  1003. foreach (DataGridViewRow drData in dgvOrderContent.Rows)
  1004. {
  1005. if (!drData.IsNewRow)
  1006. {
  1007. strRestReceipt = (string)drData.Cells["cQuantity"].Value;
  1008. if (!string.IsNullOrEmpty(strRestReceipt)) //取得待請數量
  1009. {
  1010. if (strRestReceipt.IndexOf("%") != -1)
  1011. {
  1012. dbSumRestReceipt += Convert.ToDouble(string.IsNullOrEmpty(strRestReceipt) ? "0" : strRestReceipt.Replace("%", ""));
  1013. strReceiptClass = "P";
  1014. }
  1015. else
  1016. {
  1017. dbSumRestReceipt += Convert.ToDouble(string.IsNullOrEmpty(strRestReceipt) ? "0" : strRestReceipt.Replace(",", ""));
  1018. strReceiptClass = "D";
  1019. }
  1020. strRestReceiptAmount = (string)drData.Cells["cAmount"].Value;
  1021. dbSumRestReceiptAmount += Convert.ToDouble(string.IsNullOrEmpty(strRestReceiptAmount) ? "0" : strRestReceiptAmount.Replace(",", ""));//取得待請餘額
  1022. }
  1023. }
  1024. }
  1025. }
  1026. else
  1027. {
  1028. foreach (DataGridViewRow drData in dgvOrderContent.Rows)
  1029. {
  1030. if ((string)drData.Cells["cOrderItem"].Value == strItemNo)
  1031. {
  1032. strRestReceipt = (string)drData.Cells["cQuantity"].Value; //取得待請數量
  1033. dbSumRestReceipt = string.IsNullOrEmpty(strRestReceipt) ? 0 : Convert.ToDouble(strRestReceipt.Replace(",", ""));
  1034. strRestReceiptAmount = (string)drData.Cells["cAmount"].Value; //取得待請餘額
  1035. dbSumRestReceiptAmount = Convert.ToDouble(string.IsNullOrEmpty(strRestReceiptAmount) ? "0" : strRestReceiptAmount.Replace(",", ""));
  1036. strReceiptClass = "D";
  1037. }
  1038. }
  1039. }
  1040. //變更該筆項目資料
  1041. strReceiptUnitPrice = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells[5].Value;
  1042. strReceiptUnitPrice = string.IsNullOrEmpty(strReceiptUnitPrice) ? "0" : strReceiptUnitPrice.Replace(",", ""); //請款單價
  1043. strReceiptQuantity = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells[6].Value;
  1044. if (strReceiptClass != "")
  1045. {
  1046. if (strReceiptClass == "P") //取得請款數量
  1047. {
  1048. dgvReceiptPlan.Columns["cReceiptItem"].ReadOnly = true;
  1049. dgvReceiptPlan.AllowUserToAddRows = false;
  1050. dbReceiptQuantity = string.IsNullOrEmpty(strReceiptQuantity) ? 0 : (Convert.ToDouble(strReceiptQuantity.Replace("%", "")) / 100);
  1051. dbTax = Convert.ToDouble(strReceiptUnitPrice) * dbReceiptQuantity * 0.05;
  1052. dbReceiptAmount = Convert.ToDouble(strReceiptUnitPrice) * Convert.ToDouble(dbReceiptQuantity) + dbTax;
  1053. dgvReceiptPlan.Rows[e.RowIndex].Cells[12].Value = UtilityClass.MarkNumber(dbTax); //稅額
  1054. dgvReceiptPlan.Rows[e.RowIndex].Cells[13].Value = UtilityClass.MarkNumber(dbReceiptAmount); //請款金額
  1055. for (int intCount = 0; intCount < dgvReceiptPlan.Rows.Count; intCount++)
  1056. {
  1057. //重新整理期數 (專案分期模式,不可修改請款計劃)
  1058. if (!dgvReceiptPlan.Rows[intCount].IsNewRow)
  1059. {
  1060. dgvReceiptPlan.Rows[intCount].Cells[3].Value = intCount + 1;
  1061. }
  1062. }
  1063. }
  1064. else
  1065. {
  1066. dbReceiptQuantity = Convert.ToDouble(dgvReceiptPlan.Rows[e.RowIndex].Cells[6].Value);
  1067. dbTax = Convert.ToDouble(strReceiptUnitPrice) * dbReceiptQuantity * 0.05;
  1068. dbReceiptAmount = Convert.ToDouble(strReceiptUnitPrice) * Convert.ToDouble(dbReceiptQuantity) + dbTax;
  1069. dgvReceiptPlan.Rows[e.RowIndex].Cells[12].Value = UtilityClass.MarkNumber(dbTax); //稅額
  1070. dgvReceiptPlan.Rows[e.RowIndex].Cells[13].Value = UtilityClass.MarkNumber(dbReceiptAmount); //請款金額
  1071. dbSumRestReceiptAmount = string.IsNullOrEmpty(strRestReceiptAmount) ? 0 : Convert.ToDouble(strRestReceiptAmount.Replace(",", "")); //可請款總額
  1072. for (int intCount = 0; intCount < dgvReceiptPlan.Rows.Count; intCount++)
  1073. {
  1074. //重新整理期數
  1075. if (!dgvReceiptPlan.Rows[intCount].IsNewRow)
  1076. {
  1077. dgvReceiptPlan.Rows[intCount].Cells[3].Value = intCount + 1;
  1078. }
  1079. //重新計算相關資訊
  1080. if (strItemNo == (string)dgvReceiptPlan.Rows[intCount].Cells[2].Value)
  1081. {
  1082. if (strReceiptClass != "")
  1083. {
  1084. strReceiptQuantity = (string)dgvReceiptPlan.Rows[intCount].Cells[6].Value;
  1085. dbSumRestReceipt = dbSumRestReceipt - (string.IsNullOrEmpty(strReceiptQuantity) ? 0 : Convert.ToDouble(strReceiptQuantity));
  1086. dgvReceiptPlan.Rows[intCount].Cells[7].Value = UtilityClass.MarkNumber(dbSumRestReceipt, 2);
  1087. dbSumRestReceiptAmount = dbSumRestReceiptAmount - Convert.ToDouble(dgvReceiptPlan.Rows[intCount].Cells[13].Value.ToString().Replace(",", ""));
  1088. dgvReceiptPlan.Rows[intCount].Cells[14].Value = UtilityClass.MarkNumber(dbSumRestReceiptAmount);
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. }
  1095. catch (Exception ex)
  1096. {
  1097. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1098. }
  1099. }
  1100. private void ReArrangePay(DataGridViewCellEventArgs e) //重新整理付款計劃
  1101. {
  1102. try
  1103. {
  1104. //物件宣告
  1105. string strItemNo = (string)dgvToPayPlan.Rows[e.RowIndex].Cells[2].Value; //項次
  1106. string strRestPay = ""; //付款數量
  1107. string strPayClass = ""; //付款計算方式 (P:百分比、D:三位一撇)
  1108. double dbSumRestPay = 0; //付款總量
  1109. string strRestPayAmount = ""; //待付餘額
  1110. double dbSumRestPayAmount = 0; //待付總餘額
  1111. string strPayUnitPrice = ""; //付款單價
  1112. string strPayQuantity = ""; //付款數量
  1113. double dbPayQuantity = 0; //付款數量
  1114. double dbPayAmount = 0; //付款金額
  1115. if (rbPayType.Checked == true)
  1116. {
  1117. foreach (DataGridViewRow drData in dgvOutSourcingContent.Rows)
  1118. {
  1119. if (!drData.IsNewRow)
  1120. {
  1121. strRestPay = (string)drData.Cells["cOutQuantity"].Value;
  1122. if (!string.IsNullOrEmpty(strRestPay)) //取得待付數量
  1123. {
  1124. if (strRestPay.IndexOf("%") != -1)
  1125. {
  1126. dbSumRestPay += Convert.ToDouble(string.IsNullOrEmpty(strRestPay) ? "0" : strRestPay.Replace("%", ""));
  1127. strPayClass = "P";
  1128. }
  1129. else
  1130. {
  1131. dbSumRestPay += Convert.ToDouble(string.IsNullOrEmpty(strRestPay) ? "0" : strRestPay.Replace(",", ""));
  1132. strPayClass = "D";
  1133. }
  1134. strRestPayAmount = (string)drData.Cells["cOutAmount"].Value;
  1135. dbSumRestPayAmount += Convert.ToDouble(string.IsNullOrEmpty(strRestPayAmount) ? "0" : strRestPayAmount.Replace(",", ""));//取得待付餘額
  1136. }
  1137. }
  1138. }
  1139. }
  1140. else
  1141. {
  1142. foreach (DataGridViewRow drData in dgvOutSourcingContent.Rows)
  1143. {
  1144. if ((string)drData.Cells["cOutOrderItem"].Value == strItemNo)
  1145. {
  1146. strRestPay = (string)drData.Cells["cOutQuantity"].Value; //取得待付數量
  1147. dbSumRestPay = string.IsNullOrEmpty(strRestPay) ? 0 : Convert.ToDouble(strRestPay.Replace(",", ""));
  1148. strRestPayAmount = (string)drData.Cells["cOutAmount"].Value; //取得待付餘額
  1149. dbSumRestPayAmount = Convert.ToDouble(string.IsNullOrEmpty(strRestPayAmount) ? "0" : strRestPayAmount.Replace(",", ""));
  1150. strPayClass = "D";
  1151. }
  1152. }
  1153. }
  1154. //變更該筆項目資料
  1155. strPayUnitPrice = (string)dgvToPayPlan.Rows[e.RowIndex].Cells[5].Value;
  1156. strPayUnitPrice = string.IsNullOrEmpty(strPayUnitPrice) ? "0" : strPayUnitPrice.Replace(",", ""); //付款單價
  1157. strPayQuantity = (string)dgvToPayPlan.Rows[e.RowIndex].Cells[6].Value;
  1158. if (strPayClass != "")
  1159. {
  1160. if (strPayClass == "P") //取得請款數量
  1161. {
  1162. dgvToPayPlan.Columns["cPayItem"].ReadOnly = true;
  1163. dgvToPayPlan.AllowUserToAddRows = false;
  1164. dbPayQuantity = string.IsNullOrEmpty(strPayQuantity) ? 0 : (Convert.ToDouble(strPayQuantity.Replace("%", "")) / 100);
  1165. dbPayAmount = Convert.ToDouble(strPayUnitPrice) * Convert.ToDouble(dbPayQuantity);
  1166. dgvToPayPlan.Rows[e.RowIndex].Cells[10].Value = UtilityClass.MarkNumber(dbPayAmount); //付款金額
  1167. for (int intCount = 0; intCount < dgvToPayPlan.Rows.Count; intCount++)
  1168. {
  1169. //重新整理期數 (專案分期模式,不可修改請款計劃)
  1170. if (!dgvToPayPlan.Rows[intCount].IsNewRow)
  1171. {
  1172. dgvToPayPlan.Rows[intCount].Cells[3].Value = intCount + 1;
  1173. }
  1174. }
  1175. }
  1176. else
  1177. {
  1178. dbPayQuantity = Convert.ToDouble(dgvToPayPlan.Rows[e.RowIndex].Cells[6].Value);
  1179. dbPayAmount = Convert.ToDouble(strPayUnitPrice) * Convert.ToDouble(dbPayQuantity);
  1180. dgvToPayPlan.Rows[e.RowIndex].Cells[10].Value = UtilityClass.MarkNumber(dbPayAmount); //付款金額
  1181. dbSumRestPayAmount = string.IsNullOrEmpty(strRestPayAmount) ? 0 : Convert.ToDouble(strRestPayAmount.Replace(",", "")); //可付款總額
  1182. for (int intCount = 0; intCount < dgvToPayPlan.Rows.Count; intCount++)
  1183. {
  1184. //重新整理期數
  1185. if (!dgvToPayPlan.Rows[intCount].IsNewRow)
  1186. {
  1187. dgvToPayPlan.Rows[intCount].Cells[3].Value = intCount + 1;
  1188. }
  1189. //重新計算相關資訊
  1190. if (strItemNo == (string)dgvToPayPlan.Rows[intCount].Cells[2].Value)
  1191. {
  1192. if (strPayClass != "")
  1193. {
  1194. strPayQuantity = (string)dgvToPayPlan.Rows[intCount].Cells[6].Value;
  1195. dbSumRestPay = dbSumRestPay - (string.IsNullOrEmpty(strPayQuantity) ? 0 : Convert.ToDouble(strPayQuantity.Replace(",", "")));
  1196. dgvToPayPlan.Rows[intCount].Cells[7].Value = UtilityClass.MarkNumber(dbSumRestPay, 2);
  1197. dbSumRestPayAmount = dbSumRestPayAmount - Convert.ToDouble(dgvToPayPlan.Rows[intCount].Cells[10].Value.ToString().Replace(",", ""));
  1198. dgvToPayPlan.Rows[intCount].Cells[11].Value = UtilityClass.MarkNumber(dbSumRestPayAmount);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. catch (Exception ex)
  1206. {
  1207. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1208. }
  1209. }
  1210. public void ReturnCustomer(string[] strCustomerData) //回傳客戶資料
  1211. {
  1212. txtCustomerID.Text = strCustomerData[0].ToString();
  1213. txtCustomerName.Text = strCustomerData[1].ToString();
  1214. }
  1215. private void GetHistoryList() //取得歷史資料
  1216. {
  1217. try
  1218. {
  1219. string strCustomerID = txtCustomerID.Text.Trim();
  1220. if (string.IsNullOrEmpty(strCustomerID)) return; //判斷空值即不執行
  1221. StringBuilder strPJSQL = new StringBuilder();
  1222. strPJSQL.Append("Select ProjectNumber,ProjectCName From OTB_PRJ_ProjectInfo Where CustomerID ='" + strCustomerID + "' ");
  1223. if (rbOrderStatusClose.Checked == true)
  1224. {
  1225. strPJSQL.Append("Union ");
  1226. strPJSQL.Append("Select ProjectNumber,ProjectCName From OTB_PRJ_ProjectInfo_Closed Where CustomerID ='" + strCustomerID + "' ");
  1227. }
  1228. DataTable dtTemp = UtilityClass.GetSQLResult(strPJSQL.ToString()).Tables["Result"];
  1229. }
  1230. catch (Exception ex)
  1231. {
  1232. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1233. }
  1234. }
  1235. private string GetSalesOrderNo(string strAccontingBookID) //取得最新訂單編號
  1236. {
  1237. try
  1238. {
  1239. string strReturnValue = "";
  1240. string strBookID = "";
  1241. switch (strAccountingBookID)
  1242. {
  1243. case "Origtek":
  1244. strBookID = "OTK";
  1245. break;
  1246. case "OrigtekEnergy":
  1247. strBookID = "IPS";
  1248. break;
  1249. }
  1250. string strCHKSQL = string.Format("Select CountMax From OTB_SYS_MaxNumber Where Type='ORD' And CountYear ={0} And CountMonth={1}", DateTime.Now.Year, DateTime.Now.Month);
  1251. DataTable dtTemp = UtilityClass.GetSQLResult(strCHKSQL).Tables["Result"];
  1252. if (dtTemp.Rows.Count > 0)
  1253. {
  1254. int intMaxNo = Convert.ToInt32(dtTemp.Rows[0]["CountMax"].ToString()) + 1;
  1255. string strSQL = string.Format("Update OTB_SYS_MaxNumber Set CountMax = {0} Where Type='ORD' And CountYear ={1} And CountMonth={2}", intMaxNo, DateTime.Now.Year, DateTime.Now.Month);
  1256. UtilityClass.RunSQLNonReturn(strSQL);
  1257. strReturnValue = strBookID + DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00") + intMaxNo.ToString("000");
  1258. }
  1259. else
  1260. {
  1261. int intMaxNo = 1;
  1262. string strSQL = "Insert OTB_SYS_MaxNumber (Type, CountYear, CountMonth, CountDay, CountMax, CreateUser, CreateDate, ModifyUser, ModifyDate) ";
  1263. strSQL += "Values ('ORD','" + DateTime.Now.Year.ToString() + "','" + DateTime.Now.Month.ToString() + "',' ','" + intMaxNo + "','" + strActiveUserID + "',GetDate(),'" + strActiveUserID + "', GetDate())";
  1264. UtilityClass.RunSQLNonReturn(strSQL);
  1265. strReturnValue = strBookID + DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00") + intMaxNo.ToString("000");
  1266. }
  1267. return strReturnValue;
  1268. }
  1269. catch (Exception ex)
  1270. {
  1271. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1272. return "";
  1273. }
  1274. }
  1275. public void CountOrderSum() //計算訂單總金額
  1276. {
  1277. try
  1278. {
  1279. if (dgvOrderContent.Rows.Count > 0)
  1280. {
  1281. double dbOrderSum = 0;
  1282. foreach (DataGridViewRow rwCount in dgvOrderContent.Rows)
  1283. {
  1284. string strCount = (string)rwCount.Cells["cAmount"].Value;
  1285. if (!string.IsNullOrEmpty(strCount)) dbOrderSum += Convert.ToDouble(strCount.Replace(",", ""));
  1286. }
  1287. txtOrderSum.Text = UtilityClass.MarkNumber(dbOrderSum);
  1288. }
  1289. }
  1290. catch (Exception ex)
  1291. {
  1292. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1293. }
  1294. }
  1295. public void CountOutSourcingSum() //計算委外總金額
  1296. {
  1297. try
  1298. {
  1299. if (dgvOutSourcingContent.Rows.Count > 0)
  1300. {
  1301. double dbOutSourcingSum = 0;
  1302. foreach (DataGridViewRow rwCount in dgvOutSourcingContent.Rows)
  1303. {
  1304. string strCount = (string)rwCount.Cells["cOutAmount"].Value;
  1305. if (!string.IsNullOrEmpty(strCount)) dbOutSourcingSum += Convert.ToDouble(strCount.Replace(",", ""));
  1306. }
  1307. txtOutSourcingSum.Text = UtilityClass.MarkNumber(dbOutSourcingSum);
  1308. }
  1309. }
  1310. catch (Exception ex)
  1311. {
  1312. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1313. }
  1314. }
  1315. private void GetOrderDetail(string strCustomerID, string strCustomerName, string strOrderID, string strProjectName, string strProjectNumber, string strPayType, string strOrderStatus) //取得付款細部資料
  1316. {
  1317. try
  1318. {
  1319. //物件宣告
  1320. StringBuilder sbSQL = new StringBuilder();
  1321. string strSalesOrderNo = ""; //訂單編號
  1322. string strOrderItem = ""; //項次(cOrderItem)
  1323. string strOrderContent = ""; //採購項目(cOrderContent)
  1324. string strUnitPrice = ""; //單價(cUnitPrice)
  1325. string strQuantity = ""; //數量(cQuantity)
  1326. string strTax = ""; //稅額(cTax、cReceiptTax)
  1327. string strMemo = ""; //備註(cOrderMemo)
  1328. string strOutOrderItem = ""; //項次(cOutOrderItem)
  1329. string strOutOrderContent = ""; //委外項目(cOutOrderContent)
  1330. string strOutUnitPrice = ""; //委外單價(cOutUnitPrice)
  1331. string strOutQuantity = ""; //委外數量(cOutQuantity)
  1332. string strOutMemo = ""; //備註(cOutOrderMemo)
  1333. string strReceiptItem = ""; //請款項目
  1334. string strItemNo = ""; //項次
  1335. string strPhase = ""; //期數
  1336. string strReceiptNo = ""; //請款單號
  1337. string strReceiptUnitPrice = ""; //請款單價
  1338. string strReceiptQuantity = ""; //請款數量
  1339. string strRestReceipt = ""; //請款餘量
  1340. string strExpectedReceiptDate = ""; //預計請款日期
  1341. string strActualReceiptDate = ""; //實際請款日期
  1342. string strExpectedReceiptedDate = ""; //預計收款日期
  1343. string strActualReceiptedDate = ""; //實際收款日期
  1344. string strReceiptAmount = ""; //請款金額
  1345. string strRestReceiptAmount = ""; //待請餘額
  1346. string strPhaseNo = ""; //期數
  1347. string strInvoiceNo = ""; //發票號碼
  1348. string strPayItem = ""; //請款項目
  1349. string strPayUnitPrice = ""; //請款單價
  1350. string strPayQuantity = ""; //請款數量
  1351. string strRestPay = ""; //請款餘量
  1352. string strExpectedPayDate = ""; //預計付款日期
  1353. string strActualPayDate = ""; //實際付款日期
  1354. string strPayAmount = ""; //付款金額
  1355. string strRestPayAmount = ""; //待付餘額
  1356. //取得訂單基本資料
  1357. txtCustomerID.Text = strCustomerID;
  1358. txtCustomerName.Text = strCustomerName;
  1359. txtSalesOrderNo.Text = strOrderID;
  1360. txtProjectName.Text = strProjectName;
  1361. txtProjectNumber.Text = strProjectNumber;
  1362. switch (strPayType) //設定付款方式
  1363. {
  1364. case "P":
  1365. rbPayType.Checked = true;
  1366. break;
  1367. case "M":
  1368. rbPayType1.Checked = true;
  1369. break;
  1370. case "D":
  1371. rbPayType2.Checked = true;
  1372. break;
  1373. case "H":
  1374. rbPayType3.Checked = true;
  1375. break;
  1376. }
  1377. switch (strOrderStatus) //設定訂單狀態
  1378. {
  1379. case "O":
  1380. rbOrderStatusOpen.Checked = true;
  1381. rbOrderStatusClose.Checked = false;
  1382. break;
  1383. case "C":
  1384. rbOrderStatusClose.Checked = true;
  1385. rbOrderStatusOpen.Checked = false;
  1386. break;
  1387. case " ":
  1388. rbOrderStatusOpen.Checked = true;
  1389. rbOrderStatusClose.Checked = false;
  1390. break;
  1391. }
  1392. //取得訂單內容資料
  1393. sbSQL.Clear();
  1394. sbSQL.Append("Select SalesOrderNo, ItemNo, OrderContent, UnitPrice, Quantity, Tax, Memo From OTB_SAL_OrderContent Where SalesOrderNo = '" + strOrderID + "' Order By ItemNo");
  1395. using (DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  1396. {
  1397. foreach (DataRow drData in dtTemp.Rows)
  1398. {
  1399. strOrderItem = (string)drData["ItemNo"];
  1400. strSalesOrderNo = (string)drData["SalesOrderNo"];
  1401. strOrderContent = (string)drData["OrderContent"];
  1402. strUnitPrice = (string)drData["UnitPrice"];
  1403. strQuantity = (string)drData["Quantity"];
  1404. strTax = (string)drData["Tax"];
  1405. strMemo = (string)drData["Memo"];
  1406. //產生資料
  1407. DataGridViewRow dgvRow = new DataGridViewRow();
  1408. dgvRow.CreateCells(dgvOrderContent);
  1409. dgvRow.Cells[0].Value = strOrderItem; //項次
  1410. strUnitPrice = (string.IsNullOrEmpty(strUnitPrice) ? "" : UtilityClass.DecryptDES(strUnitPrice, strKey)); //單價
  1411. strQuantity = (string.IsNullOrEmpty(strQuantity) ? "" : UtilityClass.DecryptDES(strQuantity, strKey)); //數量
  1412. strTax = (string.IsNullOrEmpty(strTax) ? "" : UtilityClass.DecryptDES(strTax, strKey)); //稅額
  1413. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strUnitPrice);
  1414. dgvRow.Cells[3].Value = UtilityClass.MarkNumber(strQuantity, 2);
  1415. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(strTax);
  1416. if (strQuantity.IndexOf("%") != -1)
  1417. {
  1418. dgvRow.Cells[5].Value = UtilityClass.MarkNumber((Convert.ToDouble(strUnitPrice.Replace(",", "")) * Convert.ToDouble(strQuantity.Replace("%", "")) / 100) + Convert.ToDouble(strTax.Replace(",","")));
  1419. }
  1420. else
  1421. {
  1422. dgvRow.Cells[5].Value = UtilityClass.MarkNumber((Convert.ToDouble(strUnitPrice.Replace(",", "")) * Convert.ToDouble(strQuantity.Replace(",", ""))) + Convert.ToDouble(strTax.Replace(",", "")));
  1423. }
  1424. dgvRow.Cells[6].Value = strMemo;
  1425. dgvOrderContent.Rows.Add(dgvRow);
  1426. }
  1427. CountOrderSum();
  1428. }
  1429. //取得委外內容資料
  1430. sbSQL.Clear();
  1431. sbSQL.Append("Select SalesOrderNo, ItemNo, OrderContent, UnitPrice, Quantity, Memo From OTB_SAL_OutSourcingContent Where SalesOrderNo = '" + strOrderID + "' Order By ItemNo");
  1432. using (DataTable dtOutTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  1433. {
  1434. foreach (DataRow drData in dtOutTemp.Rows)
  1435. {
  1436. strSalesOrderNo = (string)drData["SalesOrderNo"];
  1437. strOutOrderItem = (string)drData["ItemNo"];
  1438. strOutOrderContent = (string)drData["OrderContent"];
  1439. strOutUnitPrice = (string)drData["UnitPrice"];
  1440. strOutQuantity = (string)drData["Quantity"];
  1441. strOutMemo = (string)drData["Memo"];
  1442. //產生資料
  1443. DataGridViewRow dgvRow = new DataGridViewRow();
  1444. dgvRow.CreateCells(dgvOutSourcingContent);
  1445. dgvRow.Cells[0].Value = strOutOrderItem; //項次
  1446. strOutUnitPrice = (string.IsNullOrEmpty(strOutUnitPrice) ? "" : UtilityClass.DecryptDES(strOutUnitPrice, strKey)); //單價
  1447. strOutQuantity = (string.IsNullOrEmpty(strOutQuantity) ? "" : UtilityClass.DecryptDES(strOutQuantity, strKey)); //數量
  1448. dgvRow.Cells[2].Value = UtilityClass.MarkNumber(strOutUnitPrice);
  1449. dgvRow.Cells[3].Value = UtilityClass.MarkNumber(strOutQuantity, 2);
  1450. if (strOutQuantity.IndexOf("%") != -1)
  1451. {
  1452. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(Convert.ToDouble(strOutUnitPrice.Replace(",", "")) * Convert.ToDouble(strOutQuantity.Replace("%", "")) / 100);
  1453. }
  1454. else
  1455. {
  1456. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(Convert.ToDouble(strOutUnitPrice.Replace(",", "")) * Convert.ToDouble(strOutQuantity.Replace(",", "")));
  1457. }
  1458. dgvRow.Cells[5].Value = strMemo;
  1459. dgvOutSourcingContent.Rows.Add(dgvRow);
  1460. }
  1461. CountOutSourcingSum();
  1462. }
  1463. //取得請款計劃
  1464. sbSQL.Clear();
  1465. sbSQL.Append("Select * From OTB_SAL_ReceiptPlan Where SalesOrderNo = '" + strOrderID + "' Order By Phase");
  1466. using (DataTable dtOutTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  1467. {
  1468. foreach (DataRow drData in dtOutTemp.Rows)
  1469. {
  1470. strSalesOrderNo = drData["SalesOrderNo"].ToString();
  1471. strItemNo = drData["ItemNo"].ToString(); //項次
  1472. strPhase = drData["Phase"].ToString(); //期數
  1473. strReceiptNo = drData["ReceiptNo"].ToString(); //請款單號
  1474. strReceiptItem = drData["ReceiptItem"].ToString(); //請款項目
  1475. strReceiptUnitPrice = drData["ReceiptUnitPrice"].ToString(); //請款單價
  1476. strReceiptQuantity = drData["ReceiptQuantity"].ToString(); //請款數量
  1477. strRestReceipt = drData["RestReceipt"].ToString(); //請款餘量
  1478. strExpectedReceiptDate = drData["ExpectedReceiptDate"].ToString(); //預計請款日期
  1479. strActualReceiptDate = drData["ActualReceiptDate"].ToString(); //實際請款日期
  1480. strExpectedReceiptedDate = drData["ExpectedReceiptedDate"].ToString(); //預計收款日期
  1481. strActualReceiptedDate = drData["ActualReceiptedDate"].ToString(); //實際收款日期
  1482. strReceiptAmount = drData["ReceiptAmount"].ToString(); //請款金額
  1483. strTax = drData["Tax"].ToString(); //請款金額
  1484. strRestReceiptAmount = drData["RestReceiptAmount"].ToString(); //待請餘額
  1485. strMemo = drData["Memo"].ToString(); //備註
  1486. //產生資料
  1487. DataGridViewRow dgvRow = new DataGridViewRow();
  1488. dgvRow.CreateCells(dgvReceiptPlan);
  1489. dgvRow.Cells[1].Value = strReceiptItem; //請款項目
  1490. dgvRow.Cells[2].Value = strItemNo; //項次
  1491. dgvRow.Cells[3].Value = strPhase; //期數
  1492. dgvRow.Cells[4].Value = strReceiptNo; //請款單號
  1493. strReceiptUnitPrice = string.IsNullOrEmpty(strReceiptUnitPrice) ? "" : UtilityClass.DecryptDES(strReceiptUnitPrice, strKey); //請款單價
  1494. dgvRow.Cells[5].Value = UtilityClass.MarkNumber(strReceiptUnitPrice);
  1495. strReceiptQuantity = string.IsNullOrEmpty(strReceiptQuantity) ? "" : UtilityClass.DecryptDES(strReceiptQuantity, strKey); //請款數量
  1496. dgvRow.Cells[6].Value = UtilityClass.MarkNumber(strReceiptQuantity, 2);
  1497. strRestReceipt = string.IsNullOrEmpty(strRestReceipt) ? "" : UtilityClass.DecryptDES(strRestReceipt, strKey); //待請餘額
  1498. dgvRow.Cells[7].Value = UtilityClass.MarkNumber(strRestReceipt,2);
  1499. dgvRow.Cells[8].Value = string.IsNullOrEmpty(strExpectedReceiptDate) ? "" : Convert.ToDateTime(strExpectedReceiptDate).ToString("yyyy/MM/dd"); //預計請款日期
  1500. dgvRow.Cells[9].Value = string.IsNullOrEmpty(strActualReceiptDate) ? "" : Convert.ToDateTime(strActualReceiptDate).ToString("yyyy/MM/dd"); //實際請款日期
  1501. dgvRow.Cells[10].Value = string.IsNullOrEmpty(strExpectedReceiptedDate) ? "" : Convert.ToDateTime(strExpectedReceiptedDate).ToString("yyyy/MM/dd"); //預計收款日期
  1502. dgvRow.Cells[11].Value = string.IsNullOrEmpty(strActualReceiptedDate) ? "" : Convert.ToDateTime(strActualReceiptedDate).ToString("yyyy/MM/dd"); //實際收款日期
  1503. strTax = string.IsNullOrEmpty(strTax) ? "" : UtilityClass.DecryptDES(strTax, strKey); //稅額
  1504. dgvRow.Cells[12].Value = UtilityClass.MarkNumber(strTax);
  1505. strReceiptAmount = string.IsNullOrEmpty(strReceiptAmount) ? "" : UtilityClass.DecryptDES(strReceiptAmount, strKey); //請款金額
  1506. dgvRow.Cells[13].Value = UtilityClass.MarkNumber(strReceiptAmount);
  1507. strRestReceiptAmount = string.IsNullOrEmpty(strRestReceiptAmount) ? "" : UtilityClass.DecryptDES(strRestReceiptAmount, strKey); //待請餘額
  1508. dgvRow.Cells[14].Value = UtilityClass.MarkNumber(strRestReceiptAmount);
  1509. dgvRow.Cells[15].Value = strMemo; //備註
  1510. if (!string.IsNullOrEmpty(strReceiptNo)) //有請款記錄,設定為唯讀
  1511. {
  1512. dgvRow.ReadOnly = true;
  1513. }
  1514. dgvReceiptPlan.Rows.Add(dgvRow);
  1515. }
  1516. }
  1517. //取得付款計劃
  1518. sbSQL.Clear();
  1519. sbSQL.Append("Select * From OTB_SAL_PayPlan Where SalesOrderNo = '" + strOrderID + "' Order By PhaseNo");
  1520. using (DataTable dtOutTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"])
  1521. {
  1522. foreach (DataRow drData in dtOutTemp.Rows)
  1523. {
  1524. strSalesOrderNo = drData["SalesOrderNo"].ToString();
  1525. strItemNo = drData["ItemNo"].ToString(); //項次
  1526. strPhaseNo = drData["PhaseNo"].ToString(); //期數
  1527. strInvoiceNo = drData["InvoiceNo"].ToString(); //發票號碼
  1528. strPayItem = drData["PayItem"].ToString(); //請款項目
  1529. strPayUnitPrice = drData["PayUnitPrice"].ToString(); //請款單價
  1530. strPayQuantity = drData["PayQuantity"].ToString(); //請款數量
  1531. strRestPay = drData["RestPay"].ToString(); //請款餘量
  1532. strExpectedPayDate = drData["ExpectedPayDate"].ToString(); //預計付款日期
  1533. strActualPayDate = drData["ActualPayDate"].ToString(); //實際付款日期
  1534. strPayAmount = drData["PayAmount"].ToString(); //付款金額
  1535. strRestPayAmount = drData["RestPayAmount"].ToString(); //待付餘額
  1536. strMemo = drData["Memo"].ToString(); //備註
  1537. //產生資料
  1538. DataGridViewRow dgvRow = new DataGridViewRow();
  1539. dgvRow.CreateCells(dgvToPayPlan);
  1540. dgvRow.Cells[1].Value = strPayItem; //付款項目
  1541. dgvRow.Cells[2].Value = strItemNo; //項次
  1542. dgvRow.Cells[3].Value = strPhaseNo; //期數
  1543. dgvRow.Cells[4].Value = strInvoiceNo; //發票號碼
  1544. strPayUnitPrice = (string.IsNullOrEmpty(strPayUnitPrice) ? "" : UtilityClass.DecryptDES(strPayUnitPrice, strKey)); //付款單價
  1545. dgvRow.Cells[5].Value = UtilityClass.MarkNumber(strPayUnitPrice);
  1546. strPayQuantity = (string.IsNullOrEmpty(strPayQuantity) ? "" : UtilityClass.DecryptDES(strPayQuantity, strKey)); //付款數量
  1547. dgvRow.Cells[6].Value = UtilityClass.MarkNumber(strPayQuantity, 2);
  1548. strRestPay = (string.IsNullOrEmpty(strRestPay) ? "" : UtilityClass.DecryptDES(strRestPay, strKey)); //待付餘額
  1549. dgvRow.Cells[7].Value = UtilityClass.MarkNumber(strRestPay,2);
  1550. dgvRow.Cells[8].Value = (string.IsNullOrEmpty(strExpectedPayDate) ? "" : Convert.ToDateTime(strExpectedPayDate).ToString("yyyy/MM/dd")); //預計付款日期
  1551. dgvRow.Cells[9].Value = (string.IsNullOrEmpty(strActualPayDate) ? "" : Convert.ToDateTime(strActualPayDate).ToString("yyyy/MM/dd")); //實際付款日期
  1552. strPayAmount = (string.IsNullOrEmpty(strPayAmount) ? "" : UtilityClass.DecryptDES(strPayAmount, strKey)); //付款金額
  1553. dgvRow.Cells[10].Value = UtilityClass.MarkNumber(strPayAmount);
  1554. strRestPayAmount = (string.IsNullOrEmpty(strRestPayAmount) ? "" : UtilityClass.DecryptDES(strRestPayAmount, strKey)); //待付餘額
  1555. dgvRow.Cells[11].Value = UtilityClass.MarkNumber(strRestPayAmount);
  1556. dgvRow.Cells[12].Value = strMemo; //備註
  1557. if (!string.IsNullOrEmpty(strInvoiceNo)) //有付款記錄,設定為唯讀
  1558. {
  1559. dgvRow.ReadOnly = true;
  1560. }
  1561. dgvToPayPlan.Rows.Add(dgvRow);
  1562. }
  1563. }
  1564. }
  1565. catch (Exception ex)
  1566. {
  1567. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1568. }
  1569. }
  1570. public void CreateAR(DataGridViewCellEventArgs e) //增加應收帳款記錄
  1571. {
  1572. try
  1573. {
  1574. //物件宣告
  1575. string strCustomerID = "";
  1576. string strCustomerName = "";
  1577. string strSalesOrderNo = "";
  1578. string strProjectCName = "";
  1579. string strProjectNumber = "";
  1580. string strReceiptNo = "";
  1581. string strReceiptDate = "";
  1582. string strReceiptAmount = "";
  1583. string strMemo = "";
  1584. string strDC = "";
  1585. string strSubAccountingForAR = "";
  1586. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + "_Receipt.xml";
  1587. if (File.Exists(strXMLPath))
  1588. {
  1589. XDocument xmlContent = XDocument.Load(strXMLPath);
  1590. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  1591. {
  1592. strDC = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString();
  1593. switch (strDC)
  1594. {
  1595. case "D": //取得應收帳款的分錄編號
  1596. strSubAccountingForAR = xmlData.Element("SubAccounting").Value.ToString();
  1597. break;
  1598. }
  1599. }
  1600. //增加應收帳款
  1601. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  1602. {
  1603. string strSQL = "Insert Into OTB_FNC_AccountReceivable (AccountingBookID, SalesOrderNo, ProjectNumber, ProjectCName, ReceiptNo, CustomerID, CustomerName, ActualReceiptDate, ReceiptAmount, ReceiveStatus, Memo, CreateDate, CreateUser, ModifyDate, ModifyUser) Values ";
  1604. strSQL += "(@AccountingBookID, @SalesOrderNo, @ProjectNumber,@ProjectCName, @ReceiptNo, @CustomerID, @CustomerName, @ActualReceiptDate, @ReceiptAmount, 'N', @Memo, Getdate(), @CreateUser, Getdate(), @ModifyUser)";
  1605. strCustomerID = txtCustomerID.Text.Trim();
  1606. strCustomerName = txtCustomerName.Text.Trim();
  1607. strSalesOrderNo = txtSalesOrderNo.Text.Trim();
  1608. strProjectNumber = txtProjectNumber.Text.Trim();
  1609. strProjectCName = txtProjectName.Text.Trim();
  1610. strReceiptNo = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptNo"].Value;
  1611. strReceiptDate = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cActualReceiptDate"].Value;
  1612. strReceiptAmount = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptAmount"].Value;
  1613. strMemo = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cMemo"].Value;
  1614. //添加參數
  1615. sqlAdapter.InsertCommand = new SqlCommand();
  1616. sqlAdapter.InsertCommand.Connection = sqlConn;
  1617. sqlAdapter.InsertCommand.Transaction = sqlTran;
  1618. sqlAdapter.InsertCommand.CommandText = strSQL;
  1619. sqlAdapter.InsertCommand.Parameters.AddRange
  1620. (
  1621. new SqlParameter[]
  1622. {
  1623. new SqlParameter("@AccountingBookID",string.IsNullOrEmpty(strAccountingBookID) ? "" : strAccountingBookID),
  1624. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo),
  1625. new SqlParameter("@ProjectNumber",string.IsNullOrEmpty(strProjectNumber) ? "" : strProjectNumber),
  1626. new SqlParameter("@ProjectCName",string.IsNullOrEmpty(strProjectCName) ? "" : strProjectCName),
  1627. new SqlParameter("@ReceiptNo",string.IsNullOrEmpty(strReceiptNo) ? "" : strReceiptNo),
  1628. new SqlParameter("@CustomerID",string.IsNullOrEmpty(strCustomerID) ? "" : strCustomerID),
  1629. new SqlParameter("@CustomerName",string.IsNullOrEmpty(strCustomerName) ? "" : strCustomerName),
  1630. new SqlParameter("@ReceiptAmount",string.IsNullOrEmpty(strReceiptAmount) ? "" : UtilityClass.EncryptDES(strReceiptAmount,strKey)),
  1631. new SqlParameter("@Memo",string.IsNullOrEmpty(strMemo) ? "" : strMemo),
  1632. new SqlParameter("@CreateUser",strActiveUserID),
  1633. new SqlParameter("@ModifyUser",strActiveUserID)
  1634. }
  1635. );
  1636. SqlParameter spActualReceiptDate = new SqlParameter("@ActualReceiptDate", SqlDbType.DateTime);
  1637. spActualReceiptDate.Value = !UtilityClass.IsDate(strReceiptDate) ? null : strReceiptDate + " 00:00:00";
  1638. sqlAdapter.InsertCommand.Parameters.Add(spActualReceiptDate);
  1639. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  1640. {
  1641. sqlConn.Open();
  1642. }
  1643. sqlAdapter.InsertCommand.ExecuteNonQuery();
  1644. }
  1645. }
  1646. }
  1647. catch (Exception ex)
  1648. {
  1649. throw ex;
  1650. }
  1651. }
  1652. public void CreateAP(DataGridViewCellEventArgs e) //增加應付帳款記錄
  1653. {
  1654. try
  1655. {
  1656. //物件宣告
  1657. string strCustomerID = "";
  1658. string strCustomerName = "";
  1659. string strSalesOrderNo = "";
  1660. string strProjectNumber = "";
  1661. string strProjectCName = "";
  1662. string strInvoiceNo = "";
  1663. string strActualPaiedDate = "";
  1664. string strPayAmount = "";
  1665. string strMemo = "";
  1666. string strDC = "";
  1667. string strSubAccountingForAP = "";
  1668. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + "_Pay.xml";
  1669. if (File.Exists(strXMLPath))
  1670. {
  1671. XDocument xmlContent = XDocument.Load(strXMLPath);
  1672. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  1673. {
  1674. strDC = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString();
  1675. switch (strDC)
  1676. {
  1677. case "C": //取得應付帳款的分錄編號
  1678. strSubAccountingForAP = xmlData.Element("SubAccounting").Value.ToString();
  1679. break;
  1680. }
  1681. }
  1682. //增加應付帳款
  1683. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  1684. {
  1685. string strSQL = "Insert Into OTB_FNC_AccountPayable (AccountingBookID, SalesOrderNo, ProjectNumber, ProjectCName, InvoiceNo, CustomerID, CustomerName, ActualPaiedDate, PayAmount, PayStatus, Memo, CreateDate, CreateUser, ModifyDate, ModifyUser) Values ";
  1686. strSQL += "(@AccountingBookID, @SalesOrderNo, @ProjectNumber, @ProjectCName, @InvoiceNo, @CustomerID, @CustomerName, @ActualPaiedDate, @PayAmount, 'N', @Memo, Getdate(), @CreateUser, Getdate(), @ModifyUser)";
  1687. strCustomerID = txtCustomerID.Text.Trim();
  1688. strCustomerName = txtCustomerName.Text.Trim();
  1689. strSalesOrderNo = txtSalesOrderNo.Text.Trim();
  1690. strProjectNumber = txtProjectNumber.Text.Trim();
  1691. strProjectCName = txtProjectName.Text.Trim();
  1692. strInvoiceNo = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  1693. strActualPaiedDate = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value;
  1694. strPayAmount = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayAmount"].Value;
  1695. strMemo = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayMemo"].Value;
  1696. //添加參數
  1697. sqlAdapter.InsertCommand = new SqlCommand();
  1698. sqlAdapter.InsertCommand.Connection = sqlConn;
  1699. sqlAdapter.InsertCommand.Transaction = sqlTran;
  1700. sqlAdapter.InsertCommand.CommandText = strSQL;
  1701. sqlAdapter.InsertCommand.Parameters.AddRange
  1702. (
  1703. new SqlParameter[]
  1704. {
  1705. new SqlParameter("@AccountingBookID",string.IsNullOrEmpty(strAccountingBookID) ? "" : strAccountingBookID),
  1706. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo),
  1707. new SqlParameter("@ProjectNumber",string.IsNullOrEmpty(strProjectNumber) ? "" : strProjectNumber),
  1708. new SqlParameter("@ProjectCName",string.IsNullOrEmpty(strProjectCName) ? "" : strProjectCName),
  1709. new SqlParameter("@InvoiceNo",string.IsNullOrEmpty(strInvoiceNo) ? "" : strInvoiceNo),
  1710. new SqlParameter("@CustomerID",string.IsNullOrEmpty(strCustomerID) ? "" : strCustomerID),
  1711. new SqlParameter("@CustomerName",string.IsNullOrEmpty(strCustomerName) ? "" : strCustomerName),
  1712. new SqlParameter("@PayAmount",string.IsNullOrEmpty(strPayAmount) ? "" : UtilityClass.EncryptDES(strPayAmount,strKey)),
  1713. new SqlParameter("@Memo",string.IsNullOrEmpty(strMemo) ? "" : strMemo),
  1714. new SqlParameter("@CreateUser",strActiveUserID),
  1715. new SqlParameter("@ModifyUser",strActiveUserID)
  1716. }
  1717. );
  1718. SqlParameter spActualPayDate = new SqlParameter("@ActualPaiedDate", SqlDbType.DateTime);
  1719. spActualPayDate.Value = !UtilityClass.IsDate(strActualPaiedDate) ? null : strActualPaiedDate + " 00:00:00";
  1720. sqlAdapter.InsertCommand.Parameters.Add(spActualPayDate);
  1721. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  1722. {
  1723. sqlConn.Open();
  1724. }
  1725. sqlAdapter.InsertCommand.ExecuteNonQuery();
  1726. }
  1727. }
  1728. }
  1729. catch (Exception ex)
  1730. {
  1731. throw ex;
  1732. }
  1733. }
  1734. #endregion
  1735. #region 事件觸發及問題處理
  1736. private void PayPlanManagement_Load(object sender, EventArgs e)
  1737. {
  1738. SetupStatus();
  1739. LockForm();
  1740. GetOrderList();
  1741. }
  1742. private void tsbExit_Click(object sender, EventArgs e)
  1743. {
  1744. this.Close();
  1745. }
  1746. private void btnGetCustomer_Click(object sender, EventArgs e)
  1747. {
  1748. PickCustomer pkItemForm = new PickCustomer();
  1749. pkItemForm.txtCustomerName.Text = txtCustomerName.Text.Trim();
  1750. pkItemForm.strOwnerForm = "PayPlanManagement";
  1751. pkItemForm.Owner = this;
  1752. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  1753. pkItemForm.ShowDialog();
  1754. }
  1755. private void tsbClean_Click(object sender, EventArgs e)
  1756. {
  1757. CleanForm();
  1758. }
  1759. private void tsbCancel_Click(object sender, EventArgs e)
  1760. {
  1761. CleanToolbar();
  1762. StatusChange("NONE");
  1763. }
  1764. private void tsbSearch_Click(object sender, EventArgs e)
  1765. {
  1766. StatusChange("SEARCH");
  1767. }
  1768. private void tsbDelete_Click(object sender, EventArgs e)
  1769. {
  1770. if (txtSalesOrderNo.Text.Trim() == "")
  1771. {
  1772. MessageBox.Show("請選擇要刪除的訂單請款資料", "提示");
  1773. }
  1774. else
  1775. {
  1776. if (MessageBox.Show("請問您是否要刪除本訂單的相關請款資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  1777. {
  1778. try
  1779. {
  1780. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  1781. {
  1782. sqlConn.Open();
  1783. }
  1784. sqlTran = sqlConn.BeginTransaction();
  1785. StringBuilder sbSQL = new StringBuilder();
  1786. string strSalesOrderNo = txtSalesOrderNo.Text.Trim();
  1787. //清除訂單及委外內容
  1788. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  1789. {
  1790. sbSQL.Clear();
  1791. sbSQL.Append("Delete OTB_SAL_OrderList Where SalesOrderNo = @SalesOrderNo ");
  1792. sbSQL.Append("Delete OTB_SAL_OrderContent Where SalesOrderNo = @SalesOrderNo ");
  1793. sbSQL.Append("Delete OTB_SAL_OutSourcingContent Where SalesOrderNo = @SalesOrderNo ");
  1794. //添加參數
  1795. sqlAdapter.DeleteCommand = new SqlCommand();
  1796. sqlAdapter.DeleteCommand.Connection = sqlConn;
  1797. sqlAdapter.DeleteCommand.Transaction = sqlTran;
  1798. sqlAdapter.DeleteCommand.CommandText = sbSQL.ToString();
  1799. sqlAdapter.DeleteCommand.Parameters.AddRange
  1800. (
  1801. new SqlParameter[]
  1802. {
  1803. new SqlParameter("@SalesOrderNo",string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo)
  1804. }
  1805. );
  1806. sqlAdapter.DeleteCommand.ExecuteNonQuery();
  1807. sqlTran.Commit();
  1808. StatusChange("SEARCH");
  1809. GoEvent();
  1810. }
  1811. }
  1812. catch (Exception ex)
  1813. {
  1814. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1815. sqlTran.Rollback();
  1816. }
  1817. }
  1818. }
  1819. }
  1820. private void tsbEdit_Click(object sender, EventArgs e)
  1821. {
  1822. if (txtProjectName.Text.Trim() == "")
  1823. {
  1824. MessageBox.Show("請選擇要修改的資料!", "提示");
  1825. return;
  1826. }
  1827. else
  1828. {
  1829. StatusChange("MODIFY");
  1830. dgvOrderList.ReadOnly = true;
  1831. if (txtSalesOrderNo.Text != "")
  1832. {
  1833. txtSalesOrderNo.ReadOnly = true;
  1834. btnGetSalesOrderNo.Enabled = false;
  1835. txtCustomerName.ReadOnly = true;
  1836. btnGetCustomer.Enabled = false;
  1837. txtProjectName.ReadOnly = true;
  1838. tcOrderData.SelectedTab = OrderTab;
  1839. btnArrangeProject.Enabled = true;
  1840. btnArrangToPayProject.Enabled = true;
  1841. rbOrderStatusClose.Enabled = true;
  1842. rbOrderStatusOpen.Enabled = true;
  1843. }
  1844. }
  1845. }
  1846. private void tsbOK_Click(object sender, EventArgs e)
  1847. {
  1848. try
  1849. {
  1850. GoEvent();
  1851. //還原Toolbar狀態
  1852. tsbSearch.Enabled = true;
  1853. tsbOK.Visible = false;
  1854. //關閉查詢條件
  1855. LockForm();
  1856. StatusChange("NONE");
  1857. }
  1858. catch (Exception ex)
  1859. {
  1860. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  1861. }
  1862. }
  1863. private void cbHistory_CheckedChanged(object sender, EventArgs e)
  1864. {
  1865. GetHistoryList();
  1866. }
  1867. private void dgvOrderList_CellClick(object sender, DataGridViewCellEventArgs e)
  1868. {
  1869. CleanForm();
  1870. LockForm();
  1871. string strCustomerID = dgvOrderList.CurrentRow.Cells["cCustomerID"].Value.ToString();
  1872. string strCustomerName = dgvOrderList.CurrentRow.Cells["cCustomerName"].Value.ToString();
  1873. string strSalesOrderID = dgvOrderList.CurrentRow.Cells["cOrderID"].Value.ToString();
  1874. string strProjectName = dgvOrderList.CurrentRow.Cells["cProjectName"].Value.ToString();
  1875. string strProjectNumber = dgvOrderList.CurrentRow.Cells["cProjectNumber"].Value.ToString();
  1876. string strPayType = dgvOrderList.CurrentRow.Cells["cPayType"].Value.ToString();
  1877. string strOrderStatus = dgvOrderList.CurrentRow.Cells["cOrderStatus"].Value.ToString();
  1878. ((MainForm)ParentForm).SsStatus.Items["tsslModifyUser"].Text = dgvOrderList.CurrentRow.Cells["cModifyUser"].Value.ToString();
  1879. ((MainForm)ParentForm).SsStatus.Items["tsslModifyDate"].Text = dgvOrderList.CurrentRow.Cells["cModifyDate"].Value.ToString();
  1880. GetOrderDetail(strCustomerID, strCustomerName, strSalesOrderID, strProjectName, strProjectNumber, strPayType, strOrderStatus);
  1881. UtilityClass.SetGridColor(dgvOrderContent);
  1882. UtilityClass.SetGridColor(dgvOutSourcingContent);
  1883. UtilityClass.SetGridColor(dgvReceiptPlan);
  1884. UtilityClass.SetGridColor(dgvToPayPlan);
  1885. }
  1886. private void tsbSave_Click(object sender, EventArgs e)
  1887. {
  1888. GoEvent(); //儲存作業
  1889. }
  1890. private void btnGetSalesOrderNo_Click(object sender, EventArgs e)
  1891. {
  1892. txtSalesOrderNo.Text = GetSalesOrderNo(strAccountingBookID);
  1893. }
  1894. private void dgvOrderContent_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
  1895. {
  1896. //設定項次
  1897. string strOrderItem = (string)dgvOrderContent.Rows[e.RowIndex].Cells[0].Value;
  1898. if (!string.IsNullOrEmpty(strOrderItem))
  1899. {
  1900. string strSQL = "Select OrderContent From OTB_SAL_OrderContent Where SalesOrderNo='" + txtSalesOrderNo.Text.Trim() + "' And ItemNo = '" + strOrderItem + "'";
  1901. string strPayContent = UtilityClass.GetSQLResult(strSQL).Tables["Result"].Rows[0]["OrderContent"].ToString();
  1902. //設定採購項目
  1903. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvOrderContent.Rows[e.RowIndex].Cells["cOrderContent"];
  1904. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  1905. cbCell.DisplayMember = "ArgumentValue";
  1906. cbCell.ValueMember = "ArgumentID";
  1907. cbCell.Value = strPayContent;
  1908. }
  1909. }
  1910. private void dgvOrderContent_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  1911. {
  1912. //物件宣告
  1913. string strUnitPrice = "";
  1914. string strQuantity = "";
  1915. string strTax = "";
  1916. string strSub = "";
  1917. //Cell[0] : 項次(cOrderItem)
  1918. //Cell[1] : 採購項目(cOrderContent)
  1919. //Cell[2] : 單價(cUnitPrice)
  1920. //Cell[3] : 數量(cQuantity)
  1921. //Cell[4] : 稅額(cTax)
  1922. //Cell[5] : 總計(cAmount)
  1923. //加上三位一撇效果
  1924. if (dgvOrderContent.Columns[e.ColumnIndex].Name == "cUnitPrice")
  1925. {
  1926. strUnitPrice = (string)dgvOrderContent.Rows[e.RowIndex].Cells["cUnitPrice"].Value;
  1927. if (string.IsNullOrEmpty(strUnitPrice))
  1928. {
  1929. return;
  1930. }
  1931. else
  1932. {
  1933. dgvOrderContent.Rows[e.RowIndex].Cells["cUnitPrice"].Value = UtilityClass.MarkNumber(strUnitPrice);
  1934. }
  1935. }
  1936. if (dgvOrderContent.Columns[e.ColumnIndex].Name == "cQuantity")
  1937. {
  1938. strQuantity = (string)dgvOrderContent.Rows[e.RowIndex].Cells["cQuantity"].Value;
  1939. if (string.IsNullOrEmpty(strQuantity))
  1940. {
  1941. return;
  1942. }
  1943. else
  1944. {
  1945. dgvOrderContent.Rows[e.RowIndex].Cells["cQuantity"].Value = UtilityClass.MarkNumber(strQuantity, 2);
  1946. }
  1947. }
  1948. if (dgvOrderContent.Columns[e.ColumnIndex].Name == "cTax")
  1949. {
  1950. strTax = (string)dgvOrderContent.Rows[e.RowIndex].Cells["cTax"].Value;
  1951. if (string.IsNullOrEmpty(strTax))
  1952. {
  1953. return;
  1954. }
  1955. else
  1956. {
  1957. dgvOrderContent.Rows[e.RowIndex].Cells["cTax"].Value = UtilityClass.MarkNumber(strTax);
  1958. }
  1959. }
  1960. //數量變動
  1961. if (dgvOrderContent.Columns[e.ColumnIndex].Name.ToString() == "cQuantity" | dgvOrderContent.Columns[e.ColumnIndex].Name.ToString() == "cUnitPrice")
  1962. {
  1963. strUnitPrice = (string)dgvOrderContent.Rows[e.RowIndex].Cells["cUnitPrice"].Value;
  1964. strQuantity = (string)dgvOrderContent.Rows[e.RowIndex].Cells["cQuantity"].Value;
  1965. strTax = (string)dgvOrderContent.Rows[e.RowIndex].Cells["cTax"].Value;
  1966. if (!string.IsNullOrEmpty(strUnitPrice) & !string.IsNullOrEmpty(strQuantity))
  1967. {
  1968. if (strQuantity.IndexOf("%") == -1)
  1969. {
  1970. strSub = (Convert.ToDouble(strUnitPrice.Replace(",", "")) * Convert.ToDouble(strQuantity.Replace(",", ""))).ToString();
  1971. double dbTax = Convert.ToDouble(strSub.Replace(",", "")) * 0.05;
  1972. dgvOrderContent.Rows[e.RowIndex].Cells["cTax"].Value = Utility.UtilityClass.MarkNumber(dbTax.ToString());
  1973. dgvOrderContent.Rows[e.RowIndex].Cells["cAmount"].Value = UtilityClass.MarkNumber(Convert.ToDouble(strSub) + dbTax);
  1974. }
  1975. else
  1976. {
  1977. strSub = (Convert.ToDouble(strUnitPrice.Replace(",", "")) * Convert.ToDouble(strQuantity.Replace("%", "")) / 100).ToString();
  1978. double dbTax = Convert.ToDouble(strSub.Replace(",", "")) * 0.05;
  1979. dgvOrderContent.Rows[e.RowIndex].Cells["cTax"].Value = Utility.UtilityClass.MarkNumber(dbTax.ToString());
  1980. dgvOrderContent.Rows[e.RowIndex].Cells["cAmount"].Value = UtilityClass.MarkNumber(Convert.ToDouble(strSub) + dbTax);
  1981. }
  1982. }
  1983. }
  1984. CountOrderSum(); //重新計算訂單總金額
  1985. }
  1986. private void dgvOrderContent_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  1987. {
  1988. //物件宣告
  1989. string strUnitPrice = "";
  1990. string strQuantity = "";
  1991. //Cell[0] : 項次(cOrderItem)
  1992. //Cell[1] : 採購項目(cOrderContent)
  1993. //Cell[2] : 單價(cUnitPrice)
  1994. //Cell[3] : 數量(cQuantity)
  1995. //Cell[4] : 總計(cAmount)
  1996. if (dgvOrderContent.Columns[e.ColumnIndex].Name == "cUnitPrice")
  1997. {
  1998. strUnitPrice = (string)dgvOrderContent.Rows[e.RowIndex].Cells["cUnitPrice"].Value;
  1999. if (string.IsNullOrEmpty(strUnitPrice))
  2000. {
  2001. return;
  2002. }
  2003. else
  2004. {
  2005. dgvOrderContent.Rows[e.RowIndex].Cells["cUnitPrice"].Value = strUnitPrice.Replace(",", "");
  2006. }
  2007. }
  2008. if (dgvOrderContent.Columns[e.ColumnIndex].Name == "cQuantity")
  2009. {
  2010. strQuantity = (string)dgvOrderContent.Rows[e.RowIndex].Cells["cQuantity"].Value;
  2011. if (string.IsNullOrEmpty(strQuantity))
  2012. {
  2013. return;
  2014. }
  2015. else
  2016. {
  2017. dgvOrderContent.Rows[e.RowIndex].Cells["cQuantity"].Value = strQuantity.Replace(",", "");
  2018. }
  2019. }
  2020. }
  2021. private void dgvOrderContent_RowEnter(object sender, DataGridViewCellEventArgs e)
  2022. {
  2023. dgvOrderContent.Rows[e.RowIndex].Cells["cOrderItem"].Value = (e.RowIndex + 1).ToString();
  2024. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvOrderContent.Rows[e.RowIndex].Cells["cOrderContent"];
  2025. if (cbCell.Items.Count == 0)
  2026. {
  2027. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2028. cbCell.DisplayMember = "ArgumentValue";
  2029. cbCell.ValueMember = "ArgumentID";
  2030. }
  2031. }
  2032. private void dgvOrderContent_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  2033. {
  2034. string strErrorMsg = "";
  2035. int intMsgCount = 0;
  2036. try
  2037. {
  2038. //進行資料檢查
  2039. DataGridViewComboBoxCell cbCheck = (DataGridViewComboBoxCell)dgvOrderContent.Rows[e.RowIndex].Cells["cOrderContent"];
  2040. if (string.IsNullOrEmpty((string)cbCheck.Value))
  2041. {
  2042. strErrorMsg = "請選擇採購項目";
  2043. intMsgCount += 1;
  2044. }
  2045. if (string.IsNullOrEmpty((string)dgvOrderContent.Rows[e.RowIndex].Cells["cUnitPrice"].Value))
  2046. {
  2047. strErrorMsg = "請輸入採購單價";
  2048. intMsgCount += 1;
  2049. }
  2050. if (string.IsNullOrEmpty((string)dgvOrderContent.Rows[e.RowIndex].Cells["cQuantity"].Value))
  2051. {
  2052. strErrorMsg = "請輸入採購數量";
  2053. intMsgCount += 1;
  2054. }
  2055. //else if(!rbPayType.Checked == true)
  2056. //{
  2057. // if (dgvOrderContent.Rows[e.RowIndex].Cells["cQuantity"].Value.ToString().IndexOf("%") != -1)
  2058. // {
  2059. // strErrorMsg = "請款方式為專案形態才能以百分比方式請款!";
  2060. // intMsgCount += 1;
  2061. // }
  2062. //}
  2063. if (strErrorMsg != "" && intMsgCount < 3)
  2064. {
  2065. dgvOrderContent.Rows[e.RowIndex].ErrorText = strErrorMsg;
  2066. e.Cancel = true;
  2067. }
  2068. }
  2069. catch (Exception ex)
  2070. {
  2071. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  2072. }
  2073. }
  2074. private void dgvOrderContent_RowValidated(object sender, DataGridViewCellEventArgs e)
  2075. {
  2076. dgvOrderContent.Rows[e.RowIndex].ErrorText = "";
  2077. }
  2078. private void btnArrangeProject_Click(object sender, EventArgs e)
  2079. {
  2080. string strPayType = "";
  2081. int intPayPlanItem = 1;
  2082. string strReceiptNo = ""; //發票編號
  2083. string strOrderSum = "0"; //訂單總金額
  2084. string strAmount = "0"; //請款金額
  2085. string strBalance = "0"; //餘額
  2086. string strQuantity = "0"; //數量
  2087. string strQuantityStyle = ""; //數量呈現模式(P:百分比、D:三位一撇)
  2088. string strQuantityAmount = "0"; //訂單總量
  2089. string strReceiptUnitPrice = "0"; //請款單價
  2090. string strReceiptQuantity = "0"; //請款數量
  2091. string strRestQuantity = "0"; //請款餘量
  2092. string strTax = "0"; //稅額
  2093. bool blFound = true; //允許建立
  2094. if (MessageBox.Show("請問您是否確認要產生請款計劃?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  2095. {
  2096. foreach (DataGridViewRow drData in dgvReceiptPlan.Rows)
  2097. {
  2098. strReceiptNo = (string)drData.Cells["cReceiptNo"].Value;
  2099. if (!string.IsNullOrEmpty(strReceiptNo))
  2100. {
  2101. MessageBox.Show("目前已有請款計劃,無法重新產生請款計劃!", "提示");
  2102. return;
  2103. }
  2104. }
  2105. dgvReceiptPlan.Rows.Clear(); //重新產生,先清除舊資料
  2106. //取得資訊
  2107. if (rbPayType.Checked == true)
  2108. {
  2109. strPayType = "P"; //專案類別
  2110. foreach (DataGridViewRow drData in dgvOrderContent.Rows) //數量加總
  2111. {
  2112. strQuantity = (string)drData.Cells["cQuantity"].Value;
  2113. if (!string.IsNullOrEmpty(strQuantity))
  2114. {
  2115. if (strQuantity.IndexOf("%") == -1)
  2116. {
  2117. strQuantityAmount = ((string.IsNullOrEmpty(strQuantityAmount) ? 0 : Convert.ToDouble(strQuantityAmount)) + Convert.ToDouble(strQuantity.Replace(",", ""))).ToString();
  2118. strQuantityStyle = "D";
  2119. }
  2120. else
  2121. {
  2122. strQuantityAmount = ((string.IsNullOrEmpty(strQuantityAmount) ? 0 : Convert.ToDouble(strQuantityAmount)) + Convert.ToDouble(strQuantity.Replace("%", ""))).ToString();
  2123. strQuantityStyle = "P";
  2124. }
  2125. }
  2126. strAmount = (string)drData.Cells["cAmount"].Value; //小計加總
  2127. strOrderSum = (Convert.ToDouble(strOrderSum) + (string.IsNullOrEmpty(strAmount) ? 0 : Convert.ToDouble(strAmount.Replace(",", "")))).ToString();
  2128. }
  2129. switch (strQuantityStyle)
  2130. {
  2131. case "P":
  2132. strQuantityAmount += "%";
  2133. break;
  2134. case "D":
  2135. strQuantityAmount = UtilityClass.MarkNumber(strQuantityAmount, 2);
  2136. break;
  2137. }
  2138. }
  2139. if (rbPayType1.Checked == true)
  2140. {
  2141. strPayType = "M"; //人月類別
  2142. foreach (DataGridViewRow drData in dgvOrderContent.Rows)
  2143. {
  2144. strAmount = (string)drData.Cells["cAmount"].Value;
  2145. strOrderSum = (Convert.ToDouble(strOrderSum) + (string.IsNullOrEmpty(strAmount) ? 0 : Convert.ToDouble(strAmount.Replace(",", "")))).ToString();
  2146. }
  2147. }
  2148. if (rbPayType2.Checked == true)
  2149. {
  2150. strPayType = "D";
  2151. }
  2152. if (rbPayType3.Checked == true)
  2153. {
  2154. strPayType = "H";
  2155. }
  2156. //資料處理
  2157. switch (strPayType)
  2158. {
  2159. case "P":
  2160. strBalance = strOrderSum; //訂單總金額
  2161. strRestQuantity = strQuantityAmount; //總數量
  2162. foreach (DataGridViewRow drData in dgvOrderContent.Rows)
  2163. {
  2164. DataGridViewComboBoxCell cbCHeck = (DataGridViewComboBoxCell)drData.Cells["cOrderContent"];
  2165. if (!string.IsNullOrEmpty((string)cbCHeck.Value))
  2166. {
  2167. DataGridViewRow dgvRow = new DataGridViewRow();
  2168. dgvRow.CreateCells(dgvReceiptPlan);
  2169. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvRow.Cells[1];
  2170. if (cbCell.Items.Count == 0)
  2171. {
  2172. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2173. cbCell.DisplayMember = "ArgumentValue";
  2174. cbCell.ValueMember = "ArgumentID";
  2175. }
  2176. cbCell.Value = drData.Cells["cOrderContent"].Value; //請款項目
  2177. dgvRow.Cells[2].Value = drData.Cells["cOrderItem"].Value; //項次
  2178. dgvRow.Cells[3].Value = (intPayPlanItem++).ToString(); //期數
  2179. strReceiptUnitPrice = (string)drData.Cells["cUnitPrice"].Value;
  2180. dgvRow.Cells[5].Value = string.IsNullOrEmpty(strReceiptUnitPrice) ? "" : strReceiptUnitPrice; //請款單價
  2181. strReceiptQuantity = (string)drData.Cells["cQuantity"].Value;
  2182. dgvRow.Cells[6].Value = string.IsNullOrEmpty(strReceiptQuantity) ? "" : strReceiptQuantity; //請款數量
  2183. if (strQuantityAmount.IndexOf("%") != -1) //請款餘量
  2184. {
  2185. strRestQuantity = ((string.IsNullOrEmpty(strRestQuantity) ? 0 : Convert.ToDouble(strRestQuantity.Replace("%", ""))) - (string.IsNullOrEmpty(strReceiptQuantity) ? 0 : Convert.ToDouble(strReceiptQuantity.Replace("%", "")))).ToString() + "%";
  2186. dgvRow.Cells[7].Value = strRestQuantity;
  2187. }
  2188. else if (strQuantityAmount.IndexOf(",") != -1)
  2189. {
  2190. strRestQuantity = ((string.IsNullOrEmpty(strRestQuantity) ? 0 : Convert.ToDouble(strRestQuantity.Replace(",", ""))) - (string.IsNullOrEmpty(strReceiptQuantity) ? 0 : Convert.ToDouble(strReceiptQuantity.Replace(",", "")))).ToString();
  2191. dgvRow.Cells[7].Value = UtilityClass.MarkNumber(strRestQuantity, 2);
  2192. }
  2193. strAmount = (string)drData.Cells["cAmount"].Value; //請款金額
  2194. strTax = (Convert.ToDouble(strAmount.Replace(",", "")) / 1.05 * 0.05).ToString();
  2195. dgvRow.Cells[12].Value = string.IsNullOrEmpty(strAmount) ? "0" : UtilityClass.MarkNumber(strTax); //稅額
  2196. dgvRow.Cells[13].Value = string.IsNullOrEmpty(strAmount) ? "" : strAmount;
  2197. strBalance = ((string.IsNullOrEmpty(strBalance) ? 0 : Convert.ToDouble(strBalance.Replace(",", ""))) - (string.IsNullOrEmpty(strAmount) ? 0 : Convert.ToDouble(strAmount.Replace(",", "")))).ToString();
  2198. dgvRow.Cells[14].Value = UtilityClass.MarkNumber(strBalance); //待請餘額
  2199. dgvReceiptPlan.Rows.Add(dgvRow);
  2200. }
  2201. }
  2202. break;
  2203. case "M":
  2204. dgvReceiptPlan.Rows.Clear(); //重新產生計劃
  2205. double dbQuantity = 0;
  2206. double dbRestReceiptQuantity = 0;
  2207. double dbBalance = 0;
  2208. double dbSpendQuantity = 0;
  2209. double dbAmount = 0;
  2210. //確認每個月請款數量
  2211. InputDialog frmInput = new InputDialog();
  2212. frmInput.lbQuestionMessage.Text = "請輸入每月請款人力數!";
  2213. frmInput.txtInput.Text = "1";
  2214. frmInput.StartPosition = FormStartPosition.CenterParent;
  2215. DialogResult dr = frmInput.ShowDialog();
  2216. if (dr == DialogResult.OK)
  2217. {
  2218. dbSpendQuantity = Convert.ToDouble(frmInput.GetMsg());
  2219. }
  2220. foreach (DataGridViewRow drData in dgvOrderContent.Rows)
  2221. {
  2222. dbQuantity = 0;
  2223. strQuantity = (string)drData.Cells["cQuantity"].Value; //數量
  2224. dbRestReceiptQuantity = Convert.ToDouble(string.IsNullOrEmpty(strQuantity) ? 0 : Convert.ToDouble(strQuantity.Replace(",", "")));
  2225. dbAmount = Convert.ToDouble(drData.Cells["cAmount"].Value);
  2226. dbBalance = string.IsNullOrEmpty(dbAmount.ToString()) ? 0 : dbAmount;
  2227. DataGridViewComboBoxCell cbCHeck = (DataGridViewComboBoxCell)drData.Cells["cOrderContent"];
  2228. blFound = false;
  2229. if (!drData.IsNewRow)
  2230. {
  2231. dbQuantity = Convert.ToDouble(drData.Cells["cQuantity"].Value.ToString());
  2232. for (double dbCount = 1; dbCount <= dbQuantity; dbCount++)
  2233. {
  2234. DataGridViewRow dgvRow = new DataGridViewRow();
  2235. dgvRow.CreateCells(dgvReceiptPlan);
  2236. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvRow.Cells[1];
  2237. if (cbCell.Items.Count == 0)
  2238. {
  2239. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2240. cbCell.DisplayMember = "ArgumentValue";
  2241. cbCell.ValueMember = "ArgumentID";
  2242. }
  2243. cbCell.Value = cbCHeck.Value; //請款項目
  2244. dgvRow.Cells[2].Value = drData.Cells["cOrderItem"].Value; //項次
  2245. dgvRow.Cells[3].Value = (intPayPlanItem++).ToString(); //期數
  2246. strReceiptUnitPrice = (string)drData.Cells["cUnitPrice"].Value;
  2247. dgvRow.Cells[5].Value = string.IsNullOrEmpty(strReceiptUnitPrice) ? "" : strReceiptUnitPrice; //請款單價
  2248. if (dbRestReceiptQuantity >= dbSpendQuantity)
  2249. {
  2250. dgvRow.Cells[6].Value = UtilityClass.MarkNumber(dbSpendQuantity, 2); //請款數量
  2251. dbRestReceiptQuantity = dbRestReceiptQuantity - dbSpendQuantity;
  2252. dgvRow.Cells[7].Value = UtilityClass.MarkNumber(dbRestReceiptQuantity, 2); //請款餘量
  2253. }
  2254. else
  2255. {
  2256. dgvRow.Cells[6].Value = UtilityClass.MarkNumber(dbRestReceiptQuantity, 2); //請款數量
  2257. dbRestReceiptQuantity = dbRestReceiptQuantity - dbSpendQuantity;
  2258. if (dbRestReceiptQuantity < 0) dgvRow.Cells[7].Value = UtilityClass.MarkNumber(0, 2); //請款餘量
  2259. }
  2260. dbAmount = Convert.ToDouble(drData.Cells["cUnitPrice"].Value) * Convert.ToDouble(dgvRow.Cells[6].Value);
  2261. strTax = (dbAmount * 0.05).ToString();
  2262. dgvRow.Cells[12].Value = string.IsNullOrEmpty(dbAmount.ToString()) ? "0" : UtilityClass.MarkNumber(strTax); //稅額
  2263. dgvRow.Cells[13].Value = string.IsNullOrEmpty(dbAmount.ToString()) ? "0" : UtilityClass.MarkNumber(dbAmount * 1.05); //請款金額
  2264. dbBalance = dbBalance - (dbAmount * 1.05);
  2265. dgvRow.Cells[14].Value = UtilityClass.MarkNumber(dbBalance); //請款餘額
  2266. if (Convert.ToDouble(dgvRow.Cells[6].Value) > 0)
  2267. {
  2268. dgvReceiptPlan.Rows.Add(dgvRow);
  2269. }
  2270. }
  2271. }
  2272. }
  2273. break;
  2274. case "D":
  2275. MessageBox.Show("人日無法產生請款計劃!","提示");
  2276. break;
  2277. case "H":
  2278. MessageBox.Show("人時無法產生請款計劃!", "提示");
  2279. break;
  2280. }
  2281. }
  2282. }
  2283. private void dgvOutSourcingContent_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  2284. {
  2285. //物件宣告
  2286. string strUnitPrice = "";
  2287. string strQuantity = "";
  2288. //Cell[0] : 項次(cOutOrderItem)
  2289. //Cell[1] : 委外項目(cOutOrderContent)
  2290. //Cell[2] : 委外單價(cOutUnitPrice)
  2291. //Cell[3] : 委外數量(cOutQuantity)
  2292. //Cell[4] : 委外小計(cOutAmount)
  2293. if (dgvOutSourcingContent.Columns[e.ColumnIndex].Name == "cOutUnitPrice")
  2294. {
  2295. strUnitPrice = (string)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutUnitPrice"].Value;
  2296. if (string.IsNullOrEmpty(strUnitPrice))
  2297. {
  2298. return;
  2299. }
  2300. else
  2301. {
  2302. dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutUnitPrice"].Value = strUnitPrice.Replace(",", "");
  2303. }
  2304. }
  2305. if (dgvOutSourcingContent.Columns[e.ColumnIndex].Name == "cOutQuantity")
  2306. {
  2307. strQuantity = (string)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutQuantity"].Value;
  2308. if (string.IsNullOrEmpty(strQuantity))
  2309. {
  2310. return;
  2311. }
  2312. else
  2313. {
  2314. dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutQuantity"].Value = strQuantity.Replace(",", "");
  2315. }
  2316. }
  2317. }
  2318. private void dgvOutSourcingContent_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  2319. {
  2320. //物件宣告
  2321. string strOutUnitPrice = "";
  2322. string strOutQuantity = "";
  2323. //Cell[0] : 項次(cOutOrderItem)
  2324. //Cell[1] : 委外項目(cOutOrderContent)
  2325. //Cell[2] : 委外單價(cOutUnitPrice)
  2326. //Cell[3] : 委外數量(cOutQuantity)
  2327. //Cell[4] : 委外總計(cOutAmount)
  2328. //加上三位一撇效果
  2329. if (dgvOutSourcingContent.Columns[e.ColumnIndex].Name == "cOutUnitPrice")
  2330. {
  2331. strOutUnitPrice = (string)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutUnitPrice"].Value;
  2332. if (string.IsNullOrEmpty(strOutUnitPrice))
  2333. {
  2334. return;
  2335. }
  2336. else
  2337. {
  2338. dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutUnitPrice"].Value = UtilityClass.MarkNumber(strOutUnitPrice);
  2339. }
  2340. }
  2341. if (dgvOutSourcingContent.Columns[e.ColumnIndex].Name == "cOutQuantity")
  2342. {
  2343. strOutQuantity = (string)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutQuantity"].Value;
  2344. if (string.IsNullOrEmpty(strOutQuantity))
  2345. {
  2346. return;
  2347. }
  2348. else
  2349. {
  2350. dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutQuantity"].Value = UtilityClass.MarkNumber(strOutQuantity, 2);
  2351. }
  2352. }
  2353. //數量變動
  2354. if (dgvOutSourcingContent.Columns[e.ColumnIndex].Name.ToString() == "cOutQuantity" | dgvOutSourcingContent.Columns[e.ColumnIndex].Name.ToString() == "cOutUnitPrice")
  2355. {
  2356. strOutUnitPrice = (string)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutUnitPrice"].Value;
  2357. strOutQuantity = (string)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutQuantity"].Value;
  2358. if (!string.IsNullOrEmpty(strOutUnitPrice) & !string.IsNullOrEmpty(strOutQuantity))
  2359. {
  2360. if (strOutQuantity.IndexOf("%") == -1)
  2361. {
  2362. dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutAmount"].Value = UtilityClass.MarkNumber(Convert.ToDouble(strOutUnitPrice.Replace(",", "")) * Convert.ToDouble(strOutQuantity.Replace(",", "")));
  2363. }
  2364. else
  2365. {
  2366. dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutAmount"].Value = UtilityClass.MarkNumber(Convert.ToDouble(strOutUnitPrice.Replace(",", "")) * Convert.ToDouble(strOutQuantity.Replace("%", "")) / 100);
  2367. }
  2368. }
  2369. }
  2370. CountOutSourcingSum(); //重新計算委外加總
  2371. }
  2372. private void dgvOutSourcingContent_RowEnter(object sender, DataGridViewCellEventArgs e)
  2373. {
  2374. dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutOrderItem"].Value = (e.RowIndex + 1).ToString();
  2375. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutOrderContent"];
  2376. if (cbCell.Items.Count == 0)
  2377. {
  2378. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2379. cbCell.DisplayMember = "ArgumentValue";
  2380. cbCell.ValueMember = "ArgumentID";
  2381. }
  2382. }
  2383. private void dgvOutSourcingContent_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
  2384. {
  2385. //設定項次
  2386. string strOrderItem = (string)dgvOutSourcingContent.Rows[e.RowIndex].Cells[0].Value;
  2387. if (!string.IsNullOrEmpty(strOrderItem))
  2388. {
  2389. string strSQL = "Select OrderContent From OTB_SAL_OutSourcingContent Where SalesOrderNo='" + txtSalesOrderNo.Text.Trim() + "' And ItemNo = '" + strOrderItem + "'";
  2390. string strPayContent = UtilityClass.GetSQLResult(strSQL).Tables["Result"].Rows[0]["OrderContent"].ToString();
  2391. //設定採購項目
  2392. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutOrderContent"];
  2393. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2394. cbCell.DisplayMember = "ArgumentValue";
  2395. cbCell.ValueMember = "ArgumentID";
  2396. cbCell.Value = strPayContent;
  2397. }
  2398. }
  2399. private void dgvOutSourcingContent_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  2400. {
  2401. string strErrorMsg = "";
  2402. int intMsgCount = 0;
  2403. try
  2404. {
  2405. //進行資料檢查
  2406. DataGridViewComboBoxCell cbCheck = (DataGridViewComboBoxCell)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutOrderContent"];
  2407. if (!string.IsNullOrEmpty((string)cbCheck.Value))
  2408. {
  2409. if (string.IsNullOrEmpty((string)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutUnitPrice"].Value))
  2410. {
  2411. strErrorMsg = "請輸入委外單價";
  2412. intMsgCount += 1;
  2413. }
  2414. if (string.IsNullOrEmpty((string)dgvOutSourcingContent.Rows[e.RowIndex].Cells["cOutQuantity"].Value))
  2415. {
  2416. strErrorMsg = "請輸入委外數量";
  2417. intMsgCount += 1;
  2418. }
  2419. if (strErrorMsg != "" && intMsgCount < 2)
  2420. {
  2421. dgvOutSourcingContent.Rows[e.RowIndex].ErrorText = strErrorMsg;
  2422. e.Cancel = true;
  2423. }
  2424. }
  2425. }
  2426. catch (Exception ex)
  2427. {
  2428. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  2429. }
  2430. }
  2431. private void dgvOutSourcingContent_RowValidated(object sender, DataGridViewCellEventArgs e)
  2432. {
  2433. dgvOutSourcingContent.Rows[e.RowIndex].ErrorText = "";
  2434. }
  2435. private void dgvReceiptPlan_RowEnter(object sender, DataGridViewCellEventArgs e)
  2436. {
  2437. dgvReceiptPlan.Rows[e.RowIndex].Cells["cPhase"].Value = (e.RowIndex + 1).ToString();
  2438. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptItem"];
  2439. if (cbCell.Items.Count == 0)
  2440. {
  2441. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2442. cbCell.DisplayMember = "ArgumentValue";
  2443. cbCell.ValueMember = "ArgumentID";
  2444. }
  2445. }
  2446. private void dgvReceiptPlan_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  2447. {
  2448. //項次(cItemNo)
  2449. //期數(cPhase)
  2450. //請款單號(cReceiptNo)
  2451. //請款項目(cReceiptItem)
  2452. //請款數量(cReceiptQuantity)
  2453. //請款餘量(cRestReceipt)
  2454. //預計請款日期(cPrePayDate)
  2455. //實際請款日期(cActualReceiptDate)
  2456. //預計付款日期(cExpectedReceiptedDate)
  2457. //實際付款日期(cActualReceiptedDate)
  2458. //請款金額(cReceiptAmount)
  2459. //餘額(cRestReceiptAmount)
  2460. //備註(cMemo)
  2461. string strErrorMsg = "";
  2462. string strExpectedReceiptDate = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cExpectedReceiptDate"].Value;
  2463. string strExpectedReceiptedDate = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cExpectedReceiptedDate"].Value;
  2464. string strReceiptNo = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptNo"].Value;
  2465. string strItemNo = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cItemNo"].Value;
  2466. string strReceiptAmount = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptAmount"].Value;
  2467. string strActualReceiptDate = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cActualReceiptDate"].Value;
  2468. string strActualReceiptedDate = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cActualReceiptedDate"].Value;
  2469. string strReceiptQuantity = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptQuantity"].Value;
  2470. try
  2471. {
  2472. //標準資料檢查
  2473. if (!dgvReceiptPlan.Rows[e.RowIndex].IsNewRow)
  2474. {
  2475. DataGridViewComboBoxCell cbCheck = (DataGridViewComboBoxCell)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptItem"];
  2476. if (string.IsNullOrEmpty((string)cbCheck.Value))
  2477. {
  2478. strErrorMsg = "請選擇請款項目";
  2479. }
  2480. if (!string.IsNullOrEmpty(strItemNo))
  2481. {
  2482. if (string.IsNullOrEmpty(strItemNo))
  2483. {
  2484. strErrorMsg = "請款項次為必填項目!";
  2485. }
  2486. }
  2487. if (!string.IsNullOrEmpty(strExpectedReceiptDate))
  2488. {
  2489. if (!UtilityClass.IsDate(strExpectedReceiptDate))
  2490. {
  2491. strErrorMsg = "預計請款日期格式錯誤(應為:yyyy/mm/dd)!";
  2492. }
  2493. }
  2494. if (!string.IsNullOrEmpty(strExpectedReceiptedDate))
  2495. {
  2496. if (!UtilityClass.IsDate(strExpectedReceiptedDate))
  2497. {
  2498. strErrorMsg = "預計收款日期格式錯誤(應為:yyyy/mm/dd)!";
  2499. }
  2500. if (DateTime.Compare(Convert.ToDateTime(strExpectedReceiptDate + " 00:00:00"), Convert.ToDateTime(strExpectedReceiptedDate + " 00:00:00")) > 0)
  2501. {
  2502. strErrorMsg = "預計收款日期應大於等於預計請款日期!";
  2503. }
  2504. }
  2505. }
  2506. //進行資料檢查
  2507. if (!string.IsNullOrEmpty(strReceiptNo))
  2508. {
  2509. if (string.IsNullOrEmpty(strReceiptQuantity))
  2510. {
  2511. strErrorMsg = "請輸入請款數量";
  2512. }
  2513. else
  2514. {
  2515. strReceiptQuantity = strReceiptQuantity.Replace(",", "");
  2516. if (strReceiptQuantity.IndexOf("%") == -1)
  2517. {
  2518. if (!UtilityClass.IsNumber(strReceiptQuantity))
  2519. {
  2520. strErrorMsg = "請款數量,數字格式有誤!";
  2521. }
  2522. }
  2523. }
  2524. if (!string.IsNullOrEmpty(strReceiptAmount))
  2525. {
  2526. strReceiptAmount = strReceiptAmount.Replace(",", "");
  2527. if (!UtilityClass.IsNumber(strReceiptAmount))
  2528. {
  2529. strErrorMsg = "請款金額,數字格式有誤!";
  2530. }
  2531. }
  2532. if (!string.IsNullOrEmpty(strActualReceiptDate))
  2533. {
  2534. if (!UtilityClass.IsDate(strActualReceiptDate))
  2535. {
  2536. strErrorMsg = "實際請款日期格式錯誤(應為:yyyy/mm/dd)!";
  2537. }
  2538. }
  2539. if (!string.IsNullOrEmpty(strActualReceiptedDate))
  2540. {
  2541. if (!UtilityClass.IsDate(strActualReceiptedDate))
  2542. {
  2543. strErrorMsg = "實際收款日期格式錯誤(應為:yyyy/mm/dd)!";
  2544. }
  2545. if (DateTime.Compare(Convert.ToDateTime(strActualReceiptDate + " 00:00:00"), Convert.ToDateTime(strActualReceiptedDate + " 00:00:00")) > 0)
  2546. {
  2547. strErrorMsg = "實際收款日期應大於等於實際請款日期!";
  2548. }
  2549. }
  2550. }
  2551. if (strErrorMsg != "")
  2552. {
  2553. dgvReceiptPlan.Rows[e.RowIndex].ErrorText = strErrorMsg;
  2554. e.Cancel = true;
  2555. }
  2556. }
  2557. catch (Exception ex)
  2558. {
  2559. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  2560. }
  2561. }
  2562. private void dgvReceiptPlan_RowValidated(object sender, DataGridViewCellEventArgs e)
  2563. {
  2564. ReArrangeReceipt(e);
  2565. dgvReceiptPlan.Rows[e.RowIndex].ErrorText = "";
  2566. }
  2567. private void dgvReceiptPlan_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
  2568. {
  2569. //設定項次
  2570. string strItemNo = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cItemNo"].Value;
  2571. string strPhase = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cPhase"].Value;
  2572. string strReceiptNo = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptNo"].Value;
  2573. if (!string.IsNullOrEmpty(strPhase))
  2574. {
  2575. string strSQL = "Select ReceiptItem From OTB_SAL_ReceiptPlan Where SalesOrderNo='" + txtSalesOrderNo.Text.Trim() + "' And ItemNo = '" + strItemNo + "' And Phase = '" + strPhase + "'";
  2576. using(DataTable dtTemp =UtilityClass.GetSQLResult(strSQL).Tables["Result"])
  2577. {
  2578. if(dtTemp.Rows.Count >0)
  2579. {
  2580. string strReceiptItem = dtTemp.Rows[0]["ReceiptItem"].ToString();
  2581. //設定採購項目
  2582. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptItem"];
  2583. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2584. cbCell.DisplayMember = "ArgumentValue";
  2585. cbCell.ValueMember = "ArgumentID";
  2586. cbCell.Value = strReceiptItem;
  2587. }
  2588. }
  2589. }
  2590. if (!string.IsNullOrEmpty(strReceiptNo)) //判斷條件,設定該Row不可編輯
  2591. {
  2592. dgvReceiptPlan.Rows[e.RowIndex].ReadOnly = true;
  2593. }
  2594. }
  2595. private void tsbSetup_Click(object sender, EventArgs e)
  2596. {
  2597. PayPlanManagementSetup frmSettingForm = new PayPlanManagementSetup();
  2598. frmSettingForm.Owner = this;
  2599. frmSettingForm.strOwnerForm = this.Name.ToString();
  2600. frmSettingForm.strXMLName = this.Name.ToString();
  2601. frmSettingForm.StartPosition = FormStartPosition.CenterParent;
  2602. frmSettingForm.ShowDialog();
  2603. }
  2604. private void dgvReceiptPlan_CellContentClick(object sender, DataGridViewCellEventArgs e)
  2605. {
  2606. //物件宣告
  2607. string strDC = "";
  2608. string strReceipt = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptNo"].Value;
  2609. string strReceiptAmount = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptAmount"].Value;
  2610. try
  2611. {
  2612. //當GridView中的Button被按下
  2613. var SenderGrid = (DataGridView)sender;
  2614. if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
  2615. {
  2616. if (string.IsNullOrEmpty(strReceipt))
  2617. {
  2618. MessageBox.Show("請輸入發票號碼!", "提示");
  2619. return;
  2620. }
  2621. if (SenderGrid.Rows[e.RowIndex].ReadOnly == true) //判定唯讀則不處理
  2622. {
  2623. return;
  2624. }
  2625. //Form設定
  2626. AccountingEntries acItemForm = new AccountingEntries();
  2627. acItemForm.strOwnerForm = "PayPlanManagement_Receive";
  2628. acItemForm.Owner = this;
  2629. acItemForm.StartPosition = FormStartPosition.CenterParent;
  2630. acItemForm.strFrmStatus = "ADD";
  2631. acItemForm.txtProjectName.Text = txtProjectName.Text;
  2632. acItemForm.txtProjectNumber.Text = txtProjectNumber.Text;
  2633. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + "_Receipt.xml";
  2634. if (File.Exists(strXMLPath))
  2635. {
  2636. XDocument xmlContent = XDocument.Load(strXMLPath);
  2637. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  2638. {
  2639. DataGridViewRow dgvRow = new DataGridViewRow();
  2640. dgvRow.CreateCells(acItemForm.dgvDataMaintain);
  2641. strDC = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString();
  2642. switch (strDC)
  2643. {
  2644. case "D":
  2645. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  2646. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  2647. dgvRow.Cells[3].Value = "發票號碼:" + strReceipt;
  2648. dgvRow.Cells[4].Value = strReceiptAmount;
  2649. break;
  2650. case "C":
  2651. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  2652. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  2653. dgvRow.Cells[3].Value = "發票號碼:" + strReceipt;
  2654. dgvRow.Cells[5].Value = strReceiptAmount;
  2655. break;
  2656. }
  2657. blIsAdded = false;
  2658. if (string.IsNullOrEmpty((string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cActualReceiptDate"].Value))
  2659. {
  2660. dgvReceiptPlan.Rows[e.RowIndex].Cells["cActualReceiptDate"].Value = DateTime.Now.ToString("yyyy/MM/dd");
  2661. }
  2662. acItemForm.dgvDataMaintain.Rows.Add(dgvRow);
  2663. }
  2664. acItemForm.dgeArgs = e;
  2665. }
  2666. acItemForm.ShowDialog();
  2667. }
  2668. }
  2669. catch (Exception ex)
  2670. {
  2671. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  2672. }
  2673. }
  2674. private void dgvReceiptPlan_CellEnter(object sender, DataGridViewCellEventArgs e)
  2675. {
  2676. try
  2677. {
  2678. ReArrangeReceipt(e);
  2679. }
  2680. catch (Exception ex)
  2681. {
  2682. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  2683. }
  2684. }
  2685. private void dgvReceiptPlan_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  2686. {
  2687. //宣告物件
  2688. string strReceiptUnitPrice = ""; //請款單價
  2689. string strReceiptQuantity = ""; //請款數量
  2690. string strReceiptNo = ""; //發票號碼
  2691. //發票號碼變大寫
  2692. if (dgvReceiptPlan.Columns[e.ColumnIndex].Name == "cReceiptNo")
  2693. {
  2694. strReceiptNo = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptNo"].Value;
  2695. dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptNo"].Value = string.IsNullOrEmpty(strReceiptNo) ? "" : strReceiptNo.ToUpper();
  2696. }
  2697. //加上三位一撇效果
  2698. if (dgvReceiptPlan.Columns[e.ColumnIndex].Name == "cReceiptUnitPrice")
  2699. {
  2700. strReceiptUnitPrice = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptUnitPrice"].Value;
  2701. if (string.IsNullOrEmpty(strReceiptUnitPrice))
  2702. {
  2703. return;
  2704. }
  2705. else
  2706. {
  2707. dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptUnitPrice"].Value = UtilityClass.MarkNumber(strReceiptUnitPrice);
  2708. }
  2709. }
  2710. if (dgvReceiptPlan.Columns[e.ColumnIndex].Name == "cReceiptQuantity")
  2711. {
  2712. //回傳處理後的數量
  2713. strReceiptQuantity = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptQuantity"].Value;
  2714. if (string.IsNullOrEmpty(strReceiptQuantity))
  2715. {
  2716. return;
  2717. }
  2718. else
  2719. {
  2720. dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptQuantity"].Value = UtilityClass.MarkNumber(strReceiptQuantity, 2);
  2721. }
  2722. }
  2723. //輸入值為請款金額回推試算
  2724. //if (dgvReceiptPlan.Columns[e.ColumnIndex].Name == "cReceiptAmount")
  2725. //{
  2726. // string strUnitPrice = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptUnitPrice"].Value;
  2727. // if (!string.IsNullOrEmpty(strUnitPrice))
  2728. // {
  2729. // string strReceiptAmount = (string)dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptAmount"].Value;
  2730. // double dbRecmiptAmount = Convert.ToDouble(strReceiptAmount.Replace(",",""));
  2731. // dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptTax"].Value = UtilityClass.MarkNumber(dbRecmiptAmount / 1.05 * 0.05); //計算稅額
  2732. // dgvReceiptPlan.Rows[e.RowIndex].Cells["cReceiptQuantity"].Value = UtilityClass.MarkNumber(dbRecmiptAmount / Convert.ToDouble(strUnitPrice.Replace(",",""))); //計算數量
  2733. // }
  2734. //}
  2735. }
  2736. private void btnArrangToPayProject_Click(object sender, EventArgs e)
  2737. {
  2738. string strPayType = "";
  2739. int intPayPlanItem = 1;
  2740. string strInvoiceNo = ""; //付款單號
  2741. string strPurchaseOrderSum = "0"; //採購單總金額
  2742. string strPayAmount = "0"; //付款金額
  2743. string strPayBalance = "0"; //餘額
  2744. string strQuantityStyle = ""; //數量呈現模式(P:百分比、D:三位一撇)
  2745. string strPayQuantityAmount = "0"; //採購單總量
  2746. string strPayUnitPrice = "0"; //付款單價
  2747. string strPayQuantity = "0"; //付款數量
  2748. string strRestPayQuantity = "0"; //付款餘量
  2749. bool blFound = true; //允許建立
  2750. if (MessageBox.Show("請問您是否確認要產生付款計劃?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  2751. {
  2752. foreach (DataGridViewRow drData in dgvToPayPlan.Rows)
  2753. {
  2754. strInvoiceNo = (string)drData.Cells["cInvoiceNo"].Value;
  2755. if (!string.IsNullOrEmpty(strInvoiceNo))
  2756. {
  2757. MessageBox.Show("目前已有付款計劃,無法重新產生付款計劃!", "提示");
  2758. return;
  2759. }
  2760. }
  2761. dgvToPayPlan.Rows.Clear(); //重新產生,先清除舊資料
  2762. //取得資訊
  2763. if (rbPayType.Checked == true)
  2764. {
  2765. strPayType = "P"; //專案類別
  2766. foreach (DataGridViewRow drData in dgvOutSourcingContent.Rows) //數量加總
  2767. {
  2768. strPayQuantity = (string)drData.Cells["cOutQuantity"].Value;
  2769. if (!string.IsNullOrEmpty(strPayQuantity))
  2770. {
  2771. if (strPayQuantity.IndexOf("%") != -1)
  2772. {
  2773. strPayQuantityAmount = ((string.IsNullOrEmpty(strPayQuantity) ? 0 : Convert.ToDouble(strPayQuantityAmount)) + Convert.ToDouble(strPayQuantity.Replace("%", ""))).ToString();
  2774. strQuantityStyle = "P";
  2775. }
  2776. else
  2777. {
  2778. strPayQuantityAmount = ((string.IsNullOrEmpty(strPayQuantity) ? 0 : Convert.ToDouble(strPayQuantityAmount)) + Convert.ToDouble(strPayQuantity.Replace(",", ""))).ToString();
  2779. strQuantityStyle = "D";
  2780. }
  2781. }
  2782. strPayAmount = (string)drData.Cells["cOutAmount"].Value; //小計加總
  2783. strPurchaseOrderSum = (Convert.ToDouble(strPurchaseOrderSum) + (string.IsNullOrEmpty(strPayAmount) ? 0 : Convert.ToDouble(strPayAmount.Replace(",", "")))).ToString();
  2784. }
  2785. switch (strQuantityStyle)
  2786. {
  2787. case "P":
  2788. strPayQuantityAmount += "%";
  2789. break;
  2790. case "D":
  2791. strPayQuantityAmount = UtilityClass.MarkNumber(strPayQuantityAmount, 2);
  2792. break;
  2793. }
  2794. }
  2795. if (rbPayType1.Checked == true)
  2796. {
  2797. strPayType = "M"; //人月類別
  2798. foreach (DataGridViewRow drData in dgvOutSourcingContent.Rows)
  2799. {
  2800. strPayAmount = (string)drData.Cells["cOutAmount"].Value;
  2801. strPurchaseOrderSum = (Convert.ToDouble(strPurchaseOrderSum) + (string.IsNullOrEmpty(strPayAmount) ? 0 : Convert.ToDouble(strPayAmount.Replace(",", "")))).ToString();
  2802. }
  2803. }
  2804. if (rbPayType2.Checked == true)
  2805. {
  2806. strPayType = "D";
  2807. }
  2808. if (rbPayType3.Checked == true)
  2809. {
  2810. strPayType = "H";
  2811. }
  2812. //資料處理
  2813. switch (strPayType)
  2814. {
  2815. case "P":
  2816. strPayBalance = strPurchaseOrderSum; //採購總金額
  2817. strRestPayQuantity = strPayQuantityAmount; //採購總數量
  2818. foreach (DataGridViewRow drData in dgvOutSourcingContent.Rows)
  2819. {
  2820. DataGridViewComboBoxCell cbCHeck = (DataGridViewComboBoxCell)drData.Cells["cOutOrderContent"];
  2821. if (!string.IsNullOrEmpty((string)cbCHeck.Value))
  2822. {
  2823. DataGridViewRow dgvRow = new DataGridViewRow();
  2824. dgvRow.CreateCells(dgvToPayPlan);
  2825. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvRow.Cells[1];
  2826. if (cbCell.Items.Count == 0)
  2827. {
  2828. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2829. cbCell.DisplayMember = "ArgumentValue";
  2830. cbCell.ValueMember = "ArgumentID";
  2831. }
  2832. cbCell.Value = drData.Cells["cOutOrderContent"].Value; //付款項目
  2833. dgvRow.Cells[2].Value = drData.Cells["cOutOrderItem"].Value; //項次
  2834. dgvRow.Cells[3].Value = (intPayPlanItem++).ToString(); //期數
  2835. strPayUnitPrice = (string)drData.Cells["cOutUnitPrice"].Value;
  2836. dgvRow.Cells[5].Value = string.IsNullOrEmpty(strPayUnitPrice) ? "" : strPayUnitPrice; //付款單價
  2837. strPayQuantity = (string)drData.Cells["cOutQuantity"].Value;
  2838. dgvRow.Cells[6].Value = string.IsNullOrEmpty(strPayQuantity) ? "" : strPayQuantity; //付款數量
  2839. if (strRestPayQuantity.IndexOf("%") != -1) //付款餘量
  2840. {
  2841. strRestPayQuantity = ((string.IsNullOrEmpty(strRestPayQuantity) ? 0 : Convert.ToDouble(strRestPayQuantity.Replace("%", ""))) - (string.IsNullOrEmpty(strPayQuantity) ? 0 : Convert.ToDouble(strPayQuantity.Replace("%", "")))).ToString() + "%";
  2842. dgvRow.Cells[7].Value = strRestPayQuantity;
  2843. }
  2844. else if (strRestPayQuantity.IndexOf(",") != -1)
  2845. {
  2846. strRestPayQuantity = ((string.IsNullOrEmpty(strRestPayQuantity) ? 0 : Convert.ToDouble(strRestPayQuantity.Replace(",", ""))) - (string.IsNullOrEmpty(strPayQuantity) ? 0 : Convert.ToDouble(strPayQuantity.Replace(",", "")))).ToString();
  2847. dgvRow.Cells[7].Value = UtilityClass.MarkNumber(strRestPayQuantity, 2);
  2848. }
  2849. strPayAmount = (string)drData.Cells["cOutAmount"].Value; //付款金額
  2850. dgvRow.Cells[10].Value = string.IsNullOrEmpty(strPayAmount) ? "" : strPayAmount;
  2851. strPayBalance = ((string.IsNullOrEmpty(strPayBalance) ? 0 : Convert.ToDouble(strPayBalance.Replace(",", ""))) - (string.IsNullOrEmpty(strPayAmount) ? 0 : Convert.ToDouble(strPayAmount.Replace(",", "")))).ToString();
  2852. dgvRow.Cells[11].Value = UtilityClass.MarkNumber(strPayBalance); //待付餘額
  2853. dgvToPayPlan.Rows.Add(dgvRow);
  2854. }
  2855. }
  2856. break;
  2857. case "M":
  2858. dgvToPayPlan.Rows.Clear(); //重新產生計劃
  2859. double dbQuantity = 0;
  2860. double dbRestPayQuantity = 0;
  2861. double dbBalance = 0;
  2862. double dbPayQuantity = 0;
  2863. double dbPayAmount = 0;
  2864. //確認每個月付款數量
  2865. InputDialog frmInput = new InputDialog();
  2866. frmInput.lbQuestionMessage.Text = "請輸入每月付款人力數!";
  2867. frmInput.txtInput.Text = "1";
  2868. frmInput.StartPosition = FormStartPosition.CenterParent;
  2869. DialogResult dr = frmInput.ShowDialog();
  2870. if (dr == DialogResult.OK)
  2871. {
  2872. dbPayQuantity = Convert.ToDouble(frmInput.GetMsg());
  2873. }
  2874. foreach (DataGridViewRow drData in dgvOutSourcingContent.Rows)
  2875. {
  2876. dbQuantity = 0;
  2877. strPayQuantity = (string)drData.Cells["cOutQuantity"].Value; //數量
  2878. dbRestPayQuantity = Convert.ToDouble(string.IsNullOrEmpty(strPayQuantity) ? 0 : Convert.ToDouble(strPayQuantity.Replace(",", "")));
  2879. dbPayAmount = Convert.ToDouble(drData.Cells["cOutAmount"].Value);
  2880. dbBalance = Convert.ToDouble(string.IsNullOrEmpty(dbPayAmount.ToString()) ? 0 : dbPayAmount);
  2881. DataGridViewComboBoxCell cbCHeck = (DataGridViewComboBoxCell)drData.Cells["cOutOrderContent"];
  2882. blFound = false;
  2883. if (!drData.IsNewRow)
  2884. {
  2885. dbQuantity = Convert.ToDouble(drData.Cells["cOutQuantity"].Value.ToString());
  2886. for (double dbCount = 1; dbCount <= dbQuantity; dbCount++)
  2887. {
  2888. DataGridViewRow dgvRow = new DataGridViewRow();
  2889. dgvRow.CreateCells(dgvToPayPlan);
  2890. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvRow.Cells[1];
  2891. if (cbCell.Items.Count == 0)
  2892. {
  2893. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  2894. cbCell.DisplayMember = "ArgumentValue";
  2895. cbCell.ValueMember = "ArgumentID";
  2896. }
  2897. cbCell.Value = cbCHeck.Value; //付款項目
  2898. dgvRow.Cells[2].Value = drData.Cells["cOutOrderItem"].Value; //項次
  2899. dgvRow.Cells[3].Value = (intPayPlanItem++).ToString(); //期數
  2900. strPayUnitPrice = (string)drData.Cells["cOutUnitPrice"].Value;
  2901. dgvRow.Cells[5].Value = string.IsNullOrEmpty(strPayUnitPrice) ? "" : UtilityClass.MarkNumber(strPayUnitPrice); //付款單價
  2902. if (dbRestPayQuantity >= dbPayQuantity)
  2903. {
  2904. dgvRow.Cells[6].Value = UtilityClass.MarkNumber(dbPayQuantity, 2); //付款數量
  2905. dbRestPayQuantity = dbRestPayQuantity - dbPayQuantity;
  2906. dgvRow.Cells[7].Value = UtilityClass.MarkNumber(dbRestPayQuantity, 2); //付款餘量
  2907. }
  2908. else
  2909. {
  2910. dgvRow.Cells[6].Value = UtilityClass.MarkNumber(dbRestPayQuantity, 2); //付款數量
  2911. dbRestPayQuantity = dbRestPayQuantity - dbPayQuantity;
  2912. if (dbRestPayQuantity < 0) dgvRow.Cells[7].Value = UtilityClass.MarkNumber(0, 2); //付款餘量
  2913. }
  2914. dbPayAmount = Convert.ToDouble(drData.Cells["cOutUnitPrice"].Value) * Convert.ToDouble(dgvRow.Cells[6].Value);
  2915. dgvRow.Cells[10].Value = string.IsNullOrEmpty(dbPayAmount.ToString()) ? "0" : UtilityClass.MarkNumber(dbPayAmount); //付款金額
  2916. dbBalance = dbBalance - dbPayAmount;
  2917. dgvRow.Cells[11].Value = UtilityClass.MarkNumber(dbBalance); //付款餘額
  2918. if (Convert.ToDouble(dgvRow.Cells[6].Value) > 0)
  2919. {
  2920. dgvToPayPlan.Rows.Add(dgvRow);
  2921. }
  2922. }
  2923. }
  2924. }
  2925. break;
  2926. case "D":
  2927. MessageBox.Show("人日無法產生付款計劃!", "提示");
  2928. break;
  2929. case "H":
  2930. MessageBox.Show("人時無法產生付款計劃!", "提示");
  2931. break;
  2932. }
  2933. }
  2934. }
  2935. private void dgvToPayPlan_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  2936. {
  2937. //宣告物件
  2938. string strPayUnitPrice = ""; //請款單價
  2939. string strPayQuantity = ""; //請款數量
  2940. string strInvoiceNo = ""; //發票號碼
  2941. //發票號碼變大寫
  2942. if (dgvToPayPlan.Columns[e.ColumnIndex].Name == "cInvoiceNo")
  2943. {
  2944. strInvoiceNo = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  2945. dgvToPayPlan.Rows[e.RowIndex].Cells["cInvoiceNo"].Value = strInvoiceNo.ToUpper();
  2946. }
  2947. //加上三位一撇效果
  2948. if (dgvToPayPlan.Columns[e.ColumnIndex].Name == "cPayUnitPrice")
  2949. {
  2950. strPayUnitPrice = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayUnitPrice"].Value;
  2951. if (string.IsNullOrEmpty(strPayUnitPrice))
  2952. {
  2953. return;
  2954. }
  2955. else
  2956. {
  2957. dgvToPayPlan.Rows[e.RowIndex].Cells["cPayUnitPrice"].Value = UtilityClass.MarkNumber(strPayUnitPrice);
  2958. }
  2959. }
  2960. if (dgvToPayPlan.Columns[e.ColumnIndex].Name == "cPayQuantity")
  2961. {
  2962. strPayQuantity = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayQuantity"].Value;
  2963. if (string.IsNullOrEmpty(strPayQuantity))
  2964. {
  2965. return;
  2966. }
  2967. else
  2968. {
  2969. dgvToPayPlan.Rows[e.RowIndex].Cells["cPayQuantity"].Value = UtilityClass.MarkNumber(strPayQuantity, 2);
  2970. }
  2971. }
  2972. }
  2973. private void dgvToPayPlan_CellEnter(object sender, DataGridViewCellEventArgs e)
  2974. {
  2975. try
  2976. {
  2977. ReArrangePay(e);
  2978. }
  2979. catch (Exception ex)
  2980. {
  2981. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  2982. }
  2983. }
  2984. private void dgvToPayPlan_RowValidated(object sender, DataGridViewCellEventArgs e)
  2985. {
  2986. ReArrangePay(e);
  2987. dgvToPayPlan.Rows[e.RowIndex].ErrorText = "";
  2988. }
  2989. private void dgvToPayPlan_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
  2990. {
  2991. //設定項次
  2992. string strItemNo = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayItemNo"].Value;
  2993. string strPayPhase = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayPhase"].Value;
  2994. string strInvoiceNo = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  2995. if (!string.IsNullOrEmpty(strPayPhase))
  2996. {
  2997. string strSQL = "Select PayItem From OTB_SAL_PayPlan Where SalesOrderNo='" + txtSalesOrderNo.Text.Trim() + "' And ItemNo = '" + strItemNo + "' And PhaseNo = '" + strPayPhase + "'";
  2998. using (DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"])
  2999. {
  3000. if (dtTemp.Rows.Count > 0)
  3001. {
  3002. string strPayItem = dtTemp.Rows[0]["PayItem"].ToString();
  3003. //設定採購項目
  3004. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayItem"];
  3005. cbCell.DataSource = UtilityClass.GetSystemArgument("PayItem", "zh-TW").Tables["Arguments"];
  3006. cbCell.DisplayMember = "ArgumentValue";
  3007. cbCell.ValueMember = "ArgumentID";
  3008. cbCell.Value = strPayItem;
  3009. }
  3010. }
  3011. }
  3012. if (!string.IsNullOrEmpty(strInvoiceNo)) //判斷條件,設定該Row不可編輯
  3013. {
  3014. dgvToPayPlan.Rows[e.RowIndex].ReadOnly = true;
  3015. }
  3016. }
  3017. private void dgvToPayPlan_CellContentClick(object sender, DataGridViewCellEventArgs e)
  3018. {
  3019. //物件宣告
  3020. string strDC = "";
  3021. string strInvoiceNo = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  3022. string strPayAmount = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayAmount"].Value;
  3023. try
  3024. {
  3025. //當GridView中的Button被按下
  3026. var SenderGrid = (DataGridView)sender;
  3027. if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
  3028. {
  3029. if (string.IsNullOrEmpty(strInvoiceNo))
  3030. {
  3031. MessageBox.Show("請輸入發票號碼!", "提示");
  3032. return;
  3033. }
  3034. if (SenderGrid.Rows[e.RowIndex].ReadOnly == true) //判定唯讀則不處理
  3035. {
  3036. return;
  3037. }
  3038. //Form設定
  3039. AccountingEntries acItemForm = new AccountingEntries();
  3040. acItemForm.strOwnerForm = "PayPlanManagement_Pay";
  3041. acItemForm.Owner = this;
  3042. acItemForm.StartPosition = FormStartPosition.CenterParent;
  3043. acItemForm.strFrmStatus = "ADD";
  3044. acItemForm.txtProjectName.Text = txtProjectName.Text;
  3045. acItemForm.txtProjectNumber.Text = txtProjectNumber.Text;
  3046. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + "_Pay.xml";
  3047. if (File.Exists(strXMLPath))
  3048. {
  3049. XDocument xmlContent = XDocument.Load(strXMLPath);
  3050. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  3051. {
  3052. DataGridViewRow dgvRow = new DataGridViewRow();
  3053. dgvRow.CreateCells(acItemForm.dgvDataMaintain);
  3054. strDC = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString();
  3055. switch (strDC)
  3056. {
  3057. case "D":
  3058. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  3059. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  3060. dgvRow.Cells[4].Value = strPayAmount;
  3061. break;
  3062. case "C":
  3063. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  3064. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  3065. dgvRow.Cells[3].Value = "發票號碼:" + strInvoiceNo;
  3066. dgvRow.Cells[5].Value = strPayAmount;
  3067. break;
  3068. }
  3069. blIsAdded = false;
  3070. if (string.IsNullOrEmpty((string)dgvToPayPlan.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value))
  3071. {
  3072. dgvToPayPlan.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value = DateTime.Now.ToString("yyyy/MM/dd");
  3073. }
  3074. acItemForm.dgvDataMaintain.Rows.Add(dgvRow);
  3075. }
  3076. acItemForm.dgeArgs = e;
  3077. }
  3078. acItemForm.ShowDialog();
  3079. }
  3080. }
  3081. catch (Exception ex)
  3082. {
  3083. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  3084. }
  3085. }
  3086. private void dgvToPayPlan_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  3087. {
  3088. //期數(cPhase)
  3089. //發票號碼(cInvoiceNo)
  3090. //付款項目(cPayItem)
  3091. //付款數量(cReceiptQuantity)
  3092. //付款餘量(cRestReceipt)
  3093. //預計付款日期(cExpectedPayDate)
  3094. //實際付款日期(cActualPaiedDate)
  3095. //付款金額(cPayAmount)
  3096. //餘額(cRestPayAmount)
  3097. //備註(cPayMemo)
  3098. string strErrorMsg = "";
  3099. string strInvoiceNo = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cInvoiceNo"].Value;
  3100. string strPayAmount = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayAmount"].Value;
  3101. string strExpectedPayDate = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cExpectedPayDate"].Value;
  3102. string strActualPayDate = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value;
  3103. string strPayQuantity = (string)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayQuantity"].Value;
  3104. try
  3105. {
  3106. //標準資料檢查
  3107. if (!dgvToPayPlan.Rows[e.RowIndex].IsNewRow)
  3108. {
  3109. if (!string.IsNullOrEmpty(strExpectedPayDate))
  3110. {
  3111. if (!UtilityClass.IsDate(strExpectedPayDate))
  3112. {
  3113. strErrorMsg = "預計付款日期格式錯誤(應為:yyyy/mm/dd)!";
  3114. }
  3115. }
  3116. if (!string.IsNullOrEmpty(strActualPayDate))
  3117. {
  3118. if (!UtilityClass.IsDate(strActualPayDate))
  3119. {
  3120. strErrorMsg = "實計付款日期格式錯誤(應為:yyyy/mm/dd)!";
  3121. }
  3122. }
  3123. }
  3124. //進行資料檢查
  3125. if (!string.IsNullOrEmpty(strInvoiceNo))
  3126. {
  3127. DataGridViewComboBoxCell cbCheck = (DataGridViewComboBoxCell)dgvToPayPlan.Rows[e.RowIndex].Cells["cPayItem"];
  3128. if (string.IsNullOrEmpty((string)cbCheck.Value))
  3129. {
  3130. strErrorMsg = "請選擇付款項目";
  3131. }
  3132. if (string.IsNullOrEmpty(strPayQuantity))
  3133. {
  3134. strErrorMsg = "請輸入付款數量";
  3135. }
  3136. else
  3137. {
  3138. strPayQuantity = strPayQuantity.Replace(",", "");
  3139. if (strPayQuantity.IndexOf("%") == -1)
  3140. {
  3141. if (!UtilityClass.IsNumber(strPayQuantity))
  3142. {
  3143. strErrorMsg = "請款數量,數字格式有誤!";
  3144. }
  3145. }
  3146. }
  3147. if (!string.IsNullOrEmpty(strPayAmount))
  3148. {
  3149. strPayAmount = strPayAmount.Replace(",", "");
  3150. if (!UtilityClass.IsNumber(strPayAmount))
  3151. {
  3152. strErrorMsg = "請款金額,數字格式有誤!";
  3153. }
  3154. }
  3155. }
  3156. if (strErrorMsg != "")
  3157. {
  3158. dgvToPayPlan.Rows[e.RowIndex].ErrorText = strErrorMsg;
  3159. e.Cancel = true;
  3160. }
  3161. }
  3162. catch (Exception ex)
  3163. {
  3164. ErrorHandler.WriteErrorLog("PayPlanManagement.cs", ex);
  3165. }
  3166. }
  3167. private void txtSalesOrderNo_Leave(object sender, EventArgs e)
  3168. {
  3169. txtSalesOrderNo.Text = txtSalesOrderNo.Text.ToUpper();
  3170. }
  3171. #endregion
  3172. }
  3173. }