How to apply BAPI_GOODSMVT_CREATE by PHP

If we want to create material document in SAP R/3 from external system, we can apply BAPI function BAPI_GOODSMVT_CREATE for this action. After we already call this function, we shoud call function BAPI_TRANSACTION_COMMIT or BAPI_TRANSACTION_ROLLBACK to commit or rollback our processes. In the example , i will create issue material doc by apply this function using PHP.

Example

// This function receive refdoc and array of goods movement details 
// and return data of material doc, material year and return message
function callissuesap($refdoc, $gmm_detail){
 
    $return = array();
 
    // Create saprfc-instance
    $sap = create_connection();                      
 
    // Import Parameters
    $today = date("Ymd");
 
    $header = array();
    $header["PSTNG_DATE"] = $today;
    $header["DOC_DATE"]   = $today;
    $header["REF_DOC_NO"] = $refdoc;
    $header["HEADER_TXT"] = "";
 
    $code = array();
    $code["GM_CODE"] = "03";
 
    $item = array();
    $lineitem = array();
    foreach ($gmm_detail as $gmm_line) {
        $lineitem["ORDERID"]      = $gmm_line["orderId"];
        $lineitem["MATERIAL"]     = $gmm_line["material"];
        $lineitem["PLANT"]        = $gmm_line["plant"];
        $lineitem["STGE_LOC"]     = $gmm_line["loc"];
        $lineitem["BATCH"]        = $gmm_line["batch"];
        $lineitem["MOVE_TYPE"]    = "261";
        $lineitem["ENTRY_QNT"]    = $gmm_line["qty"];
        $lineitem["ENTRY_UOM"]    = $gmm_line["une"];
        $item[] = $lineitem;
    }
 
    // Call-Function
    $result=$sap->callFunction("BAPI_GOODSMVT_CREATE",
                                array(
                                    array("IMPORT","GOODSMVT_HEADER",$header),
                                    array("IMPORT","GOODSMVT_CODE",$code),
                                    array("EXPORT","MATERIALDOCUMENT",""),
                                    array("EXPORT","MATDOCUMENTYEAR",""),
                                    array("TABLE","GOODSMVT_ITEM",$item),
                                    array("TABLE","RETURN",array())
                                ));
 
   // Call successfull?
   if ($sap->getStatus() == SAPRFC_OK) {
        $sap->callFunction("BAPI_TRANSACTION_COMMIT",array());
        $return["MAT_DOC"] = $result["MATERIALDOCUMENT"];
        $return["MAT_DOC_YEAR"] = $result["MATDOCUMENTYEAR"];
        $return["RETURN"] = $result["RETURN"];
    }
    else {
        $mess = array();
        $messline = array();
        $messline["TYPE"] = "E";
        $messline["MESSAGE"] = $sap->getStatusTextLong();
        $mess[] = $messline;
 
//        $sap->printStatus();
        $sap->callFunction("BAPI_TRANSACTION_ROLLBACK",array());
        $return["MAT_DOC"] = "";
        $return["MAT_DOC_YEAR"] = "";
        $return["RETURN"] = $mess;
    }                     
    $sap->logoff();
 
    return $return;
}
 
function create_connection() {
    return $sap = new saprfc(array(
    "logindata"=>array(  
    "ASHOST"=>"xxx.xxx.xxx.xxx"        // application server 
    ,"SYSNR"=>"xx"         // system number
    ,"CLIENT"=>"xxx"           // client     
    ,"USER"=>"xxxxxxxxx"       // user
    ,"PASSWD"=>"xxxxxxxxx"     // password
    )
    ,"show_errors"=>false     // let class printout errors
    ,"debug"=>false)) ;     // detailed debugging information     
}
 

Information about GM_CODE

GM_Code 01: Goods receipt for purchase order
GM_Code 02: Goods receipt for production order
GM_Code 03: Goods issue
GM_Code 04: Transfer posting
GM_Code 05: Other goods receipts
GM_Code 06: Reversal of goods movements

You can read the further informations in tcode -> Materials Management -> Inventory Management -> GoodsMovement -> CreateFromData -> Tab Documentation.

ขอบคุณครั

ขอบคุณครับ

Post new comment