系统默认的生产用料清单下推生成调拨单功能,是根据调拨选单数量来的,有库存和没有库存的都混在一起,导致业务人员审核调拨单的时候需要删除没有库存的分录行,严重影响工作效率。

现通过二开程序,根据生产用料清单分录行物料库存数据进行处理,调拨单审核自动根据未完成调拨的生产用料清单生成调拨单

转换插件

  1  public override void AfterConvert(AfterConvertEventArgs e)
2 {
3 base.AfterConvert(e);
4 ExtendedDataEntity[] entityArray = e.Result.FindByEntityKey("FBillHead");
5 foreach (ExtendedDataEntity entity in entityArray)
6 {
7 DynamicObject billObj = entity.DataEntity;
8 DynamicObjectCollection mx = billObj["TransferDirectEntry"] as DynamicObjectCollection;
9 long orgId = Convert.ToInt64(billObj["StockOrgId_Id"].ToString());
10
11
12 DynamicObject factwh = queryWarehouse(orgId, 2);
13 DynamicObject rawwh = queryWarehouse(orgId, 1);
14 /******设置表头调出仓库*******/
15 BaseDataField stockFld = e.TargetBusinessInfo.GetField("FRFOutWarehouse") as BaseDataField;
16 long stockId = Convert.ToInt64(rawwh["FSTOCKID"]);
17 IViewService viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();
18 DynamicObject[] stockObjs = viewService.LoadFromCache(this.Context,
19 new object[] { stockId },
20 stockFld.RefFormDynamicObjectType);
21 stockFld.RefIDDynamicProperty.SetValue(billObj, stockId);
22 stockFld.DynamicProperty.SetValue(billObj, stockObjs[0]);
23 /***********设置表头调入仓库******************/
24 stockFld = e.TargetBusinessInfo.GetField("FRFInWarehouse") as BaseDataField;
25 stockId = Convert.ToInt64(factwh["FSTOCKID"]);
26 viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();
27 stockObjs = viewService.LoadFromCache(this.Context,
28 new object[] { stockId },
29 stockFld.RefFormDynamicObjectType);
30 stockFld.RefIDDynamicProperty.SetValue(billObj, stockId);
31 stockFld.DynamicProperty.SetValue(billObj, stockObjs[0]);
32 /*******************/
33 DynamicObjectCollection rawCols = queryInventorys(orgId, 1);
34 //IEnumerable<DynamicObject> eqInvs = (from raw in rawCols
35 // from stk in mx
36 // where raw["FSTOCKORGID"].ToString().Equals(stk["KeeperId_Id"].ToString())
37 // && raw["FMATERIALID"].ToString().Equals(stk["MaterialId_Id"].ToString()) select raw).ToList();
38 //int eqcounts=eqInvs.Count();
39 bool eqflag = false;
40 //bool eqflag = eqcounts < mx.Count &&eqcounts>0 ? false : true;//判断生产用料是否有库存,有库存为false,没有库存为true
41 /***********/
42 double usedqty = 0;
43 double leaveqty = 0;
44 double reqty = 0;
45 double actualqty = 0;
46 double invsqty = 0;
47 string srcBillNo = string.Empty;
48 List<pickMaterial> pickList = new List<pickMaterial>();
49 int k = 0;
50 srcBillNo = mx[0]["SrcBillNo"].ToString();
51 DynamicObjectCollection ppbomInvs = queryPPPbom(srcBillNo, orgId);
52 eqflag = ppbomInvs.Count() > 0 ? false : true;
53 foreach (var item in mx)
54 {
55 var material = item["MaterialId"] as DynamicObject;
56 long materialid = 0;
57 bool flag = Int64.TryParse(material["id"].ToString(), out materialid);
58 double rawqtys = 0;
59 double factqtys = 0;
60 //billObj["FRFOutWarehouse_Id"] = rawwh["FSTOCKID"];
61 //billObj["FRFInWarehouse_Id"] = factwh["FSTOCKID"];
62
63 if (flag)
64 {
65 DynamicObjectCollection factorywhinvDatas = queryInventorys(orgId, 2, materialid);//车间仓即时库存数量
66 DynamicObjectCollection rawmaterialwhinvDatas = queryInventorys(orgId, 1, materialid);//原材料仓即时库存数量
67 if (rawmaterialwhinvDatas.Count() > 0)
68 {
69 rawqtys = Convert.ToDouble(rawmaterialwhinvDatas[0]["fbaseqty"].ToString());
70 }
71 if (factorywhinvDatas.Count() > 0)
72 {
73 factqtys = Convert.ToDouble(factorywhinvDatas[0]["fbaseqty"].ToString());
74 }
75 double noTranslateQty = Convert.ToDouble(item["QTY"].ToString());
76 if (!eqflag)
77 {//有库存
78 //if (rawqtys > 0)
79 //{
80 // if (rawqtys < noTranslateQty)
81 // {
82 // item["QTY"] = rawqtys;
83 // item["BaseQty"] = rawqtys;
84 // }
85 // else
86 // {
87 // item["QTY"] = noTranslateQty;
88 // item["BaseQty"] = noTranslateQty;
89 // } ;
90 //}
91 //else
92 //{
93 // item["QTY"] = 0;
94 // item["BaseQty"] = 0;
95 //}
96 //item["FRFStockQty"] = rawqtys;
97 /**考虑下面的物料有重复的逻辑处理 2020-8-19**/
98 invsqty = QueryMaterialStock(orgId, materialid, Convert.ToInt64(rawwh["FSTOCKID"]));
99 List<pickMaterial> find = pickList.Where(p => p.materialId.Equals(materialid)).ToList();
100 reqty = Convert.ToDouble(item["QTY"].ToString());
101 if (find.Count() <= 0)
102 {
103 if (invsqty > 0)
104 {
105 usedqty = invsqty < reqty ? invsqty : reqty;
106 actualqty = invsqty < reqty ? invsqty : reqty;
107 leaveqty = invsqty - usedqty;
108 pickList.Add(new pickMaterial { materialId = materialid, usedQty = usedqty, leavingQty = leaveqty, invsQty = invsqty });
109 //Console.WriteLine("申请数量:" + reqty + " 实发数量:" + actualqty + "已占用数量:" + usedqty + " 剩余库存数量:" + leaveqty + " 即时库存数量:" + invsqty + "");
110 //this.Model.SetValue("FActualQty", actualqty, i);
111 //this.Model.SetValue("FRFStockQty", invsqty, i);
112 item["QTY"] = actualqty;
113 item["BaseQty"] = actualqty;
114 item["FRFStockQty"] = invsqty;
115
116 }
117 else
118 {
119 item["QTY"] = 0;
120 item["BaseQty"] = 0;
121 item["FRFStockQty"] = 0;
122 }
123
124 }
125 else
126 {
127 if (find.Count() > 0)
128 {
129 if (find[0].leavingQty > 0)
130 {
131 usedqty = (find[0].leavingQty < reqty ? find[0].leavingQty : reqty) + find[0].usedQty;
132 actualqty = find[0].leavingQty < reqty ? find[0].leavingQty : reqty;
133 leaveqty = invsqty - usedqty;
134 int index = pickList.FindIndex(p => p.materialId.Equals(materialid));
135 find[0].invsQty = invsqty;
136 find[0].usedQty = usedqty;
137 find[0].leavingQty = leaveqty;
138 //Console.WriteLine("申请数量:" + reqty + " 实发数量:" + actualqty + "已占用数量:" + usedqty + " 剩余库存数量:" + leaveqty + " 即时库存数量:" + invsqty + "");
139 //this.Model.SetValue("FActualQty", actualqty, i);
140 //this.Model.SetValue("FRFStockQty", invsqty, i);
141 item["QTY"] = actualqty;
142 item["BaseQty"] = actualqty;
143 item["FRFStockQty"] = invsqty;
144 }
145 else
146 {
147 //this.Model.SetValue("FActualQty", 0, i);
148 //this.Model.SetValue("FRFStockQty", invsqty, i);
149 item["QTY"] = 0;
150 item["BaseQty"] = 0;
151 }
152
153
154 }
155 }
156 }
157 else
158 {//没有库存
159 item["QTY"] = noTranslateQty;
160 item["BaseQty"] = noTranslateQty;
161 item["FRFStockQty"] = 0;
162 }
163 /***设置表体调出仓库***/
164 stockFld = e.TargetBusinessInfo.GetField("FSRCSTOCKID") as BaseDataField;
165 stockId = Convert.ToInt64(rawwh["FSTOCKID"]);
166 viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();
167 stockObjs = viewService.LoadFromCache(this.Context,
168 new object[] { stockId },
169 stockFld.RefFormDynamicObjectType);
170 stockFld.RefIDDynamicProperty.SetValue(item, stockId);
171 stockFld.DynamicProperty.SetValue(item, stockObjs[0]);
172 /***设置表体调入仓库***/
173 stockFld = e.TargetBusinessInfo.GetField("FDestStockId") as BaseDataField;
174 stockId = Convert.ToInt64(factwh["FSTOCKID"]);
175 viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();
176 stockObjs = viewService.LoadFromCache(this.Context,
177 new object[] { stockId },
178 stockFld.RefFormDynamicObjectType);
179 stockFld.RefIDDynamicProperty.SetValue(item, stockId);
180 stockFld.DynamicProperty.SetValue(item, stockObjs[0]);
181 }
182 }
183 }
184 }

