|  |  |  | 
|---|
|  |  |  | using System.Linq; | 
|---|
|  |  |  | using System.Text; | 
|---|
|  |  |  | using System.Threading.Tasks; | 
|---|
|  |  |  | using SqlSugar.Extensions; | 
|---|
|  |  |  | using WIDESEA_Common.LocationEnum; | 
|---|
|  |  |  | using WIDESEA_Common.OrderEnum; | 
|---|
|  |  |  | using WIDESEA_Core; | 
|---|
|  |  |  | using WIDESEA_Core.BaseServices; | 
|---|
|  |  |  | using WIDESEA_DTO.Stock; | 
|---|
|  |  |  | using WIDESEA_IBasicRepository; | 
|---|
|  |  |  | using WIDESEA_IBasicService; | 
|---|
|  |  |  | using WIDESEA_IOutboundRepository; | 
|---|
|  |  |  | 
|---|
|  |  |  | _basicService = basicService; | 
|---|
|  |  |  | _recordService = recordService; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public (List<Dt_StockInfo>, Dt_MesPPCutOutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(Dt_MesPPCutOutboundOrderDetail mesPPOutboundOrderDetails, List<PPStockSelectViewDTO> stockSelectViews) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | #region | 
|---|
|  |  |  | //Dt_MesPPCutOutboundOrder OutboundOrder = BaseDal.Db.Queryable<Dt_MesPPCutOutboundOrder>().Where(x => x.Id == mesPPOutboundOrderDetails.OrderId).First(); | 
|---|
|  |  |  | //List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>(); | 
|---|
|  |  |  | //List<Dt_MesPPCutOutboundOrderDetail> groupDetails = mesPPOutboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.BatchNo }).Select(x => new Dt_MesPPCutOutboundOrderDetail { OrderQuantity = x.Sum(v => v.OrderQuantity) - x.Sum(v => v.LockQuantity), MaterielCode = x.Key.MaterielCode, BatchNo = x.Key.BatchNo, }).ToList(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>(); | 
|---|
|  |  |  | //List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>(); | 
|---|
|  |  |  | //foreach (var item in groupDetails) | 
|---|
|  |  |  | //{ | 
|---|
|  |  |  | //    float originalNeedQuantity = item.OrderQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    float needQuantity = originalNeedQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, OutboundOrder.WarehouseId); | 
|---|
|  |  |  | //    if (!stockInfos.Any()) | 
|---|
|  |  |  | //    { | 
|---|
|  |  |  | //        throw new Exception($"æªæ¾å°å¯åé
åºå"); | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | //    List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out float residueQuantity); | 
|---|
|  |  |  | //    item.LockQuantity += needQuantity - residueQuantity; | 
|---|
|  |  |  | //    outStocks.AddRange(autoAssignStocks); | 
|---|
|  |  |  | //    float assignQuantity = needQuantity - residueQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    List<Dt_MesPPCutOutboundOrderDetail> details = mesPPOutboundOrderDetails.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode).ToList(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    for (int i = 0; i < details.Count; i++) | 
|---|
|  |  |  | //    { | 
|---|
|  |  |  | //        float orderQuantity = details[i].OrderQuantity; | 
|---|
|  |  |  | //        for (int j = 0; j < autoAssignStocks.Count; j++) | 
|---|
|  |  |  | //        { | 
|---|
|  |  |  | //            float detailAssignQuantity = outStockLockInfos.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode && x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);//åºåºè®¢åæç»å·²åé
æ°é | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //            float palletAssignQuantity = outStockLockInfos.Where(x => x.BatchNo == item.BatchNo && 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);//åºåºè¯¦æ
å·²åé
æ°é | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            float palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity); | 
|---|
|  |  |  | //            if (palletAssignQuantity < palletOutboundQuantity)//妿åºåºè¯¦æ
å·²åé
æ°éå°äºæçå·²åé
æ°éï¼åå¯ä»¥ç»§ç»æ·»å è¯¥æçåºåºä¿¡æ¯ | 
|---|
|  |  |  | //            { | 
|---|
|  |  |  | //                float orderDetailNeedQuantity = details[i].OrderQuantity - detailAssignQuantity; | 
|---|
|  |  |  | //                if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity) | 
|---|
|  |  |  | //                { | 
|---|
|  |  |  | //                    details[i].LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity; | 
|---|
|  |  |  | //                    Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(OutboundOrder, details[i], autoAssignStocks[j], autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity); | 
|---|
|  |  |  | //                    outStockLockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //                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(outStocks.Select(x => x.LocationCode).ToList())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //} | 
|---|
|  |  |  | #endregion | 
|---|
|  |  |  | (bool, string) checkResult = CheckSelectStockDeital(mesPPOutboundOrderDetails, stockSelectViews); | 
|---|
|  |  |  | if (!checkResult.Item1) throw new Exception(checkResult.Item2); | 
|---|
|  |  |  | Dt_MesPPCutOutboundOrder OutboundOrder = BaseDal.Db.Queryable<Dt_MesPPCutOutboundOrder>().Where(x => x.Id == mesPPOutboundOrderDetails.OrderId).First(); | 
|---|
|  |  |  | float originalNeedQuantity = mesPPOutboundOrderDetails.OrderQuantity - mesPPOutboundOrderDetails.LockQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float needQuantity = originalNeedQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Dt_StockInfo> outStocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(stockSelectViews.Select(x => x.PalletCode).ToList()); | 
|---|
|  |  |  | float assignQuantity = 0; | 
|---|
|  |  |  | outStocks.ForEach(x => | 
|---|
|  |  |  | { | 
|---|
|  |  |  | x.Details.ForEach(v => | 
|---|
|  |  |  | { | 
|---|
|  |  |  | assignQuantity += v.StockQuantity - v.OutboundQuantity; | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | mesPPOutboundOrderDetails.LockQuantity += assignQuantity; | 
|---|
|  |  |  | outStocks.ForEach(x => | 
|---|
|  |  |  | { | 
|---|
|  |  |  | x.Details.ForEach(v => | 
|---|
|  |  |  | { | 
|---|
|  |  |  | v.OutboundQuantity = v.StockQuantity; | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | needQuantity -= assignQuantity; | 
|---|
|  |  |  | if (mesPPOutboundOrderDetails.OrderQuantity > mesPPOutboundOrderDetails.LockQuantity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(mesPPOutboundOrderDetails.MaterielCode, mesPPOutboundOrderDetails.BatchNo, OutboundOrder.WarehouseId); | 
|---|
|  |  |  | stockInfos = stockInfos.Where(x => !stockSelectViews.Select(v => v.PalletCode).Contains(x.PalletCode)).ToList(); | 
|---|
|  |  |  | List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, mesPPOutboundOrderDetails.MaterielCode, needQuantity, out float residueQuantity); | 
|---|
|  |  |  | mesPPOutboundOrderDetails.LockQuantity += needQuantity - residueQuantity; | 
|---|
|  |  |  | outStocks.AddRange(autoAssignStocks); | 
|---|
|  |  |  | mesPPOutboundOrderDetails.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt(); | 
|---|
|  |  |  | if (residueQuantity > 0) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | mesPPOutboundOrderDetails.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Dt_OutStockLockInfo> outStockLockInfos = _outStockLockInfoService.GetOutStockLockInfos(OutboundOrder, mesPPOutboundOrderDetails, outStocks); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Dt_LocationInfo> locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()); | 
|---|
|  |  |  | return (outStocks, mesPPOutboundOrderDetails, outStockLockInfos, locationInfos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private (bool, string) CheckSelectStockDeital(Dt_MesPPCutOutboundOrderDetail outboundOrderDetail, List<PPStockSelectViewDTO> 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, "æå"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// åºåºåºååé
åï¼æ´æ°æ°æ®åºæ°æ® | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <param name="stockInfos"></param> | 
|---|
|  |  |  | /// <param name="outboundOrderDetails"></param> | 
|---|
|  |  |  | /// <param name="outStockLockInfos"></param> | 
|---|
|  |  |  | /// <param name="locationInfos"></param> | 
|---|
|  |  |  | /// <param name="locationStatus"></param> | 
|---|
|  |  |  | /// <param name="tasks"></param> | 
|---|
|  |  |  | /// <returns></returns> | 
|---|
|  |  |  | public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_MesPPCutOutboundOrderDetail> outboundOrderDetails, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | try | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _stockService.StockInfoService.Repository.UpdateData(stockInfos); | 
|---|
|  |  |  | List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>(); | 
|---|
|  |  |  | foreach (var item in stockInfos) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | stockInfoDetails.AddRange(item.Details); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails); | 
|---|
|  |  |  | BaseDal.UpdateData(outboundOrderDetails); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Dt_OutStockLockInfo> 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<Dt_OutStockLockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList(); | 
|---|
|  |  |  | if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _outStockLockInfoService.Repository.UpdateData(updateOutStockLockInfos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, locationStatus, LocationChangeType.OutboundAssignLocation, "", 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|