首页 PHP 正文
534

Netsuite更新customField, 删除FileCabinet的文件, 高级搜索

//$service = new NetSuiteService();
class CustomRecordSearchAdvancedBuilder{
    private static $instance; 
    private $request;
    private $NSService;
    private $search;
    private $pageIndex;
    private $searchId;
    private $hasMorePage;
    private $fileRecordRef;

    public static function getInstance(){
        if (empty(static::$instance)) {
            static::$instance = new static();
        }
        return static::$instance;
    }
    
    private function __construct() {
        $this->initAdvanceSearch();
        $this->buildCriteria();
        $this->buildRequest();
        $this->NSService = NetSuite::getInstance();
        
        $this->fileRecordRef = new RecordRef();
        $this->fileRecordRef->type = RecordType::file;
    }
    
    private function initBasicSearch(){
        $this->search = new CustomRecordSearch();
    }
    
    private function initAdvanceSearch(){
        $this->search = new CustomRecordSearchAdvanced();
        $this->buildColumns(); //Init columns element
    }
    
    private function buildColumns(){
        $this->search->columns = new CustomRecordSearchRow();
        $this->search->columns->basic = new CustomRecordSearchRowBasic();
        $this->search->columns->basic->internalId = [new SearchColumnSelectField()];
        
        $field1 = new SearchColumnSelectCustomField();
        $field1->scriptId = 'abcde';
        $field2 = new SearchColumnSelectCustomField();
        $field2->scriptId = 'abcde2';
        $field3 = new SearchColumnDateCustomField();
        $field3->scriptId = 'abcde3';
        $field4 = new SearchColumnStringCustomField();
        $field4->scriptId = 'abcde4';

        $fieldList = new SearchColumnCustomFieldList();
        $fieldList->customField = [$field1, $field2, $field3, $field4];
        
        $this->search->columns->basic->customFieldList = $fieldList;
        return $this;
    }
    
    private function buildCriteria(){
        if ($this->search instanceof CustomRecordSearchAdvanced) {
            $this->search->criteria =  new CustomRecordSearch();
            $this->search->criteria->basic = new CustomRecordSearchBasic();
            $this->search->criteria->basic->recType = new RecordRef();
            $this->search->criteria->basic->recType->type = RecordType::customRecord;
            $this->search->criteria->basic->recType->internalId = 7;
        } else {
            $this->search->basic = new CustomRecordSearchBasic();
            $this->search->basic->recType = new RecordRef();
            $this->search->basic->recType->type = RecordType::customRecord;
            $this->search->basic->recType->internalId = 7;
        }
        return $this;
    }
    
    private function buildRequest(){
        $this->request = new SearchRequest();
        $this->request->searchRecord = $this->search;
        return $this;
    }
    
    public function setSearchCreatedValue($value){
        $created = new SearchDateField();
        $created->operator = SearchDateFieldOperator::on;
        $created->searchValue = $value;
        $this->getCriteria()->basic->created = $created;
    }
      
    public function setSearchEntityId($internalId, $operator = '='){
        $long = new SearchLongField();
        $long->searchValue = $internalId;
        if ($operator == '>=') {
            $long->operator = SearchLongFieldOperator::greaterThanOrEqualTo;
        } else if ($operator == '<=') {
            $long->operator = SearchLongFieldOperator::lessThanOrEqualTo;
        } else {
            $long->operator = SearchLongFieldOperator::equalTo;
        }
        
        $this->getCriteria()->basic->internalIdNumber = $long;
        return $this;
    }
    
    public function getCriteria(){
        if ($this->search instanceof CustomRecordSearchAdvanced) {
            return $this->search->criteria;
        } else {
            return $this->search;
        }
    }
    
    public function onBeforeSearch(){
        $this->pageIndex = 1;
        $this->searchId = '';
        $this->hasMorePage = true;
        return $this;
    }
    
