|  |  |  | 
|---|
|  |  |  | using WIDESEA_OutboundRepository; | 
|---|
|  |  |  | using WIDESEA_Common.APIEnum; | 
|---|
|  |  |  | using WIDESEA_Common.OrderEnum; | 
|---|
|  |  |  | using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; | 
|---|
|  |  |  | using System.Reflection.Metadata; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | namespace WIDESEA_OutboundService | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | Dt_MesRworkOutboundOrder assignOutOrder= new Dt_MesRworkOutboundOrder(); | 
|---|
|  |  |  | List<Dt_OutProStockInfo> outProStockInfos=new List<Dt_OutProStockInfo>(); | 
|---|
|  |  |  | List<Dt_LocationInfo> locationInfos=new List<Dt_LocationInfo>(); | 
|---|
|  |  |  | float originalNeedQuantity = mesRworkOutboundOrder.RequiredQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float needQuantity = originalNeedQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float needQty = mesRworkOutboundOrder.RequiredQuantity; | 
|---|
|  |  |  | //æ¥æ¾å¯ç¨åºå | 
|---|
|  |  |  | List<Dt_ProStockInfo> stockInfoss = _stockService.ProStockInfoService.GetUseableStocks(mesRworkOutboundOrder); | 
|---|
|  |  |  | if (!stockInfoss.Any()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("æªæ¾å°å¯åé
åºå"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Dt_ProStockInfo> autoAssignStocks = _stockService.ProStockInfoService.GetOutboundStocks(stockInfoss,mesRworkOutboundOrder, needQuantity,out float residueQuantity); | 
|---|
|  |  |  | mesRworkOutboundOrder.LockQuantity += needQuantity - residueQuantity; | 
|---|
|  |  |  | autoAssignStocks.OrderBy(x => x.proStockInfoDetails.FirstOrDefault()?.StockPcsQty).ToList(); | 
|---|
|  |  |  | proStockInfos.AddRange(autoAssignStocks); | 
|---|
|  |  |  | float assignQuantity = needQuantity - residueQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //è·ååºåºåºå | 
|---|
|  |  |  | List<Dt_ProStockInfo> assignOutStocks = _stockService.ProStockInfoService.GetOutboundStocks(stockInfoss, mesRworkOutboundOrder, needQty, out float residueQuantity); | 
|---|
|  |  |  | mesRworkOutboundOrder.LockQuantity += needQty - residueQuantity; | 
|---|
|  |  |  | if (mesRworkOutboundOrder.RequiredQuantity > mesRworkOutboundOrder.LockQuantity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception($"产åç¼ç {mesRworkOutboundOrder.ProductCode},çæ¬{mesRworkOutboundOrder.ProductVersion}å¯åé
æ°éä¸è¶³,å¯ç¨æ°é{mesRworkOutboundOrder.LockQuantity}"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | proStockInfos.AddRange(assignOutStocks); | 
|---|
|  |  |  | //åé
åºåºçPCSæ°é | 
|---|
|  |  |  | float assignQuantity = needQty; | 
|---|
|  |  |  | //订åæç»çåºåºPCSæ°é | 
|---|
|  |  |  | float orderQuantity = mesRworkOutboundOrder.RequiredQuantity; | 
|---|
|  |  |  | bool isCanDate = string.IsNullOrEmpty(mesRworkOutboundOrder.DateCode); | 
|---|
|  |  |  | for (int j = 0; j < autoAssignStocks.Count; j++) | 
|---|
|  |  |  | for (int j = 0; j < assignOutStocks.Count; j++) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //åºåºè®¢åæç»å·²åé
æ°é | 
|---|
|  |  |  | float detailAssignQuantity = outProStockInfos | 
|---|
|  |  |  | .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder | 
|---|
|  |  |  | && x.PCode == mesRworkOutboundOrder.ProductCode | 
|---|
|  |  |  | && x.PVer == mesRworkOutboundOrder.ProductVersion | 
|---|
|  |  |  | && (isCanDate ? isCanDate : x.DateCode == mesRworkOutboundOrder.DateCode)) | 
|---|
|  |  |  | .Sum(x => x.AssignQuantity); | 
|---|
|  |  |  | float detailAssignQuantity = outProStockInfos.Where(x => x.OrderDetailId == mesRworkOutboundOrder.Id).Sum(x => x.AssignQuantity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //åºåºè¯¦æ
å·²åé
æ°é | 
|---|
|  |  |  | float palletAssignQuantity = outProStockInfos | 
|---|
|  |  |  | .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder | 
|---|
|  |  |  | && x.PCode == mesRworkOutboundOrder.ProductCode | 
|---|
|  |  |  | && x.PVer == mesRworkOutboundOrder.ProductVersion | 
|---|
|  |  |  | && x.PalletCode == autoAssignStocks[j].PalletCode | 
|---|
|  |  |  | && (isCanDate ? isCanDate : x.DateCode == mesRworkOutboundOrder.DateCode)) | 
|---|
|  |  |  | .Sum(x => x.AssignQuantity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float palletOutboundQuantity = autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity); | 
|---|
|  |  |  | if (palletAssignQuantity < palletOutboundQuantity)//妿åºåºè¯¦æ
å·²åé
æ°éå°äºæçå·²åé
æ°éï¼åå¯ä»¥ç»§ç»æ·»å è¯¥æçåºåºä¿¡æ¯ | 
|---|
|  |  |  | //æå©æ°é | 
|---|
|  |  |  | float orderDetailNeedQuantity = mesRworkOutboundOrder.RequiredQuantity - detailAssignQuantity; | 
|---|
|  |  |  | //çæåºåºè¯¦æ | 
|---|
|  |  |  | if (orderDetailNeedQuantity > assignOutStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | float orderDetailNeedQuantity = mesRworkOutboundOrder.RequiredQuantity - detailAssignQuantity; | 
|---|
|  |  |  | if (orderDetailNeedQuantity > autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | mesRworkOutboundOrder.LockQuantity += autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity; | 
|---|
|  |  |  | Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity); | 
|---|
|  |  |  | outProStockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], mesRworkOutboundOrder.RequiredQuantity-mesRworkOutboundOrder.LockQuantity); | 
|---|
|  |  |  | outProStockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | mesRworkOutboundOrder.LockQuantity = mesRworkOutboundOrder.RequiredQuantity; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | mesRworkOutboundOrder.LockQuantity += assignOutStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity); | 
|---|
|  |  |  | Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, assignOutStocks[j], assignOutStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity)); | 
|---|
|  |  |  | outProStockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, assignOutStocks[j], mesRworkOutboundOrder.RequiredQuantity - detailAssignQuantity); | 
|---|
|  |  |  | outProStockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | mesRworkOutboundOrder.LockQuantity = mesRworkOutboundOrder.RequiredQuantity; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(proStockInfos.Select(x => x.LocationCode).ToList())); | 
|---|
|  |  |  | List<string> locationArr = proStockInfos.Select(x => x.LocationCode).ToList(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(locationArr)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return (proStockInfos, mesRworkOutboundOrder, outProStockInfos, locationInfos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | return mesProductOutBound; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //åºåæ¿æ°æ®è½¬æ¢ | 
|---|
|  |  |  | public MesProductOutBound MesProOutBound(Dt_MesRworkOutboundOrder mesRworkOutboundOrder, List<Dt_ProStockInfoDetail> proStockInfoDetails) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //è·åä»åºä¿¡æ¯ | 
|---|
|  |  |  | Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == mesRworkOutboundOrder.WarehouseId); | 
|---|
|  |  |  | List<MesInventoryInfo> inventoryInfos = new List<MesInventoryInfo>(); | 
|---|
|  |  |  | foreach (var item in proStockInfoDetails.Where(x=>x.OutboundQuantity>0)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | int setinterval = (int)item.StockPcsQty / (int)item.SETQty; | 
|---|
|  |  |  | if (setinterval == 0) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception($"{item.BagNo}è·åSETæ°é转æ¢å¤±è´¥"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | item.OutSETQty = (int)item.OutboundQuantity / setinterval; | 
|---|
|  |  |  | if (item.OutSETQty == 0) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception($"{item.BagNo}è·åSETæ°é转æ¢å¤±è´¥"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MesInventoryInfo mesInventoryInfo = new MesInventoryInfo() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Warhouseno = warehouse.WarehouseCode, | 
|---|
|  |  |  | InternalPackageNumber = item.BagNo, | 
|---|
|  |  |  | SetCount = (int)item.OutSETQty, | 
|---|
|  |  |  | EligiblePcsCount = (int)item.OutboundQuantity | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | inventoryInfos.Add(mesInventoryInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MesProductOutBound mesProductOutBound = new MesProductOutBound() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | TaskNo = mesRworkOutboundOrder.TaskNo, | 
|---|
|  |  |  | ProductCode = mesRworkOutboundOrder.ProductCode, | 
|---|
|  |  |  | ProductVersion = mesRworkOutboundOrder.ProductVersion, | 
|---|
|  |  |  | DateCode = mesRworkOutboundOrder.DateCode, | 
|---|
|  |  |  | SaleOrder = mesRworkOutboundOrder.SaleOrder, | 
|---|
|  |  |  | InventoryInfo = inventoryInfos | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | return mesProductOutBound; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// æååºåæ¿åæ¥æ¥å£ | 
|---|
|  |  |  | /// </summary> | 
|---|