<?php

class ControllerExtensionModuleAffClassicEvent extends Controller {

	protected $mroute = 'extension/module/aff_classic';
	protected $mcode = 'module_aff_classic';

	public function eventDeleteOrderBefore(&$route, &$order_id) {
		$this->load->model('extension/aff_classic');
		$this->model_extension_aff_classic->deleteTransactionByOrderId($order_id);
	}
	
	public function eventAccountTransactionTBefore(&$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->model('account/customer');
		if(!$this->model_account_customer->getAffiliate($this->customer->getId())) {
			$data['aff_classic_status'] = false;
			return;
		}

		$this->load->language('affiliate/aff_classic');
		foreach(array('text_balance_affiliate', 'text_display_transaction_type', 'option_affiliate_transaction', 'option_store_credits', 'text_store_credits') as $_v) {
			$data[$_v] = $this->language->get($_v);
		}		
		$data['text_total'] = $data['text_store_credits'];
		$data['transaction_type'] = isset($this->request->request['tt']) && $this->request->request['tt'] === 'a' ? 'a' : 'sc';
		$data['link_type_affiliate'] = html_entity_decode($this->url->link('account/transaction', 'tt=a', true), ENT_QUOTES, 'UTF-8');
		$data['link_type_store_credits'] = html_entity_decode($this->url->link('account/transaction', '', true), ENT_QUOTES, 'UTF-8');

		$this->load->model('extension/aff_classic');
		$data['balance_affiliate'] = $this->currency->format($this->model_extension_aff_classic->getTransactionTotal($this->customer->getId()), $this->session->data['currency']);		
	}
	
	public function eventAccountGetTransactionsBefore(&$route, &$args) {
		if(!isset($this->request->request['tt']) || $this->request->request['tt'] !== 'a' || !$this->customer->getId()) return;
		$aff_classic_status = $this->config->get($this->mcode . '_status') && $this->config->get($this->mcode . '_commission_table');		
		if(!$aff_classic_status) return;
		
		$data = isset($args[0]) ? $args[0] : $args;
		$data['customer_id'] = $this->customer->getId();
		$this->load->model('extension/aff_classic');
		return $this->model_extension_aff_classic->getTransactions($data);		
	}

	public function eventAccountGetTotalTransactionsBefore(&$route, &$args) {
		if(!isset($this->request->request['tt']) || $this->request->request['tt'] !== 'a' || !$this->customer->getId()) return;
		$aff_classic_status = $this->config->get($this->mcode . '_status') && $this->config->get($this->mcode . '_commission_table');		
		if(!$aff_classic_status) return;	
		$this->load->model('extension/aff_classic');
		return $this->model_extension_aff_classic->getTotalTransactions($this->customer->getId());
	}	
	
	public function eventCustomerDeleteTransactionByOrderIdBefore(&$route, &$args) {
		if(!$this->config->get($this->mcode . '_status') || !$this->config->get($this->mcode . '_commission_table')) return;
		
		$order_id = $args[0];		
		$this->load->model('extension/aff_classic');
		$this->model_extension_aff_classic->deleteTransactionByOrderId($order_id);
		return true;
	}
	
	public function eventCustomerGetTotalTransactionsByOrderIdBefore(&$route, &$args) {
		if(!$this->config->get($this->mcode . '_status') || !$this->config->get($this->mcode . '_commission_table')) return;
		
		$order_id = $args[0];		
		$this->load->model('extension/aff_classic');
		return $this->model_extension_aff_classic->getTotalTransactionsByOrderId($order_id);
	}

	public function eventCustomerAddTransactionBefore(&$route, &$args) {
		if($this->config->get('_sc_transaction')) {
			$this->config->set('_sc_transaction', false);
			return;
		}
		if(!$this->config->get($this->mcode . '_status') || !$this->config->get($this->mcode . '_commission_table')) return;
		
		$customer_id = $args[0];
		$description = $args[1];
		$amount = isset($args[2]) ? $args[2] : '';
		$order_id = isset($args[3]) ? intval($args[3]) : 0;
		
		$this->load->model('extension/aff_classic');
		return $this->model_extension_aff_classic->addTransaction($customer_id, $description, $amount, $order_id);
	}
	
}