物料同步,我们首先要搞清楚,同步发生在何时何地。
打开组织的实用程序
打开工作流模板管理
找到图纸发布流程
下载到本地
点击运行,然后使用wcadmin账户登录系统
看到下面的流程图
在靠后的某个OR节点,执行发送物料的表达式(即java代码的方法)
我们仔细看看这部分表达式
代码的具体内容如下:
错误信息=ext.pwst.erp.ERPManager.sendMaterial((wt.fc.WTObject)primaryBusinessObject);
if(错误信息.equals("success")){
result="通过";
}else{
result="错误处理";
}
上述代码就是Windchill与SAP集成涉及到的核心方法的调用。
上述核心代码存放在Windchill服务器的什么位置呢?
我们使用xftp连接Windchill服务器进行查看,如下图所示
那么上述java文件的具体内容是怎样的呢?
我们进入自己的开发环境(Server2016)的eclipse中查看开发环境下,SAP集成代码的内容
package ext.pwst.erp;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.ptc.core.meta.server.TypeIdentifierUtility;
import com.sap.mw.jco.JCO;
import ext.pwst.doc.util.IBAHelper;
import wt.change2.ChangeHelper2;
import wt.change2.WTChangeActivity2;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.maturity.MaturityHelper;
import wt.maturity.PromotionNotice;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.part.WTPart;
import wt.util.WTException;
import wt.vc.VersionControlHelper;
public class ERPManager implements RemoteAccess {
public static Logger log = Logger.getLogger(ERPManager.class);
// 1.
public static String sendMaterial(WTObject obj){
try{
if(!RemoteMethodServer.ServerFlag){
return (String)RemoteMethodServer.getDefault().invoke("sendMaterial",
ERPManager.class.getName(), null, new Class[]{WTObject.class}, new Object[]{obj});
}else{
QueryResult qr = null;
Vector<WTPart> vecPart = new Vector<WTPart>();
if(obj instanceof PromotionNotice){
PromotionNotice pn = (PromotionNotice)obj;
qr = MaturityHelper.service.getPromotionTargets(pn);
}else if(obj instanceof WTChangeActivity2){
WTChangeActivity2 eca = (WTChangeActivity2)obj;
qr = ChangeHelper2.service.getChangeablesAfter(eca);
}
if(qr != null){
while (qr.hasMoreElements()) {
WTObject o = (WTObject)qr.nextElement();
if(o instanceof WTPart){
WTPart part = (WTPart)o;
vecPart.add(part);
}
}
}
//调用2 sendMToSAP()
String ret = sendMToSAP(vecPart);
return ret;
}
}catch(Exception e){
e.printStackTrace();
return e.getMessage();
}
}
/**
* 2. 发送物料到ERP系统
* @param part
* @param mark
* @return
* @throws Exception
*/
public static String sendMToSAP(Vector<WTPart> vecPart) throws Exception{
boolean mark = false;
StringBuffer buf = new StringBuffer();
JCO.Client mConnection = SAPClientUtil.getConnectedClient();
JCO.Function function = SAPClientUtil.createFunction("Z_PDM_MATERIAL", mConnection);//函数名
JCO.Table input = function.getTableParameterList().getTable("IM_TAB");
for(int i = 0; i < vecPart.size(); i++){
WTPart part = vecPart.get(i);
part = (WTPart)VersionControlHelper.service.getLatestIteration(part, false);
input.appendRow();
log.debug("[sendMToSAP]partnumber:" + part.getNumber());
//调用3 setTableValue()
setTableValue(input, part);
mark = true;
}
if(mark){
mConnection.execute(function);
JCO.Table output = function.getTableParameterList().getTable("IT_RETURN");
output.firstRow();
do{
String flag = (String)output.getValue("TYPE");
String number = (String)output.getValue("MATNR");
String message = (String)output.getValue("MESSAGE");
log.debug("number:" + number + ";flag:" + flag + ";message:" + message);
if(flag.equals("E")){
if(message.trim().length() > 0)
buf.append("物料编号为:" + number + " 的物料,传送erp失败。message:" + message + "\n");
}
}while(output.nextRow());
}
if(buf.toString().length() == 0){
buf.append("success");
}
return buf.toString();
}
// 3.
public static void setTableValue(JCO.Table input, WTPart part) throws WTException{
String factory = "";
String pinpai = "";
double penqimianji = 0;
double zhongliang = 0;
String bianma = "";
String miaoshu = "";
String zhongyaodu = "";
String chanpinxinghao = "";
String type = TypeIdentifierUtility.getTypeIdentifier(part).getTypename();
if(type.contains("PWSTXTB.xtbparts")){
factory = "2000";
miaoshu = part.getName();
bianma = part.getNumber();
penqimianji = IBAHelper.getIBAFloatValue(part, "com.tsmi.pengqimianji");
zhongliang = IBAHelper.getIBAFloatValue(part, "com.tsmi.zhongliang");
pinpai = IBAHelper.getIBAStringValue(part, "com.tsmi.pinpai");
pinpai = pinpai == null ? "":pinpai;
String xinghao = IBAHelper.getIBAStringValue(part, "com.tsmi.xinghao");
//如果物料型号属性存在且型号编码不超过18位,则型号作为物料号传给erp,否则传编号
if(xinghao != null && xinghao.length() > 0){
if(xinghao.length() > 18){
bianma = part.getNumber();
miaoshu = part.getName() + " " + xinghao;
}else{
bianma = xinghao;
miaoshu = part.getName() + " " + part.getNumber();
}
}
}else{
//技术部部件 重要度,产品型号
factory = "1000";
//重要度 01 安全 ;02 重要;03一般
}
String unit = part.getDefaultUnit().toString().toUpperCase();
//调用4 toERPUnit()
unit = toERPUnit(unit);
log.debug("[setTableValue]bianma:" + bianma + " miaoshu:" + miaoshu + " unit:" + unit
+ " zhongliang:" + zhongliang + " penqimianji:" + penqimianji
+ " zhongyaodu:" + zhongyaodu + " chanpinxinghao:" + chanpinxinghao + " factory:" + factory + " pinpai:" + pinpai);
input.setValue(bianma, "MATNR"); //物料编码
input.setValue(miaoshu, "MAKTX"); //物料描述
input.setValue(unit, "MEINS"); //单位
input.setValue("Z001", "MTART"); //物料类型
input.setValue(zhongliang, "NTGEW"); //重量
input.setValue(penqimianji, "VOLUM"); //涂装面积
input.setValue(zhongyaodu, "TEMPB"); //重要度
input.setValue(chanpinxinghao, "WRKST"); //产品型号
input.setValue("1000", "PRCTR"); //利润中心
input.setValue(pinpai, "ZEINR"); //品牌
input.setValue("X", "XMCNG"); //是否允许负库存
input.setValue(factory, "WERKS"); //工厂
}
//4.
public static String toERPUnit(String unit){
if(unit.equals("EA")){
return "EA";
}else if(unit.equals("KG")){
return "KG";
}else if(unit.equals("M")){
return "M";
}else if(unit.equals("L")){
return "L";
}else if(unit.equals("SQ_M")){
return "M2";
}else if(unit.equals("MIN")){
return "MIN";
}else if(unit.equals("CU_M")){
return "M3";
}else if(unit.equals("KM")){
return "KM";
}else if(unit.equals("MM")){
return "MM";
}else if(unit.equals("CAN")){
return "CAN";
}else if(unit.equals("DEG")){
return "DEG";
}else if(unit.equals("BLANK")){
return "EA";
}else if(unit.equals("PC")){
return "PC";
}else{
return "";
}
}
}
package ext.pwst.erp;
import java.util.HashMap;
import java.util.Map;
import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import ext.pwst.conf.ConfigUtil;
public class SAPClientUtil {
public static JCO.Client getConnectedClient(){
String clientV = ConfigUtil.getProperty("SAP_CLIENT");
String user = ConfigUtil.getProperty("SAP_USRE");
String pwd = ConfigUtil.getProperty("SAP_PASSWORD");
String lang = ConfigUtil.getProperty("SAP_LANG");
String ipAddress = ConfigUtil.getProperty("SAP_IPADDRESS");
String sync = ConfigUtil.getProperty("SAP_SYSNR");
JCO.Client client = JCO.createClient(clientV, user, pwd, lang, ipAddress, sync);
client.connect();
return client;
}
public static JCO.Function createFunction(String name, JCO.Client client) throws Exception {
JCO.Repository repository = new JCO.Repository("ARAsoft", client);
try {
IFunctionTemplate ft = repository.getFunctionTemplate(name.toUpperCase());
if (ft == null)
return null;
return ft.getFunction();
} catch (Exception ex) {
throw new Exception("Problem retrieving JCO.Function object.");
}
}
public static JCO.Table callRFC(String funcName, String paramName, String paramValue, String returnTab) throws Exception{
Map<String, String> params = new HashMap<String, String>();
params.put(paramName, paramValue);
return callRFC(funcName, params, returnTab);
}
public static JCO.Table callRFC(String funcName, String paramName, String paramValue, String returnTab, JCO.Client client) throws Exception{
Map<String, String> params = new HashMap<String, String>();
params.put(paramName, paramValue);
return callRFC(funcName, params, returnTab, client);
}
public static JCO.Table callRFC(String funcName, Map<String, String> params, String returnTab) throws Exception{
return callRFC(funcName, params).getTableParameterList().getTable(returnTab);
}
public static JCO.Table callRFC(String funcName, Map<String, String> params, String returnTab, JCO.Client client) throws Exception{
return callRFC(funcName, params, client).getTableParameterList().getTable(returnTab);
}
public static JCO.Function callRFC(String funcName, Map<String, String> params) throws Exception{
JCO.Client client = null;
try{
client = SAPClientUtil.getConnectedClient();
return callRFC(funcName, params, client);
}catch(Exception e){
throw e;
}finally{
if(client != null){
client.disconnect();
client = null;
}
}
}
public static JCO.Function callRFC(String funcName, Map<String, String> params, JCO.Client client) throws Exception{
client = SAPClientUtil.getConnectedClient();
JCO.Function function = createFunction(funcName, client);
JCO.ParameterList input = function.getImportParameterList();
if(params != null){
for(String key : params.keySet())
input.setValue(params.get(key), key);
}
client.execute(function);
return function;
}
}