如何控制单据头字段只反写一次原创
金蝶云社区-eris
eris
7人赞赏了该文章 1,934次浏览 未经作者许可,禁止转载编辑于2022年08月12日 10:58:25

说明:

   1. 单据反写规则的执行是按单据关联主实体数来执行的,主实体往往是单据体,所以在反写单据头执行了N次

   2. 适应情况,反写规则为累加或累减模式,单据头字段反写单据头字段


反写插件代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;

using Kingdee.BOS.Util;
using Kingdee.BOS.Core.BusinessFlow.PlugIn;
using Kingdee.BOS.Core.BusinessFlow.PlugIn.Args;
using Kingdee.BOS.BusinessEntity.BusinessFlow;

namespace Kingdee.BOS.TestPlugIn.Demo
{
    [Description("限制单据头只反写一次")]
    [Kingdee.BOS.Util.HotUpdate]
    public class WriteBackHeadPlugIn : AbstractBusinessFlowServicePlugIn
    {
        //需要干预的反写规则
        private bool _isNeedHandleRule = false;
        private bool _isFirstWriteBack = true;
        /// <summary>
        /// 反写前事件,每个反写规则走一次,得到需要干预反写规则
        /// </summary>
        /// <param name="e"></param>
        public override void BeforeWriteBack(BeforeWriteBackEventArgs e)
        {
            this._isNeedHandleRule = false;
            this._isFirstWriteBack = true;
            var ruleName = e.Rule.Name.ToString();
            if (ruleName.EqualsIgnoreCase("单据B反写单据A——单据头字段"))
            {
                this._isNeedHandleRule = true;
            }
        }
    /// <summary>     
        /// /// 反写条目反写后事件,每一行都会走     
        /// /// </summary>    
        /// /// <param name="e"></param>    
        public override void AfterCommitAmount(AfterCommitAmountEventArgs e)   
        {           
            base.AfterCommitAmount(e);        
            if (this._isNeedHandleRule)      
            {             
                //第一次反写不处理       
                if (this._isFirstWriteBack)    
                {                
                    this._isFirstWriteBack = false;     
                    return;           
                }            
                //第二次操作时,把相应的反写数据扣减或加回去         
                if (this.OperationNumber.EqualsIgnoreCase("Save") ||
                    this.OperationNumber.EqualsIgnoreCase("Delete")||
                    (this.OperationNumber.EqualsIgnoreCase("Audit") ||
                    this.OperationNumber.EqualsIgnoreCase("UnAudit"))     
                {            
                    var sField = e.SourceCommitField;//被反写字段      
                    var currWBValue= (e.WriteBackSourceRow as WSRow<Id>).Val;//当前反写值    
                    //反写后的值
                    var afterWBValue = Convert.ToDecimal(e.SourceDataObject[sField.PropertyName]);   
                    //累加模式,把反写值减掉      
                    e.SourceDataObject[sField.PropertyName] = afterWBValue - currWBValue;      
                    //累减模式,把反写值加回去
                    //e.SourceDataObject[sField.PropertyName] = afterWBValue + currWBValue;      
                }
            } 
    }
}



图标赞 7
7人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!