基础资料审核自动分配python服务插件原创
金蝶云社区-吾自
吾自
12人赞赏了该文章 120次浏览 未经作者许可,禁止转载编辑于2024年12月13日 18:18:26

基础资料审批自动分配插件python改写



import clr
clr.AddReference('System')
clr.AddReference('Kingdee.BOS')
clr.AddReference('Kingdee.BOS.Core')
clr.AddReference('Kingdee.BOS.WebApi.FormService')
clr.AddReference('Kingdee.BOS.ServiceFacade.KDServiceFx')
clr.AddReference('Newtonsoft.Json')
clr.AddReference('System.Data')
clr.AddReference('Kingdee.BOS.App')
clr.AddReference('Kingdee.BOS.WebApi.Client')
clr.AddReference("mscorlib")
clr.AddReference('Kingdee.BOS.App.Core')
clr.AddReference('Kingdee.BOS.DataEntity')
clr.AddReference('Kingdee.BOS.ServiceHelper')
clr.AddReference('Kingdee.BOS.Contracts')
clr.AddReference('Kingdee.K3.Core')

from System import *
from System.Data import *
from System.Collections import *
from System.Collections.Generic import *
from System import StringComparison
from Kingdee.BOS.Log import Logger
from Kingdee.BOS import *
from Kingdee.BOS.Util import *
from Kingdee.BOS.App.Core import *
from Kingdee.BOS.App.Data import *
from Kingdee.BOS.Core.Bill.PlugIn import *
from Kingdee.BOS.Core.Const import *
from Kingdee.BOS.ServiceHelper import *
from Kingdee.BOS.Core.DynamicForm.PlugIn.Args import *
from System import Object
from System.Collections.Generic import Dictionary, List
from Kingdee.BOS.Core import *
from Kingdee.BOS.Core.DynamicForm import *
from Kingdee.BOS.Core.DynamicForm.PlugIn import *
from Kingdee.BOS.Core.Metadata import *
from Kingdee.BOS.Core.Metadata.Operation import *
from Kingdee.BOS.Log import Logger
from Kingdee.BOS.Orm.DataEntity import DynamicObject
from Kingdee.BOS.ServiceHelper import OrganizationServiceHelper, BusinessDataServiceHelper, DBServiceHelper
from Kingdee.BOS.Util import ObjectUtils
from System.Collections.Generic import Dictionary, List
from Kingdee.BOS.Core.Enums import BOSEnums


class OrganizationSimpleInfo:
    def __init__(self):
        self.OrgId = 0  # 组织ID
        self.OrgName = ""  # 组织名称


def AfterExecuteOperationTransaction(e):
    this.OperationResult.IsShowMessage = True
    result = OperateResult()
    #raise Exception(str(dir(e.DataEntitys)))
    try:
        # 按创建组织对基础资料进行分组
        dicBaseDataByCreateOrg = GetBaseDataByOrg(e.DataEntitys)
        #raise Exception(str(dir(dicBaseDataByCreateOrg)))
        if len(dicBaseDataByCreateOrg) == 0:
            # base.AfterExecuteOperationTransaction(e)
            return

        formId = this.BusinessInfo.GetForm().Id
        allocateLogId = GetAllocateLogId(formId)
        for data in dicBaseDataByCreateOrg:
            sourceOrgId = data.Key
            #raise Exception(str(sourceOrgId))
            pkIds = data.Value
            lstDestOrgs = []  # 每个按照源组织分组下的目标组织集合
            destOrg = OrganizationServiceHelper.GetAllocateOrg(this.Context, formId, sourceOrgId)
            for row in destOrg.Rows:
                destOrgId = ObjectUtils.Object2Int64(row["FOrgID"])
                destOrgName = ObjectUtils.Object2String(row["FName"])
                info = OrganizationSimpleInfo()
                info.OrgId = destOrgId
                info.OrgName = destOrgName
                lstDestOrgs.append(info)

            Allocate(this.Context, formId, sourceOrgId, lstDestOrgs, pkIds, allocateLogId)

    except Exception as ex:
        # 捕获并记录异常
        result.SuccessStatus = False
        result.Message = "处理异常:" + str(ex)
        this.OperationResult.OperateResult.Add(result)

    # base.AfterExecuteOperationTransaction(e)

