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.

254 lines
10 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.Xml.Linq;
  11. using System.IO;
  12. using System.Configuration;
  13. using ManagementSystem.Utility;
  14. namespace ManagementSystem
  15. {
  16. public partial class XMLSetting : Form
  17. {
  18. //變數宣告
  19. public string strOwnerForm = ""; //設定表單的擁有者
  20. public string strXMLName = ""; //XML的名稱
  21. public string strAccountingBookID = ""; //取得作用中的帳本
  22. public XMLSetting()
  23. {
  24. InitializeComponent();
  25. }
  26. #region 自定義程式
  27. public void ReturnAccountList(string strAccountList)
  28. {
  29. string[] strAccList = strAccountList.Split('|');
  30. dgvDataMaintain.Rows[Convert.ToInt32(strAccList[0])].Cells["cAccountingSubID"].Value = strAccList[1].ToString();
  31. dgvDataMaintain.Rows[Convert.ToInt32(strAccList[0])].Cells["cAccountingSubName"].Value = strAccList[2].ToString();
  32. }
  33. private void CleanForm()
  34. {
  35. dgvDataMaintain.Rows.Clear();
  36. }
  37. #endregion
  38. #region 事件觸發及問題處理
  39. private void btnCancle_Click(object sender, EventArgs e)
  40. {
  41. this.Close();
  42. }
  43. private void dgvDataMaintain_CellContentClick(object sender, DataGridViewCellEventArgs e)
  44. {
  45. //當GridView中的Button被按下
  46. var SenderGrid = (DataGridView)sender;
  47. if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
  48. {
  49. PickAccountingSubject pkItemForm = new PickAccountingSubject();
  50. pkItemForm.intIndex = e.RowIndex;
  51. pkItemForm.strOwnerForm = "XMLSetting";
  52. pkItemForm.Owner = this;
  53. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  54. pkItemForm.ShowDialog();
  55. }
  56. }
  57. private void dgvDataMaintain_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
  58. {
  59. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvDataMaintain.Rows[e.RowIndex].Cells["cDC"];
  60. if (cbCell.Items.Count == 0)
  61. {
  62. cbCell.DataSource = UtilityClass.GetSystemArgument("DCClass", "zh-TW").Tables["Arguments"];
  63. cbCell.DisplayMember = "ArgumentValue";
  64. cbCell.ValueMember = "ArgumentID";
  65. }
  66. }
  67. private void dgvDataMaintain_CellEnter(object sender, DataGridViewCellEventArgs e)
  68. {
  69. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvDataMaintain.Rows[e.RowIndex].Cells["cDC"];
  70. if (cbCell.Items.Count == 0)
  71. {
  72. cbCell.DataSource = UtilityClass.GetSystemArgument("DCClass", "zh-TW").Tables["Arguments"];
  73. cbCell.DisplayMember = "ArgumentValue";
  74. cbCell.ValueMember = "ArgumentID";
  75. }
  76. }
  77. private void btnClean_Click(object sender, EventArgs e)
  78. {
  79. CleanForm();
  80. }
  81. private void dgvDataMaintain_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  82. {
  83. //宣告物件
  84. string strErrorMsg = "";
  85. string strDC = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDC"].Value;
  86. if (!dgvDataMaintain.Rows[e.RowIndex].IsNewRow)
  87. {
  88. if ((string.IsNullOrEmpty(strDC.Trim()) ? "" : strDC) == "")
  89. {
  90. strErrorMsg = "請選擇借貸方";
  91. }
  92. }
  93. if (strErrorMsg != "")
  94. {
  95. dgvDataMaintain.Rows[e.RowIndex].ErrorText = strErrorMsg;
  96. e.Cancel = true;
  97. }
  98. }
  99. private void dgvDataMaintain_RowValidated(object sender, DataGridViewCellEventArgs e)
  100. {
  101. dgvDataMaintain.Rows[e.RowIndex].ErrorText = "";
  102. }
  103. private void btnConfirm_Click(object sender, EventArgs e)
  104. {
  105. try
  106. {
  107. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString();
  108. XElement xmlRoot = new XElement("Root");
  109. foreach (DataGridViewRow drData in dgvDataMaintain.Rows)
  110. {
  111. if (!drData.IsNewRow)
  112. {
  113. XElement xmlContent = new XElement("Accounting");
  114. xmlRoot.Add(xmlContent);
  115. XElement xmlSub = new XElement("SubAccounting", drData.Cells[1].Value.ToString());
  116. xmlSub.Add(new XAttribute("SubName", drData.Cells[2].Value.ToString()));
  117. xmlSub.Add(new XAttribute("DC", drData.Cells[3].Value.ToString()));
  118. xmlContent.Add(xmlSub);
  119. }
  120. }
  121. if (!File.Exists(strXMLPath))
  122. {
  123. UtilityClass.CreateDir(strXMLPath);
  124. }
  125. xmlRoot.Save(strXMLPath + strXMLName + ".xml");
  126. MessageBox.Show("設定完成", "提示");
  127. this.Close();
  128. }
  129. catch (Exception ex)
  130. {
  131. MessageBox.Show("設定錯誤", "提示");
  132. ErrorHandler.WriteErrorLog("XMLSetting.cs", ex);
  133. this.Close();
  134. }
  135. }
  136. private void XMLSetting_Load(object sender, EventArgs e)
  137. {
  138. strAccountingBookID = MainForm.strAccountingBookID;
  139. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + strXMLName + ".xml";
  140. if (File.Exists(strXMLPath))
  141. {
  142. XDocument xmlContent = XDocument.Load(strXMLPath);
  143. //foreach (var xmlData in xmlContent.Descendants("Accounting"))
  144. foreach (XElement xmlData in xmlContent.Descendants("Accounting"))
  145. {
  146. DataGridViewRow dgvRow = new DataGridViewRow();
  147. dgvRow.CreateCells(dgvDataMaintain);
  148. dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString();
  149. dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString();
  150. dgvRow.Cells[3].Value = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString();
  151. dgvDataMaintain.Rows.Add(dgvRow);
  152. }
  153. }
  154. }
  155. #endregion
  156. private void dgvDataMaintain_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  157. {
  158. //物件宣告
  159. string strAccountingSubID = "";
  160. string strAccountingSubName = "";
  161. //Cell[0] : 選窗Button
  162. //Cell[1] : 科目代碼(cAccountingSubID)
  163. //Cell[2] : 科目名稱(cAccountingSubName)
  164. //Cell[3] : 摘要(cMemo)
  165. //Cell[4] : 借方金額(cDebit)
  166. //Cell[5] : 貸方金額(cCredit)
  167. //科目代碼變動
  168. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cAccountingSubID")
  169. {
  170. strAccountingSubID = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value;
  171. if (string.IsNullOrEmpty(strAccountingSubID))
  172. {
  173. return;
  174. }
  175. else
  176. {
  177. string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubID = '{0}'", strAccountingSubID);
  178. if (UtilityClass.IsExist(strCheckSQL))
  179. {
  180. strAccountingSubName = UtilityClass.GetAccountingSubjectByID(strAccountingSubID, strAccountingBookID).Rows[0]["AccountingSubName"].ToString();
  181. dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value = strAccountingSubName;
  182. }
  183. else
  184. {
  185. PickAccountingSubject pkItemForm = new PickAccountingSubject();
  186. pkItemForm.txtAccountingSubID.Text = strAccountingSubID;
  187. pkItemForm.intIndex = e.RowIndex;
  188. pkItemForm.strOwnerForm = "XMLSetting";
  189. pkItemForm.Owner = this;
  190. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  191. pkItemForm.ShowDialog();
  192. }
  193. }
  194. }
  195. //科目名稱變動
  196. if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cAccountingSubName")
  197. {
  198. strAccountingSubName = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value;
  199. if (string.IsNullOrEmpty(strAccountingSubName))
  200. {
  201. return;
  202. }
  203. else
  204. {
  205. string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubName = '{0}'", strAccountingSubName);
  206. if (UtilityClass.IsExist(strCheckSQL))
  207. {
  208. strAccountingSubID = UtilityClass.GetAccountingSubjectByName(strAccountingSubName).Rows[0]["AccountingSubID"].ToString();
  209. dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value = strAccountingSubID;
  210. }
  211. else
  212. {
  213. PickAccountingSubject pkItemForm = new PickAccountingSubject();
  214. pkItemForm.txtAccountingSubName.Text = strAccountingSubName;
  215. pkItemForm.intIndex = e.RowIndex;
  216. pkItemForm.strOwnerForm = "XMLSetting";
  217. pkItemForm.Owner = this;
  218. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  219. pkItemForm.ShowDialog();
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }