账表.二开案例.干预表头过滤汇总数量
【场景】汇总数量翻倍问题,通常为单据头合并字段重复计算
【数据准备】
![20240115 1651.webp](/download/0100d56b38f636264b1981ee6ef0e75aad0f.webp)
[账表.二开案例.汇总数量翻倍问题](https://wenku.my7c.com/article/528643445586728448)
【二开方案】
表单插件自实现表头过滤汇总
![image.webp](/download/010008f016b58f504f3e82aaee9a0553a76a.webp)
```csharp
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Enums;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace DynamicFormPlugIn.Report
{
[Kingdee.BOS.Util.HotUpdate]
public class ReportViewPlugIn_AdjustSummary : AbstractSysReportPlugIn
{
public ISysReportModel RptModel
{
get { return (ISysReportModel)this.Model; }
}
public ISysReportView RptView
{
get { return (ISysReportView)this.View; }
}
/// <summary>
/// 表头过滤事件
/// </summary>
private HashSet<string> filterEvents = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"FilterChanged", "UpdateReportSummary"
};
/// <summary>
/// 单据头汇总字段
/// </summary>
private HashSet<string> billSummaryKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"F_PAEZ_Qty1"
};
/// <summary>
/// 序时薄数据行号
/// </summary>
private string fidentityid = "FIDENTITYID";
public override void BeforeCustomEvents(BeforeCustomEventsArgs e)
{
if (!string.Equals(e.Key, "FLIST", System.StringComparison.OrdinalIgnoreCase))
return;
if (!filterEvents.Contains(e.EventName))
return;
//取消标准操作,自定义汇总
e.Cancel = true;
//汇总数据
JSONObject sum = new JSONObject();
JSONObject data = KDObjectConverter.DeserializeObject<JSONObject>(e.EventArgs);
string rows = string.Empty;
if (data != null && data.ContainsKey("rows"))
{
rows = ObjectUtils.Object2String(data["rows"]);
}
//根据前端回传的过滤条件从当前页数据中过滤符合条件的数据
string filter = string.Empty;
if (!string.IsNullOrWhiteSpace(rows))
{
filter = string.Format("{0} in ({1})", this.fidentityid, rows);
}
if (this.RptModel.DataSource == null)
return;
if (data != null && filter.IsNullOrEmptyOrWhiteSpace() && data.ContainsKey("FilterString") && !data["FilterString"].IsNullOrEmptyOrWhiteSpace())
{
filter = "1 <> 1";
}
List<Tuple<string, decimal, string>> summaryTuples = new List<Tuple<string, decimal, string>>();
DataRow[] datas = this.RptModel.DataSource.Select(filter);
DataRow dataRow = this.RptModel.DataSource.NewRow();
foreach (SummaryField field in this.RptModel.SummaryFields)
{
//如果未设置字段的汇总类型,则不进行发送到前端
if (field.SummaryType == 0)
{
continue;
}
if (!dataRow.Table.Columns.Contains(field.Key))
{
continue;
}
decimal value = 0;
//获取字段汇总值
if (billSummaryKeys.Contains(field.Key))
{
value = GetValueWithPk(datas, field, this.RptModel.ReportProperty.PrimaryKeyFieldName);
}
else
{
value = GetValue(datas, field);
}
string formatValue = value.ToString();
summaryTuples.Add(new Tuple<string, decimal, string>(field.Key, value, formatValue));
dataRow[field.Key] = value;
sum.Put(field.Key, formatValue);
}
if (sum.Count > 0)
{
EntryGrid grid = this.RptView.GetControl<EntryGrid>("FLIST");
grid.InvokeControlMethod("SetSummaryData", sum);
}
}
/// <summary>
/// 根据单据主键端标识汇总,避免重复,适用于单据头
/// </summary>
/// <param name="rows"></param>
/// <param name="field"></param>
/// <returns></returns>
private Decimal GetValueWithPk(DataRow[] rows, Kingdee.BOS.Core.Report.SummaryField field, string billPkName)
{
string fieldKey = field.Key;
Decimal result = 0;
HashSet<string> billPkSet = new HashSet<string>();
if (field.SummaryType == BOSEnums.Enu_SummaryType.SUM)
{
foreach (DataRow row in rows)
{
string billPk = ObjectUtils.Object2String(row[billPkName]);
if (billPkSet.Contains(billPk))
continue;
billPkSet.Add(billPk);
result += DealSum(fieldKey, row);
}
}
return result;
}
/// <summary>
/// 根据数据直接汇总,适用于单据体
/// </summary>
/// <param name="rows"></param>
/// <param name="field"></param>
/// <returns></returns>
private Decimal GetValue(DataRow[] rows, Kingdee.BOS.Core.Report.SummaryField field)
{
List<DataRow> list = new List<DataRow>();
string fieldKey = field.Key;
list.AddRange(rows);
Decimal value = 0;
if (field.SummaryType == BOSEnums.Enu_SummaryType.SUM)
{
value = list.Sum((o) =>
{
return DealSum(fieldKey, o);
});
}
else if (field.SummaryType == BOSEnums.Enu_SummaryType.AVERAGE)
{
value = list.Average((o) =>
{
return DealSum(fieldKey, o);
});
}
else if (field.SummaryType == BOSEnums.Enu_SummaryType.MAX)
{
value = list.Max((o) =>
{
return DealSum(fieldKey, o);
});
}
else if (field.SummaryType == BOSEnums.Enu_SummaryType.MIN)
{
value = list.Min((o) =>
{
return DealSum(fieldKey, o);
});
}
else if (field.SummaryType == BOSEnums.Enu_SummaryType.COUNT)
{
value = list.Count;
}
return value;
}
/// <summary>
/// 获取数据值
/// </summary>
/// <param name="fieldKey"></param>
/// <param name="o"></param>
/// <returns></returns>
private static decimal DealSum(string fieldKey, DataRow o)
{
if (!DBNull.Value.Equals(o[fieldKey]))
{
return Convert.ToDecimal(o[fieldKey]);
}
else
{
return 0;
}
}
}
}
```
【效果】
![20240115 1655.webp](/download/010032609000e9c54dc8be47ac67d09fdd9e.webp)
账表.二开案例.干预表头过滤汇总数量
【场景】汇总数量翻倍问题,通常为单据头合并字段重复计算【数据准备】![20240115 1651.webp](/download/0100d56b38f636264b1981ee6ef0e75...
点击下载文档
本文2024-09-16 18:12:19发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-20635.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章