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.

996 lines
48 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using ManagementSystem.Utility;
  12. using ManagementSystem;
  13. namespace ManagementSystem
  14. {
  15. public partial class AccountingEntries : Form
  16. {
  17. //程式內共用物件
  18. public string strFrmStatus = ""; //表單狀態
  19. public string strPKey = ""; //程式內的Key值
  20. public string strOwnerForm = ""; //呼叫的Form Name
  21. public string strKey = ""; //程式內加密的Key值
  22. public string strActiveUserID = ""; //取得登入作用的使用者帳號
  23. public string strAccountingBookID = ""; //取得作用中的帳本
  24. public string strAccountingYear = ""; //取得分錄年份
  25. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  26. SqlTransaction sqlTran;
  27. SqlDataAdapter sqlAdapter = new SqlDataAdapter();
  28. SqlCommand sqlCmd = new SqlCommand();
  29. DataSet sdInsurance = new System.Data.DataSet();
  30. public DataGridViewCellEventArgs dgeArgs;
  31. public AccountingEntries()
  32. {
  33. InitializeComponent();
  34. }
  35. #region 自定義程式
  36. private void ReSum() //重新統計借貸方的總額
  37. {
  38. string strAddedDebit = "0";
  39. string strAddedCredit = "0";
  40. string strdDebit = "0";
  41. string strCredit = "0";
  42. foreach (DataGridViewRow drAccountingItem in dgvDataMaintain.Rows)
  43. {
  44. if (drAccountingItem.Cells["cDebit"].Value != null)
  45. {
  46. strdDebit = string.IsNullOrEmpty(drAccountingItem.Cells["cDebit"].Value.ToString()) ? "0" : drAccountingItem.Cells["cDebit"].Value.ToString().Replace(",","");
  47. strAddedDebit = (Convert.ToInt32(strAddedDebit) + Convert.ToInt32(strdDebit)).ToString();
  48. }
  49. if (drAccountingItem.Cells["cCredit"].Value != null)
  50. {
  51. strCredit = string.IsNullOrEmpty(drAccountingItem.Cells["cCredit"].Value.ToString()) ? "0" : drAccountingItem.Cells["cCredit"].Value.ToString().Replace(",", "");
  52. strAddedCredit = (Convert.ToInt32(strAddedCredit) + Convert.ToInt32(strCredit)).ToString();
  53. }
  54. }
  55. lbDebit.Text = UtilityClass.MarkNumber(strAddedDebit);
  56. lbCredit.Text = UtilityClass.MarkNumber(strAddedCredit);
  57. }
  58. private void StatusChange(string strStatus) //變更主畫面狀態
  59. {
  60. strActiveUserID = MainForm.strActiveUserID;
  61. strAccountingBookID = MainForm.strAccountingBookID;
  62. strKey = MainForm.strKey;
  63. switch (strStatus.ToUpper())
  64. {
  65. case "NONE":
  66. //本功能無None狀態
  67. strFrmStatus = "";
  68. break;
  69. case "SEARCH":
  70. //本程式並無查詢功能
  71. Application.DoEvents();
  72. break;
  73. case "ADD":
  74. UnLockForm();
  75. txtAccountingID.Text = "";
  76. dpAccountingDate.Value = DateTime.Now;
  77. dpAccountingDate.Focus();
  78. strFrmStatus = "ADD";
  79. break;
  80. case "MODIFY":
  81. UnLockForm();
  82. txtAccountingID.Text = strPKey;
  83. //先取得該分錄的基本資料
  84. GetAccountingListByID(strPKey);
  85. dpAccountingDate.Focus();
  86. strFrmStatus = "MODIFY";
  87. break;
  88. case "CLOSE":
  89. UnLockForm();
  90. strFrmStatus = "CLOSE";
  91. break;
  92. case "DEL":
  93. btnSave.Text = "確認刪除";
  94. txtAccountingID.Text = strPKey;
  95. //應先取得該分錄的基本資料
  96. GetAccountingListByID(strPKey);
  97. dpAccountingDate.Enabled = false;
  98. dgvDataMaintain.AllowUserToAddRows = false;
  99. dgvDataMaintain.ReadOnly = true;
  100. btnCancle.Enabled = false;
  101. btnClose.Focus();
  102. strFrmStatus = "DEL";
  103. break;
  104. }
  105. }
  106. private bool CheckForm() //確認分錄是否正確
  107. {
  108. string strCheckResult = "";
  109. string strDebit = "";
  110. string strCredit = "";
  111. double dbDebit = 0; //借方總額
  112. double dbCredit = 0; //借方總額
  113. //確認借貸是否平衡
  114. foreach (DataGridViewRow dgRow in dgvDataMaintain.Rows)
  115. {
  116. strDebit = (string)dgRow.Cells["cDebit"].Value;
  117. strCredit = (string)dgRow.Cells["cCredit"].Value;
  118. if(!string.IsNullOrEmpty(strDebit))
  119. {
  120. dbDebit += Convert.ToDouble(strDebit.Replace(",",""));
  121. }
  122. if (!string.IsNullOrEmpty(strCredit))
  123. {
  124. dbCredit += Convert.ToDouble(strCredit.Replace(",", ""));
  125. }
  126. }
  127. if (dbDebit != dbCredit)
  128. {
  129. strCheckResult = "借貸不平衡";
  130. }
  131. //確認是否有開帳
  132. string strChackDate = dpAccountingDate.Text == "" ? DateTime.Now.ToString() : dpAccountingDate.Text;
  133. string strCheckSQL = "Select * From OTB_FNC_AccountingBookStatus Where IsOpen = 'Y' And AccountingYear = Year(Convert(DateTime,'" + Convert.ToDateTime(strChackDate).ToString("yyyy/MM/dd HH:mm:ss") + "'))";
  134. if (!UtilityClass.IsExist(strCheckSQL))
  135. {
  136. strCheckResult = "該會計年度尚未開帳! 請先至開帳功能進行開帳動作";
  137. }
  138. if (strCheckResult != "")
  139. {
  140. MessageBox.Show(strCheckResult);
  141. return false;
  142. }
  143. return true;
  144. }
  145. private void CleanForm() //清除畫面
  146. {
  147. //設定畫面物件狀態
  148. //清除GridView
  149. StatusChange(strFrmStatus);
  150. }
  151. private void UnLockForm() //解除限制唯讀物件
  152. {
  153. dpAccountingDate.Enabled = true;
  154. }
  155. private void LockForm() //限制唯讀物件
  156. {
  157. dpAccountingDate.Enabled = false;
  158. }
  159. private void GetAccountingListByID(string strAccountingSubID)
  160. {
  161. try
  162. {
  163. string strDebit = "";
  164. string strCredit = "";
  165. DataTable dtTemp = new DataTable();
  166. if (strAccountingSubID.Trim() != "")
  167. {
  168. //宣告物件
  169. string strSQL = string.Format("Select AccountingSubID, AccountingSubName, AccountingDate, Memo, Debit, Credit, ProjectNumber, ProjectCName From OTB_FNC_AccountingJournal Where AccountingID = '{0}' And AccountingBookID ='{1}' Order by AccountingOrder", strAccountingSubID, strAccountingBookID);
  170. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  171. {
  172. //添加參數
  173. sqlAdapter.SelectCommand = new SqlCommand();
  174. sqlAdapter.SelectCommand.Connection = sqlConn;
  175. sqlAdapter.SelectCommand.CommandText = strSQL;
  176. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  177. {
  178. sqlConn.Open();
  179. }
  180. //進行查詢
  181. dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
  182. if (dtTemp.Rows.Count > 0)
  183. {
  184. //strAccountingDate = Convert.ToDateTime(dtTemp.Rows[0]["AccountingDate"].ToString()).ToString("yyyy/MM/dd HH:mm:ss");
  185. dpAccountingDate.Text = Convert.ToDateTime(dtTemp.Rows[0]["AccountingDate"].ToString()).ToString("yyyy/MM/dd HH:mm:ss");
  186. foreach (DataRow drAccounting in dtTemp.Rows)
  187. {
  188. DataGridViewRow dgvRow = new DataGridViewRow();
  189. dgvRow.CreateCells(dgvDataMaintain);
  190. dgvRow.Cells[1].Value = drAccounting["AccountingSubID"].ToString(); //科目代號
  191. dgvRow.Cells[2].Value = drAccounting["AccountingSubName"].ToString(); //科目名稱
  192. dgvRow.Cells[3].Value = drAccounting["Memo"].ToString(); //摘要
  193. strDebit = UtilityClass.DecryptDES(drAccounting["Debit"].ToString(), strKey);
  194. strCredit = UtilityClass.DecryptDES(drAccounting["Credit"].ToString(), strKey);
  195. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(strDebit); //借方金額
  196. dgvRow.Cells[5].Value = UtilityClass.MarkNumber(strCredit); //貸方金額
  197. dgvRow.Cells[7].Value = drAccounting["ProjectCName"].ToString(); //專案名稱
  198. dgvRow.Cells[8].Value = drAccounting["ProjectNumber"].ToString(); //專案代碼
  199. dgvDataMaintain.Rows.Add(dgvRow);
  200. }
  201. }
  202. }
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex);
  208. }
  209. }
  210. public void ReturnAccountList(string strAccountList)
  211. {
  212. string[] strAccList = strAccountList.Split('|');
  213. dgvDataMaintain.Rows[Convert.ToInt32(strAccList[0])].Cells["cAccountingSubID"].Value = strAccList[1].ToString();
  214. dgvDataMaintain.Rows[Convert.ToInt32(strAccList[0])].Cells["cAccountingSubName"].Value = strAccList[2].ToString();
  215. }
  216. public string GetNewAccountID()
  217. {
  218. //宣告物件
  219. string strNewAccountID = "";
  220. int intMaxID = 0;
  221. //確認當日是否有取過傳票編號
  222. StringBuilder sbCheckNewAccountID = new StringBuilder();
  223. StringBuilder sbUpdateNewAccountID = new StringBuilder();
  224. sbCheckNewAccountID.Append("Select * From OTB_FNC_AccountingMaxNumber Where AccountingBookID = '"+ strAccountingBookID + "' ");
  225. sbCheckNewAccountID.Append("And CountYear = '" + DateTime.Now.Year.ToString() + "' ");
  226. sbCheckNewAccountID.Append("And CountMonth = '" + DateTime.Now.Month.ToString() + "' ");
  227. sbCheckNewAccountID.Append("And CountDay = '" + DateTime.Now.Day.ToString() + "' ");
  228. DataSet dsTemp = UtilityClass.GetSQLResult(sbCheckNewAccountID.ToString());
  229. if (dsTemp.Tables["Result"].Rows.Count > 0)
  230. {
  231. //當日已經有取得傳票編號
  232. intMaxID = Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountMax"].ToString()) + 1;
  233. strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountYear"]).ToString("0000"); //年碼
  234. strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountMonth"]).ToString("00"); //月碼
  235. strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountDay"]).ToString("00"); //日碼
  236. strNewAccountID += intMaxID.ToString("000"); //流水碼
  237. //更新傳票編號
  238. sbUpdateNewAccountID.Append("Update OTB_FNC_AccountingMaxNumber Set CountMax = CountMax + 1 ");
  239. sbUpdateNewAccountID.Append(", ModifyUser = '" + strActiveUserID + "', ModifyDate = Getdate() ");
  240. sbUpdateNewAccountID.Append("Where AccountingBookID = '" + strAccountingBookID + "' ");
  241. sbUpdateNewAccountID.Append("And CountYear = '" + dsTemp.Tables["Result"].Rows[0]["CountYear"].ToString() + "'");
  242. sbUpdateNewAccountID.Append("And CountMonth = '" + dsTemp.Tables["Result"].Rows[0]["CountMonth"].ToString() + "'");
  243. sbUpdateNewAccountID.Append("And CountDay = '" + dsTemp.Tables["Result"].Rows[0]["CountDay"].ToString() + "'");
  244. UtilityClass.RunSQLNonReturn(sbUpdateNewAccountID.ToString());
  245. }
  246. else
  247. {
  248. //當日未取過傳票編號
  249. intMaxID = 1;
  250. strNewAccountID += DateTime.Now.Year.ToString("0000"); //年碼
  251. strNewAccountID += DateTime.Now.Month.ToString("00"); //月碼
  252. strNewAccountID += DateTime.Now.Day.ToString("00"); //日碼
  253. strNewAccountID += intMaxID.ToString("000"); //流水碼
  254. //新增傳票編號
  255. sbUpdateNewAccountID.Append("Insert into OTB_FNC_AccountingMaxNumber(AccountingBookID, CountYear, CountMonth, CountDay, CountMax, Memo,CreateUser, CreateDate, ModifyUser, ModifyDate) ");
  256. sbUpdateNewAccountID.Append("Values ('" + strAccountingBookID + "',Year(getdate()),Month(getdate()),day(getdate()),1,null,'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "',Getdate())");
  257. UtilityClass.RunSQLNonReturn(sbUpdateNewAccountID.ToString());
  258. }
  259. return strNewAccountID;
  260. }
  261. public string GetNewAccountID(string strYear)
  262. {
  263. //宣告物件
  264. string strNewAccountID = "";
  265. int intMaxID = 0;
  266. //確認當日是否有取過傳票編號
  267. StringBuilder sbCheckNewAccountID = new StringBuilder();
  268. StringBuilder sbUpdateNewAccountID = new StringBuilder();
  269. sbCheckNewAccountID.Append("Select TOP 1 * From OTB_FNC_AccountingMaxNumber Where AccountingBookID = '" + strAccountingBookID + "' ");
  270. sbCheckNewAccountID.Append("And CountYear = '" + strYear + "' ");
  271. sbCheckNewAccountID.Append("Order By CountMonth DESC,CountDay DESC");
  272. DataSet dsTemp = UtilityClass.GetSQLResult(sbCheckNewAccountID.ToString());
  273. if (dsTemp.Tables["Result"].Rows.Count > 0)
  274. {
  275. //當日已經有取得傳票編號
  276. intMaxID = Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountMax"].ToString()) + 1;
  277. strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountYear"]).ToString("0000"); //年碼
  278. strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountMonth"]).ToString("00"); //月碼
  279. strNewAccountID += Convert.ToInt32(dsTemp.Tables["Result"].Rows[0]["CountDay"]).ToString("00"); //日碼
  280. strNewAccountID += intMaxID.ToString("000"); //流水碼
  281. //更新傳票編號
  282. sbUpdateNewAccountID.Append("Update OTB_FNC_AccountingMaxNumber Set CountMax = CountMax + 1 ");
  283. sbUpdateNewAccountID.Append(", ModifyUser = '" + strActiveUserID + "', ModifyDate = Getdate() ");
  284. sbUpdateNewAccountID.Append("Where AccountingBookID = '" + strAccountingBookID + "' ");
  285. sbUpdateNewAccountID.Append("And CountYear = '" + dsTemp.Tables["Result"].Rows[0]["CountYear"].ToString() + "'");
  286. sbUpdateNewAccountID.Append("And CountMonth = '" + dsTemp.Tables["Result"].Rows[0]["CountMonth"].ToString() + "'");
  287. sbUpdateNewAccountID.Append("And CountDay = '" + dsTemp.Tables["Result"].Rows[0]["CountDay"].ToString() + "'");
  288. UtilityClass.RunSQLNonReturn(sbUpdateNewAccountID.ToString());
  289. }
  290. else
  291. {
  292. //當日未取過傳票編號
  293. intMaxID = 1;
  294. strNewAccountID += DateTime.Now.Year.ToString("0000"); //年碼
  295. strNewAccountID += DateTime.Now.Month.ToString("00"); //月碼
  296. strNewAccountID += DateTime.Now.Day.ToString("00"); //日碼
  297. strNewAccountID += intMaxID.ToString("000"); //流水碼
  298. //新增傳票編號
  299. sbUpdateNewAccountID.Append("Insert into OTB_FNC_AccountingMaxNumber(AccountingBookID, CountYear, CountMonth, CountDay, CountMax, Memo,CreateUser, CreateDate, ModifyUser, ModifyDate) ");
  300. sbUpdateNewAccountID.Append("Values ('" + strAccountingBookID + "',Year(getdate()),Month(getdate()),day(getdate()),1,null,'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "',Getdate())");
  301. UtilityClass.RunSQLNonReturn(sbUpdateNewAccountID.ToString());
  302. }
  303. return strNewAccountID;
  304. }
  305. private void AddEvent(bool isNew) //新增事件
  306. {
  307. try
  308. {
  309. //宣告物件
  310. string strNewAccountingID = "";
  311. if(isNew) //判斷是否為
  312. {
  313. strNewAccountingID = GetNewAccountID();
  314. }
  315. else
  316. {
  317. strNewAccountingID = strPKey;
  318. }
  319. int intDataCount = dgvDataMaintain.Rows.Count - 1;
  320. StringBuilder strSQL = new StringBuilder();
  321. int intOrder = 0; //分錄排序欄位
  322. string strAccountingSubID = ""; //科目代號
  323. string strAccountingSubName = ""; //科目名稱
  324. string strMemo = ""; //備註
  325. string strDebit = ""; //借方金額
  326. string strCredit = ""; //貸方金額
  327. string strProjectNumber = ""; //專案代碼
  328. string strProjectCName = ""; //專案名稱
  329. string strClosedAccounting = ""; //是否為關帳分錄
  330. foreach(DataGridViewRow dgRow in dgvDataMaintain.Rows)
  331. {
  332. //設定初始值
  333. intOrder++;
  334. strAccountingSubID = ((string)dgRow.Cells["cAccountingSubID"].Value); //科目代號
  335. strAccountingSubName = ((string)dgRow.Cells["cAccountingSubName"].Value); //科目名稱
  336. strMemo = ((string)dgRow.Cells["cMemo"].Value); //備註
  337. strDebit = (string)dgRow.Cells["cDebit"].Value; //借方金額
  338. strCredit = (string)dgRow.Cells["cCredit"].Value; //貸方金額
  339. strProjectNumber = (string)dgRow.Cells["cProjectID"].Value; //專案代碼
  340. strProjectCName = (string)dgRow.Cells["cProjectName"].Value; //專案代碼
  341. strClosedAccounting = (string)dgRow.Cells["cClosedAccounting"].Value; //關帳分錄
  342. if (!string.IsNullOrEmpty(strAccountingSubID))
  343. {
  344. string strChackDate = dpAccountingDate.Text == "" ? DateTime.Now.ToString() : dpAccountingDate.Text;
  345. if (string.IsNullOrEmpty(strDebit)) { strDebit = "";} //處理借方金額為null值
  346. if (string.IsNullOrEmpty(strCredit)) { strCredit = ""; } //處理貸方金額為null值
  347. strSQL.Clear();
  348. strSQL.Append("Insert Into OTB_FNC_AccountingJournal(AccountingBookID, AccountingID, AccountingDate, AccountingOrder, AccountingSubID, AccountingSubName, ProjectNumber, ProjectCName, Memo, Debit, Credit, ClosedAccounting, CreateDate, CreateUser, ModifyDate, ModifyUser)");
  349. strSQL.Append(" Values ('" + strAccountingBookID + "','" + strNewAccountingID + "'," );
  350. strSQL.Append("Convert(DateTime,'" + Convert.ToDateTime(strChackDate).ToString("yyyy/MM/dd HH:mm:ss") + "'),");
  351. strSQL.Append(intOrder.ToString() + ",");
  352. strSQL.Append("'" + strAccountingSubID + "',");
  353. strSQL.Append("'" + strAccountingSubName + "',");
  354. strSQL.Append("'" + strProjectNumber + "',");
  355. strSQL.Append("'" + strProjectCName + "',");
  356. strSQL.Append("'" + strMemo + "',");
  357. strSQL.Append("'" + (strDebit == "" ? "" : UtilityClass.EncryptDES(strDebit.Replace(",",""), strKey)) + "',");
  358. strSQL.Append("'" + (strCredit == "" ? "" : UtilityClass.EncryptDES(strCredit.Replace(",", ""), strKey)) + "',");
  359. strSQL.Append("'" + (strClosedAccounting == "" ? "" : strClosedAccounting + "',"));
  360. strSQL.Append("Convert(DateTime,'" + Convert.ToDateTime(strChackDate).ToString("yyyy/MM/dd HH:mm:ss") + "'),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "') ");
  361. //添加參數
  362. sqlAdapter.InsertCommand = new SqlCommand();
  363. sqlAdapter.InsertCommand.Connection = sqlConn;
  364. sqlAdapter.InsertCommand.CommandText = strSQL.ToString();
  365. sqlAdapter.InsertCommand.Transaction = sqlTran;
  366. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  367. {
  368. sqlConn.Open();
  369. }
  370. sqlAdapter.InsertCommand.ExecuteNonQuery();
  371. }
  372. }
  373. }
  374. catch (Exception ex)
  375. {
  376. throw ex;
  377. }
  378. }
  379. private void AddEvent(string strYear) //新增某一個年度的分錄
  380. {
  381. try
  382. {
  383. //宣告物件
  384. string strNewAccountingID = "";
  385. if (strYear == "") //判斷是否為
  386. {
  387. strNewAccountingID = GetNewAccountID();
  388. }
  389. else
  390. {
  391. strNewAccountingID = GetNewAccountID(strYear);
  392. }
  393. int intDataCount = dgvDataMaintain.Rows.Count - 1;
  394. StringBuilder strSQL = new StringBuilder();
  395. int intOrder = 0; //分錄排序欄位
  396. string strAccountingSubID = ""; //科目代號
  397. string strAccountingSubName = ""; //科目名稱
  398. string strMemo = ""; //備註
  399. string strDebit = ""; //借方金額
  400. string strCredit = ""; //貸方金額
  401. string strProjectNumber = ""; //專案代碼
  402. string strProjectCName = ""; //專案名稱
  403. string strClosedAccounting = ""; //是否為關帳分錄
  404. foreach (DataGridViewRow dgRow in dgvDataMaintain.Rows)
  405. {
  406. //設定初始值
  407. intOrder++;
  408. strAccountingSubID = ((string)dgRow.Cells["cAccountingSubID"].Value); //科目代號
  409. strAccountingSubName = ((string)dgRow.Cells["cAccountingSubName"].Value); //科目名稱
  410. strMemo = ((string)dgRow.Cells["cMemo"].Value); //備註
  411. strDebit = (string)dgRow.Cells["cDebit"].Value; //借方金額
  412. strCredit = (string)dgRow.Cells["cCredit"].Value; //貸方金額
  413. strProjectNumber = (string)dgRow.Cells["cProjectID"].Value; //專案代碼
  414. strProjectCName = (string)dgRow.Cells["cProjectName"].Value; //專案代碼
  415. strClosedAccounting = (string)dgRow.Cells["cClosedAccounting"].Value; //關帳分錄
  416. if (!string.IsNullOrEmpty(strAccountingSubID))
  417. {
  418. string strChackDate = dpAccountingDate.Text == "" ? DateTime.Now.ToString() : dpAccountingDate.Text;
  419. if (string.IsNullOrEmpty(strDebit)) { strDebit = ""; } //處理借方金額為null值
  420. if (string.IsNullOrEmpty(strCredit)) { strCredit = ""; } //處理貸方金額為null值
  421. strSQL.Clear();
  422. strSQL.Append("Insert Into OTB_FNC_AccountingJournal(AccountingBookID, AccountingID, AccountingDate, AccountingOrder, AccountingSubID, AccountingSubName, ProjectNumber, ProjectCName, Memo, Debit, Credit, ClosedAccounting, CreateDate, CreateUser, ModifyDate, ModifyUser)");
  423. strSQL.Append(" Values ('" + strAccountingBookID + "','" + strNewAccountingID + "',");
  424. strSQL.Append("Convert(DateTime,'" + Convert.ToDateTime(strNewAccountingID.Substring(0, 4) + "/" + strNewAccountingID.Substring(4, 2) + "/" + strNewAccountingID.Substring(6, 2) + " 23:59:59").ToString("yyyy/MM/dd HH:mm:ss") + "'),");
  425. strSQL.Append(intOrder.ToString() + ",");
  426. strSQL.Append("'" + strAccountingSubID + "',");
  427. strSQL.Append("'" + strAccountingSubName + "',");
  428. strSQL.Append("'" + strProjectNumber + "',");
  429. strSQL.Append("'" + strProjectCName + "',");
  430. strSQL.Append("'" + strMemo + "',");
  431. strSQL.Append("'" + (strDebit == "" ? "" : UtilityClass.EncryptDES(strDebit.Replace(",", ""), strKey)) + "',");
  432. strSQL.Append("'" + (strCredit == "" ? "" : UtilityClass.EncryptDES(strCredit.Replace(",", ""), strKey)) + "',");
  433. strSQL.Append("'" + (strClosedAccounting == "" ? "" : strClosedAccounting + "',"));
  434. strSQL.Append("Convert(DateTime,'" + Convert.ToDateTime(strChackDate).ToString("yyyy/MM/dd HH:mm:ss") + "'),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "') ");
  435. //添加參數
  436. sqlAdapter.InsertCommand = new SqlCommand();
  437. sqlAdapter.InsertCommand.Connection = sqlConn;
  438. sqlAdapter.InsertCommand.CommandText = strSQL.ToString();
  439. sqlAdapter.InsertCommand.Transaction = sqlTran;
  440. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  441. {
  442. sqlConn.Open();
  443. }
  444. sqlAdapter.InsertCommand.ExecuteNonQuery();
  445. }
  446. }
  447. }
  448. catch (Exception ex)
  449. {
  450. throw ex;
  451. }
  452. }
  453. private void DelEvent() //刪除事件
  454. {
  455. try
  456. {
  457. //宣告物件
  458. string strSQL = string.Format("Delete OTB_FNC_AccountingJournal Where AccountingID = '{0}' And AccountingBookID = '{1}'", strPKey,strAccountingBookID);
  459. //添加參數
  460. sqlAdapter.DeleteCommand = new SqlCommand();
  461. sqlAdapter.DeleteCommand.Connection = sqlConn;
  462. sqlAdapter.DeleteCommand.CommandText = strSQL.ToString();
  463. sqlAdapter.DeleteCommand.Transaction = sqlTran;
  464. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  465. {
  466. sqlConn.Open();
  467. }
  468. sqlAdapter.DeleteCommand.ExecuteNonQuery();
  469. }
  470. catch (Exception ex)
  471. {
  472. throw ex;
  473. }
  474. }
  475. public void SaveEvent() //儲存事件
  476. {
  477. //進行資料整理
  478. switch (strFrmStatus)
  479. {
  480. case "ADD":
  481. //新增事件
  482. try
  483. {
  484. if (CheckForm()) //資料確認
  485. {
  486. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  487. {
  488. sqlConn.Open();
  489. }
  490. if(sqlTran == null)
  491. {
  492. sqlTran = sqlConn.BeginTransaction(); //開始交易
  493. }
  494. AddEvent(true); //進行資料的新增(取新傳票編號)
  495. StatusChange("NONE");
  496. switch (strOwnerForm)
  497. {
  498. case "AccountingJournalMaintain":
  499. sqlTran.Commit();
  500. ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態
  501. ((AccountingJournalMaintain)this.Owner).GoEvent(); //進行母Form查詢功能
  502. break;
  503. case "AccountingJournalMaintain_Reversal":
  504. sqlTran.Commit();
  505. ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態
  506. ((AccountingJournalMaintain)this.Owner).GoEvent(); //進行母Form查詢功能
  507. break;
  508. case "PayPlanManagement_Receive":
  509. ((PayPlanManagement)this.Owner).blIsAdded = true;
  510. ((PayPlanManagement)this.Owner).CreateAR(dgeArgs); //增加應收帳款記錄
  511. ((PayPlanManagement)this.Owner).GoEvent(); //進行母Form儲存功能
  512. sqlTran.Commit();
  513. break;
  514. case "PayPlanManagement_Pay":
  515. ((PayPlanManagement)this.Owner).blIsAdded = true;
  516. ((PayPlanManagement)this.Owner).CreateAP(dgeArgs); //增加應付帳款記錄
  517. ((PayPlanManagement)this.Owner).GoEvent(); //進行母Form儲存功能
  518. sqlTran.Commit();
  519. break;
  520. case "AccountsReceivable":
  521. ((AccountsReceivable)this.Owner).blIsAdded = true;
  522. ((AccountsReceivable)this.Owner).UpdateAR(dgeArgs); //更新母Form的收款狀況
  523. ((AccountsReceivable)this.Owner).StatusChange("SEARCH");
  524. ((AccountsReceivable)this.Owner).GoEvent(); //進行母Form儲存功能
  525. sqlTran.Commit();
  526. break;
  527. case "AccountsPay":
  528. ((AccountsPayable)this.Owner).blIsAdded = true;
  529. ((AccountsPayable)this.Owner).UpdateAP(dgeArgs); //更新母Form的付款狀況
  530. ((AccountsPayable)this.Owner).StatusChange("SEARCH");
  531. ((AccountsPayable)this.Owner).GoEvent(); //進行母Form儲存功能
  532. sqlTran.Commit();
  533. break;
  534. case "AccountingOpeningEntry":
  535. sqlTran.Commit();
  536. break;
  537. }
  538. this.Close();
  539. }
  540. }
  541. catch (Exception ex)
  542. {
  543. sqlTran.Rollback();
  544. MessageBox.Show("資料新增失敗", "提示");
  545. ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex);
  546. }
  547. break;
  548. case "CLOSE":
  549. //關帳事件
  550. try
  551. {
  552. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  553. {
  554. sqlConn.Open();
  555. }
  556. if (sqlTran == null)
  557. {
  558. sqlTran = sqlConn.BeginTransaction(); //開始交易
  559. }
  560. AddEvent(strAccountingYear); //進行資料的新增(取新傳票編號)
  561. //AddEvent(true);
  562. StatusChange("NONE");
  563. switch (strOwnerForm)
  564. {
  565. case "AccountingOpeningEntry":
  566. sqlTran.Commit();
  567. break;
  568. }
  569. this.Close();
  570. }
  571. catch (Exception ex)
  572. {
  573. sqlTran.Rollback();
  574. MessageBox.Show("關帳失敗", "提示");
  575. ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex);
  576. }
  577. break;
  578. case "MODIFY":
  579. //修改事件
  580. try
  581. {
  582. if (CheckForm()) //資料確認
  583. {
  584. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  585. {
  586. sqlConn.Open();
  587. }
  588. sqlTran = sqlConn.BeginTransaction(); //開始交易
  589. DelEvent(); //先將資料進行刪除
  590. AddEvent(false); //將資料重新新增(但不取新傳票編號)
  591. sqlTran.Commit();
  592. MessageBox.Show("資料修改成功", "提示");
  593. StatusChange("NONE");
  594. ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態
  595. ((AccountingJournalMaintain)this.Owner).GoEvent(); //進行母Form查詢功能
  596. this.Close();
  597. }
  598. }
  599. catch (Exception ex)
  600. {
  601. sqlTran.Rollback();
  602. MessageBox.Show("資料修改失敗", "提示");
  603. ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex);
  604. }
  605. break;
  606. case "DEL":
  607. //刪除事件
  608. try
  609. {
  610. if (MessageBox.Show("請問您是否確認刪除本分錄資料", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  611. {
  612. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  613. {
  614. sqlConn.Open();
  615. }
  616. sqlTran = sqlConn.BeginTransaction(); //開始交易
  617. DelEvent(); //將資料進行刪除
  618. sqlTran.Commit();
  619. MessageBox.Show("資料刪除成功", "提示");
  620. StatusChange("NONE");
  621. ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態
  622. ((AccountingJournalMaintain)this.Owner).GoEvent(); //進行母Form查詢功能
  623. this.Close();
  624. }
  625. }
  626. catch (Exception ex)
  627. {
  628. sqlTran.Rollback();
  629. MessageBox.Show("資料刪除失敗", "提示");
  630. ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex);
  631. }
  632. break;
  633. }
  634. }
  635. public void ReturnProjectInfo(string[] strProjectInfo)
  636. {
  637. txtProjectName.Text = strProjectInfo[1].ToString();
  638. txtProjectNumber.Text = strProjectInfo[0].ToString();
  639. }
  640. public void ReturnProjectInfo(int intIndexID, string[] strProjectInfo)
  641. {
  642. dgvDataMaintain.Rows[intIndexID].Cells["cProjectID"].Value = strProjectInfo[0].ToString();
  643. dgvDataMaintain.Rows[intIndexID].Cells["cProjectName"].Value = strProjectInfo[1].ToString();
  644. }
  645. #endregion
  646. #region 事件觸發及問題處理
  647. private void btnSave_Click(object sender, EventArgs e)
  648. {
  649. SaveEvent();
  650. }
  651. private void btnCancle_Click(object sender, EventArgs e)
  652. {
  653. CleanForm();
  654. }
  655. private void btnClose_Click(object sender, EventArgs e)
  656. {
  657. ((AccountingJournalMaintain)this.Owner).StatusChange("NONE"); //還原母Form的狀態
  658. this.Close();
  659. }
  660. private void AccountingEntries_Load(object sender, EventArgs e)
  661. {
  662. CleanForm();
  663. switch (strOwnerForm)
  664. {
  665. case "AccountingJournalMaintain":
  666. label4.Visible = false;
  667. txtProjectName.Visible = false;
  668. txtProjectNumber.Visible = false;
  669. btnGetProject.Visible = false;
  670. break;
  671. }
  672. }
  673. private void dgvDataMaintain_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  674. {
  675. //物件宣告
  676. string strAccountingSubID = "";
  677. string strAccountingSubName = "";
  678. string strDebit = "";
  679. string strCredit = "";
  680. string strProjectName = "";
  681. //Cell[0] : 選窗Button
  682. //Cell[1] : 科目代碼(cAccountingSubID)
  683. //Cell[2] : 科目名稱(cAccountingSubName)
  684. //Cell[3] : 摘要(cMemo)
  685. //Cell[4] : 借方金額(cDebit)
  686. //Cell[5] : 貸方金額(cCredit)
  687. //Cell[6] : 選窗Button
  688. //Cell[7] : 專案名稱(cProjectName)
  689. //Cell[8] : 專案代碼(cProjectID)
  690. //科目代碼變動
  691. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cAccountingSubID")
  692. {
  693. strAccountingSubID = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value;
  694. if (string.IsNullOrEmpty(strAccountingSubID))
  695. {
  696. return;
  697. }
  698. else
  699. {
  700. string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubID = '{0}' And AccountingBookID = '{1}'", strAccountingSubID, strAccountingBookID);
  701. if (UtilityClass.IsExist(strCheckSQL))
  702. {
  703. strAccountingSubName = UtilityClass.GetAccountingSubjectByID(strAccountingSubID, strAccountingBookID).Rows[0]["AccountingSubName"].ToString();
  704. dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value = strAccountingSubName;
  705. }
  706. else
  707. {
  708. PickAccountingSubject pkItemForm = new PickAccountingSubject();
  709. pkItemForm.txtAccountingSubID.Text = strAccountingSubID;
  710. pkItemForm.intIndex = e.RowIndex;
  711. pkItemForm.strOwnerForm = "AccountingEntries";
  712. pkItemForm.Owner = this;
  713. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  714. pkItemForm.ShowDialog();
  715. }
  716. }
  717. }
  718. //科目名稱變動
  719. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cAccountingSubName")
  720. {
  721. strAccountingSubName = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value;
  722. if (string.IsNullOrEmpty(strAccountingSubName))
  723. {
  724. return;
  725. }
  726. else
  727. {
  728. string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubName = '{0}' And AccountingBookID = '{1}'", strAccountingSubName, strAccountingBookID);
  729. if (UtilityClass.IsExist(strCheckSQL))
  730. {
  731. strAccountingSubID = UtilityClass.GetAccountingSubjectByName(strAccountingSubName).Rows[0]["AccountingSubID"].ToString();
  732. dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value = strAccountingSubID;
  733. }
  734. else
  735. {
  736. PickAccountingSubject pkItemForm = new PickAccountingSubject();
  737. pkItemForm.txtAccountingSubName.Text = strAccountingSubName;
  738. pkItemForm.intIndex = e.RowIndex;
  739. pkItemForm.strOwnerForm = "AccountingEntries";
  740. pkItemForm.Owner = this;
  741. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  742. pkItemForm.ShowDialog();
  743. }
  744. }
  745. }
  746. //數字呈現方式調整
  747. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cDebit")
  748. {
  749. strDebit = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value;
  750. if (string.IsNullOrEmpty(strDebit))
  751. {
  752. return;
  753. }
  754. else
  755. {
  756. strDebit = strDebit.Replace(",", "");
  757. dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value = UtilityClass.MarkNumber(strDebit);
  758. }
  759. }
  760. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cCredit")
  761. {
  762. strCredit = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value;
  763. if (string.IsNullOrEmpty(strCredit))
  764. {
  765. return;
  766. }
  767. else
  768. {
  769. strCredit = strCredit.Replace(",", "");
  770. dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value = UtilityClass.MarkNumber(strCredit);
  771. }
  772. }
  773. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cProjectName")
  774. {
  775. strProjectName = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cProjectName"].Value;
  776. if (string.IsNullOrEmpty(strProjectName))
  777. {
  778. dgvDataMaintain.Rows[e.RowIndex].Cells["cProjectID"].Value = "";
  779. }
  780. }
  781. }
  782. private void dgvDataMaintain_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  783. {
  784. //物件宣告
  785. string strErrorMsg = "";
  786. //Cell[0] : 選窗Button
  787. //Cell[1] : 科目代碼(cAccountingSubID)
  788. //Cell[2] : 科目名稱(cAccountingSubName)
  789. //Cell[3] : 摘要(cMemo)
  790. //Cell[4] : 借方金額(cDebit)
  791. //Cell[5] : 貸方金額(cCredit)
  792. if (!
  793. (
  794. string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value) &
  795. string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value) &
  796. string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value) & string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value))
  797. ) //判斷是否所有條件均為未填
  798. {
  799. if (string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value))
  800. {
  801. strErrorMsg = "科目代碼不得為空白";
  802. }
  803. if (string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value))
  804. {
  805. strErrorMsg = "科目名稱不得為空白";
  806. }
  807. if (string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value) & string.IsNullOrEmpty((string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value))
  808. {
  809. strErrorMsg = "借貸方不得均為空白";
  810. }
  811. }
  812. if (strErrorMsg != "")
  813. {
  814. dgvDataMaintain.Rows[e.RowIndex].ErrorText = strErrorMsg;
  815. e.Cancel = true;
  816. }
  817. }
  818. private void dgvDataMaintain_CellValidated(object sender, DataGridViewCellEventArgs e)
  819. {
  820. dgvDataMaintain.Rows[e.RowIndex].ErrorText = "";
  821. ReSum();
  822. }
  823. private void dgvDataMaintain_CellContentClick(object sender, DataGridViewCellEventArgs e)
  824. {
  825. //當GridView中的Button被按下
  826. var SenderGrid = (DataGridView)sender;
  827. if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && SenderGrid.Columns[e.ColumnIndex].Name == "cAdd" && e.RowIndex >= 0)
  828. {
  829. PickAccountingSubject pkItemForm = new PickAccountingSubject();
  830. pkItemForm.intIndex = e.RowIndex;
  831. pkItemForm.strOwnerForm = "AccountingEntries";
  832. pkItemForm.Owner = this;
  833. pkItemForm.strAccountingBookID = strAccountingBookID;
  834. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  835. pkItemForm.ShowDialog();
  836. }
  837. if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && SenderGrid.Columns[e.ColumnIndex].Name == "cAddProject" && e.RowIndex >= 0)
  838. {
  839. PickProject pkItemForm = new PickProject();
  840. pkItemForm.intIndex = e.RowIndex;
  841. pkItemForm.strOwnerForm = "AccountingEntries_Detail";
  842. pkItemForm.Owner = this;
  843. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  844. pkItemForm.ShowDialog();
  845. }
  846. }
  847. private void dgvDataMaintain_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  848. {
  849. //物件宣告
  850. string strDebit = "";
  851. string strCredit = "";
  852. //Cell[0] : 選窗Button
  853. //Cell[1] : 科目代碼(cAccountingSubID)
  854. //Cell[2] : 科目名稱(cAccountingSubName)
  855. //Cell[3] : 摘要(cMemo)
  856. //Cell[4] : 借方金額(cDebit)
  857. //Cell[5] : 貸方金額(cCredit)
  858. //數字呈現方式調整
  859. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cDebit")
  860. {
  861. strDebit = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value;
  862. if (string.IsNullOrEmpty(strDebit))
  863. {
  864. return;
  865. }
  866. else
  867. {
  868. dgvDataMaintain.Rows[e.RowIndex].Cells["cDebit"].Value = strDebit.Replace(",", "");
  869. }
  870. }
  871. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cCredit")
  872. {
  873. strCredit = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value;
  874. if (string.IsNullOrEmpty(strCredit))
  875. {
  876. return;
  877. }
  878. else
  879. {
  880. dgvDataMaintain.Rows[e.RowIndex].Cells["cCredit"].Value = strCredit.Replace(",", "");
  881. }
  882. }
  883. }
  884. private void btnGetProject_Click(object sender, EventArgs e)
  885. {
  886. PickProject pkItemForm = new PickProject();
  887. pkItemForm.txtProjectName.Text = txtProjectName.Text.Trim();
  888. pkItemForm.strOwnerForm = "AccountingEntries";
  889. pkItemForm.Owner = this;
  890. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  891. pkItemForm.ShowDialog();
  892. }
  893. #endregion
  894. }
  895. }