审核服务插件

金蝶k/3 cloud 生产用料清单下推生成调拨单二开记录-LMLPHP金蝶k/3 cloud 生产用料清单下推生成调拨单二开记录-LMLPHP

  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Kingdee.BOS.Core.DynamicForm.PlugIn;
6 using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
7 using Kingdee.BOS.Orm.DataEntity;
8 using Kingdee.BOS.Core.Validation;
9 using System.ComponentModel;
10 using Kingdee.BOS.Core;
11 using Kingdee.BOS.App.Data;
12 using Kingdee.BOS.Core.DynamicForm;
13 using Kingdee.BOS.ServiceHelper;
14 using Kingdee.BOS.Core.List;
15 using Kingdee.BOS.Core.DynamicForm.Operation;
16 using Kingdee.BOS.Core.Metadata.ConvertElement;
17 using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
18 using Kingdee.BOS.Core.Metadata;
19 using Kingdee.BOS.Orm;
20 using Kingdee.BOS.Util;
21 using Kingdee.BOS.Core.Interaction;
22 using Kingdee.BOS.App;
23 using Kingdee.BOS.App.Core;
24 using Kingdee.BOS.Contracts;
25
26 namespace RF.K3.ServicePlugIn.Stock
27 {
28 [Kingdee.BOS.Util.HotUpdate]
29 [Description("直接调拨单操作插件")]
30 public class TransferOp : AbstractOperationServicePlugIn
31 {
32 public override void OnPreparePropertys(PreparePropertysEventArgs e)
33 {
34 //e.FieldKeys.Add("");将需要应用的字段Key加入
35 base.OnPreparePropertys(e);
36 e.FieldKeys.Add("FEntryID");
37 e.FieldKeys.Add("FTransferDirectEntry");
38 e.FieldKeys.Add("FMaterialId");
39 e.FieldKeys.Add("FQTY");
40 e.FieldKeys.Add("FRFSTOCKQTY");
41 e.FieldKeys.Add("FSrcBillTypeId");
42 e.FieldKeys.Add("FSrcBillNo");
43 }
44
45 public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
46 {
47 base.BeforeExecuteOperationTransaction(e);
48 //生成调拨单前提:源单类型必须是生产用料清单,本张调拨单中并非所有分录行没有即时库存(只有部分没有即时库存)
49
50 }
51 public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
52 {
53 base.AfterExecuteOperationTransaction(e);
54 /*******提交后根据上游的生产用料清单下推生成调拨单*********/
55 foreach (DynamicObject dy in e.DataEntitys)
56 {
57 DynamicObjectCollection dycolls = dy["TransferDirectEntry"] as DynamicObjectCollection;
58 string stranSrcbillType = dycolls[0]["SrcBillTypeId"].ToString();
59 string stranSrcbill = dycolls[0]["SrcBillNo"].ToString();
60 if (stranSrcbillType.Equals("PRD_PPBOM"))
61 {
62 //PushpppBomToStkTransfer(stranSrcbill);
63 ListSelectedRow[] selectedRows = querySelectRows(stranSrcbill);
64 PushpppBomToStkTransfer(selectedRows);
65 }
66
67 }
68
69 }
70 public ListSelectedRow[] querySelectRows(string pppbomBillNumber)
71 {
72 /*****
73 * 2020-9-24 章坚华修改
74 * 针对调拨单生成时调拨数量不能为0的校验规则,通过对生产用料清单的数据进行过滤选择
75 * 如果一张生产用料清单所有物料无库存,则默认下推所有数据至调拨单
76 * 如果只是部分物料无库存,则先下推有库存的物料,待调拨单审核后再下推无库存的物料
77 * *****/
78 DynamicObjectCollection ppBoms = queryprdPPBom(pppbomBillNumber);
79 long fprdorgId = ppBoms.Count() > 0 ? Convert.ToInt64(ppBoms[0]["FPRDORGID"]) : 0;
80 int k = 0;
81 if (!fprdorgId.Equals(0))
82 {
83 DynamicObjectCollection prdppBoms = queryprdPPBomInvetories(pppbomBillNumber, fprdorgId, 1);
84 if (prdppBoms.Count <= 0)
85 {
86 prdppBoms = queryprdPPBomInvetories(pppbomBillNumber, fprdorgId, 2);
87 }
88 ListSelectedRow[] selectedRows = new ListSelectedRow[prdppBoms.Count];
89 ListSelectedRow row;
90 foreach (DynamicObject d in prdppBoms)
91 {
92 string primaryKeyValue = d["Fid"].ToString();
93 string entryId = d["FENTRYID"].ToString();
94 int seq = Convert.ToInt32(d["FSEQ"].ToString());
95 row = new ListSelectedRow(primaryKeyValue, entryId, 0, "PRD_PPBOM")
96 {
97 EntryEntityKey = "FEntity"
98 };
99 selectedRows[k] = row;
100 k++;
101 }
102 return selectedRows;
103 }
104 return null;
105 //foreach (DynamicObject d in ppBoms)
106 //{
107 // string primaryKeyValue = d["Fid"].ToString();
108 // string entryId = d["FENTRYID"].ToString();
109 // int seq = Convert.ToInt32(d["FSEQ"].ToString());
110 // row = new ListSelectedRow(primaryKeyValue, entryId, 0, "PRD_PPBOM")
111 // {
112 // EntryEntityKey = "FEntity"
113 // };
114 // selectedRows[k] = row;
115 // k++;
116 //}
117
118 }
119 public void PushpppBomToStkTransfer(ListSelectedRow[] selectedRows)
120 {
121 if(selectedRows.Count()>0)
122 {
123 try
124 {
125 ConvertOperationResult operationResult = null;
126 ConvertRuleElement rule = ConvertServiceHelper.GetConvertRules(this.Context, "PRD_PPBOM", "STK_TransferDirect").FirstOrDefault<ConvertRuleElement>(t => t.Id == "PRD_PPBOM-STK_TransferDirect");
127
128 PushArgs pushArgs = new PushArgs(rule, selectedRows)
129 {
130 TargetBillTypeId = "ce8f49055c5c4782b65463a3f863bb4a" // 请设定目标单据单据类型。如无单据类型,可以空字符
131 };
132 var convertService = ServiceHelper.GetService<IConvertService>();
133 operationResult = convertService.Push(this.Context, pushArgs);
134 DynamicObject[] objs = (from p in operationResult.TargetDataEntities
135 select p.DataEntity).ToArray();
136 var targetBillMeta = MetaDataServiceHelper.Load(this.Context, "STK_TransferDirect") as FormMetadata;
137 OperateOption saveOption = OperateOption.Create();
138 saveOption.SetIgnoreWarning(true); // 忽略交互提示
139 saveOption.SetIgnoreScopeValidateFlag(true);
140 saveOption.SetIgnoreInteractionFlag(true);
141 //saveOption.set(true); // 提交数据库保存,并获取保存结果
142 //var saveResult = BusinessDataServiceHelper.Draft(this.Context, targetBillMeta.BusinessInfo, objs, saveOption);
143 var saveResult = BusinessDataServiceHelper.Save(this.Context, targetBillMeta.BusinessInfo, objs, saveOption,"Save");
144 string save = ((IOperationResult)saveResult).IsSuccess.ToString();
145 }
146 catch (Exception ex)
147 {
148 throw ex;
149 }
150 }
151 }
152
153 #region
154 /*******
155 *
156 * ************/
157 public DynamicObjectCollection queryprdPPBom(string billNO)
158 {
159 return DBUtils.ExecuteDynamicObject(this.Context, "select a.FID,b.FENTRYID,b.FSEQ,a.FPRDORGID from T_PRD_PPBOM a inner join T_PRD_PPBOMENTRY b on a.fid=b.FID and a.FBILLNO='" + billNO + "'");
160 }
161 /// <summary>
162 /// 查询调拨单源单类型
163 /// </summary>
164 /// <param name="fid"></param>
165 /// <returns></returns>
166 public DynamicObject querystranSrcbillType(long fid)
167 {
168 return DBUtils.ExecuteDynamicObject(this.Context, "select top 1 b.FSrcBillTypeId from T_STK_STKTRANSFERIN a " +
169 " inner join T_STK_STKTRANSFERINENTRY_R b on a.fid = b.fid and a.fid = " + fid + "").FirstOrDefault<DynamicObject>();
170 }
171 /// <summary>
172 /// 查询事业部即时库存数量
173 /// </summary>
174 /// <param name="orgId">事业部Id</param>
175 /// <param name="wareHouetype">仓库类型 1材料 2 车间仓</param>
176 /// <param name="materialId">物料ID</param>
177 /// <returns></returns>
178 public DynamicObjectCollection queryInventorys(long orgId, int wareHousetype, long materialId)
179 {
180 return DBUtils.ExecuteDynamicObject(this.Context, "select sum(FBASEQTY) fbaseqty,m.FMATERIALID,invs.FSTOCKID,invs.FSTOCKORGID from T_STK_INVENTORY invs inner join " +
181 " T_BD_STOCK stock on invs.FSTOCKID = stock.FSTOCKID and stock.FRFWAREHOUSETYPE = " + wareHousetype + " and FSTOCKORGID = " + orgId + "" +
182 " inner join T_BD_MATERIAL m on m.FMASTERID = invs.FMATERIALID and m.FUSEORGID = invs.FSTOCKORGID and m.fmaterialid=" + materialId + "" +
183 " group by m.FMATERIALID, invs.FSTOCKID, invs.FSTOCKORGID");
184 }
185 /// <summary>
186 /// 生产用料清单数据查询(库存数据)
187 /// </summary>
188 /// <param name="billNo">生产用料清单编号</param>
189 /// <param name="prdOrgId">生产组织内码</param>
190 /// <param name="invsType">库存类型标识 1 有库存 2 没有库存</param>
191 /// <returns></returns>
192 public DynamicObjectCollection queryprdPPBomInvetories(string billNo,long prdOrgId,int invsType)
193 {
194 string sql = string.Empty;
195 if (invsType.Equals(1))
196 {
197 sql = "select b.FMATERIALID,b.fbaseqty,b.fnumber,a.FENTRYID,a.FSEQ,a.fid from(select b.FMATERIALID, b.FENTRYID,b.FSEQ,a.fid from T_PRD_PPBOM a " +
198 " inner join T_PRD_PPBOMENTRY b on a.fid = b.FID and a.FBILLNO = '"+billNo+"' and b.FMATERIALTYPE<>2 " +
199 " inner join T_PRD_PPBOMENTRY_C c on a.fid = c.FID and b.FENTRYID = c.FENTRYID and(c.FISSUETYPE = 3 or c.FISSUETYPE = 4)" +
200 " inner join T_PRD_PPBOMENTRY_Q q on q.FENTRYID = b.FENTRYID and q.FENTRYID = c.FENTRYID and q.FID = a.FID and b.FMUSTQTY > q.FSELTRANSLATEQTY) a" +
201 " inner join(select sum(FBASEQTY) fbaseqty, m.FMATERIALID, invs.FSTOCKID, invs.FSTOCKORGID, m.FNUMBER from T_STK_INVENTORY invs " +
202 "inner join T_BD_STOCK stock on invs.FSTOCKID = stock.FSTOCKID and stock.FRFWAREHOUSETYPE = 1 and FSTOCKORGID = "+prdOrgId+"" +
203 " inner join T_BD_MATERIAL m on m.FMASTERID = invs.FMATERIALID and m.FUSEORGID = invs.FSTOCKORGID" +
204 " group by m.FMATERIALID, invs.FSTOCKID, invs.FSTOCKORGID, m.FNUMBER having sum(FBASEQTY) > 0) b on a.FMATERIALID = b.FMATERIALID";
205 }
206 if (invsType.Equals(2))
207 {
208 sql = "select b.FMATERIALID,b.fbaseqty,b.fnumber,a.FENTRYID,a.FSEQ,a.fid from(select b.FMATERIALID, b.FENTRYID,b.FSEQ,a.fid from T_PRD_PPBOM a " +
209 " inner join T_PRD_PPBOMENTRY b on a.fid = b.FID and a.FBILLNO = '" + billNo + "' and b.FMATERIALTYPE<>2 " +
210 " inner join T_PRD_PPBOMENTRY_C c on a.fid = c.FID and b.FENTRYID = c.FENTRYID and(c.FISSUETYPE = 3 or c.FISSUETYPE = 4)" +
211 " inner join T_PRD_PPBOMENTRY_Q q on q.FENTRYID = b.FENTRYID and q.FENTRYID = c.FENTRYID and q.FID = a.FID and b.FMUSTQTY > q.FSELTRANSLATEQTY) a" +
212 " inner join(select sum(FBASEQTY) fbaseqty, m.FMATERIALID, invs.FSTOCKID, invs.FSTOCKORGID, m.FNUMBER from T_STK_INVENTORY invs " +
213 "inner join T_BD_STOCK stock on invs.FSTOCKID = stock.FSTOCKID and stock.FRFWAREHOUSETYPE = 1 and FSTOCKORGID = " + prdOrgId + "" +
214 " inner join T_BD_MATERIAL m on m.FMASTERID = invs.FMATERIALID and m.FUSEORGID = invs.FSTOCKORGID" +
215 " group by m.FMATERIALID, invs.FSTOCKID, invs.FSTOCKORGID, m.FNUMBER having sum(FBASEQTY) <= 0) b on a.FMATERIALID = b.FMATERIALID";
216 }
217 return DBUtils.ExecuteDynamicObject(this.Context, sql);
218 }
219 #endregion
220 }
221 }

