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 AccountsReceivable : 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 AccountsReceivable() { 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("AccountsReceivable.cs", ex); } } private void CleanForm() { txtCustomerName.Text = ""; txtReceiptDateStart.Text = ""; txtReceiptDateEnd.Text = ""; txtReceiptNo.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; txtReceiptDateEnd.ReadOnly = true; txtReceiptDateStart.ReadOnly = true; txtReceiptNo.ReadOnly = true; cbHistory.Enabled = false; } private void UnLockForm() //解除限制唯讀物件 { txtCustomerName.ReadOnly = false; txtReceiptDateEnd.ReadOnly = false; txtReceiptDateStart.ReadOnly = false; txtReceiptNo.ReadOnly = false; cbHistory.Enabled = 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(dgvARManagement); } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsReceivable.cs", ex); } } public void GoEvent() //執行事件 { try { //物件宣告 StringBuilder sbSQL = new StringBuilder(); string strSalesOrderNo = ""; //訂單編號 string strProjectCName = ""; //專案中文名稱 string strProjectNumber = ""; //專案編號 string strReceiptNo = ""; //統一編號 string strCustomerID = ""; //客戶編號 string strCustomerName = ""; //客戶名稱 DateTime dtActualReceiptDate; //請款日期 string strActualReceiptedDate; //收款日期 string strReceiptAmount = ""; //請款金額 string strReceiveStatus = ""; //請款狀態 string strMemo = ""; //備註 switch (strFrmStatus) { case "SEARCH": dgvARManagement.Rows.Clear(); sbSQL.Clear(); sbSQL.Append("Select SalesOrderNo, ProjectCName, ProjectNumber, ReceiptNo, CustomerID, CustomerName, ActualReceiptDate, ActualReceiptedDate , ReceiptAmount, ReceiveStatus, Memo From OTB_FNC_AccountReceivable "); sbSQL.Append(" Where AccountingBookID ='" + strAccountingBookID + "'"); if (txtReceiptNo.Text.Trim() != "") { sbSQL.Append("AND ReceiptNo like '" + txtReceiptNo.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 (txtReceiptDateStart.Text.Trim() != "") { sbSQL.Append("AND ActualReceiptDate >= '" + txtReceiptDateStart.Text.Trim() + "' "); } if (txtReceiptDateEnd.Text.Trim() != "") { sbSQL.Append("AND ActualReceiptDate <= '" + txtReceiptDateEnd.Text.Trim() + "' "); } if (cbHistory.Checked == true) { sbSQL.Append("And ReceiveStatus = 'Y' "); } else { sbSQL.Append("And ReceiveStatus = 'N' "); } sbSQL.Append("Order BY ActualReceiptDate"); DataTable dtTemp = UtilityClass.GetSQLResult(sbSQL.ToString()).Tables["Result"]; foreach (DataRow drData in dtTemp.Rows) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(dgvARManagement); //逐行產生資料 strSalesOrderNo = (string)drData["SalesOrderNo"]; strProjectCName = (string)drData["ProjectCName"]; strProjectNumber = (string)drData["ProjectNumber"]; strReceiptNo = (string)drData["ReceiptNo"]; strCustomerID = (string)drData["CustomerID"]; strCustomerName = (string)drData["CustomerName"]; dtActualReceiptDate = Convert.ToDateTime(drData["ActualReceiptDate"]); if (drData["ActualReceiptedDate"] != DBNull.Value) { strActualReceiptedDate = Convert.ToDateTime(drData["ActualReceiptedDate"]).ToShortDateString(); } else { strActualReceiptedDate = ""; } strReceiptAmount = (string)drData["ReceiptAmount"]; strReceiveStatus = (string)drData["ReceiveStatus"]; 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(strReceiptNo) ? "" : strReceiptNo; dgvRow.Cells[7].Value = dtActualReceiptDate.ToString("yyyy/MM/dd"); dgvRow.Cells[8].Value = strActualReceiptedDate; dgvRow.Cells[9].Value = string.IsNullOrEmpty(strReceiptAmount) ? "" : UtilityClass.DecryptDES(strReceiptAmount,strKey); dgvRow.Cells[10].Value = string.IsNullOrEmpty(strMemo) ? "" : strMemo; dgvRow.Cells[11].Value = string.IsNullOrEmpty(strReceiveStatus) ? "" : strReceiveStatus; if (drData["ReceiveStatus"].ToString() == "Y") { dgvRow.ReadOnly = true; } dgvARManagement.Rows.Add(dgvRow); } StatusChange("NONE"); break; case "MODIFY": //本程式不提供維護功能 break; } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsReceivable.cs", ex); sqlTran.Rollback(); } } private bool CheckForm() { if (!UtilityClass.IsDate(txtReceiptDateStart.Text.Trim()) && txtReceiptDateStart.Text.Trim() != "") { MessageBox.Show("起始時間格式有誤!", "提示"); txtReceiptDateStart.Focus(); return false; } if (!UtilityClass.IsDate(txtReceiptDateEnd.Text.Trim()) && txtReceiptDateEnd.Text.Trim() != "") { MessageBox.Show("結束時間格式有誤!", "提示"); txtReceiptDateEnd.Focus(); return false; } return true; } public void UpdateAR(DataGridViewCellEventArgs e) //更新應收帳款記錄 { try { //物件宣告 string strReceiptNo = (string)dgvARManagement.Rows[e.RowIndex].Cells["cReceiptNo"].Value; string strReceiptedDate = (string)dgvARManagement.Rows[e.RowIndex].Cells["cActualReceiptedDate"].Value; using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { string strSQL = "Update OTB_FNC_AccountReceivable Set ActualReceiptedDate = @ActualReceiptedDate , ReceiveStatus = 'Y' , ModifyDate = GetDate(), ModifyUser = @ModifyUser Where AccountingBookID = @AccountingBookID And ReceiptNo = @ReceiptNo "; strSQL += "UPdate OTB_SAL_ReceiptPlan Set ActualReceiptedDate = @ActualReceiptedDate, ModifyDate = GetDate(), ModifyUser = @ModifyUser Where AccountingBookID = @AccountingBookID And ReceiptNo = @ReceiptNo "; //添加參數 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("@ReceiptNo",string.IsNullOrEmpty(strReceiptNo) ? "" : strReceiptNo), new SqlParameter("@ModifyUser",strActiveUserID) } ); SqlParameter spActualReceiptedDate = new SqlParameter("@ActualReceiptedDate", SqlDbType.DateTime); spActualReceiptedDate.Value = strReceiptedDate; sqlAdapter.UpdateCommand.Parameters.Add(spActualReceiptedDate); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.UpdateCommand.ExecuteNonQuery(); MessageBox.Show("核銷成功", "提示"); } } catch (Exception ex) { throw ex; } } #endregion #region 事件觸發及問題處理 private void tsbClean_Click(object sender, EventArgs e) { CleanForm(); } private void tsbExit_Click(object sender, EventArgs e) { this.Close(); } private void tsbCancel_Click(object sender, EventArgs e) { CleanToolbar(); StatusChange("NONE"); } private void tsbOK_Click(object sender, EventArgs e) { try { if (CheckForm()) { dgvARManagement.Rows.Clear(); GoEvent(); //還原Toolbar狀態 CleanToolbar(); //關閉查詢條件 LockForm(); StatusChange("NONE"); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsReceivable.cs", ex); } } private void AccountsReceivable_Load(object sender, EventArgs e) { SetupStatus(); StatusChange("SEARCH"); GoEvent(); } 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 dgvARManagement_CellContentClick(object sender, DataGridViewCellEventArgs e) { //物件宣告 string strDC = ""; string strReceipt = (string)dgvARManagement.Rows[e.RowIndex].Cells["cReceiptNo"].Value; string strReceiptAmount = (string)dgvARManagement.Rows[e.RowIndex].Cells["cReceiptAmount"].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 = "AccountsReceivable"; acItemForm.Owner = this; acItemForm.StartPosition = FormStartPosition.CenterParent; acItemForm.strFrmStatus = "ADD"; acItemForm.txtProjectNumber.Text = (string)dgvARManagement.Rows[e.RowIndex].Cells["cProjectNumber"].Value; acItemForm.txtProjectName.Text = (string)dgvARManagement.Rows[e.RowIndex].Cells["cProjectName"].Value; 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)dgvARManagement.Rows[e.RowIndex].Cells["cReceiptNo"].Value; dgvRow.Cells[4].Value = strReceiptAmount; 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)dgvARManagement.Rows[e.RowIndex].Cells["cReceiptNo"].Value; dgvRow.Cells[5].Value = strReceiptAmount; break; } blIsAdded = false; if (string.IsNullOrEmpty((string)dgvARManagement.Rows[e.RowIndex].Cells["cActualReceiptedDate"].Value)) { dgvARManagement.Rows[e.RowIndex].Cells["cActualReceiptedDate"].Value = DateTime.Now.ToString("yyyy/MM/dd"); } acItemForm.dgvDataMaintain.Rows.Add(dgvRow); } acItemForm.dgeArgs = e; } acItemForm.ShowDialog(); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountsReceivable.cs", ex); } } private void dgvARManagement_RowValidated(object sender, DataGridViewCellEventArgs e) { dgvARManagement.Rows[e.RowIndex].ErrorText = ""; } private void dgvARManagement_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { string strActualReceiptedDate = (string)dgvARManagement.Rows[e.RowIndex].Cells["cActualReceiptedDate"].Value; if (!string.IsNullOrEmpty(strActualReceiptedDate)) { if (!UtilityClass.IsDate(strActualReceiptedDate)) { dgvARManagement.Rows[e.RowIndex].ErrorText = "日期格式不正確!"; e.Cancel = true; } } } #endregion } }