生产订单拆单业务二次开发

业务场景:

客户执行MRP计划,下的是一个大的生产订单,客户需要根据自定义的数量将生产订单拆分成多个小订单,关联关系保持和源单一样,这当中涉及到一些自定义字段的处理,需要二开。如果只是标准字段,可以利用系统标准的拆单功能,不必再次开发。

示例代码,仅供参考。

生产订单列表界面加一个拆单按钮,点击弹出一个动态表单,输入单数和每单数量,点确定进行拆单。

using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm.PlugIn.WizardForm;
using Kingdee.BOS.Orm.DataEntity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Bill.PlugIn;
using System.Data;
using Kingdee.BOS;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.List;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.WebApi.Client;

namespace MOListDisp
{
    [Description("生产订单列表插件")]
    public class MOListDisp : AbstractListPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.ToUpperInvariant() == "FCDBTN")
            {
                if (this.ListView.SelectedRowsInfo != null && this.ListView.SelectedRowsInfo.Count == 1)
                {
                    string Sql = "";
                    DataSet ds_Sql = new DataSet();
                    try
                    {
                        ListSelectedRow lsr = this.ListView.CurrentSelectedRowInfo;
                        WorkProInfo.Ori_FBillNo = lsr.BillNo;
                        WorkProInfo.Ori_EntryId = Convert.ToInt32(lsr.EntryPrimaryKeyValue);
                        WorkProInfo.Ori_Fid = Convert.ToInt32(lsr.PrimaryKeyValue);
                        //获取源单数据 需要根据单据编号判断开始结束编号有没有值,只拆分没有开始结束编号的单据
                        Sql = "select a.FBILLNO as 'FBILLNO',b.FSEQ as 'FSEQ',b.FQTY as 'FQTY',a.F_DEV_NO as 'F_DEV_NO',a.F_DEV_LOTS as 'F_DEV_LOTS',a.F_DEV_WORKNO as 'F_DEV_WORKNO',a.FDOCUMENTSTATUS as 'FDOCUMENTSTATUS',";
                        Sql = Sql + "c.FNUMBER as 'BillType',a.FDATE as 'FDate',d.FNUMBER as 'FPrdOrg',a.FOwnerTypeId as 'OwnerType',a.fBusinessType as 'BusinessType' ,a.fIsRework as 'IsRework' ,a.FTRUSTTEED as 'Trustteed',";
                        Sql = Sql + "a.FIsEntrust as 'IsEntrust',a.FPPBOMType as 'PPBOMType',a.FIssueMtrl as 'IssueMtrl',b.FProductType as 'ProductType',e.FNUMBER as 'WorkShop',";
                        Sql = Sql + "f.FNUMBER as 'Material',b.fPlanStartDate as 'PlanStartDate',b.FPLANFINISHDATE as 'PlanFinishDate',g.FNUMBER as 'RequestOrgId',";
                        Sql = Sql + "h.FNUMBER as 'Bom',i.FISBACKFLUSH as 'ISBACKFLUSH',j.FNUMBER as 'StockInOrg',i.FReqType as 'ReqType',i.FINSTOCKOWNERTYPEID as 'InStockOwnerType',i.FCheckProduct as 'CheckProduct',";
                        Sql = Sql + "k.FNUMBER as 'Stock',b.FCostRate as 'CostRate',i.FCreateType as 'CreateType',b.FYIELDRATE as 'YieldRate',b.FGROUP as 'Group',n.FNUMBER as 'InStockOwner',";
                        Sql = Sql + "l.FPickMtrlStatus as 'PickMtrlStatus',l.FMOChangeFlag as 'MOChangeFlag',b.FSrcBillType as 'SrcBillType',b.FSrcBillNo as 'SrcBillNo',m.FSBILLID as 'SrcBillId',m.FSID as 'SrcEntryId', ";
                        Sql = Sql + "i.FReqSrc as 'ReqSrc',b.FSaleOrderNo as 'SaleOrderNo' ,b.FSaleOrderEntrySeq as 'SaleOrderEntrySeq' ";
                        Sql = Sql + "from T_PRD_MO a left join T_PRD_MOENTRY b on a.FID = b.FID ";
                        Sql = Sql + "left join T_BAS_BILLTYPE c on a.FBILLTYPE = c.FBILLTYPEID left join T_ORG_Organizations d on a.FPRDORGID = d.FORGID ";
                        Sql = Sql + "left join T_BD_DEPARTMENT e on e.FDEPTID = b.FWORKSHOPID left join T_BD_MATERIAL f on b.FMATERIALID = f.FMATERIALID ";
                        Sql = Sql + "left join T_ORG_Organizations g on b.FREQUESTORGID = g.FORGID left join T_ENG_BOM h on b.FBOMID = h.FID ";
                        Sql = Sql + "left join T_PRD_MOENTRY_A i on i.FENTRYID = b.FENTRYID left join T_ORG_Organizations j on j.FORGID = b.FSTOCKINORGID ";
                        Sql = Sql + "left join T_BD_STOCK k on k.FSTOCKID = b.FSTOCKID left join T_PRD_MOENTRY_Q l on l.FENTRYID = b.FENTRYID ";
                        Sql = Sql + "left join T_PRD_MOENTRY_LK m on b.FENTRYID = m.FENTRYID left join T_ORG_Organizations n on n.FORGID = i.FInStockOwnerId ";                        
                        Sql = Sql + "where a.FBILLNO = '" + lsr.BillNo + "' and a.F_DEV_BEGINNO = '' and a.F_DEV_ENDNO = '' and b.FENTRYID = '" + Convert.ToInt32(lsr.EntryPrimaryKeyValue) + "' and a.F_DEV_NO <> '' and a.F_DEV_LOTS <> '' and a.F_DEV_WORKNO <> ''";
                        ds_Sql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(this.Context,Sql);
                        if (ds_Sql.Tables[0].Rows.Count > 0)
                        {
                            if (ds_Sql.Tables[0].Rows[0]["FDOCUMENTSTATUS"].ToString() == "A" || ds_Sql.Tables[0].Rows[0]["FDOCUMENTSTATUS"].ToString() == "D")
                            {
                                WorkProInfo.Ori_FQty = Convert.ToDecimal(ds_Sql.Tables[0].Rows[0]["FQTY"].ToString());
                                WorkProInfo.Ori_Seq = Convert.ToInt16(ds_Sql.Tables[0].Rows[0]["FSEQ"].ToString());
                                WorkProInfo.Ori_No = ds_Sql.Tables[0].Rows[0]["F_DEV_NO"].ToString();
                                WorkProInfo.Ori_Lots = ds_Sql.Tables[0].Rows[0]["F_DEV_LOTS"].ToString();
                                WorkProInfo.Ori_WorkNo = ds_Sql.Tables[0].Rows[0]["F_DEV_WORKNO"].ToString();
                                WorkProInfo.Ori_BillType = ds_Sql.Tables[0].Rows[0]["BillType"].ToString();
                                WorkProInfo.Ori_Date = ds_Sql.Tables[0].Rows[0]["FDate"].ToString();
                                WorkProInfo.Ori_PrdOrg = ds_Sql.Tables[0].Rows[0]["FPrdOrg"].ToString();
                                WorkProInfo.Ori_OwnerType = ds_Sql.Tables[0].Rows[0]["OwnerType"].ToString();
                                WorkProInfo.Ori_BusinessType = ds_Sql.Tables[0].Rows[0]["BusinessType"].ToString();
                                if (ds_Sql.Tables[0].Rows[0]["IsRework"].ToString() == "0")
                                {
                                    WorkProInfo.Ori_IsRework = false;
                                }
                                else
                                {
                                    WorkProInfo.Ori_IsRework = true;
                                }

                                if (ds_Sql.Tables[0].Rows[0]["Trustteed"].ToString() == "0")
                                {
                                    WorkProInfo.Ori_Trustteed = false;
                                }
                                else
                                {
                                    WorkProInfo.Ori_Trustteed = true;
                                }
                                if (ds_Sql.Tables[0].Rows[0]["IsEntrust"].ToString() == "0")
                                {
                                    WorkProInfo.Ori_IsEntrust = false;
                                }
                                else
                                {
                                    WorkProInfo.Ori_IsEntrust = true;
                                }
                                
                                WorkProInfo.Ori_PPBOMType = ds_Sql.Tables[0].Rows[0]["PPBOMType"].ToString();
                                if (ds_Sql.Tables[0].Rows[0]["IssueMtrl"].ToString() == "0")
                                {
                                    WorkProInfo.Ori_IssueMtrl = false;
                                }
                                else
                                {
                                    WorkProInfo.Ori_IssueMtrl = true;
                                }                                
                                WorkProInfo.Ori_ProductType = ds_Sql.Tables[0].Rows[0]["ProductType"].ToString();
                                WorkProInfo.Ori_WorkShop = ds_Sql.Tables[0].Rows[0]["WorkShop"].ToString();
                                WorkProInfo.Ori_Material = ds_Sql.Tables[0].Rows[0]["Material"].ToString();
                                WorkProInfo.Ori_PlanStartDate = ds_Sql.Tables[0].Rows[0]["PlanStartDate"].ToString();
                                WorkProInfo.Ori_PlanFinishDate = ds_Sql.Tables[0].Rows[0]["PlanFinishDate"].ToString();
                                WorkProInfo.Ori_RequestOrgId = ds_Sql.Tables[0].Rows[0]["RequestOrgId"].ToString();
                                WorkProInfo.Ori_Bom = ds_Sql.Tables[0].Rows[0]["Bom"].ToString();
                                if (ds_Sql.Tables[0].Rows[0]["ISBACKFLUSH"].ToString() == "0")
                                {
                                    WorkProInfo.Ori_ISBACKFLUSH = false;
                                }
                                else
                                {
                                    WorkProInfo.Ori_ISBACKFLUSH = true;
                                }                                
                                WorkProInfo.Ori_StockInOrg = ds_Sql.Tables[0].Rows[0]["StockInOrg"].ToString();
                                WorkProInfo.Ori_ReqType = ds_Sql.Tables[0].Rows[0]["ReqType"].ToString();
                                WorkProInfo.Ori_InStockOwnerType = ds_Sql.Tables[0].Rows[0]["InStockOwnerType"].ToString();
                                if (ds_Sql.Tables[0].Rows[0]["CheckProduct"].ToString() == "0")
                                {
                                    WorkProInfo.Ori_CheckProduct = false;
                                }
                                else
                                {
                                    WorkProInfo.Ori_CheckProduct = true;
                                }                                
                                WorkProInfo.Ori_Stock = ds_Sql.Tables[0].Rows[0]["Stock"].ToString();
                                WorkProInfo.Ori_CostRate = ds_Sql.Tables[0].Rows[0]["CostRate"].ToString();
                                WorkProInfo.Ori_CreateType = ds_Sql.Tables[0].Rows[0]["CreateType"].ToString();
                                WorkProInfo.Ori_YieldRate = ds_Sql.Tables[0].Rows[0]["YieldRate"].ToString();
                                WorkProInfo.Ori_Group = ds_Sql.Tables[0].Rows[0]["Group"].ToString();
                                WorkProInfo.Ori_PickMtrlStatus = ds_Sql.Tables[0].Rows[0]["PickMtrlStatus"].ToString();
                                if (ds_Sql.Tables[0].Rows[0]["MOChangeFlag"].ToString() == "0")
                                {
                                    WorkProInfo.Ori_MOChangeFlag = false;
                                }
                                else
                                {
                                    WorkProInfo.Ori_MOChangeFlag = true;
                                }
                                WorkProInfo.Ori_InStockOwner = ds_Sql.Tables[0].Rows[0]["InStockOwner"].ToString();
                                WorkProInfo.Ori_SrcBillType = ds_Sql.Tables[0].Rows[0]["SrcBillType"].ToString();
                                WorkProInfo.Ori_SrcBillNo = ds_Sql.Tables[0].Rows[0]["SrcBillNo"].ToString();
                                WorkProInfo.Ori_SrcBillId = ds_Sql.Tables[0].Rows[0]["SrcBillId"].ToString();
                                WorkProInfo.Ori_SrcEntryId = ds_Sql.Tables[0].Rows[0]["SrcEntryId"].ToString();
                                

                                DynamicFormShowParameter showParam = new DynamicFormShowParameter();
                                showParam.FormId = Common.formid;
                                this.View.ShowForm(showParam,
                                    new Action<FormResult>((formResult) =>
                                    {

                                    }));
                            }
                            else
                            {
                                this.View.ShowMessage("只能拆分创建状态的单据!");
                            }
                        }
                        else
                        {
                            this.View.ShowMessage("单据已拆分!");
                        }
                    }
                    catch (Exception ee)
                    {

                        this.View.ShowMessage(ee.Message);
                    }
                }
                if (this.ListView.SelectedRowsInfo != null && this.ListView.SelectedRowsInfo.Count > 1)
                {
                    this.View.ShowMessage("只能选择一张生产订单进行拆分!");
                }                
            }
        }
    }

    [Description("演示如何从子界面返回数据 - 子界面插件")]
    public class ReturnInfoChild : AbstractDynamicFormPlugIn
    {
        K3CloudApiClient client = new K3CloudApiClient(Common.url);//正式环境
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            // 用户点击确定按钮
            if (e.Key.ToUpperInvariant().Equals("FBTN"))  // 确认
            {
                JObject jsonRoot;
                JObject jsonRootXL;
                JArray Fields;
                string SaveResult = "";
                string autrjson = "";
                string result = "";
                JObject model;
                JObject modelXL;
                JObject basedata;
                JArray entryRows;
                JArray entryRowsXL;
                JArray subentryRows;
                JObject entryRow;
                JObject entryRowXL;
                JObject subentryRow;
                JObject josave;
                string entityKey = "";
                string entityKeyXL = "";
                string subentityKey = "";
                string number = "";
                string id = "";
                bool flg = true;
                //第一步判断数量*单数是不是大于父单据的数量
                int FBills = 0;
                decimal FQty = 0;
                decimal FLeftQty = 0;
                string SerSql = "";
                int SerCount;
                DataSet ds_SerSql = new DataSet();
                FBills = Convert.ToInt16(this.View.Model.GetValue("FBILLS").ToString());//单数
                FQty = Convert.ToDecimal(this.View.Model.GetValue("FQTY").ToString());//每单数量
                
                if (FBills > 0 && FQty > 0)
                {
                    if (FBills * FQty >= WorkProInfo.Ori_FQty || FQty >= 10000)
                    {
                        this.View.ShowMessage("拆单总数量大于源单据数量!或者每单数量大于10000!");
                    }
                    else
                    {
                        //修改父单据的数量以及开始编号和结束编号
                        FLeftQty = WorkProInfo.Ori_FQty - FBills * FQty;
                        //调用生产订单的保存API,修改实收数量,开始编号,结束编号
                        jsonRoot = new JObject();
                        jsonRoot.Add("Creator", "");
                        Fields = new JArray();
                        Fields.Add("F_DEV_BEGINNO");
                        Fields.Add("F_DEV_ENDNO");
                        Fields.Add("FTreeEntity");
                        Fields.Add("FQty");
                        Fields.Add("FSerialSubEntity");
                        Fields.Add("FDetailID");
                        Fields.Add("FSNQty1");
                        Fields.Add("FSerialNo");
                        Fields.Add("FBaseSNQty");
                        jsonRoot.Add("NeedUpDateFields", Fields);
                        jsonRoot.Add("NeedReturnFields", new JArray());
                        jsonRoot.Add("IsDeleteEntry", "false");
                        jsonRoot.Add("SubSystemId", "");
                        jsonRoot.Add("IsVerifyBaseDataField", "false");
                        jsonRoot.Add("IsEntryBatchFill", "True");
                        jsonRoot.Add("ValidateFlag", "True");
                        jsonRoot.Add("NumberSearch", "True");
                        jsonRoot.Add("InterationFlags", "STK_InvCheckResult");
                        // Model: 单据详细数据参数
                        model = new JObject();
                        jsonRoot.Add("Model", model);
                        // 单据主键:必须填写,系统据此判断是新增还是修改单据;新增单据,填0
                        model.Add("FID", WorkProInfo.Ori_Fid.ToString());
                        model.Add("F_DEV_BEGINNO", WorkProInfo.Ori_Lots + "0000");//开始编号
                        model.Add("F_DEV_ENDNO", WorkProInfo.Ori_Lots + ((int)(FLeftQty-1)).ToString().PadLeft(4,'0'));//结束编号

                        entryRows = new JArray();
                        // 把单据体行集合,添加到model中,以单据体Key为标识
                        entityKey = "FTreeEntity";
                        model.Add(entityKey, entryRows);
                        entryRow = new JObject();
                        entryRows.Add(entryRow);
                        entryRow.Add("FEntryID", WorkProInfo.Ori_EntryId.ToString());
                        entryRow.Add("FQty", FLeftQty);
                        //SN号
                        //判断序列号主档里是否有重复物料重复批号的序列号
                        SerSql = @"/*dialect*/select COUNT(*) as 'Count' from T_BD_SERIALMASTER where FNUMBER like '" + WorkProInfo.Ori_Material + "SN" + WorkProInfo.Ori_Lots + "%'";
                        ds_SerSql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(this.Context,SerSql);
                        SerCount = Convert.ToInt32(ds_SerSql.Tables[0].Rows[0]["Count"].ToString());
                        if (SerCount == 0)
                        {
                            //先把序列号生成到序列号主档
                            if (FLeftQty < 10000)
                            {
                                for (int xl = SerCount; xl < SerCount + FLeftQty; xl++)
                                {
                                    jsonRootXL = new JObject();
                                    jsonRootXL.Add("Creator", "");
                                    jsonRootXL.Add("NeedUpDateFields", new JArray());
                                    jsonRootXL.Add("NeedReturnFields", new JArray());
                                    jsonRootXL.Add("IsDeleteEntry", "true");
                                    jsonRootXL.Add("SubSystemId", "");
                                    jsonRootXL.Add("IsVerifyBaseDataField", "false");
                                    jsonRootXL.Add("IsEntryBatchFill", "true");
                                    jsonRootXL.Add("ValidateFlag", "true");
                                    jsonRootXL.Add("NumberSearch", "true");
                                    jsonRootXL.Add("InterationFlags", "");
                                    jsonRootXL.Add("IsAutoSubmitAndAudit", "false");
                                    modelXL = new JObject();
                                    jsonRootXL.Add("Model", modelXL);
                                    modelXL.Add("FSERIALID", 0);
                                    modelXL.Add("FNumber", WorkProInfo.Ori_Material + "SN" + WorkProInfo.Ori_Lots + xl.ToString().PadLeft(4, '0'));
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_Material);
                                    modelXL.Add("FMaterialID", basedata);

                                    entryRowsXL = new JArray();
                                    // 把单据体行集合,添加到model中,以单据体Key为标识
                                    entityKeyXL = "FOrgEntity";
                                    modelXL.Add(entityKeyXL, entryRowsXL);
                                    entryRowXL = new JObject();
                                    entryRowsXL.Add(entryRowXL);
                                    entryRowXL.Add("FENTRYID", 0);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", "101.2");
                                    entryRowXL.Add("FOrgId", basedata);
                                    SaveResult = Save("BD_SerialMainFile", jsonRootXL.ToString());
                                    try
                                    {
                                        josave = (JObject)JsonConvert.DeserializeObject(SaveResult);
                                        number = josave["Result"]["Number"].ToString();
                                        id = josave["Result"]["Id"].ToString();
                                        if (number == "")
                                        {
                                            flg = false;
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        flg = false;
                                    }
                                }
                            }
                            else
                            {
                                flg = false;
                            }
                        }
                        else
                        {
                            if (SerCount + FLeftQty < 10000)
                            {
                                for (int xl = SerCount; xl < FLeftQty + SerCount; xl++)
                                {
                                    jsonRootXL = new JObject();
                                    jsonRootXL.Add("Creator", "");
                                    jsonRootXL.Add("NeedUpDateFields", new JArray());
                                    jsonRootXL.Add("NeedReturnFields", new JArray());
                                    jsonRootXL.Add("IsDeleteEntry", "true");
                                    jsonRootXL.Add("SubSystemId", "");
                                    jsonRootXL.Add("IsVerifyBaseDataField", "false");
                                    jsonRootXL.Add("IsEntryBatchFill", "true");
                                    jsonRootXL.Add("ValidateFlag", "true");
                                    jsonRootXL.Add("NumberSearch", "true");
                                    jsonRootXL.Add("InterationFlags", "");
                                    jsonRootXL.Add("IsAutoSubmitAndAudit", "false");
                                    modelXL = new JObject();
                                    jsonRootXL.Add("Model", modelXL);
                                    modelXL.Add("FSERIALID", 0);
                                    modelXL.Add("FNumber", WorkProInfo.Ori_Material + "SN" + WorkProInfo.Ori_Lots + xl.ToString().PadLeft(4, '0'));
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_Material);
                                    modelXL.Add("FMaterialID", basedata);

                                    entryRowsXL = new JArray();
                                    // 把单据体行集合,添加到model中,以单据体Key为标识
                                    entityKeyXL = "FOrgEntity";
                                    modelXL.Add(entityKeyXL, entryRowsXL);
                                    entryRowXL = new JObject();
                                    entryRowsXL.Add(entryRowXL);
                                    entryRowXL.Add("FENTRYID", 0);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", "101.2");
                                    entryRowXL.Add("FOrgId", basedata);
                                    SaveResult = Save("BD_SerialMainFile", jsonRootXL.ToString());
                                    try
                                    {
                                        josave = (JObject)JsonConvert.DeserializeObject(SaveResult);
                                        number = josave["Result"]["Number"].ToString();
                                        id = josave["Result"]["Id"].ToString();
                                        if (number == "")
                                        {
                                            flg = false;
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        flg = false;
                                    }
                                }
                            }
                            else
                            {
                                flg = false;
                            }                            
                        }
                        if (flg)
                        {
                            subentryRows = new JArray();
                            subentityKey = "FSerialSubEntity";
                            entryRow.Add(subentityKey, subentryRows);
                            for (int k = SerCount; k < SerCount + FLeftQty; k++)
                            {
                                subentryRow = new JObject();
                                subentryRows.Add(subentryRow);
                                subentryRow.Add("FDetailID", 0);
                                subentryRow.Add("FSNQty1", 1);
                                subentryRow.Add("FSerialNo", WorkProInfo.Ori_Material + "SN" + WorkProInfo.Ori_Lots + k.ToString().PadLeft(4, '0'));
                                subentryRow.Add("FBaseSNQty", 1);
                            }
                            SaveResult = Save("PRD_MO", jsonRoot.ToString());
                            try
                            {
                                josave = (JObject)JsonConvert.DeserializeObject(SaveResult);
                                number = josave["Result"]["Number"].ToString();
                                id = josave["Result"]["Id"].ToString();
                                if (number != "")
                                {
                                    autrjson = "{\"CreateOrgId\":0,\"Numbers\":[\"" + WorkProInfo.Ori_FBillNo + "\"],\"InterationFlags\":\"STK_InvCheckResult\"}";
                                    result = Audit("PRD_MO", autrjson);
                                }
                                #region 利用保存API生成拆出来的单据,并修改批号,工作令号,开始编号,结束编号
                                //利用保存API生成拆出来的单据,并修改批号,工作令号,开始编号,结束编号
                                for (int i = 0; i < FBills; i++)
                                {
                                    jsonRoot = new JObject();
                                    jsonRoot.Add("Creator", "");
                                    jsonRoot.Add("NeedUpDateFields", new JArray());
                                    jsonRoot.Add("NeedReturnFields", new JArray());
                                    jsonRoot.Add("IsDeleteEntry", "false");
                                    jsonRoot.Add("SubSystemId", "");
                                    jsonRoot.Add("IsVerifyBaseDataField", "false");
                                    jsonRoot.Add("IsEntryBatchFill", "True");
                                    jsonRoot.Add("ValidateFlag", "True");
                                    jsonRoot.Add("NumberSearch", "True");
                                    jsonRoot.Add("InterationFlags", "STK_InvCheckResult");
                                    // Model: 单据详细数据参数
                                    model = new JObject();
                                    jsonRoot.Add("Model", model);
                                    // 单据主键:必须填写,系统据此判断是新增还是修改单据;新增单据,填0
                                    model.Add("FID", 0);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_BillType);
                                    model.Add("FBillType", basedata);
                                    model.Add("FDate", WorkProInfo.Ori_Date);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_PrdOrg);
                                    model.Add("FPrdOrgId", basedata);
                                    model.Add("FOwnerTypeId", WorkProInfo.Ori_OwnerType);
                                    model.Add("F_DEV_NO", WorkProInfo.Ori_No);
                                    model.Add("F_DEV_LOTS", (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString());
                                    model.Add("F_DEV_WORKNO", WorkProInfo.Ori_No + "-" + (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString());
                                    model.Add("F_DEV_BEGINNO", (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString() + "0000");
                                    model.Add("F_DEV_ENDNO", (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString() + ((int)(FQty - 1)).ToString().PadLeft(4, '0'));
                                    model.Add("FBusinessType", WorkProInfo.Ori_BusinessType);
                                    model.Add("FIsRework", WorkProInfo.Ori_IsRework);
                                    model.Add("FTrustteed", WorkProInfo.Ori_Trustteed);
                                    model.Add("FIsEntrust", WorkProInfo.Ori_IsEntrust);
                                    model.Add("FPPBOMType", WorkProInfo.Ori_PPBOMType);
                                    model.Add("FIssueMtrl", WorkProInfo.Ori_IssueMtrl);
                                    entryRows = new JArray();
                                    // 把单据体行集合,添加到model中,以单据体Key为标识
                                    entityKey = "FTreeEntity";
                                    model.Add(entityKey, entryRows);
                                    entryRow = new JObject();
                                    entryRows.Add(entryRow);
                                    entryRow.Add("FEntryID", 0);
                                    entryRow.Add("FSrcBillType", WorkProInfo.Ori_SrcBillType);
                                    entryRow.Add("FSrcBillNo", WorkProInfo.Ori_SrcBillNo);
                                    entryRow.Add("FProductType", WorkProInfo.Ori_ProductType);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_Material);
                                    entryRow.Add("FMaterialId", basedata);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_WorkShop);
                                    entryRow.Add("FWorkShopID", basedata);
                                    entryRow.Add("FQty", FQty);
                                    entryRow.Add("FYieldQty", FQty);
                                    entryRow.Add("FPlanStartDate", WorkProInfo.Ori_PlanStartDate);
                                    entryRow.Add("FPlanFinishDate", WorkProInfo.Ori_PlanFinishDate);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_RequestOrgId);
                                    entryRow.Add("FRequestOrgId", basedata);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_Bom);
                                    entryRow.Add("FBomId", basedata);
                                    entryRow.Add("FISBACKFLUSH", WorkProInfo.Ori_ISBACKFLUSH);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_StockInOrg);
                                    entryRow.Add("FStockInOrgId", basedata);
                                    entryRow.Add("FBaseYieldQty", FQty);
                                    entryRow.Add("FReqType", WorkProInfo.Ori_ReqType);
                                    entryRow.Add("FInStockOwnerTypeId", WorkProInfo.Ori_InStockOwnerType);
                                    entryRow.Add("FBaseStockInLimitH", FQty);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_InStockOwner);
                                    entryRow.Add("FInStockOwnerId", basedata);
                                    entryRow.Add("FCheckProduct", WorkProInfo.Ori_CheckProduct);
                                    entryRow.Add("FBaseStockInLimitL", FQty);
                                    entryRow.Add("FBaseUnitQty", FQty);
                                    basedata = new JObject();
                                    basedata.Add("FNumber", WorkProInfo.Ori_Stock);
                                    entryRow.Add("FStockId", basedata);
                                    entryRow.Add("FStockInLimitH", FQty);
                                    entryRow.Add("FCostRate", WorkProInfo.Ori_CostRate);
                                    entryRow.Add("FCreateType", "6");
                                    entryRow.Add("FYieldRate", WorkProInfo.Ori_YieldRate);
                                    entryRow.Add("FGroup", WorkProInfo.Ori_Group);
                                    entryRow.Add("FNoStockInQty", FQty);
                                    entryRow.Add("FBaseNoStockInQty", FQty);
                                    entryRow.Add("FPickMtrlStatus", WorkProInfo.Ori_PickMtrlStatus);
                                    entryRow.Add("FMOChangeFlag", WorkProInfo.Ori_MOChangeFlag);
                                    entryRow.Add("FSrcSplitBillNo", WorkProInfo.Ori_FBillNo);
                                    entryRow.Add("FSrcSplitSeq", 1);
                                    //SN号
                                    //判断序列号主档里是否有重复物料重复批号的序列号
                                    SerSql = @"/*dialect*/select COUNT(*) as 'Count' from T_BD_SERIALMASTER where FNUMBER like '" + WorkProInfo.Ori_Material + "SN" + (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString() + "%'";
                                    ds_SerSql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(this.Context, SerSql);
                                    SerCount = Convert.ToInt32(ds_SerSql.Tables[0].Rows[0]["Count"].ToString());
                                    if (SerCount == 0)
                                    {
                                        //先把序列号生成到序列号主档
                                        for (int xl = SerCount; xl < SerCount + FQty; xl++)
                                        {
                                            jsonRootXL = new JObject();
                                            jsonRootXL.Add("Creator", "");
                                            jsonRootXL.Add("NeedUpDateFields", new JArray());
                                            jsonRootXL.Add("NeedReturnFields", new JArray());
                                            jsonRootXL.Add("IsDeleteEntry", "true");
                                            jsonRootXL.Add("SubSystemId", "");
                                            jsonRootXL.Add("IsVerifyBaseDataField", "false");
                                            jsonRootXL.Add("IsEntryBatchFill", "true");
                                            jsonRootXL.Add("ValidateFlag", "true");
                                            jsonRootXL.Add("NumberSearch", "true");
                                            jsonRootXL.Add("InterationFlags", "");
                                            jsonRootXL.Add("IsAutoSubmitAndAudit", "false");
                                            modelXL = new JObject();
                                            jsonRootXL.Add("Model", modelXL);
                                            modelXL.Add("FSERIALID", 0);
                                            modelXL.Add("FNumber", WorkProInfo.Ori_Material + "SN" + (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString() + xl.ToString().PadLeft(4, '0'));
                                            basedata = new JObject();
                                            basedata.Add("FNumber", WorkProInfo.Ori_Material);
                                            modelXL.Add("FMaterialID", basedata);

                                            entryRowsXL = new JArray();
                                            // 把单据体行集合,添加到model中,以单据体Key为标识
                                            entityKeyXL = "FOrgEntity";
                                            modelXL.Add(entityKeyXL, entryRowsXL);
                                            entryRowXL = new JObject();
                                            entryRowsXL.Add(entryRowXL);
                                            entryRowXL.Add("FENTRYID", 0);
                                            basedata = new JObject();
                                            basedata.Add("FNumber", "101.2");
                                            entryRowXL.Add("FOrgId", basedata);
                                            SaveResult = Save("BD_SerialMainFile", jsonRootXL.ToString());
                                        }

                                        subentryRows = new JArray();
                                        subentityKey = "FSerialSubEntity";
                                        entryRow.Add(subentityKey, subentryRows);
                                        for (int k = SerCount; k < SerCount + FQty; k++)
                                        {
                                            subentryRow = new JObject();
                                            subentryRows.Add(subentryRow);
                                            subentryRow.Add("FDetailID", 0);
                                            subentryRow.Add("FSNQty1", 1);
                                            subentryRow.Add("FSerialNo", WorkProInfo.Ori_Material + "SN" + (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString() + k.ToString().PadLeft(4, '0'));
                                            subentryRow.Add("FBaseSNQty", 1);
                                        }
                                    }
                                    else
                                    {
                                        if (SerCount + FQty < 10000)
                                        {
                                            for (int xl = SerCount; xl < SerCount + FQty; xl++)
                                            {
                                                jsonRootXL = new JObject();
                                                jsonRootXL.Add("Creator", "");
                                                jsonRootXL.Add("NeedUpDateFields", new JArray());
                                                jsonRootXL.Add("NeedReturnFields", new JArray());
                                                jsonRootXL.Add("IsDeleteEntry", "true");
                                                jsonRootXL.Add("SubSystemId", "");
                                                jsonRootXL.Add("IsVerifyBaseDataField", "false");
                                                jsonRootXL.Add("IsEntryBatchFill", "true");
                                                jsonRootXL.Add("ValidateFlag", "true");
                                                jsonRootXL.Add("NumberSearch", "true");
                                                jsonRootXL.Add("InterationFlags", "");
                                                jsonRootXL.Add("IsAutoSubmitAndAudit", "false");
                                                modelXL = new JObject();
                                                jsonRootXL.Add("Model", modelXL);
                                                modelXL.Add("FSERIALID", 0);
                                                modelXL.Add("FNumber", WorkProInfo.Ori_Material + "SN" + (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString() + xl.ToString().PadLeft(4, '0'));
                                                basedata = new JObject();
                                                basedata.Add("FNumber", WorkProInfo.Ori_Material);
                                                modelXL.Add("FMaterialID", basedata);

                                                entryRowsXL = new JArray();
                                                // 把单据体行集合,添加到model中,以单据体Key为标识
                                                entityKeyXL = "FOrgEntity";
                                                modelXL.Add(entityKeyXL, entryRowsXL);
                                                entryRowXL = new JObject();
                                                entryRowsXL.Add(entryRowXL);
                                                entryRowXL.Add("FENTRYID", 0);
                                                basedata = new JObject();
                                                basedata.Add("FNumber", "101.2");
                                                entryRowXL.Add("FOrgId", basedata);
                                                SaveResult = Save("BD_SerialMainFile", jsonRootXL.ToString());
                                            }
                                            subentryRows = new JArray();
                                            subentityKey = "FSerialSubEntity";
                                            entryRow.Add(subentityKey, subentryRows);
                                            for (int k = SerCount; k < SerCount + FQty; k++)
                                            {
                                                subentryRow = new JObject();
                                                subentryRows.Add(subentryRow);
                                                subentryRow.Add("FDetailID", 0);
                                                subentryRow.Add("FSNQty1", 1);
                                                subentryRow.Add("FSerialNo", WorkProInfo.Ori_Material + "SN" + (Convert.ToInt32(WorkProInfo.Ori_Lots) + i + 1).ToString() + k.ToString().PadLeft(4, '0'));
                                                subentryRow.Add("FBaseSNQty", 1);
                                            }
                                        }
                                    }
                                    // 创建Link行集合
                                    JArray linkRows = new JArray();
                                    // 添加到单据体行中:Link子单据体标识 = 关联主单据体标识(POOrderEntry) + _Link
                                    string linkEntityKey = string.Format("{0}_Link", entityKey);
                                    entryRow.Add(linkEntityKey, linkRows);
                                    // 创建Link行:
                                    // 如有多条源单行,则分别创建Link行记录各条源单行信息
                                    JObject linkRow = new JObject();
                                    linkRows.Add(linkRow);
                                    // 填写Link行上的字段值
                                    // 特别说明:Link子单据体上字段的标识,必须在前面增加子单据体标识

                                    // FRuleId :两单之间的转换规则内码,必填
                                    // 可以通过如下SQL语句到数据库获取
                                    string RuleSql = "";
                                    DataSet ds_RuleSql = new DataSet();
                                    RuleSql = "select * from T_META_CONVERTRULE where FSOURCEFORMID = '" + WorkProInfo.Ori_SrcBillType + "' and FTARGETFORMID = 'PRD_MO' and FDEVTYPE = 0 ";
                                    ds_RuleSql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(this.Context, RuleSql);
                                    // select FID, *
                                    //   from T_META_CONVERTRULE
                                    //  where FSOURCEFORMID = 'PUR_Requisition'
                                    //    and FTARGETFORMID = 'PUR_PurchaseOrder'
                                    //    and FDEVTYPE = 0;
                                    string fldRuleIdKey = string.Format("{0}_FRuleId", linkEntityKey);
                                    linkRow.Add(fldRuleIdKey, ds_RuleSql.Tables[0].Rows[0]["FID"].ToString());
                                    // FSTableName :必填,源单单据体表格编码,通过如下语句获取:
                                    // SELECT FTableNumber
                                    //   FROM t_bf_tabledefine
                                    //  WHERE fformid = 'PUR_Requisition'
                                    //    AND fentitykey = 'FEntity'
                                    // 如果如上语句未返回结果,请到K/3 Cloud中,手工选单一次,后台会自动产生表格编码
                                    string TabSql = "";
                                    DataSet ds_TabSql = new DataSet();
                                    if (WorkProInfo.Ori_SrcBillType == "PLN_PLANORDER")//计划订单下推
                                    {
                                        TabSql = "SELECT * FROM t_bf_tabledefine WHERE fformid = '" + WorkProInfo.Ori_SrcBillType + "' AND fentitykey = 'FBillHead'";
                                    }
                                    else//销售订单下推
                                    {
                                        TabSql = "SELECT * FROM t_bf_tabledefine WHERE fformid = '" + WorkProInfo.Ori_SrcBillType + "' AND fentitykey = 'FSaleOrderEntry'";
                                    }
                                    ds_TabSql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(this.Context, TabSql);
                                    string fldSTableNameKey = string.Format("{0}_FSTableName", linkEntityKey);
                                    linkRow.Add(fldSTableNameKey, ds_TabSql.Tables[0].Rows[0]["FTABLENUMBER"].ToString());

                                    ////通过计划订单号获取计划订单内码和单据体分录内码
                                    // FSBillId :必填,源单单据内码
                                    string fldSBillIdKey = string.Format("{0}_FSBillId", linkEntityKey);
                                    linkRow.Add(fldSBillIdKey, WorkProInfo.Ori_SrcBillId);

                                    // FSId : 必填,源单单据体行内码。如果源单主关联实体是单据头,则此属性也填写源单单据内码
                                    string fldSIdKey = string.Format("{0}_FSId", linkEntityKey);
                                    linkRow.Add(fldSIdKey, WorkProInfo.Ori_SrcEntryId);

                                    SaveResult = Save("PRD_MO", jsonRoot.ToString());
                                    try
                                    {
                                        josave = (JObject)JsonConvert.DeserializeObject(SaveResult);
                                        number = josave["Result"]["Number"].ToString();
                                        id = josave["Result"]["Id"].ToString();
                                        if (number != "")
                                        {
                                            autrjson = "{\"CreateOrgId\":0,\"Numbers\":[\"" + number + "\"],\"InterationFlags\":\"STK_InvCheckResult\"}";
                                            result = Audit("PRD_MO", autrjson);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        throw;
                                    }
                                }
                                #endregion
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                        }
                        else
                        {
                            this.View.ShowMessage("拆单失败!");
                        }
                        
                    }                    
                }
                this.View.Close();
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public bool Login()
        {
            string dbid = Common.dbid;//正式环境
            //string dbid = "5dd23c391be677";//测试环境
            string username = Common.username;
            string password = Common.password;
            var loginResult = client.Login(
                               dbid,
                               username,
                               password,
                               2052);
            return loginResult;
            //return true;
        }
        public string Save(string formid, string jsonstr)
        {
            string result = "登录失败,请检查与站点地址、数据中心Id,用户名及密码!";
            //string result = "";
            if (Login())
            {
                result = client.Execute<string>(
                                               "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save",
                                               new object[] { formid, jsonstr });
            }


            return result;
        }
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="formid"></param>
        /// <param name="jsonstr"></param>
        /// <returns></returns>
        public string Audit(string formid, string jsonstr)
        {
            string result = "登录失败,请检查与站点地址、数据中心Id,用户名及密码!";
            result = client.Execute<string>("Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit", new object[] { formid, jsonstr });
            result = client.Execute<string>("Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit", new object[] { formid, jsonstr });
            return result;
        }
    }


    /// <summary>
    /// 由子窗体返回给父窗体的数据对象
    /// </summary>
    public class ReturnDataInfo
    {
        public decimal F_PAEZ_QTY { get; set; }
    }


    public static class WorkProInfo
    {
        public static string  Ori_FBillNo { set; get; }//父单据编号
        public static decimal Ori_FQty { set; get; }//父单据数量
        public static int     Ori_EntryId { set; get; }//父单据分录编码
        public static int     Ori_Fid { set; get; }//父单据单据内码
        public static string  Ori_No { set; get; }//父单据令号
        public static string  Ori_Lots { set; get; }//父单据批次号
        public static string  Ori_WorkNo { set; get; }//父单据工作令号
        public static int     Ori_Seq { set; get; }//父单据分录行号
        public static string  Ori_BillType { set; get; }//父单据单据类型编码
        public static string  Ori_Date { set; get; }//父单据单据日期
        public static string  Ori_PrdOrg { set; get; }//父单据生产组织
        public static string  Ori_OwnerType { set; get; }//父单据货主类型
        public static string  Ori_BusinessType { set; get; }//父单据销售业务类型
        public static bool    Ori_IsRework { set; get; }//父单据是否反工
        public static bool    Ori_Trustteed { set; get; }//父单据销受托
        public static bool    Ori_IsEntrust { set; get; }//父单据组织受托加工
        public static string  Ori_PPBOMType { set; get; }//父单据用料清单展开
        public static bool    Ori_IssueMtrl { set; get; }//父单据生产发料
        public static string  Ori_ProductType { set; get; }//父单据产品类型
        public static string  Ori_WorkShop { set; get; }//父单据生产车间
        public static string  Ori_Material { set; get; }//父单据物料编码
        public static string  Ori_PlanStartDate { set; get; }//父单据计划开工时间
        public static string  Ori_PlanFinishDate { set; get; }//父单据计划完工时间
        public static string  Ori_RequestOrgId { set; get; }//父单据需求组织
        public static string  Ori_Bom { set; get; }//父单据BOM版本
        public static bool    Ori_ISBACKFLUSH { set; get; }//父单据倒冲领料
        public static string  Ori_StockInOrg { set; get; }//父单据入库组织
        public static string  Ori_ReqType { set; get; }//父单据需求类型
        public static string  Ori_InStockOwnerType { set; get; }//父单据入库货主类型
        public static string  Ori_InStockOwner { set; get; }//父单据入库货主
        public static bool    Ori_CheckProduct { set; get; }//父单据产品检验
        public static string  Ori_Stock { set; get; }//父单据仓库
        public static string  Ori_CostRate { set; get; }//父单据成本权重
        public static string  Ori_CreateType { set; get; }//父单据生成方式
        public static string  Ori_YieldRate { set; get; }//父单据成品率
        public static string  Ori_Group { set; get; }//父单据组别
        public static string  Ori_PickMtrlStatus { set; get; }//父单据领料状态
        public static bool    Ori_MOChangeFlag { set; get; }//父单据变更标志
        public static string  Ori_SrcBillType { set; get; }//父单据源单类型
        public static string  Ori_SrcBillNo { set; get; }//父单据源单编码
        public static string  Ori_SrcBillId { set; get; }//父单据源单内码
        public static string  Ori_SrcEntryId { set; get; }//父单据源单分录内码
        public static string  Ori_ReqSrc { set; get; }//父单据需求来源
        public static string  Ori_SaleOrderNo { set; get; }//父单据需求单据
        public static string  Ori_SaleOrderEntrySeq { set; get; }//父单据需求单据行号
        public static string  Ori_MatNo { set; get; }//父单据物料编码
    }

}

posted @ 2019-12-22 14:37  放放的Blog  阅读(1216)  评论(0编辑  收藏  举报