yanjinhui
4 天以前 f42e4c5c9f91d983ae2e87d3b6b600d1c8e54087
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
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
 
namespace WIDESEA_SquareCabinServices
{
    public partial class CabinOrderServices
    {
        /// <summary>
        /// 立库入库任务完成
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public WebResponseContent CompleteLKInOrder(Dt_CabinOrder cabinOrder, EdiOrderCallbackRequest request)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_CabinOrderDetail> cabinOrderDetailsUp = new List<Dt_CabinOrderDetail>();
                List<Dt_SupplyTask_Hty> supplyTask_Hties = new List<Dt_SupplyTask_Hty>();
                List<Dt_InventoryInfo> infosUp = new List<Dt_InventoryInfo>();
                List<Dt_InventoryInfo> infosAdd = new List<Dt_InventoryInfo>();
                List<Dt_Inventory_Batch> batchesUp = new List<Dt_Inventory_Batch>();
                List<Dt_Inventory_Batch> batchesAdd = new List<Dt_Inventory_Batch>();
                List<Dt_MaterielInfo> materielInfos = _materielInfoService.Repository.QueryData(x => cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
                List<Dt_InventoryInfo> inventoryInfos = _inventoryInfoService.Repository.QueryData(x => cabinOrder.Warehouse_no == x.WarehouseCode && cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
                List<Dt_Inventory_Batch> inventory_Batches = _inventory_BatchServices.Repository.QueryData(x => cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
                #region 盘盈入库单
                if (cabinOrder.Order_type == "5")
                {
                    foreach (var detail in request.details)
                    {
                        Dt_CabinOrderDetail? orderDetail = cabinOrder.Details.FirstOrDefault(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
                        if (orderDetail == null) throw new Exception($"未找到盘点单【{cabinOrder.Order_no}】物料编号【{detail.productCode}】物料批次【{detail.batchNo}】的明细信息");
                        decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0) ?? 0;
                        Dt_InventoryInfo inventoryInfo = inventoryInfos.Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo).First();
                        if (orderQty != inventoryInfo.SupplyQuantity)
                            throw new Exception($"盘盈入库数量不一致,上传数量【{orderQty}】,库存盘盈数量【{inventoryInfo.SupplyQuantity}】");
                        inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
                        inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
                        inventoryInfo.SupplyQuantity = 0;
                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                        infosUp.Add(inventoryInfo);
                        Dt_Inventory_Batch inventory_Batch = inventory_Batches.First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                        inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
                        inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
                        inventory_Batch.SupplyQuantity = 0;
                        batchesUp.Add(inventory_Batch);
                        orderDetail.OrderDetailStatus = "已完成";
                        cabinOrderDetailsUp.Add(orderDetail);
                        #region 添加盘盈入库任务
                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                        {
                            WarehouseCode = inventoryInfo.WarehouseCode,
                            OperateType = OperateTypeEnum.自动完成.ToString(),
                            InsertTime = DateTime.Now,
                            TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                            BatchNo = inventoryInfo.BatchNo,
                            MaterielName = inventoryInfo.MaterielName,
                            MaterielCode = inventoryInfo.MaterielCode,
                            MaterielSpec = inventoryInfo.MaterielSpec,
                            TaskType = TaskTypeEnum.ChenckIn.ObjToInt(),
                            CreateDate = DateTime.Now,
                            Creater = App.User.UserName,
                            LocationCode = inventoryInfo.LocationCode,
                            OrderNo = cabinOrder.Order_no,
                            StockQuantity = orderQty,
                            SupplyQuantity = 0,
                            Remark = "盘盈入库"
                        };
                        supplyTask_Hties.Add(supplyTask_Hty);
                        #endregion
                    }
                    cabinOrder.OdrderStatus = "已完成";
                    if (cabinOrder.Details.Where(x => !cabinOrderDetailsUp.Select(x => x.Id).Contains(x.Id)).Any())
                        cabinOrder.OdrderStatus = "开始";
                    _unitOfWorkManage.BeginTran();
                    _supplyTaskHtyService.AddData(supplyTask_Hties);
                    _inventoryInfoService.UpdateData(infosUp);
                    _inventory_BatchServices.UpdateData(batchesUp);
                    _cabinOrderDetailServices.UpdateData(cabinOrderDetailsUp);
                    BaseDal.UpdateData(cabinOrder);
                    _unitOfWorkManage.CommitTran();
                }
                #endregion
                else
                {
                    foreach (var detail in request.details)
                    {
                        Dt_CabinOrderDetail? orderDetail = cabinOrder.Details.FirstOrDefault(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
                        if (orderDetail == null) throw new Exception($"未找到盘点单【{cabinOrder.Order_no}】物料编号【{detail.productCode}】物料名称【{detail.batchNo}】的明细信息");
                        if (orderDetail.OrderDetailStatus == "已完成") continue;
                        decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0) ?? 0;
                        orderDetail.Order_Inqty += orderQty;
                        if (orderDetail.Order_Inqty > orderDetail.Order_qty) throw new Exception($"入库数量不可超出单据数量");
                        orderDetail.OrderDetailStatus = "开始";
                        cabinOrder.OdrderStatus = orderDetail.OrderDetailStatus;
                        if (orderDetail.Order_Inqty == orderDetail.Order_qty)
                        {
                            orderDetail.OrderDetailStatus = "已完成";
                            cabinOrder.Details.Remove(orderDetail);
                        }
                        cabinOrderDetailsUp.Add(orderDetail);
                        Dt_MaterielInfo? materielInfo = materielInfos.FirstOrDefault(x => orderDetail.Goods_no == x.MaterielCode);
                        if (materielInfo == null) throw new Exception($"未找到物料编码【{orderDetail.Goods_no}】的物料信息");
                        #region 库存
                        Dt_InventoryInfo? dt_InventoryInfo = inventoryInfos.FirstOrDefault(x => x.MaterielCode == orderDetail.Goods_no && x.BatchNo == orderDetail.Batch_num);
                        if (dt_InventoryInfo != null)
                        {
                            dt_InventoryInfo.StockQuantity += orderQty;
                            dt_InventoryInfo.AvailableQuantity += orderQty;
                            dt_InventoryInfo.InDate = DateTime.Now;
                            infosUp.Add(dt_InventoryInfo);
                        }
                        else
                        {
                            dt_InventoryInfo = new Dt_InventoryInfo
                            {
                                PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
                                WarehouseCode = "001",
                                LocationCode = "立库",
                                StockStatus = StockStatusEmun.入库完成.ObjToInt(),
                                MaterielCode = materielInfo.MaterielCode,
                                MaterielName = materielInfo.MaterielName,
                                MaterielSpec = materielInfo.MaterielSpec,
                                BatchNo = detail.batchNo,
                                StockQuantity = orderQty,
                                OutboundQuantity = 0,
                                SupplyQuantity = 0,
                                AvailableQuantity = orderQty,
                                InDate = DateTime.Now,
                                ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                ShelfLife = 0,
                                ValidityPeriod = orderDetail.Exp_date,
                                Remark = "智能立库"
                            };
                            infosAdd.Add(dt_InventoryInfo);
                        }
                        #endregion
 
                        #region 库存批次
                        if (cabinOrder.Order_type != InOrderTypeEnum.Allocat.ObjToInt().ToString())
                        {
                            Dt_Inventory_Batch? inventory_Batch = inventory_Batches.FirstOrDefault(x => x.MaterielCode == dt_InventoryInfo.MaterielCode && x.BatchNo == dt_InventoryInfo.BatchNo);
                            if (inventory_Batch != null)
                            {
                                inventory_Batch.StockQuantity += orderQty;
                                inventory_Batch.AvailableQuantity += orderQty;
                                batchesUp.Add(inventory_Batch);
                            }
                            else
                            {
                                inventory_Batch = new Dt_Inventory_Batch
                                {
                                    MaterielCode = dt_InventoryInfo.MaterielCode,
                                    MaterielName = dt_InventoryInfo.MaterielName,
                                    MaterielSpec = dt_InventoryInfo.MaterielSpec,
                                    BatchNo = dt_InventoryInfo.BatchNo,
                                    StockQuantity = dt_InventoryInfo.StockQuantity,
                                    OutboundQuantity = dt_InventoryInfo.OutboundQuantity,
                                    AvailableQuantity = dt_InventoryInfo.AvailableQuantity,
                                    SupplyQuantity = dt_InventoryInfo.SupplyQuantity,
                                    ERPStockQuantity = 0,
                                    Status = false,
                                    ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                    ValidityPeriod = dt_InventoryInfo.ValidityPeriod.ObjToDate(),
                                    Remark = "自动创建"
                                };
                                batchesAdd.Add(inventory_Batch);
                            }
                        }
                        #endregion
 
                        #region 添加入库任务历史
                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                        {
                            WarehouseCode = dt_InventoryInfo.WarehouseCode,
                            TaskNum = dt_InventoryInfo.Id,
                            OperateType = OperateTypeEnum.自动完成.ToString(),
                            InsertTime = DateTime.Now,
                            TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                            BatchNo = dt_InventoryInfo.BatchNo,
                            MaterielName = dt_InventoryInfo.MaterielName,
                            MaterielCode = dt_InventoryInfo.MaterielCode,
                            MaterielSpec = dt_InventoryInfo.MaterielSpec,
                            TaskType = cabinOrder.Order_type == "1" ? TaskTypeEnum.In.ObjToInt() : TaskTypeEnum.OutReturn.ObjToInt(),
                            CreateDate = DateTime.Now,
                            Creater = App.User.UserName,
                            LocationCode = dt_InventoryInfo.LocationCode,
                            OrderNo = cabinOrder.Order_no,
                            StockQuantity = orderQty,
                            SupplyQuantity = 0,
                            Remark = "入库"
                        };
                        if (cabinOrder.Order_type == InOrderTypeEnum.Allocat.ObjToInt().ToString()) supplyTask_Hty.TaskType = TaskTypeEnum.AllocatIn.ObjToInt();
                        supplyTask_Hties.Add(supplyTask_Hty);
                        #endregion
                    }
                    if (cabinOrder.Details == null || !cabinOrder.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
                        cabinOrder.OdrderStatus = "已完成";
                    _unitOfWorkManage.BeginTran();
                    _supplyTaskHtyService.AddData(supplyTask_Hties);
                    if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
                    if (infosAdd.Count > 0) _inventoryInfoService.AddData(infosAdd);
                    if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
                    if (batchesAdd.Count > 0) _inventory_BatchServices.AddData(batchesAdd);
                    _cabinOrderDetailServices.UpdateData(cabinOrderDetailsUp);
                    BaseDal.UpdateData(cabinOrder);
                    _unitOfWorkManage.CommitTran();
                }
                content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
    }
}