    public function hasMorePage(){
        return $this->hasMorePage;
    }
    
    private function setNoMorePage(){
        $this->hasMorePage = false;
        return $this;
    }
    
    public function searchByPage(){
        if ($this->pageIndex <= 1) {
            $response = $this->doSearch();
        } else {
            $response = $this->doSearchWithId($this->searchId, $this->pageIndex);
        }
    
        $this->searchId = ResponseHandler::getSearchId($response);
        if (ResponseHandler::hasNextPage($response)) {
            $this->pageIndex += 1;
            $this->hasMorePage = true;
        } else {
            $this->hasMorePage = false;
        }
        return $response;
    }
    
    public function doSearch(){
        $response = $this->NSService->getService()->search($this->request);
        return $response;
    }
    
    public function doSearchWithId($searchId, $pageIdx){
        $request = new SearchMoreWithIdRequest();
        $request->searchId = $searchId;
        $request->pageIndex = $pageIdx;
        $response = $this->NSService->getService()->searchMoreWithId($request);
        return $response;
    }
    
    public function getFileRecord($fileInternalId){
        $this->fileRecordRef->internalId = $fileInternalId;
        $file = $this->NSService->getRecord($this->fileRecordRef);
        if (isset($file->url)) {
            parse_str($file->url, $result);
            if (isset($result['_xt'])) {
                $file->name = preg_replace('/' . preg_quote($result['_xt']) . '$/i', '', $file->name) . $result['_xt'];
            }
        }
        return $file;
    }
    
}

function testAdvanceSearch($date){
    $search = CustomRecordSearchAdvancedBuilder::getInstance();
    $search->setSearchCreatedValue($date.'T00:00:00.000');
    $response = $search->doSearch();
    return $response;
}

function getCstmRcrdSearch($internalId){
    $search = new CustomRecordSearch();
    $search->basic = new CustomRecordSearchBasic();
    $search->basic->recType = new RecordRef();
    $search->basic->recType->type = RecordType::customRecord;
    $search->basic->recType->internalId = 7;
    $long = new SearchLongField();
    $long->searchValue = $internalId;
    $long->operator = SearchLongFieldOperator::equalTo;
    $search->basic->internalIdNumber = $long;
    return $search;
}

function getOneEntity($internalId){
    $search = getCstmRcrdSearch($internalId);
    $request = new SearchRequest();
    $request->searchRecord = $search;
    $response = NetSuite::getInstance()->getService()->search($request);
    return $response;
}

function getOneEntityByAdvance($internalId){
    $search = new CustomRecordSearchAdvanced();
    $search->criteria = getCstmRcrdSearch($internalId);
    $search->columns = new CustomRecordSearchRow();
    $search->columns->basic = new CustomRecordSearchRowBasic();
    $search->columns->basic->internalId = [new SearchColumnSelectField()];
    
    $field1 = new SearchColumnSelectCustomField();
    $field1->scriptId = 'abcde';
    $field2 = new SearchColumnSelectCustomField();
    $field2->scriptId = 'abcde2';
    $field3 = new SearchColumnDateCustomField();
    $field3->scriptId = 'abcde3';
    $field4 = new SearchColumnStringCustomField();
    $field4->scriptId = 'abcde4';
    
    $fieldList = new SearchColumnCustomFieldList();
    $fieldList->customField = [$field1, $field2, $field3, $field4];
    $search->columns->basic->customFieldList = $fieldList;
    $request = new SearchRequest();
    $request->searchRecord = $search;
    $response = NetSuite::getInstance()->getService()->search($request);
    return $response;
}

function getOneFile($fileId,){
    $file = CustomRecordSearchAdvancedBuilder::getInstance()->getFileRecord($fileId);
    unset($file->content);
    return $file;
}

