Skip to Content

Web based booking in of items using PHP & BAPI’s

A company I have recently been doing work for approached me with a interesting request. They wanted a method for users to be able to book in service orders themselves, online. Vendors were not being paid on time due to the fact acknowledgment of receipt of the orders was not given in a timely fashion.

Not everyone had access to SAP for cost reasons. Also quite a few people for some reason do not like the SAP interface. The weekly spreadsheet generated based on a SAP report was also proving ineffective due to users overwriting acknowledged orders.

With this in mind, myself and the local ABAP programmer came up with the following solution.

The main ingredients of the solution involve the following:

  1. Windows Active Directory
  2. IIS, PHP

A Windows 2000 Intranet server was setup with PHP and the SAPRFC extension. Using Windows authentication we now select the correct recipient for the outstanding service orders report. The local ABAP programmer wrote a function to allow me to pass the username as a import parameter and receive the lines of outstanding orders in JTAB which I can pull off into an array using saprfc_table_read.

Here is an overview of how this works. I don’t have the script in front of me. So I’m doing this from memory. I will add some more code specific detail at a later date.

  1. PHP script calls local bespoke RFC function and sends RECIPENT (username) as an import parameter. RFC function fills JTAB with outstanding orders. Items in JTAB table are LIFNR,NAME1,EBELN,EBELP,TXZ01,EINDT,OPENQTY,MEINS,NETPR,WAERS,WEMPF. Each line is then read in a loop using the number of rows returned by saprfc_table_rows.
  2. The user is then presented with a form which allows them to acknowledge orders to the maximum value of OPENQTY for each line item.
  3. On submission the lines are sorted by vendor. We have to run the BAPI’s multiple times for each vendor. You can’t book in line items from different vendors in one go. Once this is done we can start calling our BAPI’s to book the items in.

We setup a loop by vendor which does the following,  Call BAPI_GOODSMVT_CREATE and set the import parameters GOODSMVT_HEADER & GOODSMVT_CODE.

/* Please note this is not runnable code. It's code just to get a feel of whats going on. You will need to fill in the details */

$goodsmvt_header = array('PSTING_DATE' => '20050512',
'DOC_DATE'=> '20050512',
'REF_DOC_NO'=> 'Jsimmons');
$goodsmvt_code = array('GM_CODE' => '01');

/* $fhandle is from a connection we established earlier using saprfc_open & saprfc_function_discover */

we now need to specify what items we wish to book in We have an array of line items associated with the current vendor in the outer loop which looks like this:

$lineitem = array('PO_NUMBER' => 'ponumber_from_user_form',
'PO_ITEM'=> 'poitem_from_user_form',
'MOVE_TYPE'=> '101',
'MVT_IND'=> 'B',
' ENTRY_QNT'=> ,'number_to_book_in_from_user_form');


foreach ($alllineitems as $lineitem) {
// check for errors using saprfc_error

/* Now we need to call our first BAPI to post the items */

$errorcode = saprfc_call_and_receive ($fhandle);
// Insert Error handling code in here

/* If the RESULTS table has entries , We have a problem */

if (saprfc_table_rows($fhandle,'RESULTS'){
$fhandle = saprfc_function_discover($rfcconnection,'BAPI_TRANSACTION_ROLLBACK');
$errorcode = saprfc_call_and_receive ($fhandle);
// Insert Error handling code in here
}else {
$fhandle = saprfc_function_discover($rfcconnection,'BAPI_TRANSACTION_COMMIT');
$errorcode = saprfc_call_and_receive ($fhandle);
// Insert Error handling code in here


// Start loop again for next vendor

The overall result is that we now have a intranet application which allows users to easlily book in services orders. I even created a script which nags users via email to book in services orders. The nag email contains a link to the intranet page which contains the application.

This solution has made high impact due to the fact that the SAP environment is not web enabled. Therefore using PHP integration with SAP provides a short cut to providing web based applications.


  1. Extremely cheap solution using every day tools.
  2. Browser based interface which everyone knows how to use.
  3. Vendors are paid on time which allows the company to trade on more favorable terms.
  4. Reduced administrator workload chasing people and making spreadsheets to capture outstanding service orders.

I would recommend going through some of the documentation for the SAPRFC extension. This will give you an idea of what I’m trying to convey here, and fill in the gaps.


Have fun !

Usefull Links

You must be Logged on to comment or reply to a post.