yanjinhui
8 小时以前 07b532a6a6adf0a9d965b037cf98c1f98accd138
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
using WIDESEA_Core;
using WIDESEA_Model.Models;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core.Enums;
using WIDESEA_ISquareCabinServices;
using WIDESEA_Core.Helper;
using WIDESEA_Common;
using static WIDESEA_DTO.SquareCabin.OrderDto;
 
namespace WIDESEA_SquareCabinServices
{
    public partial class DeliveryOrderServices
    {
        public WebResponseContent CompleteLKOutOrder(Dt_DeliveryOrder deliveryOrder, EdiOrderCallbackRequest request)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_DeliveryOrderDetail> cabinOrderDetailsUp = new List<Dt_DeliveryOrderDetail>();
                List<Dt_SupplyTask_Hty> supplyTask_Hties = new List<Dt_SupplyTask_Hty>();
                List<Dt_InventoryInfo> infosUp = new List<Dt_InventoryInfo>();
                List<Dt_Inventory_Batch> batchesUp = new List<Dt_Inventory_Batch>();
                List<Dt_InventoryInfo> infosDel = new List<Dt_InventoryInfo>();//添加删除
                List<Dt_Inventory_Batch> batchesDel = new List<Dt_Inventory_Batch>();
                List<Dt_MaterielInfo> materielInfos = _materielInfoService.Repository.QueryData(x => deliveryOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
                List<Dt_InventoryInfo> inventoryInfos = _inventoryInfoService.Repository.QueryData(x => deliveryOrder.Warehouse_no == x.WarehouseCode && deliveryOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
                List<Dt_Inventory_Batch> inventory_Batches = _inventory_BatchServices.Repository.QueryData(x => deliveryOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
                List<Dt_MaterielInfo> MaterieInfoBusiness =new List<Dt_MaterielInfo>();
                #region 盘亏出库单
                if (deliveryOrder.Out_type == "6")
                {
                    try
                    {
                        foreach (var detail in request.details)
                        {
                            Dt_DeliveryOrderDetail? orderDetail = deliveryOrder.Details.FirstOrDefault(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
                            if (orderDetail == null)
                            {
                                _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, "盘亏出库单", $"未找到盘点单【{deliveryOrder.Out_no}】物料编号【{detail.productCode}】物料批次【{detail.batchNo}】的明细信息");
                                throw new Exception($"未找到盘点单【{deliveryOrder.Out_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 != Math.Abs(inventoryInfo.SupplyQuantity))
                            {
                                _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, "盘亏出库单", $"盘亏出库数量不一致,上传数量【{orderQty}】,库存盘亏数量【{Math.Abs(inventoryInfo.SupplyQuantity)}】");
                                throw new Exception($"盘亏出库数量不一致,上传数量【{orderQty}】,库存盘亏数量【{Math.Abs(inventoryInfo.SupplyQuantity)}】");
 
                            }
                            //添加物料信息中的立库业务库存信息
                            var mater = materielInfos.FirstOrDefault(x => x.MaterielCode == detail.productCode);
                            if (mater != null)
                            {
                                mater.Business_qty += inventoryInfo.SupplyQuantity;
                                MaterieInfoBusiness.Add(mater);
                            }
                            inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
                            inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
                            inventoryInfo.SupplyQuantity = 0;
                            inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                            // 检查库存是否为0,如果是则标记为删除
                            if (inventoryInfo.StockQuantity <= 0)
                                infosDel.Add(inventoryInfo);
                            else
                                infosUp.Add(inventoryInfo);
                            //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.OotDetailStatus = "已完成";
                            orderDetail.Order_Outqty = orderQty; //完成数量
                            if (inventory_Batch.StockQuantity<=0)
                                batchesDel.Add(inventory_Batch);
                            else
                                batchesUp.Add(inventory_Batch);
                            cabinOrderDetailsUp.Add(orderDetail);
                            #region 添加盘亏出库任务
                            Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                            {
                                WarehouseCode = inventoryInfo.WarehouseCode,
                                OperateType = OperateTypeEnum.自动完成.ToString(),
                                InsertTime = DateTime.Now,
                                TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
                                BatchNo = inventoryInfo.BatchNo,
                                MaterielName = inventoryInfo.MaterielName,
                                MaterielCode = inventoryInfo.MaterielCode,
                                MaterielSpec = inventoryInfo.MaterielSpec,
                                TaskType = TaskTypeEnum.ChenckOut.ObjToInt(),
                                CreateDate = DateTime.Now,
                                Creater = App.User.UserName,
                                LocationCode = inventoryInfo.LocationCode,
                                OrderNo = deliveryOrder.Out_no,
                                StockQuantity = orderQty,
                                SupplyQuantity = 0,
                                Remark = "盘亏出库"
                            };
                            supplyTask_Hties.Add(supplyTask_Hty);
                            #endregion
                        }
                        deliveryOrder.OutStatus = "已完成";
                        //从所有出库明细中,筛选出那些不在已完成列表中的明细项
                        if (deliveryOrder.Details.Where(x => !cabinOrderDetailsUp.Select(x => x.Id).Contains(x.Id)).Any())
                            deliveryOrder.OutStatus = "开始";
                        _unitOfWorkManage.BeginTran();
                        _supplyTaskHtyService.AddData(supplyTask_Hties);
                        _inventoryInfoService.UpdateData(infosUp);
                        _inventory_BatchServices.UpdateData(batchesUp);
                        _deliveryOrderDetailServices.UpdateData(cabinOrderDetailsUp);
                        BaseDal.UpdateData(deliveryOrder);
                        //添加物料信息中的立库业务库存信息
                        _materielInfoService.UpdateData(MaterieInfoBusiness);
                        _unitOfWorkManage.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        _unitOfWorkManage.RollbackTran();
                        throw new Exception(ex.Message);
                    }
                }
                #endregion
                else
                {
                    List<Dt_SupplyTask> supplyTasks = _supplyTaskService.Repository.QueryData(x => deliveryOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode) && x.OrderNo == request.externalOrderNo && x.WarehouseCode == deliveryOrder.Warehouse_no && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt());
                    foreach (var detail in request.details)
                    {
                        decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0) ?? 0;
                        Dt_SupplyTask? supplyTask = supplyTasks.FirstOrDefault(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
                        if (supplyTask == null)
                        {
                            _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, "出库单", $"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的出库任务");
                            throw new Exception($"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的出库任务");
                        }
                        if (supplyTask.StockQuantity != orderQty)
                        {
                            _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, "出库单", $"订单号【{request.orderNo}】物料编号【{detail.productCode}】的出库数量与出库任务数量不相等");
                            throw new Exception($"订单号【{request.orderNo}】物料编号【{detail.productCode}】的出库数量与出库任务数量不相等");
                        }
                        //处理出库单、库存信息、出库任务、调拨任务
                        content = OutTaskFinish(supplyTask, deliveryOrder);
                        if (!content.Status) throw new Exception(content.Message);
                    }
                }
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}