public override void AfterConvert(AfterConvertEventArgs e)        {            base.AfterConvert(e);            ExtendedDataEntity[] entityArray = e.Result.FindByEntityKey("FBillHead");            foreach (ExtendedDataEntity entity in entityArray)            {                DynamicObject billObj = entity.DataEntity;                DynamicObjectCollection mx = billObj["TransferDirectEntry"] as DynamicObjectCollection;                long orgId = Convert.ToInt64(billObj["StockOrgId_Id"].ToString());
                               DynamicObject factwh = queryWarehouse(orgId, 2);                DynamicObject rawwh = queryWarehouse(orgId, 1);                /******设置表头调出仓库*******/                BaseDataField stockFld = e.TargetBusinessInfo.GetField("FRFOutWarehouse") as BaseDataField;                long stockId = Convert.ToInt64(rawwh["FSTOCKID"]);                IViewService viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();                DynamicObject[] stockObjs = viewService.LoadFromCache(this.Context,                    new object[] { stockId },                    stockFld.RefFormDynamicObjectType);                stockFld.RefIDDynamicProperty.SetValue(billObj, stockId);                stockFld.DynamicProperty.SetValue(billObj, stockObjs[0]);                /***********设置表头调入仓库******************/                stockFld = e.TargetBusinessInfo.GetField("FRFInWarehouse") as BaseDataField;                stockId = Convert.ToInt64(factwh["FSTOCKID"]);                viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();                stockObjs = viewService.LoadFromCache(this.Context,                    new object[] { stockId },                    stockFld.RefFormDynamicObjectType);                stockFld.RefIDDynamicProperty.SetValue(billObj, stockId);                stockFld.DynamicProperty.SetValue(billObj, stockObjs[0]);                /*******************/                DynamicObjectCollection rawCols = queryInventorys(orgId, 1);                //IEnumerable<DynamicObject> eqInvs = (from raw in rawCols                //                                     from stk in mx                //                                     where raw["FSTOCKORGID"].ToString().Equals(stk["KeeperId_Id"].ToString())                //                                     && raw["FMATERIALID"].ToString().Equals(stk["MaterialId_Id"].ToString()) select raw).ToList();                //int eqcounts=eqInvs.Count();                bool eqflag = false;                //bool eqflag = eqcounts < mx.Count &&eqcounts>0 ? false : true;//判断生产用料是否有库存,有库存为false,没有库存为true                /***********/                double usedqty = 0;                double leaveqty = 0;                double reqty = 0;                double actualqty = 0;                double invsqty = 0;                string srcBillNo = string.Empty;                List<pickMaterial> pickList = new List<pickMaterial>();                int k = 0;                srcBillNo = mx[0]["SrcBillNo"].ToString();                DynamicObjectCollection ppbomInvs = queryPPPbom(srcBillNo, orgId);                eqflag = ppbomInvs.Count() > 0 ? false : true;                foreach (var item in mx)                {                    var material = item["MaterialId"] as DynamicObject;                    long materialid = 0;                    bool flag = Int64.TryParse(material["id"].ToString(), out materialid);                    double rawqtys = 0;                    double factqtys = 0;                    //billObj["FRFOutWarehouse_Id"] = rawwh["FSTOCKID"];                                      //billObj["FRFInWarehouse_Id"] = factwh["FSTOCKID"];                                        if (flag)                    {                        DynamicObjectCollection factorywhinvDatas = queryInventorys(orgId, 2, materialid);//车间仓即时库存数量                        DynamicObjectCollection rawmaterialwhinvDatas = queryInventorys(orgId, 1, materialid);//原材料仓即时库存数量                        if (rawmaterialwhinvDatas.Count() > 0)                        {                            rawqtys = Convert.ToDouble(rawmaterialwhinvDatas[0]["fbaseqty"].ToString());                        }                        if (factorywhinvDatas.Count() > 0)                        {                            factqtys = Convert.ToDouble(factorywhinvDatas[0]["fbaseqty"].ToString());                        }                        double noTranslateQty = Convert.ToDouble(item["QTY"].ToString());                        if (!eqflag)                        {//有库存                            //if (rawqtys > 0)                            //{                            //    if (rawqtys < noTranslateQty)                            //    {                            //        item["QTY"] = rawqtys;                            //        item["BaseQty"] = rawqtys;                            //    }                            //    else                            //    {                            //        item["QTY"] = noTranslateQty;                            //        item["BaseQty"] = noTranslateQty;                            //    }                               ;                            //}                            //else                            //{                            //    item["QTY"] = 0;                            //    item["BaseQty"] = 0;                            //}                                                         //item["FRFStockQty"] = rawqtys;                            /**考虑下面的物料有重复的逻辑处理 2020-8-19**/                            invsqty = QueryMaterialStock(orgId, materialid, Convert.ToInt64(rawwh["FSTOCKID"]));                            List<pickMaterial> find = pickList.Where(p => p.materialId.Equals(materialid)).ToList();                            reqty = Convert.ToDouble(item["QTY"].ToString());                            if (find.Count() <= 0)                            {                                if (invsqty > 0)                                {                                    usedqty = invsqty < reqty ? invsqty : reqty;                                    actualqty = invsqty < reqty ? invsqty : reqty;                                    leaveqty = invsqty - usedqty;                                    pickList.Add(new pickMaterial { materialId = materialid, usedQty = usedqty, leavingQty = leaveqty, invsQty = invsqty });                                    //Console.WriteLine("申请数量:" + reqty + " 实发数量:" + actualqty + "已占用数量:" + usedqty + " 剩余库存数量:" + leaveqty + " 即时库存数量:" + invsqty + "");                                    //this.Model.SetValue("FActualQty", actualqty, i);                                    //this.Model.SetValue("FRFStockQty", invsqty, i);                                    item["QTY"] = actualqty;                                    item["BaseQty"] = actualqty;                                    item["FRFStockQty"] = invsqty;
                                }                                else                                {                                    item["QTY"] = 0;                                    item["BaseQty"] = 0;                                    item["FRFStockQty"] = 0;                                }
                            }                            else                            {                                if (find.Count() > 0)                                {                                    if (find[0].leavingQty > 0)                                    {                                        usedqty = (find[0].leavingQty < reqty ? find[0].leavingQty : reqty) + find[0].usedQty;                                        actualqty = find[0].leavingQty < reqty ? find[0].leavingQty : reqty;                                        leaveqty = invsqty - usedqty;                                        int index = pickList.FindIndex(p => p.materialId.Equals(materialid));                                        find[0].invsQty = invsqty;                                        find[0].usedQty = usedqty;                                        find[0].leavingQty = leaveqty;                                        //Console.WriteLine("申请数量:" + reqty + " 实发数量:" + actualqty + "已占用数量:" + usedqty + " 剩余库存数量:" + leaveqty + " 即时库存数量:" + invsqty + "");                                        //this.Model.SetValue("FActualQty", actualqty, i);                                        //this.Model.SetValue("FRFStockQty", invsqty, i);                                        item["QTY"] = actualqty;                                        item["BaseQty"] = actualqty;                                        item["FRFStockQty"] = invsqty;                                    }                                    else                                    {                                        //this.Model.SetValue("FActualQty", 0, i);                                        //this.Model.SetValue("FRFStockQty", invsqty, i);                                        item["QTY"] = 0;                                        item["BaseQty"] = 0;                                    }

                                }                            }                        }                        else                        {//没有库存                            item["QTY"] = noTranslateQty;                            item["BaseQty"] = noTranslateQty;                            item["FRFStockQty"] = 0;                        }                        /***设置表体调出仓库***/                        stockFld = e.TargetBusinessInfo.GetField("FSRCSTOCKID") as BaseDataField;                        stockId = Convert.ToInt64(rawwh["FSTOCKID"]);                        viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();                        stockObjs = viewService.LoadFromCache(this.Context,                            new object[] { stockId },                            stockFld.RefFormDynamicObjectType);                        stockFld.RefIDDynamicProperty.SetValue(item, stockId);                        stockFld.DynamicProperty.SetValue(item, stockObjs[0]);                        /***设置表体调入仓库***/                        stockFld = e.TargetBusinessInfo.GetField("FDestStockId") as BaseDataField;                        stockId = Convert.ToInt64(factwh["FSTOCKID"]);                        viewService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>();                        stockObjs = viewService.LoadFromCache(this.Context,                            new object[] { stockId },                            stockFld.RefFormDynamicObjectType);                        stockFld.RefIDDynamicProperty.SetValue(item, stockId);                        stockFld.DynamicProperty.SetValue(item, stockObjs[0]);                    }                }            }        }

05-11 22:23