using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
namespace WIDESEA_SquareCabinServices
{
public partial class CabinOrderServices
{
///
/// 立库入库任务完成
///
///
///
public WebResponseContent CompleteLKInOrder(Dt_CabinOrder cabinOrder, EdiOrderCallbackRequest request)
{
WebResponseContent content = new WebResponseContent();
try
{
List cabinOrderDetailsUp = new List();
List supplyTask_Hties = new List();
List infosUp = new List();
List infosAdd = new List();
List batchesUp = new List();
List batchesAdd = new List();
List materielInfos = _materielInfoService.Repository.QueryData(x => cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
List inventoryInfos = _inventoryInfoService.Repository.QueryData(x => cabinOrder.Warehouse_no == x.WarehouseCode && cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
List inventory_Batches = _inventory_BatchServices.Repository.QueryData(x => cabinOrder.Details.Select(x => x.Goods_no).ToList().Contains(x.MaterielCode));
#region 盘盈入库单
if (cabinOrder.Order_type == "5")
{
foreach (var detail in request.details)
{
Dt_CabinOrderDetail? orderDetail = cabinOrder.Details.FirstOrDefault(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
if (orderDetail == null) throw new Exception($"未找到盘点单【{cabinOrder.Order_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();
var qty = inventoryInfo.SupplyQuantity - inventoryInfo.StockQuantity;
if (orderQty != qty)
throw new Exception($"盘盈入库数量不一致,上传数量【{orderQty}】,库存盘盈数量【{qty}】");
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.OrderDetailStatus = "已完成";
cabinOrderDetailsUp.Add(orderDetail);
#region 添加盘盈入库任务
Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
{
WarehouseCode = inventoryInfo.WarehouseCode,
OperateType = OperateTypeEnum.自动完成.ToString(),
InsertTime = DateTime.Now,
TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
BatchNo = inventoryInfo.BatchNo,
MaterielName = inventoryInfo.MaterielName,
MaterielCode = inventoryInfo.MaterielCode,
MaterielSpec = inventoryInfo.MaterielSpec,
TaskType = TaskTypeEnum.ChenckIn.ObjToInt(),
CreateDate = DateTime.Now,
Creater = App.User.UserName,
LocationCode = inventoryInfo.LocationCode,
OrderNo = cabinOrder.Order_no,
StockQuantity = orderQty,
SupplyQuantity = 0,
Remark = "盘盈入库"
};
supplyTask_Hties.Add(supplyTask_Hty);
#endregion
}
cabinOrder.OdrderStatus = "已完成";
if (cabinOrder.Details.Where(x => !cabinOrderDetailsUp.Select(x => x.Id).Contains(x.Id)).Any())
cabinOrder.OdrderStatus = "开始";
_unitOfWorkManage.BeginTran();
_supplyTaskHtyService.AddData(supplyTask_Hties);
_inventoryInfoService.UpdateData(infosUp);
_inventory_BatchServices.UpdateData(batchesUp);
_cabinOrderDetailServices.UpdateData(cabinOrderDetailsUp);
BaseDal.UpdateData(cabinOrder);
_unitOfWorkManage.CommitTran();
}
#endregion
else
{
foreach (var detail in request.details)
{
Dt_CabinOrderDetail? orderDetail = cabinOrder.Details.FirstOrDefault(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
if (orderDetail == null) throw new Exception($"未找到盘点单【{cabinOrder.Order_no}】物料编号【{detail.productCode}】物料名称【{detail.batchNo}】的明细信息");
if (orderDetail.OrderDetailStatus == "已完成") continue;
decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0) ?? 0;
orderDetail.Order_Inqty += orderQty;
if (orderDetail.Order_Inqty > orderDetail.Order_qty) throw new Exception($"入库数量不可超出单据数量");
orderDetail.OrderDetailStatus = "开始";
cabinOrder.OdrderStatus = orderDetail.OrderDetailStatus;
if (orderDetail.Order_Inqty == orderDetail.Order_qty)
{
orderDetail.OrderDetailStatus = "已完成";
cabinOrder.Details.Remove(orderDetail);
}
cabinOrderDetailsUp.Add(orderDetail);
Dt_MaterielInfo? materielInfo = materielInfos.FirstOrDefault(x => orderDetail.Goods_no == x.MaterielCode);
if (materielInfo == null) throw new Exception($"未找到物料编码【{orderDetail.Goods_no}】的物料信息");
#region 库存
Dt_InventoryInfo? dt_InventoryInfo = inventoryInfos.FirstOrDefault(x => x.MaterielCode == orderDetail.Goods_no && x.BatchNo == orderDetail.Batch_num);
if (dt_InventoryInfo != null)
{
dt_InventoryInfo.StockQuantity += orderQty;
dt_InventoryInfo.AvailableQuantity += orderQty;
dt_InventoryInfo.InDate = DateTime.Now;
infosUp.Add(dt_InventoryInfo);
}
else
{
dt_InventoryInfo = new Dt_InventoryInfo
{
PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
WarehouseCode = "001",
LocationCode = "立库",
StockStatus = StockStatusEmun.入库完成.ObjToInt(),
MaterielCode = materielInfo.MaterielCode,
MaterielName = materielInfo.MaterielName,
MaterielSpec = materielInfo.MaterielSpec,
BatchNo = detail.batchNo,
StockQuantity = orderQty,
OutboundQuantity = 0,
SupplyQuantity = 0,
AvailableQuantity = orderQty,
InDate = DateTime.Now,
ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
ShelfLife = 0,
ValidityPeriod = orderDetail.Exp_date,
Remark = "智能立库"
};
infosAdd.Add(dt_InventoryInfo);
}
#endregion
#region 库存批次
if (cabinOrder.Order_type != InOrderTypeEnum.Allocat.ObjToInt().ToString())
{
Dt_Inventory_Batch? inventory_Batch = inventory_Batches.FirstOrDefault(x => x.MaterielCode == dt_InventoryInfo.MaterielCode && x.BatchNo == dt_InventoryInfo.BatchNo);
if (inventory_Batch != null)
{
inventory_Batch.StockQuantity += orderQty;
inventory_Batch.AvailableQuantity += orderQty;
batchesUp.Add(inventory_Batch);
}
else
{
inventory_Batch = new Dt_Inventory_Batch
{
MaterielCode = dt_InventoryInfo.MaterielCode,
MaterielName = dt_InventoryInfo.MaterielName,
MaterielSpec = dt_InventoryInfo.MaterielSpec,
BatchNo = dt_InventoryInfo.BatchNo,
StockQuantity = dt_InventoryInfo.StockQuantity,
OutboundQuantity = dt_InventoryInfo.OutboundQuantity,
AvailableQuantity = dt_InventoryInfo.AvailableQuantity,
SupplyQuantity = dt_InventoryInfo.SupplyQuantity,
ERPStockQuantity = 0,
Status = false,
ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
ValidityPeriod = dt_InventoryInfo.ValidityPeriod.ObjToDate(),
Remark = "自动创建"
};
batchesAdd.Add(inventory_Batch);
}
}
#endregion
#region 添加入库任务历史
Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
{
WarehouseCode = dt_InventoryInfo.WarehouseCode,
TaskNum = dt_InventoryInfo.Id,
OperateType = OperateTypeEnum.自动完成.ToString(),
InsertTime = DateTime.Now,
TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
BatchNo = dt_InventoryInfo.BatchNo,
MaterielName = dt_InventoryInfo.MaterielName,
MaterielCode = dt_InventoryInfo.MaterielCode,
MaterielSpec = dt_InventoryInfo.MaterielSpec,
TaskType = cabinOrder.Order_type == "1" ? TaskTypeEnum.In.ObjToInt() : TaskTypeEnum.OutReturn.ObjToInt(),
CreateDate = DateTime.Now,
Creater = App.User.UserName,
LocationCode = dt_InventoryInfo.LocationCode,
OrderNo = cabinOrder.Order_no,
StockQuantity = orderQty,
SupplyQuantity = 0,
Remark = "入库"
};
if (cabinOrder.Order_type == InOrderTypeEnum.Allocat.ObjToInt().ToString()) supplyTask_Hty.TaskType = TaskTypeEnum.AllocatIn.ObjToInt();
supplyTask_Hties.Add(supplyTask_Hty);
#endregion
}
if (cabinOrder.Details == null || !cabinOrder.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
cabinOrder.OdrderStatus = "已完成";
_unitOfWorkManage.BeginTran();
_supplyTaskHtyService.AddData(supplyTask_Hties);
if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
if (infosAdd.Count > 0) _inventoryInfoService.AddData(infosAdd);
if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
if (batchesAdd.Count > 0) _inventory_BatchServices.AddData(batchesAdd);
_cabinOrderDetailServices.UpdateData(cabinOrderDetailsUp);
BaseDal.UpdateData(cabinOrder);
_unitOfWorkManage.CommitTran();
}
content.OK();
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
content.Error(ex.Message);
}
return content;
}
}
}