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.

406 lines
16 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 ManagementSystem.Utility;
  13. namespace ManagementSystem
  14. {
  15. public partial class AccountingLedger : Form
  16. {
  17. //程式內共用物件
  18. public string strFrmStatus = ""; //表單狀態
  19. public string strPKey = ""; //程式內的Key值
  20. string strKey = ""; //程式內加密的Key值
  21. string strActiveUserID = ""; //取得登入作用的使用者帳號
  22. string strAccountingBookID = ""; //取得作用中的帳本
  23. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  24. SqlTransaction sqlTran;
  25. SqlDataAdapter sqlAdapter = new SqlDataAdapter();
  26. SqlCommand sqlCmd = new SqlCommand();
  27. DataSet sdInsurance = new System.Data.DataSet();
  28. public AccountingLedger()
  29. {
  30. InitializeComponent();
  31. }
  32. #region 自定義程式
  33. private void StatusChange(string strStatus) //變更主畫面狀態
  34. {
  35. strActiveUserID = MainForm.strActiveUserID;
  36. strAccountingBookID = MainForm.strAccountingBookID;
  37. strKey = MainForm.strKey;
  38. switch (strStatus.ToUpper())
  39. {
  40. case "NONE":
  41. //本功能無None狀態
  42. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
  43. strFrmStatus = "";
  44. CleanToolbar();
  45. CleanForm();
  46. LockForm();
  47. tsbSearch.Enabled = true;
  48. tsbOK.Visible = false;
  49. tsbCancel.Visible = false;
  50. break;
  51. case "SEARCH":
  52. //查詢功能
  53. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
  54. tsbSearch.Visible = false;
  55. tsbOK.Visible = true;
  56. tsbCancel.Visible = true;
  57. UnLockForm();
  58. break;
  59. case "ADD":
  60. //本程式無新增功能
  61. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
  62. strFrmStatus = "ADD";
  63. break;
  64. case "MODIFY":
  65. //本程式無修改功能
  66. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
  67. strFrmStatus = "MODIFY";
  68. break;
  69. case "DEL":
  70. //本程式無刪除功能
  71. ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
  72. strFrmStatus = "DEL";
  73. break;
  74. }
  75. }
  76. private void GoEvent() //執行動作
  77. {
  78. //本程式只有查詢結果
  79. if (txtAccountingSubID.Text.Trim() == "")
  80. {
  81. MessageBox.Show("請輸入科目編號", "提示");
  82. }
  83. else
  84. {
  85. dgvAccountingLedger.Rows.Clear();
  86. SearchAccountingLedger(txtAccountingSubID.Text.Trim());
  87. StatusChange("NONE");
  88. UtilityClass.SetGridColor(dgvAccountingLedger);
  89. }
  90. }
  91. private void CleanForm() //清除畫面
  92. {
  93. //設定畫面物件狀態
  94. //清除GridView
  95. cbAccountingYear.DataSource = UtilityClass.GetAccountingYears(5);
  96. cbAccountingYear.SelectedIndex = 0;
  97. txtAccountingSubID.Text = "";
  98. txtAccountingSubName.Text = "";
  99. dpAccStart.Text = DateTime.Now.Year.ToString() + "/1/1";
  100. dpAccEnd.Text = DateTime.Now.ToShortDateString();
  101. }
  102. private void LockForm() //鎖定物件
  103. {
  104. txtAccountingSubID.ReadOnly = true;
  105. txtAccountingSubName.ReadOnly = true;
  106. cbAccountingYear.Enabled = false;
  107. dpAccStart.Enabled = false;
  108. dpAccEnd.Enabled = false;
  109. btnGetSubAccID.Enabled = false;
  110. }
  111. private void UnLockForm() //物件解鎖
  112. {
  113. txtAccountingSubID.ReadOnly = false;
  114. txtAccountingSubName.ReadOnly = false;
  115. cbAccountingYear.Enabled = true;
  116. dpAccStart.Enabled = true;
  117. dpAccEnd.Enabled = true;
  118. btnGetSubAccID.Enabled = true;
  119. }
  120. private void CleanToolbar() //清除工具列
  121. {
  122. //設定Toolbar狀態
  123. tsbSearch.Visible = true;
  124. tsbAdd.Visible = false;
  125. tsbEdit.Visible = false;
  126. tsbDelete.Visible = false;
  127. tsbSave.Visible = false;
  128. tsbOK.Visible = false;
  129. tsbCancel.Visible = false;
  130. }
  131. public void ReturnAccountList(string strAccountList) //取得資料
  132. {
  133. //strAccList[0] : IndexID
  134. //strAccList[1] : 科目代碼
  135. //strAccList[2] : 科目名稱
  136. string[] strAccList = strAccountList.Split('|');
  137. txtAccountingSubID.Text = strAccList[1];
  138. txtAccountingSubName.Text = strAccList[2];
  139. }
  140. private DataTable GetAccountingSubjectByID(string strAccountingSubID)
  141. {
  142. try
  143. {
  144. DataTable dtTemp = new DataTable();
  145. if (strAccountingSubID.Trim() != "")
  146. {
  147. //宣告物件
  148. string strSQL = string.Format("Select AccountingSubID, AccountingClass, DCClass, AccountingSubName From OTB_FNC_AccountingSubjects Where AccountingSubID = '{0}' And AccountingBookID = '{1}'", strAccountingSubID, strAccountingBookID);
  149. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  150. {
  151. //添加參數
  152. sqlAdapter.SelectCommand = new SqlCommand();
  153. sqlAdapter.SelectCommand.Connection = sqlConn;
  154. sqlAdapter.SelectCommand.CommandText = strSQL;
  155. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  156. {
  157. sqlConn.Open();
  158. }
  159. //進行查詢
  160. dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
  161. }
  162. }
  163. return dtTemp;
  164. }
  165. catch (Exception ex)
  166. {
  167. ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex);
  168. return null;
  169. }
  170. }
  171. private DataTable GetAccountingSubjectByName(string strAccountingSubName)
  172. {
  173. try
  174. {
  175. DataTable dtTemp = new DataTable();
  176. if (strAccountingSubName.Trim() != "")
  177. {
  178. //宣告物件
  179. string strSQL = string.Format("Select AccountingSubID, AccountingClass, DCClass, AccountingSubName From OTB_FNC_AccountingSubjects Where AccountingSubName = '{0}' And AccountingBookID ='{1}'", strAccountingSubName, strAccountingBookID);
  180. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  181. {
  182. //添加參數
  183. sqlAdapter.SelectCommand = new SqlCommand();
  184. sqlAdapter.SelectCommand.Connection = sqlConn;
  185. sqlAdapter.SelectCommand.CommandText = strSQL;
  186. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  187. {
  188. sqlConn.Open();
  189. }
  190. //進行查詢
  191. dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
  192. }
  193. }
  194. return dtTemp;
  195. }
  196. catch (Exception ex)
  197. {
  198. ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex);
  199. return null;
  200. }
  201. }
  202. private void SearchAccountingLedger(string strAccountingSubID)
  203. {
  204. try
  205. {
  206. DataTable dtTemp = new DataTable();
  207. if (strAccountingSubID.Trim() != "")
  208. {
  209. //宣告物件
  210. string strDebit = "";
  211. string strCredit = "";
  212. double dbBalance = 0;
  213. string strSQL = string.Format("Select * From OTB_FNC_AccountingJournal Where 1 = 1 And AccountingBookID = '{0}' And AccountingSubID = '{1}' And AccountingDate Between '{2} 00:00:00' And '{3} 23:59:59' Order By AccountingDate", strAccountingBookID, strAccountingSubID, dpAccStart.Text.Trim(), dpAccEnd.Text.Trim());
  214. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  215. {
  216. //添加參數
  217. sqlAdapter.SelectCommand = new SqlCommand();
  218. sqlAdapter.SelectCommand.Connection = sqlConn;
  219. sqlAdapter.SelectCommand.CommandText = strSQL;
  220. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  221. {
  222. sqlConn.Open();
  223. }
  224. //進行查詢
  225. dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
  226. if (dtTemp.Rows.Count > 0)
  227. {
  228. foreach (DataRow drData in dtTemp.Rows)
  229. {
  230. DataGridViewRow dgvRow = new DataGridViewRow();
  231. dgvRow.CreateCells(dgvAccountingLedger);
  232. dgvRow.Cells[0].Value = drData["AccountingID"].ToString(); //傳票編號
  233. dgvRow.Cells[1].Value = drData["AccountingDate"].ToString(); //傳票日期
  234. dgvRow.Cells[2].Value = drData["Memo"].ToString(); //摘要
  235. strDebit = (string)UtilityClass.DecryptDES(drData["Debit"].ToString(), strKey);
  236. strCredit = UtilityClass.DecryptDES(drData["Credit"].ToString(), strKey);
  237. dgvRow.Cells[3].Value = UtilityClass.MarkNumber(strDebit); //借方金額
  238. dgvRow.Cells[4].Value = UtilityClass.MarkNumber(strCredit); //貸方金額
  239. dbBalance += Convert.ToDouble(string.IsNullOrEmpty(strDebit) ? "0" : strDebit) - Convert.ToDouble(string.IsNullOrEmpty(strCredit) ? "0" : strCredit); //計算餘額
  240. dgvRow.Cells[5].Value = UtilityClass.MarkNumber(dbBalance); //餘額
  241. dgvAccountingLedger.Rows.Add(dgvRow);
  242. }
  243. //將資料移到最下面
  244. dgvAccountingLedger.CurrentCell = dgvAccountingLedger.Rows[dtTemp.Rows.Count - 1].Cells[0];
  245. }
  246. }
  247. }
  248. }
  249. catch (Exception ex)
  250. {
  251. ErrorHandler.WriteErrorLog("AccountingEntries.cs", ex);
  252. }
  253. }
  254. #endregion
  255. #region 事件觸發及問題處理
  256. private void AccountingLedger_Load(object sender, EventArgs e)
  257. {
  258. StatusChange("NONE");
  259. }
  260. private void tsbSearch_Click(object sender, EventArgs e)
  261. {
  262. StatusChange("SEARCH");
  263. }
  264. private void tsbClean_Click(object sender, EventArgs e)
  265. {
  266. CleanForm();
  267. lbAccountingName.Text = "";
  268. dgvAccountingLedger.Rows.Clear();
  269. }
  270. private void tsbCancel_Click(object sender, EventArgs e)
  271. {
  272. StatusChange("NONE");
  273. lbAccountingName.Text = "";
  274. }
  275. private void tsbExit_Click(object sender, EventArgs e)
  276. {
  277. this.Close();
  278. }
  279. private void btnGetSubAccID_Click(object sender, EventArgs e)
  280. {
  281. PickAccountingSubject pkItemForm = new PickAccountingSubject();
  282. string strAccountingSubID = txtAccountingSubID.Text.Trim();
  283. string strAccountingSubName = txtAccountingSubName.Text.Trim();
  284. if(strAccountingSubID != "")
  285. {
  286. pkItemForm.txtAccountingSubID.Text = strAccountingSubID;
  287. }
  288. if (strAccountingSubName != "")
  289. {
  290. pkItemForm.txtAccountingSubName.Text = strAccountingSubName;
  291. }
  292. pkItemForm.Owner = this;
  293. pkItemForm.strOwnerForm = "AccountingLedger";
  294. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  295. pkItemForm.ShowDialog();
  296. }
  297. private void txtAccountingSubID_Leave(object sender, EventArgs e)
  298. {
  299. //物件宣告
  300. string strAccountingSubID = "";
  301. string strAccountingSubName = "";
  302. strAccountingSubID = txtAccountingSubID.Text.Trim();
  303. if (string.IsNullOrEmpty(strAccountingSubID))
  304. {
  305. return;
  306. }
  307. else
  308. {
  309. string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubID = '{0}' And AccountingBookID = '{1}'", strAccountingSubID, strAccountingBookID);
  310. if (UtilityClass.IsExist(strCheckSQL))
  311. {
  312. strAccountingSubName = GetAccountingSubjectByID(strAccountingSubID).Rows[0]["AccountingSubName"].ToString();
  313. txtAccountingSubName.Text = strAccountingSubName;
  314. }
  315. }
  316. }
  317. private void txtAccountingSubName_Leave(object sender, EventArgs e)
  318. {
  319. //物件宣告
  320. string strAccountingSubID = "";
  321. string strAccountingSubName = "";
  322. strAccountingSubName = txtAccountingSubName.Text.Trim();
  323. if (string.IsNullOrEmpty(strAccountingSubName))
  324. {
  325. return;
  326. }
  327. else
  328. {
  329. string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubName = '{0}' And AccountingBookID = '{1}'", strAccountingSubName, strAccountingBookID);
  330. if (UtilityClass.IsExist(strCheckSQL))
  331. {
  332. strAccountingSubID = GetAccountingSubjectByName(strAccountingSubName).Rows[0]["AccountingSubID"].ToString();
  333. txtAccountingSubID.Text = strAccountingSubID;
  334. }
  335. }
  336. }
  337. private void tsbOK_Click(object sender, EventArgs e)
  338. {
  339. lbAccountingName.Text = txtAccountingSubName.Text.Trim();
  340. GoEvent();
  341. }
  342. private void cbAccountingYear_SelectedIndexChanged(object sender, EventArgs e)
  343. {
  344. if (cbAccountingYear.SelectedValue.ToString() == DateTime.Now.Year.ToString())
  345. {
  346. dpAccStart.Text = DateTime.Now.Year.ToString() + "/1/1";
  347. dpAccEnd.Text = DateTime.Now.ToShortDateString();
  348. }
  349. else
  350. {
  351. dpAccStart.Text = cbAccountingYear.SelectedValue.ToString() + "/1/1";
  352. dpAccEnd.Text = cbAccountingYear.SelectedValue.ToString() + "/12/31";
  353. }
  354. }
  355. #endregion
  356. }
  357. }