wangxinhui
2025-12-29 d0cc37c3c11859cc55f30624692dca55be2b8a32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_IBasicRepository;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_StockService
{
    public partial class ProStockInfoService : ServiceBase<Dt_ProStockInfo, IProStockInfoRepository>, IProStockInfoService
    {
        public IProStockInfoRepository Repository => BaseDal;
        private readonly IStockRepository _stockRepository;
        private readonly IBasicRepository _basicRepository;
        public ProStockInfoService(IProStockInfoRepository BaseDal,IStockRepository stockRepository, IBasicRepository basicRepository) : base(BaseDal)
        {
            _stockRepository = stockRepository;
            _basicRepository = basicRepository;
        }
        public List<Dt_ProStockInfo> GetUseableStocks(string materielCode, int warehoseId)
        {
            List<string> locationCodes = _basicRepository.LocationInfoRepository.GetCanOutLocationCodes(warehoseId);
 
            return BaseDal.GetStockInfos(materielCode, locationCodes);
        }
        public List<Dt_ProStockInfo> GetOutboundStocks(List<Dt_ProStockInfo> stockInfos, decimal needQuantity)
        {
            List<Dt_ProStockInfo> assignOutStocks = new List<Dt_ProStockInfo>();
 
            decimal stockTotalQuantity = stockInfos.Select(x => x.proStockInfoDetails.Sum(v => v.StockQty-v.OutboundQuantity)).Sum(x => x);
 
            if (stockTotalQuantity >= needQuantity)//库存够
            {
                int index = 0;
                while (needQuantity > 0)
                {
                    Dt_ProStockInfo stockInfo = stockInfos[index];
                    decimal useableStockQuantity = stockInfo.proStockInfoDetails
                        .Sum(x => x.StockQty - x.OutboundQuantity);
                    if (useableStockQuantity < needQuantity && useableStockQuantity > 0)
                    {
                        stockInfo.proStockInfoDetails.ForEach(x => x.OutboundQuantity = x.StockQty);
                        needQuantity -= useableStockQuantity;
                    }
                    else
                    {
                        stockInfo.proStockInfoDetails.ForEach(x =>
                        {
                            if (x.StockQty - x.OutboundQuantity >= needQuantity)
                            {
                                x.OutboundQuantity += needQuantity;
                                needQuantity = 0;
                            }
                            else
                            {
                                needQuantity -= (x.StockQty - x.OutboundQuantity);
                                x.OutboundQuantity = x.StockQty;
                            }
                        });
                    }
                    assignOutStocks.Add(stockInfo);
                    index++;
                }
            }
            return assignOutStocks;
        }
    }
}