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_Core;
using WIDESEA_ISquareCabinServices;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
namespace WIDESEA_SquareCabinServices
{
public partial class DeliveryOrderServices
{
#region 创建平库调拨出库单、立库调拨入库单
///
/// 创建调拨单
///
/// 物料信息
///
public WebResponseContent CreateAllocatInOut(Dt_MaterielInfo materielInfo)
{
WebResponseContent response = new WebResponseContent();
try
{
List supplyTasks = new List();
List dt_InventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.MaterielCode == materielInfo.MaterielCode && x.StockStatus == StockStatusEmun.入库完成.ObjToInt() && x.StockQuantity > x.OutboundQuantity && x.WarehouseCode != "001").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;
}
}
#region 添加调拨出库单
Dt_DeliveryOrder deliveryOrder = new Dt_DeliveryOrder()
{
Out_no = DateTime.Now.ToString("yyMMddHHmmss"),
Out_type = OutOrderTypeEnum.Allocate.ObjToInt().ToString(),
OutStatus = "新建",
Details = new List()
};
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()
};
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,
Order_Inqty = item.Order_qty,
Batch_num = item.Batch_num,
OrderDetailStatus = "新建",
Status = 0
};
cabinOrder.Details.Add(orderDetail);
}
#endregion
_unitOfWorkManage.BeginTran();
_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
}
}