二开案例.列表插件.自建视图保存单据
【应用场景】
在插件或服务内部,无界面状态下创建或修改单据。
【案例演示】
采购订单,通过列表插件,自建视图保存单据。
【实现步骤】
<1>编写列表插件,代码如下。
using Kingdee.BOS;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Util;
using Kingdee.BOS.Web.Bill;
using System.ComponentModel;
using System.Linq;
namespace Jac.XkDemo.BOS.Business.PlugIn
{
/// <summary>
/// 【列表插件】自建视图保存单据
/// </summary>
[Description("【列表插件】自建视图保存单据"), HotUpdate]
public class CreateViewAndSaveBillListPlugIn : AbstractListPlugIn
{
private object pkId = null;
public override void BarItemClick(BarItemClickEventArgs e)
{
base.BarItemClick(e);
if (e.BarItemKey == "AddNewBill")
{
// 创建采购订单的单据视图(新增状态)
var billView = CreateBillView(this.Context, "PUR_PurchaseOrder");
// 设置供应商
billView.SetItemValueByNumber("FSupplierId", "GYS001", 0);
// 设置订单明细
for (var i = 0; i < 3; ++i)
{
billView.Model.CreateNewEntryRow("FPOOrderEntry");
billView.SetItemValueByNumber("FMaterialId", "0.0000.01", i);
billView.Model.SetValue("FQty", i + 1, i);
billView.Model.SetValue("FPriceUnitQty", i + 1, i);
billView.Model.SetValue("FStockQty", i + 1, i);
billView.Model.SetValue("FTaxPrice", 66, i);
}
var result = billView.Model.Save();
if (result.IsSuccess)
{
var billNo = billView.Model.GetValue("FBillNo");
pkId = billView.Model.GetPKValue();
this.View.ShowMessage(string.Format("单据编号为{0}的采购订单新增保存成功!", billNo));
}
else
{
this.View.ShowErrMessage("保存失败:" + string.Join(",", result.ValidationErrors.Select(o => o.Message)));
}
}
else if (e.BarItemKey == "SaveBill")
{
if (pkId == null)
{
this.View.ShowMessage("请先新增单据!");
return;
}
// 创建采购订单的单据视图(修改状态)
var billView = CreateBillView(this.Context, "PUR_PurchaseOrder", null, pkId);
// 修改供应商
billView.SetItemValueByNumber("FSupplierId", "VEN00002", 0);
// 新增订单明细
var rowCount = billView.Model.GetEntryRowCount("FPOOrderEntry");
for (var i = rowCount - 1; i < rowCount + 3; ++i)
{
billView.Model.CreateNewEntryRow("FPOOrderEntry");
billView.SetItemValueByNumber("FMaterialId", "0.0000.01", i);
billView.Model.SetValue("FQty", i * 6, i);
billView.Model.SetValue("FPriceUnitQty", i * 6, i);
billView.Model.SetValue("FStockQty", i * 6, i);
billView.Model.SetValue("FTaxPrice", 88, i);
}
var result = billView.Model.Save();
if (result.IsSuccess)
{
var billNo = billView.Model.GetValue("FBillNo");
this.View.ShowMessage(string.Format("单据编号为{0}的采购订单修改保存成功!", billNo));
}
else
{
this.View.ShowErrMessage("保存失败:" + string.Join(",", result.ValidationErrors.Select(o => o.Message)));
}
}
}
/// <summary>
/// 创建单据视图
/// </summary>
/// <param name="ctx"></param>
/// <param name="formId"></param>
/// <param name="layoutId"></param>
/// <param name="pkId"></param>
/// <returns></returns>
private static BillView CreateBillView(Context ctx, string formId, string layoutId = null, object pkId = null)
{
var meta = (FormMetadata) Kingdee.BOS.ServiceHelper.MetaDataServiceHelper.Load(ctx, formId); //单据唯一标识
var form = meta.BusinessInfo.GetForm();
var param = new BillOpenParameter(formId, layoutId);
param.Context = ctx;
param.FormMetaData = meta;
if (pkId != null && !string.IsNullOrWhiteSpace(pkId.ToString()))
{
param.Status = OperationStatus.EDIT;
param.InitStatus = OperationStatus.EDIT;
param.PkValue = pkId; //单据主键内码FID
}
else
{
param.Status = OperationStatus.ADDNEW;
param.InitStatus = OperationStatus.ADDNEW;
}
param.SetCustomParameter("formID", form.Id);
param.SetCustomParameter("PlugIns", form.CreateFormPlugIns()); //插件实例模型
param.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
param.NetCtrlDisable = true; // 禁用网控
var provider = form.GetFormServiceProvider();
var billview = (BillView) provider.GetService(typeof(IDynamicFormView));
//var type = Type.GetType("Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web");
//var billview2 = (BillView)Activator.CreateInstance(type);
billview.Initialize(param, provider); //初始化
billview.LoadData(); //加载单据数据
return billview;
}
}
}
<2>拷贝插件组件到应用站点的WebSite\Bin目录下,重启IIS。
<3>BOSIDE扩展采购订单,列表菜单新增菜单项,注册列表插件,保存元数据,开发完毕。
【功能验证】
<1>登录业务站点,打开采购订单列表,点击菜单【AddNewBill】和【SaveBill】,即可无界面执行新增单据和保存单据。
新增单据:
保存单据:
---------------------------------------------------------------------------------------------------------
【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696
二开案例.列表插件.自建视图保存单据
本文2024-09-23 04:08:27发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-163397.html