using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
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.Common.Security;
using JNPF.FriendlyException;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Interfaces;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
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;
using Tnb.ProductionMgr.Entities.Entity;
using Tnb.BasicData;
using Tnb.BasicData.Interfaces;
namespace Tnb.WarehouseMgr
{
///
/// 出库签收
///
[OverideVisualDev(ModuleConsts.MODULE_WmsMaterialSignH_ID)]
public class WmsMaterialSignHService : 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 IWmsCarryUnbindService _wmsCarryUnbindService;
private readonly IWareHouseService _wareHouseService;
private readonly IOtherOutstockHService _otherOutstockHService;
private readonly IThirdApiRecordService _thirdApiRecordService;
public WmsMaterialSignHService(
ISqlSugarRepository repository,
IUserManager userManager,
IBillRullService billRullService,
IRunService runService,
IVisualDevService visualDevService,
IWmsPDAScanInStockService wmsPDAScanInStock,
IWmsCarryUnbindService wmsCarryUnbindService,
IThirdApiRecordService thirdApiRecordService,
IWareHouseService wareHouseService,
IOtherOutstockHService otherOutstockHService)
{
_db = repository.AsSugarClient();
_userManager = userManager;
_billRullService = billRullService;
_runService = runService;
_visualDevService = visualDevService;
_wmsPDAScanInStock = wmsPDAScanInStock;
_wmsCarryUnbindService = wmsCarryUnbindService;
_wareHouseService = wareHouseService;
_thirdApiRecordService = thirdApiRecordService;
_otherOutstockHService = otherOutstockHService;
}
///
/// 物料签收
///
///
///
///
///
[HttpPost]
public async Task MaterialSign(MaterialSignInput input)
{
string msg = "成功";
try
{
await s_MaterialSignSemaphore.WaitAsync();
if (input == null)
{
throw new ArgumentNullException(nameof(input));
}
if (input.details.Count == 0)
{
throw new AppFriendlyException($"【MaterialSign】未接收到物料列表数据,请重试", 500);
}
WmsCarryH wmsCarryH = await _db.Queryable().Where(r => r.carry_code == input.carry_code).FirstAsync();
if (wmsCarryH == null)
{
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}不存在", 500);
}
if (wmsCarryH.is_lock == 1)
{
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}已锁定", 500);
}
if (wmsCarryH.carry_status != "1")
{
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}未绑定物料", 500);
}
if (string.IsNullOrEmpty(wmsCarryH.location_id))
{
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}当前库位为空,无法签收", 500);
}
BasLocation carryLoc = await _db.Queryable().Where(r => r.id == wmsCarryH.location_id).FirstAsync();
if (carryLoc != null && carryLoc.is_type == "0")
{
throw new Exception($"托盘{wmsCarryH.carry_code}在存储库位中,不能签收!");
}
List wmsCarryCodes = _db.Queryable().Where(r => r.carry_id == wmsCarryH.id).ToList();
WmsMaterialSignH wmsMaterialSignH = new WmsMaterialSignH();
wmsMaterialSignH.create_id = input.create_id;
wmsMaterialSignH.create_time = DateTime.Now;
wmsMaterialSignH.carry_id = wmsCarryH.id;
wmsMaterialSignH.carry_code = wmsCarryH.carry_code;
WmsDistaskH wmsDistaskH = await _db.Queryable().Where(r => r.carry_id == wmsCarryH.id).OrderByDescending(r => r.id).FirstAsync();
if (wmsDistaskH == null)
{
throw new AppFriendlyException($"【MaterialSign】无法找到载具{input.carry_code}的执行任务", 500);
}
if (string.IsNullOrEmpty(wmsDistaskH.biz_type))
{
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}任务{wmsDistaskH.bill_code}的业务类型异常", 500);
}
wmsMaterialSignH.biz_type = wmsDistaskH.biz_type;
// wms其它出库记录主表
OtherOutstockAddDetailInput otherOutstockAddDetailInput = new OtherOutstockAddDetailInput();
switch (wmsDistaskH.biz_type)
{
case WmsWareHouseConst.BIZTYPE_WMSMATERIALTRANSFER_ID:
{
WmsMaterialTransferD wmsMaterialTransferD = await _db.Queryable().Where(r => r.id == wmsDistaskH.source_id).FirstAsync();
WmsMaterialTransfer wmsMaterialTransfer = await _db.Queryable().Where(r => r.id == wmsMaterialTransferD.bill_id).FirstAsync();
otherOutstockAddDetailInput.source_detail_id = wmsMaterialTransferD.id;
otherOutstockAddDetailInput.source_id = wmsMaterialTransfer.id;
otherOutstockAddDetailInput.source_bill_code = wmsMaterialTransfer.bill_code;
otherOutstockAddDetailInput.department = "";
otherOutstockAddDetailInput.salesman = "";
otherOutstockAddDetailInput.warehouse_code = wmsMaterialTransfer.warehouse_outstock;
break;
}
}
List wmsMaterialSignDs = new List();
List details = new List();
await _db.Ado.BeginTranAsync();
foreach (var item in input.details)
{
if (item.sign_qty < 0)
{
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}签收数量不能为空", 500);
}
WmsCarryCode wmsCarryCode = wmsCarryCodes.Where(r => r.id == item.carry_code_id).FirstOrDefault();
if (wmsCarryCode == null)
{
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code} 载具物料明细id {item.carry_code_id}不存在", 500);
}
if (item.sign_qty > wmsCarryCode.codeqty)
{
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}签收数量{item.sign_qty}超过了可签收数量{wmsCarryCode.codeqty}", 500);
}
WmsMaterialSignD wmsMaterialSignD = new WmsMaterialSignD();
wmsMaterialSignD.bill_id = wmsMaterialSignH.id;
wmsMaterialSignD.create_id = input.create_id;
wmsMaterialSignD.create_time = DateTime.Now;
wmsMaterialSignD.barcode = wmsCarryCode.barcode;
wmsMaterialSignD.material_id = wmsCarryCode.material_id;
BasMaterial basMaterial = await _db.Queryable().Where(r => r.code == wmsCarryCode.material_code).FirstAsync();
if (basMaterial == null)
{
throw new AppFriendlyException($"【MaterialSign】物料{wmsCarryCode.material_code} 基础资料不存在", 500);
}
wmsMaterialSignD.material_code = basMaterial.code;
wmsMaterialSignD.material_name = basMaterial.name;
wmsMaterialSignD.material_specification = basMaterial.material_specification;
wmsMaterialSignD.code_batch = wmsCarryCode.code_batch;
wmsMaterialSignD.qty = wmsCarryCode.codeqty;
wmsMaterialSignD.sign_qty = item.sign_qty;
wmsMaterialSignDs.Add(wmsMaterialSignD);
wmsCarryCode.codeqty = wmsCarryCode.codeqty - item.sign_qty;
if (wmsCarryCode.codeqty == 0)
{
CarryCodeUnbindCodeInput carryCodeUnbindCodeInput = new CarryCodeUnbindCodeInput();
carryCodeUnbindCodeInput.carry_code_id = wmsCarryCode.id;
await _wmsCarryUnbindService.CarryCodeUnbindCode(carryCodeUnbindCodeInput, _db);
}
await _db.Updateable().SetColumns(r => r.codeqty == wmsCarryCode.codeqty)
.Where(r => r.barcode == wmsCarryCode.barcode).ExecuteCommandAsync();
// wms其他出库记录明细
OtherOutstockAddDetailDetail otherOutstockAddDetailDetail = new OtherOutstockAddDetailDetail();
otherOutstockAddDetailDetail.actual_outstock_qty = item.sign_qty;
otherOutstockAddDetailDetail.auxprop_xph = wmsCarryCode.auxprop_xph;
otherOutstockAddDetailDetail.auxprop_gys = wmsCarryCode.auxprop_gys;
otherOutstockAddDetailDetail.qty = item.sign_qty;
otherOutstockAddDetailDetail.unit = wmsCarryCode.unit_id;
otherOutstockAddDetailDetail.batchno = wmsCarryCode.code_batch;
otherOutstockAddDetailDetail.material_code = basMaterial.code;
otherOutstockAddDetailDetail.material_id = basMaterial.id;
otherOutstockAddDetailDetail.material_name = basMaterial.name;
details.Add(otherOutstockAddDetailDetail);
}
if (!string.IsNullOrEmpty(otherOutstockAddDetailInput.warehouse_code))
{
otherOutstockAddDetailInput.details = details;
await _otherOutstockHService.AddDetail(otherOutstockAddDetailInput, _db);
}
// 如果没有条码 设置载具状态为空闲
List _wmsCarryCodes = _db.Queryable().Where(r => r.carry_id == wmsCarryH.id).ToList();
await _db.Updateable().SetColumns(r => r.carry_status == (_wmsCarryCodes.Count == 0 ? "0" : r.carry_status)).Where(r => r.id == wmsCarryH.id).ExecuteCommandAsync();
await _db.Updateable(wmsCarryCodes).ExecuteCommandAsync();
wmsMaterialSignH.warehouse_sign_id = carryLoc.wh_id;
if (_wmsCarryCodes.Count > 0)
{
// 没其他情况 先默认原材料
wmsMaterialSignH.warehouse_instock_id = WmsWareHouseConst.WAREHOUSE_YCL_ID;
switch (wmsMaterialSignH.warehouse_instock_id)
{
case WmsWareHouseConst.WAREHOUSE_YCL_ID:
{
try
{
await _wareHouseService.s_taskExecuteSemaphore_YCLInstock.WaitAsync();
InStockStrategyQuery inStockStrategyInput = new() { warehouse_id = "1", Size = 1, AvoidBusyPassage = true, Region_id = WmsWareHouseConst.REGION_YCLCache_ID };
List endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput);
if (endLocations.Count == 0)
{
throw new AppFriendlyException("没有可以回库的库位", 500);
}
CommonCreatePretaskInput commonCreatePretaskInput = new CommonCreatePretaskInput();
commonCreatePretaskInput.startlocation_id = carryLoc.id;
commonCreatePretaskInput.endlocation_id = endLocations[0].id;
commonCreatePretaskInput.carry_id = wmsCarryH.id;
commonCreatePretaskInput.carry_code = wmsCarryH.carry_code;
commonCreatePretaskInput.task_type = WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID;
commonCreatePretaskInput.biz_type = WmsWareHouseConst.BIZTYPE_WmsMaterialSign_ID;
Logger.LogInformation($"【MaterialSign】 开始生成原材料仓回库任务 起点{carryLoc.location_code} 终点{endLocations[0].location_code} 托盘 {wmsCarryH.carry_code}");
Entities.Dto.Outputs.Result res = await _wareHouseService.CommonCreatePretask(commonCreatePretaskInput, _db);
if (res.code != HttpStatusCode.OK)
{
throw new AppFriendlyException(res.msg, 500);
}
}
catch (Exception ex)
{
throw;
}
finally
{
_wareHouseService.s_taskExecuteSemaphore_YCLInstock.Release();
}
break;
}
}
}
await _db.Updateable().SetColumns(r => r.is_use == "0").Where(r => r.id == carryLoc.id).ExecuteCommandAsync();
await _db.Insertable(wmsMaterialSignH).ExecuteCommandAsync();
await _db.Insertable(wmsMaterialSignDs).ExecuteCommandAsync();
#region 更新库存表
//switch (wmsDistaskH.biz_type)
//{
// // 原材料调拨出库
// case WmsWareHouseConst.BIZTYPE_WmsRawmatTransferoutstock_ID:
// {
// //WmsRawmatTransferoutstockD wmsRawmatTransferoutstockD = await _db.Queryable().Where(r => r.id == wmsDistaskH.source_id).FirstAsync();
// //WmsRawmatTransferoutstockH wmsRawmatTransferoutstockH = await _db.Queryable().Where(r => r.id == wmsRawmatTransferoutstockD.bill_id).FirstAsync();
// //WmsTransferOrderH wmsTransferOrderH = await _db.Queryable().Where(r => r.id == wmsRawmatTransferoutstockH.transfer_order_id).FirstAsync();
// break;
// }
// case WmsWareHouseConst.BIZTYPE_WMSMATERIALTRANSFER_ID:
// {
// WmsMaterialTransferD wmsMaterialTransferD = await _db.Queryable().Where(r => r.id == wmsDistaskH.source_id).FirstAsync();
// WmsMaterialTransfer wmsMaterialTransfer = await _db.Queryable().Where(r => r.id == wmsMaterialTransferD.bill_id).FirstAsync();
// List wmsStockReportCodes = new List();
// foreach (var wmsMaterialSignD in wmsMaterialSignDs)
// {
// WmsStockReportCode wmsStockReportCode = new WmsStockReportCode();
// wmsStockReportCode.material_id = wmsMaterialSignD.id;
// wmsStockReportCode.material_code = wmsMaterialSignD.material_code;
// wmsStockReportCode.barcode = wmsMaterialSignD.barcode;
// wmsStockReportCode.code_batch = wmsMaterialSignD.code_batch;
// wmsStockReportCode.codeqty = wmsMaterialSignD.qty - wmsMaterialSignD.sign_qty;
// wmsStockReportCode.unit_id = wmsMaterialSignD.unit_id;
// wmsStockReportCode.create_id = _userManager?.User?.Id;
// wmsStockReportCode.create_time = DateTime.Now;
// wmsStockReportCode.warehouse_id = wmsMaterialTransfer.warehouse_instock;
// wmsStockReportCode.
// wmsStockReportCodes.Add(wmsStockReportCode);
// }
// //// todo 对接其它出库单 出库数量为签收数量
// //if (!_wareHouseService.GetFloor1WXSGWOutstockLocation().Contains(carryLoc.id))
// //{
// // // todo 对接其它入库单 入库数量为签收数量
// //}
// break;
// }
//}
#endregion
#region 上传bip
switch (wmsDistaskH.biz_type)
{
// 原材料调拨出库
case WmsWareHouseConst.BIZTYPE_WmsRawmatTransferoutstock_ID:
{
WmsRawmatTransferoutstockD wmsRawmatTransferoutstockD = await _db.Queryable().Where(r => r.id == wmsDistaskH.source_id).FirstAsync();
WmsRawmatTransferoutstockH wmsRawmatTransferoutstockH = await _db.Queryable().Where(r => r.id == wmsRawmatTransferoutstockD.bill_id).FirstAsync();
WmsTransferOrderH wmsTransferOrderH = await _db.Queryable().Where(r => r.id == wmsRawmatTransferoutstockH.transfer_order_id).FirstAsync();
// 出库仓库 wmsRawmatTransferoutstockD.warehouse_code
// 入库仓库 wmsRawmatTransferoutstockD.warehouse_instock_code
// 调拨订单 wmsRawmatTransferoutstockH.erp_pk
foreach(var wmsMaterialSignD in wmsMaterialSignDs)
{
// 出库数量wmsMaterialSignD.sign_qty
}
//自制的不调erp接口
if (string.IsNullOrEmpty(wmsTransferOrderH.erp_pk))
{
break;
}
List materialIds = wmsMaterialSignDs.Select(x => x.material_id).Distinct().ToList();
List erpWarehouserelaHs = await _db.Queryable().Where(x=>x.id!=null).ToListAsync();
string supplierId = WmsWareHouseConst.TIANYIGONGYINGSHANG_ID;
List tableIds = new List();
// tableIds.Add( wmsMaterialSignH.create_id);
tableIds.Add(WmsWareHouseConst.AdministratorOrgId);
tableIds.AddRange(materialIds);
tableIds.Add(supplierId);
tableIds.Add(wmsRawmatTransferoutstockD.unit_id);
string userId = input.create_id ?? WmsWareHouseConst.AdministratorUserId;
tableIds.Add(userId);
List erpExtendFields = await _db.Queryable().Where(x => tableIds.Contains(x.table_id)).ToListAsync();
// string erpCreateId = erpExtendFields.Find(x => x.table_id == userId)?.user_id ?? WmsWareHouseConst.ERPUSERID;
string erpCreateId = WmsWareHouseConst.ERPUSERID;
ErpExtendField erpOrg = erpExtendFields.Find(x => x.table_id == (WmsWareHouseConst.AdministratorOrgId));
string nowStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
List wmsErpWarehouserelaHs = await _db.Queryable().Where(x=>!SqlFunc.IsNullOrEmpty(x.id)).ToListAsync();
List> requestData = new List>();
Dictionary erpRequestData = new Dictionary();
erpRequestData.Add("billmaker", erpCreateId);
// erpRequestData.Add("cbiztype", "");
erpRequestData.Add("cdptid",null);//部门先写死
erpRequestData.Add("cdptvid",null);//部门先写死
erpRequestData.Add("corpoid", erpOrg.corpoid);
erpRequestData.Add("corpvid", erpOrg.corpvid);
erpRequestData.Add("cotherwhid", wmsRawmatTransferoutstockD.warehouse_instock_code);
erpRequestData.Add("cwarehouseid", wmsErpWarehouserelaHs.Find(x=>x.erp_warehousecode==wmsRawmatTransferoutstockD.erp_wh_type)?.erp_warehouseid ?? "");
erpRequestData.Add("creationtime", nowStr);
erpRequestData.Add("creator", erpCreateId);
erpRequestData.Add("ctrantypeid", "0001H11000000000D32A");//先写死
erpRequestData.Add("vtrantypecode", "4Y-01");//先写死
erpRequestData.Add("dbilldate", nowStr);
erpRequestData.Add("dmakedate", nowStr);
erpRequestData.Add("fbillflag", 1);
erpRequestData.Add("fmodetype", 0);
erpRequestData.Add("ntotalnum", input.details.Sum(x=>x.sign_qty));
erpRequestData.Add("pk_org", erpOrg.pk_org);
erpRequestData.Add("pk_org_v", erpOrg.pk_org_v);
erpRequestData.Add("pk_group", erpOrg.pk_group);
erpRequestData.Add("vbillcode", wmsRawmatTransferoutstockH.bill_code);
erpRequestData.Add("csourcebillhid", wmsTransferOrderH.erp_pk);
List> erpRequestDataDetails = new List>();
foreach(var item in wmsMaterialSignDs)
{
WmsCarryCode wmsCarryCode = await _db.Queryable().Where(x=>x.barcode==item.barcode).FirstAsync();
string gysid = wmsCarryCode?.auxprop_gys ?? "";
ErpExtendField supplier = await _db.Queryable().Where(x=>x.supplier_id==gysid).FirstAsync();
// 出库数量wmsMaterialSignD.sign_qty
erpRequestDataDetails.Add(new Dictionary()
{
["cbodytranstypecode"] = "4Y-01",
["cbodywarehouseid"] = wmsErpWarehouserelaHs.Find(x=>x.erp_warehousecode==wmsRawmatTransferoutstockD.erp_wh_type)?.erp_warehouseid ?? "",
["cmaterialoid"] = erpExtendFields.Find(x => x.table_id == item.material_id)?.cmaterialoid ?? "",
["cmaterialvid"] = erpExtendFields.Find(x => x.table_id == item.material_id)?.cmaterialvid ?? "",
["corpoid"] = erpOrg.corpoid,
["corpvid"] = erpOrg.corpvid,
["crowno"] = wmsRawmatTransferoutstockD.lineno,
["csourcebillhid"] = wmsTransferOrderH?.erp_pk ?? "",
["csourcebillbid"] = wmsRawmatTransferoutstockD?.erp_line_pk ?? "",
["vsourcebillcode"] = wmsTransferOrderH?.erp_bill_code ?? "",
["vsourcerowno"] = wmsRawmatTransferoutstockD?.lineno ?? "",
["cunitid"] = erpExtendFields.Find(x => x.table_id == wmsRawmatTransferoutstockD.unit_id)?.cunitid ?? "",
["cvendorid"] = gysid,
["cvendorvid"] = supplier?.supplier_vid ?? gysid,
["dbizdate"] = wmsTransferOrderH.create_time.Value.ToString("yyyy-MM-dd HH:mm:ss"),
["dplanarrivedate"] = nowStr,
["dplanoutdate"] = nowStr,
["nnum"] = item.sign_qty,
["nshouldnum"] = item.sign_qty,
["pk_group"] = erpOrg.pk_group,
["pk_org"] = erpOrg.pk_org,
["pk_org_v"] = erpOrg.pk_org_v,
["vbatchcode"] = item.code_batch,
["vfree1"] = wmsCarryCode?.auxprop_xph,
});
}
erpRequestData.Add("dtls", erpRequestDataDetails);
requestData.Add(erpRequestData);
BasFactoryConfig config = await _db.Queryable().FirstAsync(x => x.enabled == 1 && x.key == FactoryConfigConst.BIPURL);
ThirdWebapiRecord thirdWebapiRecord = new ThirdWebapiRecord();
thirdWebapiRecord.id = SnowflakeIdHelper.NextId();
thirdWebapiRecord.third_name = WmsWareHouseConst.BIP;
thirdWebapiRecord.name = "调拨出库";
thirdWebapiRecord.method = "POST";
//thirdWebapiRecord.url = config.value + "uapws/rest/transOut/save";
thirdWebapiRecord.url = WmsWareHouseConst.BIP_DOMAIN + "uapws/rest/transOut/save";
thirdWebapiRecord.request_data = JsonConvert.SerializeObject(requestData);
thirdWebapiRecord.create_time = DateTime.Now;
thirdWebapiRecord.remark = $"【WmsMaterialSignHService MaterialSign】原材料调拨出库wms_rawmat_transferoutstock_h:{wmsRawmatTransferoutstockH.bill_code}";
await _db.Insertable(thirdWebapiRecord).ExecuteCommandAsync();
BasFactoryConfig callErp = await _db.Queryable().FirstAsync(x => x.enabled == 1 && x.key == FactoryConfigConst.CALLERP);
if(callErp.value=="1"){
ThirdResult thirdResult = await _thirdApiRecordService.Send(new List { thirdWebapiRecord }, "自动", _db);
msg = thirdResult.msgResult;
}
break;
}
case WmsWareHouseConst.BIZTYPE_WMSMATERIALTRANSFER_ID:
{
Logger.LogInformation("【WmsMaterialSignHService ModifyAsync】同步其它出库单到erp...");
// todo 对接其它出库单 出库数量为签收数量
WmsMaterialTransferD wmsMaterialTransferd = await _db.Queryable().Where(r => r.id == wmsDistaskH.source_id).FirstAsync();
WmsMaterialTransfer wmsMaterialTransfer = await _db.Queryable().Where(r => r.id == wmsMaterialTransferd.bill_id).FirstAsync();
//自制的不调erp接口
if (string.IsNullOrEmpty(wmsMaterialTransfer.erp_bill_code))
{
break;
}
List dList = await _db.Queryable().Where(x => x.bill_id == wmsMaterialTransferd.bill_id).OrderBy(x => x.id).ToListAsync();
DictionaryDataEntity unitData = await _db.Queryable()
.LeftJoin((x, y) => x.Id == y.DictionaryTypeId)
.Where((x, y) => x.EnCode == DictConst.MeasurementUnit && (y.EnCode == wmsMaterialTransferd.unit_id || y.Id== wmsMaterialTransferd.unit_id))
.Select((x, y) => y)
.FirstAsync();
string unitId = unitData?.Id ?? "";
List ids = new List();
// ids.Add(wmsMaterialTransfer.create_id);
ids.Add(WmsWareHouseConst.AdministratorOrgId);
ids.Add(wmsMaterialTransfer.warehouse_outstock);
ids.Add(wmsMaterialTransfer.warehouse_instock);
ids.Add(wmsMaterialTransferd.material_id);
ids.Add(wmsCarryCodes[0].auxprop_gys);
ids.Add(unitId);
string userId = wmsMaterialTransfer.create_id ?? WmsWareHouseConst.AdministratorUserId;
ids.Add(userId);
List erpExtendFields = await _db.Queryable().Where(x => ids.Contains(x.table_id)).ToListAsync();
ErpExtendField erpOrg = erpExtendFields.Find(x => x.table_id == (wmsMaterialTransfer.org_id ?? WmsWareHouseConst.AdministratorOrgId));
//string erpCreateId = erpExtendFields.Find(x => x.table_id == userId)?.user_id ?? WmsWareHouseConst.ERPUSERID;
string erpCreateId = WmsWareHouseConst.ERPUSERID;
List erpWarehouserelaHs = await _db.Queryable().Where(x => x.id != null).ToListAsync();
// BasWarehouse outWarehouse = await _db.Queryable().SingleAsync(x => x.id == wmsMaterialTransfer.warehouse_outstock);
// BasWarehouse inWarehouse = await _db.Queryable().SingleAsync(x => x.id == wmsMaterialTransfer.warehouse_instock);
// string inwhcode = inWarehouse?.whcode ?? "";
// string outwhcode = outWarehouse?.whcode ?? "";
List> requestData = new List>();
Dictionary erpRequestData = new Dictionary();
string nowStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
erpRequestData.Add("billmaker", erpCreateId);
erpRequestData.Add("cdptid", null);// 先写死
erpRequestData.Add("cdptvid", null);// 先写死
erpRequestData.Add("corpoid", erpOrg.corpoid);
erpRequestData.Add("corpvid", erpOrg.corpoid);
erpRequestData.Add("cothercalbodyoid", erpOrg.pk_org);
// erpRequestData.Add("cotherwhid", erpExtendFields.Find(x => x.table_id == wmsMaterialTransfer.warehouse_instock)?.cotherwhid ?? "");
erpRequestData.Add("cotherwhid", erpWarehouserelaHs.Find(x => x.erp_warehousecode == wmsMaterialTransfer.erp_warehouse_instock)?.erp_warehouseid ?? "");
erpRequestData.Add("creationtime", nowStr);
erpRequestData.Add("creator", erpCreateId);
erpRequestData.Add("ctrantypeid", "0001H11000000000D31X");
erpRequestData.Add("cwarehouseid", erpWarehouserelaHs.Find(x => x.erp_warehousecode == wmsMaterialTransfer.erp_warehouse_outstock)?.erp_warehouseid ?? "");
erpRequestData.Add("dbilldate", nowStr);
erpRequestData.Add("dmakedate", nowStr);
erpRequestData.Add("ntotalnum", wmsMaterialSignDs.Sum(r => r.sign_qty));
erpRequestData.Add("pk_group", erpOrg.pk_group);
erpRequestData.Add("pk_org", erpOrg.pk_org);
erpRequestData.Add("pk_org_v", erpOrg.pk_org_v);
erpRequestData.Add("vbillcode", wmsMaterialTransfer.bill_code);
erpRequestData.Add("vtrantypecode", "4I-02");//其他出库 先写死
erpRequestData.Add("csourcebillhid", wmsMaterialTransfer.erp_pk);//其他出库 先写死
List> erpRequestDataDetails = new List>();
erpRequestDataDetails.Add(new Dictionary()
{
["cbodytranstypecode"] = "4I-02",
["cbodywarehouseid"] = erpWarehouserelaHs.Find(x => x.erp_warehousecode == wmsMaterialTransfer.erp_warehouse_outstock)?.erp_warehouseid ?? "",
["cmaterialoid"] = erpExtendFields.Find(x => x.table_id == wmsMaterialTransferd.material_id)?.cmaterialoid ?? "",
["cmaterialvid"] = erpExtendFields.Find(x => x.table_id == wmsMaterialTransferd.material_id)?.cmaterialvid ?? "",
["corpoid"] = erpOrg.corpoid,
["corpvid"] = erpOrg.corpvid,
["crowno"] = wmsMaterialTransferd.lineno,
["csourcebillbid"] = wmsMaterialTransferd.erp_line_pk,
["csourcebillhid"] = wmsMaterialTransfer.erp_pk,
["cunitid"] = erpExtendFields.Find(x => x.table_id == unitId)?.cunitid ?? "",
["cvendorid"] = erpExtendFields.Find((x=>x.table_id==wmsCarryCodes[0].auxprop_gys))?.supplier_id ?? "",
["cvendorvid"] = erpExtendFields.Find((x=>x.table_id==wmsCarryCodes[0].auxprop_gys))?.supplier_vid ?? "",
["dbizdate"] = nowStr,
["nnum"] = wmsMaterialSignDs.Sum(r => r.sign_qty),
["nshouldnum"] = wmsMaterialTransferd.qty,
["pk_group"] = erpOrg.pk_group,
["pk_org"] = erpOrg.pk_org,
["pk_org_v"] = erpOrg.pk_org_v,
["vbatchcode"] = wmsMaterialTransferd.code_batch,
["vfree1"] = wmsCarryCodes[0].auxprop_xph,
});
erpRequestData.Add("dtls", erpRequestDataDetails);
requestData.Add(erpRequestData);
BasFactoryConfig config = await _db.Queryable().FirstAsync(x => x.enabled == 1 && x.key == FactoryConfigConst.BIPURL);
ThirdWebapiRecord thirdWebapiRecord = new ThirdWebapiRecord();
thirdWebapiRecord.id = SnowflakeIdHelper.NextId();
thirdWebapiRecord.third_name = WmsWareHouseConst.BIP;
thirdWebapiRecord.name = "其它出库";
thirdWebapiRecord.method = "POST";
// thirdWebapiRecord.url = config.value + "uapws/rest/generalout/save";
thirdWebapiRecord.url = WmsWareHouseConst.BIP_DOMAIN + "uapws/rest/generalout/save";
thirdWebapiRecord.request_data = JsonConvert.SerializeObject(requestData);
thirdWebapiRecord.create_time = DateTime.Now;
thirdWebapiRecord.remark = $"【WmsMaterialSignHService MaterialSign】原材料转库单wms_material_transfer:{wmsMaterialTransfer.bill_code},erp转库单:{wmsMaterialTransfer.erp_bill_code}";
await _db.Insertable(thirdWebapiRecord).ExecuteCommandAsync();
Logger.LogInformation("【WmsMaterialSignHService ModifyAsync】同步其它出库单到erp成功");
BasFactoryConfig callErp2 = await _db.Queryable().FirstAsync(x => x.enabled == 1 && x.key == FactoryConfigConst.CALLERP);
if(callErp2.value=="1"){
ThirdResult thirdResult = await _thirdApiRecordService.Send(new List { thirdWebapiRecord }, "自动", _db);
Logger.LogInformation($"【WmsMaterialSignHService ModifyAsync】thirdResult {JsonConvert.SerializeObject(thirdResult)}");
// if (thirdResult.Code!=200)
// {
// throw Oops.Bah(thirdResult.msgResult ?? "erp接口调用失败");
// }
msg = thirdResult.msgResult;
}
if (wmsMaterialTransfer.type == WmsWareHouseConst.MATERIALTRANSFER_JZGLRK_CODE
|| wmsMaterialTransfer.type == WmsWareHouseConst.MATERIALTRANSFER_QTCRK_CODE
|| wmsMaterialTransfer.type == WmsWareHouseConst.MATERIALTRANSFER_PACKING_CODE)
{
// todo 对接其它入库单 入库数量为签收数量
List> requestData2 = new List>();
Dictionary erpRequestData2 = new Dictionary();
erpRequestData2.Add("approver", erpCreateId);
erpRequestData2.Add("billmaker", erpCreateId);
erpRequestData2.Add("corpoid", erpOrg.corpoid);
erpRequestData2.Add("corpvid", erpOrg.corpvid);
erpRequestData2.Add("creationtime", nowStr);
erpRequestData2.Add("creator", erpCreateId);
erpRequestData2.Add("ctrantypeid", "0001H11000000000D310");
// erpRequestData2.Add("cwarehouseid",erpExtendFields.Find(x=>x.table_id==wmsMaterialTransfer.warehouse_instock)?.cotherwhid ?? "");
// erpRequestData2.Add("cwarehouseid", wmsMaterialTransfer.erp_warehouse_instock);
erpRequestData2.Add("cwarehouseid", erpWarehouserelaHs.Find(x => x.erp_warehousecode == wmsMaterialTransfer.erp_warehouse_instock)?.erp_warehouseid ?? "");
erpRequestData2.Add("cwhsmanagerid", "");
erpRequestData2.Add("dbilldate", nowStr);
erpRequestData2.Add("dmakedate", nowStr);
erpRequestData2.Add("ntotalnum", wmsMaterialSignDs.Sum(x => x.sign_qty));
erpRequestData2.Add("pk_group", erpOrg.pk_group);
erpRequestData2.Add("pk_org", erpOrg.pk_org);
erpRequestData2.Add("pk_org_v", erpOrg.pk_org_v);
erpRequestData2.Add("vbillcode", wmsMaterialTransfer.bill_code);
erpRequestData2.Add("vtrantypecode", "4A-02");
List> erpRequestDataDetails2 = new List>();
erpRequestDataDetails2.Add(new Dictionary()
{
["cbodytranstypecode"] = "4A-02",
["cbodywarehouseid"] = erpWarehouserelaHs.Find(x => x.erp_warehousecode == wmsMaterialTransfer.erp_warehouse_instock)?.erp_warehouseid ?? "",
//["cgeneralbid"] = erpWarehouserelaHs.Find(x => x.wms_warehousecode == whcode)?.erp_warehouseid ?? "",
["cgeneralbid"] = null,
["cgeneralhid"] = null,
["cmaterialoid"] = erpExtendFields.Find(x => x.table_id == wmsMaterialTransferd.material_id)?.cmaterialoid ?? "",
["cmaterialvid"] = erpExtendFields.Find(x => x.table_id == wmsMaterialTransferd.material_id)?.cmaterialvid ?? "",
["corpoid"] = erpOrg.corpoid,
["corpvid"] = erpOrg.corpvid,
// ["crowno"] = (wmsMaterialTransferDs.FindIndex(x => x.id == item.id) + 1) * 10,
["crowno"] = wmsMaterialTransferd.lineno,
["cunitid"] = erpExtendFields.Find(x => x.table_id == unitId)?.cunitid ?? "",
["cvendorid"] = erpExtendFields.Find(x => x.table_id == wmsCarryCodes[0]?.auxprop_gys)?.supplier_id ?? "",
["cvendorvid"] = erpExtendFields.Find(x => x.table_id == wmsCarryCodes[0]?.auxprop_gys)?.supplier_vid ?? "",
["dbizdate"] = nowStr,
["nnum"] = wmsMaterialTransferd.qty,
["pk_group"] = erpOrg.pk_group,
["pk_org"] = erpOrg.pk_org,
["pk_org_v"] = erpOrg.pk_org_v,
["csourcebillbid"] = wmsMaterialTransferd.erp_line_pk,
["csourcebillhid"] = wmsMaterialTransfer.erp_pk,
["vbatchcode"] = wmsMaterialTransferd.code_batch,
["vfree1"] = wmsCarryCodes[0].auxprop_xph,
});
erpRequestData2.Add("dtls", erpRequestDataDetails2);
requestData2.Add(erpRequestData2);
ThirdWebapiRecord thirdWebapiRecord2 = new ThirdWebapiRecord();
thirdWebapiRecord2.id = SnowflakeIdHelper.NextId();
thirdWebapiRecord2.third_name = WmsWareHouseConst.BIP;
thirdWebapiRecord2.name = "其它入库";
thirdWebapiRecord2.method = "POST";
// thirdWebapiRecord.url = config.value + "uapws/rest/generalin/save";
thirdWebapiRecord2.url = WmsWareHouseConst.BIP_DOMAIN + "uapws/rest/generalin/save";
thirdWebapiRecord2.request_data = JsonConvert.SerializeObject(requestData2);
thirdWebapiRecord2.create_time = DateTime.Now;
thirdWebapiRecord2.remark = $"【WmsMaterialSignHService MaterialSign】原材料转库单wms_material_transfer:{wmsMaterialTransfer.bill_code},erp转库单:{wmsMaterialTransfer.erp_bill_code}";
await _db.Insertable(thirdWebapiRecord2).ExecuteCommandAsync();
Logger.LogInformation("【WmsMaterialSignHService ModifyAsync】同步其它入库单到erp成功");
if(callErp2.value=="1"){
ThirdResult thirdResult = await _thirdApiRecordService.Send(new List { thirdWebapiRecord2 }, "自动", _db);
// if (thirdResult.Code!=200)
// {
// throw Oops.Bah(thirdResult.msgResult);
// }
msg = thirdResult.msgResult;
}
}
break;
}
}
#endregion
await _db.Ado.CommitTranAsync();
}
catch (Exception ex)
{
Logger.LogError($"【MaterialSign】 {ex.Message}");
Logger.LogError($"【MaterialSign】 {ex.StackTrace}");
await _db.Ado.RollbackTranAsync();
throw new AppFriendlyException($"【MaterialSign】物料签收失败 {ex.Message}", 500);
}
finally
{
s_MaterialSignSemaphore.Release();
}
return msg;
}
}
}