function getFileInfo($fileId){
    $file = getOneFile($fileId);
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    return $finfo->buffer($file->content);
}
function removeFileFromFileCabinet($fileId) {
    $recordRef = new RecordRef();
    $recordRef->type = RecordType::file;
    $recordRef->internalId = $fileId;
    $request = new DeleteRequest();
    $request->baseRef = $recordRef;
    $response = NetSuite::getInstance()->getService()->delete($request);
    return $response;
}

function testUpdateCustomFieldValueForEntity($entityInternalId){
    $fieldName = 'abcde';//Hidden fields will not be updated.
    $response = getOneEntity($entityInternalId);
    if (ResponseHandler::isSuccess($response) && ResponseHandler::getTotalRecords($response) > 0) {
        $record = $response->searchResult->recordList->record[0];
        $updateRecord = new CustomRecord();
        $updateRecord->internalId = (int)$record->internalId;
        $updateRecord->recType = $record->recType;
        $updateRecord->customFieldList = new CustomFieldList();
        $stringCustomField = new StringCustomFieldRef();
        $stringCustomField->scriptId = $fieldName;
        $stringCustomField->value = 'hello 你好';//utf8_encode()'2021-02-23T16:00:00.000-08:00'
        $updateRecord->customFieldList->customField = [$stringCustomField];

        $request = new UpdateRequest();
        $request->record = $updateRecord;
        $updateResponse = $NetSuiteService->update($request);
        if ($updateResponse->writeResponse->status->isSuccess) {
            var_dump(getOneEntity($entityInternalId));
        } else {
            var_dump($updateResponse);
        }
    }
}

class ResponseHandler{
    
    public static function hasNextPage($response){
        return $response->searchResult->pageIndex < $response->searchResult->totalPages;
    }
    
    public static function getTotalRecords($response){ 
        return $response->searchResult->totalRecords; // e.g. pageSize=1000, total Records may be 1440
    }
    
    public static function getSearchId($response){
        return $response->searchResult->searchId;
    }
    
    public static function getTotalPages($response){
        return $response->searchResult->totalPages;
    }
    
    public static function getRecordList($response){
        return (array)$response->searchResult->searchRowList->searchRow;
    }
    
    public static function getTotalRecordsOfOnePage($response){
        return count(static::getRecordList($response));
    }
    
    public static function isSuccess($response){
        return $response->searchResult->status->isSuccess; //boolean
    }
    
    public static function isInvalidSearchMore($response){
        return !static::isSuccess($response) && $response->searchResult->status->statusDetail[0]->code == 'INVALID_SEARCH_MORE';
    }
    
    public static function handle($response){
        if (static::isSuccess($response) && static::getTotalRecords($response) > 0) {
            static::forEachEntity($response);
        }
    }
    
    private static function forEachEntity($response){
         foreach($response->searchResult->searchRowList->searchRow as $record){//CustomRecordSearchRow
            $signal = static::onEachRecord($record);
            if ($signal > 0) continue;
         }
    }
    
    private static function onEachRecord($record){
    }
}
根据自定义字段的值进行检索:
function searchOnCustomField(){
    $search = new CustomRecordSearch();
    $search->basic = new CustomRecordSearchBasic();
    $search->basic->recType = new RecordRef();
    $search->basic->recType->type = RecordType::customRecord;
    $search->basic->recType->internalId = 7;
    $search->basic->customFieldList = new CustomFieldList();
    $field = new SearchStringCustomField();
    $fieldName = 'custrecordtmpfilename';
    $field->scriptId = $fieldName;
    $field->searchValue = "abcdef";
    $field->operator = SearchStringFieldOperator::startsWith;
    $fieldList = new SearchCustomFieldList();
    $fieldList->customField = [$field];
    $search->basic->customFieldList = $fieldList;
    $request = new SearchRequest();
    $request->searchRecord = $search;
    
    $NetSuiteService->setSearchPreferences($bodyFieldsOnly=true,$pageSize=50,$returnSearchColumns=true);
    $response = $service->search($request);
    
}

正在加载评论...