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.ERP; 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 NewOutboundOrderDetailService : ServiceBase, INewOutboundOrderDetailService { /// /// 低温、药水库存分配 /// /// /// /// public (List, List, List, List) CPAssignStockOutbound(List outboundOrderDetails) { if (!outboundOrderDetails.Any()) { throw new Exception($"未找到出库单明细信息"); } if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1) { throw new Exception($"请勿同时操作多个单据明细"); } Dt_NewOutboundOrder outboundOrder = _outboundRepository.NewOutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId); List dt_OutboundOrderDetails = _outboundRepository.NewOutboundOrderDetailRepository.QueryData(x => x.OrderId == outboundOrder.Id); List outStocks = new List(); List groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.LocationName }).Select(x => new Dt_NewOutboundOrderDetail { OrderQuantity = x.Sum(v => v.OrderQuantity) - x.Sum(v => v.LockQuantity), MaterielCode = x.Key.MaterielCode, LocationName = x.Key.LocationName }).ToList(); List warehouse = new List(); warehouse = _basicService.WarehouseService.Repository.QueryData(x => x.WarehouseDes == outboundOrder.OutWareHouse); List outStockLockInfos = new List(); List locationInfos = new List(); List PalletCodes = new List(); foreach (var groupDetail in groupDetails) { Dt_StockInfo dt_StockInfo = new Dt_StockInfo(); List dt_StockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => x.MaterielCode == groupDetail.MaterielCode); foreach (var stockInfoDetail in dt_StockInfoDetails) { dt_StockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.Id == stockInfoDetail.StockId && x.StockStatus == StockStatusEmun.入库完成.ObjToInt()); if (dt_StockInfo != null) { PalletCodes.Add(dt_StockInfo.PalletCode); } } if (!PalletCodes.Any()) { throw new Exception($"未找到可分配库存"); } } foreach (var PalletCode in PalletCodes.Distinct()) { List stockInfos = new List(); if (outboundOrderDetails.All(x => x.OrderQuantity == x.LockQuantity)) break; foreach (var item in groupDetails) { if (item.OrderQuantity == item.LockQuantity) continue; ///出库指定库位出库判断 if (item.LocationName != null && item.LocationName != "") { //stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse).Where(x => x.LocationCode == item.LocationName).ToList(); List nowStockInfos = Db.Queryable() .Where(x => x.PalletCode == PalletCode && x.Details.Any(v => v.MaterielCode == item.MaterielCode) && x.LocationCode == item.LocationName) .Includes(x => x.Details).ToList(); if (stockInfos.Count() == 0 && nowStockInfos.Count() > 0) { stockInfos.AddRange(nowStockInfos); } } else { //stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse); List nowStockInfos = Db.Queryable() .Where(x => x.PalletCode == PalletCode && x.Details.Any(v => v.MaterielCode == item.MaterielCode)) .Includes(x => x.Details).ToList(); if (stockInfos.Count() == 0 && nowStockInfos.Count() > 0) { stockInfos.AddRange(nowStockInfos); } } } if (stockInfos.Count() == 0) { continue; } List autoAssignStocks = new List(); List newResidueQuantitys = new List(); if (warehouse.Any(x => x.WarehouseCode.Contains("DW")) || warehouse.Any(x => x.WarehouseCode.Contains("YS"))) { autoAssignStocks = _stockService.StockInfoService.CPGetOutboundStocks(stockInfos, dt_OutboundOrderDetails, out List residueQuantitys); newResidueQuantitys.AddRange(residueQuantitys); outStocks.AddRange(autoAssignStocks); } foreach (var residueQuantity in newResidueQuantitys) { foreach (var item in dt_OutboundOrderDetails) { if (residueQuantity.MaterielCode == item.MaterielCode) { decimal originalNeedQuantity = item.OrderQuantity; decimal needQuantity = originalNeedQuantity - item.LockQuantity; item.LockQuantity += needQuantity - residueQuantity.NewNeendQuantity; decimal assignQuantity = needQuantity - residueQuantity.NewNeendQuantity; List details = dt_OutboundOrderDetails.Where(x => x.MaterielCode == item.MaterielCode).ToList(); for (int i = 0; i < details.Count; i++) { decimal orderQuantity = details[i].OrderQuantity; for (int j = 0; j < autoAssignStocks.Count; j++) { decimal detailAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);//出库订单明细已分配数量 decimal palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//出库详情已分配数量 if (string.IsNullOrEmpty(item.BatchNo)) { palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//出库详情已分配数量 } decimal palletOutboundQuantity = 0; if (warehouse.Any(x => x.WarehouseCode.Contains("DW")) || warehouse.Any(x => x.WarehouseCode.Contains("YS"))) { palletOutboundQuantity = autoAssignStocks[j].Details.Where(x => x.MaterielCode == item.MaterielCode).Sum(x => x.OutboundQuantity); } if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息 { decimal orderDetailNeedQuantity = details[i].OrderQuantity - detailAssignQuantity; if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Where(x => x.MaterielCode == details[i].MaterielCode).Sum(v => v.OutboundQuantity) - palletAssignQuantity) { //details[i].LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity; Dt_StockInfoDetail dt_StockInfoDetail = new Dt_StockInfoDetail(); foreach (var autoStockDestail in autoAssignStocks[j].Details) { if (autoStockDestail.MaterielCode == details[i].MaterielCode) { dt_StockInfoDetail = autoStockDestail; } } Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], dt_StockInfoDetail.OutboundQuantity); outStockLockInfos.Add(outStockLockInfo); details[i].LockQuantity += outStockLockInfo.AssignQuantity; break; } else { Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], details[i].OrderQuantity - details[i].LockQuantity); outStockLockInfos.Add(outStockLockInfo); details[i].LockQuantity = details[i].OrderQuantity; break; } } } } locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(autoAssignStocks.Select(x => x.LocationCode).ToList())); } } } newResidueQuantitys = new List(); } return (outStocks, dt_OutboundOrderDetails, outStockLockInfos, locationInfos); } public WebResponseContent CPLockOutboundStockDataUpdate(List stockInfos, List outboundOrderDetails, List outStockLockInfos, List locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List? tasks = null) { try { _stockService.StockInfoService.Repository.UpdateData(stockInfos); List stockInfoDetails = new List(); foreach (var item in stockInfos) { stockInfoDetails.AddRange(item.Details); } _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails); BaseDal.UpdateData(outboundOrderDetails); List addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList(); if (addOutStockLockInfos != null && addOutStockLockInfos.Any()) { if (tasks != null) { addOutStockLockInfos.ForEach(x => { x.TaskNum = tasks.FirstOrDefault(v => v.PalletCode == x.PalletCode)?.TaskNum; }); } _outStockLockInfoService.Repository.AddData(addOutStockLockInfos); } List updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList(); if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any()) { _outStockLockInfoService.Repository.UpdateData(updateOutStockLockInfos); } _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); } } } }