委外库存导入提交

This commit is contained in:
tangyu 2025-01-08 10:34:05 +08:00
parent 9570f8c199
commit bc7e86f51a
2 changed files with 659 additions and 0 deletions

View File

@ -0,0 +1,597 @@
package com.foreverwin.iep.integration.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.foreverwin.iep.common.faas.common.util.StringUtils;
import com.foreverwin.iep.integration.domain.InvokeRequest;
import com.foreverwin.iep.integration.domain.InvokeResponse;
import com.foreverwin.iep.integration.entity.IntInData;
import com.foreverwin.iep.integration.entity.IntInDataLog;
import com.foreverwin.iep.integration.handler.def.sap.*;
import com.foreverwin.iep.integration.service.IntInDataLogService;
import com.foreverwin.iep.integration.service.IntInDataService;
import com.foreverwin.iep.integration.service.RegisterDocumentService;
import com.foreverwin.iep.prd.entity.Item;
import com.foreverwin.iep.prd.mapper.ZLotMaterialCompleteTransactionMapper;
import com.foreverwin.mom.common.core.constant.SecurityConstants;
import com.foreverwin.mom.common.core.exception.BusinessException;
import com.foreverwin.mom.common.core.util.R;
import com.foreverwin.mom.common.data.tenant.TenantContextHolder;
import com.foreverwin.mom.common.log.annotation.SysLog;
import com.foreverwin.mom.common.security.util.SecurityUtils;
import com.foreverwin.mom.wms.api.feign.RemoteWmsInventoryRecordService;
import com.foreverwin.mom.wms.api.feign.RemoteWmsInventoryService;
import com.foreverwin.mom.wms.entity.Inventory;
import com.foreverwin.mom.wms.entity.InventoryRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* 生产管理报工平台
*
* @author mom code generator
* @date 2024-11-11 10:06:18
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/copyShopOrderStatisticTransCallErp")
@Api(value = "copyShopOrderStatisticTransCallErp", tags = "完工入库事务表管理")
public class CopyShopOrderStatisticTransController {
private final GoodMvtCreateService goodMvtCreateService;
private final ZLotMaterialCompleteTransactionMapper zLotMaterialCompleteTransactionMapper;
private final IntInDataService intInDataService;
private final IntInDataLogService intInDataLogService;
private final RegisterDocumentService registerDocumentService;
private final RemoteWmsInventoryRecordService inventoryRecordMapper;
private final InventoryRecordService inventoryRecordService;
private final RemoteWmsInventoryService inventoryService;
@ApiOperation(value = "生产管理报工平台-工单报工", notes = "生产管理报工平台-工单报工")
@SysLog("生产管理报工平台-工单报工")
@PostMapping("/callErp")
public R callErp() {
R<List<InventoryRecord>> byType = inventoryRecordMapper.getByType("0");
List<InventoryRecord> inventoryRecordList = byType.getData();
// if (inventoryRecordList.size() == 0){
// return R.failed("没有需要同步的库存数据");
// }
InvokeResponse invokeResponse = new InvokeResponse();
String site = TenantContextHolder.getTenantId();
List<InventoryRecord> newList = new ArrayList<>();
String bat = SapUtil.noPrefixGenerateReqId();
for (InventoryRecord inventoryRecord : inventoryRecordList) {
newList.add(inventoryRecord);
}
//========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
//========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
if (CollectionUtils.isNotEmpty(newList)) {
// 传参对象
InvokeRequest invokeRequest = new InvokeRequest();
LocalDateTime nowLocalDateTime = LocalDateTime.now();
String nowTime = nowLocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String shopOrder = "";
try {
for (InventoryRecord inventoryRecord : newList) {
shopOrder = inventoryRecord.getShopOrder();
invokeRequest.setTransSid(SapUtil.generateReqId());
invokeRequest.setTransTime(nowTime);
invokeRequest.setFromSystem("IEP");
invokeRequest.setToSystem("ERP");
invokeRequest.setSite(site);
invokeRequest.setMessageId("ZPP_MES_PRODORDCONF");
// 组装抛送的报完呢
String jsonObjectStr = SapUtil.appendHead(assembleMsgBody(inventoryRecord));
invokeRequest.setMessageContent(jsonObjectStr);
invokeRequest.setStart(nowTime);
invokeRequest.setKeyValue(inventoryRecord.getSid());
invokeRequest.setRetry(false);
TenantContextHolder.setTenantId(site);
invokeResponse = inventoryRecordService.call(invokeRequest);
invokeResponse = updateLotComplete(invokeResponse.getObjectResponse(), newList, invokeResponse);
}
} catch (Exception e) {
throw BusinessException.build("工单" + shopOrder + "报工失败!" + invokeResponse.getMessageContent());
} finally {
// 保存日志
saveLogData(invokeRequest, invokeResponse);
}
}
if ("E".equals(invokeResponse.getResult())) {
return R.failed(invokeResponse.getMessageContent());
} else {
R<List<InventoryRecord>> listR = inventoryRecordMapper.selectBySyncStatus();
List<InventoryRecord> inventoryRecords = listR.getData();
for (InventoryRecord inventoryRecord : inventoryRecords) {
processInventoryRecord(inventoryRecord, site);
}
return R.ok();
}
}
private String assembleMsgBody(InventoryRecord statisticsTransaction) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("RFC_FUNCTION", "ZPP_MES_PRODORDCONF");
JSONObject headRe = new JSONObject();
headRe.put("EXCHANGE_SYSTEM", "MES");
headRe.put("EXCHANGE_ID", SapUtil.generateReqId());
headRe.put("EXCHANGE_DOC", "iMES_Call_SAP");
jsonObject.put("REQUEST_CONTEXT", headRe);
JSONObject bodyRe = new JSONObject();
JSONObject isInput = new JSONObject();
isInput.put("AUFNR", statisticsTransaction.getShopOrder());//订单号
isInput.put("VORNR", statisticsTransaction.getOperation());// 工序
isInput.put("PLNBEZ", statisticsTransaction.getWorkshopNumber());//物料号
isInput.put("LMNGA", statisticsTransaction.getQtyDone());//完工数量
isInput.put("XMNGA", statisticsTransaction.getQtyScrapped());//报废数量
isInput.put("MEINH", statisticsTransaction.getMainUom());//基本单位
isInput.put("BUDAT", statisticsTransaction.getSyncPostingDate());//过账日期
isInput.put("ISM01", statisticsTransaction.getLaborTime());//工时
isInput.put("ILE01", "");
isInput.put("ISM02", "");
isInput.put("ILE02", "");
isInput.put("ISM03", statisticsTransaction.getEnergy());//能耗
isInput.put("ILE03", "");
isInput.put("ISM04", statisticsTransaction.getAccessories());//辅料
isInput.put("ILE04", "");
isInput.put("ISM05", statisticsTransaction.getOthersQty());//其他
isInput.put("ILE05", "");
bodyRe.put("IS_INPUT", isInput);
jsonObject.put("REQUEST_BODY", bodyRe);
return jsonObject.toJSONString();
}
// --------------------------------------------- 生产报工取消开始 -----------------------------------------------------
// @ApiOperation(value = "生产管理报工平台-工单取消报工", notes = "生产管理报工平台-工单取消报工")
// @SysLog("生产管理报工平台-工单取消报工")
// @PostMapping("/callErpCancel")
// public R callErpCancel(@RequestBody List<ZShoporderStatisticsTransaction> shoporderStatisticsTransactionList) {
// InvokeResponse invokeResponse = new InvokeResponse();
// List<ZShoporderStatisticsTransaction> newList = new ArrayList<>();
// for (ZShoporderStatisticsTransaction statisticsTransaction : shoporderStatisticsTransactionList) {
// // 重新校验一遍是否已经报过工,防止重复报工
// ZShoporderStatisticsTransaction targetObj = zShoporderStatisticsTransactionMapper.selectById(statisticsTransaction.getSid());
// if (!"S".equals(targetObj.getSyncStatus())) {
// throw BusinessException.build("工单" + targetObj.getShopOrder() + "还未报工无需取消报工");
// } else {
// newList.add(targetObj);
// }
// }
// if (CollectionUtils.isNotEmpty(newList)) {
// // 传参对象
// InvokeRequest invokeRequest = new InvokeRequest();
// LocalDateTime nowLocalDateTime = LocalDateTime.now();
// String nowTime = nowLocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// String shopOrder = "";
// try {
// for (ZShoporderStatisticsTransaction statisticsTransaction : newList) {
// String site = statisticsTransaction.getSite();
// shopOrder = statisticsTransaction.getShopOrder();
// invokeRequest.setTransSid(SapUtil.generateReqId());
// invokeRequest.setTransTime(nowTime);
// invokeRequest.setFromSystem("IEP");
// invokeRequest.setToSystem("ERP");
// invokeRequest.setSite(site);
// invokeRequest.setMessageId("ZPP_MES_PRODOR_CANCEL");
//
// // 组装抛送的报完呢
// String jsonObjectStr = SapUtil.appendHead(assembleCancelMsgBody(statisticsTransaction));
// invokeRequest.setMessageContent(jsonObjectStr);
// invokeRequest.setStart(nowTime);
// invokeRequest.setKeyValue(shopOrder);
// invokeRequest.setRetry(false);
// TenantContextHolder.setTenantId(site);
// invokeResponse = shopOrderStatisticTransCancelService.call(invokeRequest);
// }
// } catch (Exception e) {
// throw BusinessException.build("工单" + shopOrder + "取消报工失败!");
// } finally {
// // 保存日志
// saveLogData(invokeRequest, invokeResponse);
// }
// }
// if ("E".equals(invokeResponse.getResult())) {
// return R.failed(invokeResponse.getMessageContent());
// } else {
// return R.ok();
// }
// }
// private String assembleCancelMsgBody(ZShoporderStatisticsTransaction statisticsTransaction) {
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("RFC_FUNCTION", "ZPP_MES_PRODOR_CANCEL");
// JSONObject headRe = new JSONObject();
// headRe.put("EXCHANGE_SYSTEM", "MES");
// headRe.put("EXCHANGE_ID", SapUtil.generateReqId());
// headRe.put("EXCHANGE_DOC", "iMES_Call_SAP");
// jsonObject.put("REQUEST_CONTEXT", headRe);
// JSONObject bodyRe = new JSONObject();
// JSONObject isInput = new JSONObject();
// isInput.put("AUFNR", statisticsTransaction.getShopOrder()); // 工单
// isInput.put("RUECK", statisticsTransaction.getSyncTransBatch()); // 操作完成的确认编号
// isInput.put("RMZHL", statisticsTransaction.getSyncTicketLine()); // 确认计算器
// isInput.put("VORNR", statisticsTransaction.getSyncTicketNo()); // 活动编号
// bodyRe.put("IS_INPUT", isInput);
// jsonObject.put("REQUEST_BODY", bodyRe);
// return jsonObject.toJSONString();
// }
private void saveLogData(InvokeRequest invokeRequest, InvokeResponse invokeResponse) {
if (StringUtils.isNotEmpty(invokeRequest.getTransSid())) {
// 存储日志
IntInData intInData = new IntInData();
intInData.setSid(invokeRequest.getTransSid());
intInData.setRequest(invokeRequest.getMessageContent());
intInData.setFunctionName(invokeRequest.getMessageId());
intInData.setKeyValue(invokeRequest.getKeyValue());
intInData.setFromSystem(invokeRequest.getFromSystem());
intInData.setToSystem(invokeRequest.getToSystem());
intInData.setRetryCount(0);
intInData.setSite(invokeRequest.getSite());
intInData.setCreator(SecurityUtils.getUserName());
intInData.setDateCreate(LocalDateTime.now());
intInDataService.save(intInData);
IntInDataLog log = new IntInDataLog();
log.setSid(UUID.randomUUID().toString());
log.setIntInDataSid(intInData.getSid());
log.setCreator(SecurityUtils.getUserName());
log.setDateCreate(LocalDateTime.now());
log.setSeq(0);
log.setMessage(StringUtils.isEmpty(invokeResponse.getMessageContent()) ? "N/A" : invokeResponse.getMessageContent());
log.setResponse(invokeResponse.getObjectResponse().toJSONString());
log.setState(invokeResponse.getResult());
intInDataLogService.save(log);
}
}
// --------------------------------- 物料消耗报工开始 -----------------------------------------------
@ApiOperation(value = "生产管理报工平台-物料消耗报工", notes = "生产管理报工平台-物料消耗报工")
@SysLog("生产管理报工平台-物料消耗报工")
@PostMapping("/callErpMaterial")
public R callErpMaterial() {
R<List<InventoryRecord>> byType = inventoryRecordMapper.getByType("1");
List<InventoryRecord> inventoryRecords = byType.getData();
// if (inventoryRecords.size() == 0){
// return R.failed("没有需要同步的库存数据");
// }
InvokeResponse invokeResponse = new InvokeResponse();
List<InventoryRecord> newList = new ArrayList<>();
String site = TenantContextHolder.getTenantId();
String postingDate = "";
JSONArray itemList = new JSONArray();
List<InventoryRecord> srcList = new ArrayList<>();
for (InventoryRecord consumptionTransaction : inventoryRecords) {
// 重新校验一遍是否已经报过工,防止重复报工
R<InventoryRecord> byId = inventoryRecordMapper.getById(consumptionTransaction.getSid());
InventoryRecord targetObj = byId.getData();
if ("S".equals(targetObj.getSyncStatus())) {
throw BusinessException.build("批次" + targetObj.getRollNumber() + "消耗已经报工,不可重复报工");
} else {
postingDate = consumptionTransaction.getSyncPostingDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
targetObj.setSyncPostingDate(consumptionTransaction.getSyncPostingDate());
srcList.add(targetObj);
JSONObject jsonObject = new JSONObject();
// site = consumptionTransaction.getSite();
jsonObject.put("ZEILE", ""); // 物料凭证中的项目
jsonObject.put("MATNR", consumptionTransaction.getWorkshopNumber()); // 物料编号
jsonObject.put("MATKL", ""); // 物料组
jsonObject.put("WERKS", site); // 工厂
jsonObject.put("LGORT", consumptionTransaction.getErpInventory()); // 存储地点
jsonObject.put("CHARG", consumptionTransaction.getSapBatchNo()); // 批次号
jsonObject.put("BWART", "261"); // 移动类型
jsonObject.put("SOBKZ", ""); // 特殊库存标识
jsonObject.put("AUFNR", consumptionTransaction.getShopOrder()); // 订单号
jsonObject.put("EBELN", ""); // 采购订单号
jsonObject.put("EBELP", ""); // 采购凭证的项目编号
jsonObject.put("MENGE", consumptionTransaction.getMainQty()); // 数量
jsonObject.put("MEINS", consumptionTransaction.getMainUom()); // 基本计量单位
if(consumptionTransaction.getSubQty()!=null){
jsonObject.put("ERFMG", consumptionTransaction.getSubQty()); // 以录入项单位表示的数量/
jsonObject.put("ERFME", consumptionTransaction.getSubUom()); // 录入单位/
}else{
jsonObject.put("ERFMG", null); // 以录入项单位表示的数量
jsonObject.put("ERFME", ""); // 录入单位
}
jsonObject.put("MAT_KDAUF", ""); // 评估销售定单库存的销售定单号
jsonObject.put("MAT_KDPOS", ""); // 评估销售订单库存的销售订单项目
jsonObject.put("UMWRK", ""); // 收货工厂/发货工厂
jsonObject.put("UMLGO", ""); // 收货/发货库存地点
jsonObject.put("UMCHA", ""); // 收货/发货批量
jsonObject.put("RSNUM", consumptionTransaction.getReservedNo()); // 预留/相关需求的编号
jsonObject.put("RSPOS", consumptionTransaction.getReservedLine()); // 预留 / 相关需求的项目编号
itemList.add(jsonObject);
newList.add(targetObj);
}
}
if (itemList.size() > 0) {
InvokeRequest invokeRequest = new InvokeRequest();
// 传参对象
try {
LocalDateTime nowLocalDateTime = LocalDateTime.now();
String nowTime = nowLocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
invokeRequest.setTransSid(SapUtil.generateReqId());
invokeRequest.setTransTime(nowTime);
invokeRequest.setFromSystem("IEP");
invokeRequest.setToSystem("ERP");
invokeRequest.setSite(site);
invokeRequest.setMessageId("ZMM_MES_GOODSMVT_CREATE");
// 组装抛送的报完呢
String jsonObjectStr = SapUtil.appendHead(assembleMaterialMsgBody(itemList, postingDate));
invokeRequest.setMessageContent(jsonObjectStr);
invokeRequest.setStart(nowTime);
invokeRequest.setKeyValue(nowTime);
invokeRequest.setRetry(false);
TenantContextHolder.setTenantId(site);
invokeResponse = goodMvtCreateService.call(invokeRequest);
// 处理ERP返回的报文
invokeResponse = updateMaterialConsumption(invokeResponse.getObjectResponse(), srcList, invokeResponse);
} catch (Exception e) {
// 不用回滚,将错误抛出
invokeResponse.setResult("E");
invokeResponse.setMessageContent(e.getMessage());
}
}
if ("E".equals(invokeResponse.getResult())) {
return R.failed("SAP错误:"+invokeResponse.getMessageContent());
} else {
return R.ok();
}
}
public InvokeResponse updateMaterialConsumption(JSONObject jsonObject, List<InventoryRecord> srcList, InvokeResponse invokeResponse) {
JSONObject bodyJson = jsonObject.getJSONObject("RESPONSE_BODY");
JSONObject outJson = bodyJson.getJSONObject("ES_OUTPUT");
String result = outJson.getString("MSGTY");
String message = outJson.getString("MSGTX");
String msgno = outJson.getString("MSGNO"); // 系统消息编号
String mblnr = outJson.getString("MBLNR"); // 物料凭证编号
for (InventoryRecord targetObj : srcList) {
targetObj.setSyncStatus(result);
targetObj.setSyncMessageText(message);
targetObj.setSyncMessageNo(msgno);
targetObj.setSyncSubmitter(SecurityUtils.getUserName());
targetObj.setSyncVoucherNo(mblnr);
targetObj.setCreator(SecurityUtils.getUserName());
targetObj.setDateCreate(LocalDateTime.now());
R r = inventoryRecordMapper.apiUpdateById(targetObj);
r.getMsg();
}
invokeResponse.setResult(result);
invokeResponse.setMessageContent(message);
return invokeResponse;
}
// 凭证中的过账日期 yyyy-MM-dd
private String assembleMaterialMsgBody(JSONArray itemList, String postingDate) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("RFC_FUNCTION", "ZMM_MES_GOODSMVT_CREATE");
JSONObject headRe = new JSONObject();
headRe.put("EXCHANGE_SYSTEM", "MES");
headRe.put("EXCHANGE_ID", SapUtil.generateReqId());
headRe.put("EXCHANGE_DOC", "iMES_Call_SAP");
jsonObject.put("REQUEST_CONTEXT", headRe);
JSONObject bodyRe = new JSONObject();
JSONObject headBody = new JSONObject();
headBody.put("ZPARTNER", SecurityUtils.getUserName()); // 员工BP编号
headBody.put("BUDAT", postingDate); // 凭证中的过账日期
bodyRe.put("IS_HEAD", headBody);
bodyRe.put("IT_ITEM", itemList);
jsonObject.put("REQUEST_BODY", bodyRe);
return jsonObject.toJSONString();
}
// --------------------------------- 批次完工入库报工开始 -----------------------------------------------
@ApiOperation(value = "生产管理报工平台-批次完工入库获取SAP批次号", notes = "生产管理报工平台-批次完工入库获取SAP批次号")
@SysLog("生产管理报工平台-批次完工入库获取SAP批次号")
@PostMapping("/api/getSapBatchNo")
public R<String> getSapBatchNo(@RequestBody String erpItem) {
String sapBatchNo = registerDocumentService.getSapBatchNo(erpItem);
return R.ok(sapBatchNo);
}
@ApiOperation(value = "生产管理报工平台-批次完工入库报工", notes = "生产管理报工平台-批次完工入库报工")
@SysLog("生产管理报工平台-批次完工入库报工")
@PostMapping("/callErpLot")
public R callErpLot() {
R<List<InventoryRecord>> byType = inventoryRecordMapper.getByType("0");
List<InventoryRecord> inventoryRecords = byType.getData();
// if (inventoryRecords.size() == 0){
// return R.failed("没有需要同步的库存数据");
// }
InvokeResponse invokeResponse = new InvokeResponse();
List<InventoryRecord> newList = new ArrayList<>();
String site = "";
String postingDate = "";
JSONArray itemList = new JSONArray();
List<InventoryRecord> srcList = new ArrayList<>();
for (InventoryRecord completeTransaction : inventoryRecords) {
// 重新校验一遍是否已经报过工,防止重复报工
R<InventoryRecord> byId = inventoryRecordMapper.getById(completeTransaction.getSid());
InventoryRecord targetObj = byId.getData();
if ("S".equals(targetObj.getSyncStatus())) {
throw BusinessException.build("批次" + targetObj.getRollNumber() + "完工已经报工,不可重复报工");
} else {
postingDate = completeTransaction.getSyncPostingDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
targetObj.setSyncPostingDate(completeTransaction.getSyncPostingDate());
srcList.add(targetObj);
JSONObject jsonObject = new JSONObject();
site = completeTransaction.getSite();
jsonObject.put("ZEILE", null); // 物料凭证中的项目
jsonObject.put("MATNR", completeTransaction.getWorkshopNumber()); // 物料编号
jsonObject.put("MATKL", ""); // 物料组
jsonObject.put("WERKS", site); // 工厂
jsonObject.put("LGORT", completeTransaction.getErpInventory()); // 存储地点
jsonObject.put("CHARG", completeTransaction.getSapBatchNo()); // 批次号
jsonObject.put("BWART", "101"); // 移动类型
jsonObject.put("SOBKZ", ""); // 特殊库存标识
jsonObject.put("AUFNR", completeTransaction.getShopOrder()); // 订单号
jsonObject.put("EBELN", ""); // 采购订单号
jsonObject.put("EBELP", ""); // 采购凭证的项目编号
jsonObject.put("MENGE", completeTransaction.getMainQty()); // 数量
jsonObject.put("MEINS", completeTransaction.getMainUom()); // 基本计量单位
if(completeTransaction.getSubQty()!=null){
jsonObject.put("ERFMG", completeTransaction.getSubQty()); // 以录入项单位表示的数量
jsonObject.put("ERFME", completeTransaction.getSubUom()); // 录入单位
}else{
jsonObject.put("ERFMG", null); // 以录入项单位表示的数量
jsonObject.put("ERFME", ""); // 录入单位
}
jsonObject.put("MAT_KDAUF", completeTransaction.getSoNo()); // 评估销售定单库存的销售定单号
if(StringUtils.isNotEmpty(completeTransaction.getSoLineNo())){
jsonObject.put("MAT_KDPOS", Integer.valueOf(completeTransaction.getSoLineNo())); // 评估销售订单库存的销售订单项目
}else{
jsonObject.put("MAT_KDPOS", null); // 评估销售订单库存的销售订单项目
}
jsonObject.put("UMWRK", ""); // 收货工厂/发货工厂
jsonObject.put("UMLGO", ""); // 收货/发货库存地点
jsonObject.put("UMCHA", ""); // 收货/发货批量
jsonObject.put("RSNUM", ""); // 预留/相关需求的编号
jsonObject.put("RSPOS", ""); // 预留 / 相关需求的项目编号
jsonObject.put("Z_JH", completeTransaction.getBarcode()); // 卷号
jsonObject.put("Z_CVN", completeTransaction.getExternalLot()); // 客户卷号
jsonObject.put("Z_GRADE", completeTransaction.getGrade()); // 等级
itemList.add(jsonObject);
newList.add(targetObj);
}
}
if (itemList.size() > 0) {
InvokeRequest invokeRequest = new InvokeRequest();
// 传参对象
try {
LocalDateTime nowLocalDateTime = LocalDateTime.now();
String nowTime = nowLocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
invokeRequest.setTransSid(SapUtil.generateReqId());
invokeRequest.setTransTime(nowTime); // 设定过账日期
invokeRequest.setFromSystem("IEP");
invokeRequest.setToSystem("ERP");
invokeRequest.setSite(site);
invokeRequest.setMessageId("ZMM_MES_GOODSMVT_CREATE");
// 组装抛送的报完呢
String jsonObjectStr = SapUtil.appendHead(assembleMaterialMsgBody(itemList, postingDate));
invokeRequest.setMessageContent(jsonObjectStr);
invokeRequest.setStart(nowTime);
invokeRequest.setKeyValue(nowTime);
invokeRequest.setRetry(false);
TenantContextHolder.setTenantId(site);
invokeResponse = goodMvtCreateService.call(invokeRequest);
// 处理ERP返回的报文
invokeResponse = updateLotComplete(invokeResponse.getObjectResponse(), srcList, invokeResponse);
} catch (Exception e) {
// 不用回滚,将错误抛出
invokeResponse.setResult("E");
invokeResponse.setMessageContent(e.getMessage());
}
}
if ("E".equals(invokeResponse.getResult())) {
return R.failed("SAP错误:"+invokeResponse.getMessageContent());
} else {
return R.ok();
}
}
public InvokeResponse updateLotComplete(JSONObject jsonObject, List<InventoryRecord> srcList, InvokeResponse invokeResponse) {
JSONObject bodyJson = jsonObject.getJSONObject("RESPONSE_BODY");
JSONObject outJson = bodyJson.getJSONObject("ES_OUTPUT");
String rmzhl = outJson.getString("RMZHL");
String rueck = outJson.getString("RUECK");
String result = outJson.getString("MSGTY");
String message = outJson.getString("MSGTX");
String msgNo = outJson.getString("MSGNO");
String mblnr = outJson.getString("MBLNR");
String msgid = outJson.getString("MSGID");// 物料凭证编号
for (InventoryRecord targetObj : srcList) {
targetObj.setSyncTicketLine(rmzhl);
targetObj.setSyncTicketNo(rueck);
targetObj.setSyncStatus(result);
targetObj.setSyncMessageText(message);
targetObj.setSyncMessageNo(msgNo);
targetObj.setSyncVoucherNo(mblnr);
targetObj.setSyncSubmitter(SecurityUtils.getUserName());
targetObj.setCreator(SecurityUtils.getUserName());
targetObj.setDateCreate(LocalDateTime.now());
targetObj.setSyncMessageRemark(msgid);
R r = inventoryRecordMapper.apiUpdateById(targetObj);
r.getMsg();
}
invokeResponse.setResult(result);
invokeResponse.setMessageContent(message);
return invokeResponse;
}
private void processInventoryRecord(InventoryRecord inventoryRecord, String site) {
String user = SecurityUtils.getUserName();
String erpInventory = inventoryRecord.getErpInventory();
String barcode = inventoryRecord.getBarcode();
String status = inventoryRecord.getStatus();
String workshopNumber = inventoryRecord.getWorkshopNumber();
String sapBatchNo = inventoryRecord.getSapBatchNo();
String qtyDone = inventoryRecord.getQtyDone();
String dateOfManufacture = inventoryRecord.getDateOfManufacture();
String grade = inventoryRecord.getGrade();
String uom = inventoryRecord.getUom();
String subUom = inventoryRecord.getSubUom();
String subQty = inventoryRecord.getSubQty();
String inventoryBo = Inventory.genHandle(site, barcode);
Inventory inventory = new Inventory();
inventory.setHandle(inventoryBo);
inventory.setSite(site);
inventory.setCreator(user);
inventory.setCreateTime(LocalDateTime.now());
R<Item> itemR = inventoryService.selectCurrentByMes(site, workshopNumber);
Item itemObj = itemR.getData();
inventory.setItemBo(itemObj.getHandle());
inventory.setUnitOfMeasure(itemObj.getUnitOfMeasure());
inventory.setErpStorageLocation(erpInventory);
inventory.setStatus(status);
inventory.setInventoryId(barcode);
inventory.setItem(workshopNumber);
inventory.setBatchNo(sapBatchNo);
BigDecimal qtyDones = new BigDecimal(qtyDone);
inventory.setOriginalQty(qtyDones);
inventory.setQtyOnHand(qtyDones);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
inventory.setProductionDate(LocalDate.parse(dateOfManufacture, dateTimeFormatter).atTime(0, 0));
inventory.setQualityInspectionStatus(grade);
inventory.setRollType(uom);
inventory.setAuxiliaryUnit(subUom);
inventory.setAuxiliaryOty(subQty);
inventoryService.createInventory(inventory);
}
}

