View previous topic :: View next topic
Author
Message
admin Администратор Joined: 01 Sep 2007 Posts: 1640
Posted: Sat Sep 22, 2007 4:49 pm Post subject: SAP PHP Example
ABAP
Code:
* Author: RobKing, Stafford, UK
REPORT ZRKBAPITEST .
DATA: doc_header LIKE bapiache08,
ACCOUNTPAYABLE LIKE BAPIACAP03 OCCURS 0 WITH HEADER LINE,
ACCOUNTGL LIKE BAPIACGL03 OCCURS 0 WITH HEADER LINE,
ACCOUNTTAX LIKE BAPIACTX01 OCCURS 0 WITH HEADER LINE,
CURRENCYAMOUNT LIKE BAPIACCR01 OCCURS 0 WITH HEADER LINE,
return LIKE bapiret2 OCCURS 0 WITH HEADER LINE,
obj_type LIKE bapiache08-obj_type,
obj_key LIKE bapiache02-obj_key,
obj_sys LIKE bapiache02-obj_sys,
docnum LIKE bkpf-belnr.
* Get last SA type document posted to build document key.
SELECT MAX( belnr )
INTO docnum
FROM bkpf
WHERE BUKRS = '001'
AND bstat = ' '
AND BLART = 'KR'
AND gjahr = sy-datlo(4).
* Create new document number to be used.
docnum = docnum + 1.
* Begin to populate docnument header.
CONCATENATE '0' DOCNUM '001 ' SY-DATLO(4) INTO DOC_HEADER-OBJ_KEY.
DOC_HEADER-OBJ_TYPE = 'BKPFI'.
CONCATENATE SY-SYSID SY-MANDT INTO DOC_HEADER-OBJ_SYS.
*doc_header-obj_sys = '1900000363'
doc_header-username = sy-uname.
DOC_HEADER-HEADER_TXT = 'SE38 ZRKBAPITEST2'.
DOC_HEADER-COMP_CODE = '001'.
DOC_HEADER-DOC_DATE = '20040924'.
doc_header-pstng_date = sy-datlo.
DOC_HEADER-DOC_TYPE = 'KR'.
CONCATENATE '0' docnum INTO docnum.
doc_header-fisc_year = '2004'.
doc_header-ac_doc_no = docnum.
* fill account payable
ACCOUNTPAYABLE-ITEMNO_ACC = '1'.
ACCOUNTPAYABLE-VENDOR_NO = '0000140088'.
ACCOUNTPAYABLE-GL_ACCOUNT = '0000901102'.
*accountpayable-w_tax_code = 'V0'.
APPEND ACCOUNTPAYABLE.
* fill account gl
ACCOUNTGL-ITEMNO_ACC = '2'.
ACCOUNTGL-GL_ACCOUNT = '0000188601'.
ACCOUNTGL-TAX_CODE = 'V0'.
ACCOUNTGL-FISC_YEAR = '2004'.
ACCOUNTGL-FIS_PERIOD = '10'.
APPEND ACCOUNTGL.
* fill currency amount
CURRENCYAMOUNT-ITEMNO_ACC = '1'.
CURRENCYAMOUNT-CURRENCY = 'GBP'.
CURRENCYAMOUNT-AMT_DOCCUR = '0.0200'.
CURRENCYAMOUNT-CURR_TYPE = '00'.
APPEND CURRENCYAMOUNT.
* fill currency amount
CURRENCYAMOUNT-ITEMNO_ACC = '2'.
CURRENCYAMOUNT-CURRENCY = 'GBP'.
CURRENCYAMOUNT-AMT_DOCCUR = '-0.0200'.
CURRENCYAMOUNT-CURR_TYPE = '00'.
APPEND CURRENCYAMOUNT.
* fill currency amount
*curencyamount-itemno_acc = '3'.
*curencyamount-currency = 'GBP'.
*curencyamount-amt_doccur = '0.0200'.
*curencyamount-curr_type = '00'.
*append curencyamount.
* All tables filled - now call BAPI.
CALL FUNCTION 'BAPI_ACC_INVOICE_RECEIPT_POST'
EXPORTING
DOCUMENTHEADER = DOC_HEADER
* IMPORTING
* OBJ_TYPE =
* OBJ_KEY =
* OBJ_SYS =
TABLES
ACCOUNTPAYABLE = ACCOUNTPAYABLE
ACCOUNTGL = ACCOUNTGL
ACCOUNTTAX = ACCOUNTTAX
CURRENCYAMOUNT = CURRENCYAMOUNT
* PURCHASEORDER =
* PURCHASEAMOUNT =
RETURN = RETURN
* CRITERIA =
* VALUEFIELD =
* EXTENSION1 =
.
LOOP AT RETURN WHERE TYPE = 'E'.
WRITE: / RETURN-MESSAGE.
* exit.
ENDLOOP.
IF sy-subrc EQ 0.
WRITE: / 'BAPI call failed - debug and fix!'.
ELSE.
CLEAR return.
REFRESH return.
* COMMIT WORK AND WAIT.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'
IMPORTING
return = return.
WRITE: / 'BAPI call worked!!'.
WRITE: / doc_header-obj_key, ' posted'.
ENDIF.
PHP code:
Code: <html>
<body>
<?
include_once("sap.php");
$sap = new SAPConnection();
$sap->Connect("logon_data.conf");
if ($sap->GetStatus() == SAPRFC_OK ) $sap->Open ();
if ($sap->GetStatus() != SAPRFC_OK ) {
$sap->PrintStatus();
exit;
}
$sap_attr = $sap->GetAttributes();
$doc_type = "KR";
$number_range = getNumberRange($sap,$doc_type);
$object = "RF_BELEG";
$comp_code = "001 ";
$nrlevel = getNextNumber($sap,$object,$comp_code,$number_range);
$doc_no = substr($nrlevel,-10);
$doc_no = $doc_no + 1;
$fce = &$sap->NewFunction ("BAPI_ACC_INVOICE_RECEIPT_POST");
if ($fce == false ) {
$result = PrintError("function BAPI_ACC_INVOICE_RECEIPT_POST error.");
$sap->PrintStatus();
exit;
}
// Document header
$obj_type = "BKPFI";
$obj_key = $doc_no.$comp_code.date("Y");
$obj_sys = $sap_attr["sysid"].$sap_attr["client"];
$header_txt = "TEST BOC BAPI POSTING";
$username = $sap_attr["user"];
$result = update_DOCUMENTHEADER($obj_type,$obj_key,$obj_sys,$comp_code,$header_txt,$username,$doc_type);
// Account payable
$itemno_acc = "1";
$vendor_no = "0000940088";
$gl_account = "0000401102";
// $tax_code = "V0";
$result = add_ACCOUNTPAYABLE($itemno_acc,$vendor_no,$gl_account);
// Account GL
$itemno_acc = "2";
$gl_account = "0000488601";
$tax_code = "V1";
$costcenter = "";
// $costcenter = "0000000617";
$orderid = "";
// $orderid = "61107/03/1";
$result = add_ACCOUNTGL($itemno_acc,$gl_account,$tax_code,$costcenter,$orderid);
// Account Tax
$itemno_acc = "3";
$gl_account = "0000445501";
$tax_code = "V1";
$acct_key = "VST";
//
$result = add_ACCOUNTTAX($itemno_acc,$gl_account,$tax_code,$acct_key);
// Currency Amount (account payable)
$itemno_acc = "1";
$currency = "GBP";
$amt_doccur = "-118.5000";
$amt_base = "0.0000";
$curr_type = "00";
$result = add_CURRENCYAMOUNT($itemno_acc,$currency,$amt_doccur,$amt_base,$curr_type);
// Currency Amount (account GL)
$itemno_acc = "2";
$currency = "GBP";
$amt_doccur = "100.0000";
$amt_base = "0.0000";
$curr_type = "00";
$result = add_CURRENCYAMOUNT($itemno_acc,$currency,$amt_doccur,$amt_base,$curr_type);
// Currency Amount (account tax)
$itemno_acc = "3";
$currency = "GBP";
$amt_base = "100.0000";
$amt_doccur = "18.5000";
$curr_type = "00";
//
$result = add_CURRENCYAMOUNT($itemno_acc,$currency,$amt_doccur,$amt_base,$curr_type);
$fce->Call();
// $fce->Debug();
if ($fce->GetStatus() == SAPRFC_OK) {
echo "<table><tr><td>Number</td><td>Message</td></tr>";
$fce->RETURN->Reset();
while ( $fce->RETURN->Next() ) {
echo "<tr><td>".$fce->RETURN->row["NUMBER"]."</td><td>".$fce->RETURN->row["MESSAGE"]."</td></tr>";
echo "</table>";
}
} else {
$fce->PrintStatus();
}
commit_work();
$sap->Close();
function PrintError($error_msg) {
$statustext="";
$statustext.="<font size=4 color=green><pre>";
$statustext.=$error_msg;
$statustext.="</font></pre><br>";
echo $statustext;
}
function update_DOCUMENTHEADER($obj_type,$obj_key,$obj_sys,$comp_code,$header_txt,$username,$doc_type) {
global $fce;
// DOCUMENTHEADER structure (Header)
// -------------------------------------------------------------------------
$fce->DOCUMENTHEADER["OBJ_TYPE"] = $obj_type;
$fce->DOCUMENTHEADER["OBJ_KEY"] = $obj_key;
$fce->DOCUMENTHEADER["OBJ_SYS"] = $obj_sys;
// -------------------------------------------------------------------------
$fce->DOCUMENTHEADER["USERNAME"] = $username;
$fce->DOCUMENTHEADER["HEADER_TXT"] = $header_txt;
$fce->DOCUMENTHEADER["COMP_CODE"]= $comp_code;
$fce->DOCUMENTHEADER["DOC_DATE"] = date("Ymd");
$fce->DOCUMENTHEADER["PSTNG_DATE"] = date("Ymd");
$fce->DOCUMENTHEADER["TRANS_DATE"] = date("Ymd");
$fce->DOCUMENTHEADER["FISC_YEAR"] = date("Y");
// $fce->DOCUMENTHEADER["FIS_PERIOD"] = date("m");
$fce->DOCUMENTHEADER["DOC_TYPE"] = $doc_type;
// $fce->DOCUMENTHEADER["AC_DOC_NO"] = $ac_doc_no;
return 1;
// OBJ_TYPE CHARACTER 5
// OBJ_KEY CHARACTER 20
// OBJ_SYS CHARACTER 10
// USERNAME CHARACTER 12
// HEADER_TXT CHARACTER 25
// OBJ_KEY_R CHARACTER 20
// COMP_CODE CHARACTER 4
// AC_DOC_NO CHARACTER 10
// FISC_YEAR NUMC 4
// DOC_DATE DATE
// PSTNG_DATE DATE
// TRANS_DATE DATE
// FIS_PERIOD NUMC 2
// DOC_TYPE CHARACTER 2
// REF_DOC_NO CHARACTER 16
// COMPO_ACC CHARACTER 4
}
// big chunck of code missing from here ;o)
function getNextNumber($sap,$object,$comp_code,$number_range)
{
$fce = $sap->NewFunction ("RFC_READ_TABLE");
if ($fce == false ) {
$sap->PrintStatus();
exit;
}
$fce->QUERY_TABLE = "NRIV";
$row_OPTIONS["TEXT"] = "OBJECT = '".$object."'";
$row_result = $fce->OPTIONS->Append($row_OPTIONS);
$row_OPTIONS["TEXT"] = "AND SUBOBJECT = '".$comp_code."'";
$row_result = $fce->OPTIONS->Append($row_OPTIONS);
$row_OPTIONS["TEXT"] = "AND NRRANGENR = '".$number_range."'";
$row_result = $fce->OPTIONS->Append($row_OPTIONS);
$row_FIELDS["FIELDNAME"] = "NRLEVEL";
$row_result = $fce->FIELDS->Append($row_FIELDS);
$fce->Call();
if ($fce->GetStatus() == SAPRFC_OK) {
$fce->DATA->Reset();
while ( $fce->DATA->Next() ) {
return $fce->DATA->row["WA"];
}
}
return "";
}
?>
</body>
</html>
Back to top
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.