业务场景:以采购订单为例,多系统对接时,会出于信息保密等因素,采购订单推给第三方系统时不会携带单价,含税单价。当第三方系统调用webapi,回传收料通知单,采购入库单回云星空系统时,单据会缺少单价、金额、价税合计等财务信息。
解决方案:收料通知单提交操作里面获取采购订单上的单价、含税单价,并使用单据视图触发值更新服务,最后再保存到系统中。
示例代码:
public class ERP
{
/// <summary>
/// 创建单据视图
/// </summary>
/// <param name="ctx">上下文</param>
/// <param name="formId">单据类型编码</param>
/// <param name="pkId">单据内码</param>
/// <returns></returns>
public static BillView CreateBillView(Context ctx, string formId, string pkId)
{
FormMetadata metadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
Form form = metadata.BusinessInfo.GetForm();
BillOpenParameter billOpenParameter = new BillOpenParameter(form.Id, string.Empty);
billOpenParameter.Context = ctx;
billOpenParameter.ServiceName = form.FormServiceName;
billOpenParameter.PageId = Guid.NewGuid().ToString();
billOpenParameter.FormMetaData = metadata;
billOpenParameter.LayoutId = metadata.GetLayoutInfo().Id;
billOpenParameter.Status = OperationStatus.EDIT;
billOpenParameter.CreateFrom = CreateFrom.Default;
billOpenParameter.ParentId = 0;
billOpenParameter.GroupId = "";
billOpenParameter.DefaultBillTypeId = null;
billOpenParameter.DefaultBusinessFlowId = null;
billOpenParameter.SetCustomParameter("PlugIns", form.CreateFormPlugIns()); //插件实例模型
billOpenParameter.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
billOpenParameter.NetCtrlDisable = true; // 禁用网控
IResourceServiceProvider formServiceProvider = metadata.BusinessInfo.GetForm().GetFormServiceProvider(isWebService: true);
ImportBillView importBillView = new ImportBillView();
importBillView.Initialize(billOpenParameter, formServiceProvider);
importBillView.Model.Load(pkId);
return importBillView;
}
}
public class ERP_PUR_ReceiveBill_SubmitPlugIn : AbstractOperationServicePlugIn
{
public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
{
base.AfterExecuteOperationTransaction(e);
#region 获取完整数据包
List<string> pkIds = new List<string>();
for (int i = 0; i < e.DataEntitys.Count(); i++)
{
string id = ObjectUtils.Object2String(e.DataEntitys[i]["Id"]);
pkIds.Add(id);
}
DynamicObject[] formDatas = BusinessDataServiceHelper.Load(this.Context, pkIds.ToArray(), this.BusinessInfo.GetDynamicObjectType());
#endregion 获取完整数据包
for (int i = 0; i < formDatas.Count(); i++)
{
DynamicObject formData = formDatas[i] as DynamicObject;
string id = ObjectUtils.Object2String(formData["Id"]);
DynamicObjectCollection DetailEntity = formData["PUR_ReceiveEntry"] as DynamicObjectCollection;
if (DetailEntity.Count == 0)
{
continue;
}
#region 获取源单信息
string formId = ObjectUtils.Object2String(DetailEntity[0]["SrcFormId"]);
//源单不是采购订单跳过
if (string.Compare(formId, "PUR_PurchaseOrder", StringComparison.OrdinalIgnoreCase) != 0)
{
continue;
}
string scrBillNo = ObjectUtils.Object2String(DetailEntity[0]["SrcBillNo"]);
//获取源单据元数据
FormMetadata metada = MetaDataServiceHelper.Load(this.Context, formId) as FormMetadata;
//构建快捷过滤条件
OQLFilter filter = new OQLFilter();
filter.Add(new OQLFilterHeadEntityItem() { FilterString = "FBillNo = '" + scrBillNo + "'" });
//构建关心的字段片段信息
List<SelectorItemInfo> lstSelectorItemInfos = new List<SelectorItemInfo>();
lstSelectorItemInfos.Add(new SelectorItemInfo("FEntryID"));//分录主键
lstSelectorItemInfos.Add(new SelectorItemInfo("FPrice"));//单价
lstSelectorItemInfos.Add(new SelectorItemInfo("FTaxPrice"));//含税单价
lstSelectorItemInfos.Add(new SelectorItemInfo("FSettleCurrId"));//结算币别
lstSelectorItemInfos.Add(new SelectorItemInfo("FEntryTaxRate"));//税率
lstSelectorItemInfos.Add(new SelectorItemInfo("FExchangeTypeId"));//汇率类型
lstSelectorItemInfos.Add(new SelectorItemInfo("FExchangeRate"));//汇率
DynamicObject[] srcForms = BusinessDataServiceHelper.Load(this.Context, metada.BusinessInfo, lstSelectorItemInfos, filter);
if (srcForms == null || srcForms.Count() == 0)
{
continue;
}
#endregion
var srcForm = srcForms[0];
#region 财务信息
DynamicObjectCollection finEntry = srcForm["POOrderFinance"] as DynamicObjectCollection;
if (finEntry.Count() == 0)
{
continue;
}
string settleCurrId = ObjectUtils.Object2String(finEntry[0]["SETTLECURRID_ID"]);//结算币别
string exchangeTypeId = ObjectUtils.Object2String(finEntry[0]["ExchangeTypeId_Id"]);//汇率类型
decimal exchangeRate = ObjectUtils.ToDecimal(finEntry[0]["ExchangeRate"]);//汇率
#endregion
//构建收获通知单的单据视图用于触发值更新事件
BillView billView = ERP.CreateBillView(this.Context, "PUR_ReceiveBill", id);
foreach (var detail in DetailEntity)
{
int row = ObjectUtils.Object2Int(detail["SEQ"]) - 1;
string entryId = ObjectUtils.Object2String(detail["Id"]);
string srcEntryId = ObjectUtils.Object2String(detail["SrcEntryId"]);
bool isFree = ObjectUtils.Object2Bool(detail["IsFree"]);
if (isFree)
{
continue;
}
//结算币别、汇率类型、汇率赋值
billView.Model.SetValue("FSettleCurrId", settleCurrId, row);
billView.Model.SetValue("FExchangeTypeId", exchangeTypeId, row);
billView.Model.SetValue("FExchangeRate", exchangeRate, row);
//单价、含税单价、税率赋值
DynamicObjectCollection receiveEntry = srcForm["POOrderEntry"] as DynamicObjectCollection;
foreach (var receive in receiveEntry)
{
string receiveId = ObjectUtils.Object2String(receive["Id"]);
if (receiveId == srcEntryId)
{
decimal price = ObjectUtils.ToDecimal(receive["Price"]);
decimal taxPrice = ObjectUtils.ToDecimal(receive["TaxPrice"]);
decimal taxRate = ObjectUtils.ToDecimal(receive["TaxRate"]);
billView.Model.SetValue("FTaxPrice", taxPrice, row);
billView.Model.SetValue("FPrice", price, row);
billView.Model.SetValue("FTaxRate", taxRate, row);
break;
}
}
//触发值更新事件
billView.InvokeFieldUpdateService("FSettleCurrId", row);
billView.InvokeFieldUpdateService("FTaxPrice", row);
billView.InvokeFieldUpdateService("FTaxRate", row);
billView.InvokeFieldUpdateService("FPrice", row);
}
FormMetadata metadaThisForm = MetaDataServiceHelper.Load(this.Context, "PUR_ReceiveBill") as FormMetadata;
var result = BusinessDataServiceHelper.Save(this.Context, metadaThisForm.BusinessInfo, billView.Model.DataObject);
}
}
}
推荐阅读