金蝶云星空解锁时同时解锁序列号

 

业务背景

公司业务要求,如果检查发现序列号有问题,先锁库不允许出库。

如果已经锁库,此时序列号允许出库,则可以解锁。

 前置任务:金蝶云星空锁库时同时锁定序列号 - lanrenka - 博客园 (cnblogs.com)

系统现状

即时库存锁库,锁定的是数量,库存-锁库数=可用数,当可用量小于等于0就不可以再出库了。

如果想要控制锁到序列号,系统就不支持了。

 

方案设计

锁库时同时锁定库存和根据输入的序列号锁定序列号,解锁的时候,同时解锁库存和根据输入的序列号进行解锁。

 

详细设计

解锁操作-扩展
添加页签控件,将原来的解锁信息放到第一个页签,第二个页签添加一个单据体,单据体增加一个基础资料,绑定序列号主档。

 

 

菜单项,添加按钮,按序列号解锁

 

 

找到表单插件,新建插件,继承它。

 

 

实现

解锁数量不能大于锁库

解锁=序列号

序列号必须在当前即时库存序列号主档标记锁定

释放库存清空锁定标记必须一起成功

#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2024  NJRN 保留所有权利。
 * CLR版本:4.0.30319.42000
 * 机器名称:INC1507245
 * 公司名称:Increase
 * 命名空间:Krystal.K3.SCM.Business.PlugIn.STK.DynamicForm
 * 唯一标识:38bf5845-45d5-4367-ad27-ceac9c162040
 * 文件名:UnLockOperateBySerialExtend
 * 当前用户域:INC1507245
 * 
 * 创建者:Krystal
 * 电子邮箱:543375940@qq.com
 * 创建时间:2024/8/14 15:46:46
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 时间:
 * 修改说明:
 *
 * 版本:V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Resource;
