From c4c9228d894b455327c76a6e286e394513ef542a Mon Sep 17 00:00:00 2001
From: Ben Lin <maobin001@msn.com>
Date: 星期三, 25 十二月 2024 00:22:57 +0800
Subject: [PATCH] 生成入库单更新

---
 Tiger.Business.MES/Transaction/InStoreOrderNo.cs |  225 +++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 158 insertions(+), 67 deletions(-)

diff --git a/Tiger.Business.MES/Transaction/InStoreOrderNo.cs b/Tiger.Business.MES/Transaction/InStoreOrderNo.cs
index 78bb79d..ad37121 100644
--- a/Tiger.Business.MES/Transaction/InStoreOrderNo.cs
+++ b/Tiger.Business.MES/Transaction/InStoreOrderNo.cs
@@ -38,7 +38,7 @@
             OrgCode = input.OrgCode;
             pageSize = input.pageSize;
             _GetScannedList();
-            CurBatchNo = !inStoreInfos.IsNullOrEmpty() && inStoreInfos.InStoreScanInfos.Count>0? InStoreScanInfos[0].BATCH_NO: DateTime.Now.ToString("yyyyMMddHHmmss");
+            CurBatchNo = !inStoreInfos.IsNullOrEmpty() && inStoreInfos.InStoreScanInfos.Count > 0 ? InStoreScanInfos[0].BATCH_NO : DateTime.Now.ToString("yyyyMMddHHmmss");
             Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
             return this;
         }
@@ -66,8 +66,9 @@
 
         #region Functions
 
