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

class Status extends CI_Controller {

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


	public function index()
    {
	
		## METHODE ACCEPTE
		$this->token_model->valideMethod('GET');
		
		## AUTHENTIFICATION 
		$this->token_model->valideToken();

		## RECUPERATION DES DONNEES 
		
		if(!empty($_GET['transaction_id'])) $cond = "transaction_id = '".$_GET['transaction_id']."'";
		elseif(!empty($_GET['order_id'])) $cond = "order_id = '".$_GET['order_id']."'";
		
		

		//REQUETE DE RECUPERATION
		success:
		$query = "select  transactions.agent_id,amount_total,transactions.account_number,transaction_id,infos,merchant_key,transactions.date_created,transactions.date_payment as status_date,transactions.status,service_code,provider_code, amount from transactions 
			
			left join users on users.agent_id = transactions.agent_id  and role = 'marchand'
			left join api_users on api_users.user_id = users.user_id
			where transactions.agent_id = '".$this->token_model->user_id."' and $cond limit 1";// and   transaction_id='".$this->token_model->Data['transactionid']."'";
		
		
		$trans = $this->db->query($query)->result_array();
		
		
		if(count($trans)>1 or empty($trans))
		{
			header('HTTP/1.0 404 Resource not found');
			$return['error'] =apiError(400,"resource not available");
			local_json($return);
		}
		
		$trans = $trans[0];
		
		$infos = json_decode($trans['infos'],true);
		$infos['date_created'] = $trans['date_created'];
		$infos['amount'] = $trans['amount'];
		$infos['amount_total'] = $trans['amount_total'];
		$infos['status']=strtoupper($trans['status']);
		if($infos['status']!='PENDING') $infos['status_date'] = $trans['status_date'];
		$trans['infos'] = json_encode($infos);
		
		$trans = loadTransaction($trans);
		
		
		if($trans['status']=='') $trans['status'] = 'PENDING';

		if(strtolower($trans['status'])=='pending')
		{
			
			uses_class('finance');
			$finance = new finance();
			$status = $finance->getStatus($trans);
			$trans['status']=strtoupper($status);
			$finance->valideTransaction($status,$trans);
		}
		
		$data = $trans;
		
		/*
		$reponse['date_created'] = $trans['date_created'];
		
		$reponse['stime'] = ($trans['status_date']);
		$reponse['ttime'] = strtotime($trans['status_date']);
		$reponse['status_date'] = $trans['status_date'];
		$reponse['amount_total'] = $trans['amount_total'];
		unset($reponse['provider_number']);
		unset($reponse['provider_message']);
		unset($reponse['provider_name']);
		unset($reponse['phone']);
		$reponse['status']=strtoupper($trans['status']);
		$reponse['country']= strtoupper($reponse['country']);
		*/
		 
		unset($data['notif_url']);
		unset($data['notify_url']);
		unset($data['cancel_url']);
		unset($data['provider_code']);

		
		$reponse = $data;
		

		
	
		$result['data']=$reponse;
		local_json($result);

		## METHODE ACCEPTE
		$this->token_model->valideMethod('POST');

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

		## RECUPERATION DES DONNEES 
		$this->token_model->getData('JSON');
       
        $json = $this->token_model->Data;
        
    }
	

}