<?php
/**
	* 
	* Start 
	* Create Date: 12 July, 2016
	* This module file using for only packages.
	* Created saravana kumar.
	* @see no link
	* Last Updated: 12 July, 2016
	* End
	* 
*/

namespace App\Models;
use CodeIgniter\Model;

Class finance_model extends Model {

	public function __construct(){
		$this->db = \Config\Database::connect();
	}
	

	public function GetTransactions($condition) {
	    $builder = $this->db->table('history');
		$builder->select('history.*,uusers.username,coin_info.coin_symbol');
	    $builder->join('uusers', 'history.uusersid = uusers.uusersid', 'inner');
	    $builder->join('coin_info', 'history.currency = coin_info.coid', 'left');
	    $builder->where($condition);
	    // $builder->where('history.is_delete','0');
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getResult();
	    
		} else {
			return false;
		}
	}

	public function Getadminearnings($condition) {
		
		
	    $builder = $this->db->table('history');
	    
	    $builder->where($condition);
	    // $builder->where('history.is_delete','0');
	    
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getResult();
	    
		} else {
			return false;
		}
	}

	public function GetExpiredeposit($condition) {
	    $builder = $this->db->table('deposit');
		$builder->select('deposit.*,uusers.username');
	    $builder->join('uusers', 'deposit.uusersid = uusers.uusersid', 'inner');
	    $builder->where($condition);
	    
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getResult();
	    
		} else {
			return false;
		}
	}

	public function GetDeposit($type) {
	    $builder = $this->db->table('history');
		$builder->select('history.*,uusers.username');
	    $builder->join('uusers', 'history.uusersid = uusers.uusersid', 'inner'); 
	    $builder->where('history.type',$type);
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getResult();
		} else {
			return false;
		}
	}
	public function PaymentName($condition) {
		$builder = $this->db->table('payment_controls');

		$builder->where($condition);
		$builder->select('payment_name');
		$query = $builder->get();
		
		if ($query->getNumRows()>0) {
			return $query->getRow();
		} else {
			return false;
		}

	}
    
    public function Paymentimage($condition) {
		$this->db->where($condition);
		$this->db->select('payment_value');
		$this->db->from('payment_controls');
		$query = $this->db->get();
		
		if ($query->num_rows()>0) {
			return $query->row();
		} else {
			return false;
		}
   }

   public function GetTransactionsCount($condition) {
		$query = $this->db->table('history');

		$query->select('historyid');
	    // $this->db->join('uusers', 'history.uusersid = uusers.uusersid', 'inner');
	   	$query->where($condition);
	   	$query1 = $query->get();
	    
	  	

	    if ($query1) {
	    	return $query1->getNumRows();
		} else {
			return 0;
		}
	}

	public function userGateway($uusersid,$payment_id) {
		$this->db->where('uusersid',$uusersid);
		$this->db->where('payment_id',$payment_id);
		$this->db->select('contents');
		$this->db->from('uusers_payment');
		$query = $this->db->get();
		
		if ($query->num_rows()>0) {
			return $query->row();
		} else {
			return false;
		}

	}

	public function GetTransactionsplans($condition) {
		
		/*$this->db->select('history.*,uusers.username,deposit.planid,plans.plan_name,package.package_name');
	    $this->db->from('history');
	    $this->db->join('uusers', 'history.uusersid = uusers.uusersid', 'inner');
	    $this->db->join('deposit', 'deposit.depositid = history.depositid', 'inner');
	    $this->db->join('plans', 'plans.planid = deposit.planid', 'inner');
	    $this->db->join('package', 'package.packageid = plans.packageid', 'inner');*/
	    $this->db->select('history.*,uusers.username,coin_info.coin_symbol');
	    $this->db->from('history');
	    $this->db->join('uusers', 'history.uusersid = uusers.uusersid', 'inner');
	    $this->db->join('deposit', 'deposit.depositid = history.depositid', 'inner');
	    $this->db->join('coin_info', 'deposit.currency = coin_info.coid', 'left');
	    $this->db->where($condition);
	    $this->db->where('history.is_delete','0');
	    
	    $query = $this->db->get();
	    if ($query->num_rows() > 0 ) {
	    	return $query->result();
	    
		} else {
			return false;
		}
	}

	public function Getsumof($condition='') {

		if($condition){
			$this->db->where($condition);
		}
		$this->db->select('SUM(history.amount) as amount, history.date, history.transactionid, history.uusersid, history.payment_id, uusers.username');
		$this->db->from('history');
		$this->db->join('uusers', 'history.uusersid = uusers.uusersid', 'inner');
		$query = $this->db->get();
		
		if ($query->num_rows()>0) {
			return $query->result();
		} else {
			return false;
		}
	}


	public function Getuserexchange($condition)
	{
		$this->db->select('exchange.*,uusers.username');
		$this->db->from('exchange');
		// $this->db->join('uusers', 'exchange.rec_user = uusers.uusersid', 'left');		
		$this->db->join('uusers', 'exchange.userid = uusers.uusersid', 'inner');		
		$this->db->where($condition);
		$query = $this->db->get();
		
		if ($query->num_rows()>0) {
			return $query->result();
		} else {
			return false;
		}
	}

}