/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:F_MST_0001.cs
* 2.功能描述:帐套维护页面
* 编辑履历:
* 作者 日期 版本 修改内容
* 张国印 2014/08/27 1.00 新建
*******************************************************************************/
using System;
using System.Data;
using System.Reflection;
using System.Windows.Forms;
using Dongke.IBOSS.PRD.Basics.BaseControls;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Basics.Library;
using Dongke.IBOSS.PRD.Client.CommonModule;
using Dongke.IBOSS.PRD.Client.DataModels;
using Dongke.IBOSS.PRD.WCF.Proxys;
namespace Dongke.IBOSS.PRD.Client.SystemModule
{
///
/// 帐套维护页面
///
public partial class F_MST_0001 : FormBase
{
#region 成员变量
// 窗体的单例模式
private static F_MST_0001 _instance;
// 帐套数据源
private DataTable _dtSourse;
#endregion
#region 构造函数
///
/// 构造函数
///
public F_MST_0001()
{
InitializeComponent();
this.SetFromTitleInfo();
}
#endregion
#region 事件
///
/// 窗体关闭事件
///
///
///
private void F_MST_0001_FormClosed(object sender, FormClosedEventArgs e)
{
_instance = null;
}
///
/// 页面加载
///
///
///
private void F_MST_0001_Load(object sender, EventArgs e)
{
try
{
FormPermissionManager.FormPermissionControl(this.Name, this,
LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData,
LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData);
// 设置“保存”按钮不可用
this.btnSave.Enabled = false;
// 设置表格不自动创建列
this.dgvAccount.AutoGenerateColumns = false;
// 窗体启动立即加载全部帐套信息
this.GetAllAccount();
this.RefreshDataGridViewData();
this.SetAccountCodeEnable();
this.dgvAccount.IsSetInputColumnsColor = true;
this.dgvAccount.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 新增一行时,给有效属性设置选中状态
///
///
///
private void dgvAccount_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells["ValueFlag"].Value = 1;
}
///
/// 行校验,帐套代码、帐套名称不能为空
///
///
///
private void dgvAccount_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
try
{
// 按Esc时不进行校验
if (!this.dgvAccount.IsCurrentRowDirty)
{
this.dgvAccount.IsSetInputColumnsColor = true;
return;
}
if (this.ActiveControl != null && "btnCancel".Equals(this.ActiveControl.Name))
{
// 清除单元格的错误消息
this.dgvAccount.CurrentRow.ErrorText = string.Empty;
return;
}
DataGridViewRow row = this.dgvAccount.CurrentRow;
if (!row.IsNewRow)
{
// 帐套代码不能为空
if (row.Cells["AccountCode"].Value == null
|| string.IsNullOrEmpty(row.Cells["AccountCode"].Value.ToString().Trim()))
{
// 增加单元格的错误消息
this.dgvAccount.CurrentRow.ErrorText =
string.Format(Messages.MSG_CMN_W005, "帐套编码");
e.Cancel = true;
return;
}
// 帐套名称不能为空
if (row.Cells["AccountName"].Value == null
|| string.IsNullOrEmpty(row.Cells["AccountName"].Value.ToString().Trim()))
{
// 增加单元格的错误消息
this.dgvAccount.CurrentRow.ErrorText =
string.Format(Messages.MSG_CMN_W005, "帐套名称");
e.Cancel = true;
return;
}
}
// 清除单元格的错误消息
this.dgvAccount.CurrentRow.ErrorText = string.Empty;
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 输入验证,帐套代码不能重复输入
///
///
///
private void dgvAccount_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
try
{
if (this.ActiveControl != null && "btnCancel".Equals(this.ActiveControl.Name))
{
return;
}
if (!this.dgvAccount.CurrentRow.IsNewRow)
{
DataGridViewRow rowItem = this.dgvAccount.Rows[e.RowIndex];
DataGridViewColumn columnItem = this.dgvAccount.Columns[e.ColumnIndex];
object value = rowItem.Cells[columnItem.Name].EditedFormattedValue;
// 帐套代码
if ("AccountCode".Equals(columnItem.Name))
{
if (value != null || !string.IsNullOrEmpty(value.ToString().Trim()))
{
bool isUnique = Utility.IsUnique(value.ToString(),
this.dgvAccount.CurrentRow.Index,
this.dgvAccount, "AccountCode");
if (!isUnique)
{
// 增加单元格的错误消息
this.dgvAccount.CurrentRow.ErrorText =
string.Format(Messages.MSG_CMN_W006, "帐套编码");
e.Cancel = true;
this.btnSave.Enabled = false;
return;
}
}
}
// 清除单元格的错误消息
this.dgvAccount.CurrentRow.ErrorText = string.Empty;
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 根据数据变化,设置保存按钮的状态
///
///
///
private void dgvAccount_RowValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
this.RefreshSaveBtnStatus();
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 根据数据有没有改变,设置"保存"按钮是否可用
///
///
///
private void dgvAccount_CellValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
this.RefreshSaveBtnStatus();
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 关闭窗体
///
///
///
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 窗体关闭
///
///
///
private void F_MST_0001_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
// 判断数据是否有变化
if (DataJudge.IsChange((DataTable)this.dgvAccount.DataSource))
{
// 判断单元格是否有错误提示
if (string.IsNullOrEmpty(this.dgvAccount.CurrentRow.ErrorText))
{
DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001,
this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
// 保存数据
if (dialogResult == DialogResult.Yes)
{
this.SaveAccountInfo();
}
// 点击取消,不关闭窗体
else if (dialogResult == DialogResult.Cancel)
{
e.Cancel = true;
return;
}
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 保存按钮
///
///
///
private void btnSave_Click(object sender, EventArgs e)
{
try
{
// 判断数据是否有变化
if (DataJudge.IsChange((DataTable)this.dgvAccount.DataSource))
{
// 判断单元格是否有错误提示
if (string.IsNullOrEmpty(this.dgvAccount.CurrentRow.ErrorText))
{
this.SaveAccountInfo();
this.SetAccountCodeEnable();
}
else
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "列表中数据存在错误。"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "列表中数据没有变化。"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 是否停用被选中后 刷新页面
///
///
///
private void chkDisplayDisabledData_CheckedChanged(object sender, EventArgs e)
{
try
{
this.RefreshDataGridViewData();
this.SetAccountCodeEnable();
this.dgvAccount.IsSetInputColumnsColor = true;
this.btnSave.Enabled = true;
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 排序后维持表格状态
///
///
///
private void dgvAccount_Sorted(object sender, EventArgs e)
{
this.SetAccountCodeEnable();
this.dgvAccount.IsSetInputColumnsColor = true;
}
///
/// 添加行事件
///
///
///
private void dgvAccount_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
this.dgvAccount.IsSetInputColumnsColor = true;
}
#endregion
#region 私有方法
///
/// 设置窗体按钮的文本信息
///
private void SetFromTitleInfo()
{
// 窗口标题
this.Text = FormTitles.F_MST_0001;
// 设置按钮文本
this.btnCancel.Text = ButtonText.BTN_CLOSE;
this.btnSave.Text = ButtonText.BTN_SAVE;
}
///
/// 得到帐套信息
///
///
private void RefreshDataGridViewData()
{
try
{
if (this.chkDisplayDisabledData.Checked == false)
{
_dtSourse.DefaultView.RowFilter = "ValueFlag=1";
}
else
{
_dtSourse.DefaultView.RowFilter = null;
}
this.dgvAccount.DataSource = _dtSourse;
this.btnSave.Enabled = false;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 将已经存在的帐套代码单元格置成只读
///
private void SetAccountCodeEnable()
{
for (int i = 0; i < this.dgvAccount.Rows.Count; i++)
{
if (!this.dgvAccount.Rows[i].IsNewRow)
{
if (string.IsNullOrEmpty(this.dgvAccount.Rows[i].Cells["AccountID"].Value.ToString()))
{
this.dgvAccount.Rows[i].Cells["AccountCode"].ReadOnly = false;
}
else
{
this.dgvAccount.Rows[i].Cells["AccountCode"].ReadOnly = true;
}
}
}
}
///
/// 获取到帐套信息
///
///
private DataSet GetAccountInfo()
{
try
{
//获取是否停用选项的值
bool valueFlag = chkDisplayDisabledData.Checked;
DataSet dsResultAccount = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return SystemModuleProxy.Service.GetAccountInfo(valueFlag);
}));
return dsResultAccount;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 设置保存按钮的可用状态
///
private void RefreshSaveBtnStatus()
{
if (DataJudge.IsChange((DataTable)this.dgvAccount.DataSource))
{
this.btnSave.Enabled = true;
}
else
{
this.btnSave.Enabled = false;
}
}
///
/// 保存数据
///
private void SaveAccountInfo()
{
bool bolIsValidCheck = this.IsValidCheck();
if (!bolIsValidCheck)
{
return;
}
int resultState = this.SaveAccountData();
// 存在相同的帐套代码 1
if (Constant.INT_IS_ONE == resultState)
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W006, "帐套编码"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 保存成功 2表示有数据被修改 3表示没有数据被修改
if (Constant.INT_IS_TWO == resultState)
{
this.GetAllAccount();
this.btnSave.Enabled = false;
MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "帐套管理", "保存"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "帐套管理", "保存"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
this.RefreshDataGridViewData();
this.SetAccountCodeEnable();
this.dgvAccount.IsSetInputColumnsColor = true;
}
///
/// 调用服务保存帐套数据
///
///
private int SaveAccountData()
{
// 取得系统默认密码
string defaultPassword = Constant.S_PASSWORD_DEFAULT; ;
DataSet dtSetting = CommonModuleProxy.Service.GetSysSettingBySettingType("S_CMN_0001");
if (dtSetting != null)
{
if (dtSetting.Tables.Count > Constant.INT_IS_ZERO)
{
if (dtSetting.Tables[0].Rows.Count > Constant.INT_IS_ZERO)
{
defaultPassword = dtSetting.Tables[0].Rows[0]["SETTINGVALUE"].ToString();
}
}
}
defaultPassword = Encryption.GetMD5String(defaultPassword);
DataTable AccountData = (DataTable)this.dgvAccount.DataSource;
int result = (int)DoAsync(new BaseAsyncMethod(() =>
{
return SystemModuleProxy.Service.SaveAccountData(AccountData, defaultPassword);
}));
return result;
}
///
/// 验证是否可以进行帐套保存 True为可以保存,False为不可保存
///
/// True为验证通过,False为验证不通过
private bool IsValidCheck()
{
// 初始帐套不能设置成无效校验
bool isDefaultAccountDisable = isDefaultAccountToDisable();
if (isDefaultAccountDisable)
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "系统默认的帐套不能置成无效状态"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
// 帐套数不能超过授权的帐套数
bool isExceedLicenseAccountNumber = this.IsExceedLicenseAccountNumber();
if (isExceedLicenseAccountNumber)
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "所创建的有效帐套数超出了授权许可的帐套数"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
///
/// 判断是否将默认帐套设置成无效了
///
///
/// true:将默认帐套设置成无效
/// false:未将默认用户设置成无效
///
private bool isDefaultAccountToDisable()
{
for (int i = 0; i < this.dgvAccount.Rows.Count; i++)
{
if (!this.dgvAccount.Rows[i].IsNewRow)
{
if (LogInUserInfo.CurrentUser.CurrentUserEntity.AccountCode.ToUpper() ==
this.dgvAccount.Rows[i].Cells["AccountCode"].Value.ToString().ToUpper()
&& !Constant.ValueFlag.Effective.GetHashCode().ToString().Equals(
this.dgvAccount.Rows[i].Cells["valueFlag"].Value.ToString()))
{
return true;
}
}
}
return false;
}
///
/// 判断当前的有效帐套数是否超过了授权帐套数
///
///
/// true:超出
/// false:未超出
///
private bool IsExceedLicenseAccountNumber()
{
int accountNumber = 0;
for (int i = 0; i < this.dgvAccount.Rows.Count; i++)
{
if (!this.dgvAccount.Rows[i].IsNewRow)
{
if (Constant.ValueFlag.Effective.GetHashCode().ToString().Equals(
this.dgvAccount.Rows[i].Cells["valueFlag"].Value.ToString()))
{
accountNumber += 1;
}
}
}
int licenseCount = 999; //目前设置为最大值,正式使用后需要修改为0
if (LogInUserInfo.CurrentUser.CurrentLicenseInfo != null)
{
if (LogInUserInfo.CurrentUser.CurrentLicenseInfo.Tables.Count > 0)
{
if (LogInUserInfo.CurrentUser.CurrentLicenseInfo.Tables.Contains("BaseInfoTable"))
{
licenseCount = (int)LogInUserInfo.CurrentUser.CurrentLicenseInfo.
Tables["BaseInfoTable"].Rows[0]["AccountNumber"];
}
}
}
if (accountNumber > licenseCount)
{
return true;
}
return false;
}
///
/// 获取全部帐套信息
///
private void GetAllAccount()
{
try
{
DataSet dsResultAccount = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return SystemModuleProxy.Service.GetAllAccountInfo();
}));
this._dtSourse = dsResultAccount.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}