View File

@ -0,0 +1,62 @@
package com.foreverwin.iep.integration.handler.def.sap;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.iep.integration.domain.InvokeRequest;
import com.foreverwin.iep.integration.domain.InvokeResponse;
import com.foreverwin.iep.integration.service.HandlerService;
import com.foreverwin.iep.integration.utils.JS;
import com.foreverwin.iep.integration.utils.Local;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
/**
* 生产订单报工接口
*/
@AllArgsConstructor
@Service("IN_MES_PRODORDCONF")
public class InventoryRecordService implements HandlerService {
@Override
public InvokeResponse call(InvokeRequest invokeRequest) {
InvokeResponse invokeResponse = new InvokeResponse(invokeRequest);
try {
// 处理响应
invokeResponse = SapUtil.sendSap(invokeRequest.getMessageContent());
invokeResponse.setTransSid(invokeRequest.getTransSid());
invokeResponse.setTransTime(Local.now());
if(!"E".equals(invokeResponse.getResult())){
// 请求处理成功
JSONObject jsonObject = invokeResponse.getObjectResponse();
// 处理结果事务
invokeResponse = updateShopOrderStatistic(jsonObject,invokeRequest.getKeyValue(),invokeResponse);
}
return invokeResponse;
} catch (Exception e) {
invokeResponse.setResult("E");
invokeResponse.setMessageContent(e.getMessage());
return invokeResponse;
}
}
@Override
public String validation(InvokeRequest invokeRequest) {
JSONObject jsonObject = JS.parseObject(invokeRequest.getMessageContent());
return null;
}
public InvokeResponse updateShopOrderStatistic(JSONObject jsonObject, String sid, InvokeResponse invokeResponse) {
JSONObject bodyJson = jsonObject.getJSONObject("RESPONSE_BODY");
JSONObject outJson = bodyJson.getJSONObject("ES_OUTPUT");
String result = outJson.getString("MSGTY");
invokeResponse.setResult(result);
String message = outJson.getString("MSGTX");
invokeResponse.setMessageContent(message);
return invokeResponse;
}
}