/********************************************************************************
** 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 批分托盘物料关联主子表,降数量
///
/// 批分托盘物料关联主子表,降数量
///
///
///
public static List BatchesDropTrayItemQty(this List batchesList, bool dropAlloc = true)
{
var trayItemMstList = BLLCreator.CreateDapper().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();
//属于当前托盘,当前物料的分拣明细
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().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
}
}