yanjinhui
2025-10-26 e1383d0a6ee0558c34f013f4e2561063bc2353b3
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
using HslCommunication.Secs.Types;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
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_ISquareCabinServices;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
 
namespace WIDESEA_SquareCabinServices
{
    public partial class DeliveryOrderServices
    {
        #region 创建平库调拨出库单、立库调拨入库单  在立库出库完成后调用  在出库完成后将物料表中的业务数量(Business_qty)减去实际出库数量
        /// <summary>
        /// 创建调拨单
        /// </summary>
        /// <param name="materielInfo">物料信息</param>
        /// <returns></returns>
        public WebResponseContent CreateAllocatInOut(Dt_MaterielInfo materielInfo)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                string WareCode = WarehouseEnum.大件库.ObjToInt().ToString("000");
                if (materielInfo.Business_qty >= materielInfo.MinQty) return response;
                Dt_Tactics tactics = _tacticsService.Repository.QueryFirst(x => x.TacticeName == "出库策略");
                List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                List<Dt_InventoryInfo> dt_InventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.MaterielCode == materielInfo.MaterielCode && x.StockStatus == StockStatusEmun.入库完成.ObjToInt() && x.StockQuantity > x.OutboundQuantity && x.WarehouseCode == WareCode);
                if (tactics.SelectTactice == TacticsEnum.ComeOutonFirstTime.ObjToInt())
                    dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.ValidityPeriod).ToList();
                else
                    dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.InDate).ToList();
                decimal Qty = 0;
                foreach (var item in dt_InventoryInfos)
                {
                    if (materielInfo.Business_qty >= materielInfo.MinQty) break;
                    item.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                    while (item.StockQuantity > item.OutboundQuantity && materielInfo.Business_qty < materielInfo.MinQty)
                    {
                        Qty += materielInfo.BoxQty;
                        materielInfo.Business_qty += materielInfo.BoxQty;
                        item.OutboundQuantity += materielInfo.BoxQty;
                    }
                    item.AvailableQuantity = item.StockQuantity - item.OutboundQuantity;
                }
                #region 大件库补立库后立库业务库存数还是小于立库最小库存数,添加提示信息
 
                #endregion
                #region 添加调拨出库单
                Dt_DeliveryOrder deliveryOrder = new Dt_DeliveryOrder()
                {
                    Out_no = DateTime.Now.ToString("yyMMddHHmmss"),
                    Out_type = OutOrderTypeEnum.Allocate.ObjToInt().ToString(),
                    OutStatus = "新建",
                    Details = new List<Dt_DeliveryOrderDetail>()
                };
                dt_InventoryInfos = dt_InventoryInfos.Where(X => X.StockStatus == StockStatusEmun.出库锁定.ObjToInt()).ToList();
                foreach (var item in dt_InventoryInfos.GroupBy(x => x.BatchNo))
                {
                    Dt_DeliveryOrderDetail deliveryOrderDetail = new Dt_DeliveryOrderDetail()
                    {
                        Batch_num = item.Key,
                        Order_Outqty = 0,
                        Order_qty = item.Select(x => x.OutboundQuantity).Sum(),
                        CreateDate = DateTime.Now,
                        Creater = App.User.UserName ?? "System",
                        Goods_no = item.First().MaterielCode,
                        OotDetailStatus = "新建",
                        Status = 2,
                        Reservoirarea = item.First().WarehouseCode
                    };
                    deliveryOrder.Warehouse_no = item.First().WarehouseCode;
                    deliveryOrder.Details.Add(deliveryOrderDetail);
                }
                #endregion
 
                #region 添加调拨出库任务
                foreach (var item in dt_InventoryInfos)
                {
                    Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                    {
                        WarehouseCode = item.WarehouseCode,
                        BatchNo = item.BatchNo,
                        MaterielName = item.MaterielName,
                        MaterielCode = item.MaterielCode,
                        MaterielSpec = item.MaterielSpec,
                        TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
                        TaskType = TaskTypeEnum.AllocatOut.ObjToInt(),
                        CreateDate = DateTime.Now,
                        Creater = App.User.UserName ?? "System",
                        LocationCode = item.LocationCode,
                        OrderNo = deliveryOrder.Out_no,
                        StockQuantity = item.OutboundQuantity,
                        SupplyQuantity = 0,
                        Remark = "调拨出库"
                    };
                    supplyTasks.Add(supplyTask);
                }
                #endregion
 
                #region 添加调拨入库单
                Dt_CabinOrder cabinOrder = new Dt_CabinOrder()
                {
                    Order_no = deliveryOrder.Out_no,
                    Order_type = InOrderTypeEnum.Allocat.ObjToInt().ToString(),
                    Warehouse_no = "001",
                    OdrderStatus = "新建",
                    Details = new List<Dt_CabinOrderDetail>()
                };
                foreach (var item in deliveryOrder.Details)
                {
                    Dt_CabinOrderDetail orderDetail = new Dt_CabinOrderDetail()
                    {
                        Reservoirarea = cabinOrder.Warehouse_no,
                        Goods_no = item.Goods_no,
                        Order_qty = item.Order_qty,
                        Batch_num = item.Batch_num,
                        OrderDetailStatus = "新建",
                        Status = 0
                    };
                    cabinOrder.Details.Add(orderDetail);
                }
                #endregion
 
                _unitOfWorkManage.BeginTran();
                _basicService.MaterielInfoService.Repository.UpdateData(materielInfo);
                _inventoryInfoService.Repository.UpdateData(dt_InventoryInfos);
                _supplyTaskService.AddData(supplyTasks);
                BaseDal.Db.InsertNav(deliveryOrder).Include(x => x.Details).ExecuteCommand();
                _cabinOrderServices.Repository.Db.InsertNav(cabinOrder).Include(x => x.Details).ExecuteCommand();
                _unitOfWorkManage.CommitTran();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                response.Error(ex.Message);
            }
            return response;
        }
        #endregion
    }
}