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

class Pay extends CI_Controller {

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


	public function payin()
    {
	
		## 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;
        uses_class('finance');
       
        $finance = new finance($this->token_model);//
        $finance->TypeService = 'pay';
        $finance->Type = 'payin';
        $finance->agent_id = $this->token_model->user_id;
        $finance->setData($json);
       
        if($finance->getProvider())
        {
            $finance->Retrait();
           if(!empty($finance->Return['reponse'])) $result['data'] = $finance->Return['reponse'];
           elseif(!empty($finance->Return['error'])) $result['error'] = $finance->Return['error'];
        }
        else
        {

            header('HTTP/1.0 404 Resource not found');
            if(!empty($finance->Return['error'])) $result['error'] = $finance->Return['error'];
            else $result['error'] =apiError(400,$finance->Error);
			local_json($result);
        }
        local_json($result);
    }
	public function payout()
    {
	
		## 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;
        uses_class('finance');
        $finance = new finance($this->token_model);//
        $finance->TypeService = 'pay';
        $finance->Type = 'payout';
        $finance->agent_id = $this->token_model->user_id;
        $finance->setData($json);
       
        if($finance->getProvider())
        {
           $finance->Depot();
           if(!empty($finance->Return['reponse'])) $result['data'] = $finance->Return['reponse'];
           elseif(!empty($finance->Return['error'])) $result['error'] = $finance->Return['error'];
        }
        else
        {
            header('HTTP/1.0 404 Resource not found');
			$return['error'] =apiError(400,$finance->Error);
			local_json($return);
        }
        local_json($result);
    }
	public function bulkout()
    {
	
		## 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;
        $cjson = $json;
        unset($cjson['data']);
        uses_class('finance');
        $finance = new finance($this->token_model);//
        $finance->TypeService = 'pay';
        $finance->Type = 'payout';
        $finance->agent_id = $this->token_model->user_id;
        if(is_array($json['data']))
        {
            $cpt = 1;
            foreach($json['data'] as $dat)
            {
                if($cpt>1) $add = '-'.substr(strval(1000+$cpt),0);
                $mjson = array_merge($dat,$cjson);
                $mjson['order_id'].=$add;
                $mjson['reference_id'].=$add;

                
               
               
                $finance->setData($mjson);
                if($finance->getProvider())
                {
                    $finance->BulkOut();

                
                }
                $cpt++;
            }
            $return['transaction_id']= $finance->Bulks[0]['transaction_id'];
            $return['order_id']= $finance->Bulks[0]['order_id'];
            $return['transaction_id']= $finance->Bulks[0]['transaction_id'];
            $return['datas'] = $finance->Bulks;
            local_json($return);
        }
        $finance = new finance($this->token_model);//
        $finance->TypeService = 'bulk';
        $finance->Type = 'bulkout';
        $finance->agent_id = $this->token_model->user_id;
       
        $finance->setData($json);
        if($finance->getProvider())
        {
            $finance->BulkOut();
           if(!empty($finance->Return['reponse'])) $result['data'] = $finance->Return['reponse'];
           elseif(!empty($finance->Return['error'])) $result['error'] = $finance->Return['error'];
        }
        else
        {
            header('HTTP/1.0 404 Resource not found');
			$return['error'] =apiError(400,$finance->Error);
			local_json($return);
        }
       
    }
}