<?php
/*
	--------------------
	--> DOCUMENTITION <--
	--------------------
	## Class :: ManageContract
	--------------------
	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
	> BillAmt  :: Amount billed format :: dd.cccc || R
	> PaymentInfoKey :: id of payment_method || R - or add and update
	> PaymentType :: R || contains value CARD CHECK
	> StartDate :: R || MMDDYYYY || The start date must be greater than today's date.
	> EndDate :: R || MMDDYYYY || date must be greater than the start date.
	> BillingPeriod :: R || contains value DAY WEEK MONTH YEAR
	> BillingInterval :: R 
	> ContractId :: contract id|| O
	> ContractName :: name of contract || O
	> TaxAmt  :: text amount || dd.cccc format  || O
	> TotalAmt :: BillAmt + TaxAmt || O
	> MaxFailures :: The number of times the system will try to re-process a failed payment|| O 
	> FailureCount :: count failular || O
	> Status :: contains,ACTIVE INACTIVE PENDING CLOSED || O
	> EmailMerchant :: send email to merchant 0 = no,1 = true || O
	> EmailMerchantFailure :: send failular email to merchant 0 = no,1 = true || O
	> EmailCustomer :: send email to merchant 0 = no,1 = true || O
	> EmailCustomerFailure :: send failular email to merchant 0 = no,1 = true || O
	
	--------------------
	## Response :: 
	success ::  Array([ContractKey] =>  -- [Vendor] =>  -- [code] =>  OK [error] =>  OK [Username] =>  -- )
	error ::  Array ( [code] =>  Not_Enough_Privilege [error] =>  Not enough privilege to access this merchant )
	--------------------
	## Sample code :: 
	> creating Contract
	$return = \Encgateway\ManageContract::create(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'ADD','Vendor'=>'107','CustomerKey'=> '45','CustomerID'=>'102','BillAmt' => '12.00','PaymentInfoKey' => '30','PaymentType' => 'CARD','StartDate' => '04302016','EndDate' => '05102016','BillingPeriod' => 'WEEK','BillingInterval' => '2','ContractId' => '0001','ContractName' => 'My contract','TaxAmt' => '0.00','TotalAmt' => '0.00','MaxFailures' => 1,'FailureCount'=>'2','status' => 'ACTIVE','EmailMerchant'=>'1','EmailMerchantFailure'=>'1','EmailCustomer'=>'1','EmailCustomerFailure'=>'1'));

	> delete Contract
	$return = \Encgateway\ManageContract::delete(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'DELETE','Vendor'=>'107','CustomerKey'=> '45','CustomerID'=>'102','PaymentInfoKey' => '30','ContractKey' => '16','PaymentType' => 'CARD'));

	> 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 ManageContract extends ApiResource {
	
	public static $data = array(); 
	
	public static $processors = "ManageContract";
	
	/* 
		validate parameter for add update and delete contract 
	*/
	public static function validParams($param){
		$rule = array(
			'UserName' => 'required',
			'Password' => 'required',
			'TransType' => 'required|contains,ADD UPDATE DELETE',
			'Vendor' => 'required',
			'CustomerKey' => 'required',
			'BillAmt' => 'required',
			'ContractKey' => 'required|integer',
			'PaymentInfoKey' => 'required',
			'PaymentType' => 'required|contains,CARD CHECK',
			'StartDate' => 'required',
			'EndDate' => 'required',
			'BillingPeriod' => 'required|contains,DAY WEEK MONTH YEAR',
			'BillingInterval' => 'required',
			'ContractId' => 'alpha_numeric',
			'ContractName' => 'alpha_space',
			'TaxAmt' => 'float',
			'TotalAmt' => 'float',
			'MaxFailures' => 'integer',
			'FailureCount' => 'integer',
			'Status' => 'contains,ACTIVE INACTIVE PENDING CLOSED',
			'EmailMerchant' => 'integer|contains,0 1',
			'EmailMerchantFailure' => 'integer|contains,0 1',
			'EmailCustomer' => 'integer|contains,0 1',
			'EmailCustomerFailure' => 'integer|contains,0 1',
		);
		if(isset($param['ApiToken']) ) {
			unset($rule['UserName']);
			unset($rule['Password']);
		}
		if(isset($param['TransType']) &&  $param['TransType'] == "ADD"){
			unset($rule['ContractKey']);
		}elseif(isset($param['TransType']) &&  $param['TransType'] == "UPDATE") {
		}else{
			unset($rule['BillAmt']);
			unset($rule['PaymentInfoKey']);
			unset($rule['PaymentType']);
			unset($rule['StartDate']);
			unset($rule['EndDate']);
			unset($rule['BillingPeriod']);
			unset($rule['BillingInterval']);
			unset($rule['ContractName']);
			unset($rule['TaxAmt']);
			unset($rule['TotalAmt']);
			unset($rule['MaxFailures']);
			unset($rule['FailureCount']);
			unset($rule['Status']);
			unset($rule['EmailMerchant']);
			unset($rule['EmailMerchantFailure']);
			unset($rule['EmailCustomer']);
			unset($rule['EmailCustomerFailure']);
		}
		
		$is_valid = GUMP::is_valid($param,$rule);
		
		return $is_valid;
	}
	
	
	/* 
		function for update contract 
	*/
	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['ContractKey'])) : 
					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 contract 
	*/
	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['ContractKey'])) : 
					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 contract 
	*/
	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['ContractKey'])) : 
					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;
    }
	
}

?>