金蝶云星空插件实现多单据体下推

例子为物流运输单下推物流结算单 

clr.AddReference('Kingdee.BOS.DataEntity')
clr.AddReference('Kingdee.BOS.ServiceHelper')
clr.AddReference('Kingdee.K3.SCM.App')
from Kingdee.BOS.Orm.DataEntity import *
from Kingdee.BOS.ServiceHelper import *
from Kingdee.K3.SCM.App.Utils import *
from System import *
#最后触发:单据转换后事件
def AfterConvert(e):
    billDatas = e.Result.FindByEntityKey("FBillHead");
    if billDatas is None or billDatas.Length < 1:
        return;
    for billData in billDatas:
        bill = billData.DataEntity;
        srcEntrys = bill["FSrcEntity"];
        if srcEntrys is None or srcEntrys.Count < 1:
            continue;
        if len(set(map(lambda x: x["SrcBillId"].ToString(), srcEntrys))) > 1:
            raise Exception("要求必须来源于同一张物流运输单");
        #查询运输单出库明细
        srcBillId = srcEntrys[0]["SrcBillId"].ToString();
        ds = DBServiceHelper.ExecuteDataSet(this.Context, "/*dialect*/select t1.FSrcCustId FStkCustId,t1.FSrcSaleDeptId FStkSaleDeptId,t1.FSrcOrgId FStkOrgId,t1.FSrcDeptId FStkDeptId,isnull(t2.F_TXBE_IsSettleByStkOrg,'') FIsSettleByStkOrg,sum(t1.FSrcTransQty) FStkQty,row_number()over(order by t1.FSrcCustId,t1.FSrcSaleDeptId,t1.FSrcOrgId,t1.FSrcDeptId) FRow from t_ora_TransBillSrcEntry t1 left join T_BD_Customer t2 on t1.FSrcCustId=t2.FCustId where t1.FID=" + srcBillId + " group by t1.FSrcCustId,t1.FSrcSaleDeptId,t1.FSrcOrgId,t1.FSrcDeptId,t2.F_TXBE_IsSettleByStkOrg");
        if ds is None or ds.Tables[0].Rows.Count < 1:
            raise Exception("查询运输单出库明细失败");
        totalQty = sum(map(lambda x: Convert.ToDecimal(x["FStkQty"]), ds.Tables[0].Rows));
        if totalQty <= 0:
            raise Exception("源单出库明细运输数量统计必须大于零");
        #按费用项目+税率归集金额
        dict1 = {};
        for srcEntry in srcEntrys:
            feeItemId = srcEntry["FeeItemId_Id"].ToString();
            taxRate = srcEntry["TaxRate"].ToString();
            curTransAmount = Convert.ToDecimal(srcEntry["CurTransAmount"]);
            key = feeItemId + ";" + taxRate;
            if key not in dict1:
                dict1[key] = curTransAmount;
            else:
                dict1[key] += curTransAmount;
        #循环费用项目循环添加结算明细
        entrys = bill["FEntity"];
        entrys.Clear();
        entity = e.TargetBusinessInfo.GetEntity("FEntity");
        FStkItemId = e.TargetBusinessInfo.GetField("FStkItemId");
        FStkCustId = e.TargetBusinessInfo.GetField("FStkCustId");
        FStkSaleDeptId = e.TargetBusinessInfo.GetField("FStkSaleDeptId");
        #获取对应的字段名称:发货组织
        FStkOrgId = e.TargetBusinessInfo.GetField("FStkOrgId");
        FStkDeptId = e.TargetBusinessInfo.GetField("FStkDeptId");
        FSettleOrgId = e.TargetBusinessInfo.GetField("FSettleOrgId");
        FSettleDeptId = e.TargetBusinessInfo.GetField("FSettleDeptId");
        for key in dict1:
            arr1 = key.Split(';');
            feeItemId = arr1[0];
            taxRate = Convert.ToDecimal(arr1[1]);
            totalAmount = dict1[key];
            for nRow in range(ds.Tables[0].Rows.Count):
                stkCustId = ds.Tables[0].Rows[nRow]["FStkCustId"].ToString();
                stkSaleDeptId = ds.Tables[0].Rows[nRow]["FStkSaleDeptId"].ToString();
                stkOrgId = ds.Tables[0].Rows[nRow]["FStkOrgId"].ToString();   #获取发货组织id
                stkDeptId = ds.Tables[0].Rows[nRow]["FStkDeptId"].ToString();
                isSettleByStkOrg = True if ds.Tables[0].Rows[nRow]["FIsSettleByStkOrg"].ToString() == "1" else False;
                stkQty = Convert.ToDecimal(ds.Tables[0].Rows[nRow]["FStkQty"]);
                rate = round(stkQty / totalQty * 100, 4);
                amount = round(totalAmount * rate / 100, 2);
                newEntry = DynamicObject(entity.DynamicObjectType);
                newEntry["Seq"] = entrys.Count + 1;
                FieldUtils.SetBaseDataFieldValue(this.Context, FStkItemId, newEntry, feeItemId);
                FieldUtils.SetBaseDataFieldValue(this.Context, FStkCustId, newEntry, stkCustId);
                FieldUtils.SetBaseDataFieldValue(this.Context, FStkSaleDeptId, newEntry, stkSaleDeptId);
                FieldUtils.SetBaseDataFieldValue(this.Context, FStkOrgId, newEntry, stkOrgId);
                FieldUtils.SetBaseDataFieldValue(this.Context, FStkDeptId, newEntry, stkDeptId);
                FieldUtils.SetBaseDataFieldValue(this.Context, FSettleOrgId, newEntry, stkOrgId);
                newEntry["StkQty"] = stkQty;
                newEntry["StdRate"] = rate;
                newEntry["StdAmount"] = amount;
                newEntry["IsShare"] = True;
                if isSettleByStkOrg:
                    #FieldUtils.SetBaseDataFieldValue(this.Context, FSettleOrgId, newEntry, stkOrgId);
                    FieldUtils.SetBaseDataFieldValue(this.Context, FSettleDeptId, newEntry, stkDeptId);
                else:
                    #FieldUtils.SetBaseDataFieldValue(this.Context, FSettleOrgId, newEntry, bill["SaleOrgId_Id"]);
                    FieldUtils.SetBaseDataFieldValue(this.Context, FSettleDeptId, newEntry, stkSaleDeptId);
                newEntry["SettleDate"] = DateTime.Now;
                newEntry["SettleAmount"] = amount;
                newEntry["SettleTaxRate"] = taxRate;
                newEntry["SettleAmountNoTax"] = round(amount / (1 + taxRate / 100), 2);
                newEntry["SettleRemark"] = "";
                newEntry["RelatedAPAmount"] = 0;
                entrys.Add(newEntry);
            #尾差处理
            sumAmount = sum(map(lambda y: Convert.ToDecimal(y["StdAmount"]), filter(lambda x: x["StkItemId_Id"].ToString() == feeItemId and Convert.ToDecimal(x["SettleTaxRate"]) == taxRate, entrys)));
            if sumAmount != totalAmount:
                amount = Convert.ToDecimal(entrys[entrys.Count - 1]["StdAmount"]) - (sumAmount - totalAmount);
                entrys[entrys.Count - 1]["StdAmount"] = amount;
                entrys[entrys.Count - 1]["SettleAmount"] = amount;
                entrys[entrys.Count - 1]["SettleAmountNoTax"] = round(amount / (1 + taxRate / 100), 2);
        #累计单头结算金额
        bill["TotalSettleAmount"] = sum(map(lambda y: Convert.ToDecimal(y["SettleAmount"]), filter(lambda x: Convert.ToBoolean(x["IsShare"]), entrys)));

posted on 2024-11-05 14:52  这一生,谢谢自己  阅读(11)  评论(0编辑  收藏  举报