<?php
class ControllerExtensionModuleAffClassicEvent extends Controller {
	private $error = array();

	protected $mroute = 'extension/module/aff_classic';
	protected $mcode = 'module_aff_classic';
	
	public function eventDeleteCustomerBefore(&$route, &$customer_id) {
		$cid = is_array($customer_id) ? $customer_id[0] : $customer_id;
		$this->load->model('extension/aff_classic');
		$this->model_extension_aff_classic->deleteTransactionsByCustomerId($cid);
	}
	
	public function eventAddTransactionBefore(&$route, &$args) {
		if($this->config->get('module_aff_classic_status') && $this->config->get('module_aff_classic_commission_table') && isset($args[0]) && preg_match("/^\d+$/", $args[0])) {	
			$customer_id = $args[0];
			$description = isset($args[1]) ? $args[1] : '';
			$amount = isset($args[2]) ? $args[2] : '';
			$order_id = isset($args[3]) ? $args[3] : 0;
			$this->load->model('extension/aff_classic');
			return $this->model_extension_aff_classic->addTransaction($customer_id, $description, $amount, $order_id);			
		}
	}
	
	public function eventDeleteTransactionByOrderIdBefore(&$route, &$args) {
		if($this->config->get('module_aff_classic_status') && $this->config->get('module_aff_classic_commission_table')) {				
			$order_id = is_array($args) ? $args[0] : $args;
			$this->load->model('extension/aff_classic');
			$this->model_extension_aff_classic->deleteTransaction($order_id);
			return true;
		}
	}	

	public function eventGetTotalTransactionsByOrderIdBefore(&$route, &$args) {
		if($this->config->get('module_aff_classic_status') && $this->config->get('module_aff_classic_commission_table')) {				
			$order_id = is_array($args) ? $args[0] : $args;
			$this->load->model('extension/aff_classic');
			return $this->model_extension_aff_classic->getTotalTransactionsByOrderId($order_id);			
		}
	}	
	
	
	public function eventAddTransactionCBefore(&$route, &$data) {	
		if(!isset($this->request->request['affiliate_transaction']) || !$this->request->request['affiliate_transaction'] || !$this->config->get($this->mcode . '_status') || !$this->config->get($this->mcode . '_commission_table')) return;

		$this->load->language($this->mroute);

		$json = array();

		if (!$this->user->hasPermission('modify', 'customer/customer')) {
			$json['error'] = $this->language->get('error_permission');
		} else {
			$this->load->model('extension/aff_classic');

			$this->model_extension_aff_classic->addTransaction($this->request->get['customer_id'], $this->request->post['description'], $this->request->post['amount']);

			$json['success'] = $this->language->get('text_success_add_transaction');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));		
		
		return true;
	}

	public function eventCustomerTransactionTBefore(&$route, &$data, &$template) {
		$data['aff_classic_status'] = $this->config->get($this->mcode . '_status') && $this->config->get($this->mcode . '_commission_table');	
		if(!$data['aff_classic_status']) return;
	
		$this->load->language($this->mroute);
		$this->load->model('extension/aff_classic');
		foreach(array('text_balance_affiliate', 'text_display_transaction_type','entry_transaction_type', 'option_affiliate_transaction', 'option_store_credits') as $_v) {
			$data[$_v] = $this->language->get($_v);
		}	
		$data['transaction_type'] = isset($this->request->request['tt']) && $this->request->request['tt'] === 'a' ? 'a' : 'sc';
		$data['customer_id'] = $this->request->get['customer_id'];
		$data['user_token'] = $this->session->data['user_token'];
		$data['balance_affiliate'] = $this->currency->format($this->model_extension_aff_classic->getTransactionTotal($this->request->get['customer_id']), $this->config->get('config_currency'));	
	}
	
	public function eventCustomerFormTBefore(&$route, &$data, &$template) {
		$data['aff_classic_status'] = $this->config->get($this->mcode . '_status') && $this->config->get($this->mcode . '_commission_table');
		if(!$data['aff_classic_status']) return;		
		
		$this->load->language($this->mroute);
		foreach(array('entry_transaction_type', 'option_affiliate_transaction', 'option_store_credits') as $_v) {
			$data[$_v] = $this->language->get($_v);
		}		
	}
}