def GetBaseDataByOrg(baseDatas):
    dicBaseDataByCreateOrg = Dictionary[long, List[Object]]()  # 按创建组织对基础资料进行分组
    #raise Exception(str(len(baseDatas)))
    for data in baseDatas:
        id = ObjectUtils.Object2Int64(data["Id"])
        createOrgId = ObjectUtils.Object2Int64(data["CreateOrgId_Id"])
        useOrgId = ObjectUtils.Object2Int64(data["UseOrgId_Id"])
        #raise Exception(str(id) + " " + str(createOrgId) + " " + str(useOrgId))
        # 源组织下需要分配
        if createOrgId == useOrgId and createOrgId != 0 and id != 0:
            if dicBaseDataByCreateOrg.ContainsKey(createOrgId):
                # 创建组织已有分组,将基础资料加入该分组
                hsBaseDatas = dicBaseDataByCreateOrg[createOrgId]
                hsBaseDatas.Add(id)
            else:
                # 创建组织没有分组,创建分组并加入基础资料
                hsBaseDatas = List[Object]()
                hsBaseDatas.Add(id)
                dicBaseDataByCreateOrg.Add(createOrgId, hsBaseDatas)

    return dicBaseDataByCreateOrg

def Allocate(ctx, formId, sourceOrgId, lstDestOrgs, pkIds, allocateLogId):
    metaData = FormMetaDataCache.GetCachedFormMetaData(ctx, formId)
    businessInfo = metaData.BusinessInfo

    allocateParameter = AllocateParameter(businessInfo,  # 业务对象元数据
                                            metaData.InheritPath,  # 当前基础资料的继承路径
                                            sourceOrgId,  # 创建组织ID
                                            BOSEnums.Enu_AllocateType.Allocate,  # 操作类型:分配
                                            OperationNumberConst.OperationNumber_Allocate)  # 操作编码

    # 记录分配日志记录编号
    allocateParameter.AllocateLogId = allocateLogId
    # 需要分配的业务对象ID
    allocateParameter.PkId = pkIds
    # 分配人
    allocateParameter.AllocateUserId = ctx.UserId
    # 自动提交审核
    allocateParameter.AutoSubmitAndAudit = True
    # 执行方式,'C' 表示客户开发调用
    allocateParameter.RunWay = "C"

    result = None

    for org in lstDestOrgs:
        # 目标组织
        allocateParameter.DestOrgId = org.OrgId
        allocateParameter.DestOrgName = org.OrgName

        # 此结果为每个目标组织的分配结果
        result = BusinessDataServiceHelper.Allocate(ctx, allocateParameter)

    # 这里只会返回最后一个目标组织的分配结果
    return result

def GetAllocateLogId(formId):
    seqs = DBServiceHelper.GetSequenceInt32(this.Context, "T_ORG_AUTOALLOCLOGINFO", 1)
    langSEQS = DBServiceHelper.GetSequenceInt32(this.Context, "T_ORG_AUTOALLOCLOGINFO_L", 1)

    sql = "INSERT INTO T_ORG_AUTOALLOCLOGINFO(FID, FSTARTTIME, FUSEDTIME, FRUNWAY, FRUNSTATUS, FOBJECTTYPEID) " \
            "VALUES({0}, NOW(), NOW(), 'C', 'F', '{1}');".format(seqs[0], formId)

    langSql = "INSERT INTO T_ORG_AUTOALLOCLOGINFO_L(FPKID, FID, FLOCALEID) " \
                "VALUES({0}, {1}, 2052);".format(langSEQS[0], seqs[0])

    DBServiceHelper.Execute(this.Context, sql)
    DBServiceHelper.Execute(this.Context, langSql)
    return seqs[0]

挂在到(客户,供应商,物料等分配型的基础资料审核操作上)

注意:基础资料控制规则那也要配置

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