//第二步:构造环境上下文对象,传入login,并按需设置其它上下文参数
U8EnvContext envContext = new U8EnvContext();
envContext.U8Login = u8Login;
//第三步:设置API地址标识(Url)
//当前API:添加新单据的地址标识为:U8API/ProductIn/Add
U8ApiAddress myApiAddress = new U8ApiAddress("U8API/ProductIn/Add");
//第四步:构造APIBroker
U8ApiBroker broker = new U8ApiBroker(myApiAddress, envContext);
//第五步:API参数赋值
//给普通参数sVouchType赋值。此参数的数据类型为System.String,此参数按值传递,表示单据类型:11
broker.AssignNormalValue("sVouchType", "10");
MSXML2.IXMLDOMDocument2 docHead = new DOMDocument();
MSXML2.IXMLDOMDocument2 docBody = new DOMDocument();
switch (iType)
{
#region 生产订单
case 0:
{
docHead.load(Server.MapPath("./") + @"XML\rdrecord10_scdd.xml");
docBody.load(Server.MapPath("./") + @"XML\rdrecords10_scdd.xml");
MSXML2.IXMLDOMNode nodeHead = docHead.selectSingleNode("//z:row");
dtHead.Rows[0]["ddate"] = u8Login.CurDate;
dtHead.Rows[0]["cmaker"] = u8Login.cUserName;
IXMLDOMElement xeHead = (IXMLDOMElement)nodeHead;
xeHead.setAttribute("cwhcode", dtHead.Rows[0]["cwhcode"].ToString());
xeHead.setAttribute("ddate", dtHead.Rows[0]["ddate"].ToString());
// xeHead.setAttribute("ccode", dtHead.Rows[0]["ccode"].ToString());
// xeHead.setAttribute("crdcode", dtHead.Rows[0]["crdcode"].ToString());
xeHead.setAttribute("cdepcode", dtHead.Rows[0]["cdepcode"].ToString());
xeHead.setAttribute("cmaker", dtHead.Rows[0]["cmaker"].ToString());
xeHead.setAttribute("csource", "生产订单");
xeHead.setAttribute("cbustype", "成品入库");
xeHead.setAttribute("vt_id", dtHead.Rows[0]["vt_id"].ToString());
// xeHead.setAttribute("imquantity", dtHead.Rows[0]["imquantity"].ToString());
// xeHead.setAttribute("cpspcode", dtHead.Rows[0]["cpspcode"].ToString());
xeHead.setAttribute("cmpocode", dtHead.Rows[0]["cmpocode"].ToString());
xeHead.setAttribute("iproorderid", dtHead.Rows[0]["iproorderid"].ToString());
xeHead.setAttribute("crdcode", dtHead.Rows[0]["cRdCode"].ToString());//入库类别
xeHead.setAttribute("crdname", dtHead.Rows[0]["cRdName"].ToString());//入库类别
int irowno = 1;
MSXML2.IXMLDOMNode nodeBody = docBody.selectSingleNode("//z:row");
IXMLDOMElement xeBody = (IXMLDOMElement)nodeBody;
foreach (DataRow datarow in dtBody.Rows)
{
if (Convert.ToDouble(datarow["ihasquantity"].ToString()) <= 0) continue;
if (irowno != 1)
{
IXMLDOMNode newNode = nodeBody.cloneNode(false);
IXMLDOMElement xeBody1 = (IXMLDOMElement)newNode;
xeBody1.setAttribute("cinvcode", datarow["cinvcode"].ToString());
xeBody1.setAttribute("cinvm_unit", datarow["cinvm_unit"].ToString());
xeBody1.setAttribute("cposition", datarow["cposition"].ToString());
xeBody1.setAttribute("cbatch", datarow["cbatch"].ToString());
xeBody1.setAttribute("iquantity", datarow["ihasquantity"].ToString());
xeBody1.setAttribute("inquantity", datarow["inquantity"].ToString());
xeBody1.setAttribute("impoids", datarow["impoids"].ToString());
xeBody1.setAttribute("cposname", datarow["cposname"].ToString());
xeBody1.setAttribute("cmocode", datarow["cmocode"].ToString());
xeBody1.setAttribute("imoseq", datarow["imoseq"].ToString());
xeBody1.setAttribute("isotype", datarow["isotype"].ToString());
xeBody1.setAttribute("iordertype", datarow["iordertype"].ToString());
xeBody1.setAttribute("iorderdid", datarow["iorderdid"].ToString());
xeBody1.setAttribute("iexpiratdatecalcu", datarow["iexpiratdatecalcu"].ToString());
xeBody1.setAttribute("irowno", irowno);
xeBody1.setAttribute("cinvouchtype", datarow["cinvouchtype"].ToString());
docBody.selectSingleNode("//rs:data").appendChild(newNode);
}
else
{
xeBody.setAttribute("cinvcode", datarow["cinvcode"].ToString());
xeBody.setAttribute("cinvm_unit", datarow["cinvm_unit"].ToString());
xeBody.setAttribute("cposition", datarow["cposition"].ToString());
xeBody.setAttribute("cbatch", datarow["cbatch"].ToString());
xeBody.setAttribute("iquantity", datarow["ihasquantity"].ToString());
xeBody.setAttribute("inquantity", datarow["inquantity"].ToString());
xeBody.setAttribute("impoids", datarow["impoids"].ToString());
xeBody.setAttribute("cposname", datarow["cposname"].ToString());
xeBody.setAttribute("cmocode", datarow["cmocode"].ToString());
xeBody.setAttribute("imoseq", datarow["imoseq"].ToString());
xeBody.setAttribute("isotype", datarow["isotype"].ToString());
xeBody.setAttribute("iordertype", datarow["iordertype"].ToString());
xeBody.setAttribute("iorderdid", datarow["iorderdid"].ToString());
xeBody.setAttribute("iexpiratdatecalcu", datarow["iexpiratdatecalcu"].ToString());
xeBody.setAttribute("irowno", irowno);
xeBody.setAttribute("cinvouchtype", datarow["cinvouchtype"].ToString());
}
irowno++;
}
}
break;
#endregion
#region 产品检验单
case 1:
{
docHead.load(Server.MapPath("./") + @"XML\rdrecord10_cpjyd.xml");
docBody.load(Server.MapPath("./") + @"XML\rdrecords10_cpjyd.xml");
MSXML2.IXMLDOMNode nodeHead = docHead.selectSingleNode("//z:row");
dtHead.Rows[0]["ddate"] = u8Login.CurDate;
dtHead.Rows[0]["cmaker"] = u8Login.cUserName;
IXMLDOMElement xeHead = (IXMLDOMElement)nodeHead;
xeHead.setAttribute("cwhcode", dtHead.Rows[0]["cwhcode"].ToString());
xeHead.setAttribute("ddate", dtHead.Rows[0]["ddate"].ToString());
xeHead.setAttribute("cdepcode", dtHead.Rows[0]["cdepcode"].ToString());
xeHead.setAttribute("cmaker", dtHead.Rows[0]["cmaker"].ToString());
xeHead.setAttribute("csource", "产品检验单");
xeHead.setAttribute("cbustype", "成品入库");
xeHead.setAttribute("vt_id", dtHead.Rows[0]["vt_id"].ToString());
xeHead.setAttribute("imquantity", dtHead.Rows[0]["imquantity"].ToString());
xeHead.setAttribute("cmpocode", dtHead.Rows[0]["cmpocode"].ToString());
xeHead.setAttribute("iproorderid", dtHead.Rows[0]["iproorderid"].ToString());
int irowno = 1;
MSXML2.IXMLDOMNode nodeBody = docBody.selectSingleNode("//z:row");
IXMLDOMElement xeBody = (IXMLDOMElement)nodeBody;
foreach (DataRow datarow in dtBody.Rows)
{
if (Convert.ToDouble(datarow["ihasquantity"].ToString()) <= 0) continue;
if (irowno != 1)
{
IXMLDOMNode newNode = nodeBody.cloneNode(false);
IXMLDOMElement xeBody1 = (IXMLDOMElement)newNode;
xeBody1.setAttribute("cinvcode", datarow["cinvcode"].ToString());
xeBody1.setAttribute("cinvm_unit", datarow["cinvm_unit"].ToString());
xeBody1.setAttribute("cposition", datarow["cposition"].ToString());
xeBody1.setAttribute("cbatch", datarow["cbatch"].ToString());
xeBody1.setAttribute("iquantity", datarow["ihasquantity"].ToString());
xeBody1.setAttribute("inquantity", datarow["inquantity"].ToString());
xeBody1.setAttribute("impoids", datarow["impoids"].ToString());
xeBody1.setAttribute("ccheckcode", datarow["ccheckcode"].ToString());
xeBody1.setAttribute("icheckidbaks", datarow["icheckidbaks"].ToString());
xeBody1.setAttribute("ccheckpersoncode", datarow["ccheckpersoncode"].ToString());
xeBody1.setAttribute("dcheckdate", datarow["dcheckdate"].ToString());
xeBody1.setAttribute("brelated", datarow["brelated"].ToString());
xeBody1.setAttribute("cmocode", datarow["cmocode"].ToString());
xeBody1.setAttribute("imoseq", datarow["imoseq"].ToString());
xeBody1.setAttribute("isotype", datarow["isotype"].ToString());
xeBody1.setAttribute("iordertype", datarow["iordertype"].ToString());
xeBody1.setAttribute("iorderdid", datarow["iorderdid"].ToString());
xeBody1.setAttribute("iexpiratdatecalcu", datarow["iexpiratdatecalcu"].ToString());
xeBody1.setAttribute("irowno", irowno);
xeBody1.setAttribute("cinvouchtype", datarow["cinvouchtype"].ToString());
xeBody1.setAttribute("imergecheckautoid", datarow["imergecheckautoid"].ToString());
docBody.selectSingleNode("//rs:data").appendChild(newNode);
}
else
{
xeBody.setAttribute("cinvcode", datarow["cinvcode"].ToString());
xeBody.setAttribute("cinvm_unit", datarow["cinvm_unit"].ToString());
xeBody.setAttribute("cposition", datarow["cposition"].ToString());
xeBody.setAttribute("cbatch", datarow["cbatch"].ToString());
xeBody.setAttribute("iquantity", datarow["ihasquantity"].ToString());
xeBody.setAttribute("inquantity", datarow["inquantity"].ToString());
xeBody.setAttribute("impoids", datarow["impoids"].ToString());
xeBody.setAttribute("ccheckcode", datarow["ccheckcode"].ToString());
xeBody.setAttribute("icheckidbaks", datarow["icheckidbaks"].ToString());
xeBody.setAttribute("ccheckpersoncode", datarow["ccheckpersoncode"].ToString());
xeBody.setAttribute("dcheckdate", datarow["dcheckdate"].ToString());
xeBody.setAttribute("brelated", datarow["brelated"].ToString());
xeBody.setAttribute("cmocode", datarow["cmocode"].ToString());
xeBody.setAttribute("imoseq", datarow["imoseq"].ToString());
xeBody.setAttribute("isotype", datarow["isotype"].ToString());
xeBody.setAttribute("iordertype", datarow["iordertype"].ToString());
xeBody.setAttribute("iorderdid", datarow["iorderdid"].ToString());
xeBody.setAttribute("iexpiratdatecalcu", datarow["iexpiratdatecalcu"].ToString());
xeBody.setAttribute("irowno", irowno);
xeBody.setAttribute("cinvouchtype", datarow["cinvouchtype"].ToString());
xeBody.setAttribute("imergecheckautoid", datarow["imergecheckautoid"].ToString());
}
irowno++;
}
}
break;
#endregion
#region 不良品
case 2:
{
docHead.load(Server.MapPath("./") + @"XML\rdrecord10_blpcl.xml");
docBody.load(Server.MapPath("./") + @"XML\rdrecords10_blpcl.xml");
MSXML2.IXMLDOMNode nodeHead = docHead.selectSingleNode("//z:row");
dtHead.Rows[0]["ddate"] = u8Login.CurDate;
dtHead.Rows[0]["cmaker"] = u8Login.cUserName;
IXMLDOMElement xeHead = (IXMLDOMElement)nodeHead;
xeHead.setAttribute("cwhcode", dtHead.Rows[0]["cwhcode"].ToString());
xeHead.setAttribute("ddate", dtHead.Rows[0]["ddate"].ToString());
// xeHead.setAttribute("ccode", dtHead.Rows[0]["ccode"].ToString());
//xeHead.setAttribute("crdcode", dtHead.Rows[0]["crdcode"].ToString());
xeHead.setAttribute("cdepcode", dtHead.Rows[0]["cdepcode"].ToString());
xeHead.setAttribute("cmaker", dtHead.Rows[0]["cmaker"].ToString());
xeHead.setAttribute("csource", "产品不良品处理单");
xeHead.setAttribute("cbustype", "成品入库");
xeHead.setAttribute("vt_id", dtHead.Rows[0]["vt_id"].ToString());
xeHead.setAttribute("imquantity", dtHead.Rows[0]["imquantity"].ToString());
xeHead.setAttribute("cmpocode", dtHead.Rows[0]["cmpocode"].ToString());
xeHead.setAttribute("iproorderid", dtHead.Rows[0]["iproorderid"].ToString());
int irowno = 1;
MSXML2.IXMLDOMNode nodeBody = docBody.selectSingleNode("//z:row");
IXMLDOMElement xeBody = (IXMLDOMElement)nodeBody;
foreach (DataRow datarow in dtBody.Rows)
{
if (Convert.ToDouble(datarow["ihasquantity"].ToString()) <= 0) continue;
if (irowno != 1)
{
IXMLDOMNode newNode = nodeBody.cloneNode(false);
IXMLDOMElement xeBody1 = (IXMLDOMElement)newNode;
xeBody1.setAttribute("cinvcode", datarow["cinvcode"].ToString());
xeBody1.setAttribute("cinvm_unit", datarow["cinvm_unit"].ToString());
xeBody1.setAttribute("cposition", datarow["cposition"].ToString());
xeBody1.setAttribute("cbatch", datarow["cbatch"].ToString());
xeBody1.setAttribute("iquantity", datarow["ihasquantity"].ToString());
xeBody1.setAttribute("inquantity", datarow["inquantity"].ToString());
xeBody1.setAttribute("impoids", datarow["impoids"].ToString());
xeBody1.setAttribute("ccheckcode", datarow["ccheckcode"].ToString());
xeBody1.setAttribute("icheckidbaks", datarow["icheckidbaks"].ToString());
xeBody1.setAttribute("crejectcode", datarow["crejectcode"].ToString());
xeBody1.setAttribute("irejectids", datarow["irejectids"].ToString());
xeBody1.setAttribute("ccheckpersoncode", datarow["ccheckpersoncode"].ToString());
xeBody1.setAttribute("dcheckdate", datarow["dcheckdate"].ToString());
xeBody1.setAttribute("cmocode", datarow["cmocode"].ToString());
xeBody1.setAttribute("imoseq", datarow["imoseq"].ToString());
xeBody1.setAttribute("isotype", datarow["isotype"].ToString());
xeBody1.setAttribute("iordertype", datarow["iordertype"].ToString());
xeBody1.setAttribute("iorderdid", datarow["iorderdid"].ToString());
xeBody1.setAttribute("iexpiratdatecalcu", datarow["iexpiratdatecalcu"].ToString());
xeBody1.setAttribute("irowno", irowno);
xeBody1.setAttribute("cinvouchtype", datarow["cinvouchtype"].ToString());
docBody.selectSingleNode("//rs:data").appendChild(newNode);
}
else
{
xeBody.setAttribute("cinvcode", datarow["cinvcode"].ToString());
xeBody.setAttribute("cinvm_unit", datarow["cinvm_unit"].ToString());
xeBody.setAttribute("cposition", datarow["cposition"].ToString());
xeBody.setAttribute("cbatch", datarow["cbatch"].ToString());
xeBody.setAttribute("iquantity", datarow["ihasquantity"].ToString());
xeBody.setAttribute("inquantity", datarow["inquantity"].ToString());
xeBody.setAttribute("impoids", datarow["impoids"].ToString());
xeBody.setAttribute("ccheckcode", datarow["ccheckcode"].ToString());
xeBody.setAttribute("icheckidbaks", datarow["icheckidbaks"].ToString());
xeBody.setAttribute("crejectcode", datarow["crejectcode"].ToString());
xeBody.setAttribute("irejectids", datarow["irejectids"].ToString());
xeBody.setAttribute("ccheckpersoncode", datarow["ccheckpersoncode"].ToString());
xeBody.setAttribute("dcheckdate", datarow["dcheckdate"].ToString());
xeBody.setAttribute("cmocode", datarow["cmocode"].ToString());
xeBody.setAttribute("imoseq", datarow["imoseq"].ToString());
xeBody.setAttribute("isotype", datarow["isotype"].ToString());
xeBody.setAttribute("iordertype", datarow["iordertype"].ToString());
xeBody.setAttribute("iorderdid", datarow["iorderdid"].ToString());
xeBody.setAttribute("iexpiratdatecalcu", datarow["iexpiratdatecalcu"].ToString());
xeBody.setAttribute("irowno", irowno);
xeBody.setAttribute("cinvouchtype", datarow["cinvouchtype"].ToString());
}
irowno++;
}
}
break;
#endregion
default:
throw new Exception("无效的type类型:" + iType);
break;
}
//传入表头的MSXML2.DOMDocumentClass对象
broker.AssignNormalValue("DomHead", docHead);
//传入表体的MSXML2.DOMDocumentClass对象
broker.AssignNormalValue("domBody", docBody);
//给普通参数domPosition赋值。此参数的数据类型为System.Object,此参数按引用传递,表示货位:传空
broker.AssignNormalValue("domPosition", null);
//该参数errMsg为OUT型参数,由于其数据类型为System.String,为一般值类型,因此不必传入一个参数变量。在API调用返回时,可以通过GetResult("errMsg")获取其值
//给普通参数cnnFrom赋值。此参数的数据类型为ADODB.Connection,此参数按引用传递,表示连接对象,如果由调用方控制事务,则需要设置此连接对象,否则传空
broker.AssignNormalValue("cnnFrom", null);
//该参数VouchId为INOUT型普通参数。此参数的数据类型为System.String,此参数按值传递。在API调用返回时,可以通过GetResult("VouchId")获取其值
string reVouchId = "";
broker.AssignNormalValue("VouchId", reVouchId);
//该参数domMsg为OUT型参数,由于其数据类型为MSXML2.IXMLDOMDocument2,非一般值类型,因此必须传入一个参数变量。在API调用返回时,可以直接使用该参数
MSXML2.IXMLDOMDocument2 domMsg = new DOMDocument();
broker.AssignNormalValue("domMsg", domMsg);
//给普通参数bCheck赋值。此参数的数据类型为System.Boolean,此参数按值传递,表示是否控制可用量。
broker.AssignNormalValue("bCheck", false);
//给普通参数bBeforCheckStock赋值。此参数的数据类型为System.Boolean,此参数按值传递,表示检查可用量
broker.AssignNormalValue("bBeforCheckStock", true);
//给普通参数bIsRedVouch赋值。此参数的数据类型为System.Boolean,此参数按值传递,表示是否红字单据
broker.AssignNormalValue("bIsRedVouch", false);
//给普通参数sAddedState赋值。此参数的数据类型为System.String,此参数按值传递,表示传空字符串
broker.AssignNormalValue("sAddedState", "");
//给普通参数bReMote赋值。此参数的数据类型为System.Boolean,此参数按值传递,表示是否远程:转入false
broker.AssignNormalValue("bReMote", false);
//第六步:调用API
if (!broker.Invoke())
{
//错误处理
Exception apiEx = broker.GetException();
if (apiEx != null)
{
if (apiEx is MomSysException)
{
MomSysException sysEx = apiEx as MomSysException;
//Console.WriteLine("系统异常:" + sysEx.Message);
dr["cError"] = "系统异常:" + sysEx.Message;
}
else if (apiEx is MomBizException)
{
MomBizException bizEx = apiEx as MomBizException;
//Console.WriteLine("API异常:" + bizEx.Message);
dr["cError"] = "API异常:" + bizEx.Message;
//todo:异常处理
}
//异常原因
String exReason = broker.GetExceptionString();
if (exReason.Length != 0)
{
dr["cError"] = "异常原因:" + exReason;
//Console.WriteLine("异常原因:" + exReason);
}
}
//结束本次调用,释放API资源
broker.Release();
return dtRetrun;
//return "-1";
}
//第七步:获取返回结果
//获取返回值
//获取普通返回值。此返回值数据类型为System.Boolean,此参数按值传递,表示返回值:true:成功,false:失败
System.Boolean result = Convert.ToBoolean(broker.GetReturnValue());
//获取out/inout参数值
//获取普通OUT参数errMsg。此返回值数据类型为System.String,在使用该参数之前,请判断是否为空
System.String errMsgRet = broker.GetResult("errMsg") as System.String;
//获取普通INOUT参数VouchId。此返回值数据类型为System.String,在使用该参数之前,请判断是否为空
System.String VouchIdRet = broker.GetResult("VouchId") as System.String;
//System.String cCodeRet = broker.GetResult("cCode") as System.String;
//获取普通OUT参数domMsg。此返回值数据类型为MSXML2.IXMLDOMDocument2,在使用该参数之前,请判断是否为空
MSXML2.IXMLDOMDocument2 domMsgRet = (MSXML2.IXMLDOMDocument2)broker.GetResult("domMsg");
//结束本次调用,释放API资源
broker.Release();
if (!result)
{
if (errMsgRet != null)
{
dr["cError"] = errMsgRet;
}
else
{
dr["cError"] = "库存可用量不足。";
}
}