<?php

/*
	--------------------
	--> DOCUMENTITION <--
	--------------------
	## Class :: ManageCustomer
	--------------------
	R :: required 
	O :: optional 
	--------------------
	## Valid fields ::   
	> UserName :: username || R 
	> Password :: password || R
	> TransType :: contains value ADD UPDATE DELETE || R
	> Vendor :: merchant id || R
	> CustomerID :: unique id  of customer || R 
	> CustomerKey :: for delete or update recored || R
	> CustomerName  :: compnay name || R
	> FirstName :: first name || O
	> LastName :: title like Business unit || O
	> Department  :: like Sales || O
	> Street1 :: street address 1 || O
	> Street2 :: street address 2 || O
	> Street3 :: street address 3 || O
	> City :: city name || O 
	> StateID :: code of state contain two character || O
	> Province :: province || O
	> CountryID :: country code contain three character || O
	> Zip :: zip code || O
	> DayPhone :: day phone number || O 
	> NightPhone :: night phone number || O
	> Mobile :: mobile phone number || O
	> Fax :: fax number || O
	> Email :: valid email address || O
	> Status :: ACTIVE INACTIVE PENDING CLOSED || O
	--------------------
	## Response :: 
	success ::  Array([CustomerKey] =>  -- [Vendor] =>  -- [code] =>  OK [error] =>  OK [Username] =>  -- )
	error ::  Array ( [code] =>  Not_Enough_Privilege [error] =>  Not enough privilege to access this merchant )
	--------------------
	## Sample code :: 
	> creating Customer
	$return = \Encgateway\ManageCustomer::crate(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'ADD','Vendor'=>'107','CustomerID'=>'102','CustomerName' => 'My customer',"FirstName" => ' ','LastName' =>' ','Title'=>' ','Department'=>' ','Street1' => 'Address 1','Street2' => 'Address2','Street3' => 'Address 3','City'=> 'city','StateID' => '23','Province' => '','Zip' =>'123456', 'CountryID' => 'Can','DayPhone' => '10234','NightPhone' => '1000','Mobile'=> '10002','Fax' => '123456','Email'=>'test@gmail.com','Status'=>'Active'));

	> delete Customer
	$return = \Encgateway\ManageCustomer::delete(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'DELETE','Vendor'=>'107','CustomerKey'=> '58','CustomerID'=>'102','CustomerName' => 'My customer'));

	> update Customer
	$return = \Encgateway\ManageCustomer::update(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'UPDATE','Vendor'=>'1237','CustomerKey'=> '60','CustomerID'=>'102','CustomerName' => 'My customer',"FirstName" => '','LastName' =>'','Title'=>'','Department'=>'','Street1' => 'Address 1','Street2' => 'Address2','Street3' => 'Address 3','City'=> 'city','StateID' => '23','Province' => '','Zip' =>'123456', 'CountryID' => 'Can','DayPhone' => '10234','NightPhone' => '1000','Mobile'=> '10002','Fax' => '123456','Email'=>'test@gmail.com','Status'=>'INACTIVE'));
	
*/

namespace Encgateway;

class ManageCustomer extends ApiResource {
	
	public static $data = array(); 
	
	public static $processors = "ManageCustomer";
	
