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 System.Configuration; using System.IO; using System.Xml.Linq; using ManagementSystem.Utility; namespace ManagementSystem { public partial class AccountsPayable : Form { //程式內共用物件 public bool blIsAdded = true; //判斷分錄是否寫入成功 string strFrmStatus = ""; //表單狀態 string strActiveUserID = ""; //取得登入作用的使用者帳號 string strAccountingBookID = ""; //取得作用中的帳本 SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID); SqlTransaction sqlTran; SqlCommand sqlCmd = new SqlCommand(); DataSet sdInsurance = new System.Data.DataSet(); string strKey = ""; //程式內加密的Key值 public AccountsPayable() { InitializeComponent(); } #region 自定義程式 private void SetupStatus() //畫面載入設定 { try { strActiveUserID = MainForm.strActiveUserID; strAccountingBookID = MainForm.strAccountingBookID; strKey = MainForm.strKey; if (MainForm.strKey == "") { //設定Toolbar初始狀態 tsbAdd.Enabled = false; tsbSave.Enabled = false; tsbDelete.Enabled = false; } //設定畫面初始狀態 StatusChange("NONE"); } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex); } } private void CleanForm() { txtCustomerName.Text = ""; txtProjectName.Text = ""; txtPaiedDateStart.Text = ""; txtPaiedDateEnd.Text = ""; txtInvoiceNo.Text = ""; cbHistory.Checked = false; } private void CleanToolbar() //清除工具列 { //設定Toolbar狀態 tsbSearch.Visible = true; tsbSearch.Enabled = true; tsbAdd.Visible = false; //本程式不存在新功能 tsbEdit.Visible = false; tsbEdit.Enabled = false; tsbDelete.Visible = false; tsbDelete.Enabled = false; tsbSave.Visible = false; tsbOK.Visible = false; tsbCancel.Visible = false; } private void LockForm() //限制唯讀物件 { txtCustomerName.ReadOnly = true; txtProjectName.ReadOnly = true; txtPaiedDateEnd.ReadOnly = true; txtPaiedDateStart.ReadOnly = true; txtInvoiceNo.ReadOnly = true; cbHistory.Enabled = false; } private void UnLockForm() //解除限制唯讀物件 { txtCustomerName.ReadOnly = false; txtProjectName.ReadOnly = false; txtPaiedDateEnd.ReadOnly = false; txtPaiedDateStart.ReadOnly = false; txtInvoiceNo.ReadOnly = false; cbHistory.Enabled = true; } private bool CheckForm() { if (!UtilityClass.IsDate(txtPaiedDateStart.Text.Trim()) && txtPaiedDateStart.Text.Trim() != "") { MessageBox.Show("起始時間格式有誤!", "提示"); txtPaiedDateStart.Focus(); return false; } if (!UtilityClass.IsDate(txtPaiedDateEnd.Text.Trim()) && txtPaiedDateEnd.Text.Trim() != "") { MessageBox.Show("結束時間格式有誤!", "提示"); txtPaiedDateEnd.Focus(); return false; } return true; } public void StatusChange(string strStatus) //變更主畫面狀態 { try { switch (strStatus.ToUpper()) { case "NONE": CleanForm(); CleanToolbar(); LockForm(); ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = ""; strFrmStatus = ""; tsbSearch.Enabled = true; break; case "SEARCH": UnLockForm(); CleanForm(); tsbSearch.Enabled = false; tsbEdit.Enabled = false; tsbDelete.Enabled = false; tsbSave.Visible = false; tsbOK.Visible = true; tsbCancel.Visible = true; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋"; strFrmStatus = "SEARCH"; break; case "ADD": //本程式不提供新增功能 ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增"; strFrmStatus = "ADD"; break; case "MODIFY": //本程式不提供修改功能 ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改"; strFrmStatus = "MODIFY"; break; case "DEL": ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除"; strFrmStatus = "DEL"; break; } UtilityClass.SetGridColor(dgvAPManagement); } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex); } } public void GoEvent() //執行事件 { try { //物件宣告 StringBuilder sbSQL = new StringBuilder(); string strSalesOrderNo = ""; //訂單編號 string strProjectCName = ""; //專案中文名稱 string strProjectNumber = ""; //專案編號 string strInvoiceNo = ""; //統一編號 string strCustomerID = ""; //客戶編號 string strCustomerName = ""; //客戶名稱 string strActualPaiedDate = ""; //付款日期 string strPayAmount = ""; //付款金額 string strPayStatus = ""; //付款狀態 string strMemo = ""; //備註 switch (strFrmStatus) { case "SEARCH": dgvAPManagement.Rows.Clear(); sbSQL.Clear(); sbSQL.Append("Select SalesOrderNo, ProjectCName, ProjectNumber, InvoiceNo, CustomerID, CustomerName, ActualPaiedDate , PayAmount, PayStatus, Memo From OTB_FNC_AccountPayable "); sbSQL.Append(" Where AccountingBookID ='" + strAccountingBookID + "' "); if (txtInvoiceNo.Text.Trim() != "") { sbSQL.Append("AND InvoiceNo like '" + txtInvoiceNo.Text.Trim() + "' "); } if (txtCustomerName.Text.Trim() != "") { sbSQL.Append("AND CustomerName like '" + txtCustomerName.Text.Trim() + "' "); } if (txtProjectName.Text.Trim() != "") { sbSQL.Append("AND ProjectName like '" + txtProjectName.Text.Trim() + "' "); } if (txtPaiedDateStart.Text.Trim() != "") { sbSQL.Append("AND ActualPaiedDate >= '" + txtPaiedDateStart.Text.Trim() + "' "); } if (txtPaiedDateEnd.Text.Trim() != "") { sbSQL.Append("AND ActualPaiedDate <= '" + txtPaiedDateEnd.Text.Trim() + "' "); } if (cbHistory.Checked == true) { sbSQL.Append("And PayStatus = 'Y' "); } else { sbSQL.Append("And PayStatus = 'N' "); } DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"]; foreach (DataRow drData in dtTemp.Rows) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(dgvAPManagement); //逐行產生資料 strSalesOrderNo = (string)drData["SalesOrderNo"]; strProjectCName = (string)drData["ProjectCName"]; strProjectNumber = (string)drData["ProjectNumber"]; strInvoiceNo = (string)drData["InvoiceNo"]; strCustomerID = (string)drData["CustomerID"]; strCustomerName = (string)drData["CustomerName"]; if (drData["ActualPaiedDate"] != DBNull.Value) { strActualPaiedDate = Convert.ToDateTime(drData["ActualPaiedDate"]).ToShortDateString(); } strPayAmount = (string)drData["PayAmount"]; strPayStatus = (string)drData["PayStatus"]; strMemo = (string)drData["Memo"]; //產生Row dgvRow.Cells[1].Value = string.IsNullOrEmpty(strSalesOrderNo) ? "" : strSalesOrderNo; dgvRow.Cells[2].Value = string.IsNullOrEmpty(strProjectCName) ? "" : strProjectCName; dgvRow.Cells[3].Value = string.IsNullOrEmpty(strProjectNumber) ? "" : strProjectNumber; dgvRow.Cells[4].Value = string.IsNullOrEmpty(strCustomerID) ? "" : strCustomerID; dgvRow.Cells[5].Value = string.IsNullOrEmpty(strCustomerName) ? "" : strCustomerName; dgvRow.Cells[6].Value = string.IsNullOrEmpty(strInvoiceNo) ? "" : strInvoiceNo; dgvRow.Cells[7].Value = strActualPaiedDate; dgvRow.Cells[8].Value = string.IsNullOrEmpty(strPayAmount) ? "" : UtilityClass.DecryptDES(strPayAmount, strKey); dgvRow.Cells[9].Value = string.IsNullOrEmpty(strMemo) ? "" : strMemo; dgvRow.Cells[10].Value = string.IsNullOrEmpty(strPayStatus) ? "" : strPayStatus; if (drData["PayStatus"].ToString() == "Y") { dgvRow.ReadOnly = true; } dgvAPManagement.Rows.Add(dgvRow); } StatusChange("NONE"); break; case "MODIFY": //本程式不提供維護功能 break; } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex); sqlTran.Rollback(); } } public void UpdateAP(DataGridViewCellEventArgs e) //更新應收帳款記錄 { try { //物件宣告 string strInvoiceNo = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cInvoiceNo"].Value; string strPaiedDate = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value; using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { string strSQL = "Update OTB_FNC_AccountPayable Set ActualPaiedDate = @ActualPaiedDate , PayStatus = 'Y' , ModifyDate = GetDate(), ModifyUser = @ModifyUser Where AccountingBookID = @AccountingBookID And InvoiceNo = @InvoiceNo "; strSQL += "UPdate OTB_SAL_PayPlan Set ActualPayDate = @ActualPaiedDate, ModifyDate = GetDate(), ModifyUser = @ModifyUser Where AccountingBookID = @AccountingBookID And InvoiceNo = @InvoiceNo "; //添加參數 sqlAdapter.UpdateCommand = new SqlCommand(); sqlAdapter.UpdateCommand.Connection = sqlConn; sqlAdapter.UpdateCommand.Transaction = sqlTran; sqlAdapter.UpdateCommand.CommandText = strSQL; sqlAdapter.UpdateCommand.Parameters.AddRange ( new SqlParameter[] { new SqlParameter("@AccountingBookID",string.IsNullOrEmpty(strAccountingBookID) ? "" : strAccountingBookID), new SqlParameter("@InvoiceNo",string.IsNullOrEmpty(strInvoiceNo) ? "" : strInvoiceNo), new SqlParameter("@ModifyUser",strActiveUserID) } ); SqlParameter spActualPaiedDate = new SqlParameter("@ActualPaiedDate", SqlDbType.DateTime); spActualPaiedDate.Value = strPaiedDate; sqlAdapter.UpdateCommand.Parameters.Add(spActualPaiedDate); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.UpdateCommand.ExecuteNonQuery(); MessageBox.Show("核銷成功", "提示"); } } catch (Exception ex) { throw ex; } } #endregion #region 事件觸發及問題處理 private void AccountsPayable_Load(object sender, EventArgs e) { SetupStatus(); StatusChange("SEARCH"); GoEvent(); } private void tsbClean_Click(object sender, EventArgs e) { CleanForm(); } private void tsbCancel_Click(object sender, EventArgs e) { CleanToolbar(); StatusChange("NONE"); } private void tsbOK_Click(object sender, EventArgs e) { try { if (CheckForm()) { dgvAPManagement.Rows.Clear(); GoEvent(); //還原Toolbar狀態 CleanToolbar(); //關閉查詢條件 LockForm(); StatusChange("NONE"); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex); } } private void tsbSearch_Click(object sender, EventArgs e) { UnLockForm(); StatusChange("SEARCH"); txtCustomerName.Focus(); tsbOK.Visible = true; tsbCancel.Visible = true; } private void tsbSetup_Click(object sender, EventArgs e) { XMLSetting frmSettingForm = new XMLSetting(); frmSettingForm.Owner = this; frmSettingForm.strOwnerForm = this.Name.ToString(); frmSettingForm.strXMLName = this.Name.ToString(); frmSettingForm.StartPosition = FormStartPosition.CenterParent; frmSettingForm.ShowDialog(); } private void dgvAPManagement_CellContentClick(object sender, DataGridViewCellEventArgs e) { //物件宣告 string strDC = ""; string strInvoiceNo = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cInvoiceNo"].Value; string strPayAmount = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cPayAmount"].Value; string strProjectName = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cProjectName"].Value; string strProjectNumber = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cProjectNumber"].Value; try { //當GridView中的Button被按下 var SenderGrid = (DataGridView)sender; if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) { if (SenderGrid.Rows[e.RowIndex].ReadOnly == true) //判定唯讀則不處理 { return; } //Form設定 AccountingEntries acItemForm = new AccountingEntries(); acItemForm.strOwnerForm = "AccountsPay"; acItemForm.Owner = this; acItemForm.StartPosition = FormStartPosition.CenterParent; acItemForm.strFrmStatus = "ADD"; string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + this.Name.ToString() + ".xml"; if (File.Exists(strXMLPath)) { XDocument xmlContent = XDocument.Load(strXMLPath); foreach (XElement xmlData in xmlContent.Descendants("Accounting")) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(acItemForm.dgvDataMaintain); strDC = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString(); switch (strDC) { case "D": dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString(); dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString(); dgvRow.Cells[3].Value = "發票號碼:" + (string)dgvAPManagement.Rows[e.RowIndex].Cells["cInvoiceNo"].Value; dgvRow.Cells[4].Value = strPayAmount; break; case "C": dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString(); dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString(); dgvRow.Cells[3].Value = "發票號碼:" + (string)dgvAPManagement.Rows[e.RowIndex].Cells["cInvoiceNo"].Value; dgvRow.Cells[5].Value = strPayAmount; break; } blIsAdded = false; if (string.IsNullOrEmpty((string)dgvAPManagement.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value)) { dgvAPManagement.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value = DateTime.Now.ToString("yyyy/MM/dd"); } acItemForm.dgvDataMaintain.Rows.Add(dgvRow); } acItemForm.txtProjectName.Text = strProjectName; acItemForm.txtProjectNumber.Text = strProjectNumber; acItemForm.dgeArgs = e; } acItemForm.ShowDialog(); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsPayable.cs", ex); } } private void dgvAPManagement_RowValidated(object sender, DataGridViewCellEventArgs e) { dgvAPManagement.Rows[e.RowIndex].ErrorText = ""; } private void dgvAPManagement_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { string strActualPaiedDate = (string)dgvAPManagement.Rows[e.RowIndex].Cells["cActualPaiedDate"].Value; if (!string.IsNullOrEmpty(strActualPaiedDate)) { if (!UtilityClass.IsDate(strActualPaiedDate)) { dgvAPManagement.Rows[e.RowIndex].ErrorText = "日期格式不正確!"; e.Cancel = true; } } } private void tsbExit_Click(object sender, EventArgs e) { this.Close(); } #endregion } }