jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/********************************************************************************
 
** auth: ZH
 
** date: 2018/12/26
 
** desc: 批分拓展方法
 
** Ver.:  V1.0.0
 
*********************************************************************************/
using HH.WMS.Entitys;
using HH.WMS.Entitys.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HH.WMS.BLL;
using HH.WMS.Entitys.Entitys;
 
namespace HH.WMS.WebApi.Extension
{
    public static class BatchesExtension
    {
        #region 批分托盘物料关联主子表,降数量
        /// <summary>
        /// 批分托盘物料关联主子表,降数量
        /// </summary>
        /// <param name="batchesList"></param>
        /// <returns></returns>
        public static List<TN_WM_B_TRAY_ITEM_MSTEntity> BatchesDropTrayItemQty(this List<BatchesEntity> batchesList, bool dropAlloc = true)
        {
            var trayItemMstList = BLLCreator.CreateDapper<TN_WM_B_TRAY_ITEM_MSTEntity>().GetList(new
            {
                CN_S_TRAY_CODE = batchesList.Select(x => x.TrayCode).ToList(),
                CN_S_ITEM_CODE = batchesList.Select(x => x.ItemCode).ToList()
            });
            foreach (var trayItemMst in trayItemMstList)
            {
                trayItemMst.TrayItemDtlList = new List<TN_WM_B_TRAY_ITEM_DTLEntity>();
                //属于当前托盘,当前物料的分拣明细
                var currentBatches = batchesList.Where(x => x.ItemCode == trayItemMst.CN_S_ITEM_CODE && x.TrayCode == trayItemMst.CN_S_TRAY_CODE);
                decimal qty = currentBatches.Sum(y => y.Qty);
                if (qty == 0) continue;
 
                bool batches = false;
                if (dropAlloc)
                    batches = trayItemMst.CN_F_QUANTITY >= qty &&
                        trayItemMst.CN_F_ALLOC_QTY >= qty;
                else
                    batches = trayItemMst.CN_F_QUANTITY - trayItemMst.CN_F_ALLOC_QTY >= qty;
 
                if (batches)
                {
                    trayItemMst.CN_F_QUANTITY -= qty;
                    if (dropAlloc)
                        trayItemMst.CN_F_ALLOC_QTY -= qty;
 
                    trayItemMst.TrayItemDtlList = BLLCreator.CreateDapper<TN_WM_B_TRAY_ITEM_DTLEntity>().GetList(new
                    {
                        CN_PARENT_GUID = trayItemMst.CN_GUID
                    }).OrderBy(x => x.CN_S_LOT_NO).ToList();
 
                    foreach (var cs in currentBatches)
                    {
                        var csQty = cs.Qty;
                        foreach (var trayItemDtl in trayItemMst.TrayItemDtlList)
                        {
                            if (csQty == 0) break;
                            if (trayItemDtl.CN_F_QUANTITY == 0) continue;
                            if ((string.IsNullOrEmpty(cs.ItemProductionLot) || cs.ItemProductionLot.Equals(trayItemDtl.CN_S_PRODUCTION_BATCH)) &&
                                (string.IsNullOrEmpty(cs.ItemArrivalLot) || cs.ItemArrivalLot.Equals(trayItemDtl.CN_S_LOT_NO)))
                            {
                                if (trayItemDtl.CN_F_QUANTITY >= csQty)
                                {
                                    trayItemDtl.CN_F_QUANTITY -= csQty;
                                    csQty = 0;
                                }
                                else
                                {
                                    csQty -= trayItemDtl.CN_F_QUANTITY;
                                    trayItemDtl.CN_F_QUANTITY = 0;
                                }
                            }
                        }
                    }
                }
            }
            return trayItemMstList;
        }
        #endregion
    }
}