Tags

, , , ,

SAPRFC adalah extension module PHP. Dengan extension ini, memungkinkan PHP untuk memanggil fungsi SAP R/3. Asumsinya anda sudah punya extension ini di sistem anda. Tutorial ini hanya agar SAPRFC lebih mudah digunakan bersama Yii :D. Jika anda belum punya SAPRFC, silakan baca cara installnya di sini.

Kalau sudah, berikut caranya

Buat file MSAPConnection.php di folder protected/components. Isinya

<?php

Yii::$classMap['SAPConnection'] = Yii::getPathOfAlias('ext.sapclasses') . '/sap.php';

/**
 * Description of MSAPConnection
 *
 * @property SAPConnection $SAPConn
 * 
 * @method SAPFunction NewFunction(string $name)
 * @author Misbahul D Munir
 */
class MSAPConnection extends CApplicationComponent
{
    /**
     *
     * @var SAPConnection 
     */
    private $_sap;

    /**
     *
     * @var string|array 
     */
    public $config;

    public function getSAPConn()
    {
        if (!$this->_sap) {
            $this->_sap = new SAPConnection();
            $this->_sap->Connect($this->config);
            if ($this->_sap->GetStatus() == SAPRFC_OK) {
                $this->_sap->Open();
            }
        }
        return $this->_sap;
    }

    public function __call($name, $parameters)
    {
        $conn = $this->getSAPConn();
        if (method_exists($conn, $name)) {
            return call_user_func_array(array($conn, $name), $arguments);
        } else {
            return parent::__call($name, $parameters);
        }
    }

    public function __destruct()
    {
        if ($this->_sap){
            $this->_sap->Close();
        }
    }

    public function Close($force = false)
    {
        // don't do anything
        if ($force) {
            if ($this->_sap){
                $this->_sap->Close();
            }
            unset($this->_sap);
        }
    }
}

Kode

Yii::$classMap['SAPConnection'] = Yii::getPathOfAlias('ext.sapclasses') . '/sap.php';

silakan diubah sesuai dengan letak file sap.php.

Cara memakainya di aplikasi

// configurasi
// file protected/config/main.php

    'components' => array(
        ...

        'sap' => array(
            'class' => 'MSAPConnection',
            'config' => '/path/to/logon_data.conf'
        ),
    )
    ...

Property config adalah file berupa configurasi koneksi ke server sap.

// contoh pemakaian di aplikasi
// memanggil rfc Z_ZHR_MASTER1

$dsap = Yii::app()->sap;
$fce = $dsap->NewFunction("Z_ZCHR_MASTER1");
if ($fce == false) {
    $dsap->PrintStatus();
    exit;
}

//Param Export
$fce->I_BUKRS = '3000';
$fce->I_CNAME = strtoupper(str_replace("*", "%", $nm_karyawan));

//Execute Function
$fce->Call();
$this->data = array();
if ($fce->GetStatus() == SAPRFC_OK) {
    $fce->T_MASTER->Reset();
    //Display Tables
    $i = 0;
    while ($fce->T_MASTER->Next()) {
        $this->_data[] = array('label' => $fce->T_MASTER->row['CNAME'],
            'value' => $fce->T_MASTER->row['CNAME'],
            'nip' => $fce->T_MASTER->row['PERNR'],
            'jabatan' => $fce->T_MASTER->row['PERSK_TEXT'],
            'unit_kerja' => $fce->T_MASTER->row['KOSTL_TEXT'],);
        $i++;
        if ($i > 10) {
            break;
        }
    }
} else {
    $fce->PrintStatus();
}
$fce->Close();

Gampang bukan :D. Tadi adalah tutorial di Yii 1. Lalu bagaimana dengan Yii 2. Insya Allah di tulisan berikutnya gan.

Advertisements