From c4e1a656954799267cbd61d3de3a040e8dc8e46a Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期二, 11 十一月 2025 08:48:42 +0800
Subject: [PATCH] 提交出库
---
项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs | 302 ++++++++++++++++++++++++++++++++++++++-----------
1 files changed, 231 insertions(+), 71 deletions(-)
diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
index 3e6904c..fdf2e9c 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
@@ -1,4 +1,5 @@
锘縰sing WIDESEA_Common.LocationEnum;
+using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
@@ -21,9 +22,10 @@
private readonly ILocationInfoService _locationInfoService;
private readonly IBasicService _basicService;
private readonly IRecordService _recordService;
+ private readonly IOutboundOrderService _outboundOrderService;
private readonly ILocationStatusChangeRecordService _locationStatusChangeRecordService;
- public OutboundOrderDetailService(IRepository<Dt_OutboundOrderDetail> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, ILocationInfoService locationInfoService, ILocationStatusChangeRecordService locationStatusChangeRecordService) : base(BaseDal)
+ public OutboundOrderDetailService(IRepository<Dt_OutboundOrderDetail> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, ILocationInfoService locationInfoService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IOutboundOrderService outboundOrderService) : base(BaseDal)
{
_unitOfWorkManage = unitOfWorkManage;
_stockService = stockService;
@@ -32,90 +34,259 @@
_recordService = recordService;
_locationInfoService = locationInfoService;
_locationStatusChangeRecordService = locationStatusChangeRecordService;
+ _outboundOrderService = outboundOrderService;
}
+
/// <summary>
- ///
+ /// 鍒嗛厤鍑哄簱搴撳瓨 鎸夊厛杩涘厛鍑哄師鍒欏垎閰�
/// </summary>
- /// <param name="outboundOrderDetails"></param>
- /// <returns></returns>
public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails)
{
if (!outboundOrderDetails.Any())
{
- throw new Exception($"鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+ throw new Exception("鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
}
if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1)
{
- throw new Exception($"璇峰嬁鍚屾椂鎿嶄綔澶氫釜鍗曟嵁鏄庣粏");
+ throw new Exception("璇峰嬁鍚屾椂鎿嶄綔澶氫釜鍗曟嵁鏄庣粏");
}
- Dt_OutboundOrder outboundOrder = Repository.Db.Queryable<Dt_OutboundOrder>().Where(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId).First();
- List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
- List<Dt_OutboundOrderDetail> groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.BatchNo }).Select(x => new Dt_OutboundOrderDetail { OrderQuantity = x.Sum(v => v.OrderQuantity) - x.Sum(v => v.LockQuantity), MaterielCode = x.Key.MaterielCode, BatchNo = x.Key.BatchNo }).ToList();
+ var outboundOrder = _outboundOrderService.Db.Queryable<Dt_OutboundOrder>()
+ .First(x => x.Id == outboundOrderDetails.First().OrderId);
+
+ List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
+
+ // 鎸夌墿鏂欏拰鎵规鍒嗙粍澶勭悊
+ var groupDetails = outboundOrderDetails
+ .GroupBy(x => new { x.MaterielCode, x.BatchNo })
+ .Select(x => new
+ {
+ MaterielCode = x.Key.MaterielCode,
+ BatchNo = x.Key.BatchNo,
+ Details = x.ToList(),
+ TotalNeedQuantity = x.Sum(v => v.OrderQuantity - v.OverOutQuantity - v.LockQuantity)
+ })
+ .Where(x => x.TotalNeedQuantity > 0)
+ .ToList();
+
foreach (var item in groupDetails)
{
- var originalNeedQuantity = item.OrderQuantity;
+ var needQuantity = item.TotalNeedQuantity;
- var needQuantity = originalNeedQuantity;
-
+ // 鑾峰彇鍙敤搴撳瓨锛堟寜鍏堣繘鍏堝嚭鎺掑簭锛�
List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo);
if (!stockInfos.Any())
{
- throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
+ throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]鏈壘鍒板彲鍒嗛厤搴撳瓨");
}
- List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
- item.LockQuantity += needQuantity - residueQuantity;
- outStocks.AddRange(autoAssignStocks);
- var assignQuantity = needQuantity - residueQuantity;
+ // 鍒嗛厤搴撳瓨锛堟寜鍏堣繘鍏堝嚭锛�
+ List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(
+ stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
- List<Dt_OutboundOrderDetail> details = outboundOrderDetails.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode).ToList();
-
- for (int i = 0; i < details.Count; i++)
+ if (residueQuantity > 0)
{
- var orderQuantity = details[i].OrderQuantity;
- for (int j = 0; j < autoAssignStocks.Count; j++)
- {
- var 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);//鍑哄簱璁㈠崟鏄庣粏宸插垎閰嶆暟閲�
-
- var 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);//鍑哄簱璇︽儏宸插垎閰嶆暟閲�
- }
- var palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity);
- if (palletAssignQuantity < palletOutboundQuantity)//濡傛灉鍑哄簱璇︽儏宸插垎閰嶆暟閲忓皬浜庢墭鐩樺凡鍒嗛厤鏁伴噺锛屽垯鍙互缁х画娣诲姞璇ユ墭鐩樺嚭搴撲俊鎭�
- {
- var 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;
- }
-
- }
- }
+ throw new Exception($"鐗╂枡[{item.MaterielCode}]搴撳瓨涓嶈冻锛岄渶瑕亄needQuantity}锛屽彲鐢▄needQuantity - residueQuantity}");
}
+ outStocks.AddRange(autoAssignStocks);
- locationInfos.AddRange(_locationInfoService.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()));
+ // 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲忓埌鍚勪釜鏄庣粏
+ DistributeLockQuantityByFIFO(item.Details, autoAssignStocks, outStockLockInfos, outboundOrder);
}
+
+ locationInfos.AddRange(_locationInfoService.GetLocationInfos(outStocks.Select(x => x.LocationCode).Distinct().ToList()));
return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos);
}
+ /// <summary>
+ /// 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲忓埌鍚勪釜鏄庣粏
+ /// </summary>
+ private void DistributeLockQuantityByFIFO(
+ List<Dt_OutboundOrderDetail> details,
+ List<Dt_StockInfo> assignStocks,
+ List<Dt_OutStockLockInfo> outStockLockInfos,
+ Dt_OutboundOrder outboundOrder)
+ {
+ // 鎸夊厛杩涘厛鍑烘帓搴忓嚭搴撳崟鏄庣粏锛堝亣璁惧厛鍒涘缓鐨勬槑缁嗛渶瑕佷紭鍏堟弧瓒筹級
+ var sortedDetails = details
+ .OrderBy(x => x.Id) // 鎸塈D鎺掑簭锛屽亣璁惧厛鍒涘缓鐨処D灏�
+ .ToList();
+
+ // 鎸夊厛杩涘厛鍑烘帓搴忓簱瀛橈紙鐢熶骇鏃ユ湡鏈�鏃╃殑浼樺厛锛�
+ var sortedStockDetails = assignStocks
+ .SelectMany(x => x.Details)
+ .Where(x => details.Any(d => d.MaterielCode == x.MaterielCode))
+ .OrderBy(x => x.ProductionDate)
+ .ToList();
+
+ // 涓烘瘡涓簱瀛樻槑缁嗗垱寤哄垎閰嶈褰�
+ foreach (var stockDetail in sortedStockDetails)
+ {
+ var stockInfo = assignStocks.First(x => x.Id == stockDetail.StockId);
+ var allocatedQuantity = stockDetail.OutboundQuantity; // 杩欎釜搴撳瓨鏄庣粏鍒嗛厤鐨勬暟閲�
+
+ if (allocatedQuantity <= 0) continue;
+
+ // 鎸夐『搴忓垎閰嶇粰鍚勪釜鍑哄簱鍗曟槑缁�
+ decimal remainingAllocate = allocatedQuantity;
+
+ foreach (var detail in sortedDetails)
+ {
+ if (remainingAllocate <= 0) break;
+
+ // 璁$畻杩欎釜鏄庣粏杩橀渶瑕佸垎閰嶇殑鏁伴噺
+ var alreadyAssigned = outStockLockInfos
+ .Where(x => x.OrderDetailId == detail.Id && x.StockId == stockInfo.Id)
+ .Sum(x => x.AssignQuantity);
+
+ var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.LockQuantity - alreadyAssigned;
+
+ if (detailNeed <= 0) continue;
+
+ // 鍒嗛厤鏁伴噺
+ var assignQuantity = Math.Min(remainingAllocate, detailNeed);
+
+ // 鍒涘缓鍑哄簱閿佸畾淇℃伅
+ var lockInfo = _outStockLockInfoService.GetOutStockLockInfo(
+ outboundOrder, detail, stockInfo, assignQuantity, stockDetail.Barcode);
+ outStockLockInfos.Add(lockInfo);
+
+ // 鏇存柊鏄庣粏鐨勯攣瀹氭暟閲�
+ detail.LockQuantity += assignQuantity;
+ remainingAllocate -= assignQuantity;
+ }
+
+ // 濡傛灉杩樻湁鍓╀綑鍒嗛厤鏁伴噺锛岃鏄庨�昏緫鏈夎
+ if (remainingAllocate > 0)
+ {
+ throw new Exception($"搴撳瓨鍒嗛厤閫昏緫閿欒锛屽墿浣欐湭鍒嗛厤鏁伴噺锛歿remainingAllocate}");
+ }
+ }
+ }
+
+
+/// <summary>
+ /// 鍒涘缓鍑哄簱閿佸畾淇℃伅
+ /// </summary>
+ private void CreateOutStockLockInfos(Dt_OutboundOrder outboundOrder,List<Dt_OutboundOrderDetail> details,
+ List<Dt_StockInfo> assignStocks,List<Dt_OutStockLockInfo> outStockLockInfos)
+ {
+ foreach (var stock in assignStocks)
+ {
+ // 鑾峰彇璇ュ簱瀛樹腑鐩稿叧鐗╂枡鐨勫彲鐢ㄦ潯鐮佷俊鎭�
+ var stockDetails = stock.Details
+ .Where(x => details.Any(d => d.MaterielCode == x.MaterielCode) &&
+ x.StockQuantity > x.OutboundQuantity)
+ .OrderBy(x => x.ProductionDate) // 鍏堣繘鍏堝嚭
+ .ToList();
+ if (!stockDetails.Any()) continue;
+
+ var stockAssignQuantity = stockDetails.Sum(x => x.OutboundQuantity);
+
+
+ // 鏌ユ壘杩欎釜搴撳瓨宸茬粡鍒嗛厤鐨勬暟閲�
+ var existingAssign = outStockLockInfos
+ .Where(x => x.StockId == stock.Id &&
+ details.Any(d => d.Id == x.OrderDetailId))
+ .Sum(x => x.AssignQuantity);
+
+ var availableAssign = stockAssignQuantity - existingAssign;
+
+ if (availableAssign <= 0) continue;
+
+ // 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶆潯鐮�
+ var barcodeAllocation = AllocateBarcodes(stockDetails, availableAssign);
+
+
+ // 鍒嗛厤缁欏悇涓槑缁�
+ foreach (var detail in details.Where(d => d.LockQuantity > 0))
+ {
+ var alreadyAssigned = outStockLockInfos
+ .Where(x => x.OrderDetailId == detail.Id && x.StockId == stock.Id)
+ .Sum(x => x.AssignQuantity);
+
+ var canAssign = Math.Min(detail.LockQuantity - alreadyAssigned, availableAssign);
+
+ if (canAssign > 0)
+ {
+ // 涓鸿繖涓垎閰嶇‘瀹氭潯鐮�
+ var (barcode, barcodeQuantity) = GetBarcodeForAllocation(barcodeAllocation, canAssign);
+
+ var lockInfo = _outStockLockInfoService.GetOutStockLockInfo(
+ outboundOrder, detail, stock, canAssign, barcode,null);
+ outStockLockInfos.Add(lockInfo);
+
+ availableAssign -= canAssign;
+
+ // 鏇存柊鏉$爜鍒嗛厤璁板綍
+ UpdateBarcodeAllocation(barcodeAllocation, barcode, barcodeQuantity);
+ }
+
+ if (availableAssign <= 0) break;
+ }
+ }
+ }
+
+ /// <summary>
+ /// 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶆潯鐮�
+ /// </summary>
+ private Dictionary<string, decimal> AllocateBarcodes(List<Dt_StockInfoDetail> stockDetails, decimal totalQuantity)
+ {
+ var allocation = new Dictionary<string, decimal>();
+ decimal remainingQuantity = totalQuantity;
+
+ foreach (var detail in stockDetails.OrderBy(x => x.ProductionDate))
+ {
+ if (remainingQuantity <= 0) break;
+
+ decimal available = detail.StockQuantity - detail.OutboundQuantity;
+ decimal allocate = Math.Min(available, remainingQuantity);
+
+ allocation[detail.Barcode] = allocate;
+ remainingQuantity -= allocate;
+ }
+
+ return allocation;
+ }
+
+ /// <summary>
+ /// 涓哄垎閰嶈幏鍙栧悎閫傜殑鏉$爜
+ /// </summary>
+ private (string barcode, decimal quantity) GetBarcodeForAllocation(Dictionary<string, decimal> barcodeAllocation, decimal requiredQuantity)
+ {
+ foreach (var (barcode, quantity) in barcodeAllocation)
+ {
+ if (quantity >= requiredQuantity)
+ {
+ return (barcode, requiredQuantity);
+ }
+ }
+
+ // 濡傛灉鍗曚釜鏉$爜鏁伴噺涓嶈冻锛屼娇鐢ㄧ涓�涓潯鐮�
+ var first = barcodeAllocation.First();
+ return (first.Key, Math.Min(first.Value, requiredQuantity));
+ }
+
+ /// <summary>
+ /// 鏇存柊鏉$爜鍒嗛厤璁板綍
+ /// </summary>
+ private void UpdateBarcodeAllocation(Dictionary<string, decimal> barcodeAllocation, string barcode, decimal usedQuantity)
+ {
+ if (barcodeAllocation.ContainsKey(barcode))
+ {
+ barcodeAllocation[barcode] -= usedQuantity;
+ if (barcodeAllocation[barcode] <= 0)
+ {
+ barcodeAllocation.Remove(barcode);
+ }
+ }
+ }
/// <summary>
/// 鍑哄簱搴撳瓨鍒嗛厤鍚庯紝鏇存柊鏁版嵁搴撴暟鎹�
/// </summary>
@@ -126,30 +297,19 @@
/// <param name="locationStatus"></param>
/// <param name="tasks"></param>
/// <returns></returns>
- public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutboundOrderDetail> outboundOrderDetails, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
+ public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutboundOrderDetail> outboundOrderDetails,
+ List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos,
+ LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
{
try
{
+ // 鏇存柊搴撳瓨鐘舵��
+ stockInfos.ForEach(x => x.StockStatus = (int)StockStatusEmun.鍑哄簱閿佸畾);
_stockService.StockInfoService.Repository.UpdateData(stockInfos);
- List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
- foreach (var item in stockInfos)
- {
- foreach (var detail in item.Details)
- {
- // 杩涜瀹夊叏杞崲
- if (detail.OutboundQuantity != null && decimal.TryParse(detail.OutboundQuantity.ToString(), out decimal outboundDecimal))
- {
- decimal outboundDecimal1 = Convert.ToDecimal(detail.OutboundQuantity);
- }
- else
- {
- detail.OutboundQuantity = 0; // 榛樿鍊兼垨璁板綍閿欒
- }
- }
- stockInfoDetails.AddRange(item.Details);
-
- }
- _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
+
+ // 鏇存柊搴撳瓨鏄庣粏
+ var stockDetails = stockInfos.SelectMany(x => x.Details).ToList();
+ _stockService.StockInfoDetailService.Repository.UpdateData(stockDetails);
BaseDal.UpdateData(outboundOrderDetails);
List<Dt_OutStockLockInfo> addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList();
--
Gitblit v1.9.3