yanjinhui
3 天以前 b0ed419ede8bd1829e670891ffe878630ba517e2
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
 
namespace WIDESEA_SquareCabinServices
{
    public partial class DeliveryOrderServices
    {
        #region 创建盘点单、盘点任务
        /// <summary>  在库存批次信息中调用该接口进行盘点
        /// 创建盘点单
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public WebResponseContent CreateCheckOrder(int[] keys)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_DeliveryOrder> deliveryOrders = new List<Dt_DeliveryOrder>();
                //List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                List<Dt_SupplyTask> supplyTasks1 = new List<Dt_SupplyTask>();
                List<Dt_InventoryInfo> _InventoryInfos = new List<Dt_InventoryInfo>();
                List<Dt_Inventory_Batch> inventory_Batches = _inventory_BatchServices.Repository.QueryData(x => keys.Contains(x.Id));
                foreach (var item in inventory_Batches)
                {
                    var supplyTasks = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == item.MaterielCode && x.BatchNo == item.BatchNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt()).ToList();
                    if (supplyTasks.Count > 0) throw new Exception($"物料编号【{item.MaterielCode}】批次号【{item.BatchNo}】已存在盘点任务,请先完成当前盘点任务");
                    supplyTasks = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == item.MaterielCode && x.BatchNo == item.BatchNo && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt()).ToList();
                    if (supplyTasks.Count > 0) throw new Exception($"物料编号【{item.MaterielCode}】批次号【{item.BatchNo}】已存在盘点任务,请先完成当前盘点任务");
                    List<Dt_InventoryInfo> inventoryInfos = _inventoryInfoService.Repository.QueryData(x => item.MaterielCode == x.MaterielCode && x.BatchNo == item.BatchNo);
                    if (inventoryInfos.Count > 0)
                    {
                        foreach (var items in inventoryInfos.GroupBy(x => x.WarehouseCode))
                        {
                            Dt_DeliveryOrder? deliveryOrder = deliveryOrders.Where(x => x.Warehouse_no == items.Key).FirstOrDefault();
                            bool isNull = false;
                            if (deliveryOrder == null)
                            {
                                isNull = true;
                                deliveryOrder = new Dt_DeliveryOrder()
                                {
                                    //Out_no = item.MaterielCode + "_" + item.BatchNo,
                                    Out_no = $"PD{items.Key}{DateTime.Now.ToString("yyMMddHHmmss")}",
                                    Out_type = "20",
                                    OutStatus = "新建",
                                    Warehouse_no = items.Key,
                                    Details = new List<Dt_DeliveryOrderDetail>()
                                };
                            }
                            var Status = items.Key == "001" ? 0 : 2;//判断是否为立库区域
                            Dt_DeliveryOrderDetail deliveryOrderDetail = new Dt_DeliveryOrderDetail()
                            {
                                Batch_num = item.BatchNo,
                                Order_Outqty = 0,
                                Order_qty = items.Select(x => x.StockQuantity).Sum(),
                                CreateDate = DateTime.Now,
                                Creater = App.User.UserName,
                                Goods_no = item.MaterielCode,
                                OotDetailStatus = "新建",
                                Status = Status,
                                Reservoirarea = items.Key
                            };
                            deliveryOrder.Details.Add(deliveryOrderDetail);
                            if (isNull) deliveryOrders.Add(deliveryOrder);
                            #region 创建盘点任务
                            foreach (var inventory in items)
                            {
                                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                                {
                                    WarehouseCode = inventory.WarehouseCode,
                                    TaskStatus = SupplyStatusEnum.NewCheck.ObjToInt(),
                                    BatchNo = inventory.BatchNo,
                                    MaterielName = inventory.MaterielName,
                                    MaterielCode = inventory.MaterielCode,
                                    MaterielSpec = inventory.MaterielSpec,
                                    TaskType = TaskTypeEnum.OutInventory.ObjToInt(),
                                    CreateDate = DateTime.Now,
                                    Creater = App.User.UserName,
                                    LocationCode = inventory.LocationCode,
                                    OrderNo = deliveryOrder.Out_no,
                                    StockQuantity = inventory.StockQuantity,
                                    SupplyQuantity = 0,
                                    Remark = "盘点"
                                };
                                supplyTasks1.Add(supplyTask);
                                _InventoryInfos.Add(inventory);
                            }
                            //}
                            #endregion
                        }
                    }
                }
                _InventoryInfos.ForEach(x =>
                {
                    x.StockStatus = StockStatusEmun.盘点锁定.ObjToInt();
                });
                _unitOfWorkManage.BeginTran();
                _inventoryInfoService.UpdateData(_InventoryInfos);
                _supplyTaskService.AddData(supplyTasks1);
                BaseDal.Db.InsertNav(deliveryOrders)
                        .Include(x => x.Details)
                        .ExecuteCommand();
                _unitOfWorkManage.CommitTran();
                content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
 
        #region 查询盘点单信息
        /// <summary>
        /// 查询盘点单    pad查询盘单信息
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent GetCheckOrders(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
                string warehouseCode = saveModel.MainData["warehouseId"].ToString();
                string orderNo = saveModel.MainData["orderNo"].ToString();
                List<Dt_DeliveryOrder> dt_ReceiveOrders = new List<Dt_DeliveryOrder>();
                if (string.IsNullOrEmpty(orderNo))
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Warehouse_no == warehouseCode && x.Out_type == "20").Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
                }
                else
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.Out_no.Contains(orderNo) || x.Client_no.Contains(orderNo)) && (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Out_type == "20" && x.Warehouse_no == warehouseCode).OrderByDescending(x => x.CreateDate).Includes(x => x.Details).ToPageList(pageNo, 5);
                }
 
                content.OK(data: dt_ReceiveOrders);
            }
            catch (Exception)
            {
 
                throw;
            }
            return content;
        }
        #endregion
 
        #region 查找盘点/出库任务
        /// <summary>
        /// 查找盘点/出库任务
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent GetCheckOutTasks(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
                string LocationCode = saveModel.MainData["locationCode"].ToString();
                string warehouseCode = saveModel.MainData["warehouseId"].ToString();
                string orderNo = saveModel.MainData["orderNo"].ToString();
                bool isCheck = saveModel.MainData["isCheck"].ObjToBool();
                var TaskStatus = isCheck ? SupplyStatusEnum.NewCheck.ObjToInt() : SupplyStatusEnum.NewOut.ObjToInt();
                List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                if (string.IsNullOrEmpty(LocationCode))
                {
                    supplyTasks = Db.Queryable<Dt_SupplyTask>().Where(x => x.TaskStatus == TaskStatus && x.OrderNo == orderNo && x.WarehouseCode == warehouseCode).ToPageList(pageNo, 5);
                }
                else
                {
                    supplyTasks = Db.Queryable<Dt_SupplyTask>().Where(x => x.TaskStatus == TaskStatus && x.OrderNo == orderNo && x.WarehouseCode == warehouseCode && x.LocationCode.Contains(LocationCode)).ToPageList(pageNo, 5);
                }
                content.OK(data: supplyTasks);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        #endregion
 
        #region 完成盘点任务
        public WebResponseContent CheckFinish(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var LocationCode = saveModel.MainData["locationCode"].ToString();
                var TaskId = saveModel.MainData["taskId"].ObjToInt();
                var qty = saveModel.MainData["qty"].ObjToInt();
                Dt_SupplyTask supplyTask = _supplyTaskService.Repository.QueryFirst(x => x.TaskId == TaskId && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt());
                if (supplyTask == null) throw new Exception("当前盘点任务已完成");
                if (supplyTask.LocationCode != LocationCode) throw new Exception($"当前盘点货位【{LocationCode}】与任务分配货位不匹配");
                supplyTask.SupplyQuantity = qty;
                content = CheckTaskFinish(supplyTask, LocationCode);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
 
        #region 盘点任务完成,平库和立库
        public WebResponseContent CheckTaskFinish(Dt_SupplyTask supplyTask, string LocationCode = null)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                supplyTask.TaskStatus = SupplyStatusEnum.CheckFinish.ObjToInt();
                Dt_DeliveryOrder cabinOrder = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == supplyTask.OrderNo && x.Warehouse_no == supplyTask.WarehouseCode && x.Out_type == "20").Includes(x => x.Details).First();
                if (cabinOrder == null) return WebResponseContent.Instance.Error($"盘点单已完成");
                Dt_DeliveryOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Batch_num == supplyTask.BatchNo && x.Reservoirarea == supplyTask.WarehouseCode && x.Goods_no == supplyTask.MaterielCode).First();
                if (cabinOrderDetail == null) return WebResponseContent.Instance.Error($"盘点单明细已完成");
                Dt_MaterielInfo materielInfo = _materielInfoService.Repository.QueryFirst(x => x.MaterielCode == cabinOrderDetail.Goods_no);
                if (materielInfo == null) return WebResponseContent.Instance.Error($"请维护物料编号【{cabinOrderDetail.Goods_no}】的物料信息");
                cabinOrderDetail.Order_Outqty += supplyTask.SupplyQuantity;
                #region 平库盘点只能出现整箱差异
                if (supplyTask.WarehouseCode == WarehouseEnum.大件库.ObjToInt().ToString("000"))
                {
                    var Qty = supplyTask.SupplyQuantity % materielInfo.BoxQty;
                    if (Qty != 0) return WebResponseContent.Instance.Error($"大件库库存为整箱!盘点数量不能存在散件");
                }
                #endregion
 
                #region 修改盘点单
                cabinOrder.OutStatus = "开始";
                cabinOrderDetail.OotDetailStatus = "开始";
                #endregion
 
                #region 库存
                Dt_InventoryInfo inventoryInfo = new Dt_InventoryInfo();
                if (supplyTask.WarehouseCode != WarehouseEnum.立库.ObjToInt().ToString("000"))
                {
                    inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.WarehouseCode == supplyTask.WarehouseCode && x.LocationCode == supplyTask.LocationCode);
                    if (inventoryInfo == null) return WebResponseContent.Instance.Error($"未找到货位【{LocationCode}】的库存信息");
                }
                else
                {
                    inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.WarehouseCode == supplyTask.WarehouseCode);
                }
                inventoryInfo.SupplyQuantity = supplyTask.SupplyQuantity - inventoryInfo.StockQuantity;
                #endregion
 
                #region 判断当前物料批次的盘点任务是否全部完成
                List<Dt_SupplyTask> supplyTasks = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == supplyTask.MaterielCode && x.BatchNo == supplyTask.BatchNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt() && x.WarehouseCode == supplyTask.WarehouseCode && x.TaskId != supplyTask.TaskId).ToList();
                List<Dt_SupplyTask> supplyTasks1 = _supplyTaskService.Repository.QueryData(x => x.MaterielCode == supplyTask.MaterielCode && x.BatchNo == supplyTask.BatchNo && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt() && x.TaskId != supplyTask.TaskId).ToList();
                Dt_Inventory_Batch inventory_Batch = new Dt_Inventory_Batch();
                List<Dt_InventoryInfo> inventoryInfos = new List<Dt_InventoryInfo>();
                if (supplyTasks1.Count < 1)
                {
                    #region 查找库存
                    inventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.Id != inventoryInfo.Id);
                    var SupplyQuantitys = inventoryInfos.Sum(x => x.SupplyQuantity) + inventoryInfo.SupplyQuantity;//计算盘点总数
                    #endregion
 
                    #region 库存批次
                    inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no);
                    
                    if (SupplyQuantitys == 0)
                    {
                        foreach (var item in inventoryInfos)
                        {
                            item.StockQuantity += item.SupplyQuantity;
                            item.SupplyQuantity = 0;
                            item.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                        }
                        inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
                        inventoryInfo.SupplyQuantity = 0;
                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                    }
                    else
                    {
                        inventory_Batch.SupplyQuantity += SupplyQuantitys;
                    }
                    #endregion
                }
                _unitOfWorkManage.BeginTran();
                #region 处理盘点单
                if (supplyTasks.Count < 1)
                {
                    #region 完成盘点单
                    cabinOrder.OutStatus = "已完成";
                    cabinOrder.Details.ForEach(x =>
                    {
                        x.OotDetailStatus = "已完成";
                    });
                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrder.Details, OperateTypeEnum.自动完成);
                    cabinOrder.Details = null;
                    BaseDal.DeleteAndMoveIntoHty(cabinOrder, OperateTypeEnum.自动完成);
 
                    #endregion
                }
                else
                {
                    BaseDal.UpdateData(cabinOrder);
                    _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
                }
                #endregion
                _inventoryInfoService.UpdateData(inventoryInfo);
                _supplyTaskService.Repository.DeleteAndMoveIntoHty(supplyTask, OperateTypeEnum.自动完成);
                _inventoryInfoService.UpdateData(inventoryInfos);
                _inventory_BatchServices.UpdateData(inventory_Batch);
                _unitOfWorkManage.CommitTran();
                #endregion
                content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
    }
}