#region
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using CSFrameworkV5.Business;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Language;
using CSFrameworkV5.Library.CommonClass;
using GSBase;
#endregion
namespace CSFrameworkV5.Library
{
/// *************************************************************************/
/// *
/// * 文件名 :frmBaseBusinessForm.cs
/// * 程序说明 : 业务窗体基类
/// * 业务窗体是指有主从表结构数据操作的窗体.
/// * 原创作者 :www.csframework.com
/// *
/// * Copyright 2006-2021 C/S框架网 www.csframework.com
/// *
/// **************************************************************************/
///
/// 业务窗体是指有主从表结构数据操作的窗体
///
public partial class frmBaseBusinessForm : frmBaseDataForm,
IBusinessSupportable
{
///
/// 业务单据窗体的业务逻辑层
///
protected bllBaseBusiness _BLL = new bllUnknow();
private bllReports _BLLInstance;
private DataSet PrintSet; //打印数据源
public frmBaseBusinessForm()
{
InitializeComponent();
}
public bllBaseBusiness BLL => _BLL;
///
/// 当改变按钮状态时调用此方法
///
/// 当前的状态
protected override void ButtonStateChanged(UpdateType currentState)
{
base.ButtonStateChanged(currentState);
}
///
/// 新增记录
///
///
public override void DoAdd(IButtonInfo sender)
{
try
{
frmWaitingEx.ShowMe(this);
_BLL.GetDataByKey("0", true); //下载一个空业务单据
_BLL.NewBusiness(); //增加一条主表记录
DoBindingSummaryEditor(_BLL
.DataBinder); //显示主表记录详细资料
DoBindingDetailGrid(_BLL.CurrentBusiness); //绑定明细表
base.DoAdd(sender);
ShowDetailPage(true);
}
finally
{
frmWaitingEx.HideMe(this);
}
}
// 2024-05-27 新增 BEGIN
protected void BindDetailGrids()
{
AssertFocusedRow();
var docNo =
_SummaryView.GetDataRow(_SummaryView.FocusedRowHandle)
[_BLL.KeyFieldName]
.ToStringEx();
_BLL.GetDataByKey(docNo, true);
DoBindingDetailGrid(_BLL.CurrentBusiness);
}
// 2024-05-27 新增 BEGIN
protected virtual void OnGridViewClick(object sender, EventArgs e)
{
BindDetailGrids();
}
protected virtual bool DoBeforeDelete(DataRow row)
{
if (_BLL.IsOwnerChange(row) == false)
{
Msg.Warning(
"您不能删除别人创建的单据!\r\n1.您与制单人不构成上下级关系\r\n2.制单人是分配临时的权限!");
return false;
}
if (_BLL.IsApproved(row))
{
Msg.Warning("单据已经审核,不能删除!");
return false;
}
return true;
}
protected virtual bool DoBeforeEdit(DataRow row)
{
if (_BLL.IsOwnerChange(row) == false)
{
Msg.Warning(
"您不能修改别人创建的单据!\r\n1.您与制单人不构成上下级关系\r\n2.制单人是分配临时的权限!");
return false;
}
if (_BLL.IsApproved(row))
{
Msg.Warning("单据已经审核,不能修改!");
return false;
}
return true;
}
///
/// 绑定明细表格的数据源
///
///
protected virtual void DoBindingDetailGrid(DataSet dataSource)
{
}
///
/// 绑定主表当前记录的所有输入控件的数据源
///
///
protected virtual void DoBindingSummaryEditor(DataTable dataSource)
{
}
///
/// 取消修改
///
///
public override void DoCancel(IButtonInfo sender)
{
if (!Msg.AskQuestion("要取消修改吗?")) return;
try
{
frmWaitingEx.ShowMe(this);
if (_UpdateType == UpdateType.Add)
{
base.DoCancel(sender);
ShowSummaryPage(
true); //只显示Summary页面. 因为禁止使用DetailPage,所以并没有刷新Detail页面数据.
}
else
{
base.DoCancel(sender);
DoViewContent(sender); //刷新Detail页面资料.
}
}
finally
{
frmWaitingEx.HideMe(this);
}
}
///
/// 删除记录
///
///
public override void DoDelete(IButtonInfo sender)
{
AssertFocusedRow(); //是否选择记录
var row = _SummaryView.GetDataRow(_SummaryView.FocusedRowHandle);
if (!DoBeforeDelete(row)) return; //删除记录前业务逻辑检查(比如记录已经审核、存档或关联数据等)
if (Msg.AskQuestion("确定要删除当前记录?"))
try
{
frmWaitingEx.ShowMe(this);
var b = _BLL.Delete(row[_BLL.KeyFieldName]
.ToStringEx()); //调用底层接口,删除数据
AssertEqual(b, true, "删除记录时发生错误!");
_SummaryView.RemoveRow(_SummaryView
.FocusedRowHandle); //删除表格中当前记录(删除缓存记录)
base.DoDelete(sender);
ShowSummaryPage(true);
}
finally
{
frmWaitingEx.HideMe(this);
}
}
///
/// 修改单据
///
///
public override void DoEdit(IButtonInfo sender)
{
AssertFocusedRow();
try
{
frmWaitingEx.ShowMe(this);
var row =
_SummaryView.GetDataRow(_SummaryView.FocusedRowHandle);
if (!DoBeforeEdit(row)) return;
base.DoEdit(sender);
DoViewContent(sender);
ShowDetailPage(true);
}
finally
{
frmWaitingEx.HideMe(this);
}
}
///
/// 当用户在Summary页选择一条记录. 显示当前记录的详细资料及明细表资料.
///
///
public override void DoViewContent(IButtonInfo sender)
{
AssertFocusedRow();
var docNo =
_SummaryView.GetDataRow(_SummaryView.FocusedRowHandle)[
_BLL.KeyFieldName].ToStringEx();
_BLL.GetDataByKey(docNo, true);
var summary = _BLL.CurrentBusiness.Tables[_BLL.SummaryTableName];
DoBindingSummaryEditor(
summary); //显示主表记录详细资料
DoBindingDetailGrid(_BLL.CurrentBusiness); //绑定明细表
PrintSet = _BLL.CurrentBusiness;
base.DoViewContent(sender);
ShowDetailPage(
false); //用户点击ViewContent按钮可以显示Summary页
}
public override void Refresh()
{
base.Refresh();
}
///
/// 获取当前记录的指定字段的值
///
/// 字段名
///
protected string GetSummaryFieldValue(string fieldName)
{
if (_SummaryView == null) return "";
if (_SummaryView.IsValidRowHandle(_SummaryView.FocusedRowHandle))
return ConvertEx.ToString(
_SummaryView.GetDataRow(_SummaryView.FocusedRowHandle)[
fieldName]);
return "";
}
///
/// 初始化数据窗体的常用按钮
///
public override void InitButtons()
{
base.InitButtons();
Buttons.AddRange(GetBusinessButtons());
Buttons.AddRange(GetPrintableButtons());
}
///
/// 设置为修改状态
///
protected override void SetEditMode()
{
base.SetEditMode();
_buttons.GetButtonByName("btnApproval").Enable = false;
_buttons.GetButtonByName("btnApprovalUndo").Enable = false;
_buttons.GetButtonByName("btnShowModifyHistory").Enable = false;
}
///
/// 删除单据:只删除明细记录,主表金额清零!!!
///
///
protected virtual void SetSummaryRowZero(DataRow row)
{
}
///
/// 设置为查看状态
///
protected override void SetViewMode()
{
base.SetViewMode();
_buttons.GetButtonByName("btnApproval").Enable =
ButtonAuthorized(ButtonAuthority.APPROVAL);
_buttons.GetButtonByName("btnApprovalUndo").Enable =
ButtonAuthorized(ButtonAuthority.APPROVAL);
_buttons.GetButtonByName("btnShowModifyHistory").Enable =
ButtonAuthorized(ButtonAuthority.SHOW_MOD_HISTORY);
}
#region IBusinessSupportable 成员
///
/// 打印
///
///
public virtual void DoPrint(IButtonInfo button)
{
var tmpFile = "";
var ReportsName =
PrintSet.Tables[0].Rows[0]["AppNAME"].ToString();
var myReport = new MyFastReportMain();
var SYSReports = _BLL.GetReports(ReportsName);
foreach (DataRow dr in SYSReports.Rows)
{
var bs = (byte[])dr["Data"];
if (bs == null) bs = new byte[] { };
//写入到临时文件
tmpFile = Path.Combine(Application.StartupPath,
ReportsName + ".frx");
File.WriteAllBytes(tmpFile, bs);
}
// string ReportFormat = Environment.CurrentDirectory + "\\SN.frx";
try
{
var reportString = File.ReadAllText(tmpFile);
myReport.Print(PrintType.PrintReport, PrintSet, reportString);
File.Delete(tmpFile);
}
catch (Exception)
{
Msg.Warning("该模块未设置打印模版!!!");
}
}
///
/// 打印设计
///
///
public virtual void DoPrintT(IButtonInfo button)
{
var tmpFile = "";
var ReportsName =
PrintSet.Tables[0].Rows[0]["AppNAME"].ToString();
var myReport = new MyFastReportMain();
var SYSReports = _BLL.GetReports(ReportsName);
foreach (DataRow dr in SYSReports.Rows)
{
var bs = (byte[])dr["Data"];
if (bs == null) bs = new byte[] { };
//写入到临时文件
tmpFile = Path.Combine(Application.StartupPath,
ReportsName + ".frx");
File.WriteAllBytes(tmpFile, bs);
}
// string ReportFormat = Environment.CurrentDirectory + "\\SN.frx";
try
{
var reportString = File.ReadAllText(tmpFile);
myReport.Print(PrintType.DesignReport, PrintSet, reportString);
File.Delete(tmpFile);
}
catch (Exception)
{
Msg.Warning("该模块未设置打印模版!!!");
}
}
///
/// 打印预览
///
///
public virtual void DoPrintUN(IButtonInfo button)
{
var tmpFile = "";
var ReportsName =
PrintSet.Tables[0].Rows[0]["AppNAME"].ToString();
var myReport = new MyFastReportMain();
var SYSReports = _BLL.GetReports(ReportsName);
foreach (DataRow dr in SYSReports.Rows)
{
var bs = (byte[])dr["Data"];
if (bs == null) bs = new byte[] { };
//写入到临时文件
tmpFile = Path.Combine(Application.StartupPath,
ReportsName + ".frx");
File.WriteAllBytes(tmpFile, bs);
}
// string ReportFormat = Environment.CurrentDirectory + "\\SN.frx";
try
{
var reportString = File.ReadAllText(tmpFile);
myReport.Print(PrintType.PreviewReport, PrintSet, reportString);
File.Delete(tmpFile);
}
catch (Exception)
{
Msg.Warning("该模块未设置打印模版!!!");
}
}
public virtual IButtonInfo[] GetPrintableButtons()
{
var list = new List();
//打印
if (ButtonAuthorized(ButtonAuthority.PRINT))
{
var btnPrint = ToolbarRegister.CreateButton("btnPrint",
LanLib.Get("打印"),
ToolBarGroup.打印功能, Globals.LoadBitmap("32_Print.png"),
new Size(57, 28), true, true, DoPrint);
list.Add(btnPrint);
}
if (ButtonAuthorized(ButtonAuthority.PRINT))
{
var btnPrint = ToolbarRegister.CreateButton("btnPrint",
LanLib.Get("报表设计"),
ToolBarGroup.打印功能, Globals.LoadBitmap("32_Print.png"),
new Size(57, 28), true, true, DoPrintT);
list.Add(btnPrint);
}
if (ButtonAuthorized(ButtonAuthority.PRINT))
{
var btnPrint = ToolbarRegister.CreateButton("btnPrint",
LanLib.Get("预览"),
ToolBarGroup.打印功能, Globals.LoadBitmap("32_Print.png"),
new Size(57, 28), true, true, DoPrintUN);
list.Add(btnPrint);
}
return list.ToArray();
}
///
/// 返回业务单据窗体的按钮数组
///
///
public virtual IButtonInfo[] GetBusinessButtons()
{
var list = new ArrayList();
//审核/反审
if (ButtonAuthorized(ButtonAuthority.APPROVAL))
{
list.Add(ToolbarRegister.CreateButton("btnApproval", "审核",
ToolBarGroup.审核功能,
Globals.LoadBitmap("24_Approval.ico"), new Size(57, 28),
true, false, DoApproval));
list.Add(ToolbarRegister.CreateButton("btnApprovalUndo", "反审",
ToolBarGroup.审核功能,
Globals.LoadBitmap("24_ApprovalUndo.ico"), new Size(57, 28),
true, false, DoApprovalUndo));
}
//查看日志
if (ButtonAuthorized(ButtonAuthority.SHOW_MOD_HISTORY))
list.Add(ToolbarRegister.CreateButton("btnShowModifyHistory",
"查看日志", ToolBarGroup.扩展功能1,
Globals.LoadBitmap("32_EditLog.png"), new Size(57, 28),
true, true, DoShowModifyHistory));
var ii = (IButtonInfo[])list.ToArray(typeof(IButtonInfo));
return ii;
}
///
/// 审核单据
///
///
public virtual void DoApproval(IButtonInfo button)
{
AssertFocusedRow();
var row = _SummaryView.GetDataRow(_SummaryView.FocusedRowHandle);
var docNo = row[_BLL.KeyFieldName].ToStringEx();
if (_BLL.IsApproved(row))
{
Msg.Warning($"单据<{docNo}>已经审核,不可重复操作!");
}
else
{
if (Msg.AskQuestion($"确定要审核单据<{docNo}>吗?"))
{
_BLL.ApprovalBusiness(row); //审核单据
_SummaryView.RefreshRow(_SummaryView.FocusedRowHandle);
DoViewContent(null); //显示单据明细
}
}
}
public virtual void DoApprovalUndo(IButtonInfo button)
{
AssertFocusedRow();
var row = _SummaryView.GetDataRow(_SummaryView.FocusedRowHandle);
var docNo = row[_BLL.KeyFieldName].ToStringEx();
if (!_BLL.IsApproved(row))
{
Msg.Warning($"单据<{docNo}>没有审核,不需要反审核操作!");
}
else
{
if (Msg.AskQuestion($"确定要反审核单据<{docNo}>吗?"))
{
_BLL.ApprovalBusinessUndo(row); //审核单据
_SummaryView.RefreshRow(_SummaryView.FocusedRowHandle);
DoViewContent(null); //显示单据明细
}
}
}
///
/// 查看单据的修改历史记录
///
///
public virtual void DoShowModifyHistory(IButtonInfo button)
{
AssertFocusedRow();
var form = MdiTools.OpenChildForm(MdiParent as IMdiForm,
typeof(frmLogEditHistory), false);
if (form != null)
{
var keyValue = "";
if (tcBusiness.SelectedTabPageIndex == 0) //数据查看
keyValue =
_SummaryView.GetDataRow(_SummaryView.FocusedRowHandle)[
_BLL.KeyFieldName].ToStringEx();
else //数据编辑
keyValue = _BLL.DataBinder != null
? _BLL.DataBindRow[_BLL.KeyFieldName].ToStringEx()
: "";
(form as frmLogEditHistory).ShowData(_BLL.SummaryTableName,
keyValue, DateTime.Today);
}
}
///
/// 外部调用接口,显示单据
///
/// 单据号码
public virtual void ShowBusiness(string docNo)
{
Msg.ShowInformation("请在派生类实现些方法,主键:" + docNo);
}
#endregion
}
}