WMS/HHWMS.Web/Areas/Receipt/Controllers/ReceiptInController.cs
zhougf 17b3236989 添加库存查询条件
入库保存时,依据容器的code,并不是前端传递的code
2026-03-07 11:26:25 +08:00

399 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Linq;
using HHWMS.Web.Models;
using Microsoft.AspNetCore.Mvc;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
namespace HHWMS.Web.Controllers
{
[Area("receipt")]
[ActionDescription("入库操作")]
public partial class ReceiptInController : BaseController
{
private readonly IUnitWork _unitWork;
private readonly WTMContext _context;
public ReceiptInController(IUnitWork unitWork, WTMContext wtmContext)
{
_unitWork = unitWork;
_context = wtmContext;
}
#region Index
[ActionDescription("Sys.Search")]
public ActionResult Index()
{
return PartialView("Index");
}
#endregion Index
[HttpGet]
public ActionResult SearchMaterials(string keyword)
{
if (string.IsNullOrEmpty(keyword))
{
return JsonMore(new { Data = new object[] { } });
}
var materials = _unitWork.Find<Material>(x =>
x.MaterialCode.Contains(keyword) ||
x.MaterialName.Contains(keyword))
.Take(10)
.Select(x => new {
MaterialCode = x.MaterialCode,
MaterialName = x.MaterialName,
MaterialSpec = x.MaterialSpec,
MaterialUnit = x.MaterialUnit
})
.ToList();
return JsonMore( materials );
}
[HttpPost]
public ActionResult AddInventory(string containerCode, string materialCode, decimal qty)
{
try
{
if (string.IsNullOrEmpty(containerCode))
{
return JsonMore(null, 500, "容器编码为空!");
}
if (string.IsNullOrEmpty(materialCode))
{
return JsonMore(null, 500, "物料编码为空!");
}
if (qty <= 0)
{
return JsonMore(null, 500, "数量必须大于0!");
}
// 查询容器
var container = _unitWork.FindSingle<Container>(u => u.Code == containerCode);
if (container == null)
{
return JsonMore(null, 500, $"库内没有编码为 {containerCode} 的容器!");
}
// 查询物料信息
var material = _unitWork.FindSingle<Material>(x => x.MaterialCode == materialCode);
if (material == null)
{
return JsonMore(null, 500, $"未找到物料编码为 {materialCode} 的物料信息!");
}
// 创建库存记录
var inventory = new Inventory()
{
ZoneCode = ZoneCodeEnum.立库,
ContainerCode = container.Code,
MaterialCode = material.MaterialCode,
MaterialName = material.MaterialName,
MaterialType = material.MaterialType,
MaterialSpec = material.MaterialSpec,
MaterialUnit = material.MaterialUnit,
Qty = qty,
TaskStatus = InventoryTaskStatus.任务中,
CreateTime = DateTime.Now
};
_unitWork.Add(inventory);
// 创建入库交易记录
var transaction = new InventoryTransaction
{
BillType = InventoryTranType.入库,
MaterialCode = material.MaterialCode,
MaterialName = material.MaterialName,
MaterialUnit = material.MaterialUnit,
ContainerCode = container.Code,
Qty = qty,
CreateTime = DateTime.Now,
CreateBy = "WMS",
UpdateTime = DateTime.Now,
UpdateBy = "WMS"
};
_unitWork.Add(transaction);
return JsonMore(new { Id = inventory.ID });
}
catch (Exception ex)
{
return JsonMore(null, 500, ex.Message);
}
}
[HttpPost]
public ActionResult TaskIn(string containerCode)
{
try
{
if (string.IsNullOrEmpty(containerCode))
{
return JsonMore(null, 500, "容器编码为空!");
}
// 查询容器
var container = _unitWork.FindSingle<Container>(u => u.Code == containerCode);
if (container == null)
{
return JsonMore(null, 500, $"库内没有编码为 {containerCode} 的容器!");
}
// 查询容器的库存
var inventories = _unitWork.Find<Inventory>(x => x.ContainerCode == containerCode).ToList();
// 根据库存情况设置容器状态
if (inventories.Any())
{
container.Status = ContainerStatus.有;
}
else
{
container.Status = ContainerStatus.空;
}
// 查找仓位
var location = _unitWork.Find<Location>(u => u.Status == LocationStatus.空仓位
&& u.Type == container.Type
&& !u.IsStop)
.OrderBy(x => x.Layer)
.ThenBy(x => x.Line)
.FirstOrDefault();
// 判断location是否为空
if (location == null)
{
return JsonMore(null, 500, "库内没有空闲仓位!");
}
// 更新容器状态
container.IsLock = ContainerLockEnum.任务锁;
container.UpdateTime = DateTime.Now;
container.UpdateBy = Wtm?.LoginUserInfo?.ITCode ?? "WMS";
// _unitWork.Update(container);
// 更新仓位状态
location.Status = LocationStatus.任务锁;
location.UpdateTime = DateTime.Now;
location.UpdateBy = Wtm?.LoginUserInfo?.ITCode ?? "WMS";
// _unitWork.Update(location);
// 更新库存记录
// foreach (var inventory in inventories)
// {
// inventory.LocationCode = location.Code;
// _unitWork.Update(inventory);
// }
// 创建入库任务
var task = new TaskHeader
{
ZoneCode = ZoneCodeEnum.立库,
TaskNo = _unitWork.GetSerialNumber("In"),
TaskType = TaskTypeEnum.立库入库,
ContainerCode = container.Code,
FromLocation = "",
ToLocation = location.Code,
Status = TaskStatusEnum.待下发,
CreateTime = DateTime.Now,
CreateBy = Wtm?.LoginUserInfo?.ITCode??"WMS",
};
// _unitWork.Add(task);
// 下发任务到wcs
var postData = new
{
TaskNo = task.TaskNo,
PalletNo = task.ContainerCode,
FromLocation = task.FromLocation,
ToLocation = task.ToLocation
};
try
{
var res = Wtm.CallAPI<ApiJsonResult<dynamic>>("WCS", "TaskAssign", HttpMethodEnum.POST, postData)
.Result?.Data;
if (res.Success)
{
task.Status = TaskStatusEnum.已下发;
task.UpdateTime = DateTime.Now;
task.UpdateBy = "WMS";
_unitWork.Add(task);
_unitWork.Update(container);
_unitWork.Update(location);
}
else
{
task.ErrorMsg = res.Msg;
return JsonMore(null, 500, res.Msg);
}
}
catch (Exception ex)
{
task.ErrorMsg = ex.Message;
return JsonMore(null, 500, "任务下发失败!");
}
return JsonMore(null);
}
catch (Exception ex)
{
return JsonMore(null, 500, ex.Message);
}
}
[HttpPost]
public ActionResult DeleteInventory(int id)
{
try
{
// 查询库存记录
var inventory = _unitWork.Find<Inventory>(x => x.ID == id).FirstOrDefault();
if (inventory == null)
{
return JsonMore(null, 500, "未找到对应的库存记录!");
}
// 创建出库交易记录
var transaction = new InventoryTransaction
{
BillType = InventoryTranType.出库,
MaterialCode = inventory.MaterialCode,
MaterialName = inventory.MaterialName,
MaterialUnit = inventory.MaterialUnit,
ContainerCode = inventory.ContainerCode,
Qty = inventory.Qty,
CreateTime = DateTime.Now,
CreateBy = "WMS",
UpdateTime = DateTime.Now,
UpdateBy = "WMS"
};
_unitWork.Add(transaction);
// 删除库存记录
_unitWork.Delete(inventory);
return JsonMore(null);
}
catch (Exception ex)
{
return JsonMore(null, 500, ex.Message);
}
}
[HttpPost]
public ActionResult UpdateInventory(int id, decimal qty)
{
try
{
if (qty <= 0)
{
return JsonMore(null, 500, "数量必须大于0!");
}
// 查询库存记录
var inventory = _unitWork.Find<Inventory>(x => x.ID == id).FirstOrDefault();
if (inventory == null)
{
return JsonMore(null, 500, "未找到对应的库存记录!");
}
// 创建出库交易记录(原数量)
var outTransaction = new InventoryTransaction
{
BillType = InventoryTranType.出库,
MaterialCode = inventory.MaterialCode,
MaterialName = inventory.MaterialName,
MaterialUnit = inventory.MaterialUnit,
ContainerCode = inventory.ContainerCode,
Qty = inventory.Qty,
CreateTime = DateTime.Now,
CreateBy = "WMS",
UpdateTime = DateTime.Now,
UpdateBy = "WMS"
};
_unitWork.Add(outTransaction);
// 创建入库交易记录(新数量)
var inTransaction = new InventoryTransaction
{
BillType = InventoryTranType.入库,
MaterialCode = inventory.MaterialCode,
MaterialName = inventory.MaterialName,
MaterialUnit = inventory.MaterialUnit,
ContainerCode = inventory.ContainerCode,
Qty = qty,
CreateTime = DateTime.Now,
CreateBy = "WMS",
UpdateTime = DateTime.Now,
UpdateBy = "WMS"
};
_unitWork.Add(inTransaction);
// 更新库存数量
inventory.Qty = qty;
inventory.UpdateTime = DateTime.Now;
inventory.UpdateBy = Wtm?.LoginUserInfo?.ITCode ?? "WMS";
_unitWork.Update(inventory);
return JsonMore(null);
}
catch (Exception ex)
{
return JsonMore(null, 500, ex.Message);
}
}
[HttpGet]
public JsonResult ValidateContainer(string containerCode)
{
try
{
if (string.IsNullOrEmpty(containerCode))
{
return Json(new { Code = 400, Msg = "容器编码不能为空" });
}
var container = _unitWork.FindSingle<Container>(u => u.Code == containerCode);
if (container == null)
{
return Json(new { Code = 400, Msg = "容器不存在" });
}
if (!string.IsNullOrWhiteSpace(container.LocationCode))
{
return Json(new { Code = 400, Msg = $"{containerCode} 容器状态不正确!已存在任务或者已在库内" });
}
// 获取容器内的物料信息
var inventories = _unitWork.Find<Inventory>(x => x.ContainerCode == containerCode)
.Select(x => new {
Id = x.ID,
ContainerCode = x.ContainerCode,
MaterialCode = x.MaterialCode,
MaterialName = x.MaterialName,
Qty = x.Qty
})
.ToList();
return Json(new {
Code = 200,
Msg = "验证成功",
Data = inventories
});
}
catch (Exception ex)
{
return Json(new { Code = 500, Msg = ex.Message });
}
}
}
}