wangxinhui
昨天 011ca316e6ec2ed93e31c45a9ebd9d3c66664871
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.MaterielEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.ERP;
using WIDESEA_IBasicRepository;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_OutboundService
{
    public class ProDeliveryOrderService : ServiceBase<Dt_ProDeliveryOrder, IProDeliveryOrderRepository>, IProDeliveryOrderService
    {
        public IProDeliveryOrderRepository Repository => BaseDal;
        private readonly IBasicRepository _basicRepository;
        private readonly IMapper _mapper;
        private readonly IProDeliveryOrderDetailRepository _proDeliveryOrderDetailRepository;
        public ProDeliveryOrderService(IProDeliveryOrderRepository BaseDal, IBasicRepository basicRepository, IMapper mapper, IProDeliveryOrderDetailRepository proDeliveryOrderDetailRepository) : base(BaseDal)
        {
            _basicRepository = basicRepository;
            _mapper = mapper;
            _proDeliveryOrderDetailRepository = proDeliveryOrderDetailRepository;
        }
        /// <summary>
        /// 接收ERP成品销售出库信息
        /// </summary>
        /// <returns></returns>
        public WebResponseContent ReceiveProDeliveryOrder(ERPProDeliveryDTO eRPProDeliveryDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (eRPProDeliveryDTO == null)
                {
                    return content.Error("销售出库信息不能为空");
                }
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == eRPProDeliveryDTO.WarehouseCode);
                if (warehouse == null)
                {
                    return content.Error($"仓库信息不存在{eRPProDeliveryDTO.WarehouseCode}");
                }
                //获取所有物料 
                List<Dt_MaterielInfo> materielInfos = _basicRepository.MaterielInfoRepository.QueryData(x => x.WarehouseId == warehouse.WarehouseId && x.MaterielInvOrgId == MaterielInvOrgEnum.新厂.ObjToInt());
 
                //获取所有客户
                List<Dt_CustomerInfo> customerInfos = _basicRepository.CustomerInfoRepository.QueryData();
 
                //判断是否存在物料
                DeliveryDetailItem? deliveryDetailItem = eRPProDeliveryDTO.DeliveryDetail.FirstOrDefault(x => !materielInfos.Select(x => x.MaterielCode).Contains(x.MaterialCode));
                if (deliveryDetailItem != null)
                {
                    return content.Error($"物料信息{deliveryDetailItem.MaterialCode}不存在");
                }
 
                //判断是否存在客户
                DeliveryDetailItem? deliveryDetailCustom = eRPProDeliveryDTO.DeliveryDetail.FirstOrDefault(x => !customerInfos.Select(x => x.CustomerCode).Contains(x.Customer));
 
                if (deliveryDetailCustom != null)
                {
                    return content.Error($"客户信息{deliveryDetailCustom.Customer}不存在");
                }
 
                //获取所有销售出库信息
                List<Dt_ProDeliveryOrder> proDeliveryOrdersOld = BaseDal.Db.Queryable<Dt_ProDeliveryOrder>().Includes(x => x.Details).ToList();
 
                if (eRPProDeliveryDTO.OperateType == 1)
                {
                    //判断重复插入
                    Dt_ProDeliveryOrder? proDeliveryOrderOld = proDeliveryOrdersOld.FirstOrDefault(x => x.DeliveryCode == eRPProDeliveryDTO.DeliveryCode);
                    if (proDeliveryOrderOld != null)
                    {
                        return content.Error($"销售出库单号{proDeliveryOrderOld.DeliveryCode}信息已存在");
                    }
                    List<Dt_ProDeliveryOrderDetail> proDeliveryOrderDetails = eRPProDeliveryDTO.DeliveryDetail.Select(x => _mapper.Map<Dt_ProDeliveryOrderDetail>(x)).ToList();
                    Dt_ProDeliveryOrder proDeliveryOrderAdd = _mapper.Map<Dt_ProDeliveryOrder>(eRPProDeliveryDTO);
                    proDeliveryOrderDetails.ForEach(x =>
                    {
                        Dt_MaterielInfo? materielInfo = materielInfos.FirstOrDefault(t => t.MaterielCode == x.MaterialCode);
                        x.MaterielName = materielInfo?.MaterielName;
                        x.Unit = materielInfo?.MaterielUnit;
                    });
                    proDeliveryOrderAdd.Details = proDeliveryOrderDetails;
                    proDeliveryOrderAdd.WarehouseId = warehouse.WarehouseId;
                    //新增
                    BaseDal.Db.InsertNav(proDeliveryOrderAdd).Include(x => x.Details).ExecuteCommand();
 
                }
                else if (eRPProDeliveryDTO.OperateType == 2)
                {
                    //判断是否存在
                    Dt_ProDeliveryOrder? proDeliveryOrderOld = proDeliveryOrdersOld.FirstOrDefault(x => x.DeliveryCode == eRPProDeliveryDTO.DeliveryCode);
                    if (proDeliveryOrderOld == null)
                    {
                        return content.Error($"销售出库单号{eRPProDeliveryDTO.DeliveryCode}信息不存在");
                    }
                    if (proDeliveryOrderOld.ProDeliveryStatus != OutOrderStatusEnum.未开始.ObjToInt())
                    {
                        return content.Error($"销售出库单号{proDeliveryOrderOld.DeliveryCode}状态为{(OutOrderStatusEnum)proDeliveryOrderOld.ProDeliveryStatus}");
                    }
                    List<Dt_ProDeliveryOrderDetail> proDeliveryOrderDetails = eRPProDeliveryDTO.DeliveryDetail.Select(x => _mapper.Map<Dt_ProDeliveryOrderDetail>(x)).ToList();
                    Dt_ProDeliveryOrder proDeliveryOrder = _mapper.Map<Dt_ProDeliveryOrder>(eRPProDeliveryDTO);
                    proDeliveryOrder.Id = proDeliveryOrderOld.Id;
                    proDeliveryOrderDetails.ForEach(x =>
                    {
                        Dt_MaterielInfo? materielInfo = materielInfos.FirstOrDefault(t => t.MaterielCode == x.MaterialCode);
                        x.MaterielName = materielInfo?.MaterielName;
                        x.Unit = materielInfo?.MaterielUnit;
                    });
                    proDeliveryOrder.Details = proDeliveryOrderDetails;
                    proDeliveryOrder.WarehouseId = warehouse.WarehouseId;
                    //更新
                    BaseDal.Db.UpdateNav(proDeliveryOrder).Include(x => x.Details).ExecuteCommand();
                }
                else if (eRPProDeliveryDTO.OperateType == 3)
                {
                    //判断是否存在
                    Dt_ProDeliveryOrder? proDeliveryOrderOld = proDeliveryOrdersOld.FirstOrDefault(x => x.DeliveryCode == eRPProDeliveryDTO.DeliveryCode);
                    if (proDeliveryOrderOld == null)
                    {
                        return content.Error($"销售出库单号{eRPProDeliveryDTO.DeliveryCode}信息不存在");
                    }
                    if (proDeliveryOrderOld.ProDeliveryStatus != OutOrderStatusEnum.未开始.ObjToInt())
                    {
                        return content.Error($"销售出库单号{proDeliveryOrderOld.DeliveryCode}状态为{(OutOrderStatusEnum)proDeliveryOrderOld.ProDeliveryStatus}");
                    }
                    //删除
                    BaseDal.Db.DeleteNav(proDeliveryOrderOld).Include(x => x.Details).ExecuteCommand();
                }
                else
                {
                    return content.Error("未找到操作类型");
                }
                //更新数据
                return content.OK("接收成功");
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// ERP成品销售出库分配库存
        /// </summary>
        public (List<Dt_ProStockInfo>, List<Dt_ProDeliveryOrder>, List<Dt_ProDeliveryOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignProStockOutbound(List<Dt_ProDeliveryOrder> proDeliveryOrders,List<Dt_ProDeliveryOrderDetail> deliveryOrderDetails)
        {
            List<Dt_ProStockInfo> outStocks = new List<Dt_ProStockInfo>();
 
            //出库详情
            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
            //货位存储
            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
 
            //foreach (var item in )
            //{
 
            //}
 
            //foreach (var item in deliveryOrderDetails)
            //{
            //    decimal needQuantity = item.ReqQuantity;
            //    //获取可用库存
            //    List<Dt_ProStockInfo> stockInfos = _stockService.ProStockInfoService.GetUseableStocks(item.MaterialCode, WarehouseEnum.LLDCP.ObjToInt()).Where(x => !outStocks.Select(x => x.PalletCode).Contains(x.PalletCode)).ToList();
            //    if (!stockInfos.Any())
            //    {
            //        continue;
            //    }
            //    //分配实际库存
            //    List<Dt_ProStockInfo> autoAssignStocks = _stockService.ProStockInfoService.GetOutboundStocks(stockInfos, needQuantity);
            //    //添加库存分配
            //    outStocks.AddRange(autoAssignStocks);
            //    //订单数量
            //    decimal orderQuantity = item.ReqQuantity;
            //    bool assignStop = true;
            //    while (assignStop)
            //    {
            //        //出库订单明细已分配数量
            //        decimal detailAssignQuantity = outStockLockInfos.Where(x => x.OrderDetailId == item.OutDetailId).Sum(x => x.AssignQuantity);
 
            //        decimal orderDetailNeedQuantity = item.ReqQuantity - detailAssignQuantity;
 
            //        decimal useStockLength = autoAssignStocks[0].proStockInfoDetails
            //            .Sum(x => x.StockQty);
            //        if (orderDetailNeedQuantity > useStockLength)
            //        {
 
            //            //生成详情
            //            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(item, autoAssignStocks[0], useStockLength);
            //            outStockLockInfos.Add(outStockLockInfo);
            //            item.AssignTotalUsage += useStockLength;
            //            autoAssignStocks.Remove(autoAssignStocks[0]);
            //        }
            //        else
            //        {
            //            //生成详情
            //            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(item, autoAssignStocks[0], orderDetailNeedQuantity);
            //            outStockLockInfos.Add(outStockLockInfo);
            //            item.AssignTotalUsage = orderQuantity;
            //            autoAssignStocks.Remove(autoAssignStocks[0]);
            //            assignStop = false;
            //        }
            //    }
            //    item.OutMESOrderStatus = OutOrderStatusEnum.出库中.ObjToInt();
            //    locationInfos.AddRange(_basicRepository.LocationInfoRepository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()));
            //}
 
            return (outStocks, proDeliveryOrders, deliveryOrderDetails, outStockLockInfos, locationInfos);
        }
    }
}