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
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;
 
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_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));
                #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) 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))
                                throw new Exception($"盘亏出库数量不一致,上传数量【{orderQty}】,库存盘亏数量【{Math.Abs(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.OotDetailStatus = "已完成";
                            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);
                        _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) throw new Exception($"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的出库任务");
                        if (supplyTask.StockQuantity != orderQty)
                            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;
        }
    }
}