using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Enums; using WIDESEA_Core.Helper; using WIDESEA_DTO.Stock; using WIDESEA_IBasicRepository; using WIDESEA_IBasicService; using WIDESEA_IOutboundRepository; using WIDESEA_IOutboundService; using WIDESEA_IRecordService; using WIDESEA_IStockRepository; using WIDESEA_IStockService; using WIDESEA_Model.Models; using WIDESEA_OutboundRepository; using WIDESEA_StockRepository; namespace WIDESEA_OutboundService { public partial class OutboundOrderDetailService : ServiceBase, IOutboundOrderDetailService { public override PageGridData GetPageData(PageDataOptions options) { return base.GetPageData(options); } public WebResponseContent LockOutboundStock(int orderDetailId) { Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId); (bool, string) result = CheckDeital(outboundOrderDetail); if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2); decimal needQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity; List outStock = new List(); List stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode); decimal stockTotalQuantity = stockInfos.Select(x => x.Details.Sum(v => v.StockQuantity - v.OutboundQuantity)).Sum(x => x); if (stockTotalQuantity >= needQuantity)//库存够 { int index = 0; while (needQuantity > 0) { Dt_StockInfo stockInfo = stockInfos[index]; decimal useableStockQuantity = stockInfo.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity - x.OutboundQuantity); if (useableStockQuantity < needQuantity) { outboundOrderDetail.LockQuantity += useableStockQuantity; stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity); needQuantity -= useableStockQuantity; } else { outboundOrderDetail.LockQuantity += needQuantity; stockInfo.Details.ForEach(x => { if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == outboundOrderDetail.MaterielCode) { if (x.StockQuantity - x.OutboundQuantity >= needQuantity) { x.OutboundQuantity += needQuantity; needQuantity = 0; } else { needQuantity -= (x.StockQuantity - x.OutboundQuantity); x.OutboundQuantity = x.StockQuantity; } } }); } outStock.Add(stockInfo); index++; } return WebResponseContent.Instance.OK(data: new { stockInfos, outboundOrderDetail }); } else { for (int i = 0; i < stockInfos.Count; i++) { Dt_StockInfo stockInfo = stockInfos[i]; decimal useableStockQuantity = stockInfo.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity - x.OutboundQuantity); if (useableStockQuantity < needQuantity) { outboundOrderDetail.LockQuantity += useableStockQuantity; stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity); needQuantity -= useableStockQuantity; } else { outboundOrderDetail.LockQuantity += needQuantity; stockInfo.Details.ForEach(x => { if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == outboundOrderDetail.MaterielCode) { if (x.StockQuantity - x.OutboundQuantity >= needQuantity) { x.OutboundQuantity += needQuantity; needQuantity = 0; } else { needQuantity -= (x.StockQuantity - x.OutboundQuantity); x.OutboundQuantity = x.StockQuantity; } } }); } outStock.Add(stockInfo); } return WebResponseContent.Instance.OK(data: new { stockInfos, outboundOrderDetail }); } } private (bool, string) CheckDeital(Dt_OutboundOrderDetail outboundOrderDetail) { if (outboundOrderDetail == null) { return (false, "未找到出库单明细信息"); } if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt()) { return (false, "该明细不可操作"); } return (true, "成功"); } public (List, List, List) AssignStockOutbound(Dt_OutboundOrder outboundOrder, List stockSelectViews) { List outStocks = null; List outStockLockInfos = _outStockLockInfoService.GetOutStockLockInfos(outboundOrder, outboundOrder.Details.First(), outStocks); List locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()); return (outStocks, outStockLockInfos, locationInfos); } /// /// 分配出库库存 /// /// /// 指定库存 /// /// public (List, Dt_OutboundOrderDetail, List, List) AssignStockOutbound(Dt_OutboundOrderDetail outboundOrderDetail, List stockSelectViews) { (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews); if (!checkResult.Item1) throw new Exception(checkResult.Item2); Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetail.OrderId); decimal originalNeedQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity; decimal needQuantity = originalNeedQuantity; #region 根据托盘号查找库存 //List outStocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(stockSelectViews.Select(x => x.PalletCode).ToList()); #endregion List outStockDetails = _stockService.StockInfoDetailService.GetStockInfosByBatchNoCodes(stockSelectViews); List outStocks = _stockService.StockInfoService.Repository.GetStockInfosByIds(outStockDetails.Select(x => x.StockId).ToList()); decimal assignQuantity = outStocks.Count; #region MyRegion //decimal assignQuantity = 0; //outStocks.ForEach(x => //{ // x.Details.ForEach(v => // { // assignQuantity += v.StockQuantity - v.OutboundQuantity; // }); //}); #endregion outboundOrderDetail.LockQuantity += assignQuantity; outStocks.ForEach(x => { x.Details.ForEach(v => { v.OutboundQuantity = v.StockQuantity; }); }); needQuantity -= assignQuantity; #region 指定出库数量少于订单数量自动分配满足条件库存 //if (outboundOrderDetail.OrderQuantity > outboundOrderDetail.LockQuantity) //{ // List stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode); // stockInfos = stockInfos.Where(x => !stockSelectViews.Select(v => v.PalletCode).Contains(x.PalletCode)).ToList(); // List autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, outboundOrderDetail.MaterielCode, needQuantity, out decimal residueQuantity); // outboundOrderDetail.LockQuantity += needQuantity - residueQuantity; // outStocks.AddRange(autoAssignStocks); // outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt(); // if (residueQuantity > 0) // { // outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt(); // } //} #endregion #region 未指定库存,自动分配库存 if (outboundOrderDetail.OrderQuantity > outboundOrderDetail.LockQuantity) { List stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode);//获取所有库存 stockInfos = stockInfos.Where(x => !outStocks.Select(v => v.Id).Contains(x.Id)).ToList(); List autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, outboundOrderDetail.MaterielCode, needQuantity, out decimal residueQuantity); outboundOrderDetail.LockQuantity += needQuantity - residueQuantity; outStocks.AddRange(autoAssignStocks); //outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt(); //if (residueQuantity > 0) //{ // outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt(); //} } #endregion outboundOrderDetail.OrderDetailStatus = outboundOrderDetail.OrderQuantity > outboundOrderDetail.LockQuantity ? OrderDetailStatusEnum.AssignOverPartial.ObjToInt() : OrderDetailStatusEnum.AssignOver.ObjToInt(); //添加出库分配详情 List outStockLockInfos = _outStockLockInfoService.GetOutStockLockInfos(outboundOrder, outboundOrderDetail, outStocks); List locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()); return (outStocks, outboundOrderDetail, outStockLockInfos, locationInfos); } /// /// 指定库存锁定库存 /// /// /// /// public WebResponseContent LockOutboundStock(int orderDetailId, List stockSelectViews) { try { Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId); (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews); if (!checkResult.Item1) throw new Exception(checkResult.Item2); //分配出库库存 (List, Dt_OutboundOrderDetail, List, List) result = AssignStockOutbound(outboundOrderDetail, stockSelectViews); _unitOfWorkManage.BeginTran(); WebResponseContent content = LockOutboundStockDataUpdate(result.Item1, new List { result.Item2 }, result.Item3, result.Item4); if (content.Status) { _unitOfWorkManage.CommitTran(); } else { _unitOfWorkManage.RollbackTran(); } return content; } catch (Exception ex) { _unitOfWorkManage.RollbackTran(); return WebResponseContent.Instance.Error(ex.Message); } } /// /// 锁定库存//不指定库存锁定库存 /// /// /// public WebResponseContent LockOutboundStock(int[] keys) { try { List stockSelectViews = new List(); List stockInfos = new List(); List outboundOrderDetails = new List(); List outStockLockInfos = new List(); List locationInfos = new List(); foreach (var item in keys) { Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == item); (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews); if (!checkResult.Item1) throw new Exception(checkResult.Item2); (List, Dt_OutboundOrderDetail, List, List) result = AssignStockOutbound(outboundOrderDetail, stockSelectViews); if (result.Item1.Count > 0) { stockInfos.AddRange(result.Item1); outboundOrderDetails.Add(result.Item2); outStockLockInfos.AddRange(result.Item3); locationInfos.AddRange(result.Item4); } } _unitOfWorkManage.BeginTran(); WebResponseContent content = LockOutboundStockDataUpdate(stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos); if (content.Status) { _unitOfWorkManage.CommitTran(); } else { _unitOfWorkManage.RollbackTran(); } return content; } catch (Exception ex) { _unitOfWorkManage.RollbackTran(); return WebResponseContent.Instance.Error(ex.Message); } } /// /// 锁定出库单库存数据修改 /// /// /// /// /// /// /// /// public WebResponseContent LockOutboundStockDataUpdate(List stockInfos, List outboundOrderDetails, List outStockLockInfos, List locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.OutLock, List? tasks = null) { try { #region 修改库存和库存详情 List stockInfoDetails = new List(); foreach (var item in stockInfos) { if (item.StockStatus != StockStatusEmun.移库中.ObjToInt()) { item.StockStatus = locationStatus == LocationStatusEnum.InStock ? StockStatusEmun.已入库.ObjToInt() : StockStatusEmun.出库锁定.ObjToInt(); item.Details.ForEach(x => { x.Status = item.StockStatus; }); } stockInfoDetails.AddRange(item.Details); } _stockService.StockInfoService.Repository.UpdateData(stockInfos); _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails); #endregion #region 修改出库单和出库单详情 List outboundOrders = _outboundRepository.OutboundOrderRepository.GetStockInfos(outboundOrderDetails.Select(x => x.OrderId).ToList()); foreach (var item in outboundOrderDetails) { var outboundOrder = outboundOrders.Where(x => x.Id == item.OrderId).FirstOrDefault(); outboundOrder.Details.RemoveAll(x => x.Id == item.Id); outboundOrder.Details.Add(item); outboundOrder.OrderStatus = outboundOrder.Details.FirstOrDefault (x => x.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt()) != null ? OutboundStatusEnum.出库中.ObjToInt() : OutboundStatusEnum.未开始.ObjToInt(); //var outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == item.OrderId); //if (!outboundOrders.Any(x => x.Id == item.OrderId)) //{ // var outboundOrder = _outboundRepository.OutboundOrderRepository.GetStockInfo(item.OrderId); // if (outboundOrder != null) // { // outboundOrder.Details.RemoveAll(x => x.Id == item.Id); // outboundOrder.Details.Add(item); // outboundOrder.OrderStatus = outboundOrder.Details.FirstOrDefault // (x => x.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt()) != null // ? OutboundStatusEnum.出库中.ObjToInt() : OutboundStatusEnum.未开始.ObjToInt(); // outboundOrders.Add(outboundOrder); // } //} } _outboundRepository.OutboundOrderRepository.UpdateData(outboundOrders); BaseDal.UpdateData(outboundOrderDetails); #endregion #region 添加出库详情 List addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList(); if (addOutStockLockInfos != null && addOutStockLockInfos.Any()) { _outStockLockInfoService.Repository.AddData(addOutStockLockInfos); } #endregion #region 修改出库详情 List updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList(); if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any()) { _outStockLockInfoService.Repository.UpdateData(updateOutStockLockInfos); } #endregion //添加货位信息变更 _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, locationStatus.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", tasks?.Select(x => x.TaskNum).ToList()); _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, locationStatus); return WebResponseContent.Instance.OK(); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } private (bool, string) CheckSelectStockDeital(Dt_OutboundOrderDetail outboundOrderDetail, List stockSelectViews) { if (outboundOrderDetail == null) { return (false, "未找到出库单明细信息"); } if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt()) { return (false, "该明细不可操作"); } if (stockSelectViews.Sum(x => x.UseableQuantity) > outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity) { return (false, "选择数量超出单据数量"); } return (true, "成功"); } /// /// 撤销分配 /// /// /// public WebResponseContent RevokeLockOutboundStock(int orderDetailId) { Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId); (bool, string) result = CheckRevoke(outboundOrderDetail); if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2); List outStockLockInfos = _outStockLockInfoService.GetByOrderDetailId(orderDetailId, OutStockStatus.已分配); if (outStockLockInfos.Count > 0) { List stocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(outStockLockInfos.Select(x => x.PalletCode).ToList()); if (stocks.Count > 0) { stocks.ForEach(x => { x.Details.ForEach(v => { v.OutboundQuantity = 0; }); }); outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(); outboundOrderDetail.LockQuantity = outboundOrderDetail.OverOutQuantity; outStockLockInfos.ForEach(x => { x.Status = OutStockStatus.撤销.ObjToInt(); }); List locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(stocks.Select(x => x.LocationCode).ToList()); return LockOutboundStockDataUpdate(stocks, new List { outboundOrderDetail }, outStockLockInfos, locationInfos, LocationStatusEnum.InStock); } return WebResponseContent.Instance.Error("未找到库存信息"); } return WebResponseContent.Instance.Error("未找到已分配的出库信息"); } private (bool, string) CheckRevoke(Dt_OutboundOrderDetail outboundOrderDetail) { if (outboundOrderDetail == null) { return (false, "未找到出库单明细信息"); } if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOver.ObjToInt()) { return (false, "该明细不可操作"); } return (true, "成功"); } } }