C # Call the sap interface and return data to sap

Source: Internet
Author: User

Public class SapClass
{
/// <Summary>
///
/// </Summary>
/// <Param name = "fphm"> invoice number </param>
/// <Param name = "fprq"> Invoice date </param>
/// <Param name = "fkf"> payer </param>
/// <Param name = "fxqd"> distribution channel </param>
Public DataTable nco (string fphm, DateTime fprq, string fkf, char fxqd)
{

// MyBackendConfig config = new MyBackendConfig ();
IDestinationConfiguration ID = new MyBackendConfig ();

RfcDestinationManager. RegisterDestinationConfiguration (ID );

RfcDestination prd = RfcDestinationManager. GetDestination (PlatRunning. Global. receivettings. GetValue ("SapName"); // SAP system ID

DataTable dt = nco (prd, fphm, fprq, fkf, fxqd );
RfcDestinationManager. UnregisterDestinationConfiguration (ID );
Return dt;

}

/// <Summary>
/// SAPTOJS
/// </Summary>
/// <Param name = "prd"> </param>
/// <Param name = "fphm"> </param>
/// <Param name = "fprq"> </param>
/// <Param name = "fkf"> </param>
/// <Param name = "fxqd"> </param>
Private DataTable nco (RfcDestination prd, string fphm, DateTime fprq, string fkf, char fxqd)
{
RfcRepository repo = null;
Try
{
Repo = prd. Repository;
}
Catch (Exception ex)
{
Throw (ex );
}

IRfcFunction companyBapi = repo. CreateFunction ("ZSD_SY_FUC_SAPTOJS"); // call the function name
// Set the Import Parameters
CompanyBapi. SetValue ("VBELN", fphm );
CompanyBapi. SetValue ("ERDAT", fprq );
CompanyBapi. SetValue ("KUNRG", fkf );
CompanyBapi. SetValue ("ZBJ", fxqd );

CompanyBapi. Invoke (prd); // executes the Function

IRfcTable table = companyBapi. GetTable ("IT_JS_EXPORT"); // obtain the corresponding inner table IT_JS_EXPORT
// Instantiate an empty table structure for processing in advance
DataTable dt = CreateSapToJSTable ();
For (int I = 0; I <table. Count; I ++)
{
Table. CurrentIndex = I;
DataRow dr = dt. NewRow ();
Dr ["fxqd"] = table [I]. GetString ("VTWEG ");
Dr ["fphm"] = table [I]. GetString ("VBELN ");
Dr ["fphh"] = table [I]. GetString ("POSNR ");
Dr ["fpcjsj"] = table [I]. GetString ("ERDAT ");
Dr ["khdm"] = table [I]. GetString ("KUNRG ");
Dr ["khmc"] = table [I]. GetString ("NAME ");
Dr ["khdzch"] = table [I]. GetString ("STRAS ");
Dr ["sapddh"] = table [I]. GetString ("AUBEL ");
Dr ["khddh"] = table [I]. GetString ("BSTKD ");
Dr ["sapwlh"] = table [I]. GetString ("MATNR ");
Dr ["khwlh"] = table [I]. GetString ("KDMAT ");
Dr ["spmc"] = table [I]. GetString ("ARKTX ");
Dr ["kpm"] = table [I]. GetString ("TEXT ");
Dr ["jldw"] = table [I]. GetString ("VRKME ");
Dr ["sl"] = table [I]. GetString ("FKIMG ");
Dr ["bb"] = table [I]. GetString ("WAERK ");
Dr ["dj"] = table [I]. GetString ("KBETR ");
Dr ["jgdw"] = table [I]. GetString ("KPEIN ");
Dr ["zj"] = table [I]. GetString ("NETWR ");
Dr ["shl"] = table [I]. GetString ("MWSK1 ");
Dr ["se"] = table [I]. GetString ("mwsystolic ");
Dr ["myxz"] = table [I]. GetString ("KTGRM ");
Dt. Rows. Add (dr );
}
Return dt;
}

/// <Summary>
///
/// </Summary>
/// <Param name = "sapfph"> sap Invoice No. </param>
/// <Param name = "qtfph"> Generate the invoice number </param>
Public DataTable nco (string sapfph, string qtfph)
{
// MyBackendConfig config = new MyBackendConfig ();
IDestinationConfiguration ID = new MyBackendConfig ();

RfcDestinationManager. RegisterDestinationConfiguration (ID );

RfcDestination prd = RfcDestinationManager. GetDestination (PlatRunning. Global. receivettings. GetValue ("SapName"); // SAP system ID
DataTable dt = nco (prd, sapfph, qtfph );
RfcDestinationManager. UnregisterDestinationConfiguration (ID );
Return dt;
}

/// <Summary>
/// JSTOSAP
/// </Summary>
/// <Param name = "prd"> </param>
/// <Param name = "sapfph"> </param>
/// <Param name = "qtfph"> </param>
Private DataTable nco (RfcDestination prd, string sapfph, string qtfph)
{
DataTable dt = WriteTableToSAP (prd, sapfph, qtfph );
Return dt;
}
# Region write back data to SAP
Private DataTable WriteTableToSAP (RfcDestination rfcDest, string sapfph, string qtfph)
{
RfcRepository repo = rfcDest. Repository;
IRfcFunction funBapi = repo. CreateFunction ("ZSD_SY_FUC_JSTOSAP ");
// Call the function name
IRfcTable tblROF = funBapi. GetTable ("IT_JSTOSAP ");
TblROF. Clear ();
TblROF. Insert ();
TblROF. CurrentRow. SetValue ("VBELN", sapfph );
TblROF. CurrentRow. SetValue ("JSVBELN", qtfph );
FunBapi. SetValue ("IT_JSTOSAP", tblROF );
FunBapi. Invoke (rfcDest );
// Reference the return result
IRfcTable ReturnTable = funBapi. GetTable ("IT_RETURN ");
DataTable dtReturn = ReturnMessage ();
For (int I = 0; I <ReturnTable. Count; I ++)
{
ReturnTable. CurrentIndex = I;
DataRow dr = dtReturn. NewRow ();
Dr ["isSuccess"] = ReturnTable [I]. GetString ("TYPE ");
Dr ["ReturnMessage"] = ReturnTable [I]. GetString ("MESSAGE ");
DtReturn. Rows. Add (dr );
}
Return dtReturn;
}
# Endregion
# Region Interface Parameters
Public class MyBackendConfig: IDestinationConfiguration
{
Public RfcConfigParameters GetParameters (String destinationName)
{
If ("DEV". Equals (destinationName ))
{
String strtemp = string. Empty;
RfcConfigParameters parms = new RfcConfigParameters ();

Parms. Add (RfcConfigParameters. AppServerHost, "192.168.0.122"); // SAP Host IP Address
Parms. Add (RfcConfigParameters. SystemNumber, "00"); // SAP instance
Parms. Add (RfcConfigParameters. User, "SYRFC"); // User Name
Parms. Add (RfcConfigParameters. Password, "123456"); // Password
Parms. Add (RfcConfigParameters. Client, "710"); // Client
Parms. Add (RfcConfigParameters. Language, "ZH"); // login Language

Parms. Add (RfcConfigParameters. PoolSize, "5 ");

Parms. Add (RfcConfigParameters. MaxPoolSize, "10 ");

Parms. Add (RfcConfigParameters. IdleTimeout, "60 ");

Return parms;

}

Else
Return null;

}

Public bool ChangeEventsSupported ()
{

Return false;

}

Public event RfcDestinationManager. ConfigurationChangeHandler ConfigurationChanged;

}
# Endregion

Public DataTable CreateSapToJSTable ()
{
DataTable dt = new DataTable ();
Dt. Columns. Add ("fxqd"); // Distribution Channel
Dt. Columns. Add ("fphm"); // invoice number
Dt. Columns. Add ("fphh"); // invoice line number
Dt. Columns. Add ("fpcjsj"); // invoice creation time
Dt. Columns. Add ("khdm"); // Customer Code
Dt. Columns. Add ("khmc"); // CUSTOMER NAME
Dt. Columns. Add ("khdzch"); // customer address (Chinese)
Dt. Columns. Add ("sapddh"); // sap Order No.
Dt. Columns. Add ("khddh"); // customer order number
Dt. Columns. Add ("sapwlh"); // sap item No.
Dt. Columns. Add ("khwlh"); // customer ID
Dt. Columns. Add ("spmc"); // Product Name
Dt. Columns. Add ("kpm"); // invoice name
Dt. Columns. Add ("jldw"); // measurement unit
Dt. Columns. Add ("sl"); // quantity
Dt. Columns. Add ("bb"); // currency
Dt. Columns. Add ("dj"); // unit price including tax
Dt. Columns. Add ("jgdw"); // price unit
Dt. Columns. Add ("zj"); // total price
Dt. Columns. Add ("shl"); // Tax Rate
Dt. Columns. Add ("se"); // Tax Amount
Dt. Columns. Add ("myxz"); // trade nature
Return dt;
}
Public DataTable ReturnMessage ()
{
DataTable dt = new DataTable ();
Dt. Columns. Add ("isSuccess"); // successful or not
Dt. Columns. Add ("ReturnMessage"); // Message
Return dt;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.