diff --git a/HHWMS.Web/Areas/Api/IWCSController.cs b/HHWMS.Web/Areas/Api/IWCSController.cs index c72ef68..230fab1 100644 --- a/HHWMS.Web/Areas/Api/IWCSController.cs +++ b/HHWMS.Web/Areas/Api/IWCSController.cs @@ -399,65 +399,8 @@ namespace HHWMS.Web inventory.Qty = inventory.Qty > 0 ? inventory.Qty : 0; } - if (!string.IsNullOrEmpty(inventory.ReceiptDetailId)) - { - // ERP相关 入库报工 - var finishInRequest = new FinishInRequest - { - Main = new FinishInMain - { - ErpCode = "003", // 账套号 - InCode = _unitWork.GetSerialNumber("RW"), // 报工单号(上游单号) - InDate = (inventory.CreateTime ?? DateTime.Now) - .ToString("yyyy-MM-dd"), // 报工日期 - StepCode = inventory.BatchCode, // 工序(批次编码) - WorkerCode = inventory.CreateBy, // 工人编码(创建人) - Maker = _unitWork.FindSingle(x => - x.ITCode == inventory.CreateBy).Name // 制单人(校验员) - }, - Details = new List() - }; - if (task.TaskType == TaskTypeEnum.入库) - { - // 根据库存构建明细 - finishInRequest.Details.Add(new FinishInDetail - { - MoDetailId = inventory.ReceiptDetailId, // 生产订单明细(ERP明细ID) - InvCode = inventory.MaterialCode, // 物料号 - Qty = inventory.Qty ?? 0 // 完工数量 - }); - } - else if (task.TaskType == TaskTypeEnum.出库) - { - finishInRequest.Details.Add(new FinishInDetail - { - MaterialDetails = new List - { - new MaterialDetail - { - MoDetailId = inventory.ReceiptDetailId, // 生产订单明细ID - InvCode = inventory.MaterialCode, // 物料号 - Qty = (decimal)inventory.Qty // 完工数量(空值兜底为0) - } - } - }); - } - - // 调用ERP接口 - var apiResult = await Wtm.CallAPI("ERP", "FinishIn", - HttpMethodEnum.POST, - finishInRequest); - - if (apiResult.Data != null && apiResult.Data.Result) - { - inventory.UploadErpFlag = 1; - } - else - { - inventory.UploadErpFlag = 0; - } - } - + var invVM = Wtm.CreateVM(); + await invVM.ProcessErpFinishIn(inventory, task.TaskType); if (task.TaskType != TaskTypeEnum.移库) { @@ -466,7 +409,7 @@ namespace HHWMS.Web invTran.Type = task.TaskType == TaskTypeEnum.入库 ? InventoryTranType.入库 : InventoryTranType.出库; - + invTran.CreateTime = DateTime.Now; invTrans.Add(invTran); } else @@ -474,11 +417,13 @@ namespace HHWMS.Web // 创建库存交易记录 放到任务完成,计算数量信息 var invTran = _mapper.Map(inventory); invTran.Type = InventoryTranType.入库; + invTran.CreateTime = DateTime.Now; invTrans.Add(invTran); var invTran2 = _mapper.Map(inventory); invTran2.Type = InventoryTranType.出库; + invTran2.CreateTime = DateTime.Now; invTrans.Add(invTran2); } diff --git a/HHWMS.Web/Areas/Inventory/Controllers/InventoryController.cs b/HHWMS.Web/Areas/Inventory/Controllers/InventoryController.cs index 7b94f45..66e6eda 100644 --- a/HHWMS.Web/Areas/Inventory/Controllers/InventoryController.cs +++ b/HHWMS.Web/Areas/Inventory/Controllers/InventoryController.cs @@ -545,10 +545,10 @@ namespace HHWMS.Web.Controllers { return FFResult().Alert("所选库存存在任务,请检查后重试!").RefreshGrid(); } - if (string.IsNullOrEmpty(inv.SourceCode)) - { - return FFResult().Alert("所选库存没关联订单,无需回传!").RefreshGrid(); - } + // if (string.IsNullOrEmpty(inv.ReceiptDetailId)) + // { + // return FFResult().Alert("所选库存没关联订单,无需回传!").RefreshGrid(); + // } if (inv.UploadErpFlag is 1) { return FFResult().Alert("所选库存已报工回传,无需再次回传!").RefreshGrid(); diff --git a/HHWMS.Web/Areas/Inventory/ViewModels/InventoryTransactionVMs/InventoryTransactionListVM.cs b/HHWMS.Web/Areas/Inventory/ViewModels/InventoryTransactionVMs/InventoryTransactionListVM.cs index a17c810..6addfb7 100644 --- a/HHWMS.Web/Areas/Inventory/ViewModels/InventoryTransactionVMs/InventoryTransactionListVM.cs +++ b/HHWMS.Web/Areas/Inventory/ViewModels/InventoryTransactionVMs/InventoryTransactionListVM.cs @@ -8,6 +8,23 @@ namespace HHWMS.Web { public partial class InventoryTransactionListVM : BasePagedListVM { + public List ZoneCodeList { get; set; } + public List BillTypeList { get; set; } + + protected override void InitVM() + { + ZoneCodeList = new List + { + new ComboSelectListItem { Text = "成品", Value = "1028" }, + new ComboSelectListItem { Text = "线边", Value = "1027" }, + new ComboSelectListItem { Text = "模具", Value = "1025" } + }; + BillTypeList = new List + { + new ComboSelectListItem { Text = "入库", Value = "in" }, + new ComboSelectListItem { Text = "出库", Value = "out" } + }; + } protected override List InitGridAction() { return new List @@ -25,28 +42,41 @@ namespace HHWMS.Web protected override IEnumerable> InitGridHeader() { + InitVM(); return new List>{ - this.MakeGridHeader(x => x.ZoneCode), - this.MakeGridHeader(x => x.Type), + this.MakeGridHeader(x => x.ZoneCode) + .SetFormat((a, b) => + { + var data = ZoneCodeList.FirstOrDefault(x => x.Value.ToString() == a.ZoneCode); + return data?.Text ?? a.ZoneCode; + }), + this.MakeGridHeader(x => x.Type) + .SetFormat((a, b) => + { + var data = BillTypeList.FirstOrDefault(x => x.Value.ToString() == a.Type); + return data?.Text ?? a.Type; + }), + this.MakeGridHeader(x => x.MaterialCode), + this.MakeGridHeader(x => x.MaterialType).SetWidth(100), + this.MakeGridHeader(x => x.MaterialName), + this.MakeGridHeader(x => x.Qty) + .SetFormat((a, b) => { return (int?)a.Qty ?? 0; }), + this.MakeGridHeader(x => x.CreateTime), this.MakeGridHeader(x => x.ContainerCode), this.MakeGridHeader(x => x.LocationCode), - this.MakeGridHeader(x => x.MaterialCode), - this.MakeGridHeader(x => x.MaterialType), - this.MakeGridHeader(x => x.MaterialName), - this.MakeGridHeader(x => x.MaterialSpec), - this.MakeGridHeader(x => x.MaterialUnit), + this.MakeGridHeader(x => x.MaterialSpec).SetHide(), + this.MakeGridHeader(x => x.MaterialUnit).SetHide(), this.MakeGridHeader(x => x.Batch), this.MakeGridHeader(x => x.Lot), - this.MakeGridHeader(x => x.ManufactureDate), - this.MakeGridHeader(x => x.ExpirationDate), + this.MakeGridHeader(x => x.ManufactureDate).SetHide(), + this.MakeGridHeader(x => x.ExpirationDate).SetHide(), this.MakeGridHeader(x => x.CompanyCode), this.MakeGridHeader(x => x.SupplierCode), - this.MakeGridHeader(x => x.Qty), - this.MakeGridHeader(x => x.BillType), - this.MakeGridHeader(x => x.SourceCode), - this.MakeGridHeader(x => x.BillCode), - this.MakeGridHeader(x => x.BillDetailId), - this.MakeGridHeader(x => x.Status), + this.MakeGridHeader(x => x.BillType).SetHide(), + this.MakeGridHeader(x => x.SourceCode).SetHide(), + this.MakeGridHeader(x => x.BillCode).SetHide(), + this.MakeGridHeader(x => x.BillDetailId).SetHide(), + this.MakeGridHeader(x => x.Status).SetHide(), this.MakeGridHeaderAction(width: 200) }; } @@ -56,11 +86,11 @@ namespace HHWMS.Web var query = DC.Set() .CheckContain(Searcher.ZoneCode, x => x.ZoneCode) .CheckContain(Searcher.Type, x => x.Type) - .CheckContain(Searcher.ContainerCode, x => x.ContainerCode) - .CheckContain(Searcher.LocationCode, x => x.LocationCode) .CheckContain(Searcher.MaterialCode, x => x.MaterialCode) .CheckContain(Searcher.MaterialType, x => x.MaterialType) .CheckContain(Searcher.MaterialName, x => x.MaterialName) + .CheckContain(Searcher.ContainerCode, x => x.ContainerCode) + .CheckContain(Searcher.LocationCode, x => x.LocationCode) .CheckContain(Searcher.MaterialSpec, x => x.MaterialSpec) .CheckContain(Searcher.MaterialUnit, x => x.MaterialUnit) .CheckContain(Searcher.Batch, x => x.Batch) @@ -69,6 +99,7 @@ namespace HHWMS.Web .CheckContain(Searcher.SourceCode, x => x.SourceCode) .CheckContain(Searcher.BillCode, x => x.BillCode) .CheckContain(Searcher.Status, x => x.Status) + .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false) .Select(x => new InventoryTransaction_View { ID = x.ID, @@ -93,6 +124,7 @@ namespace HHWMS.Web BillCode = x.BillCode, BillDetailId = x.BillDetailId, Status = x.Status, + CreateTime = x.CreateTime, }) .OrderBy(x => x.ID); return query; diff --git a/HHWMS.Web/Areas/Inventory/ViewModels/InventoryTransactionVMs/InventoryTransactionSearcher.cs b/HHWMS.Web/Areas/Inventory/ViewModels/InventoryTransactionVMs/InventoryTransactionSearcher.cs index 53c293c..74ee41a 100644 --- a/HHWMS.Web/Areas/Inventory/ViewModels/InventoryTransactionVMs/InventoryTransactionSearcher.cs +++ b/HHWMS.Web/Areas/Inventory/ViewModels/InventoryTransactionVMs/InventoryTransactionSearcher.cs @@ -42,6 +42,9 @@ namespace HHWMS.Web public String BillCode { get; set; } [Display(Name = "Inventory.Status")] public String Status { get; set; } + + [Display(Name = "Inventory.CreateTime")] + public DateRange CreateTime { get; set; } protected override void InitVM() { diff --git a/HHWMS.Web/Areas/Inventory/ViewModels/InventoryVMs/InventoryListVM.cs b/HHWMS.Web/Areas/Inventory/ViewModels/InventoryVMs/InventoryListVM.cs index 0d66b41..46e3942 100644 --- a/HHWMS.Web/Areas/Inventory/ViewModels/InventoryVMs/InventoryListVM.cs +++ b/HHWMS.Web/Areas/Inventory/ViewModels/InventoryVMs/InventoryListVM.cs @@ -62,41 +62,87 @@ namespace HHWMS.Web protected override IEnumerable> InitGridHeader() { - return new List>{ - this.MakeGridHeader(x => x.Name_view).SetWidth(80), - this.MakeGridHeader(x => x.ContainerCode).SetWidth(90), - this.MakeGridHeader(x => x.LocationCode).SetWidth(130), - this.MakeGridHeader(x => x.MaterialCode).SetWidth(90), - this.MakeGridHeader(x => x.MaterialType).SetWidth(100), - this.MakeGridHeader(x => x.Qty).SetWidth(100) - .SetFormat((a, b) => - { - return (int?)a.Qty ?? 0; - }), - this.MakeGridHeader(x => x.SupplierCode).SetWidth(100), - this.MakeGridHeader(x => x.Batch).SetWidth(100), - this.MakeGridHeader(x => x.Lot).SetWidth(100), - this.MakeGridHeader(x => x.TaskStatus).SetWidth(100) - .SetBackGroundFunc((a)=>{ - if(a.TaskStatus == InventoryTaskStatusEnum.Locked ) + if (Wtm.LoginUserInfo.Groups.Any(x => x.GroupCode == "001")) + { + // 线边 + return new List> + { + this.MakeGridHeader(x => x.Name_view).SetWidth(80).SetHide(), + this.MakeGridHeader(x => x.MaterialCode).SetWidth(90).SetHide(), + this.MakeGridHeader(x => x.MaterialType).SetWidth(100), + this.MakeGridHeader(x => x.Qty).SetWidth(100) + .SetFormat((a, b) => { return (int?)a.Qty ?? 0; }) + .SetShowTotal(true), + this.MakeGridHeader(x => x.Batch).SetWidth(100), + this.MakeGridHeader(x => x.Lot).SetWidth(100), + this.MakeGridHeader(x => x.TaskStatus).SetWidth(100) + .SetBackGroundFunc((a) => { - return "#FF0000"; - } - return ""; - }), - this.MakeGridHeader(x => x.MaterialName).SetWidth(100), - this.MakeGridHeader(x => x.CompanyCode).SetWidth(150), - this.MakeGridHeader(x => x.SourceCode).SetWidth(150), - this.MakeGridHeader(x => x.MaterialSpec).SetHide().SetWidth(150), - this.MakeGridHeader(x => x.MaterialUnit).SetHide(), - this.MakeGridHeader(x => x.TaskQty).SetHide(), - this.MakeGridHeader(x => x.ManufactureDate).SetHide(), - this.MakeGridHeader(x => x.ExpirationDate).SetHide(), - this.MakeGridHeader(x => x.ReceiptCode).SetHide(), - this.MakeGridHeader(x => x.ReceiptDetailId).SetHide(), - this.MakeGridHeader(x => x.Status).SetWidth(100), - this.MakeGridHeaderAction(width: 200) - }; + if (a.TaskStatus == InventoryTaskStatusEnum.Locked) + { + return "#FF0000"; + } + + return ""; + }), + this.MakeGridHeader(x => x.CreateTime).SetWidth(100), + this.MakeGridHeader(x => x.MaterialName).SetWidth(100), + this.MakeGridHeader(x => x.ContainerCode).SetWidth(90), + this.MakeGridHeader(x => x.LocationCode).SetWidth(130), + this.MakeGridHeader(x => x.SupplierCode).SetWidth(100).SetHide(), + this.MakeGridHeader(x => x.CompanyCode).SetWidth(150), + this.MakeGridHeader(x => x.SourceCode).SetWidth(150), + this.MakeGridHeader(x => x.MaterialSpec).SetHide().SetWidth(150), + this.MakeGridHeader(x => x.MaterialUnit).SetHide(), + this.MakeGridHeader(x => x.TaskQty).SetHide(), + this.MakeGridHeader(x => x.ManufactureDate).SetHide(), + this.MakeGridHeader(x => x.ExpirationDate).SetHide(), + this.MakeGridHeader(x => x.ReceiptCode).SetHide(), + this.MakeGridHeader(x => x.ReceiptDetailId).SetHide(), + this.MakeGridHeader(x => x.Status).SetWidth(100), + this.MakeGridHeaderAction(width: 200) + }; + } + else + { + return new List> + { + this.MakeGridHeader(x => x.Name_view).SetWidth(80).SetHide(), + this.MakeGridHeader(x => x.MaterialCode).SetWidth(90).SetHide(), + this.MakeGridHeader(x => x.MaterialType).SetWidth(100), + this.MakeGridHeader(x => x.Qty).SetWidth(100) + .SetFormat((a, b) => { return (int?)a.Qty ?? 0; }) + .SetShowTotal(true), + this.MakeGridHeader(x => x.CompanyCode).SetWidth(150), + this.MakeGridHeader(x => x.SourceCode).SetWidth(150), + this.MakeGridHeader(x => x.Batch).SetWidth(100).SetHide(), + this.MakeGridHeader(x => x.Lot).SetWidth(100).SetHide(), + this.MakeGridHeader(x => x.TaskStatus).SetWidth(100) + .SetBackGroundFunc((a) => + { + if (a.TaskStatus == InventoryTaskStatusEnum.Locked) + { + return "#FF0000"; + } + + return ""; + }), + this.MakeGridHeader(x => x.CreateTime).SetWidth(100), + this.MakeGridHeader(x => x.MaterialName).SetWidth(100), + this.MakeGridHeader(x => x.ContainerCode).SetWidth(90), + this.MakeGridHeader(x => x.LocationCode).SetWidth(130), + this.MakeGridHeader(x => x.SupplierCode).SetWidth(100).SetHide(), + this.MakeGridHeader(x => x.MaterialSpec).SetHide().SetWidth(150), + this.MakeGridHeader(x => x.MaterialUnit).SetHide(), + this.MakeGridHeader(x => x.TaskQty).SetHide(), + this.MakeGridHeader(x => x.ManufactureDate).SetHide(), + this.MakeGridHeader(x => x.ExpirationDate).SetHide(), + this.MakeGridHeader(x => x.ReceiptCode).SetHide(), + this.MakeGridHeader(x => x.ReceiptDetailId).SetHide(), + this.MakeGridHeader(x => x.Status).SetWidth(100), + this.MakeGridHeaderAction(width: 200) + }; + } } public override IOrderedQueryable GetSearchQuery() @@ -140,6 +186,7 @@ namespace HHWMS.Web Status = x.Status, TaskStatus = x.TaskStatus, Flag = x.Flag, + CreateTime = x.CreateTime }) .OrderBy(x => x.ID); return query; diff --git a/HHWMS.Web/Areas/Inventory/ViewModels/InventoryVMs/InventoryVM.cs b/HHWMS.Web/Areas/Inventory/ViewModels/InventoryVMs/InventoryVM.cs index 4dbfc66..226a195 100644 --- a/HHWMS.Web/Areas/Inventory/ViewModels/InventoryVMs/InventoryVM.cs +++ b/HHWMS.Web/Areas/Inventory/ViewModels/InventoryVMs/InventoryVM.cs @@ -6,7 +6,7 @@ using System.ComponentModel.DataAnnotations; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Extensions; using HHWMS.Web.Models; - +using Microsoft.Extensions.DependencyInjection; namespace HHWMS.Web { @@ -18,6 +18,7 @@ namespace HHWMS.Web [Display(Name = "Location.OutPort", Description = "拣货口")] public string OutPort { get; set; } + private IUnitWork unitWork; public InventoryVM() { @@ -26,6 +27,7 @@ namespace HHWMS.Web protected override void InitVM() { + unitWork = new UnitWork(Wtm); AllZones = DC.Set().GetSelectListItems(Wtm, y => y.Name); AllOutPorts = new List @@ -63,5 +65,95 @@ namespace HHWMS.Web { base.DoDelete(); } + + /// + /// ERP报工处理 + /// + /// 库存实体 + /// 任务类型(入库/出库) + /// 返回处理结果,true表示成功,false表示失败 + public async Task ProcessErpFinishIn(Inventory inventory, TaskTypeEnum? taskType) + { + if (inventory == null + //|| string.IsNullOrEmpty(inventory.ReceiptDetailId) + || inventory.ZoneId == 1025 ) //模具库不报工 + { + return false; + } + + try + { + // ERP相关 入库报工 + var finishInRequest = new FinishInRequest + { + Main = new FinishInMain + { + ErpCode = "003", // 账套号 + InCode = unitWork.GetSerialNumber("RW"), // 报工单号(上游单号) + InDate = (inventory.CreateTime ?? DateTime.Now) + .ToString("yyyy-MM-dd"), // 报工日期 + StepCode = inventory.BatchCode, // 工序(批次编码) + WorkerCode = inventory.CreateBy, // 工人编码(创建人) + Maker = unitWork.FindSingle(x => + x.ITCode == inventory.CreateBy).Name, // 制单人(校验员) + Type = inventory.ZoneId == 1027 ? 1: 2, + WhCode = inventory.ZoneId == 1027 ? "xb": "cp", + }, + Details = new List() + }; + + if (taskType == TaskTypeEnum.入库) + { + // 根据库存构建明细 + finishInRequest.Details.Add(new FinishInDetail + { + MoDetailId = inventory.ZoneId == 1027 ? "" : inventory.ReceiptDetailId, // 生产订单明细(ERP明细ID) + InvCode = inventory.MaterialCode, // 物料号 + Qty = inventory.Qty ?? 0 // 完工数量 + }); + } + else if (taskType == TaskTypeEnum.出库) + { + finishInRequest.Details.Add(new FinishInDetail + { + MaterialDetails = new List + { + new MaterialDetail + { + MoDetailId = inventory.ZoneId == 1027 ? "" : inventory.ReceiptDetailId, // 生产订单明细ID + InvCode = inventory.MaterialCode, // 物料号 + Qty = (decimal)inventory.Qty // 完工数量(空值兜底为0) + } + } + }); + } + + // 调用ERP接口 + var apiResult = await Wtm.CallAPI("ERP", "FinishIn", + HttpMethodEnum.POST, + finishInRequest); + + if (apiResult.Data != null && apiResult.Data.Result) + { + inventory.UploadErpFlag = 1; + return true; + } + else + { + inventory.UploadErpFlag = 0; + return false; + } + } + catch (Exception ex) + { + // 异常情况下设置标志为失败 + inventory.UploadErpFlag = 0; + return false; + } + finally + { + unitWork.Update(inventory); + } + } } } diff --git a/HHWMS.Web/Areas/Inventory/Views/InventoryTransaction/Index.cshtml b/HHWMS.Web/Areas/Inventory/Views/InventoryTransaction/Index.cshtml index 1a8ab56..6123347 100644 --- a/HHWMS.Web/Areas/Inventory/Views/InventoryTransaction/Index.cshtml +++ b/HHWMS.Web/Areas/Inventory/Views/InventoryTransaction/Index.cshtml @@ -3,8 +3,8 @@ - - + + @@ -16,8 +16,8 @@ - + diff --git a/HHWMS.Web/Areas/Receipt/Controllers/ReceiptHeaderController.cs b/HHWMS.Web/Areas/Receipt/Controllers/ReceiptHeaderController.cs index 1ccfa4a..4d70888 100644 --- a/HHWMS.Web/Areas/Receipt/Controllers/ReceiptHeaderController.cs +++ b/HHWMS.Web/Areas/Receipt/Controllers/ReceiptHeaderController.cs @@ -250,383 +250,16 @@ namespace HHWMS.Web.Controllers [ActionDescription("GetReceiptDetails")] public async Task GetReceiptDetails(ReceiptDetailVM vm) { - string receiptCode = vm.Entity.SourceCode; - string tuhao = vm.Entity.MaterialType; - try + var receiptHeaderVM = Wtm.CreateVM(); + var result = await receiptHeaderVM.GetReceiptDetailsFromERP(vm); + if (result.Success) { - // if (string.IsNullOrWhiteSpace(receiptCode)) - // { - // return FFResult().Alert($"入库单单号不能为空"); - // } - - // 根据入库单单号查找对应的 SourceCode(生产订单号) - // var header = _unitwork.FindSingle(h => h.SourceCode == receiptCode); - // if (header != null) - // { - // return FFResult().Alert($"已存在此入库单,单号:{receiptCode}"); - // } - - // 调用 ERP 接口获取该订单的最新数据 - var request = new LoadMoDetailListRequest - { - ErpCode = "003", - MoCode = receiptCode ?? "", // 使用生产订单号查询 - InvCode = vm.Entity?.MaterialCode ?? "", - InvDefine1 = vm.Entity?.MaterialType ?? "", - Size = 100, - Page = 1 - }; - - var apiResult = await Wtm.CallAPI("ERP", "LoadMoDetailList", HttpMethodEnum.POST, request); - - if (apiResult?.Data != null && apiResult.Data.Code == 0 && apiResult.Data.Data?.Count > 0) - { - Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails", - request: $"订单号:{receiptCode},获取到 {apiResult.Data.Data.Count} 条明细", - result: "开始处理订单数据"); - - // 使用相同的逻辑处理订单数据 - await ProcessOrderData(_unitwork, apiResult.Data.Data); - - Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails", - request: $"订单号:{receiptCode}", - result: "订单数据处理完成"); - - return FFResult().CloseDialog().RefreshGrid().Message("订单数据获取成功"); - } - else - { - return FFResult().Alert(apiResult?.Data?.Msg ?? apiResult?.ErrorMsg ?? "未获取到订单数据"); - } + return FFResult().CloseDialog().RefreshGrid().Alert("拉取成功!"); } - catch (Exception ex) + else { - Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails", - request: $"入库单号:{receiptCode}", - result: $"错误:{ex.Message}"); - return FFResult().Alert($"获取订单数据失败:{ex.Message}"); + return FFResult().CloseDialog().RefreshGrid().Alert($"拉取失败!{result.Msg}"); } } - - /// - /// 处理订单数据并保存到本地数据库(照搬 LoadErpOrderJob) - /// - private async Task ProcessOrderData(IUnitWork unitWork, List orderList) - { - // 按订单号分组 - var orderGroups = orderList.GroupBy(x => x.MoCode); - - foreach (var group in orderGroups) - { - var moCode = group.Key; - var orderDetails = group.ToList(); - - // 检查订单是否已存在 - var existingHeader = unitWork.FindSingle(h => h.SourceCode == moCode); - - if (existingHeader == null) - { - // 创建新的入库单头 - var header = new ReceiptHeader - { - SourceCode = moCode, // 生产订单号 - Code = unitWork.GetSerialNumber("REC", 8), // 生成入库单号 - BillType = "生产订单", - TotalQty = orderDetails.Sum(x => x.Qty), - TotalLines = orderDetails.Count, - FirstStatus = ReceiptStatusEnum.新建, - LastStatus = ReceiptStatusEnum.新建, - Remark = orderDetails.First().Memo, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", - UpdateVersion = 1 - }; - - unitWork.Add(header); // 保存以获取 HeaderId - - // 获取订单明细中的所有物料编码 - var invCodes = orderDetails - .Where(x => !string.IsNullOrWhiteSpace(x.InvCode)) - .Select(x => x.InvCode) - .Distinct() - .ToList(); - - // 一次性查询本地数据库中已存在的物料(优化性能) - var existingMaterials = unitWork.Find(m => invCodes.Contains(m.MaterialCode)) - .ToList(); - - // 待新增的物料列表 - var newMaterials = new List(); - - // 创建入库单明细 - var details = new List(); - foreach (var item in orderDetails) - { - // 判断物料是否存在,不存在则创建 - if (!string.IsNullOrWhiteSpace(item.InvCode)) - { - var existingMaterial = existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode); - if (existingMaterial == null) - { - // 检查是否已在待新增列表中 - if (!newMaterials.Any(m => m.MaterialCode == item.InvCode)) - { - // 物料不存在,创建新物料 - var newMaterial = new Material - { - MaterialCode = item.InvCode, - MaterialName = item.InvName, - MaterialSpec = item.InvStd, - MaterialType = item.InvDefine1, // 图号 - Qty = 0, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" - }; - newMaterials.Add(newMaterial); - } - } - } - - var detail = new ReceiptDetail - { - HeaderId = header.ID, - SourceCode = header.SourceCode, - MaterialCode = item.InvCode, - MaterialType = item.InvDefine1, // 图号 - MaterialName = item.InvName, - Lot = item.MoDetailId, // 明细id - MaterialSpec = item.InvStd, - Qty = item.Qty, - CompletedQty = 0, - Status = ReceiptStatusEnum.新建, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", - UpdateVersion = 1 - }; - - details.Add(detail); - - // 保存工序路线到 MaterialProcess 表 - if (item.ProcessRoute != null && item.ProcessRoute.Count > 0) - { - await SaveProcessRoute(unitWork, item.InvCode, item.ProcessRoute); - } - } - - // 批量新增物料 - if (newMaterials.Count > 0) - { - unitWork.BatchAdd(newMaterials); - } - - unitWork.BatchAdd(details); - - Wtm.DoLog("保存订单数据", ActionLogTypesEnum.Normal, "ProcessOrderData", - request: $"订单号:{moCode}", - result: $"成功创建入库单 {header.Code},明细 {details.Count} 条"); - } - else - { - // 订单已存在,更新数据 - existingHeader.TotalQty = orderDetails.Sum(x => x.Qty); - existingHeader.TotalLines = orderDetails.Count; - existingHeader.Remark = orderDetails.First().Memo; - existingHeader.UpdateVersion++; - existingHeader.UpdateTime = DateTime.Now; - existingHeader.UpdateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"; - unitWork.Update(existingHeader); - - // 更新明细 - var oldDetails = unitWork.Find(d => d.HeaderId == existingHeader.ID).ToList(); - - // 获取订单明细中的所有物料编码 - var invCodes = orderDetails - .Where(x => !string.IsNullOrWhiteSpace(x.InvCode)) - .Select(x => x.InvCode) - .Distinct() - .ToList(); - - // 一次性查询本地数据库中已存在的物料(优化性能) - var existingMaterials = unitWork.Find(m => invCodes.Contains(m.MaterialCode)) - .ToList(); - - // 待新增的物料列表 - var newMaterials = new List(); - - var newDetails = new List(); - foreach (var item in orderDetails) - { - // 判断明细是否存在 用MoDetailId 判断 - if (!oldDetails.Any(x => x.Lot == item.MoDetailId)) - { - // 判断物料是否存在,不存在则创建 - if (!string.IsNullOrWhiteSpace(item.InvCode)) - { - var existingMaterial = existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode); - if (existingMaterial == null) - { - // 检查是否已在待新增列表中 - if (!newMaterials.Any(m => m.MaterialCode == item.InvCode)) - { - // 物料不存在,创建新物料 - var newMaterial = new Material - { - MaterialCode = item.InvCode, - MaterialName = item.InvName, - MaterialSpec = item.InvStd, - MaterialType = item.InvDefine1, // 图号 - Qty = 0, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" - }; - newMaterials.Add(newMaterial); - } - } - } - - var detail = new ReceiptDetail - { - HeaderId = existingHeader.ID, - SourceCode = existingHeader.SourceCode, - MaterialCode = item.InvCode, - MaterialType = item.InvDefine1, // 图号 - MaterialName = item.InvName, - Lot = item.MoDetailId, // 明细id - MaterialSpec = item.InvStd, - Qty = item.Qty, - CompletedQty = 0, - Status = ReceiptStatusEnum.新建, - Batch = item.Version, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", - UpdateVersion = 1 - }; - - newDetails.Add(detail); - - // 更新工序路线 - if (item.ProcessRoute != null && item.ProcessRoute.Count > 0) - { - await SaveProcessRoute(unitWork, item.InvCode, item.ProcessRoute); - } - } - } - - // 批量新增物料 - if (newMaterials.Count > 0) - { - unitWork.BatchAdd(newMaterials); - } - - unitWork.BatchAdd(newDetails); - - Wtm.DoLog("更新订单数据", ActionLogTypesEnum.Normal, "ProcessOrderData", - request: $"订单号:{moCode}", - result: $"成功更新入库单 {existingHeader.Code},新增明细 {newDetails.Count} 条"); - } - } - } - - /// - /// 保存工序路线到 MaterialProcess 表 - /// - private async Task SaveProcessRoute(IUnitWork unitWork, string materialCode, List processRoute) - { - // 查询已存在的工序路线 - var existingProcesses = unitWork.Find(p => p.MaterialCode == materialCode).ToList(); - - // 待新增的工序 - var processesToAdd = new List(); - // 待更新的工序 - var processesToUpdate = new List(); - - var processConfigs = unitWork.Find(x => x.Code != "").ToList(); - // 遍历新的工序路线 - foreach (var route in processRoute) - { - // 检查工艺是否存在于ProcessConfig中,不存在则添加 - var processConfig = processConfigs.Any(p => p.Code == route.ProcessCode); - if (!processConfig) - { - unitWork.Add(new ProcessConfig - { - Code = route.ProcessCode, - Name = route.ProcessName, - Zone = "Zone2", - Port = "EnterExit_P2005" - }); - } - - // 查找是否存在相同工序名称的记录 - var existing = existingProcesses.FirstOrDefault(p => - p.ProcessName == route.ProcessName); - - if (existing != null) - { - // 已存在,检查是否需要更新 - bool needUpdate = false; - - if (existing.ProcessCode != route.ProcessCode || existing.ProcessOrder != route.Sort) - { - existing.ProcessCode = route.ProcessCode; - existing.ProcessOrder = route.Sort; - needUpdate = true; - } - - if (!existing.IsActive) - { - existing.IsActive = true; - needUpdate = true; - } - - if (needUpdate) - { - existing.UpdateTime = DateTime.Now; - existing.UpdateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"; - processesToUpdate.Add(existing); - } - } - else - { - // 不存在,新增 - var newProcess = new MaterialProcess - { - MaterialCode = materialCode, - ProcessCode = route.ProcessCode, - ProcessName = route.ProcessName, - ProcessOrder = route.Sort, - Weight = 0, - IsActive = true, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" - }; - processesToAdd.Add(newProcess); - } - } - - // 查找需要删除的工序(旧的多余的) - var processesToDelete = existingProcesses.Where(existing => - !processRoute.Any(route => - route.ProcessName == existing.ProcessName) - ).ToList(); - - // 执行数据库操作 - if (processesToAdd.Count > 0) - { - unitWork.BatchAdd(processesToAdd); - } - - if (processesToUpdate.Count > 0) - { - unitWork.BatchUpdate(processesToUpdate); - } - - if (processesToDelete.Count > 0) - { - unitWork.BatchDelete(processesToDelete); - } - - await Task.CompletedTask; - } } } diff --git a/HHWMS.Web/Areas/Receipt/Controllers/ReceiptInController.cs b/HHWMS.Web/Areas/Receipt/Controllers/ReceiptInController.cs index 3fa95a2..07eab00 100644 --- a/HHWMS.Web/Areas/Receipt/Controllers/ReceiptInController.cs +++ b/HHWMS.Web/Areas/Receipt/Controllers/ReceiptInController.cs @@ -72,6 +72,7 @@ namespace HHWMS.Web.Controllers if (apiResult?.Data != null && apiResult.Data.Code == 0) { var materialsList = new List(); + var oldMaterials = new List(); var newMaterials = new List(); // 获取接口返回的所有物料编码 @@ -125,21 +126,34 @@ namespace HHWMS.Web.Controllers } else { + if (existingMaterial.MaterialName != item.InvName + || existingMaterial.MaterialType != item.InvDefine1) + { + existingMaterial.MaterialName = item.InvName; + existingMaterial.MaterialSpec = item.InvStd; + existingMaterial.MaterialUnit = item.UnitName; + existingMaterial.MaterialType = item.InvDefine1; + existingMaterial.SupplierCode = item.InvDefine4; + existingMaterial.UpdateTime = DateTime.Now; + existingMaterial.UpdateBy = "System"; + oldMaterials.Add(existingMaterial); + } // 物料已存在,添加到返回列表 materialsList.Add(new { - MaterialCode = existingMaterial.MaterialCode, - MaterialName = existingMaterial.MaterialName, - MaterialSpec = existingMaterial.MaterialSpec, - MaterialUnit = existingMaterial.MaterialUnit, - MaterialType = existingMaterial.MaterialType, - SupplierCode = existingMaterial.SupplierCode + MaterialCode = item.InvCode, + MaterialName = item.InvName, + MaterialSpec = item.InvStd, + MaterialUnit = item.UnitName, + MaterialType = item.InvDefine1, // 永丰图号 + SupplierCode = item.InvDefine4, // 客户图号 }); } } // 保存新增的物料 _unitWork.BatchAdd(newMaterials); + _unitWork.BatchUpdate(oldMaterials); // 返回物料列表到前端 return Json(new { Code = 200, Data = materialsList }); @@ -235,367 +249,33 @@ namespace HHWMS.Web.Controllers /// /// [HttpPost] - public async Task GetOrdersByMaterial(string materialCode) + public async Task GetOrdersByMaterial(string materialCode,string batch) { - - // 查询包含这个物料的入库单单号 - - var request = new LoadMoDetailListRequest + var receiptHeaderVM = Wtm.CreateVM(); + var vm = new ReceiptDetailVM() { - ErpCode = "003", - InvCode = materialCode, - Size = 100, - Page = 1 + Entity = new ReceiptDetail() + { + MaterialCode = materialCode, + Batch = batch ?? "32" + } }; - - var apiResult = await Wtm.CallAPI("ERP", "LoadMoDetailList", HttpMethodEnum.POST, request); - - if (apiResult?.Data != null && apiResult.Data.Code == 0 && apiResult.Data.Data?.Count > 0) + var result = await receiptHeaderVM.GetReceiptDetailsFromERP(vm); + if (result.Data != null) { - Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails", - request: $"物料:{materialCode},获取到 {apiResult.Data.Data.Count} 条明细", - result: "开始处理订单数据"); + var orders = result.Data.Select(x => new + { + value = x.MoDetailId, + label = x.MoCode, + }) + .ToList(); - // 使用相同的逻辑处理订单数据 - await ProcessOrderData(_unitWork, apiResult.Data.Data); - Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails", - request: $"订单号:{materialCode}", - result: "订单数据处理完成"); + return Json(new { Code = 200, Data = orders }); } - - var orders = _unitWork.Find(x => x.MaterialCode == materialCode) - .Select(x => new - { - value = x.Lot, - label = x.SourceCode, - }) - .ToList(); - - return Json(new { Code = 200, Data = orders }); + return Json(new { Code = 200, Data = new List() }); } - - /// - /// 处理订单数据并保存到本地数据库(照搬 LoadErpOrderJob) - /// - private async Task ProcessOrderData(IUnitWork unitWork, List orderList) - { - // 按订单号分组 - var orderGroups = orderList.GroupBy(x => x.MoCode); - - foreach (var group in orderGroups) - { - var moCode = group.Key; - var orderDetails = group.ToList(); - - // 检查订单是否已存在 - var existingHeader = unitWork.FindSingle(h => h.SourceCode == moCode); - - if (existingHeader == null) - { - // 创建新的入库单头 - var header = new ReceiptHeader - { - SourceCode = moCode, // 生产订单号 - Code = unitWork.GetSerialNumber("REC", 8), // 生成入库单号 - BillType = "生产订单", - TotalQty = orderDetails.Sum(x => x.Qty), - TotalLines = orderDetails.Count, - FirstStatus = ReceiptStatusEnum.新建, - LastStatus = ReceiptStatusEnum.新建, - Remark = orderDetails.First().Memo, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", - UpdateVersion = 1 - }; - - unitWork.Add(header); // 保存以获取 HeaderId - - // 获取订单明细中的所有物料编码 - var invCodes = orderDetails - .Where(x => !string.IsNullOrWhiteSpace(x.InvCode)) - .Select(x => x.InvCode) - .Distinct() - .ToList(); - - // 一次性查询本地数据库中已存在的物料(优化性能) - var existingMaterials = unitWork.Find(m => invCodes.Contains(m.MaterialCode)) - .ToList(); - - // 待新增的物料列表 - var newMaterials = new List(); - - // 创建入库单明细 - var details = new List(); - foreach (var item in orderDetails) - { - // 判断物料是否存在,不存在则创建 - if (!string.IsNullOrWhiteSpace(item.InvCode)) - { - var existingMaterial = existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode); - if (existingMaterial == null) - { - // 检查是否已在待新增列表中 - if (!newMaterials.Any(m => m.MaterialCode == item.InvCode)) - { - // 物料不存在,创建新物料 - var newMaterial = new Material - { - MaterialCode = item.InvCode, - MaterialName = item.InvName, - MaterialSpec = item.InvStd, - MaterialType = item.InvDefine1, // 图号 - Qty = 0, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" - }; - newMaterials.Add(newMaterial); - } - } - } - - var detail = new ReceiptDetail - { - HeaderId = header.ID, - SourceCode = header.SourceCode, - MaterialCode = item.InvCode, - MaterialType = item.InvDefine1, // 图号 - MaterialName = item.InvName, - Lot = item.MoDetailId, // 明细id - MaterialSpec = item.InvStd, - Qty = item.Qty, - CompletedQty = 0, - Status = ReceiptStatusEnum.新建, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", - UpdateVersion = 1 - }; - - details.Add(detail); - - // 保存工序路线到 MaterialProcess 表 - if (item.ProcessRoute != null && item.ProcessRoute.Count > 0) - { - await SaveProcessRoute(unitWork, item.InvCode, item.ProcessRoute); - } - } - - // 批量新增物料 - if (newMaterials.Count > 0) - { - unitWork.BatchAdd(newMaterials); - } - - unitWork.BatchAdd(details); - - Wtm.DoLog("保存订单数据", ActionLogTypesEnum.Normal, "ProcessOrderData", - request: $"订单号:{moCode}", - result: $"成功创建入库单 {header.Code},明细 {details.Count} 条"); - } - else - { - // 订单已存在,更新数据 - existingHeader.TotalQty = orderDetails.Sum(x => x.Qty); - existingHeader.TotalLines = orderDetails.Count; - existingHeader.Remark = orderDetails.First().Memo; - existingHeader.UpdateVersion++; - existingHeader.UpdateTime = DateTime.Now; - existingHeader.UpdateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"; - unitWork.Update(existingHeader); - - // 更新明细 - var oldDetails = unitWork.Find(d => d.HeaderId == existingHeader.ID).ToList(); - - // 获取订单明细中的所有物料编码 - var invCodes = orderDetails - .Where(x => !string.IsNullOrWhiteSpace(x.InvCode)) - .Select(x => x.InvCode) - .Distinct() - .ToList(); - - // 一次性查询本地数据库中已存在的物料(优化性能) - var existingMaterials = unitWork.Find(m => invCodes.Contains(m.MaterialCode)) - .ToList(); - - // 待新增的物料列表 - var newMaterials = new List(); - - var newDetails = new List(); - foreach (var item in orderDetails) - { - // 判断明细是否存在 用MoDetailId 判断 - if (!oldDetails.Any(x => x.Lot == item.MoDetailId)) - { - // 判断物料是否存在,不存在则创建 - if (!string.IsNullOrWhiteSpace(item.InvCode)) - { - var existingMaterial = existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode); - if (existingMaterial == null) - { - // 检查是否已在待新增列表中 - if (!newMaterials.Any(m => m.MaterialCode == item.InvCode)) - { - // 物料不存在,创建新物料 - var newMaterial = new Material - { - MaterialCode = item.InvCode, - MaterialName = item.InvName, - MaterialSpec = item.InvStd, - MaterialType = item.InvDefine1, // 图号 - Qty = 0, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" - }; - newMaterials.Add(newMaterial); - } - } - } - - var detail = new ReceiptDetail - { - HeaderId = existingHeader.ID, - SourceCode = existingHeader.SourceCode, - MaterialCode = item.InvCode, - MaterialType = item.InvDefine1, // 图号 - MaterialName = item.InvName, - Lot = item.MoDetailId, // 明细id - MaterialSpec = item.InvStd, - Qty = item.Qty, - CompletedQty = 0, - Status = ReceiptStatusEnum.新建, - Batch = item.Version, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", - UpdateVersion = 1 - }; - - newDetails.Add(detail); - - // 更新工序路线 - if (item.ProcessRoute != null && item.ProcessRoute.Count > 0) - { - await SaveProcessRoute(unitWork, item.InvCode, item.ProcessRoute); - } - } - } - - // 批量新增物料 - if (newMaterials.Count > 0) - { - unitWork.BatchAdd(newMaterials); - } - - unitWork.BatchAdd(newDetails); - - Wtm.DoLog("更新订单数据", ActionLogTypesEnum.Normal, "ProcessOrderData", - request: $"订单号:{moCode}", - result: $"成功更新入库单 {existingHeader.Code},新增明细 {newDetails.Count} 条"); - } - } - } - - /// - /// 保存工序路线到 MaterialProcess 表 - /// - private async Task SaveProcessRoute(IUnitWork unitWork, string materialCode, List processRoute) - { - // 查询已存在的工序路线 - var existingProcesses = unitWork.Find(p => p.MaterialCode == materialCode).ToList(); - - // 待新增的工序 - var processesToAdd = new List(); - // 待更新的工序 - var processesToUpdate = new List(); - - var processConfigs = unitWork.Find(x => x.Code != "").ToList(); - // 遍历新的工序路线 - foreach (var route in processRoute) - { - // 检查工艺是否存在于ProcessConfig中,不存在则添加 - var processConfig = processConfigs.Any(p => p.Code == route.ProcessCode); - if (!processConfig) - { - unitWork.Add(new ProcessConfig - { - Code = route.ProcessCode, - Name = route.ProcessName, - Zone = "Zone2", - Port = "EnterExit_P2005" - }); - } - - // 查找是否存在相同工序名称的记录 - var existing = existingProcesses.FirstOrDefault(p => - p.ProcessName == route.ProcessName); - - if (existing != null) - { - // 已存在,检查是否需要更新 - bool needUpdate = false; - - if (existing.ProcessCode != route.ProcessCode || existing.ProcessOrder != route.Sort) - { - existing.ProcessCode = route.ProcessCode; - existing.ProcessOrder = route.Sort; - needUpdate = true; - } - - if (!existing.IsActive) - { - existing.IsActive = true; - needUpdate = true; - } - - if (needUpdate) - { - existing.UpdateTime = DateTime.Now; - existing.UpdateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"; - processesToUpdate.Add(existing); - } - } - else - { - // 不存在,新增 - var newProcess = new MaterialProcess - { - MaterialCode = materialCode, - ProcessCode = route.ProcessCode, - ProcessName = route.ProcessName, - ProcessOrder = route.Sort, - Weight = 0, - IsActive = true, - CreateTime = DateTime.Now, - CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" - }; - processesToAdd.Add(newProcess); - } - } - - // 查找需要删除的工序(旧的多余的) - var processesToDelete = existingProcesses.Where(existing => - !processRoute.Any(route => - route.ProcessName == existing.ProcessName) - ).ToList(); - - // 执行数据库操作 - if (processesToAdd.Count > 0) - { - unitWork.BatchAdd(processesToAdd); - } - - if (processesToUpdate.Count > 0) - { - unitWork.BatchUpdate(processesToUpdate); - } - - if (processesToDelete.Count > 0) - { - unitWork.BatchDelete(processesToDelete); - } - - await Task.CompletedTask; - } - + /// /// 获取托盘类型信息 @@ -735,7 +415,8 @@ namespace HHWMS.Web.Controllers MaterialSpec = material.MaterialSpec, MaterialUnit = material.MaterialUnit, SupplierCode = material.SupplierCode, - SourceCode = model.OrderId, // 单号 + SourceCode = model.BillCode,//客户订单号 + ProduceCode = model.OrderId, // 单号 ReceiptDetailId = model.SourceCode, // erpdetailid 回传使用 BatchCode = materialProcess.ProcessCode, //erp回传使用 批次编码 Batch = materialProcess.ProcessName, @@ -1004,6 +685,8 @@ namespace HHWMS.Web.Controllers public int Qty { get; set; } public bool IsQualified { get; set; } public string OperatorInfo { get; set; } + + public string BillCode { get; set; } } public class LineRange diff --git a/HHWMS.Web/Areas/Receipt/Controllers/SimpleReceiptInController.cs b/HHWMS.Web/Areas/Receipt/Controllers/SimpleReceiptInController.cs index 2f56888..f927a03 100644 --- a/HHWMS.Web/Areas/Receipt/Controllers/SimpleReceiptInController.cs +++ b/HHWMS.Web/Areas/Receipt/Controllers/SimpleReceiptInController.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Threading.Tasks; using AutoMapper; using HHWMS.Web.Models; using Microsoft.AspNetCore.Mvc; @@ -134,7 +135,7 @@ namespace HHWMS.Web.Controllers } [HttpPost] - public ActionResult AddInventory([FromBody] SimpleReceiptInModel model) + public async Task AddInventory([FromBody] SimpleReceiptInModel model) { try { @@ -183,7 +184,8 @@ namespace HHWMS.Web.Controllers MaterialSpec = material.MaterialSpec, MaterialUnit = material.MaterialUnit, CompanyCode = model.CompanyCode, - SourceCode = model.OrderId, + SourceCode = model.BillCode,// 客户订单 + ProduceCode = model.OrderId,// 生产订单 ReceiptDetailId = model.SourceCode, // 优先使用 OrderId SupplierCode = material.SupplierCode, Batch = model.Batch, @@ -198,9 +200,13 @@ namespace HHWMS.Web.Controllers // 创建入库交易记录 var invTran = _mapper.Map(inventory); + invTran.BillCode = inventory.ID.ToString(); invTran.Type = InventoryTranType.入库; _unitWork.Add(invTran); + + var invVM = Wtm.CreateVM(); + await invVM.ProcessErpFinishIn(inventory, TaskTypeEnum.入库); return JsonMore(new { Id = inventory.ID }); } @@ -320,7 +326,7 @@ namespace HHWMS.Web.Controllers } [HttpPost] - public ActionResult DeleteInventory(int id) + public async Task DeleteInventory(int id) { try { @@ -334,13 +340,17 @@ namespace HHWMS.Web.Controllers // 创建出库交易记录 var invTran = _mapper.Map(inventory); + invTran.BillCode = inventory.ID.ToString(); invTran.Type = InventoryTranType.出库; _unitWork.Add(invTran); + // var invVM = Wtm.CreateVM(); + // await invVM.ProcessErpFinishIn(inventory, TaskTypeEnum.出库); + // 删除库存记录 _unitWork.Delete(inventory); - + return JsonMore(null); } catch (Exception ex) @@ -350,7 +360,7 @@ namespace HHWMS.Web.Controllers } [HttpPost] - public ActionResult UpdateInventory(int id, decimal qty) + public async Task UpdateInventory(int id, decimal qty) { try { @@ -370,21 +380,32 @@ namespace HHWMS.Web.Controllers // 创建出库交易记录(原数量) var invOutTran = _mapper.Map(inventory); invOutTran.Type = InventoryTranType.出库; + invOutTran.Qty -= qty; + invOutTran.BillCode = inventory.ID.ToString(); + invOutTran.CreateTime = DateTime.Now; _unitWork.Add(invOutTran); // 创建入库交易记录(新数量) - var invInTran = _mapper.Map(inventory); - invInTran.Qty = qty; - invInTran.Type = InventoryTranType.入库; - - _unitWork.Add(invInTran); + // var invInTran = _mapper.Map(inventory); + // invInTran.Qty = qty; + // invInTran.BillCode = inventory.ID.ToString(); + // invInTran.Type = InventoryTranType.入库; + // invInTran.CreateTime = DateTime.Now; + // + // _unitWork.Add(invInTran); + // 出库数量 + // inventory.Qty -= qty; + // var invVM = Wtm.CreateVM(); + // await invVM.ProcessErpFinishIn(inventory, TaskTypeEnum.出库); + // 更新库存数量 inventory.Qty = qty; inventory.UpdateTime = DateTime.Now; inventory.UpdateBy = Wtm?.LoginUserInfo?.ITCode ?? "WMS"; _unitWork.Update(inventory); + return JsonMore(null); } @@ -586,6 +607,12 @@ namespace HHWMS.Web.Controllers /// 订单号(上游单号) /// public string SourceCode { get; set; } + + /// + /// 生产订单号 + /// + public string BillCode { get; set; } + public string OrderId { get; set; } /// diff --git a/HHWMS.Web/Areas/Receipt/ViewModels/ReceiptHeaderVMs/ReceiptHeaderVM.cs b/HHWMS.Web/Areas/Receipt/ViewModels/ReceiptHeaderVMs/ReceiptHeaderVM.cs index 55d95e5..440337e 100644 --- a/HHWMS.Web/Areas/Receipt/ViewModels/ReceiptHeaderVMs/ReceiptHeaderVM.cs +++ b/HHWMS.Web/Areas/Receipt/ViewModels/ReceiptHeaderVMs/ReceiptHeaderVM.cs @@ -1,7 +1,12 @@ -using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using HHWMS.Web.Models; using WalkingTec.Mvvm.Core; +using Microsoft.AspNetCore.Mvc; +using WalkingTec.Mvvm.Mvc; +using System; +using System.Collections.Generic; +using NPOI.SS.Formula.Functions; namespace HHWMS.Web { @@ -9,12 +14,17 @@ namespace HHWMS.Web { public ReceiptDetailListVM2 Details = new ReceiptDetailListVM2(); public List ReceiptTypes { get; set; } + + private IUnitWork _unitwork; + public ReceiptHeaderVM() { + // 默认构造函数,用于框架创建实例 } protected override void InitVM() { + _unitwork = new UnitWork(Wtm); Details.CopyContext(this); Details.Searcher.HeaderId = Entity.ID; @@ -41,5 +51,390 @@ namespace HHWMS.Web { base.DoDelete(); } + + /// + /// 根据入库单单号获取ERP生产订单数据并更新 + /// + public async Task>> GetReceiptDetailsFromERP(ReceiptDetailVM vm) + { + string receiptCode = vm.Entity.SourceCode; + try + { + // 调用 ERP 接口获取该订单的最新数据 + var request = new LoadMoDetailListRequest + { + ErpCode = "003", + MoCode = receiptCode, // 使用生产订单号查询 + InvCode = vm.Entity?.MaterialCode, + InvDefine1 = vm.Entity?.MaterialType, + StepCode = vm.Entity?.Batch, + Size = 100, + Page = 1 + }; + + var apiResult = await Wtm.CallAPI("ERP", "MOWorkAPI", HttpMethodEnum.POST, request); + + if (apiResult?.Data != null && apiResult.Data.Code == 0 && apiResult.Data.Data?.Count > 0) + { + Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails", + request: $"订单号:{receiptCode},获取到 {apiResult.Data.Data.Count} 条明细", + result: "开始处理订单数据"); + + // 使用相同的逻辑处理订单数据 + await ProcessOrderData(_unitwork, apiResult.Data.Data); + + Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails", + request: $"订单号:{receiptCode}", + result: "订单数据处理完成"); + + return new ApiJsonResult>() + { + Success = true, + Data = apiResult.Data.Data + }; + } + else + { + return new ApiJsonResult>() + { + Success = false, + Msg = apiResult?.Data?.Msg ?? apiResult?.ErrorMsg ?? "未获取到订单数据" + }; + } + } + catch (Exception ex) + { + Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails", + request: $"入库单号:{receiptCode}", + result: $"错误:{ex.Message}"); + return new ApiJsonResult>() + { + Success = false, + Msg = $"获取订单数据失败:{ex.Message}" + }; + } + } + + /// + /// 处理订单数据并保存到本地数据库(照搬 LoadErpOrderJob) + /// + private async Task ProcessOrderData(IUnitWork unitWork, List orderList) + { + // 按订单号分组 + var orderGroups = orderList.GroupBy(x => x.MoCode); + + foreach (var group in orderGroups) + { + var moCode = group.Key; + var orderDetails = group.ToList(); + + // 检查订单是否已存在 + var existingHeader = unitWork.FindSingle(h => h.SourceCode == moCode); + + if (existingHeader == null) + { + // 创建新的入库单头 + var header = new ReceiptHeader + { + SourceCode = moCode, // 生产订单号 + Code = unitWork.GetSerialNumber("REC", 8), // 生成入库单号 + BillType = "生产订单", + TotalQty = orderDetails.Sum(x => x.Qty), + TotalLines = orderDetails.Count, + FirstStatus = ReceiptStatusEnum.新建, + LastStatus = ReceiptStatusEnum.新建, + Remark = orderDetails.First().Memo, + CreateTime = DateTime.Now, + CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", + UpdateVersion = 1 + }; + + unitWork.Add(header); // 保存以获取 HeaderId + + // 获取订单明细中的所有物料编码 + var invCodes = orderDetails + .Where(x => !string.IsNullOrWhiteSpace(x.InvCode)) + .Select(x => x.InvCode) + .Distinct() + .ToList(); + + // 一次性查询本地数据库中已存在的物料(优化性能) + var existingMaterials = unitWork.Find(m => invCodes.Contains(m.MaterialCode)) + .ToList(); + + // 待新增的物料列表 + var newMaterials = new List(); + + // 创建入库单明细 + var details = new List(); + foreach (var item in orderDetails) + { + // 判断物料是否存在,不存在则创建 + if (!string.IsNullOrWhiteSpace(item.InvCode)) + { + var existingMaterial = existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode); + if (existingMaterial == null) + { + // 检查是否已在待新增列表中 + if (!newMaterials.Any(m => m.MaterialCode == item.InvCode)) + { + // 物料不存在,创建新物料 + var newMaterial = new Material + { + MaterialCode = item.InvCode, + MaterialName = item.InvName, + MaterialSpec = item.InvStd, + MaterialType = item.InvDefine1, // 图号 + Qty = 0, + CreateTime = DateTime.Now, + CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" + }; + newMaterials.Add(newMaterial); + } + } + } + + var detail = new ReceiptDetail + { + HeaderId = header.ID, + SourceCode = header.SourceCode, + MaterialCode = item.InvCode, + MaterialType = item.InvDefine1, // 图号 + MaterialName = item.InvName, + Lot = item.MoDetailId, // 明细id + MaterialSpec = item.InvStd, + Qty = item.Qty, + CompletedQty = 0, + Status = ReceiptStatusEnum.新建, + CreateTime = DateTime.Now, + CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", + UpdateVersion = 1 + }; + + details.Add(detail); + + + // 保存工序路线到 MaterialProcess 表 + if (item.ProcessRoute != null && item.ProcessRoute.Count > 0) + { + await SaveProcessRoute(unitWork, item.InvCode, item.ProcessRoute); + } + } + + // 批量新增物料 + if (newMaterials.Count > 0) + { + unitWork.BatchAdd(newMaterials); + } + + unitWork.BatchAdd(details); + + Wtm.DoLog("保存订单数据", ActionLogTypesEnum.Normal, "ProcessOrderData", + request: $"订单号:{moCode}", + result: $"成功创建入库单 {header.Code},明细 {details.Count} 条"); + } + else + { + // 订单已存在,更新数据 + existingHeader.TotalQty = orderDetails.Sum(x => x.Qty); + existingHeader.TotalLines = orderDetails.Count; + existingHeader.Remark = orderDetails.First().Memo; + existingHeader.UpdateVersion++; + existingHeader.UpdateTime = DateTime.Now; + existingHeader.UpdateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"; + unitWork.Update(existingHeader); + + // 更新明细 + var oldDetails = unitWork.Find(d => d.HeaderId == existingHeader.ID).ToList(); + + // 获取订单明细中的所有物料编码 + var invCodes = orderDetails + .Where(x => !string.IsNullOrWhiteSpace(x.InvCode)) + .Select(x => x.InvCode) + .Distinct() + .ToList(); + + // 一次性查询本地数据库中已存在的物料(优化性能) + var existingMaterials = unitWork.Find(m => invCodes.Contains(m.MaterialCode)) + .ToList(); + + // 待新增的物料列表 + var newMaterials = new List(); + + var newDetails = new List(); + foreach (var item in orderDetails) + { + // 判断明细是否存在 用MoDetailId 判断 + if (!oldDetails.Any(x => x.Lot == item.MoDetailId)) + { + // 判断物料是否存在,不存在则创建 + if (!string.IsNullOrWhiteSpace(item.InvCode)) + { + var existingMaterial = existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode); + if (existingMaterial == null) + { + // 检查是否已在待新增列表中 + if (!newMaterials.Any(m => m.MaterialCode == item.InvCode)) + { + // 物料不存在,创建新物料 + var newMaterial = new Material + { + MaterialCode = item.InvCode, + MaterialName = item.InvName, + MaterialSpec = item.InvStd, + MaterialType = item.InvDefine1, // 图号 + Qty = 0, + CreateTime = DateTime.Now, + CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" + }; + newMaterials.Add(newMaterial); + } + } + } + + var detail = new ReceiptDetail + { + HeaderId = existingHeader.ID, + SourceCode = existingHeader.SourceCode, + MaterialCode = item.InvCode, + MaterialType = item.InvDefine1, // 图号 + MaterialName = item.InvName, + Lot = item.MoDetailId, // 明细id + MaterialSpec = item.InvStd, + Qty = item.Qty, + CompletedQty = 0, + Status = ReceiptStatusEnum.新建, + Batch = item.Version, + CreateTime = DateTime.Now, + CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统", + UpdateVersion = 1 + }; + + newDetails.Add(detail); + + // 更新工序路线 + if (item.ProcessRoute != null && item.ProcessRoute.Count > 0) + { + await SaveProcessRoute(unitWork, item.InvCode, item.ProcessRoute); + } + } + } + + // 批量新增物料 + if (newMaterials.Count > 0) + { + unitWork.BatchAdd(newMaterials); + } + + unitWork.BatchAdd(newDetails); + + Wtm.DoLog("更新订单数据", ActionLogTypesEnum.Normal, "ProcessOrderData", + request: $"订单号:{moCode}", + result: $"成功更新入库单 {existingHeader.Code},新增明细 {newDetails.Count} 条"); + } + } + } + + /// + /// 保存工序路线到 MaterialProcess 表 + /// + private async Task SaveProcessRoute(IUnitWork unitWork, string materialCode, List processRoute) + { + // 查询已存在的工序路线 + var existingProcesses = unitWork.Find(p => p.MaterialCode == materialCode).ToList(); + + // 待新增的工序 + var processesToAdd = new List(); + // 待更新的工序 + var processesToUpdate = new List(); + + var processConfigs = unitWork.Find(x => x.Code != "").ToList(); + // 遍历新的工序路线 + foreach (var route in processRoute) + { + // 检查工艺是否存在于ProcessConfig中,不存在则添加 + var processConfig = processConfigs.Any(p => p.Code == route.ProcessCode); + if (!processConfig) + { + unitWork.Add(new ProcessConfig + { + Code = route.ProcessCode, + Name = route.ProcessName, + Zone = "Zone2", + Port = "EnterExit_P2005" + }); + } + + // 查找是否存在相同工序名称的记录 + var existing = existingProcesses.FirstOrDefault(p => + p.ProcessName == route.ProcessName); + + if (existing != null) + { + // 已存在,检查是否需要更新 + bool needUpdate = false; + + if (existing.ProcessCode != route.ProcessCode || existing.ProcessOrder != route.Sort) + { + existing.ProcessCode = route.ProcessCode; + existing.ProcessOrder = route.Sort; + needUpdate = true; + } + + if (!existing.IsActive) + { + existing.IsActive = true; + needUpdate = true; + } + + if (needUpdate) + { + existing.UpdateTime = DateTime.Now; + existing.UpdateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"; + processesToUpdate.Add(existing); + } + } + else + { + // 不存在,新增 + var newProcess = new MaterialProcess + { + MaterialCode = materialCode, + ProcessCode = route.ProcessCode, + ProcessName = route.ProcessName, + ProcessOrder = route.Sort, + Weight = 0, + IsActive = true, + CreateTime = DateTime.Now, + CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统" + }; + processesToAdd.Add(newProcess); + } + } + + // 查找需要删除的工序(旧的多余的) + var processesToDelete = existingProcesses.Where(existing => + !processRoute.Any(route => + route.ProcessName == existing.ProcessName) + ).ToList(); + + // 执行数据库操作 + if (processesToAdd.Count > 0) + { + unitWork.BatchAdd(processesToAdd); + } + + if (processesToUpdate.Count > 0) + { + unitWork.BatchUpdate(processesToUpdate); + } + + if (processesToDelete.Count > 0) + { + unitWork.BatchDelete(processesToDelete); + } + + await Task.CompletedTask; + } } -} +} \ No newline at end of file diff --git a/HHWMS.Web/Areas/Receipt/Views/ReceiptIn/Index.cshtml b/HHWMS.Web/Areas/Receipt/Views/ReceiptIn/Index.cshtml index 7029c29..05699bf 100644 --- a/HHWMS.Web/Areas/Receipt/Views/ReceiptIn/Index.cshtml +++ b/HHWMS.Web/Areas/Receipt/Views/ReceiptIn/Index.cshtml @@ -39,8 +39,8 @@ - - + + @@ -48,7 +48,9 @@ - + + + { form.processName = processOptions.value.find(item => item.value === value).processName; + form.processCode = processOptions.value.find(item => item.value === value).processCode; // 选择工序后,加载订单列表 - loadOrdersByMaterial(form.materialCode); + loadOrdersByMaterial(form.materialCode,form.processCode); focusNext('sourceCodeRef'); } @@ -210,17 +219,14 @@ } // 根据物料加载订单列表 - const loadOrdersByMaterial = (materialCode) => { - if (!materialCode) { - form.sourceCode = ''; - orderOptions.value = []; - return; - } + const loadOrdersByMaterial = (materialCode,batch) => { + form.sourceCode = ''; + form.orderId = ''; orderOptions.value = []; $.ajax({ type: "post", dataType: "json", - data: { materialCode: materialCode }, + data: { materialCode: materialCode,batch: batch}, url: "/receipt/ReceiptIn/GetOrdersByMaterial", success: (json) => { if (json.Code == 200) { @@ -532,6 +538,7 @@ palletCodeRef, materialCodeRef, sourceCodeRef, + billCodeRef, numInput, operatorIdRef, operatorInfoRef, diff --git a/HHWMS.Web/Areas/Receipt/Views/SimpleReceiptIn/Index.cshtml b/HHWMS.Web/Areas/Receipt/Views/SimpleReceiptIn/Index.cshtml index 46bd206..9577fc0 100644 --- a/HHWMS.Web/Areas/Receipt/Views/SimpleReceiptIn/Index.cshtml +++ b/HHWMS.Web/Areas/Receipt/Views/SimpleReceiptIn/Index.cshtml @@ -33,16 +33,21 @@ $refs.sourceCodeSelect.focus(); })"/>/> - + + + + + + public string Maker { get; set; } + + public int Type { get; set; } + public string WhCode { get; set; } } /// diff --git a/HHWMS.Web/Models/Request/Request/LoadMoDetailListRequest.cs b/HHWMS.Web/Models/Request/Request/LoadMoDetailListRequest.cs index 0b6dbb6..ba92462 100644 --- a/HHWMS.Web/Models/Request/Request/LoadMoDetailListRequest.cs +++ b/HHWMS.Web/Models/Request/Request/LoadMoDetailListRequest.cs @@ -34,6 +34,8 @@ namespace HHWMS.Web.Models /// 当前页码,必填 /// public int Page { get; set; } + + public string StepCode { get; set; } } } diff --git a/HHWMS.Web/Resources/Program.zh.resx b/HHWMS.Web/Resources/Program.zh.resx index 1ea25c1..e47e0a9 100644 --- a/HHWMS.Web/Resources/Program.zh.resx +++ b/HHWMS.Web/Resources/Program.zh.resx @@ -1246,7 +1246,7 @@ 数量 - 单号 + 客户单号 唯一码 @@ -1269,6 +1269,9 @@ 物料状态 + + 创建时间 + 任务状态