-        public ApiAction<InStoreInfos> GetInStoreInfos() {
-            return new() { Data= inStoreInfos };
+        public ApiAction<InStoreInfos> GetInStoreInfos()
+        {
+            return new() { Data = inStoreInfos };
         }
         /// <summary>
         /// 鎵弿鏉$爜
@@ -230,7 +231,7 @@
                 RcvRptInput.labels = new();
                 RcvRptInput.ErpProdInBths = ErpProdInBths;
                 RcvRptInput.userId = UserCode;
-                inStoreInfos= new()
+                inStoreInfos = new()
                 {
                     ErpProdInBths = ErpProdInBths,
                     InStoreScanInfos = InStoreScanInfos,
@@ -294,7 +295,7 @@
                 {
                     foreach (var sn in ErpProdInSns)
                     {
-                        var d = action.Data.ErpProdInBths.Where(q=> q.WORK_ORDER == sn.SOURCECODE).FirstOrDefault();
+                        var d = action.Data.ErpProdInBths.Where(q => q.WORK_ORDER == sn.SOURCECODE).FirstOrDefault();
                         if (!d.IsNullOrEmpty())
                         {
                             sn.BUSINESSCODE = d.ORDER_NO;
@@ -345,12 +346,13 @@
         /// 鏌ヨ宸叉壂鎻忓垪琛�
         /// </summary>
         /// <returns></returns>
-        public async Task<ApiAction<RcvRptDocCreateInput>> GetScannedList()
+        public async Task<ApiAction<InStoreInfos>> GetScannedList()
         {
-            var action = new ApiAction<RcvRptDocCreateInput>();
+            var action = new ApiAction<InStoreInfos>();
             try
             {
-                action.Data = _GetScannedList();
+                _GetScannedList();
+                action.Data = inStoreInfos;
             }
             catch (Exception ex)
             {
@@ -366,40 +368,71 @@
         private RcvRptDocCreateInput _GetScannedList()
         {
             RcvRptInput.RcvRptDocBases = new();
+            RcvRptInput.ErpProdInBths = new();
             RcvRptInput.PrintJsons = new();
             RcvRptInput.labels = new();
             RcvRptInput.userId = UserCode;
-            if (RcvRptInput.ErpProdInBths.Count == 0)
+            ErpProdInSns.Clear();
+            ErpProdIns.Clear();
+            ErpProdInBths.Clear();
+            InStoreScanInfos.Clear();
+            RcvRptInput.ErpProdInBths = Biz.Db.Queryable<BIZ_ERP_PROD_IN_BTH>()
+                .Where(q => q.BATCH_NO == SqlFunc.Subqueryable<BIZ_ERP_PROD_IN_BTH>().Where(s => s.ORDER_NO == UserCode && s.GHOST_ROW == false).Select(s => s.BATCH_NO))
+                .IncludesAllFirstLayer().ToList();
+            foreach (var item in RcvRptInput.ErpProdInBths)
             {
-                RcvRptInput.ErpProdInBths = Biz.Db.Queryable<BIZ_ERP_PROD_IN_BTH>()
-                    .Where(q => q.BATCH_NO == SqlFunc.Subqueryable<BIZ_ERP_PROD_IN_BTH>().Where(s => s.ORDER_NO == UserCode).Select(s => s.BATCH_NO))
-                    .IncludesAllFirstLayer().ToList();
-                foreach (var item in RcvRptInput.ErpProdInBths)
+                var values = item.ProdInSns.GroupBy(x => x.CARTONNO)
+                    .Select(g => new InStoreScanInfo() { SN = g.Key, SALES_ORDER = item.SALES_ORDER, BATCH_NO = item.BATCH_NO, WORK_ORDER = item.WORK_ORDER, SCAN_QTY = g.Sum(x => x.Qty).ToDouble() });
+                foreach (var t in values)
                 {
-                    var values = item.ProdInSns.GroupBy(x => x.CARTONNO)
-                        .Select(g => new InStoreScanInfo() { SN = g.Key, SALES_ORDER = item.SALES_ORDER, BATCH_NO = item.BATCH_NO, WORK_ORDER = item.WORK_ORDER, SCAN_QTY = g.Sum(x => x.Qty).ToDouble() });
-                    foreach (var t in values)
+                    InStoreScanInfos.Add(t);
+                }
+                item.CartonQty = values.Count();
+
+                ErpProdIns.Add(new()
+                {
+                    AUTH_ORG = OrgCode,
+                    BILLCODE = UserCode,
+                    BILLDATE = DateTime.Now,
+                    STATUS = 0,
+                    HANDLED = -1,
+                    WAREHOUSECODE = "",
+                    SOURCECODE = item.WORK_ORDER,
+                    HANDLED_DATE = DateTime.Now,
+                });
+                ErpProdInSns.AddRange(item.ProdInSns);
+
+                //鐢熸垚鎵撳嵃瀹炰綋
+                if (RcvRptInput.PrintJsons.Count == 0)
+                {
+                    RcvRptInput.PrintJsons.Add(new()
                     {
-                        InStoreScanInfos.Add(t);
+                        ID = Guid.NewGuid().ToString("N"),
+                        Items = new() {
+                            item
+                        }
+                    });
+                }
+                else
+                {
+                    //鎷煎垎椤�
+                    bool isAdded = false;
+                    foreach (var p in RcvRptInput.PrintJsons)
+                    {
+                        if (!p.Items.Any(q => q.WORK_ORDER == item.WORK_ORDER))
+                        {
+                            if (p.Items.Count < pageSize)
+                            {
+                                p.Items.Add(item);
+                                isAdded = true;
+                            }
+                        }
+                        else
+                        {
+                            isAdded = true;
+                        }
                     }
-                    item.CartonQty = values.Count();
-
-                    BIZ_ERP_PROD_IN prodIn = new()
-                    {
-                        AUTH_ORG = OrgCode,
-                        BILLCODE = UserCode,
-                        BILLDATE = DateTime.Now,
-                        STATUS = 0,
-                        HANDLED = -1,
-                        WAREHOUSECODE = "",
-                        SOURCECODE = item.WORK_ORDER,
-                        HANDLED_DATE = DateTime.Now,
-                    };
-                    ErpProdIns.Add(prodIn);
-                    ErpProdInSns.AddRange(item.ProdInSns);
-
-                    //鐢熸垚鎵撳嵃瀹炰綋
-                    if (RcvRptInput.PrintJsons.Count == 0)
+                    if (!isAdded)
                     {
                         RcvRptInput.PrintJsons.Add(new()
                         {
@@ -409,40 +442,10 @@
                         }
                         });
                     }
-                    else
-                    {
-                        //鎷煎垎椤�
-                        bool isAdded = false;
-                        foreach (var p in RcvRptInput.PrintJsons)
-                        {
-                            if (!p.Items.Any(q => q.WORK_ORDER == item.WORK_ORDER))
-                            {
-                                if (p.Items.Count < pageSize)
-                                {
-                                    p.Items.Add(item);
-                                    isAdded = true;
-                                }
-                            }
-                            else
-                            {
-                                isAdded = true;
-                            }
-                        }
-                        if (!isAdded)
-                        {
-                            RcvRptInput.PrintJsons.Add(new()
-                            {
-                                ID = Guid.NewGuid().ToString("N"),
-                                Items = new() {
-                            item
-                        }
-                            });
-                        }
-                    }
                 }
-                ErpProdInBths = RcvRptInput.ErpProdInBths;
             }
-            
+            ErpProdInBths = RcvRptInput.ErpProdInBths;
+
             inStoreInfos = new()
             {
                 ErpProdInBths = ErpProdInBths,
@@ -451,6 +454,94 @@
             return RcvRptInput;
         }
 
+        /// <summary>
+        /// 鍒犻櫎绠�
+        /// </summary>
+        /// <param name="SN"></param>
+        /// <returns></returns>
+        public async Task<ApiAction> Delete(string CartonNo)
+        {
+            var action = new ApiAction();
+            try
+            {
+                var ErpProdInSn = ErpProdInSns.Where(s => s.CARTONNO == CartonNo).ToList();
+                BIZ_ERP_PROD_IN_BTH erpbth = new();
+                if (!ErpProdInSn.IsNullOrEmpty())
+                {
+                    erpbth = ErpProdInBths.Where(q => q.WORK_ORDER == ErpProdInSn[0].SOURCECODE).FirstOrDefault();
+                    if (!erpbth.IsNullOrEmpty())
+                    {
+                        erpbth.SCAN_QTY -= ErpProdInSn.Count(q => q.CARTONNO == CartonNo);
+                        erpbth.CartonQty--;
+                    }
+                }
+
+                //淇濆瓨鏁版嵁搴�
+                var db = Business.Biz.Db;
+                var dbTran = db.UseTran(() =>
+                {
+                    db.Deleteable<BIZ_ERP_PROD_IN_SN>().Where(x => x.BUSINESSCODE == UserCode && x.CARTONNO == CartonNo).ExecuteCommand();
+                    db.Updateable(erpbth, $"InStoreOrderNo_{UserCode}").ExecuteCommand();
+                    if (erpbth.CartonQty == 0)
+                    {
+                        db.Deleteable(erpbth).ExecuteCommand();
+                    }
+                });
+                if (!dbTran.IsSuccess)
+                {
+                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction delete exception");
+                    this.Close(!dbTran.IsSuccess);
+                    throw dbTran.ErrorException;
+                }
+            }
+            catch (Exception ex)
+            {
+                action.CatchExceptionWithLog(ex, $"鏌ヨ宸叉壂鎻忓垪琛ㄥ紓甯�");
+            }
+            return action;
+        }
+
+        /// <summary>
+        /// 娓呴櫎
+        /// </summary>
+        /// <returns></returns>
+        public async Task<ApiAction> Clear()
+        {
+            var action = new ApiAction();
+            try
+            {
+                RcvRptInput = new();
+                ProdInBatchs = new() { };
+                ErpProdInBths = new() { };
+                ErpProdIns = new();
+                /// <summary>
+                /// 鎵弿鍒楄〃 
+                /// </summary>
+                InStoreScanInfos = new() { };
+                ErpProdInSns = new();
+                inStoreInfos = new() { };
+
+                //淇濆瓨鏁版嵁搴�
+                var db = Business.Biz.Db;
+                var dbTran = db.UseTran(() =>
+                {
+                    db.Deleteable<BIZ_ERP_PROD_IN_BTH>().Where(x => x.ORDER_NO == UserCode && x.IS_HANDLED == "N").ExecuteCommand();
+                    db.Deleteable<BIZ_ERP_PROD_IN_SN>().Where(x => x.BUSINESSCODE == UserCode).ExecuteCommand();
+                });
+                if (!dbTran.IsSuccess)
+                {
+                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
+                    this.Close(!dbTran.IsSuccess);
+                    throw dbTran.ErrorException;
+                }
+            }
+            catch (Exception ex)
+            {
+                action.CatchExceptionWithLog(ex, $"鏌ヨ宸叉壂鎻忓垪琛ㄥ紓甯�");
+            }
+            return action;
+        }
+
         #endregion Functions
 
         public override bool Close(bool needSaveHistoryLog = false)

--
Gitblit v1.9.3