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.
 
 

666 lines
26 KiB

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 AccountingJournalMaintain : Form
{
//程式內共用物件
string strFrmStatus = ""; //表單狀態
string strActiveUserID = ""; //取得登入作用的使用者帳號
string strAccountingBookID = ""; //取得作用中的帳本
SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
SqlCommand sqlCmd = new SqlCommand();
DataSet sdInsurance = new System.Data.DataSet();
string strPKey = ""; //程式內的Key值
string strKey = ""; //程式內加密的Key值
int intStartCount = 0;
public AccountingJournalMaintain()
{
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("AccountJournalMaintain.cs", ex);
}
}
public void StatusChange(string strStatus) //變更主畫面狀態
{
switch (strStatus.ToUpper())
{
case "NONE":
CleanForm();
CleanToolbar();
LockForm();
dgvDataMaintain.AllowUserToAddRows = false;
((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "";
strFrmStatus = "";
tsbSearch.Enabled = true;
intStartCount = dgvDataMaintain.Rows.Count;
break;
case "SEARCH":
UnLockForm();
dgvDataMaintain.DataSource = null;
Application.DoEvents();
tsbSearch.Visible = false;
tsbAdd.Enabled = false;
tsbEdit.Enabled = false;
tsbDelete.Enabled = false;
tsbReturn.Enabled = false;
tsbOK.Visible = true;
tsbCancel.Visible = true;
((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋";
strFrmStatus = "SEARCH";
break;
case "ADD":
UnLockForm();
dgvDataMaintain.AllowUserToAddRows = false;
((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增";
strFrmStatus = "ADD";
break;
case "MODIFY":
UnLockForm();
((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改";
strFrmStatus = "MODIFY";
break;
case "DEL":
((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除";
strFrmStatus = "DEL";
break;
}
}
private void CleanForm() //清除畫面
{
//設定畫面物件狀態
//清除GridView
cbAccountingYear.DataSource = UtilityClass.GetAccountingYears(5);
cbAccountingYear.SelectedIndex = 0;
dpAccStart.Text = DateTime.Now.Year.ToString() + "/1/1" ;
dpAccEnd.Text = DateTime.Now.ToShortDateString();
txtAccountingID.Text = "";
txtAccSubName.Text = "";
txtMemo.Text = "";
Application.DoEvents();
}
private void CleanToolbar() //清除工具列
{
//設定Toolbar狀態
tsbSearch.Visible = true;
tsbSearch.Enabled = true;
tsbAdd.Visible = true;
tsbAdd.Enabled = true;
tsbEdit.Visible = true;
tsbEdit.Enabled = true;
tsbDelete.Visible = true;
tsbDelete.Enabled = true;
tsbReturn.Visible = true;
tsbReturn.Enabled = true;
tsbSave.Visible = false;
tsbOK.Visible = false;
tsbCancel.Visible = false;
}
private void UnLockForm() //解除限制唯讀物件
{
cbAccountingYear.Enabled = true;
txtAccountingID.ReadOnly = false;
txtAccSubName.ReadOnly = false;
txtMemo.ReadOnly = false;
dpAccStart.Enabled = true;
dpAccEnd.Enabled = true;
}
private void LockForm() //限制唯讀物件
{
cbAccountingYear.Enabled = false;
txtAccountingID.ReadOnly = true;
txtAccSubName.ReadOnly = true;
txtMemo.ReadOnly = true;
dpAccStart.Enabled = false;
dpAccEnd.Enabled = false;
}
private void SetGridColor() //處理欄位間隔顏色的改變
{
string ColorKey = "";
Color clRowColor = Color.White;
foreach (DataGridViewRow drColor in dgvDataMaintain.Rows)
{
if (!string.IsNullOrEmpty(ColorKey))
{
if (ColorKey == drColor.Cells[1].Value.ToString())
{
drColor.DefaultCellStyle.BackColor = clRowColor;
}
else
{
if (clRowColor == Color.White)
{
clRowColor = Color.LightGray;
}
else
{
clRowColor = Color.White;
}
drColor.DefaultCellStyle.BackColor = clRowColor;
}
}
ColorKey = drColor.Cells[1].Value.ToString();
}
}
public bool UpdateAccount(DataSet dsRecordUpdate) //維護分錄
{
try
{
return true;
}
catch (Exception ex)
{
ErrorHandler.WriteErrorLog("AccountJournalMaintain.cs", ex);
return false;
}
}
public void GoEvent() //執行事件
{
try
{
StringBuilder strSQL = new StringBuilder("");
string strDebit = "";
string strCredit = "";
//檢查查詢條件是否相符
if (cbAccountingYear.SelectedValue.ToString() != dpAccStart.Value.Year.ToString())
{
MessageBox.Show("傳票日期選擇之年份應與會計年度相符", "提示");
dpAccStart.Focus();
return;
}
//查詢事件
strSQL.Clear();
strSQL.Append("Select * ");
strSQL.Append("From OTB_FNC_AccountingJournal ");
strSQL.Append("Where AccountingBookID ='" + strAccountingBookID + "'");
strSQL.Append("And AccountingID in (Select AccountingID From OTB_FNC_AccountingJournal Where 1=1 ");
if (cbAccountingYear.SelectedValue.ToString() != "") //會計年度
{
strSQL.Append(" And AccountingID like '" + cbAccountingYear.SelectedValue.ToString() + "%' ");
}
if (dpAccStart.Text.Trim() != "") //分錄日期區間
{
strSQL.Append(" And AccountingDate Between Convert(DateTime, '" + dpAccStart.Text.Trim() + " 00:00:00', 120) AND Convert(DateTime, '" + dpAccEnd.Text.Trim() + " 23:59:59', 120)");
}
if (txtAccountingID.Text.Trim() != "") //傳票編號
{
strSQL.Append(" And AccountingID like '" + txtAccountingID.Text.Trim() + "' ");
}
if (txtAccSubName.Text.Trim() != "") //科目名稱
{
strSQL.Append(" And AccountingSubName like '" + txtAccSubName.Text.Trim() + "' ");
}
if (txtMemo.Text.Trim() != "") //備註
{
strSQL.Append(" And Memo like '" + txtMemo.Text.Trim() + "' ");
}
strSQL.Append(")");
strSQL.Append(" Order By AccountingID, AccountingOrder ");
//進行查詢
dgvDataMaintain.Rows.Clear();
using(DataSet dsTemp = UtilityClass.GetSQLResult(strSQL.ToString()))
{
DataTable dtTemp = dsTemp.Tables["Result"];
foreach (DataRow drAccounting in dtTemp.Rows)
{
DataGridViewRow dgvRow = new DataGridViewRow();
dgvRow.CreateCells(dgvDataMaintain);
dgvRow.Cells[1].Value = drAccounting["AccountingID"].ToString(); //傳票編號
dgvRow.Cells[2].Value = DateTime.Parse(drAccounting["AccountingDate"].ToString()).ToShortDateString(); //傳票日期
dgvRow.Cells[3].Value = drAccounting["AccountingSubID"].ToString(); //科目代號
dgvRow.Cells[4].Value = drAccounting["AccountingSubName"].ToString(); //科目名稱
dgvRow.Cells[5].Value = drAccounting["Memo"].ToString(); //摘要
strDebit = UtilityClass.DecryptDES(drAccounting["Debit"].ToString(), strKey);
strCredit = UtilityClass.DecryptDES(drAccounting["Credit"].ToString(), strKey);
dgvRow.Cells[6].Value = UtilityClass.MarkNumber(strDebit); //借方金額
dgvRow.Cells[7].Value = UtilityClass.MarkNumber(strCredit); //貸方金額
dgvRow.Cells[8].Value = drAccounting["ProjectCName"].ToString(); //專案名稱
dgvRow.Cells[9].Value = drAccounting["ProjectNumber"].ToString(); //專案代碼
dgvDataMaintain.Rows.Add(dgvRow);
}
SetGridColor();
//將資料移到最下面
if (dgvDataMaintain.Rows.Count > 0)
{
dgvDataMaintain.CurrentCell = dgvDataMaintain.Rows[dtTemp.Rows.Count - 1].Cells[0];
}
}
}
catch (Exception ex)
{
ErrorHandler.WriteErrorLog("AccountJournalMaintain.cs", ex);
}
}
private bool CheckSel() //確認是否有資料被勾選
{
foreach (DataGridViewRow row in dgvDataMaintain.Rows)
{
DataGridViewCheckBoxCell Selcell = (DataGridViewCheckBoxCell)row.Cells["cCheckedColumn"]; //選擇狀態列
if (Selcell.Value != null)
{
if (Selcell.Value == Selcell.TrueValue)
{
return true;
}
}
}
return false;
}
private void ShowUpdateDate(string strGUID) // 呈現最新修改人
{
//排除空值
if (strGUID.Trim() == "")
{
return;
}
if (strGUID == null)
{
return;
}
strPKey = strGUID;
string strSQL = "Select ModifyDate, ModifyUser From OTB_FNC_AccountingJournal Where AccountingID = '" + strGUID + "' And AccountingBookID ='" + strAccountingBookID + "'";
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
{
//添加參數
sqlAdapter.SelectCommand = new SqlCommand();
sqlAdapter.SelectCommand.Connection = sqlConn;
sqlAdapter.SelectCommand.CommandText = strSQL;
if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
{
sqlConn.Open();
}
//進行查詢
DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"];
((MainForm)ParentForm).SsStatus.Items["tsslModifyUser"].Text = dtTemp.Rows[0]["ModifyUser"].ToString().Trim();
((MainForm)ParentForm).SsStatus.Items["tsslModifyDate"].Text = dtTemp.Rows[0]["ModifyDate"].ToString().Trim();
}
}
#endregion
#region 事件觸發及問題處理
private void AccountJournalMaintain_Load(object sender, EventArgs e)
{
SetupStatus();
GoEvent();
}
private void tsbReturn_Click(object sender, EventArgs e)
{
string strDebit = "";
string strCredit = "";
string strSelectStatus = "";
if (CheckSel())
{
StatusChange("MODIFY");
AccountingEntries frmAdd = new AccountingEntries();
frmAdd.strFrmStatus = "ADD";
frmAdd.Owner = this;
frmAdd.strOwnerForm = this.Name + "_Reversal";
frmAdd.strPKey = strPKey;
frmAdd.StartPosition = FormStartPosition.CenterParent;
//分錄產生
foreach (DataGridViewRow row in dgvDataMaintain.Rows)
{
DataGridViewCheckBoxCell Selcell = (DataGridViewCheckBoxCell)row.Cells["cCheckedColumn"]; //選擇狀態列
strSelectStatus = Selcell.Value.ToString();
if (strSelectStatus != "False") //確認該分錄被選擇
{
strDebit = (string)row.Cells["cDebit"].Value;
strCredit = (string)row.Cells["cCredit"].Value;
DataGridViewRow dgvRow = new DataGridViewRow();
dgvRow.CreateCells(frmAdd.dgvDataMaintain);
dgvRow.Cells[1].Value = (string)row.Cells["cAccountSubID"].Value;
dgvRow.Cells[2].Value = (string)row.Cells["cAccountSubName"].Value;
dgvRow.Cells[3].Value = "傳票編號:" + (string)row.Cells["cAccountingID"].Value + "沖銷分錄";
if (!string.IsNullOrEmpty(strDebit))
{
dgvRow.Cells[5].Value = strDebit;
}
else
{
dgvRow.Cells[4].Value = strCredit;
}
frmAdd.dgvDataMaintain.Rows.Add(dgvRow);
}
}
frmAdd.ShowDialog();
}
else
{
MessageBox.Show("請先選擇要沖銷的分錄!", "提示");
}
}
private void tsbExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void tsbAdd_Click(object sender, EventArgs e)
{
StatusChange("ADD");
tsbSearch.Enabled = false;
tsbEdit.Enabled = false;
tsbDelete.Enabled = false;
tsbReturn.Enabled = false;
AccountingEntries frmAdd = new AccountingEntries();
frmAdd.strFrmStatus = "ADD";
frmAdd.strOwnerForm = this.Name.ToString();
frmAdd.Owner = this;
frmAdd.StartPosition = FormStartPosition.CenterParent;
frmAdd.ShowDialog();
}
private void tsbEdit_Click(object sender, EventArgs e)
{
if (CheckSel())
{
StatusChange("MODIFY");
tsbEdit.Visible = false;
tsbSearch.Enabled = false;
tsbAdd.Enabled = false;
tsbDelete.Enabled = false;
tsbReturn.Enabled = false;
AccountingEntries frmMdf = new AccountingEntries();
frmMdf.strFrmStatus = "MODIFY";
frmMdf.strOwnerForm = this.Name.ToString();
frmMdf.Owner = this;
frmMdf.strPKey = strPKey;
frmMdf.StartPosition = FormStartPosition.CenterParent;
frmMdf.ShowDialog();
}
else
{
MessageBox.Show("請先選擇要修改的資料!","提示");
}
}
private void tsbDelete_Click(object sender, EventArgs e)
{
if (CheckSel())
{
StatusChange("DEL");
tsbDelete.Visible = false;
tsbEdit.Enabled = false;
tsbSearch.Enabled = false;
tsbAdd.Enabled = false;
tsbReturn.Enabled = false;
AccountingEntries frmAdd = new AccountingEntries();
frmAdd.strFrmStatus = "DEL";
frmAdd.Owner = this;
frmAdd.strPKey = strPKey;
frmAdd.StartPosition = FormStartPosition.CenterParent;
frmAdd.ShowDialog();
}
else
{
MessageBox.Show("請先選擇要刪除的資料!", "提示");
}
}
private void tsbSearch_Click(object sender, EventArgs e)
{
StatusChange("SEARCH");
}
private void tsbOK_Click(object sender, EventArgs e)
{
GoEvent();
CleanToolbar();
}
private void tsbCancel_Click(object sender, EventArgs e)
{
CleanToolbar();
StatusChange("NONE");
}
private void dgvDataMaintain_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) //製作合併儲存格的效果
{
try
{
Brush gridBrush = new SolidBrush(this.dgvDataMaintain.GridColor);
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
int cellheight;
int fontheight;
int cellwidth;
int fontwidth;
int countU = 0;
int countD = 0;
int count = 0;
#region 對第二列相同儲存格進行合併
if (e.RowIndex != -1)
{
if (e.ColumnIndex == 1) //傳票編號欄位合併
{
cellheight = e.CellBounds.Height;
fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
cellwidth = e.CellBounds.Width;
fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
Pen gridLinePen = new Pen(gridBrush);
string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
string curSelected = this.dgvDataMaintain.Rows[e.RowIndex].Cells[1].Value == null ? "" : this.dgvDataMaintain.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim();
if (!string.IsNullOrEmpty(curValue))
{
for (int intCount = e.RowIndex; intCount < this.dgvDataMaintain.Rows.Count; intCount++)
{
if (this.dgvDataMaintain.Rows[intCount].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
{
this.dgvDataMaintain.Rows[intCount].Cells[e.ColumnIndex].Selected = this.dgvDataMaintain.Rows[e.RowIndex].Selected;
this.dgvDataMaintain.Rows[intCount].Selected = this.dgvDataMaintain.Rows[e.RowIndex].Selected;
countD++;
}
else
{
break;
}
}
for (int intCount = e.RowIndex; intCount >= 0; intCount--)
{
if (this.dgvDataMaintain.Rows[intCount].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
{
this.dgvDataMaintain.Rows[intCount].Cells[e.ColumnIndex].Selected = this.dgvDataMaintain.Rows[e.RowIndex].Selected;
this.dgvDataMaintain.Rows[intCount].Selected = this.dgvDataMaintain.Rows[e.RowIndex].Selected;
countU++;
}
else
{
break;
}
}
count = countD + countU - 1;
if (count < 2) { return; }
}
if (this.dgvDataMaintain.Rows[e.RowIndex].Selected)
{
backBrush.Color = e.CellStyle.SelectionBackColor;
fontBrush.Color = e.CellStyle.SelectionForeColor;
}
e.Graphics.FillRectangle(backBrush, e.CellBounds);
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (countU - 1) + (cellheight * count - fontheight) / 2);
if (countD == 1)
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
count = 0;
}
//畫右邊的線
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
e.Handled = true;
}
}
#endregion
}
catch (Exception ex)
{
ErrorHandler.WriteErrorLog("AccountJournalMaintain.cs", ex);
}
}
private void dgvDataMaintain_CellContentClick(object sender, DataGridViewCellEventArgs e) //CheckBox被選取處理事件
{
//當GridView中的Button被按下
var SenderGrid = (DataGridView)sender;
if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn && e.RowIndex >= 0)
{
foreach (DataGridViewRow dgRow in SenderGrid.Rows)
{
dgRow.Cells["cCheckedColumn"].Value = false;
}
foreach (DataGridViewRow dgRow in SenderGrid.Rows)
{
if (dgRow.Cells[1].Value.ToString() == SenderGrid.Rows[e.RowIndex].Cells[1].Value.ToString())
{
dgRow.Cells[0].Value = true;
strPKey = SenderGrid.Rows[e.RowIndex].Cells[1].Value.ToString();
}
else
{
dgRow.Cells[0].Value = false;
}
}
}
}
private void dgvDataMaintain_CellValueChanged(object sender, DataGridViewCellEventArgs e) //設定選取項目
{
if (dgvDataMaintain.CurrentRow != null)
{
DataGridViewCheckBoxCell Selcell = (DataGridViewCheckBoxCell)dgvDataMaintain.CurrentRow.Cells["cCheckedColumn"];
Selcell.Value = Selcell.TrueValue;
}
}
private void tsbClean_Click(object sender, EventArgs e)
{
CleanForm();
}
private void dgvDataMaintain_CellClick(object sender, DataGridViewCellEventArgs e)
{
ShowUpdateDate(dgvDataMaintain.CurrentRow.Cells["cAccountingID"].Value.ToString()); //顯示最新修改資料
}
private void dgvDataMaintain_CellEnter(object sender, DataGridViewCellEventArgs e)
{
ShowUpdateDate(dgvDataMaintain.CurrentRow.Cells["cAccountingID"].Value.ToString()); //顯示最新修改資料
}
private void cbAccountingYear_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbAccountingYear.SelectedValue.ToString() == DateTime.Now.Year.ToString())
{
dpAccStart.Text = DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/1";
dpAccEnd.Text = DateTime.Now.ToShortDateString();
}
else
{
dpAccStart.Text = cbAccountingYear.SelectedValue.ToString() + "/1/1";
dpAccEnd.Text = cbAccountingYear.SelectedValue.ToString() + "/12/31";
}
}
#endregion
}
}