<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends CI_Controller {

	public function __construct(){
        parent::__construct();
		$this->load->model('token_model');
    }
	public function balance()
    {
	
		$headers = apache_request_headers();

		## METHOD INCONNU
		$method = $_SERVER['REQUEST_METHOD'];
		$this->token_model->valideMethod($method,'GET');

		## AUTHENTIFICATION 
		$login = $headers['Authorization'];
		$explode = explode(' ',$login);
		$login = trim($explode[1]);		
		$this->token_model->valideToken($login);

		$account = $this->db->query("select * from customers_accounts where agent_id='".$this->token_model->user_id."'")->row_array();
		
		
		if(empty($account))
		{
			header('HTTP/1.0 401 Unauthorized');
			$return['error'] =apiError(400,"This account is not configured.");
			local_json($return);
		}
		
		$reponse['solde']= $account['Balance'];
		$reponse['commission'] = floor($account['commission']*10000)/10000;
		$result['data']= $reponse;
		local_json($result);
    }
    public function status($trans="")
    {
	
		## METHODE ACCEPTE
		$this->token_model->valideMethod('GET');

		## AUTHENTIFICATION 
		$this->token_model->valideToken();

		## RECUPERATION DES DONNEES 
		$this->token_model->getData('JSON');

        //REQUETE DE RECUPERATION
        $query = "select * from transactions where agent_id = '".$this->token_model->user_id."' and   transaction_id='$trans'";
       
		$trans = $this->db->query($query)->row_array();
		if(empty($trans))
		{
			header('HTTP/1.0 404 Resource not found');
			$return['error'] =apiError(400,"resource not available");
			local_json($return);
		}
		
		if(!empty($trans['status'])) $reponse['status']=strtoupper($trans['status']);
		else $reponse['status']=NULL;
		$reponse['id']=$trans['transaction_id'];
		$reponse['date_init']=strtodatetime($trans['Jour']);
		$reponse['date_end']=strtodatetime($trans['DatePayement']);
		$reponse['amount']= doubleval($trans['montant']);
		$reponse['fees']=doubleval($trans['frais']);
		if(!empty(strtotime($reponse['date_pay']))) $reponse['date_pay']=$trans['DatePayement'];
		if(!empty($reponse['reference'])) $reponse['reference']=$trans['MaviancePTN'];
		
		$result['data']=$reponse;
		local_json($result);
    }

}