erp材料出库单同步接口,材料出库与抽检业务编写,采购收货收货质检逻辑、采购订单同步接口完善、erp成品调拨入库同步
This commit is contained in:
223
WarehouseMgr/Tnb.WarehouseMgr/WmsRawmatOutstockService.cs
Normal file
223
WarehouseMgr/Tnb.WarehouseMgr/WmsRawmatOutstockService.cs
Normal file
@@ -0,0 +1,223 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Aop.Api.Domain;
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.Common.Dtos.VisualDev;
|
||||
using JNPF.Common.Enums;
|
||||
using JNPF.FriendlyException;
|
||||
using JNPF.Systems.Interfaces.System;
|
||||
using JNPF.VisualDev;
|
||||
using JNPF.VisualDev.Interfaces;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NPOI.HPSF;
|
||||
using Senparc.Weixin.Work.AdvancedAPIs.OaDataOpen;
|
||||
using SqlSugar;
|
||||
using Tnb.BasicData.Entities;
|
||||
using Tnb.WarehouseMgr.Entities;
|
||||
using Tnb.WarehouseMgr.Entities.Consts;
|
||||
using Tnb.WarehouseMgr.Entities.Dto;
|
||||
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
|
||||
using Tnb.WarehouseMgr.Entities.Entity;
|
||||
using Tnb.WarehouseMgr.Interfaces;
|
||||
|
||||
namespace Tnb.WarehouseMgr
|
||||
{
|
||||
/// <summary>
|
||||
/// 材料出库单
|
||||
/// </summary>
|
||||
[OverideVisualDev(ModuleConsts.MODULE_WmsRawmatOutstock_ID)]
|
||||
public class WmsRawmatOutstockService : BaseWareHouseService
|
||||
{
|
||||
private readonly ISqlSugarClient _db;
|
||||
private readonly IUserManager _userManager;
|
||||
private readonly IBillRullService _billRullService;
|
||||
private readonly IRunService _runService;
|
||||
private readonly IVisualDevService _visualDevService;
|
||||
private readonly IWmsPDAScanInStockService _wmsPDAScanInStock;
|
||||
private readonly IWareHouseService _wareHouseService;
|
||||
public WmsRawmatOutstockService(
|
||||
ISqlSugarRepository<WmsCarryH> repository,
|
||||
IUserManager userManager,
|
||||
IBillRullService billRullService,
|
||||
IRunService runService,
|
||||
IVisualDevService visualDevService,
|
||||
IWmsPDAScanInStockService wmsPDAScanInStock,
|
||||
IWareHouseService wareHouseService)
|
||||
{
|
||||
_db = repository.AsSugarClient();
|
||||
_userManager = userManager;
|
||||
_billRullService = billRullService;
|
||||
_runService = runService;
|
||||
_visualDevService = visualDevService;
|
||||
_wmsPDAScanInStock = wmsPDAScanInStock;
|
||||
_wareHouseService = wareHouseService;
|
||||
}
|
||||
|
||||
|
||||
public override async Task ModifyAsync(WareHouseUpInput input)
|
||||
{
|
||||
if (input == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(input));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 下发
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="AppFriendlyException"></exception>
|
||||
[HttpPost, NonUnify, AllowAnonymous]
|
||||
public async Task<Entities.Dto.Outputs.Result> Distribute(RawmatOutstockInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _s_taskExecuteSemaphore_YCLOutstock.WaitAsync();
|
||||
if (string.IsNullOrEmpty(input.source_id))
|
||||
{
|
||||
throw new AppFriendlyException("来源单据id不可为空", 500);
|
||||
}
|
||||
if (input.carrys.Count == 0)
|
||||
{
|
||||
throw new AppFriendlyException("至少选择一个载具", 500);
|
||||
}
|
||||
|
||||
WmsRawmatOutstockD wmsRawmatOutstockD = await _db.Queryable<WmsRawmatOutstockD>().FirstAsync(it => it.id == input.source_id);
|
||||
WmsRawmatOutstockH wmsRawmatOutstockH = await _db.Queryable<WmsRawmatOutstockH>().FirstAsync(it => it.id == wmsRawmatOutstockD.bill_id);
|
||||
|
||||
List<WmsCarryH> _items = await _db.Queryable<WmsCarryH>().Where(r => input.carrys.Select(c => c.carry_code).Contains(r.carry_code)).ToListAsync();
|
||||
|
||||
List<WmsCarryCode> wmsCarryCodes = await _db.Queryable<WmsCarryCode>().Where(r => _items.Select(c => c.id).Contains(r.carry_id)
|
||||
&& r.material_id == wmsRawmatOutstockD.material_id && r.code_batch == wmsRawmatOutstockD.batchno).ToListAsync();
|
||||
|
||||
List<WmsCarryH> items = new List<WmsCarryH>();
|
||||
|
||||
foreach (var carry in input.carrys.Select(c => c.carry_code))
|
||||
{
|
||||
items.Add(_items.Where(r => r.carry_code == carry).First());
|
||||
}
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item.is_lock == 1)
|
||||
{
|
||||
throw new AppFriendlyException($"载具{item.carry_code}已锁定", 500);
|
||||
}
|
||||
BasLocation carrryLoc = await _db.Queryable<BasLocation>().Where(r => r.id == item.location_id).FirstAsync();
|
||||
if (carrryLoc == null || carrryLoc.is_type != "0")
|
||||
{
|
||||
throw new Exception($"托盘{item.carry_code}已经不在存储库位中,不能出库!");
|
||||
}
|
||||
}
|
||||
|
||||
decimal? sign_qty = 0;
|
||||
|
||||
decimal qty = wmsCarryCodes.Sum(c => c.codeqty);
|
||||
if (wmsRawmatOutstockD.actual_outstock_qty + qty > wmsRawmatOutstockD.qty)
|
||||
{
|
||||
// 最后一个托盘的数量
|
||||
decimal lastCarryQty = wmsCarryCodes.Where(r => r.carry_id == items[items.Count - 1].id).Sum(r => r.codeqty);
|
||||
// 如果最后一个托盘数量小于超出的出库数量
|
||||
if (lastCarryQty < wmsRawmatOutstockD.actual_outstock_qty + qty - wmsRawmatOutstockD.qty)
|
||||
{
|
||||
throw new AppFriendlyException($"本次出库数量{qty}已超过可出库数量{wmsRawmatOutstockD.qty - wmsRawmatOutstockD.actual_outstock_qty} 且无需选择{items[items.Count - 1].carry_code}", 500);
|
||||
}
|
||||
// 签收数量
|
||||
sign_qty = lastCarryQty - (wmsRawmatOutstockD.actual_outstock_qty + qty - wmsRawmatOutstockD.qty);
|
||||
}
|
||||
|
||||
await _db.Ado.BeginTranAsync();
|
||||
|
||||
List<BasLocation> endLocations = new List<BasLocation>();
|
||||
|
||||
endLocations = _db.Queryable<BasLocation>().Where(r => _wareHouseService.GetFloor1YCLDBOutstockLocation().Contains(r.id) && r.is_lock == 0 && r.is_use == "0")
|
||||
.OrderBy("is_lock, task_nums, location_code").ToList();
|
||||
|
||||
if (endLocations.Count() < items.Count)
|
||||
{
|
||||
throw new AppFriendlyException($@"可用的终点库位数量为{endLocations.Count()}个 下发数量为{items.Count}个 请检查终点库位的锁定和占用状态", 500);
|
||||
}
|
||||
|
||||
foreach (var wmsCarryH in items)
|
||||
{
|
||||
BasLocation startLocation = await _db.Queryable<BasLocation>().Where(r => r.id == wmsCarryH.location_id).FirstAsync();
|
||||
|
||||
BasLocation endLocation = null;
|
||||
|
||||
endLocation = await _db.Queryable<BasLocation>().Where(r => _wareHouseService.GetFloor1YCLDBOutstockLocation().Contains(r.id) && r.is_lock == 0 && r.is_use == "0").OrderBy("is_lock, task_nums, location_code").FirstAsync();
|
||||
|
||||
if (endLocation == null)
|
||||
{
|
||||
throw new AppFriendlyException($@"没有可用的终点库位!请检查终点库位的锁定和占用状态", 500);
|
||||
}
|
||||
|
||||
WmsRawmatOutstockC wmsRawmatOutstockC = new WmsRawmatOutstockC();
|
||||
wmsRawmatOutstockC.bill_id = wmsRawmatOutstockH.id;
|
||||
wmsRawmatOutstockC.mat_bill_id = wmsRawmatOutstockD.id;
|
||||
wmsRawmatOutstockC.carry_id = wmsCarryH.id;
|
||||
wmsRawmatOutstockC.carry_code = wmsCarryH.carry_code;
|
||||
wmsRawmatOutstockC.create_id = input.create_id;
|
||||
wmsRawmatOutstockC.create_time = DateTime.Now;
|
||||
wmsRawmatOutstockC.startlocation_id = startLocation.id;
|
||||
wmsRawmatOutstockC.startlocation_code = startLocation.location_code;
|
||||
wmsRawmatOutstockC.endlocation_id = endLocation.id;
|
||||
wmsRawmatOutstockC.endlocation_code = endLocation.location_code;
|
||||
decimal carryQty = wmsCarryCodes.Where(r => r.carry_id == wmsCarryH.id).Sum(r => r.codeqty);
|
||||
wmsRawmatOutstockC.qty = carryQty;
|
||||
wmsRawmatOutstockC.sign_qty = wmsCarryH.id == items[items.Count - 1].id && sign_qty > 0 ? sign_qty : carryQty;
|
||||
|
||||
await _db.Insertable(wmsRawmatOutstockC).ExecuteCommandAsync();
|
||||
|
||||
CommonCreatePretaskInput commonCreatePretaskInput = new CommonCreatePretaskInput();
|
||||
commonCreatePretaskInput.startlocation_id = startLocation.id;
|
||||
commonCreatePretaskInput.endlocation_id = endLocation.id;
|
||||
commonCreatePretaskInput.task_type = WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_TYPE_ID;
|
||||
commonCreatePretaskInput.biz_type = WmsWareHouseConst.BIZTYPE_WmsRawmatOutstock_ID;
|
||||
commonCreatePretaskInput.source_id = input.source_id;
|
||||
commonCreatePretaskInput.carry_id = wmsCarryH.id;
|
||||
commonCreatePretaskInput.carry_code = wmsCarryH.carry_code;
|
||||
commonCreatePretaskInput.isExcuteMission = false;
|
||||
|
||||
Entities.Dto.Outputs.Result res = await _wareHouseService.CommonCreatePretask(commonCreatePretaskInput, _db);
|
||||
|
||||
if (res.code != HttpStatusCode.OK)
|
||||
{
|
||||
Logger.LogInformation($@"生成预任务失败");
|
||||
throw new AppFriendlyException($@"生成预任务失败", 500);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新子表已下发数量
|
||||
await _db.Updateable<WmsRawmatOutstockD>().SetColumns(r => r.actual_outstock_qty == r.actual_outstock_qty + qty).Where(r => r.id == input.source_id).ExecuteCommandAsync();
|
||||
|
||||
await _db.Ado.CommitTranAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _db.Ado.RollbackTranAsync();
|
||||
Logger.LogError("【Distribute】" + ex.Message);
|
||||
Logger.LogError("【Distribute】" + ex.StackTrace);
|
||||
return await ToApiResult(HttpStatusCode.InternalServerError, ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_ = _s_taskExecuteSemaphore_YCLOutstock.Release();
|
||||
InvokeGenPretaskExcute();
|
||||
}
|
||||
|
||||
return await ToApiResult(HttpStatusCode.OK, "成功");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user