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.

419 lines
17 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 CashFlowForecastEdit : 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. private string strInputType = "Single"; //輸入模式 Single:單次 ;Cycle_T:次數循環 ;Cycle_D:日期區間循環
  25. SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
  26. SqlTransaction sqlTran;
  27. SqlDataAdapter sqlAdapter = new SqlDataAdapter();
  28. SqlCommand sqlCmd = new SqlCommand();
  29. public CashFlowForecastEdit()
  30. {
  31. InitializeComponent();
  32. }
  33. #region 自定義程式
  34. private void SetupStatus() //畫面載入設定
  35. {
  36. try
  37. {
  38. strActiveUserID = MainForm.strActiveUserID;
  39. strAccountingBookID = MainForm.strAccountingBookID;
  40. strKey = MainForm.strKey;
  41. dpStartDate.Enabled = false;
  42. dpForcastDate.Enabled = true;
  43. }
  44. catch (Exception ex)
  45. {
  46. ErrorHandler.WriteErrorLog("CashFlowForecastEdit.cs", ex);
  47. }
  48. }
  49. private void CleanForm() //清除畫面資料
  50. {
  51. rbForecastType.Checked = true;
  52. rbForecastType1.Checked = false;
  53. dpStartDate.Text = "";
  54. txtCycleTimes.Text = "";
  55. dpForcastDate.Text = "";
  56. txtCustomerName.Text = "";
  57. txtCustomerID.Text = "";
  58. txtProjectName.Text = "";
  59. txtProjectNumber.Text = "";
  60. txtMemo.Text = "";
  61. txtInvoiceNo.Text = "";
  62. txtForecastIncome.Text = "";
  63. txtForecastExpand.Text = "";
  64. }
  65. private void HindObject() //依照新增的狀態顯示對應物件
  66. {
  67. if (rbForecastType.Checked)
  68. {
  69. dpStartDate.Enabled = false;
  70. txtCycleTimes.Enabled = false;
  71. dpForcastDate.Enabled = true;
  72. txtInvoiceNo.Enabled = true;
  73. dpEndDate.Enabled = false;
  74. strInputType = "Single";
  75. dpForcastDate.Focus();
  76. }
  77. else if (rbForecastType1.Checked)
  78. {
  79. dpStartDate.Enabled = true;
  80. txtCycleTimes.Enabled = true;
  81. dpForcastDate.Enabled = false;
  82. txtInvoiceNo.Enabled = false;
  83. dpEndDate.Enabled = false;
  84. strInputType = "Cycle_T";
  85. dpStartDate.Focus();
  86. }
  87. else if(rbForecastType2.Checked)
  88. {
  89. dpStartDate.Enabled = true;
  90. txtCycleTimes.Enabled = false;
  91. dpForcastDate.Enabled = false;
  92. txtInvoiceNo.Enabled = false;
  93. dpEndDate.Enabled = true;
  94. strInputType = "Cycle_D";
  95. dpStartDate.Focus();
  96. }
  97. }
  98. private string CheckForm(string strType) //循環輸入檢查
  99. {
  100. string strReturnMessage = "";
  101. try
  102. {
  103. if (txtMemo.Text.Trim() == "")
  104. {
  105. strReturnMessage = "備註不得為空!";
  106. txtMemo.Focus();
  107. }
  108. if (txtForecastIncome.Text.Trim() != "" && txtForecastExpand.Text.Trim() != "")
  109. {
  110. strReturnMessage = "預估收入及預估支出不得均有值!";
  111. txtForecastIncome.Focus();
  112. }
  113. if (txtForecastIncome.Text.Trim() == "" && txtForecastExpand.Text.Trim() == "")
  114. {
  115. strReturnMessage = "預估收入及預估支出不得均為空!";
  116. txtForecastIncome.Focus();
  117. }
  118. else
  119. {
  120. if (!UtilityClass.IsNumber(txtForecastIncome.Text.Trim().Replace(",", "")) && txtForecastIncome.Text.Trim() != "")
  121. {
  122. strReturnMessage = "預估收入數值格式有誤!";
  123. txtForecastIncome.Focus();
  124. }
  125. if (!UtilityClass.IsNumber(txtForecastExpand.Text.Trim().Replace(",", "")) && txtForecastExpand.Text.Trim() != "")
  126. {
  127. strReturnMessage = "預估支出數值格式有誤!";
  128. txtForecastExpand.Focus();
  129. }
  130. }
  131. if (strType.ToUpper().Substring(0,5) == "CYCLE")
  132. {
  133. if (dpStartDate.Text.Trim() == "")
  134. {
  135. strReturnMessage = "開始計算日期不得為空!";
  136. dpStartDate.Focus();
  137. }
  138. if (!UtilityClass.IsDate(dpStartDate.Text.Trim()))
  139. {
  140. strReturnMessage = "開始計算日期格式有錯!";
  141. dpStartDate.Focus();
  142. }
  143. }
  144. if (strType.ToUpper() == "CYCLE_T") //次數循環模式驗證
  145. {
  146. if (!UtilityClass.IsNumber(txtCycleTimes.Text.Trim()))
  147. {
  148. strReturnMessage = "周期次數的值必需為數字!";
  149. txtCycleTimes.Focus();
  150. }
  151. }
  152. else if(strType.ToUpper() == "CYCLE_D") //日期區間循環模式驗證
  153. {
  154. if (dpEndDate.Text.Trim() == "")
  155. {
  156. strReturnMessage = "結束計算日期不得為空!";
  157. dpEndDate.Focus();
  158. }
  159. if (!UtilityClass.IsDate(dpEndDate.Text.Trim()))
  160. {
  161. strReturnMessage = "結束計算日期格式有錯!";
  162. dpEndDate.Focus();
  163. }
  164. }
  165. return strReturnMessage;
  166. }
  167. catch
  168. {
  169. return "檢測有誤!";
  170. }
  171. }
  172. private void AddEven() //新增事件
  173. {
  174. try
  175. {
  176. //宣告物件
  177. string strIncome = "";
  178. string strExpand = "";
  179. StringBuilder strSQL = new StringBuilder();
  180. switch (strInputType.ToUpper())
  181. {
  182. case "SINGLE": //單次執行模式
  183. strSQL.Clear();
  184. strSQL.Append("Insert Into OTB_FNC_CashFlowForecast(AccountingBookID, ItemNo, ForecastDate, ForecastIncome, ForecastExpand, CustomerName, Memo, InvoiceNo, ProjectNumber, ProjectCName ,CreateDate, CreateUser, ModifyDate, ModifyUser) Values ('");
  185. strSQL.Append(strAccountingBookID + "',NEWID(),");
  186. strSQL.Append("'" + dpForcastDate.Text.Trim() + "',");
  187. strIncome = txtForecastIncome.Text.Trim();
  188. strSQL.Append(string.IsNullOrEmpty(strIncome) ? "''," : "'" + UtilityClass.EncryptDES(strIncome, strKey) + "',");
  189. strExpand = (string)txtForecastExpand.Text.Trim();
  190. strSQL.Append(string.IsNullOrEmpty(strExpand) ? "''," : "'" + UtilityClass.EncryptDES(strExpand, strKey) + "',");
  191. strSQL.Append("'" + txtCustomerName.Text.Trim() + "',");
  192. strSQL.Append("'" + txtMemo.Text.Trim() + "',");
  193. strSQL.Append("'" + txtInvoiceNo.Text.Trim() + "',");
  194. strSQL.Append("'" + txtProjectNumber.Text.Trim() + "',");
  195. strSQL.Append("'" + txtProjectName.Text.Trim() + "',");
  196. strSQL.Append("Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "'); ");
  197. break;
  198. case "CYCLE_T": //循環執行模式(次數)
  199. strSQL.Clear();
  200. DateTime dtCountDate = Convert.ToDateTime(dpStartDate.Text);
  201. DateTime dtPayDate = DateTime.Now;
  202. for (int intTimes = 0; intTimes <= Convert.ToInt32(txtCycleTimes.Text.ToString()); intTimes++)
  203. {
  204. //計算付款日期 Start
  205. dtPayDate = dtCountDate.AddMonths(intTimes);
  206. //計算付款日期 End
  207. strSQL.Append("Insert Into OTB_FNC_CashFlowForecast(AccountingBookID, ItemNo, ForecastDate, ForecastIncome, ForecastExpand, CustomerName, Memo, ProjectNumber, ProjectCName ,CreateDate, CreateUser, ModifyDate, ModifyUser) Values ('");
  208. strSQL.Append(strAccountingBookID + "',NEWID(),");
  209. strSQL.Append("'" + dtPayDate.ToShortDateString() + "',");
  210. strIncome = txtForecastIncome.Text.Trim();
  211. strSQL.Append(string.IsNullOrEmpty(strIncome) ? "''," : "'" + UtilityClass.EncryptDES(strIncome, strKey) + "',");
  212. strExpand = (string)txtForecastExpand.Text.Trim();
  213. strSQL.Append(string.IsNullOrEmpty(strExpand) ? "''," : "'" + UtilityClass.EncryptDES(strExpand, strKey) + "',");
  214. strSQL.Append("'" + txtCustomerName.Text.Trim() + "',");
  215. strSQL.Append("'" + txtMemo.Text.Trim() + "',");
  216. strSQL.Append("'" + txtProjectNumber.Text.Trim() + "',");
  217. strSQL.Append("'" + txtProjectName.Text.Trim() + "',");
  218. strSQL.Append("Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "'); ");
  219. }
  220. break;
  221. case "CYCLE_D": //循環執行模式(周期)
  222. strSQL.Clear();
  223. //DateTime dtStartDate = Convert.ToDateTime(dpStartDate.Text);
  224. DateTime dtCountDate1 = Convert.ToDateTime(dpStartDate.Text);
  225. DateTime dtEndDate = Convert.ToDateTime(dpEndDate.Text);
  226. DateTime dtPayDate1 = Convert.ToDateTime(dpStartDate.Text);
  227. int intAddCount = 0;
  228. while (dtCountDate1 <= dtEndDate)
  229. {
  230. dtPayDate1 = Convert.ToDateTime(dpStartDate.Text).AddMonths(intAddCount);
  231. strSQL.Append("Insert Into OTB_FNC_CashFlowForecast(AccountingBookID, ItemNo, ForecastDate, ForecastIncome, ForecastExpand, CustomerName, Memo, ProjectNumber, ProjectCName ,CreateDate, CreateUser, ModifyDate, ModifyUser) Values ('");
  232. strSQL.Append(strAccountingBookID + "',NEWID(),");
  233. strSQL.Append("'" + dtPayDate1.ToShortDateString() + "',");
  234. strIncome = txtForecastIncome.Text.Trim();
  235. strSQL.Append(string.IsNullOrEmpty(strIncome) ? "''," : "'" + UtilityClass.EncryptDES(strIncome, strKey) + "',");
  236. strExpand = (string)txtForecastExpand.Text.Trim();
  237. strSQL.Append(string.IsNullOrEmpty(strExpand) ? "''," : "'" + UtilityClass.EncryptDES(strExpand, strKey) + "',");
  238. strSQL.Append("'" + txtCustomerName.Text.Trim() + "',");
  239. strSQL.Append("'" + txtMemo.Text.Trim() + "',");
  240. strSQL.Append("'" + txtProjectNumber.Text.Trim() + "',");
  241. strSQL.Append("'" + txtProjectName.Text.Trim() + "',");
  242. strSQL.Append("Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "'); ");
  243. dtCountDate1 = dtCountDate1.AddMonths(1);
  244. intAddCount += 1;
  245. }
  246. break;
  247. }
  248. using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
  249. {
  250. //添加參數
  251. sqlAdapter.InsertCommand = new SqlCommand();
  252. sqlAdapter.InsertCommand.Connection = sqlConn;
  253. sqlAdapter.InsertCommand.CommandText = strSQL.ToString();
  254. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  255. {
  256. sqlConn.Open();
  257. }
  258. sqlAdapter.InsertCommand.ExecuteNonQuery();
  259. MessageBox.Show("資料新增成功", "提示");
  260. }
  261. }
  262. catch (Exception ex)
  263. {
  264. MessageBox.Show("資料新增失敗", "提示");
  265. ErrorHandler.WriteErrorLog("CashFlowForecastEdit.cs", ex);
  266. }
  267. }
  268. public void ReturnCustomer(string[] strCustomerData) //回傳客戶資料
  269. {
  270. txtCustomerID.Text = strCustomerData[0].ToString();
  271. txtCustomerName.Text = strCustomerData[1].ToString();
  272. }
  273. public void ReturnProjectInfo(string[] strProjectInfo) //回傳專案資料
  274. {
  275. txtProjectName.Text = strProjectInfo[1].ToString();
  276. txtProjectNumber.Text = strProjectInfo[0].ToString();
  277. }
  278. #endregion
  279. #region 事件觸發及問題處理
  280. private void CashFlowForecastEdit_Load(object sender, EventArgs e)
  281. {
  282. SetupStatus();
  283. }
  284. private void btnClose_Click(object sender, EventArgs e)
  285. {
  286. this.Close();
  287. }
  288. private void btnClean_Click(object sender, EventArgs e)
  289. {
  290. CleanForm();
  291. }
  292. private void txtForecastIncome_Enter(object sender, EventArgs e)
  293. {
  294. if (txtForecastIncome.Text.Trim() != "")
  295. {
  296. txtForecastIncome.Text = txtForecastIncome.Text.Trim().Replace(",", "");
  297. }
  298. }
  299. private void txtForecastIncome_Leave(object sender, EventArgs e)
  300. {
  301. if (txtForecastIncome.Text.Trim() != "")
  302. {
  303. txtForecastIncome.Text = UtilityClass.MarkNumber(txtForecastIncome.Text.Trim());
  304. }
  305. }
  306. private void txtForecastExpand_Enter(object sender, EventArgs e)
  307. {
  308. if (txtForecastExpand.Text.Trim() != "")
  309. {
  310. txtForecastExpand.Text = txtForecastExpand.Text.Trim().Replace(",", "");
  311. }
  312. }
  313. private void txtForecastExpand_Leave(object sender, EventArgs e)
  314. {
  315. if (txtForecastExpand.Text.Trim() != "")
  316. {
  317. txtForecastExpand.Text = UtilityClass.MarkNumber(txtForecastExpand.Text.Trim());
  318. }
  319. }
  320. private void btnSave_Click(object sender, EventArgs e)
  321. {
  322. string strCheckResult = CheckForm(strInputType);
  323. if (strCheckResult == "")
  324. {
  325. switch (strFrmStatus.ToUpper())
  326. {
  327. case "ADD":
  328. AddEven();
  329. break;
  330. }
  331. ((CashFlowForecast)this.Owner).SetupStatus(); //進行母Form查詢功能
  332. this.Close();
  333. }
  334. else
  335. {
  336. MessageBox.Show(strCheckResult, "提示");
  337. }
  338. }
  339. private void rbForecastType_CheckedChanged(object sender, EventArgs e)
  340. {
  341. HindObject();
  342. }
  343. private void rbForecastType1_CheckedChanged(object sender, EventArgs e)
  344. {
  345. HindObject();
  346. }
  347. private void rbForecastType2_CheckedChanged(object sender, EventArgs e)
  348. {
  349. HindObject();
  350. }
  351. private void btnGetCustomer_Click(object sender, EventArgs e)
  352. {
  353. PickCustomer pkItemForm = new PickCustomer();
  354. pkItemForm.txtCustomerName.Text = txtCustomerName.Text.Trim();
  355. pkItemForm.strOwnerForm = "CashFlowForecastEdit";
  356. pkItemForm.Owner = this;
  357. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  358. pkItemForm.ShowDialog();
  359. }
  360. private void btnGetProject_Click(object sender, EventArgs e)
  361. {
  362. PickProject pkItemForm = new PickProject();
  363. pkItemForm.txtProjectName.Text = txtProjectName.Text.Trim();
  364. pkItemForm.strOwnerForm = "CashFlowForecastEdit";
  365. pkItemForm.Owner = this;
  366. pkItemForm.StartPosition = FormStartPosition.CenterParent;
  367. pkItemForm.ShowDialog();
  368. }
  369. #endregion
  370. }
  371. }