<?php
/**
	* 
	* Start 
	* Create Date: 22 August, 2016
	* This module file using for user dashboard page.
	* Created saravana kumar.
	* @see no link
	* Last Updated: 22 August, 2016
	* End
	* 
*/

	namespace App\Models;
use CodeIgniter\Model;

Class userdash_model extends Model {
	
   public function __construct(){
		$this->db = \Config\Database::connect();
		$session = \Config\Services::session();
	}
	public function Mydashresult($condition,$limit) {
		$session = \Config\Services::session();

	    $builder = $this->db->table('history');
		$builder->select('history.amount, history.date, history.transactionid, payment_controls.payment_value,payment_controls.payment_name');
	    $builder->join('payment_controls', 'history.payment_id = payment_controls.payment_id', 'inner');
	    $builder->where($condition);
	    $builder->where('uusersid',$session->get('uusersid'));
	    $builder->where('payment_controls.payment_key','logo');
	    // $builder->limit();
	    $builder->orderBy('history.date','DESC');
	    $query = $builder->get($limit);
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getResult();
		} else {
			return false;
		}
	}

	public function MyHistoryData($condition) {
		$session = \Config\Services::session();

	    $builder = $this->db->table('history');
		$builder->selectSum('amount');
	    $builder->where($condition);
	    $builder->where('uusersid',$session->get('uusersid'));
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getRow()->amount;
		} else {
			return false;
		}
	}

	public function MyHistoryData1($condition) {
	    $builder = $this->db->table('history');
		$builder->selectSum('amount');
	    $builder->where($condition);
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getRow()->amount;
		} else {
			return false;
		}
	}

	public function MySponserCount($condition) {
	    $builder = $this->db->table('uusers');
		$builder->select('*');
	    $builder->where($condition);
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getNumRows();
		} else {
			return 0;
		}
	}

	// public function MySponsers() {
	//     $builder = $this->db->table('uusers');
	// 	$builder->select('*');
	//     $builder->where('uusersid',$session->get('uusersid'));
	//     $query = $builder->get();
	//     if ($query->getNumRows() > 0 ) {
	//     	return $query->getResult();
	// 	} else {
	// 		return 0;
	// 	}
	// }

  	public function MySponsers() {
		$session = \Config\Services::session();
	    $builder = $this->db->table('uusers');
		$builder->select('*');
	    $builder->where('uusersid',$session->get('uusersid'));
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getResult();
		} else {
			return 0;
		}
	}
  
	public function MySponsersLevel($condition) {
	    $builder = $this->db->table('uusers');
		$builder->select('*');
	    $builder->where($condition);
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getResult();
		} else {
			return 0;
		}
	}

	public function MylevelHistoryData($condition) {
	    $builder = $this->db->table('history');
		$builder->selectSum('amount');
	    $builder->where($condition);
	    $query = $builder->get();
	    if ($query->getNumRows() > 0 ) {
	    	return $query->getRow()->amount;
		} else {
			return false;
		}
	}


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

}