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
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_IWMsInfoServices;
using WIDESEA_Model.Models;
 
 
namespace WIDESEA_WMsInfoServices
{
    public class Inventory_BatchServices : ServiceBase<Dt_Inventory_Batch, IRepository<Dt_Inventory_Batch>>, IInventory_BatchServices
    {
        private IMessageInfoService _messageInfoService;
        private IMaterielInfoService _materielInfoService;
        private IInventoryInfoService _inventoryInfoService;
        private ISupplyTaskService _supplyTaskService;
        public Inventory_BatchServices(IRepository<Dt_Inventory_Batch> BaseDal, IMessageInfoService messageInfoService, IMaterielInfoService materielInfoService, IInventoryInfoService inventoryInfoService, ISupplyTaskService supplyTaskService) : base(BaseDal)
        {
            _messageInfoService = messageInfoService;
            _materielInfoService = materielInfoService;
            _inventoryInfoService = inventoryInfoService;
            _supplyTaskService = supplyTaskService;
        }
 
        public IRepository<Dt_Inventory_Batch> Repository => BaseDal;
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            try
            {
                string WareCode = WarehouseEnum.大件库.ObjToInt().ToString("000");
                var qty = saveModel.MainData["availableQuantity"].ObjToInt();
                var id = saveModel.MainData["id"].ObjToInt();
                var materielCode = saveModel.MainData["materielCode"].ToString();
                var batchNo = saveModel.MainData["batchNo"].ToString();
                //var Inventory_Batch = BaseDal.QueryFirst(x => x.Id == id);
                //if (qty > Inventory_Batch.AvailableQuantity) throw new Exception("调拨数量不可大于可用数量");
                var materielInfo = _materielInfoService.Repository.QueryFirst(x => x.MaterielCode == materielCode);
                if (materielInfo == null) return WebResponseContent.Instance.Error($"请维护药品编号【{materielCode}】的信息");
                if (!Enum.IsDefined(typeof(MaterielSourceTypeEnum), materielInfo.MaterielSourceType))
                    return WebResponseContent.Instance.Error($"请设置药品编号【{materielCode}】的属性分类");
                if (materielInfo.MaterielSourceType == MaterielSourceTypeEnum.PurchasePart)
                    return WebResponseContent.Instance.Error($"药品编号【{materielCode}】的属性分类为大件,不可调拨");
                List<Dt_InventoryInfo> dt_InventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.MaterielCode == materielInfo.MaterielCode && x.BatchNo == batchNo && x.StockStatus == StockStatusEmun.入库完成.ObjToInt() && x.AvailableQuantity > 0 && x.WarehouseCode == WareCode).OrderBy(x => x.InDate).ToList();
                if (dt_InventoryInfos.Count < 1) return WebResponseContent.Instance.Error($"物料编号【{materielInfo.MaterielCode}】大件库无库存!请及时补充库存!");
                var StockQty = dt_InventoryInfos.Sum(x => x.AvailableQuantity);
                if (StockQty < qty) return WebResponseContent.Instance.Error($"调拨数量不可大于大件库可用数量,大件库可用数量为【{StockQty}】");
                var zx = (qty / materielInfo.BoxQty).ObjToInt();
                var ys = qty % materielInfo.BoxQty;
                if (ys > 0) zx++;
                decimal Qty = 0;
                List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                Dt_DeliveryOrder deliveryOrder = new Dt_DeliveryOrder()
                {
                    Out_no = DateTime.Now.ToString("yyMMddHHmmss"),
                    Warehouse_no = WareCode,
                    Out_type = OutOrderTypeEnum.Allocate.ObjToInt().ToString(),
                    OutStatus = "新建",
                    Details = new List<Dt_DeliveryOrderDetail>()
                };
                foreach (var item in dt_InventoryInfos)
                {
                    decimal taskQty = 0;
                    if (zx < 1) break;
                    while (item.AvailableQuantity > 0 && zx > 0)
                    {
                        zx--;
                        Qty += materielInfo.BoxQty;
                        taskQty += materielInfo.BoxQty;
                        materielInfo.Business_qty += materielInfo.BoxQty;
                        item.OutboundQuantity += materielInfo.BoxQty;
                        item.AvailableQuantity -= materielInfo.BoxQty;
                    }
                    #region 添加调拨出库任务
                    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 = taskQty,
                        SupplyQuantity = 0,
                        Remark = "调拨出库"
                    };
                    supplyTasks.Add(supplyTask);
                    #endregion
                }
                Dt_DeliveryOrderDetail deliveryOrderDetail = new Dt_DeliveryOrderDetail()
                {
                    Batch_num = batchNo,
                    Order_Outqty = 0,
                    Order_qty = Qty,
                    CreateDate = DateTime.Now,
                    Creater = App.User.UserName ?? "System",
                    Goods_no = materielCode,
                    OotDetailStatus = "新建",
                    Status = 2,
                    Reservoirarea = WareCode
                };
                deliveryOrder.Details.Add(deliveryOrderDetail);
                #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
                Db.Ado.BeginTran();
                _materielInfoService.UpdateData(materielInfo);
                _inventoryInfoService.Repository.UpdateData(dt_InventoryInfos);
                _supplyTaskService.AddData(supplyTasks);
                if (deliveryOrder.Details.Count > 0) Db.InsertNav(deliveryOrder).Include(x => x.Details).ExecuteCommand();
                if (cabinOrder.Details.Count > 0) Db.InsertNav(cabinOrder).Include(x => x.Details).ExecuteCommand();
                Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK("调拨出入库单生成成功,请操作调拨单");
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent GetExpiredAndlow()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                DateTime dateTime = DateTime.Now.AddDays(30).Date;
                List<Dt_Inventory_Batch> inventory_Batches = BaseDal.QueryData(x => x.ValidityPeriod < dateTime);
                foreach (var item in inventory_Batches)
                {
                    var day = (item.ValidityPeriod - DateTime.Now).Days;
                    string d = $"还有{day}天过期";
                    if (day < 0) d = $"已过期{Math.Abs(day)}天";
                    _messageInfoService.AddMessageInfo(MessageGroupByEnum.InventoryExceedAlarm, "物料名称:" + item.MaterielName, $"物料编号【{item.MaterielCode}】物料批次【{item.BatchNo}】{d}", messageRemark: $"物料编号【{item.MaterielCode}】物料批次【{item.BatchNo}】");
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}