Skip to content

单据体分录

本节介绍如何给微信表单添加一个单据体分录组件

表单元数据设计


业务插件读取单据体数据及编辑单据体

CSharp
using Kejie.BOS;
using Kejie.BOS.Core.DynamicForm.Plugin.Args;
using Kejie.BOS.Core.DynamicService;
using Kejie.BOS.Core.WxBillShell;
using Kejie.BOS.Core.WxBillShell.Plugin;
using Kejie.BOS.DataEntity;
using System.Text.Json.Nodes;

namespace Kejie.Template.MultiDev.Plugin
{
    public class WxShoppingCart : WxBillShellPlugin
    {
        public WxShoppingCart(IWxBillShellView view) : base(view)
        {
        }

        public override void BeforeLoadData(BeforeLoadDataEventArgs e)
        {
            var ent = View.ServiceProvider.GetService<IFormDataService>()
                .Load(View.DbContext, "Mt_ShoppingCart", [View.SessionManager.GetUuid()!], "FId").FirstOrDefault();
            if (ent != null)
            {
                ShellView.ShellShowParameters.PkValue = ent["Id"]!.ToString();
                ShellView.ShellShowParameters.Status = BOS.Core.Bill.BillStatus.EDIT;
            }
        }

        public override async Task BeforeBindViewDataAsync(BeforeBindViewDataEventArgs e)
        {
            await SetTotalAmount();
            await base.BeforeBindViewDataAsync(e);
        }

        public override async Task DataChanged(DataChangedEventArgs e)
        {
            if (e.Field.Key == "FQty")
            {
                await SetTotalAmount();
                await ShellView.SourceBillView.InvokeService("Save", null);
                return;
            }

            await base.DataChanged(e);
        }

        public override async Task BeforeInvokeEvent(BeforeInvokeEventArgs e)
        {
            if (e.Key == "FTabbar" && e.EventName == "clickItem")
            {
                View.RedirectTo(e.EventArgs!.GetValue<string>());
                return;
            }

            if (e.Key == "FSelectAll" && e.EventName == "change")
            {
                //点击全选时,遍历所有单据体明细,将复选框全部勾选或反选
                await View.GetValue<DynamicObjectCollection>("FShoppingCartDetails")!.ForEachAsync(async (detail, index) =>
                {
                    await View.SetValue("FSelect", e.EventArgs!.GetValue<bool>(), index);
                });

                return;
            }

            if (e.Key == "FBtnDelete" && e.EventName == "click")
            {
                var details = View.GetValue<DynamicObjectCollection>("FShoppingCartDetails")!;
                var selectedCartDetails = details.Where(detail => detail["FSelect"].ToBoolean()).ToList();
                if (selectedCartDetails.Count == 0)
                {
                    View.ShowWarning("请选择要删除的商品");
                    return;
                }

                await selectedCartDetails.ForEachAsync(async detail =>
                {
                    var index = details.IndexOf(detail);
                    //删除单据体明细
                    await View.DeleteEntryRow("FShoppingCartDetails", index);
                });

                await ShellView.SourceBillView.InvokeService("Save", null);
                await SetTotalAmount();
                return;
            }

            if (e.Key == "FBtnSubmit" && e.EventName == "click")
            {
                if (View.SessionManager.GetCustomerContext() == null)
                {
                    View.NavigateTo("Mt_WxLogin");
                    return;
                }

                var selectedCartDetails = View.GetValue<DynamicObjectCollection>("FShoppingCartDetails")!.Where(detail => detail["FSelect"].ToBoolean()).ToList();
                if (selectedCartDetails.Count == 0)
                {
                    View.ShowWarning("请选择要结算的商品");
                    return;
                }

                var details = new JsonArray();
                selectedCartDetails.ForEach(detail =>
                {
                    details.Add(new JsonObject
                    {
                        { "Item", detail!["FItem_Id"]!.ToString() },
                        { "Qty", detail["FQty"].ToDecimal() }
                    });
                });


                var customParams = new JsonObject
                {
                    { "details", details }
                };

                View.NavigateTo("Mt_WxCustomerOrderCreate4Cart", true, null, customParams);
                return;
            }

            await base.BeforeInvokeEvent(e);
        }

        private async Task SetTotalAmount()
        {
            //计算总计金额
            var totalAmount = View.GetValue<DynamicObjectCollection>("FShoppingCartDetails")!.Sum(detail =>
            {
                var item = detail["FItem"] as DynamicObject;
                return item!["FPrice"].ToDecimal() * detail["FQty"].ToDecimal();
            });

            await View.SetValue("FTotalAmount", totalAmount);
        }
    }
}

效果图