using Kingdee.BOS.Util;
using Kingdee.K3.Core.SCM.STK;
using Kingdee.K3.SCM.ServiceHelper;
using Kingdee.K3.SCM.Stock.Business.PlugIn;
using Krystal.K3Cloud.Core.Const;
using Krystal.K3Cloud.Core.Model.STK;
using Krystal.K3Cloud.Core.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace Krystal.K3.SCM.Business.PlugIn.STK.DynamicForm
{
    /// <summary>
    /// 功能描述    :UnLockOperateBySerialExtend  
    /// 创 建 者    :Administrator
    /// 创建日期    :2024/8/14 15:46:46 
    /// 最后修改者  :Krystal
    /// 最后修改日期:2024/8/14 15:46:46 
    /// </summary>
    [Description("即时库存解锁操作插件--二开继承"), HotUpdate]
    public class UnLockOperateBySerialExtend: UnLockStockOperate
    {
        #region <常量>
        /// <summary>
        /// 操作类型:StockLock-库存锁库,Inv-即时库存,SaleOrder-销售订单 StockLockLog-库存日志解锁
        /// </summary>
        private string opType = "Inv";
        #endregion <常量>

        #region <变量>
        /// <summary>
        /// 标示是否关闭界面
        /// </summary>
        private bool IsClose;
        #endregion <变量>



        #region <方法>
        /// <summary>
        /// 获得动态对象的值,优先返回MasterId
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private long GetDynamicValue(DynamicObject obj)
        {
            if (obj == null)
            {
                return 0L;
            }
            if (obj.DynamicObjectType.Properties.ContainsKey(FormConst.MASTER_ID))
            {
                return Convert.ToInt64(obj[FormConst.MASTER_ID]);
            }
            if (obj.DynamicObjectType.Properties.ContainsKey("Id"))
            {
                return Convert.ToInt64(obj["Id"]);
            }
            return 0L;
        }
        /// <summary>
        /// 校验序列号是否在库,且未锁定
        /// </summary>
        /// <param name="objList"></param>
        /// <param name="invId"></param>
        /// <returns></returns>
        private bool CheckSerial(List<DynamicObject> objList, string invId, out string serialMsg)
        {
            bool isSucceed = true;
            serialMsg = string.Empty;
            //序列号物料编码,在库状态
            List<SqlParam> para = new List<SqlParam>()
            {
                //new SqlParam("@STOCKID", KDDbType.Int32,493513),
                new SqlParam("@InvId", KDDbType.String,invId),
                new SqlParam("@IsLock", KDDbType.Int32,1),
                //new SqlParam("@ENDTIME",KDDbType.String,_filterArgs.EndTime.ToString("yyyy-MM-dd")),
                //new SqlParam("@t",KDDbType.String,temp_detail)
            };
            //执行sql语句并将结果集填充至DataSet  XXXX_PR_STK_SerialInfoByInvId未使用
            DataSet getSerial = DBUtils.ExecuteDataSet(this.Context, System.Data.CommandType.StoredProcedure
                , string.Format(@"{0}XXXX_PR_STK_SerialInfoByInvId", OtherConst.DIALECT), para);



            //调用上面的将DataSet转换为List<Menu>实体集合
            List<SerialMainFile> serialListInStock = (List<SerialMainFile>)KrystalCommonUtil
                .DataSetToList<SerialMainFile>(getSerial, 0);
            if (serialListInStock == null || serialListInStock.Count <= 0)
            {
                isSucceed = false;
            }
            //LINQ两个集合关联,左集合在右边找不到,则为空,找出为空的数据提示出来
            //使用查询语句
            var list = from left in objList
                       join right in serialListInStock on left["F_XXXX_SerialId_Id"] + "" equals right.FSERIALID + "" into temp
                       select new
                       {
                           serialId = left["F_XXXX_SerialId_Id"] + "",
                           serialNumber = (left["F_XXXX_SerialId"] as DynamicObject)["Number"] + "",
                           serialNo = temp.Select(t => t.FSERIALNO).FirstOrDefault()
                       };

            var isEmpty = list.Where(s => s.serialNo.IsNullOrEmptyOrWhiteSpace()).ToList();
            if (isEmpty != null && isEmpty.Count > 0)
            {
                isSucceed = false;
                List<string> empNumber = isEmpty.Select(s => s.serialNumber).ToList();
                serialMsg = string.Format(@"序列号不在当前库存里,可以查询序列号报表。在库则可能未锁定【{0}】", string.Join(",", empNumber));
                //this.View.ShowErrMessage(string.Format(@"序列号不在当前库存里,可以查询序列号报表。在库则可能未锁定【{0}】", string.Join(",", empNumber)));
                return isSucceed;
            }
            return isSucceed;
        }


        /// <summary>
        /// 保存解锁信息
        /// </summary>
        /// <returns></returns>
        private bool SaveUnLockStockBySerial()
        {
            Entity unLockEntity = View.BusinessInfo.GetEntity("FEntity");
            DynamicObjectCollection objList = View.Model.GetEntityDataObject(unLockEntity);
            if (objList == null || objList.Count <= 0 || objList.Count > 1)
            {
                this.View.ShowErrMessage("只能处理一行物料。");
                return true;
            }
            Entity serialEntity = View.BillBusinessInfo.GetEntity("F_XXXX_Serial");
            List<DynamicObject> subEntityList = (from p in View.Model.GetEntityDataObject(serialEntity)
                                                 where p["F_XXXX_SerialId"] != null
                                                 select p).ToList();
            if (subEntityList == null || subEntityList.Count <= 0)
            {
                this.View.ShowErrMessage("序列号必须录入。");
                return true;
            }
            List<LockStockArgs> stockArgList = new List<LockStockArgs>();
            List<LockStockArgs> warnStockArgList = new List<LockStockArgs>();
            bool isCancel = false;
            bool isWarn = false;
            List<string> messages = new List<string>();
            List<string> warnMessages = new List<string>();
            int rowindex = 0;
            bool haveSelRow = false;
            string preMsg = ResManager.LoadKDString("第{0}行记录", "004023000017456", SubSystemType.SCM);
            foreach (DynamicObject obj in objList)
            {
                rowindex++;
                if (!Convert.ToBoolean(obj["FSelect"]))
                {
                    continue;
                }
                haveSelRow = true;
                isCancel = false;
                isWarn = false;
                string invId = obj["InvDetailID"] + "";
                decimal unLockQty = Convert.ToDecimal(obj["UnLockQty"]);
                decimal lockQty = Convert.ToDecimal(obj["LockQty"]);
                long secUnitID = GetDynamicValue(obj["SecUnitID"] as DynamicObject);
                decimal secUnLockQty = Convert.ToDecimal(obj["SecUnLockQty"]);
                decimal secLockQty = Convert.ToDecimal(obj["SecLockQty"]);
                Convert.ToDecimal(obj["LeftQty"]);
                decimal secLeftQty = Convert.ToDecimal(obj["SecLeftQty"]);
                decimal baseUnLockQty = Convert.ToDecimal(obj["BaseUnLockQty"]);
                decimal baselockQty = Convert.ToDecimal(obj["BaseLcokQty"]);
                decimal baseLeftQty = Convert.ToDecimal(obj["BaseLeftQty"]);
                if (baseUnLockQty == 0m && secUnLockQty == 0m)
                {
                    continue;
                }
                if (obj["ReleaseDate"] != null && !string.IsNullOrWhiteSpace(obj["ReleaseDate"].ToString()) && DateTime.Parse(obj["ReleaseDate"].ToString()) != DateTime.Today)
                {
                    isWarn = true;
                    string message3 = ResManager.LoadKDString("第{0}行记录的预计解锁日期{1}不等于系统当前日期{2}", "004023030009708", SubSystemType.SCM);
                    warnMessages.Add(string.Format(message3, rowindex, ((DateTime)obj["ReleaseDate"]).ToShortDateString(), DateTime.Today.ToShortDateString()));
                }
                if (baseUnLockQty < 0m)
                {
                    messages.Add(string.Format(preMsg, rowindex) + ResManager.LoadKDString("解锁数量(基本)不能小于0", "004023000022233", SubSystemType.SCM));
                    isCancel = true;
                }
                else if (baselockQty < baseUnLockQty)
                {
                    messages.Add(string.Format(preMsg, rowindex) + ResManager.LoadKDString("解锁数量(基本)超过可解锁数量(基本)", "004023000022234", SubSystemType.SCM));
                    isCancel = true;
                }
                else if (secUnitID > 0)
                {
                    if (secLockQty < secUnLockQty)
                    {
                        messages.Add(string.Format(preMsg, rowindex) + ResManager.LoadKDString("解锁数量(辅助)超过可解锁数量(辅助)", "004023030000436", SubSystemType.SCM));
                        isCancel = true;
                    }
                    else if (baseLeftQty == 0m && secLeftQty != 0m)
                    {
                        messages.Add(string.Format(preMsg, rowindex) + ResManager.LoadKDString("剩余解锁量(基本)和剩余解锁量(辅助)一个为0,另一个不为0", "004023000022246", SubSystemType.SCM));
                        isCancel = true;
                    }
                }
                #region 序列号校验

                //序列号个数=锁库数
                decimal serialCount = subEntityList.Count;

                //C#判断锁库数是否为整数
                if (serialCount != unLockQty)
                {
                    //this.View.ShowErrMessage("序列号个数必须与解锁量一致。");
                    messages.Add("序列号个数必须与解锁量一致。");
                    isCancel = true;
                }
                string serialMsg = string.Empty;
                if (!CheckSerial(subEntityList, invId, out serialMsg))
                {
                    messages.Add(serialMsg);
                    isCancel = true;
                }
                #endregion

                if (!isCancel)//不取消
                {
                    LockStockArgs stockArg = new LockStockArgs();
                    stockArg.FEntryID = Convert.ToInt64(obj["Id"]);
                    stockArg.FInvDetailID = obj["InvDetailID"].ToString();
                    stockArg.BillDetailID = obj["BillDetailID"].ToString();
                    stockArg.BillNo = Convert.ToString(obj["BILLNO"]);
                    if (Convert.ToInt32(obj["BILLSEQ"]) > 0)
                    {
                        stockArg.BillSEQ = Convert.ToInt32(obj["BILLSEQ"]);
                    }
                    if (obj["Lot"] is DynamicObject dyLot && Convert.ToInt64(dyLot["Id"]) > 0)
                    {
                        stockArg.Lot = GetDynamicValue(dyLot);
                    }
                    stockArg.LockQty = lockQty;
                    stockArg.UnLockQty = unLockQty;
                    stockArg.LockBaseQty = Convert.ToDecimal(obj["BaseLcokQty"]);
                    stockArg.UnLockBaseQty = baseUnLockQty;
                    stockArg.LockSecQty = Convert.ToDecimal(obj["SecLockQty"]);
                    stockArg.UnLockSecQty = secUnLockQty;
                    if (obj["ReserveDate"] != null && !string.IsNullOrWhiteSpace(obj["ReserveDate"].ToString()))
                    {
                        stockArg.ReserveDate = DateTime.Parse(obj["ReserveDate"].ToString());
                    }
                    stockArg.ReserveDays = Convert.ToInt32(obj["ReserveDays"]);
                    if (obj["ReleaseDate"] != null && !string.IsNullOrWhiteSpace(obj["ReleaseDate"].ToString()))
                    {
                        stockArg.ReLeaseDate = DateTime.Parse(obj["ReleaseDate"].ToString());
                    }
                    stockArg.UnLockNote = Convert.ToString(obj["UnLockNote"]);
                    if (isWarn)
                    {
                        warnStockArgList.Add(stockArg);
                    }
                    else
                    {
                        stockArgList.Add(stockArg);
                    }
                }
            }
            if (!haveSelRow)
            {
                View.ShowErrMessage(ResManager.LoadKDString("没有选择任何数据解锁,请先选择数据。", "004023000019865", SubSystemType.SCM));
                return true;
            }
            isCancel = false;
            if (messages.Count > 0)
            {
                isCancel = true;
                List<FieldAppearance> listFieldApp2 = new List<FieldAppearance>();
                FieldAppearance fielAp2 = K3DisplayerUtil.CreateDisplayerField<TextFieldAppearance, TextField>(View.Context, "FErrInfo", ResManager.LoadKDString("异常信息", "004023000017458", SubSystemType.SCM));
                fielAp2.Width = new LocaleValue("500", View.Context.UserLocale.LCID);
                listFieldApp2.Add(fielAp2);
                K3DisplayerModel k3Model2 = K3DisplayerModel.Create(View.Context, listFieldApp2.ToArray());
                foreach (string message2 in messages)
                {
                    k3Model2.AddMessage(message2);
                }
                k3Model2.CancelButton.Visible = false;
                View.ShowK3Displayer(k3Model2);
                return true;
            }
            if (warnMessages.Count > 0)
            {
                List<FieldAppearance> listFieldApp = new List<FieldAppearance>();
                FieldAppearance fielAp = K3DisplayerUtil.CreateDisplayerField<TextFieldAppearance, TextField>(View.Context, "FErrInfo", ResManager.LoadKDString("异常信息", "004023000017458", SubSystemType.SCM));
                fielAp.Width = new LocaleValue("500", View.Context.UserLocale.LCID);
                listFieldApp.Add(fielAp);
                K3DisplayerModel k3Model = K3DisplayerModel.Create(View.Context, listFieldApp.ToArray());
                foreach (string message in warnMessages)
                {
                    k3Model.AddMessage(message);
                }
                k3Model.CancelButton.Visible = true;
                k3Model.CancelButton.Caption = new LocaleValue(ResManager.LoadKDString("", "004023000013912", SubSystemType.SCM));
                k3Model.OKButton.Caption = new LocaleValue(ResManager.LoadKDString("", "004023030005539", SubSystemType.SCM));
                k3Model.OKButton.Visible = true;
                k3Model.SummaryMessage = ResManager.LoadKDString("以下锁库记录的预计解锁日期与当前日期不同,是否继续?", "004023000017459", SubSystemType.SCM);
                View.ShowK3Displayer(k3Model, delegate (FormResult o)
                {
                    if (o != null && o.ReturnData is K3DisplayerModel && (o.ReturnData as K3DisplayerModel).IsOK)
                    {
                        isCancel = true;
                        stockArgList.AddRange(warnStockArgList);
                        if (stockArgList.Count > 0)
                        {
                            using (KDTransactionScope scope = new KDTransactionScope(TransactionScopeOption.RequiresNew))
                            {
                                StockServiceHelper.SaveUnLockInfo(base.Context, stockArgList, (opType.Equals("StockLock") || opType.Equals("StockLockLog")) ? "Inv" : opType);
                                int updateCount = UnLockSerial(subEntityList);

                                scope.Complete();
                            }
                            this.View.Model.SetValue("FLockQty", 0, 0);
                            //this.View.Model.SetValue("BaseLcokQty", 0, 0);
                            this.View.UpdateView("FLockQty");
                            IsClose = true;
                            View.ReturnToParentWindow(true);
                            View.Close();
                        }
                    }
                    else
                    {
                        isCancel = true;
                    }
                });
                return true;
            }
            if (!isCancel && stockArgList.Count > 0)
            {
                using (KDTransactionScope scope = new KDTransactionScope(TransactionScopeOption.RequiresNew))
                {
                    StockServiceHelper.SaveUnLockInfo(base.Context, stockArgList, (opType.Equals("StockLock") || opType.Equals("StockLockLog")) ? "Inv" : opType);
                    int updateCount = UnLockSerial(subEntityList);
                    scope.Complete();
                }
                this.View.Model.SetValue("FLockQty", 0, 0);
                //this.View.Model.SetValue("BaseLcokQty", 0, 0);
                this.View.UpdateView("FLockQty");
                View.ShowNotificationMessage(ResManager.LoadKDString("解锁操作成功!", "004023030000442", SubSystemType.SCM));
                IsClose = true;
                View.ReturnToParentWindow(true);
            }
            else if (!isCancel)
            {
                IsClose = true;
                View.ReturnToParentWindow(false);
            }
            return isCancel;
        }


        private int UnLockSerial(List<DynamicObject> subEntityList)
        {
            int updateCount = 0;
            List<string> updateSql = new List<string>();
            foreach (var ss in subEntityList)
            {
                updateSql.Add(string.Format(@"{0}UPDATE T_BD_SERIALMASTER SET F_XXXX_ISLOCK=0 WHERE FSERIALID={1};", OtherConst.DIALECT, ss["F_XXXX_SerialId_Id"]));
            }
            if (updateSql != null && updateSql.Count > 0)
            {
                updateCount = DBUtils.ExecuteBatch(this.Context, updateSql, updateSql.Count);
            }
            return updateCount;
        }
        #endregion <方法>

        #region <事件>
        public override void BeforeSetItemValueByNumber(BeforeSetItemValueByNumberArgs e)
        {
            base.BeforeSetItemValueByNumber(e);
            switch (e.BaseDataField.Key.ToUpperInvariant())
            {
                case "F_XXXX_SERIALID":
                    // 未审核的基础资料也显示出来
                    e.IsShowApproved = false;
                    break;
            }
        }

        public override void BeforeF7Select(BeforeF7SelectEventArgs e)
        {
            base.BeforeF7Select(e);
            switch (e.FieldKey.ToUpperInvariant())
            {
                case "F_XXXX_SERIALID":
                    string DelFilter = "";
                    Entity subEntity = View.BillBusinessInfo.GetEntity("FEntity");
                    var getEntity = View.Model.GetEntityDataObject(subEntity);
                    if (getEntity == null || getEntity.Count <= 0 || getEntity.Count > 1)
                    {
                        this.View.ShowErrMessage("只能处理一行物料");
                        return;
                    }
                    var em = getEntity.FirstOrDefault();
                    //物料不为空
                    //var mater = em["MaterialId"] as DynamicObject;
                    //if (mater == null)
                    //{
                    //    this.View.ShowErrMessage("物料为空");
                    //    return;
                    //}
                    //long materialId = Convert.ToInt64(mater["Id"]);
                    ////仓库不为空
                    //var stock = em["StockId"] as DynamicObject;
                    //if (stock == null)
                    //{
                    //    this.View.ShowErrMessage("仓库为空");
                    //    return;
                    //}
                    //long stockId = Convert.ToInt64(stock["Id"]);

                    //DelFilter = string.Format(@" t0.FMATERIALID ={0} and t0.F_XXXX_ISLOCK=1 and t2.FSTOCKID={1}  ", materialId, stockId);
                    ////仓位不为空
                    //var stockLoc = em["StockLocId"] as DynamicObject;
                    //if (stockLoc != null)
                    //{
                    //    long stockLocId = Convert.ToInt64(stockLoc["Id"]);
                    //    DelFilter += string.Format(@" and t2.FSTOCKLOCID={0}", stockLocId);
                    //}
                    string invId = em["InvDetailID"] + "";
                    //序列号物料编码,在库状态
                    List<SqlParam> para = new List<SqlParam>()
                    {
                        //new SqlParam("@STOCKID", KDDbType.Int32,493513),
                        new SqlParam("@InvId", KDDbType.String,invId),
                        new SqlParam("@IsLock", KDDbType.Int32,1),
                        //new SqlParam("@ENDTIME",KDDbType.String,_filterArgs.EndTime.ToString("yyyy-MM-dd")),
                        //new SqlParam("@t",KDDbType.String,temp_detail)
                    };
                    //执行sql语句并将结果集填充至DataSet  XXXX_PR_STK_SerialInfoByInvId未使用
                    DataSet getSerial = DBUtils.ExecuteDataSet(this.Context, System.Data.CommandType.StoredProcedure
                        , string.Format(@"{0}XXXX_PR_STK_SerialInfoByInvId", OtherConst.DIALECT), para);



                    //调用上面的将DataSet转换为List<Menu>实体集合
                    List<SerialMainFile> serialListInStock = (List<SerialMainFile>)KrystalCommonUtil
                        .DataSetToList<SerialMainFile>(getSerial, 0);
                    if (serialListInStock == null || serialListInStock.Count <= 0)
                    {
                        //isSucceed = false;//无可解锁数据
                    }
                    else
                    {
                        //DelFilter = string.Format(@"  EXISTS (SELECT FID   FROM fn_StrSplit('{0}',',') m where m.fid=t0.FSERIALID )"
                        //  , string.Join(",", serialListInStock.ToList().Select(s => s.FSERIALID).ToList()));
                        DelFilter = string.Format(@"  t0.FSERIALID in  ({0})"
                          , string.Join(",", serialListInStock.ToList().Select(s => s.FSERIALID).ToList()));
                    }

                    if (e.ListFilterParameter.Filter.IsNullOrEmptyOrWhiteSpace())
                    {
                        e.ListFilterParameter.Filter = DelFilter;
                    }
                    else
                    {
                        e.ListFilterParameter.Filter = " and " + DelFilter;
                    }

                    // 未审核的基础资料也显示出来
                    e.IsShowApproved = false;
                    // 已禁用的基础资料也显示出来
                    //e.IsShowUsed = false;
                    break;
            }
        }
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            switch (e.BarItemKey.ToUpperInvariant())
            {
                case "XXXX_TBUNLOCKSERIAL":
                    //解锁的即时库存只有一条
                    //序列号不为空,序列号的个数=解锁数
                    //序列号全部在当前即时库存,未锁定,一个不满足,不可以锁
                    string text;
                    if ((text = opType) == null || !(text == "Inv"))
                    {
                        return;
                    }
                    e.Cancel = SaveUnLockStockBySerial();
                    if (!e.Cancel)
                    {
                        View.Close();
                    }

                    break;
            }

        }

        public override void BeforeClosed(BeforeClosedEventArgs e)
        {
            
        }
        #endregion <事件>
    }
}
完整代码

 

 

测试

 

 

 

 

 

 解锁成功直接关闭窗口,可以查询锁库日志

 

 

posted @ 2024-08-19 17:52  lanrenka  阅读(43)  评论(0编辑  收藏  举报