feat(材料管理):增加调拨出库
This commit is contained in:
@@ -8,6 +8,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Transactions;
|
||||
|
||||
|
||||
namespace BLL
|
||||
@@ -54,6 +55,8 @@ namespace BLL
|
||||
(string.IsNullOrEmpty(table.ReqUnitId) || x.ReqUnitId.Contains(table.ReqUnitId)) &&
|
||||
(string.IsNullOrEmpty(table.UnitWorkId) || x.WeldTaskId.Contains(table.UnitWorkId)) &&
|
||||
(string.IsNullOrEmpty(table.WeldTaskId) || x.WeldTaskId.Contains(table.WeldTaskId)) &&
|
||||
(string.IsNullOrEmpty(table.TransferWarehouseId) || x.TransferWarehouseId == table.TransferWarehouseId) &&
|
||||
(string.IsNullOrEmpty(table.TransferBatchId) || x.TransferBatchId == table.TransferBatchId) &&
|
||||
(table.InOutType == null || x.InOutType == table.InOutType) &&
|
||||
(table.TypeInt == null || x.TypeInt == table.TypeInt) &&
|
||||
(table.Category == null || x.Category == table.Category) &&
|
||||
@@ -90,7 +93,9 @@ namespace BLL
|
||||
WarehouseMan = x.WarehouseMan,
|
||||
WarehouseDate = x.WarehouseDate,
|
||||
WarehouseManName = warehouseperson.PersonName,
|
||||
PipeLineSortIndex = y.PipeLineSortIndex
|
||||
PipeLineSortIndex = y.PipeLineSortIndex,
|
||||
TransferWarehouseId = x.TransferWarehouseId,
|
||||
TransferBatchId = x.TransferBatchId
|
||||
|
||||
}
|
||||
;
|
||||
@@ -153,6 +158,8 @@ namespace BLL
|
||||
WarehouseDate = x.WarehouseDate,
|
||||
WarehouseManName = x.WarehouseManName,
|
||||
PipeLineSortIndex = x.PipeLineSortIndex,
|
||||
TransferWarehouseId = x.TransferWarehouseId,
|
||||
TransferBatchId = x.TransferBatchId,
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
@@ -203,7 +210,9 @@ namespace BLL
|
||||
AuditDate2 = x.AuditDate2,
|
||||
WarehouseMan = x.WarehouseMan,
|
||||
WarehouseDate = x.WarehouseDate,
|
||||
WarehouseManName = x.WarehouseManName
|
||||
WarehouseManName = x.WarehouseManName,
|
||||
TransferWarehouseId = x.TransferWarehouseId,
|
||||
TransferBatchId = x.TransferBatchId
|
||||
};
|
||||
}
|
||||
|
||||
@@ -721,6 +730,8 @@ namespace BLL
|
||||
AuditDate2 = newtable.AuditDate2,
|
||||
WarehouseMan = newtable.WarehouseMan,
|
||||
WarehouseDate = newtable.WarehouseDate,
|
||||
TransferWarehouseId = newtable.TransferWarehouseId,
|
||||
TransferBatchId = newtable.TransferBatchId,
|
||||
};
|
||||
Funs.DB.Tw_InOutPlanMaster.InsertOnSubmit(table);
|
||||
Funs.DB.SubmitChanges();
|
||||
@@ -753,6 +764,8 @@ namespace BLL
|
||||
table.AuditDate2 = newtable.AuditDate2;
|
||||
table.WarehouseMan = newtable.WarehouseMan;
|
||||
table.WarehouseDate = newtable.WarehouseDate;
|
||||
table.TransferWarehouseId = newtable.TransferWarehouseId;
|
||||
table.TransferBatchId = newtable.TransferBatchId;
|
||||
Funs.DB.SubmitChanges();
|
||||
}
|
||||
|
||||
@@ -932,6 +945,22 @@ namespace BLL
|
||||
}
|
||||
public static string GetDataInCusBillCode(string projectid, string unitcode, string typeString, string unitWorkCode = "", string Category = "")
|
||||
{
|
||||
if (typeString == TwConst.TypeInt.调拨出库.ToString())
|
||||
{
|
||||
string datePrefix = string.Format("{0:yyyyMMdd}", DateTime.Now);
|
||||
string codePrefix = datePrefix + "-TR-";
|
||||
var transferCodes = GetModle(new Tw_InOutMasterOutput
|
||||
{
|
||||
ProjectId = projectid,
|
||||
TypeInt = (int)TwConst.TypeInt.调拨出库
|
||||
})
|
||||
.Where(x => x.CusBillCode != null && x.CusBillCode.StartsWith(codePrefix))
|
||||
.Select(x => x.CusBillCode)
|
||||
.ToList();
|
||||
|
||||
int maxSequence = transferCodes.Select(GetTransferSequence).DefaultIfEmpty(0).Max();
|
||||
return string.Format("{0}-TR-{1}-OUT-AP", datePrefix, (maxSequence + 1).ToString("D2"));
|
||||
}
|
||||
if (typeString == TwConst.TypeInt.其他入库.ToString())
|
||||
{
|
||||
//生成规则是20240919-unitcode-AP-GR01
|
||||
@@ -998,6 +1027,220 @@ namespace BLL
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据调拨出库申请编号生成同批次调拨入库申请编号
|
||||
/// </summary>
|
||||
public static string GetTransferInCusBillCode(string outCusBillCode)
|
||||
{
|
||||
return (outCusBillCode ?? string.Empty).Replace("-OUT-AP", "-IN-AP");
|
||||
}
|
||||
|
||||
private static int GetTransferSequence(string cusBillCode)
|
||||
{
|
||||
string[] parts = (cusBillCode ?? string.Empty).Split('-');
|
||||
int sequence;
|
||||
return parts.Length >= 3 && int.TryParse(parts[2], out sequence) ? sequence : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提交调拨出库草稿,并在同一事务内生成配对的调拨入库申请
|
||||
/// </summary>
|
||||
public static ResponeData SubmitTransferPlan(string outPlanId, string targetWarehouseId, IDictionary<string, decimal> detailPlanNums)
|
||||
{
|
||||
ResponeData result = new ResponeData();
|
||||
try
|
||||
{
|
||||
TransactionOptions options = new TransactionOptions
|
||||
{
|
||||
IsolationLevel = System.Transactions.IsolationLevel.Serializable,
|
||||
Timeout = TransactionManager.DefaultTimeout
|
||||
};
|
||||
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
|
||||
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
|
||||
{
|
||||
Tw_InOutPlanMaster outPlan = db.Tw_InOutPlanMaster.FirstOrDefault(x => x.Id == outPlanId);
|
||||
if (outPlan == null || outPlan.TypeInt != (int)TwConst.TypeInt.调拨出库)
|
||||
{
|
||||
result.code = 0;
|
||||
result.message = "未找到有效的调拨出库申请!";
|
||||
return result;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(outPlan.TransferBatchId))
|
||||
{
|
||||
Tw_InOutPlanMaster existingInPlan = db.Tw_InOutPlanMaster.FirstOrDefault(x =>
|
||||
x.TransferBatchId == outPlan.TransferBatchId && x.InOutType == (int)TwConst.InOutType.入库);
|
||||
if (existingInPlan != null)
|
||||
{
|
||||
result.data = existingInPlan.Id;
|
||||
result.message = "调拨入库申请已生成。";
|
||||
scope.Complete();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
if (outPlan.State != (int)TwConst.State.待提交)
|
||||
{
|
||||
result.code = 0;
|
||||
result.message = "只有待提交的调拨出库申请允许提交!";
|
||||
return result;
|
||||
}
|
||||
if (string.IsNullOrEmpty(targetWarehouseId) || targetWarehouseId == outPlan.WarehouseId)
|
||||
{
|
||||
result.code = 0;
|
||||
result.message = "调出仓库和调入仓库不能为空且不能相同!";
|
||||
return result;
|
||||
}
|
||||
|
||||
Base_Warehouse sourceWarehouse = db.Base_Warehouse.FirstOrDefault(x => x.WarehouseId == outPlan.WarehouseId && x.ProjectId == outPlan.ProjectId);
|
||||
Base_Warehouse targetWarehouse = db.Base_Warehouse.FirstOrDefault(x => x.WarehouseId == targetWarehouseId && x.ProjectId == outPlan.ProjectId);
|
||||
if (sourceWarehouse == null || targetWarehouse == null)
|
||||
{
|
||||
result.code = 0;
|
||||
result.message = "调出仓库或调入仓库不属于当前项目!";
|
||||
return result;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(outPlan.TransferBatchId))
|
||||
{
|
||||
outPlan.TransferBatchId = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
List<Tw_InOutPlanDetail> outDetails = db.Tw_InOutPlanDetail.Where(x => x.InOutPlanMasterId == outPlan.Id).ToList();
|
||||
if (outDetails.Count == 0)
|
||||
{
|
||||
result.code = 0;
|
||||
result.message = "请添加调拨明细!";
|
||||
return result;
|
||||
}
|
||||
if (detailPlanNums == null || detailPlanNums.Keys.Except(outDetails.Select(x => x.Id)).Any())
|
||||
{
|
||||
result.code = 0;
|
||||
result.message = "调拨明细数据已变化,请刷新后重新提交!";
|
||||
return result;
|
||||
}
|
||||
foreach (Tw_InOutPlanDetail detail in outDetails)
|
||||
{
|
||||
decimal planNum;
|
||||
if (!detailPlanNums.TryGetValue(detail.Id, out planNum) || planNum <= 0 || string.IsNullOrEmpty(detail.MaterialCode))
|
||||
{
|
||||
result.code = 0;
|
||||
result.message = "调拨明细的材料主编码不能为空,计划数量必须大于0!";
|
||||
return result;
|
||||
}
|
||||
detail.PlanNum = planNum;
|
||||
}
|
||||
|
||||
// 调拨提交按材料主编码汇总校验,防止同一材料拆行后绕过库存上限。
|
||||
var planTotals = outDetails.GroupBy(x => x.MaterialCode).Select(x => new
|
||||
{
|
||||
MaterialCode = x.Key,
|
||||
PlanNum = x.Sum(y => y.PlanNum ?? 0)
|
||||
}).ToList();
|
||||
var stockTotals = db.Tw_MaterialStock
|
||||
.Where(x => x.ProjectId == outPlan.ProjectId && x.WarehouseId == outPlan.WarehouseId)
|
||||
.GroupBy(x => x.PipeLineMatCode)
|
||||
.Select(x => new
|
||||
{
|
||||
MaterialCode = x.Key,
|
||||
StockNum = x.Sum(y => y.StockNum ?? 0)
|
||||
}).ToList();
|
||||
foreach (var planTotal in planTotals)
|
||||
{
|
||||
decimal stockNum = stockTotals.Where(x => x.MaterialCode == planTotal.MaterialCode).Select(x => x.StockNum).FirstOrDefault();
|
||||
if (planTotal.PlanNum > stockNum)
|
||||
{
|
||||
result.code = 0;
|
||||
result.message = string.Format("材料[{0}]计划调拨数量{1}超过当前库存{2}!", planTotal.MaterialCode, planTotal.PlanNum, stockNum);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
outPlan.TransferWarehouseId = targetWarehouseId;
|
||||
outPlan.InOutType = (int)TwConst.InOutType.出库;
|
||||
outPlan.State = (int)TwConst.State.待审核;
|
||||
|
||||
string inPlanId = Guid.NewGuid().ToString();
|
||||
Tw_InOutPlanMaster inPlan = new Tw_InOutPlanMaster
|
||||
{
|
||||
Id = inPlanId,
|
||||
ProjectId = outPlan.ProjectId,
|
||||
CusBillCode = GetTransferInCusBillCode(outPlan.CusBillCode),
|
||||
WarehouseId = targetWarehouse.WarehouseId,
|
||||
WarehouseCode = targetWarehouse.WarehouseName,
|
||||
TransferWarehouseId = sourceWarehouse.WarehouseId,
|
||||
TransferBatchId = outPlan.TransferBatchId,
|
||||
Source = outPlan.Source,
|
||||
InOutType = (int)TwConst.InOutType.入库,
|
||||
TypeInt = (int)TwConst.TypeInt.调拨入库,
|
||||
State = (int)TwConst.State.待审核,
|
||||
CreateMan = outPlan.CreateMan,
|
||||
CreateDate = outPlan.CreateDate,
|
||||
WeldTaskId = outPlan.WeldTaskId,
|
||||
ReqUnitId = outPlan.ReqUnitId,
|
||||
Category = outPlan.Category,
|
||||
Remark = outPlan.Remark
|
||||
};
|
||||
db.Tw_InOutPlanMaster.InsertOnSubmit(inPlan);
|
||||
|
||||
List<Tw_InOutPlanDetail> inDetails = outDetails.Select(x => new Tw_InOutPlanDetail
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
InOutPlanMasterId = inPlanId,
|
||||
MaterialCode = x.MaterialCode,
|
||||
PlanNum = x.PlanNum,
|
||||
SortIndex = x.SortIndex
|
||||
}).ToList();
|
||||
db.Tw_InOutPlanDetail.InsertAllOnSubmit(inDetails);
|
||||
db.SubmitChanges();
|
||||
scope.Complete();
|
||||
result.data = inPlanId;
|
||||
result.message = "调拨出入库申请生成成功。";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string existingInPlanId = GetExistingTransferInPlanId(outPlanId);
|
||||
if (!string.IsNullOrEmpty(existingInPlanId))
|
||||
{
|
||||
result.code = 1;
|
||||
result.data = existingInPlanId;
|
||||
result.message = "调拨入库申请已生成。";
|
||||
return result;
|
||||
}
|
||||
result.code = 0;
|
||||
result.message = "调拨申请提交失败:" + ex.Message;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 并发提交发生唯一键冲突或死锁时,回查已成功生成的配对入库申请。
|
||||
/// </summary>
|
||||
private static string GetExistingTransferInPlanId(string outPlanId)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
|
||||
{
|
||||
string transferBatchId = db.Tw_InOutPlanMaster
|
||||
.Where(x => x.Id == outPlanId)
|
||||
.Select(x => x.TransferBatchId)
|
||||
.FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(transferBatchId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return db.Tw_InOutPlanMaster
|
||||
.Where(x => x.TransferBatchId == transferBatchId && x.InOutType == (int)TwConst.InOutType.入库)
|
||||
.Select(x => x.Id)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据任务单生成出库计划单
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user