Integrating Netsuite PHP Toolkit in Yii


  • Download PHP Toolkit from here.
  • Copy the PHP Toolkit from the zip archive into protected/vendors folder
  • Make the Config variables global in NSconfig.php . i.e. add following code in beginning of the file
    global $nsendpoint;
    global $nshost;
    global $nsemail;
    global $nspassword;
    global $nsrole;
    global $nsaccount;
    
  • Create a new file in protected/models and name it as NSEntity.php. Below are its contents
    class NSEntity {
    
    	private $service;
    
    	public function __construct() {
    
    		Yii::$enableIncludePath = false;
    		Yii::import('application.vendors.PHPToolkit.NetSuiteService', true);
    
    		$this->service = new NetSuiteService();
    		if (!Yii::app()->session['nshost']) {
    			global $nshost,$nsaccount;
    
    			$params = new GetDataCenterUrlsRequest();
    			$params->account = $nsaccount;
    			$response = $this->service->getDataCenterUrls($params);
    			Yii::app()->session['nshost'] = $response->getDataCenterUrlsResult->dataCenterUrls->webservicesDomain;
    
    			$nshost = Yii::app()->session['nshost'];
    
    		}
    	}
    
    	function search($searchRequest, $searchId, $pageNum) {
    
    		$this->service->setSearchPreferences(false, 10);
    
    		if ($searchId && $pageNum) {
    			$req = new SearchMoreWithIdRequest();
    			$req->searchId = $searchId;
    			$req->pageIndex = $pageNum;
    			$searchResponse = $this->service->searchMoreWithId($req);
    		} else {
    			$searchResponse = $this->service->search($searchRequest);
    		}
    
    		$response = array();
    		if (!$searchResponse->searchResult->status->isSuccess) {
    			$response['status'] = FALSE;
    			$response['error'] = '';
    		} else {
    			$response['status'] = TRUE;
    			$response['recordCount'] = $searchResponse->searchResult->totalRecords;
    			$response['pageCount'] = $searchResponse->searchResult->totalPages;
    			$response['searchId'] = $searchResponse->searchResult->searchId;
    			$response['records'] = array();
    			foreach ($searchResponse->searchResult->recordList->record as $record) {
    
    				$response['records'][] = $this->map($record);
    			}
    		}
    		return $response;
    	}
    
    	function load($id, $type) {
    
    		if ($id) {
    			$request = new GetRequest();
    
    			$request->baseRef = new RecordRef();
    			$request->baseRef->type = $type;
    			$request->baseRef->internalId = $id;
    			$getResponse = $this->service->get($request);
    
    			if ($getResponse && $getResponse->readResponse->status->isSuccess) {
    				return array('status' => TRUE, 'record' => $this->map($getResponse->readResponse->record));
    			} else {
    
    				return array('status' => FALSE, 'msg' => $getResponse->readResponse->status->statusDetail[0]->message);
    			}
    		}
    	}
    
    	function map($record) {
    		return $record;
    	}
    
    	function getCustomField($record, $fieldName) {
    		foreach ($record->customFieldList->customField as $field)
    			if ($field->internalId == $fieldName)
    				return $field->value;
    		return '';
    	}
    
    	function update($record) {
    		$request = new UpdateRequest();
    		$request->record = $record;
    		$updateResponse = $this->service->update($request);
    		if ($updateResponse->writeResponse->status->isSuccess) {
    			return array('status' => TRUE);
    		} else {
    			return array('status' => FALSE, 'msg' => $updateResponse->writeResponse->status->statusDetail[0]->message);
    		}
    	}
    
    }
    
  • Most of the magic lies in the constructor where Netsuite Service is intialized and and we get the dynamic Date Center URL, once for a session.
  • NSEntity.php serves as the base class for any entity that you want to use in the app. For e.g. here is a class for Items.
    Yii::import('application.models.NSEntity');
    class Item extends NSEntity {
    
    	public $record;
    
    	public function __construct() {
    		parent::__construct();
    	}
    
    	function map($record) {
    		$thumb = $this->getCustomField($record, 'custitem_internalimagethumb');
    		if ($thumb && $thumb->internalId)
    			$thumb = $thumb->internalId;
    
    		return array(
    			'type' => lcfirst(get_class($record))
    			, 'internal_id' => $record->internalId
    			, 'item_id' => $record->itemId
    			, 'image_thumb' => $thumb
    		);
    	}
    
    	function createSearchRequest($searchTerm) {
    		$itemIdSearchField = new SearchStringField();
    		$itemIdSearchField->operator = "contains";
    		$itemIdSearchField->searchValue = $searchTerm;
    
    		$search = new ItemSearchBasic();
    		$search->itemId = $itemIdSearchField;
    
    		$request = new SearchRequest();
    		$request->searchRecord = $search;
    		return $request;
    	}
    
    }
    
Advertisements

I am a web developer working @ Gaditek

Tagged with: ,
Posted in Computer And Internet, Netsuite, Programming & Development, Uncategorized
3 comments on “Integrating Netsuite PHP Toolkit in Yii
  1. Bhargav says:

    How to add item in the netsuite (get the item internal id after inserting it) and use it for the Order

  2. fungku says:

    It’s so hard finding netsuite stuff online. Thanks for including it in your blog.

    I use netsuite PHP toolkit similarly in Laravel, except I created a nice composer package for it. (fungku/netsuite-php on github or packagist)

    Keep on with netsuite posts 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: