using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.Sql; using System.Data.SqlClient; using ManagementSystem.Utility; namespace ManagementSystem { public partial class CashFlowForecastEdit : Form { //程式內共用物件 public string strFrmStatus = ""; //表單狀態 public string strPKey = ""; //程式內的Key值 public string strOwnerForm = ""; //呼叫的Form Name public string strKey = ""; //程式內加密的Key值 public string strActiveUserID = ""; //取得登入作用的使用者帳號 public string strAccountingBookID = ""; //取得作用中的帳本 private string strInputType = "Single"; //輸入模式 Single:單次 ;Cycle_T:次數循環 ;Cycle_D:日期區間循環 SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID); SqlTransaction sqlTran; SqlDataAdapter sqlAdapter = new SqlDataAdapter(); SqlCommand sqlCmd = new SqlCommand(); public CashFlowForecastEdit() { InitializeComponent(); } #region 自定義程式 private void SetupStatus() //畫面載入設定 { try { strActiveUserID = MainForm.strActiveUserID; strAccountingBookID = MainForm.strAccountingBookID; strKey = MainForm.strKey; dpStartDate.Enabled = false; dpForcastDate.Enabled = true; } catch (Exception ex) { ErrorHandler.WriteErrorLog("CashFlowForecastEdit.cs", ex); } } private void CleanForm() //清除畫面資料 { rbForecastType.Checked = true; rbForecastType1.Checked = false; dpStartDate.Text = ""; txtCycleTimes.Text = ""; dpForcastDate.Text = ""; txtCustomerName.Text = ""; txtCustomerID.Text = ""; txtProjectName.Text = ""; txtProjectNumber.Text = ""; txtMemo.Text = ""; txtInvoiceNo.Text = ""; txtForecastIncome.Text = ""; txtForecastExpand.Text = ""; } private void HindObject() //依照新增的狀態顯示對應物件 { if (rbForecastType.Checked) { dpStartDate.Enabled = false; txtCycleTimes.Enabled = false; dpForcastDate.Enabled = true; txtInvoiceNo.Enabled = true; dpEndDate.Enabled = false; strInputType = "Single"; dpForcastDate.Focus(); } else if (rbForecastType1.Checked) { dpStartDate.Enabled = true; txtCycleTimes.Enabled = true; dpForcastDate.Enabled = false; txtInvoiceNo.Enabled = false; dpEndDate.Enabled = false; strInputType = "Cycle_T"; dpStartDate.Focus(); } else if(rbForecastType2.Checked) { dpStartDate.Enabled = true; txtCycleTimes.Enabled = false; dpForcastDate.Enabled = false; txtInvoiceNo.Enabled = false; dpEndDate.Enabled = true; strInputType = "Cycle_D"; dpStartDate.Focus(); } } private string CheckForm(string strType) //循環輸入檢查 { string strReturnMessage = ""; try { if (txtMemo.Text.Trim() == "") { strReturnMessage = "備註不得為空!"; txtMemo.Focus(); } if (txtForecastIncome.Text.Trim() != "" && txtForecastExpand.Text.Trim() != "") { strReturnMessage = "預估收入及預估支出不得均有值!"; txtForecastIncome.Focus(); } if (txtForecastIncome.Text.Trim() == "" && txtForecastExpand.Text.Trim() == "") { strReturnMessage = "預估收入及預估支出不得均為空!"; txtForecastIncome.Focus(); } else { if (!UtilityClass.IsNumber(txtForecastIncome.Text.Trim().Replace(",", "")) && txtForecastIncome.Text.Trim() != "") { strReturnMessage = "預估收入數值格式有誤!"; txtForecastIncome.Focus(); } if (!UtilityClass.IsNumber(txtForecastExpand.Text.Trim().Replace(",", "")) && txtForecastExpand.Text.Trim() != "") { strReturnMessage = "預估支出數值格式有誤!"; txtForecastExpand.Focus(); } } if (strType.ToUpper().Substring(0,5) == "CYCLE") { if (dpStartDate.Text.Trim() == "") { strReturnMessage = "開始計算日期不得為空!"; dpStartDate.Focus(); } if (!UtilityClass.IsDate(dpStartDate.Text.Trim())) { strReturnMessage = "開始計算日期格式有錯!"; dpStartDate.Focus(); } } if (strType.ToUpper() == "CYCLE_T") //次數循環模式驗證 { if (!UtilityClass.IsNumber(txtCycleTimes.Text.Trim())) { strReturnMessage = "周期次數的值必需為數字!"; txtCycleTimes.Focus(); } } else if(strType.ToUpper() == "CYCLE_D") //日期區間循環模式驗證 { if (dpEndDate.Text.Trim() == "") { strReturnMessage = "結束計算日期不得為空!"; dpEndDate.Focus(); } if (!UtilityClass.IsDate(dpEndDate.Text.Trim())) { strReturnMessage = "結束計算日期格式有錯!"; dpEndDate.Focus(); } } return strReturnMessage; } catch { return "檢測有誤!"; } } private void AddEven() //新增事件 { try { //宣告物件 string strIncome = ""; string strExpand = ""; StringBuilder strSQL = new StringBuilder(); switch (strInputType.ToUpper()) { case "SINGLE": //單次執行模式 strSQL.Clear(); strSQL.Append("Insert Into OTB_FNC_CashFlowForecast(AccountingBookID, ItemNo, ForecastDate, ForecastIncome, ForecastExpand, CustomerName, Memo, InvoiceNo, ProjectNumber, ProjectCName ,CreateDate, CreateUser, ModifyDate, ModifyUser) Values ('"); strSQL.Append(strAccountingBookID + "',NEWID(),"); strSQL.Append("'" + dpForcastDate.Text.Trim() + "',"); strIncome = txtForecastIncome.Text.Trim(); strSQL.Append(string.IsNullOrEmpty(strIncome) ? "''," : "'" + UtilityClass.EncryptDES(strIncome, strKey) + "',"); strExpand = (string)txtForecastExpand.Text.Trim(); strSQL.Append(string.IsNullOrEmpty(strExpand) ? "''," : "'" + UtilityClass.EncryptDES(strExpand, strKey) + "',"); strSQL.Append("'" + txtCustomerName.Text.Trim() + "',"); strSQL.Append("'" + txtMemo.Text.Trim() + "',"); strSQL.Append("'" + txtInvoiceNo.Text.Trim() + "',"); strSQL.Append("'" + txtProjectNumber.Text.Trim() + "',"); strSQL.Append("'" + txtProjectName.Text.Trim() + "',"); strSQL.Append("Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "'); "); break; case "CYCLE_T": //循環執行模式(次數) strSQL.Clear(); DateTime dtCountDate = Convert.ToDateTime(dpStartDate.Text); DateTime dtPayDate = DateTime.Now; for (int intTimes = 0; intTimes <= Convert.ToInt32(txtCycleTimes.Text.ToString()); intTimes++) { //計算付款日期 Start dtPayDate = dtCountDate.AddMonths(intTimes); //計算付款日期 End strSQL.Append("Insert Into OTB_FNC_CashFlowForecast(AccountingBookID, ItemNo, ForecastDate, ForecastIncome, ForecastExpand, CustomerName, Memo, ProjectNumber, ProjectCName ,CreateDate, CreateUser, ModifyDate, ModifyUser) Values ('"); strSQL.Append(strAccountingBookID + "',NEWID(),"); strSQL.Append("'" + dtPayDate.ToShortDateString() + "',"); strIncome = txtForecastIncome.Text.Trim(); strSQL.Append(string.IsNullOrEmpty(strIncome) ? "''," : "'" + UtilityClass.EncryptDES(strIncome, strKey) + "',"); strExpand = (string)txtForecastExpand.Text.Trim(); strSQL.Append(string.IsNullOrEmpty(strExpand) ? "''," : "'" + UtilityClass.EncryptDES(strExpand, strKey) + "',"); strSQL.Append("'" + txtCustomerName.Text.Trim() + "',"); strSQL.Append("'" + txtMemo.Text.Trim() + "',"); strSQL.Append("'" + txtProjectNumber.Text.Trim() + "',"); strSQL.Append("'" + txtProjectName.Text.Trim() + "',"); strSQL.Append("Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "'); "); } break; case "CYCLE_D": //循環執行模式(周期) strSQL.Clear(); //DateTime dtStartDate = Convert.ToDateTime(dpStartDate.Text); DateTime dtCountDate1 = Convert.ToDateTime(dpStartDate.Text); DateTime dtEndDate = Convert.ToDateTime(dpEndDate.Text); DateTime dtPayDate1 = Convert.ToDateTime(dpStartDate.Text); int intAddCount = 0; while (dtCountDate1 <= dtEndDate) { dtPayDate1 = Convert.ToDateTime(dpStartDate.Text).AddMonths(intAddCount); strSQL.Append("Insert Into OTB_FNC_CashFlowForecast(AccountingBookID, ItemNo, ForecastDate, ForecastIncome, ForecastExpand, CustomerName, Memo, ProjectNumber, ProjectCName ,CreateDate, CreateUser, ModifyDate, ModifyUser) Values ('"); strSQL.Append(strAccountingBookID + "',NEWID(),"); strSQL.Append("'" + dtPayDate1.ToShortDateString() + "',"); strIncome = txtForecastIncome.Text.Trim(); strSQL.Append(string.IsNullOrEmpty(strIncome) ? "''," : "'" + UtilityClass.EncryptDES(strIncome, strKey) + "',"); strExpand = (string)txtForecastExpand.Text.Trim(); strSQL.Append(string.IsNullOrEmpty(strExpand) ? "''," : "'" + UtilityClass.EncryptDES(strExpand, strKey) + "',"); strSQL.Append("'" + txtCustomerName.Text.Trim() + "',"); strSQL.Append("'" + txtMemo.Text.Trim() + "',"); strSQL.Append("'" + txtProjectNumber.Text.Trim() + "',"); strSQL.Append("'" + txtProjectName.Text.Trim() + "',"); strSQL.Append("Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "'); "); dtCountDate1 = dtCountDate1.AddMonths(1); intAddCount += 1; } break; } using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.InsertCommand = new SqlCommand(); sqlAdapter.InsertCommand.Connection = sqlConn; sqlAdapter.InsertCommand.CommandText = strSQL.ToString(); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.InsertCommand.ExecuteNonQuery(); MessageBox.Show("資料新增成功", "提示"); } } catch (Exception ex) { MessageBox.Show("資料新增失敗", "提示"); ErrorHandler.WriteErrorLog("CashFlowForecastEdit.cs", ex); } } public void ReturnCustomer(string[] strCustomerData) //回傳客戶資料 { txtCustomerID.Text = strCustomerData[0].ToString(); txtCustomerName.Text = strCustomerData[1].ToString(); } public void ReturnProjectInfo(string[] strProjectInfo) //回傳專案資料 { txtProjectName.Text = strProjectInfo[1].ToString(); txtProjectNumber.Text = strProjectInfo[0].ToString(); } #endregion #region 事件觸發及問題處理 private void CashFlowForecastEdit_Load(object sender, EventArgs e) { SetupStatus(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void btnClean_Click(object sender, EventArgs e) { CleanForm(); } private void txtForecastIncome_Enter(object sender, EventArgs e) { if (txtForecastIncome.Text.Trim() != "") { txtForecastIncome.Text = txtForecastIncome.Text.Trim().Replace(",", ""); } } private void txtForecastIncome_Leave(object sender, EventArgs e) { if (txtForecastIncome.Text.Trim() != "") { txtForecastIncome.Text = UtilityClass.MarkNumber(txtForecastIncome.Text.Trim()); } } private void txtForecastExpand_Enter(object sender, EventArgs e) { if (txtForecastExpand.Text.Trim() != "") { txtForecastExpand.Text = txtForecastExpand.Text.Trim().Replace(",", ""); } } private void txtForecastExpand_Leave(object sender, EventArgs e) { if (txtForecastExpand.Text.Trim() != "") { txtForecastExpand.Text = UtilityClass.MarkNumber(txtForecastExpand.Text.Trim()); } } private void btnSave_Click(object sender, EventArgs e) { string strCheckResult = CheckForm(strInputType); if (strCheckResult == "") { switch (strFrmStatus.ToUpper()) { case "ADD": AddEven(); break; } ((CashFlowForecast)this.Owner).SetupStatus(); //進行母Form查詢功能 this.Close(); } else { MessageBox.Show(strCheckResult, "提示"); } } private void rbForecastType_CheckedChanged(object sender, EventArgs e) { HindObject(); } private void rbForecastType1_CheckedChanged(object sender, EventArgs e) { HindObject(); } private void rbForecastType2_CheckedChanged(object sender, EventArgs e) { HindObject(); } private void btnGetCustomer_Click(object sender, EventArgs e) { PickCustomer pkItemForm = new PickCustomer(); pkItemForm.txtCustomerName.Text = txtCustomerName.Text.Trim(); pkItemForm.strOwnerForm = "CashFlowForecastEdit"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } private void btnGetProject_Click(object sender, EventArgs e) { PickProject pkItemForm = new PickProject(); pkItemForm.txtProjectName.Text = txtProjectName.Text.Trim(); pkItemForm.strOwnerForm = "CashFlowForecastEdit"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } #endregion } }