反写上游子单据体
说明:目前反写规则只支持关联实体(一般为单据体),和关联实体的父单据体(单据头)的反写,如果需要反写上游单据的关联实体的子单据体,则需要插件处理。
示例:单据B关联实体反写单据A关联实体的子单据体
步骤:
在单据A和单据B关联的单据体都增加一个隐藏的整形字段,命名为隐藏整数,key设置为FHideInterger,并设置单据B中的隐藏整数默认值为100
创建一个反写规则,如下图:
编写反写插件,并注册到单据B的反写插件中,业务逻辑要点:
3.1 反写规则为上面的反写规则才运行
3.2 保存和删除操作都需要处理
3.3 此反写规则每次保存都需要运行
执行效果,如下图:
反写代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using Kingdee.BOS;
using Kingdee.BOS.Core;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.BusinessEntity;
using Kingdee.BOS.Core.BusinessFlow;
using Kingdee.BOS.Core.BusinessFlow.PlugIn;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.BusinessFlow.PlugIn.Args;
using Kingdee.BOS.BusinessEntity.BusinessFlow;
namespace Kingdee.BOS.TestPlugIn.BillABillB
{
[Description("单据B反写插件")]
[Kingdee.BOS.Util.HotUpdate]
public class BillBWriteBackPlugIn : AbstractBusinessFlowServicePlugIn
{
//需要干预的反写规则
private bool _isNeedHandleRule = false;
/// <summary>
/// 反写前事件,每个反写规则走一次,得到需要干预反写规则
/// </summary>
/// <param name="e"></param>
public override void BeforeWriteBack(BeforeWriteBackEventArgs e)
{
this._isNeedHandleRule = false;
var ruleName = e.Rule.Name.ToString();
if (ruleName.EqualsIgnoreCase("单据B反写单据A—隐藏的整数字段"))
{
this._isNeedHandleRule = true;
}
}
/// <summary>
/// 加入需要反写的上游字段,反写取得是上游部分数据包,所以要通过插件加入自定义需要处理的字段
/// </summary>
/// <param name="e"></param>
public override void AfterCustomReadFields(AfterCustomReadFieldsEventArgs e)
{
if (this._isNeedHandleRule)
{
e.AddFieldKey("F_KKK_SubText2");//上游单据子单据体文本
}
}
/// <summary>
/// 反写条目反写后事件,每一行都会走
/// </summary>
/// <param name="e"></param>
public override void AfterCommitAmount(AfterCommitAmountEventArgs e)
{
base.AfterCommitAmount(e);
if (this._isNeedHandleRule)
{
//保存操作
if (this.OperationNumber.EqualsIgnoreCase("Save"))
{
Random rd = new Random();
var wbRule = e.WriteBackRuleRow as WRule<Id>;//反写条目
var curRowObj = wbRule.Row.DataEntity;//当前单据当前行数据包
curRowObj["FHideInteger"] = rd.Next(); //确保反写规则每次保存操作都会走
var targetFieldValue = curRowObj["FEntry1Remark"]; //当前单据单据体备注
var sourceField = e.SourceBusinessInfo.GetField("F_KKK_SUBTEXT2") ;//上游单据子单据体文本
var sourceSubObjs = sourceField.Entity.DynamicProperty.GetValue( e.SourceActiveRow) as DynamicObjectCollection;
foreach(var subObj in sourceSubObjs)
{
sourceField.DynamicProperty.SetValue(subObj,targetFieldValue);
}
}
else if (this.OperationNumber.EqualsIgnoreCase("Delete")) //删除操作
{
//清空上游单据子单据体文本
var sourceField = e.SourceBusinessInfo.GetField("F_KKK_SUBTEXT2");//上游单据子单据体文本
var sourceRow = e.SourceActiveRow;
var sourceSubObjs = sourceField.Entity.DynamicProperty.GetValue(e.SourceActiveRow) as DynamicObjectCollection;
foreach (var subObj in sourceSubObjs)
{
sourceField.DynamicProperty.SetValue(subObj, "");
}
}
}
}
}
}
反写上游子单据体
本文2024-09-23 03:37:26发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-160034.html