342 lines
13 KiB
C#
342 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using WalkingTec.Mvvm.Core;
|
|
using WalkingTec.Mvvm.Core.Extensions;
|
|
using HHWMS.Web;
|
|
using HHWMS.Web.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Z.EntityFramework.Plus;
|
|
|
|
namespace HHWMS.Web
|
|
{
|
|
public partial class ShipmentHeaderVM : BaseCRUDVM<ShipmentHeader>
|
|
{
|
|
public ShipmentDetailListVM Details = new ShipmentDetailListVM();
|
|
public List<ComboSelectListItem> ShipTypes { get; set; }
|
|
|
|
public ShipmentHeaderVM()
|
|
{
|
|
}
|
|
|
|
protected override void InitVM()
|
|
{
|
|
Details.CopyContext(this);
|
|
Details.Searcher.HeaderId = Entity.ID;
|
|
|
|
var CurrentCulture = Wtm.CurrentCulture;
|
|
var dictDatas = DC.Set<SysDictData>().ToList();
|
|
ShipTypes = dictDatas.Where(x => x.DictType == "ShipType").Select(x => new ComboSelectListItem
|
|
{
|
|
Text = CurrentCulture == "zh" ? x.DictLabel : x.DictLabelUs,
|
|
Value = x.DictValue
|
|
}).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分解出库单
|
|
/// </summary>
|
|
/// <returns>处理结果</returns>
|
|
public ResponseResult<dynamic> DecomposeShipment()
|
|
{
|
|
try
|
|
{
|
|
using var transaction = DC.BeginTransaction();
|
|
try
|
|
{
|
|
var details = DC.Set<ShipmentDetail>()
|
|
.Where(x => x.HeaderId == Entity.ID && x.Status != ShipStatusEnum.分配完成)
|
|
.ToList();
|
|
|
|
if (!details.Any())
|
|
{
|
|
return ResponseResult<dynamic>.Error("没有需要分配的出库单明细");
|
|
}
|
|
|
|
foreach (var detail in details)
|
|
{
|
|
var availableInventories = DC.Set<Inventory>()
|
|
.Where(x => x.MaterialCode == detail.MaterialCode
|
|
&& x.TaskStatus == InventoryTaskStatus.无任务
|
|
&& x.Qty > x.TaskQty
|
|
)
|
|
.OrderBy(x => x.CreateTime)
|
|
.ToList();
|
|
|
|
decimal remainingQty = (decimal)(detail.Qty - (detail.DividedQty ?? 0));
|
|
decimal dividedQty = 0;
|
|
|
|
foreach (var inventory in availableInventories)
|
|
{
|
|
if (remainingQty <= 0) break;
|
|
|
|
var currentInventory = DC.Set<Inventory>()
|
|
.FirstOrDefault(x => x.ID == inventory.ID);
|
|
|
|
if (currentInventory == null || currentInventory.UpdateVersion != inventory.UpdateVersion)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
decimal availableQty = (decimal)(currentInventory.Qty - (currentInventory.TaskQty ?? 0));
|
|
if (availableQty <= 0) continue;
|
|
|
|
decimal qtyToDivide = Math.Min(remainingQty, availableQty);
|
|
|
|
// 更新库存分配数量
|
|
currentInventory.TaskQty = (currentInventory.TaskQty ?? 0) + qtyToDivide;
|
|
currentInventory.Status = InventoryTaskStatus.任务中;
|
|
currentInventory.UpdateVersion++;
|
|
DC.UpdateEntity(currentInventory);
|
|
|
|
remainingQty -= qtyToDivide;
|
|
dividedQty += qtyToDivide;
|
|
|
|
/*var allocation = new InventoryAllocation
|
|
{
|
|
InventoryId = currentInventory.ID,
|
|
ShipmentDetailId = detail.ID,
|
|
DividedQty = qtyToDivide,
|
|
Status = ShipStatusEnum.分配完成.ToString()
|
|
};
|
|
DC.AddEntity(allocation);*/
|
|
}
|
|
|
|
if (dividedQty > 0)
|
|
{
|
|
var currentDetail = DC.Set<ShipmentDetail>()
|
|
.FirstOrDefault(x => x.ID == detail.ID);
|
|
|
|
if (currentDetail != null && currentDetail.UpdateVersion == detail.UpdateVersion)
|
|
{
|
|
currentDetail.DividedQty = (currentDetail.DividedQty ?? 0) + dividedQty;
|
|
currentDetail.Status = currentDetail.DividedQty >= currentDetail.Qty ?
|
|
ShipStatusEnum.分配完成 :
|
|
ShipStatusEnum.分配中;
|
|
currentDetail.UpdateVersion++;
|
|
DC.UpdateEntity(currentDetail);
|
|
}
|
|
}
|
|
}
|
|
|
|
var currentHeader = DC.Set<ShipmentHeader>()
|
|
.FirstOrDefault(x => x.ID == Entity.ID);
|
|
|
|
if (currentHeader != null && currentHeader.UpdateVersion == Entity.UpdateVersion)
|
|
{
|
|
var allDetails = DC.Set<ShipmentDetail>()
|
|
.Where(x => x.HeaderId == Entity.ID)
|
|
.ToList();
|
|
|
|
bool allDivided = allDetails.All(x => x.Status == ShipStatusEnum.分配完成);
|
|
|
|
currentHeader.FirstStatus = allDivided ?
|
|
ShipStatusEnum.分配完成 :
|
|
ShipStatusEnum.分配中;
|
|
currentHeader.UpdateVersion++;
|
|
DC.UpdateEntity(currentHeader);
|
|
}
|
|
|
|
DC.SaveChanges();
|
|
transaction.Commit();
|
|
|
|
var unallocatedDetails = details.Where(x => x.Status != ShipStatusEnum.分配完成).ToList();
|
|
if (unallocatedDetails.Any())
|
|
{
|
|
var errorMsg = string.Join("\n", unallocatedDetails.Select(x =>
|
|
$"物料{x.MaterialCode}缺少{x.Qty - (x.DividedQty ?? 0)}个"));
|
|
return ResponseResult<dynamic>.Error($"部分物料库存不足,请检查:\n{errorMsg}");
|
|
}
|
|
|
|
return ResponseResult<dynamic>.Success(new
|
|
{
|
|
message = "出库单分解完成",
|
|
headerId = Entity.ID,
|
|
status = Entity.FirstStatus,
|
|
allocatedDetails = details.Select(x => new
|
|
{
|
|
x.ID,
|
|
x.MaterialCode,
|
|
x.Qty,
|
|
DividedQty = x.DividedQty,
|
|
x.Status
|
|
}).ToList()
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
throw new Exception($"处理出库单时发生错误: {ex.Message}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ResponseResult<dynamic>.Error($"分解出库单失败:{ex.Message}");
|
|
}
|
|
}
|
|
|
|
public override void DoAdd()
|
|
{
|
|
base.DoAdd();
|
|
}
|
|
|
|
public override void DoEdit(bool updateAllFields = false)
|
|
{
|
|
base.DoEdit(updateAllFields);
|
|
}
|
|
|
|
public override void DoDelete()
|
|
{
|
|
base.DoDelete();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取分配库存对应的货架列表
|
|
/// </summary>
|
|
/// <returns>货架列表</returns>
|
|
public ResponseResult<List<string>> GetAllocatedLocations()
|
|
{
|
|
try
|
|
{
|
|
// 获取已分配的出库单明细
|
|
var details = DC.Set<ShipmentDetail>()
|
|
.Where(x => x.HeaderId == Entity.ID
|
|
&& (x.Status == ShipStatusEnum.分配完成 || x.Status == ShipStatusEnum.分配中))
|
|
.ToList();
|
|
|
|
if (!details.Any())
|
|
{
|
|
return ResponseResult<List<string>>.Error("没有已分配的出库单明细");
|
|
}
|
|
|
|
// 获取明细对应的所有库存记录
|
|
var allocatedInventories = DC.Set<Inventory>()
|
|
.Where(x => x.TaskStatus == InventoryTaskStatus.任务中
|
|
&& details.Select(d => d.MaterialCode).Contains(x.MaterialCode))
|
|
.ToList();
|
|
|
|
// 获取去重后的货架编码列表
|
|
var locations = allocatedInventories
|
|
.Where(x => !string.IsNullOrEmpty(x.LocationCode))
|
|
.Select(x => x.LocationCode)
|
|
.Distinct()
|
|
.OrderBy(x => x)
|
|
.ToList();
|
|
|
|
if (!locations.Any())
|
|
{
|
|
return ResponseResult<List<string>>.Error("未找到相关库存的货架信息");
|
|
}
|
|
|
|
// 返回货架列表,并包含统计信息
|
|
return ResponseResult<List<string>>.Success(locations);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ResponseResult<List<string>>.Error($"获取货架列表失败:{ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取分配库存的详细信息
|
|
/// </summary>
|
|
/// <returns>库存详细信息</returns>
|
|
public ResponseResult<List<InventoryLocationInfo>> GetAllocatedInventoryDetails()
|
|
{
|
|
try
|
|
{
|
|
// 获取已分配的出库单明细
|
|
var details = DC.Set<ShipmentDetail>()
|
|
.Where(x => x.HeaderId == Entity.ID
|
|
&& (x.Status == ShipStatusEnum.分配完成 || x.Status == ShipStatusEnum.分配中))
|
|
.ToList();
|
|
|
|
if (!details.Any())
|
|
{
|
|
return ResponseResult<List<InventoryLocationInfo>>.Error("没有已分配的出库单明细");
|
|
}
|
|
|
|
// 获取明细对应的所有库存记录,并按货架分组
|
|
var inventoryGroups = DC.Set<Inventory>()
|
|
.Where(x => x.TaskStatus == InventoryTaskStatus.任务中
|
|
&& details.Select(d => d.MaterialCode).Contains(x.MaterialCode))
|
|
.GroupBy(x => x.LocationCode)
|
|
.Select(g => new InventoryLocationInfo
|
|
{
|
|
LocationCode = g.Key,
|
|
Inventories = g.Select(inv => new InventoryInfo
|
|
{
|
|
MaterialCode = inv.MaterialCode,
|
|
MaterialName = inv.MaterialName,
|
|
Qty = (decimal)inv.Qty,
|
|
TaskQty = inv.TaskQty ?? 0,
|
|
BatchCode = inv.Batch
|
|
}).ToList()
|
|
})
|
|
.OrderBy(x => x.LocationCode)
|
|
.ToList();
|
|
|
|
if (!inventoryGroups.Any())
|
|
{
|
|
return ResponseResult<List<InventoryLocationInfo>>.Error("未找到相关库存信息");
|
|
}
|
|
|
|
return ResponseResult<List<InventoryLocationInfo>>.Success(inventoryGroups);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ResponseResult<List<InventoryLocationInfo>>.Error($"获取库存详细信息失败:{ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 库存位置信息
|
|
/// </summary>
|
|
public class InventoryLocationInfo
|
|
{
|
|
/// <summary>
|
|
/// 货架编码
|
|
/// </summary>
|
|
public string LocationCode { get; set; }
|
|
|
|
/// <summary>
|
|
/// 该货架上的库存列表
|
|
/// </summary>
|
|
public List<InventoryInfo> Inventories { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 库存信息
|
|
/// </summary>
|
|
public class InventoryInfo
|
|
{
|
|
/// <summary>
|
|
/// 物料编码
|
|
/// </summary>
|
|
public string MaterialCode { get; set; }
|
|
|
|
/// <summary>
|
|
/// 物料名称
|
|
/// </summary>
|
|
public string MaterialName { get; set; }
|
|
|
|
/// <summary>
|
|
/// 总数量
|
|
/// </summary>
|
|
public decimal Qty { get; set; }
|
|
|
|
/// <summary>
|
|
/// 任务数量
|
|
/// </summary>
|
|
public decimal TaskQty { get; set; }
|
|
|
|
/// <summary>
|
|
/// 批次号
|
|
/// </summary>
|
|
public string BatchCode { get; set; }
|
|
}
|
|
}
|
|
}
|