再单据的【保存】按钮上挂服务插件实现保存时后台自动提交审核原创
金蝶云社区-搬砖
搬砖
10人赞赏了该文章 2,277次浏览 未经作者许可,禁止转载编辑于2021年04月25日 17:16:52

前文引用Python脚本实现保存自动提交审核 (kingdee.com)

在系统实施过程中,我们经常会遇到这样的需求:单据A审核通过之后,自动生成单据B,并自动审核,在没有开发资源支持的情况下,通过BOS平台预置的【服务端服务】里面的【自动下推】可以很方便的实现这一功能,并且可以实现单据间的关联及反写,但是却无法实现单据自动审核,现在可以通过Python脚本来补充这一功能:

实现方法:

1.在BOS中打开需要自动生成的下游单据。

2.增加【复选框】字段(字段标识:F_PATC_CheckBox1),自动审核,这个字段勾选的单据才执行自动提交审核,以达到不对所有单据进行自动审核的控制。(该字段 为后台逻辑判断字段,建议加到【其他】页签,避免干扰用户!!!)

2.在【操作列表】中找到【保存】,点击【编辑】。

3.在【其他控制】-【服务插件】中注册Python脚本。


当时想用上面这位大师的代码,现在才知道格式问题,当时死活没成功,遂弄了一个C#插件的,通过论坛工具转成Python脚本,提供一个C#插件转Python脚本的代码。注册到保存按钮即可


代码如下:

code='''
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;

namespace YunyanTest
{
    public class PurReq : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("F_PATC_CheckBox1");
        }

        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            foreach (DynamicObject entity in e.DataEntitys)
            {
                string FID = entity["ID"].ToString();

                string checkBox = entity["F_PATC_CheckBox1"].ToString();

                //this.BusinessInfo

                if (checkBox == "1" || checkBox == "True")
                {
                    ISubmitService submitService = ServiceHelper.GetService<ISubmitService>();
                    var submitResult = submitService.Submit(this.Context, this.BusinessInfo, new string[] { FID.ToString() }, "");

                    if (submitResult.IsSuccess)
                    {
                        OperateOption option = OperateOption.Create();
                        IAuditService auditService = ServiceHelper.GetService<IAuditService>();
                        var auditResult = auditService.Audit(this.Context, this.BusinessInfo, new string[] { FID.ToString() }, option);
                    }
                }


            }
        }
    }
}
'''

refDlls = '''
Kingdee.BOS
Kingdee.BOS.App
Kingdee.BOS.BusinessEntity
Kingdee.BOS.Contracts
Kingdee.BOS.Core
Kingdee.BOS.DataEntity
Kingdee.BOS.MC.ServiceHelper
Kingdee.BOS.ServiceFacade.KDServiceFx
Kingdee.BOS.ServiceHelper
Kingdee.BOS.Web
Kingdee.BOS.WebApi.Client
Kingdee.K3.SCM.Core
Newtonsoft.Json
System
System.Core
System.Data
System.Data.DataSetExtensions
System.Xml
System.Xml.Linq
YunyanTest
'''

clr.AddReference('System')
clr.AddReference('Kingdee.BOS')
from System import AppDomain
from System.IO import FileInfo
from System.Reflection import Assembly
from System.Reflection import BindingFlags
from System.CodeDom.Compiler import CompilerParameters
from Microsoft.CSharp import CSharpCodeProvider
from Kingdee.BOS.Core.Util import MD5Compute
from Kingdee.BOS.Cache import KCacheManagerFactory

def CompileCode(code, refDlls):	#动态编译C#代码,放入缓存
	refAsmNames = filter(lambda y: y <> '', map(lambda x: x.strip(), refDlls.split()))
	try:
		kcmgr = KCacheManagerFactory.Instance.GetCacheManager('PyCodeGeneratorCache', 'PyCodeGeneratorCache')
	except:
		return None
	cacheKey = MD5Compute().MDString(code + '-'.join(refAsmNames))
	if(kcmgr.Get(cacheKey) is not None):
		return kcmgr.Get(cacheKey)
	cSharpCodePrivoder = CSharpCodeProvider({'CompilerVersion':'v4.0'})
	codeCompiler = cSharpCodePrivoder.CreateCompiler()
	compilerParameters = CompilerParameters()
	compilerParameters.GenerateExecutable = False; 
	compilerParameters.GenerateInMemory = True; 
	for refAsmName in refAsmNames:
		asms = filter(lambda asm: asm.GetName().Name == refAsmName, AppDomain.CurrentDomain.GetAssemblies())
		if(len(asms) > 0):
			compilerParameters.ReferencedAssemblies.Add(FileInfo(asms[0].CodeBase.Substring(8)).DirectoryName.Replace('\\', '/')+'/' + refAsmName + '.dll')
		else:
			try:
				compilerParameters.ReferencedAssemblies.Add(FileInfo(Assembly.Load(refAsmName).CodeBase.Substring(8)).DirectoryName.Replace('\\', '/') + '/' + refAsmName + '.dll');
			except:
				pass
		compilerResults = codeCompiler.CompileAssemblyFromSource(compilerParameters, code);
	if (compilerResults.Errors.HasErrors):
		raise Exception('\r\n'.join(map(lambda err: err.ErrorText, compilerResults.Errors)))
	compiledAsm = compilerResults.CompiledAssembly;
	kcmgr.Put(cacheKey, compiledAsm)
	return compiledAsm

assembly = CompileCode(code, refDlls)
csPlugin = assembly.CreateInstance('YunyanTest.PurReq') if assembly is not None else None

def getPlugIn():
	csPlugin.SetContext(this.Context, this.BusinessInfo, this.FormOperation, this.Option)
	return csPlugin

def OnPreparePropertys(e):
    getPlugIn().OnPreparePropertys(e)

def EndOperationTransaction(e):
    getPlugIn().EndOperationTransaction(e)



代码基于【工具分享】C#插件转Python工具,C#高效开发 + Python快速部署 (kingdee.com) 这个帖子工具转成的


赞 10