391 lines
17 KiB
C#
391 lines
17 KiB
C#
using Gatedge.K3.Pilot.PlugIn.Common;
|
||
using Gatedge.K3.Pilot.PlugIn.Models;
|
||
using Gatedge.K3.Pilot.PlugIn.Services.DBService;
|
||
using Kingdee.BOS;
|
||
using Kingdee.BOS.App;
|
||
using Kingdee.BOS.App.Core;
|
||
using Kingdee.BOS.Contracts;
|
||
using Kingdee.BOS.Core;
|
||
using Kingdee.BOS.Core.Bill;
|
||
using Kingdee.BOS.Core.Bill.PlugIn;
|
||
using Kingdee.BOS.Core.Const;
|
||
using Kingdee.BOS.Core.DynamicForm;
|
||
using Kingdee.BOS.Core.DynamicForm.Operation;
|
||
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
|
||
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
|
||
using Kingdee.BOS.Core.List;
|
||
using Kingdee.BOS.Core.Metadata;
|
||
using Kingdee.BOS.Core.Metadata.ConvertElement;
|
||
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
|
||
using Kingdee.BOS.Orm;
|
||
using Kingdee.BOS.Orm.DataEntity;
|
||
using Kingdee.BOS.Util;
|
||
using Kingdee.K3.BD.NewCode.Core.Utils;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Gatedge.K3.Pilot.PlugIn.BOSPlugIn.Sal_OutStock
|
||
{
|
||
|
||
[Description("销售出库单插件"), HotUpdate]
|
||
public class Bill : AbstractBillPlugIn
|
||
{
|
||
|
||
public override void AfterDoOperation(AfterDoOperationEventArgs e)
|
||
{
|
||
base.AfterDoOperation(e);
|
||
if (e.Operation.Operation.EqualsIgnoreCase("DoPushAOrgBill"))
|
||
{
|
||
var dataObj = this.View.Model.DataObject;
|
||
var entry = dataObj["SAL_OUTSTOCKENTRY"] as DynamicObjectCollection;
|
||
var dal = new SalOutStockDAL(this.Context);
|
||
foreach (var item in entry)
|
||
{
|
||
var entryId = item["Id"].Long2Int();
|
||
if (entryId != 0)
|
||
{
|
||
var datas = dal.GetSalOutStockSrcPO(entryId);
|
||
List<ListSelectedRow> selectedRows = new List<ListSelectedRow>();
|
||
var entity_Links = new List<Entity_Link>();
|
||
if (datas != null)
|
||
{
|
||
foreach (var data in datas)
|
||
{
|
||
var outStockQty = item["合计出库数量"].Convert<decimal>();
|
||
var poInStockQty = item["采购入库数量"].Convert<decimal>();
|
||
var poDeviQty = item["收料数量"].Convert<decimal>();
|
||
var billTypeId = item["采购订单类型"].Convert<string>();
|
||
|
||
if (outStockQty <= poInStockQty)
|
||
continue;
|
||
|
||
entity_Links.Add(new Entity_Link
|
||
{
|
||
EntryId = item["采购订单分录内码"].Long2Int(),
|
||
BaseUnitQty = outStockQty - poInStockQty,
|
||
});
|
||
|
||
selectedRows.Add(new ListSelectedRow("0", item["采购订单分录内码"]?.ToString(), 0, ""));
|
||
|
||
var reSaveResult = PushPO2Re(billTypeId, selectedRows, entity_Links);
|
||
|
||
if (reSaveResult.IsSuccess)
|
||
{
|
||
var selectRows = reSaveResult.SuccessDataEnity.SelectMany(x => x["PUR_ReceiveEntry"] as DynamicObjectCollection).Select(x => new ListSelectedRow("0", x["Id"].Long2Int().ToString(), 0, "")).ToList();
|
||
if (selectRows.Count > 0)
|
||
{
|
||
var reObj = reSaveResult.SuccessDataEnity.FirstOrDefault();
|
||
if (reObj != null)
|
||
{
|
||
var reBillTypeId = reObj["BillTypeId"].ToString();
|
||
var isResult = PushRe2InStock(selectRows, reBillTypeId);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 采购订单下推收料单
|
||
/// </summary>
|
||
/// <param name="billTypeId"></param>
|
||
/// <param name="selectedRows"></param>
|
||
/// <param name="entity_Links"></param>
|
||
/// <returns></returns>
|
||
private IOperationResult PushPO2Re(string billTypeId, List<ListSelectedRow> selectedRows, List<Entity_Link> entity_Links)
|
||
{
|
||
IOperationResult result = null;
|
||
|
||
try
|
||
{
|
||
string sourceFormId = FormIdConstants.PUR_PurchaseOrder;
|
||
string targetFormId = FormIdConstants.PUR_ReceiveBill;
|
||
string convertRuleId = "PUR_PurchaseOrder-PUR_ReceiveBill";
|
||
//PUR_ReceiveBill-STK_InStock
|
||
result = DoPustBill(sourceFormId, targetFormId, convertRuleId, selectedRows, billTypeId, entity_Links, "FQty");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
result = new OperationResult();
|
||
result.IsSuccess = false;
|
||
var opResFirst = new OperateResult();
|
||
opResFirst.Name = ex.Message;
|
||
opResFirst.Message = ex.Message + "\r\n" + ex.StackTrace;
|
||
opResFirst.SuccessStatus = false;
|
||
result.OperateResult.Add(opResFirst);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
private IOperationResult PushRe2InStock(List<ListSelectedRow> selectedRows, string reBillTypeId)
|
||
{
|
||
IOperationResult result = null;
|
||
|
||
try
|
||
{
|
||
string sourceFormId = FormIdConstants.PUR_ReceiveBill;
|
||
string targetFormId = FormIdConstants.STK_InStock;
|
||
string convertRuleId = "PUR_ReceiveBill-STK_InStock";
|
||
//PUR_ReceiveBill-STK_InStock
|
||
result = DoPustBill(sourceFormId, targetFormId, convertRuleId, selectedRows, reBillTypeId);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
result = new OperationResult();
|
||
result.IsSuccess = false;
|
||
var opResFirst = new OperateResult();
|
||
opResFirst.Name = ex.Message;
|
||
opResFirst.Message = ex.Message + "\r\n" + ex.StackTrace;
|
||
opResFirst.SuccessStatus = false;
|
||
result.OperateResult.Add(opResFirst);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
private void SetQtyAction(DynamicObject[] destObjs, List<Entity_Link> entity_Links, string sourceFormId, string sourceBillTypeId, string qtyName)
|
||
{
|
||
//获取元数据服务
|
||
IMetaDataService metadataService = ServiceHelper.GetService<IMetaDataService>();
|
||
FormMetadata metada = metadataService.Load(this.Context, sourceFormId) as FormMetadata;
|
||
|
||
var view = CreateView(metada, sourceBillTypeId);
|
||
foreach (var obj in destObjs)
|
||
{
|
||
view.Model.DataObject = obj;
|
||
var entrys = obj["PUR_ReceiveEntry"] as DynamicObjectCollection;
|
||
foreach (var item in entrys)
|
||
{
|
||
var seq = item["Seq"].Long2Int() - 1;
|
||
DynamicObjectCollection entityDetail_Link = item["FEntityDetail_Link"] == null ? null : item["FEntityDetail_Link"] as DynamicObjectCollection;
|
||
if (entityDetail_Link != null && entityDetail_Link.Count > 0)
|
||
{
|
||
var qty = 0M;
|
||
foreach (var link in entityDetail_Link)
|
||
{
|
||
var entity = entity_Links.FirstOrDefault(w => w.EntryId == link["Sid"].Long2Int());
|
||
qty += entity != null ? entity.BaseUnitQty : 0M;
|
||
}
|
||
|
||
view.Model.SetValue(qtyName, qty, seq);
|
||
view.InvokeFieldUpdateService(qtyName, seq);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 销售订单下推发货通知单
|
||
/// </summary>
|
||
/// <param name="selectedRows"></param>
|
||
/// <returns></returns>
|
||
private IOperationResult PushSO2De(List<ListSelectedRow> selectedRows)
|
||
{
|
||
IOperationResult result = null;
|
||
|
||
try
|
||
{
|
||
string sourceFormId = FormIdConstants.SAL_SaleOrder;
|
||
string targetFormId = FormIdConstants.SAL_DELIVERYNOTICE;
|
||
string convertRuleId = "SaleOrder-DeliveryNotice";
|
||
//PUR_ReceiveBill-STK_InStock
|
||
//result = DoPustBill(selectedRows, "", sourceFormId, targetFormId, convertRuleId);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
result = new OperationResult();
|
||
result.IsSuccess = false;
|
||
var opResFirst = new OperateResult();
|
||
opResFirst.Name = ex.Message;
|
||
opResFirst.Message = ex.Message + "\r\n" + ex.StackTrace;
|
||
opResFirst.SuccessStatus = false;
|
||
result.OperateResult.Add(opResFirst);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发货通知单下推销售出库单
|
||
/// </summary>
|
||
/// <param name="selectedRows"></param>
|
||
/// <returns></returns>
|
||
private IOperationResult PushDe2OS(List<ListSelectedRow> selectedRows)
|
||
{
|
||
IOperationResult result = null;
|
||
|
||
try
|
||
{
|
||
string sourceFormId = FormIdConstants.SAL_DELIVERYNOTICE;
|
||
string targetFormId = FormIdConstants.SAL_OUTSTOCK;
|
||
string convertRuleId = "DeliveryNotice-OutStock";
|
||
//PUR_ReceiveBill-STK_InStock
|
||
//result = DoPustBill(selectedRows, "", sourceFormId, targetFormId, convertRuleId);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
result = new OperationResult();
|
||
result.IsSuccess = false;
|
||
var opResFirst = new OperateResult();
|
||
opResFirst.Name = ex.Message;
|
||
opResFirst.Message = ex.Message + "\r\n" + ex.StackTrace;
|
||
opResFirst.SuccessStatus = false;
|
||
result.OperateResult.Add(opResFirst);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下推单据
|
||
/// </summary>
|
||
/// <param name="selectedRows"></param>
|
||
/// <param name="sourceFormId"></param>
|
||
/// <param name="targetFormId"></param>
|
||
/// <param name="convertRuleId"></param>
|
||
/// <returns></returns>
|
||
private IOperationResult DoPustBill(string sourceFormId, string targetFormId, string convertRuleId, List<ListSelectedRow> selectedRows, string billTypeId, List<Entity_Link> entity_Links = null, string qtyName = "")
|
||
{
|
||
IOperationResult result = null;
|
||
|
||
try
|
||
{
|
||
IConvertService service = ServiceHelper.GetService<IConvertService>();
|
||
|
||
//获取元数据服务
|
||
IMetaDataService metadataService = ServiceHelper.GetService<IMetaDataService>();
|
||
|
||
//获取ViewService
|
||
IViewService viewService = ServiceHelper.GetService<IViewService>();
|
||
//获取源单元数据
|
||
|
||
//获取转换规则
|
||
var ruleMeta = service.GetConvertRule(this.Context, convertRuleId);
|
||
|
||
if (ruleMeta != null)
|
||
{
|
||
FormMetadata sourceMetada = metadataService.Load(this.Context, sourceFormId) as FormMetadata;
|
||
|
||
var policies = ruleMeta.Rule.Policies;
|
||
//字段映射关系
|
||
var policie7002 = policies.FirstOrDefault(x => x.ElementType == 7002) as DefaultConvertPolicyElement;
|
||
if (policie7002 == null)
|
||
throw new Exception("转换单据中,字段映射关系类型值:7002不存在。");
|
||
|
||
//单据类型映射
|
||
var policie7009 = policies.FirstOrDefault(x => x.ElementType == 7009) as BillTypeMapPolicyElement;
|
||
if (policie7009 == null)
|
||
throw new Exception("转换单据中,单据类型属性类型值:7009不存在。");
|
||
var billTypeMap = policie7009.BillTypeMaps.FirstOrDefault(w => w.SourceBillTypeId == billTypeId);
|
||
|
||
if (billTypeMap == null)
|
||
throw new Exception($"转换规则中,源单单据类型不存在!");
|
||
|
||
var sourceBillTypeId = billTypeMap.SourceBillTypeId;
|
||
var targetBillTypeId = billTypeMap.TargetBillTypeId;
|
||
//设置来源单据类型
|
||
selectedRows.ForEach(x =>
|
||
{
|
||
x.EntryEntityKey = policie7002.SourceEntryKey;
|
||
});
|
||
|
||
var pushArgs = new PushArgs(ruleMeta.Rule, selectedRows.ToArray());
|
||
pushArgs.TargetBillTypeId = targetBillTypeId;
|
||
OperateOption option = OperateOption.Create();//选项参数
|
||
|
||
option.SetVariableValue(ConvertConst.SelectByBillId, false);//不按照整单下推
|
||
|
||
//源单数据转换目标数据
|
||
ConvertOperationResult convertResult = service.Push(this.Context, pushArgs, option);
|
||
|
||
DynamicObject[] destObjs = convertResult.TargetDataEntities.Select(r => r.DataEntity).ToArray();
|
||
|
||
if (entity_Links != null && !qtyName.IsNullOrEmptyOrWhiteSpace())
|
||
SetQtyAction(destObjs, entity_Links, targetFormId, targetBillTypeId, qtyName);
|
||
|
||
FormMetadata destFormMetadata = metadataService.Load(this.Context, targetFormId) as FormMetadata;
|
||
|
||
IOperationResult saveResult = ServiceHelper.GetService<ISaveService>().SaveAndAudit(this.Context, destFormMetadata.BusinessInfo, destObjs, OperateOption.Create());
|
||
|
||
result = saveResult;
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
result = new OperationResult();
|
||
result.IsSuccess = false;
|
||
var opResFirst = new OperateResult();
|
||
opResFirst.Name = ex.Message;
|
||
opResFirst.Message = ex.Message + "\r\n" + ex.StackTrace;
|
||
opResFirst.SuccessStatus = false;
|
||
result.OperateResult.Add(opResFirst);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
private IBillView CreateView(FormMetadata metadata, string billTypeId = null)
|
||
{
|
||
|
||
var OpenParameter = CreateOpenParameter(this.Context, metadata, billTypeId);
|
||
var Provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider(true);
|
||
var importViewClass = "Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web";
|
||
var importViewClassType = Type.GetType(importViewClass);
|
||
var billView = (IDynamicFormViewService)Activator.CreateInstance(importViewClassType);
|
||
|
||
billView.Initialize(OpenParameter, Provider);
|
||
billView.LoadData();
|
||
return (IBillView)billView;
|
||
}
|
||
|
||
private BillOpenParameter CreateOpenParameter(Context ctx, FormMetadata metaData, string billTypeId)
|
||
{
|
||
var form = metaData.BusinessInfo.GetForm();
|
||
var openPara = new BillOpenParameter(form.Id, metaData.GetLayoutInfo().Id);
|
||
openPara = new BillOpenParameter(form.Id, "");
|
||
openPara.Context = ctx;
|
||
openPara.ServiceName = form.FormServiceName;
|
||
openPara.PageId = Guid.NewGuid().ToString();
|
||
//# 单据
|
||
openPara.FormMetaData = metaData;
|
||
openPara.LayoutId = metaData.GetLayoutInfo().Id;
|
||
//# 操作相关参数
|
||
openPara.Status = OperationStatus.ADDNEW;
|
||
// 单据主键:本案例演示新建商品类型,不需要设置主键
|
||
openPara.PkValue = null;
|
||
// 界面创建目的:普通无特殊目的 (为工作流、为下推、为复制等)
|
||
openPara.CreateFrom = CreateFrom.Default;
|
||
// 基础资料分组:如果需要为新建的基础资料指定所在分组,请设置此属性
|
||
openPara.ParentId = 0;
|
||
// 基础资料分组维度:基础资料允许添加多个分组字段,每个分组字段会有一个分组维度
|
||
// 具体分组维度Id,请参阅 form.FormGroups 属性
|
||
openPara.GroupId = "";
|
||
// 单据类型
|
||
openPara.DefaultBillTypeId = billTypeId;
|
||
// 业务流程
|
||
openPara.DefaultBusinessFlowId = null;
|
||
//# 修改主业务组织无须用户确认
|
||
openPara.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
|
||
//# 插件
|
||
var plugins = form.CreateFormPlugIns();
|
||
openPara.SetCustomParameter(FormConst.PlugIns, plugins);
|
||
return openPara;
|
||
}
|
||
}
|
||
}
|