	/* 
		validate parameter for add update and delete customer 
	*/
	public static function validParams($param){
		$rule = array(
			'UserName' => 'required',
			'Password' => 'required',
			'TransType' => 'required|contains,ADD UPDATE DELETE',
			'Vendor' => 'required',
			'CustomerID' => 'required',
			'CustomerName' => 'required|max_len,256',
			'FirstName' => 'max_len,256',
			'CustomerKey' => 'required',
			'LastName' => 'max_len,256',
			'Title' => 'max_len,256',
			'Department' => 'max_len,256',
			'Street1' => 'max_len,512',
			'Street2' => 'max_len,512',
			'Street3' => 'max_len,512',
			'City' => 'max_len,256',
			'StateID' => 'max_len,2',
			'Province' => 'max_len,50',
			'CountryID' => 'max_len,3',
			'Zip' => 'max_len,10',
			'DayPhone' => 'max_len,15',
			'NightPhone' => 'max_len,15',
			'Mobile' => 'max_len,15',
			'Fax' => 'max_len,25',
			'Email' => 'valid_email',
			'Status' => 'contains,ACTIVE INACTIVE PENDING CLOSED'
		);
		if(isset($param['ApiToken']) ) {
			unset($rule['UserName']);
			unset($rule['Password']);
		}
		if(isset($param['TransType']) &&  $param['TransType'] == "ADD"){
			unset($rule['CustomerKey']);
		}elseif(isset($param['TransType']) &&  $param['TransType'] == "UPDATE") {
			
		}else{
			unset($rule['FirstName']);unset($rule['LastName']);unset($rule['Title']);unset($rule['Zip']);unset($rule['Street1']);unset($rule['Street2']);
			unset($rule['Street3']);unset($rule['City']);unset($rule['StateID']);unset($rule['Province']);unset($rule['Department']);unset($rule['CountryID']);unset($rule['DayPhone']);
			unset($rule['NightPhone']);unset($rule['Mobile']);unset($rule['Fax']);unset($rule['Status']);unset($rule['Email']);
		}
		
		$is_valid = GUMP::is_valid($param,$rule);
		
		return $is_valid;
	}
	
	
	/* 
		function for update customer 
	*/
	public static function update($params = null) {
		self::$data['params'] 	= $params;
		self::$data['success'] 	= 0;
		self::$data['error'] 	= '';
		if(self::_validateArray($params)){
			if(Apib::getApiKey()) {
				$params['ApiToken'] = Apib::getApiKey();
			}
			$ret = self::validParams($params);
			if($ret == 1){
				$response = self::_doTrans($params,self::$processors);
				self::$data['response'] = $response; 
				if(isset($response['CustomerKey'])) : 
					self::$data['success'] = 1;
				endif;
			}else{
				self::$data['error'] = $ret;
			}
		}else{
			self::$data['error']['invalid_argument'] = 'Array is not valid, You must pass array is argument.';
		}
		
		self::$data = self::responseFormat(self::$data,Apib::getOutputFormat());
		return self::$data;
	}
	
	/* 
		function for delete customer 
	*/
	public static function delete($params = null) {
		self::$data['params'] 	= $params;
		self::$data['success'] 	= 0;
		self::$data['error'] 	= '';
		if(self::_validateArray($params)){
			if(Apib::getApiKey()) {
				$params['ApiToken'] = Apib::getApiKey();
			}
			$ret = self::validParams($params);
			if($ret == 1){
				$response = self::_doTrans($params,self::$processors);
				self::$data['response'] = $response; 
				if(isset($response['CustomerKey'])) : 
					self::$data['success'] = 1;
				endif;
			}else{
				self::$data['error'] = $ret;
			}
		}else{
			self::$data['error']['invalid_argument'] = 'Array is not valid, You must pass array is argument.';
		}
		
		self::$data = self::responseFormat(self::$data,Apib::getOutputFormat());
		return self::$data;
	} 
	
	/* 
		function for creating customer 
	*/
	public static function create($params = null) {	
		self::$data['params'] 	= $params;
		self::$data['success'] 	= 0;
		self::$data['error'] 	= '';
		if(self::_validateArray($params)){
			if(Apib::getApiKey()) {
				$params['ApiToken'] = Apib::getApiKey();
			}
			$ret = self::validParams($params);
			if($ret == 1){
				$response = self::_doTrans($params,self::$processors);
				self::$data['response'] = $response; 
				if(isset($response['CustomerKey'])) : 
					self::$data['success'] = 1;
				endif;
			}else{
				self::$data['error'] = $ret;
			}
		}else{
			self::$data['error']['invalid_argument'] = 'Array is not valid, You must pass array is argument.';
		}
		
		self::$data = self::responseFormat(self::$data,Apib::getOutputFormat());
		return self::$data;
    }
}