盘点单

This commit is contained in:
2024-09-12 17:17:21 +08:00
parent c1b4d60778
commit 1173fb4e2a
5 changed files with 494 additions and 0 deletions

View File

@@ -2244,6 +2244,158 @@ namespace Tnb.WarehouseMgr
}
}
/// <summary>
/// 盘点单
/// </summary>
[HttpPost, NonUnify, AllowAnonymous]
public async Task<Entities.Dto.Outputs.Result> Inventorycheck(InventorycheckInput input)
{
LoggerErp2Mes.LogInformation($"【Inventorycheck】ERP传入数据:{JsonConvert.SerializeObject(input)}");
var db = _repository.AsSugarClient();
try
{
if (string.IsNullOrEmpty(input.erp_pk))
{
_LoggerErp2Mes.LogWarning($"【Inventorycheck】主表主键不能为空");
throw new AppFriendlyException($@"主表主键不能为空!", 500);
}
int count_erp_line_pk = input.details.Where(r => string.IsNullOrEmpty(r.erp_line_pk)).Count();
if (count_erp_line_pk > 0)
{
_LoggerErp2Mes.LogWarning($@"【Inventorycheck】子表主键不能为空");
throw new AppFriendlyException($@"子表主键不能为空!", 500);
}
var wmsInventorychecksDistinct = input.details.Select(r => new
{
material_id = r.material_code,
code_batch = r.code_batch,
}).Distinct();
if (wmsInventorychecksDistinct.Count() < input.details.Count)
{
_LoggerErp2Mes.LogWarning($@"【Inventorycheck】表体存在物料和批号重复的明细");
throw new AppFriendlyException($@"表体存在物料和批号重复的明细!", 500);
}
WmsErpWarehouserelaH wmsErpWarehouserelaH = await db.Queryable<WmsErpWarehouserelaH>().Where(r => r.erp_warehousecode == input.warehouse_code).FirstAsync();
if (wmsErpWarehouserelaH == null)
{
_LoggerErp2Mes.LogWarning($@"【Inventorycheck】不存在erp仓库类型{input.warehouse_code}对应wms系统的映射关系");
throw new AppFriendlyException($@"不存在erp仓库类型{input.warehouse_code}对应wms系统的映射关系", 500);
}
string warehouse_code = wmsErpWarehouserelaH.wms_warehousecode;
BasWarehouse warehouse = await db.Queryable<BasWarehouse>().Where(r => r.whcode == warehouse_code).FirstAsync();
if (warehouse == null)
{
_LoggerErp2Mes.LogWarning($"【Inventorycheck】无法查询到出库仓库{warehouse_code}的档案记录!");
return await ToApiResult(HttpStatusCode.InternalServerError, $"无法查询到出库仓库{warehouse_code}的档案记录!");
}
await db.Ado.BeginTranAsync();
// 判断是否重复传输
WmsInventorycheckH wmsInventorycheckHRep = await db.Queryable<WmsInventorycheckH>().Where(r => r.erp_pk == input.erp_pk).FirstAsync();
if (wmsInventorycheckHRep != null)
{
// 判断单据是否已经下发
List<WmsInventorycheckD> _wmsSaleDs = await db.Queryable<WmsInventorycheckD>().Where(r => r.bill_id == wmsInventorycheckHRep.id).ToListAsync();
bool isxf = _wmsSaleDs.Where(r => r.actual_qty > 0).Any();
if (isxf)
{
_LoggerErp2Mes.LogWarning($@"【Inventorycheck】wms已下发使用盘点单{input.erp_bill_code}");
throw new AppFriendlyException($@"wms已下发使用盘点单{input.erp_bill_code}", 500);
}
else // 删除数据重新插入
{
await db.Deleteable<WmsInventorycheckH>().Where(r => r.id == wmsInventorycheckHRep.id).ExecuteCommandAsync();
await db.Deleteable<WmsInventorycheckD>().Where(r => r.bill_id == wmsInventorycheckHRep.id).ExecuteCommandAsync();
}
}
WmsInventorycheckH wmsInventorycheckH = new WmsInventorycheckH();
string Code = await _billRuleService.GetBillNumber("CheckStockCode");
wmsInventorycheckH.create_id = WmsWareHouseConst.ErpUserId;
wmsInventorycheckH.create_time = DateTime.Now;
wmsInventorycheckH.org_id = WmsWareHouseConst.AdministratorOrgId;
wmsInventorycheckH.bill_code = Code;
wmsInventorycheckH.warehouse_id = warehouse.id;
wmsInventorycheckH.warehouse_code = warehouse.whcode;
wmsInventorycheckH.warehouse_name = warehouse.whname;
wmsInventorycheckH.dept_code = input.dept_code;
wmsInventorycheckH.biller = input.biller;
wmsInventorycheckH.erp_bill_code = input.erp_bill_code;
wmsInventorycheckH.erp_pk = input.erp_pk;
await db.Insertable(wmsInventorycheckH).ExecuteCommandAsync();
List<WmsInventorycheckD> wmsInventorycheckDs = new List<WmsInventorycheckD>();
foreach (var detail in input.details)
{
WmsInventorycheckD wmsInventorycheckD = new WmsInventorycheckD();
wmsInventorycheckD.bill_id = wmsInventorycheckH.id;
wmsInventorycheckD.create_id = WmsWareHouseConst.ErpUserId;
wmsInventorycheckD.create_time = DateTime.Now;
wmsInventorycheckD.org_id = WmsWareHouseConst.AdministratorOrgId;
wmsInventorycheckD.production_date = detail.production_date;
var material = await db.Queryable<BasMaterial>().Where(p => p.code == detail.material_code).FirstAsync();
if (material != null)
{
wmsInventorycheckD.material_id = material.id;
wmsInventorycheckD.material_code = material.code;
wmsInventorycheckD.material_name = material.name;
wmsInventorycheckD.material_specification = material.material_specification;
}
wmsInventorycheckD.qty = detail.qty;
wmsInventorycheckD.actual_qty = 0;
wmsInventorycheckD.code_batch = detail.code_batch;
wmsInventorycheckD.erp_line_pk = detail.erp_line_pk;
wmsInventorycheckD.lineno = detail.lineno;
var erpExtendField = await db.Queryable<ErpExtendField>().InnerJoin<DictionaryDataEntity>((a, b) => a.table_id == b.Id).Where((a, b) => b.EnCode == detail.unit_code).Select((a, b) => b).FirstAsync();
if (erpExtendField != null)
{
wmsInventorycheckD.unit_id = erpExtendField.Id;
wmsInventorycheckD.unit_code = erpExtendField.EnCode;
}
else
{
_LoggerErp2Mes.LogWarning($@"【Inventorycheck】表体明细中单位{detail.unit_code}在wms系统中未找到");
throw new AppFriendlyException($@"表体明细中单位{detail.unit_code}在wms系统中未找到", 500);
}
wmsInventorycheckDs.Add(wmsInventorycheckD);
}
await db.Insertable(wmsInventorycheckDs).ExecuteCommandAsync();
await db.Ado.CommitTranAsync();
LoggerErp2Mes.LogInformation($"【Inventorycheck】成功生成单据:{Code}");
return await ToApiResult(HttpStatusCode.OK, "成功");
}
catch (Exception ex)
{
LoggerErp2Mes.LogError($"【Inventorycheck】{ex.Message}");
LoggerErp2Mes.LogError($"【Inventorycheck】{ex.StackTrace}");
await db.Ado.RollbackTranAsync();
return await ToApiResult(HttpStatusCode.InternalServerError, ex.Message);
}
finally
{
}
}
protected Task<Entities.Dto.Outputs.Result> ToApiResult(HttpStatusCode statusCode, string msg)
{