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

class Report extends MY_Controller {

	public function index()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			redirect('report/daily_bonus', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function daily_bonus()
	{
		save_current_url(base_url() . 'report/daily_bonus/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
		
			if($_POST) {
				$this->session->set_userdata(array('report_username' => trim($this->input->post('username', TRUE)), 'report_downline' => trim($this->input->post('downline', TRUE)), 'report_start_date' => trim($this->input->post('start_date', TRUE)), 'report_end_date' => trim($this->input->post('end_date', TRUE))));
			
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_daily_bonus_summary_report');
				
				$this->load->view('report_daily_bonus_summary', $_POST);
			}
			else {
				$this->session->unset_userdata(array('report_username' => '', 'report_downline' => '', 'report_start_date' => '', 'report_end_date' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_daily_bonus_summary_report');
				
				$this->load->view('report_daily_bonus_summary', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function daily_bonus_getlist()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
		
			/* Array of database columns which should be read and sent back to DataTables. Use a space where
			 * you want to insert a non-database field (for example a counter or static image)
			 */
			$aColumns = array(
								$this->db->dbprefix('member_income_summary') . '.income_date', 
								'SUM(' . $this->db->dbprefix('member_income_summary') . '.intro_bonus) AS intro_bonus',
								'SUM(' . $this->db->dbprefix('member_income_summary') . '.algebra_bonus) AS algebra_bonus',
								'SUM(' . $this->db->dbprefix('member_income_summary') . '.differential_bonus) AS differential_bonus',
								'SUM(' . $this->db->dbprefix('member_income_summary') . '.same_bonus) AS same_bonus',
								'SUM(' . $this->db->dbprefix('member_income_summary') . '.pool_bonus) AS pool_bonus',
							);
			$jColumns = $this->db->dbprefix('member_income_summary') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_income_summary';
			$jTable = 'members';
			//
			
			$start_date = date('Y-m-d', strtotime($this->session->userdata('report_start_date')));
			$end_date = date('Y-m-d', strtotime($this->session->userdata('report_end_date')));
			$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date >= ', $start_date);
			$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date <= ', $end_date);
			
			if($this->session->userdata('report_username') != '') {
				$this->db->where($this->db->dbprefix('members') . '.username', $this->session->userdata('report_username'));
			}
			
			if($this->session->userdata('report_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('report_downline')));
				if($query->num_rows() > 0) {
					$row = $query->row();
					$member_id = $row->member_id;
					$where = "(" . $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%,$member_id,%' OR " . $this->db->dbprefix('members') . ".member_id = $member_id)";
					$this->db->where($where);
				}
			}
			
			$this->db->group_by($this->db->dbprefix('member_income_summary') . '.income_date'); 
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by(str_replace(array('SUM(' . $this->db->dbprefix('member_income_summary') . '.leader_bonus) AS leader_bonus'), array($this->db->dbprefix('member_income_summary') . '.leader_bonus'), $aColumns[intval($this->db->escape_str($iSortCol))]), $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['income_date'];
				$row[] = $aRow['intro_bonus'];
				$row[] = $aRow['algebra_bonus'];
				$row[] = $aRow['differential_bonus'];
				$row[] = $aRow['same_bonus'];
				$row[] = $aRow['pool_bonus'];
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function income_summary()
	{
		save_current_url(base_url() . 'report/income_summary/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
		
			if($_POST) {
				$this->session->set_userdata(array('report_username' => trim($this->input->post('username', TRUE)), 'report_income_date' => trim($this->input->post('income_date', TRUE)), 'report_start_date' => trim($this->input->post('start_date', TRUE)), 'report_end_date' => trim($this->input->post('end_date', TRUE))));
			
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_income_summary_report');
				
				$this->load->view('report_income_summary', $_POST);
			}
			else {
				$this->session->unset_userdata(array('report_username' => '', 'report_income_date' => '', 'report_start_date' => '', 'report_end_date' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_income_summary_report');
				
				$this->load->view('report_income_summary', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function income_summary_getlist()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
		
			/* Array of database columns which should be read and sent back to DataTables. Use a space where
			 * you want to insert a non-database field (for example a counter or static image)
			 */
			$aColumns = array(
								$this->db->dbprefix('member_income_summary') . '.income_date', 
								$this->db->dbprefix('member_income_summary') . '.intro_bonus',
								$this->db->dbprefix('member_income_summary') . '.algebra_bonus',
								$this->db->dbprefix('member_income_summary') . '.differential_bonus',
								$this->db->dbprefix('member_income_summary') . '.same_bonus',
								$this->db->dbprefix('member_income_summary') . '.pool_bonus'
							);
			$jColumns = $this->db->dbprefix('member_income_summary') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_income_summary';
			$jTable = 'members';
			//
			
			$income_date_option = $this->session->userdata('report_income_date');
			if($income_date_option == 2) {
				$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date', date('Y-m-d'));
			}
			else if($income_date_option == 3) {
				$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date', date('Y-m-d', strtotime('-1 day', time())));
			}
			else if($income_date_option == 4) {
				$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date >= ', date('Y-m-d', strtotime('-7 days', time())));
				$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date < ', date('Y-m-d'));
			}
			else if($income_date_option == 5) {
				$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date >= ', date('Y-m-01'));
				$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date < ', date('Y-m-01', strtotime('+1 month', time())));
			}
			else if($income_date_option == 6) {
				$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date >= ', date('Y-m-01', strtotime('-1 month', time())));
				$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date < ', date('Y-m-01'));
			}
			else if($income_date_option == 7) {
				if($this->session->userdata('report_start_date') == '' || $this->session->userdata('report_end_date') == '') {
					$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date', date('Y-m-d'));
				}
				else {
					$start_date = date('Y-m-d', strtotime($this->session->userdata('report_start_date')));
					$end_date = date('Y-m-d', strtotime($this->session->userdata('report_end_date')));
					$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date >= ', $start_date);
					$this->db->where($this->db->dbprefix('member_income_summary') . '.income_date <= ', $end_date);
				}
			}
			
			$this->db->where($this->db->dbprefix('members') . '.username', $this->session->userdata('report_username'));
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['income_date'];
				$row[] = $aRow['intro_bonus'];
				$row[] = $aRow['algebra_bonus'];
				$row[] = $aRow['differential_bonus'];
				$row[] = $aRow['same_bonus'];
				$row[] = $aRow['pool_bonus'];
				$row[] = number_format($aRow['intro_bonus'] + $aRow['algebra_bonus'] + $aRow['differential_bonus'] + $aRow['same_bonus'] + $aRow['pool_bonus'], 2, '.', '');
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function income_detail()
	{
		save_current_url(base_url() . 'report/income_detail/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			if($_POST) {
				$this->session->set_userdata(
					array(
							'report_username' => trim($this->input->post('username', TRUE)), 
							'report_income_date' => trim($this->input->post('income_date', TRUE)), 
							'report_start_date' => trim($this->input->post('start_date', TRUE)), 
							'report_end_date' => trim($this->input->post('end_date', TRUE)),
							'report_bonus_type' => ($this->input->post('bonus_type', TRUE) != '') ? implode(',', $this->input->post('bonus_type', TRUE)) : '', 
					)
				);
			
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_income_detail_report');
				
				$this->load->view('report_income_detail', $_POST);
			}
			else {
				$this->session->unset_userdata(array('report_username' => '', 'report_income_date' => '', 'report_start_date' => '', 'report_end_date' => '', 'report_bonus_type' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_income_detail_report');
				
				$this->load->view('report_income_detail', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function income_detail_getlist()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
		
			/* Array of database columns which should be read and sent back to DataTables. Use a space where
			 * you want to insert a non-database field (for example a counter or static image)
			 */
			$aColumns = array(
								'transaction_date', 
								'transaction_type', 
								'from_member_username',
								'v_unit2_amount',
								'status'
							);
			
			// DB table to use
			$sTable = 'member_transactions';
			//
			
			$current_time = time();
			$income_date_option = $this->session->userdata('report_income_date');
			if($income_date_option == 2) {
				$start_date = strtotime(date('Y-m-d') . ' 00:00:00');
				$end_date = strtotime(date('Y-m-d') . ' 23:59:59');
				$this->db->where('transaction_date >= ', $start_date);
				$this->db->where('transaction_date <= ', $end_date);
			}
			else if($income_date_option == 3) {
				$start_date = strtotime(date('Y-m-d', strtotime('-1 day', $current_time)) . ' 00:00:00');
				$end_date = strtotime(date('Y-m-d', strtotime('-1 day', $current_time)) . ' 23:59:59');
				$this->db->where('transaction_date >= ', $start_date);
				$this->db->where('transaction_date <= ', $end_date);
			}
			else if($income_date_option == 4) {
				$start_date = strtotime(date('Y-m-d', strtotime('-7 days', time())) . ' 00:00:00');
				$end_date = strtotime(date('Y-m-d', strtotime('-1 day', $current_time)) . ' 23:59:59');
				$this->db->where('transaction_date >= ', $start_date);
				$this->db->where('transaction_date <= ', $end_date);
			}
			else if($income_date_option == 5) {
				$start_date = strtotime(date('Y-m-01') . ' 00:00:00');
				$end_date = strtotime(date('Y-m-01', strtotime('+1 month', time())) . ' 00:00:00');
				$this->db->where('transaction_date >= ', $start_date);
				$this->db->where('transaction_date < ', $end_date);
			}
			else if($income_date_option == 6) {
				$start_date = strtotime(date('Y-m-01', strtotime('-1 month', time())) . ' 00:00:00');
				$end_date = strtotime(date('Y-m-01') . ' 00:00:00');
				$this->db->where('transaction_date >= ', $start_date);
				$this->db->where('transaction_date < ', $end_date);
			}
			else if($income_date_option == 7) {
				if($this->session->userdata('report_start_date') != '' && $this->session->userdata('report_end_date') != '') {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('report_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('report_end_date'))));
					$this->db->where('transaction_date >= ', $start_date);
					$this->db->where('transaction_date <= ', $end_date);
				}
			}
			
			if($this->session->userdata('report_bonus_type') != '') {
				$where = "transaction_type IN(" .  $this->session->userdata('report_bonus_type') . ")";
				$this->db->where($where);
			}
			
			$this->db->where('to_member_username', $this->session->userdata('report_username'));
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				
				switch($aRow['transaction_type']) {
					case 1 : $row[] = $this->lang->line('report_intro_bonus'); break;
					case 2 : $row[] = $this->lang->line('report_algebra_bonus'); break;
					case 3 : $row[] = $this->lang->line('report_differential_bonus'); break;
					case 4 : $row[] = $this->lang->line('report_same_bonus'); break;
					case 5 : $row[] = $this->lang->line('report_pool_bonus'); break;
				}
				
				$row[] = ($aRow['from_member_username'] != '') ? $aRow['from_member_username'] : '-';
				$row[] = number_format($aRow['v_unit2_amount'], 2, '.', '');
				
				switch($aRow['status']) {
					case 0 : $row[] = $this->lang->line('report_unpaid'); break;
					case 1 : $row[] = $this->lang->line('report_paid'); break;
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function balance()
	{
		save_current_url(base_url() . 'report/balance/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			if($_POST) {
				$this->session->set_userdata(array('report_username' => trim($this->input->post('username', TRUE)), 'report_income_date' => trim($this->input->post('income_date', TRUE)), 'report_start_date' => trim($this->input->post('start_date', TRUE)), 'report_end_date' => trim($this->input->post('end_date', TRUE))));
			
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_balance_report');
				
				$this->load->view('report_balance', $_POST);
			}
			else {
				$this->session->unset_userdata(array('report_username' => '', 'report_income_date' => '', 'report_start_date' => '', 'report_end_date' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_balance_report');
				
				$this->load->view('report_balance', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function balance_getlist()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
		
			/* Array of database columns which should be read and sent back to DataTables. Use a space where
			 * you want to insert a non-database field (for example a counter or static image)
			 */
			$aColumns = array(
								'transaction_date', 
								'transaction_type', 
								'debit_amount',
								'credit_amount',
								'to_coin_balance',
								'remark',
								'1 AS tbl_type'
							);
			
			// DB table to use
			$sTable = 'member_transactions';
			//
			
			$where = "";
			$current_time = time();
			$income_date_option = $this->session->userdata('report_income_date');
			if($income_date_option == 2) {
				$start_date = strtotime(date('Y-m-d') . ' 00:00:00');
				$end_date = strtotime(date('Y-m-d') . ' 23:59:59');
				$where = "transaction_date >= $start_date AND transaction_date <= $end_date";
			}
			else if($income_date_option == 3) {
				$start_date = strtotime(date('Y-m-d', strtotime('-1 day', $current_time)) . ' 00:00:00');
				$end_date = strtotime(date('Y-m-d', strtotime('-1 day', $current_time)) . ' 23:59:59');
				$where = "transaction_date >= $start_date AND transaction_date <= $end_date";
			}
			else if($income_date_option == 4) {
				$start_date = strtotime(date('Y-m-d', strtotime('-7 days', time())) . ' 00:00:00');
				$end_date = strtotime(date('Y-m-d', strtotime('-1 day', $current_time)) . ' 23:59:59');
				$where = "transaction_date >= $start_date AND transaction_date <= $end_date";
			}
			else if($income_date_option == 5) {
				$start_date = strtotime(date('Y-m-01') . ' 00:00:00');
				$end_date = strtotime(date('Y-m-01', strtotime('+1 month', time())) . ' 00:00:00');
				$where = "transaction_date >= $start_date AND transaction_date < $end_date";
			}
			else if($income_date_option == 6) {
				$start_date = strtotime(date('Y-m-01', strtotime('-1 month', time())) . ' 00:00:00');
				$end_date = strtotime(date('Y-m-01') . ' 00:00:00');
				$where = "transaction_date >= $start_date AND transaction_date < $end_date";
			}
			else if($income_date_option == 7) {
				if($this->session->userdata('report_start_date') != '' && $this->session->userdata('report_end_date') != '') {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('report_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('report_end_date'))));
					$where = "transaction_date >= $start_date AND transaction_date <= $end_date";
				}
			}
			
			if($where != '') {
				$where = "($where AND transaction_type = 1 AND to_member_username = '" .  $this->session->userdata('report_username') . "' AND transaction_type = 1) UNION ALL SELECT  transaction_date, transaction_type, debit_amount, credit_amount, to_coin_balance, remark, 2 AS tbl_type FROM sw_member_topup WHERE ($where AND to_member_username = '" .  $this->session->userdata('report_username') . "' AND transaction_type = 2) UNION ALL SELECT  transaction_date, transaction_type, debit_amount, credit_amount, to_coin_balance, remark, 3 AS tbl_type FROM sw_member_converts WHERE ($where AND from_member_username = '" .  $this->session->userdata('report_username') . "' AND transaction_type = 1) UNION ALL SELECT  transaction_date, transaction_type, debit_amount, 0 AS credit_amount, to_coin_balance, remark, 4 AS tbl_type FROM sw_member_split WHERE ($where AND to_member_username = '" .  $this->session->userdata('report_username') . "' AND transaction_type = 1)";
			}
			else {
				$where = "(transaction_type = 1 AND to_member_username = '" .  $this->session->userdata('report_username') . "' AND transaction_type = 1) UNION ALL SELECT  transaction_date, transaction_type, debit_amount, credit_amount, to_coin_balance, remark, 2 AS tbl_type FROM sw_member_topup WHERE (to_member_username = '" .  $this->session->userdata('report_username') . "' AND transaction_type = 2) UNION ALL SELECT  transaction_date, transaction_type, debit_amount, credit_amount, to_coin_balance, remark, 3 AS tbl_type FROM sw_member_converts WHERE (from_member_username = '" .  $this->session->userdata('report_username') . "' AND transaction_type = 1) UNION ALL SELECT  transaction_date, transaction_type, debit_amount, 0 AS credit_amount, to_coin_balance, remark, 4 AS tbl_type FROM sw_member_split WHERE (to_member_username = '" .  $this->session->userdata('report_username') . "' AND transaction_type = 1)";
			}
			
			$this->db->where($where);
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				
				switch($aRow['tbl_type']) {
					case 1 : $row[] = $this->lang->line('report_mined'); break;
					case 2 : $row[] = $this->lang->line('report_topup'); break;
					case 3 : $row[] = $this->lang->line('report_convert'); break;
					case 4 : $row[] = $this->lang->line('report_split'); break;
				}
				
				$row[] = ($aRow['tbl_type'] == 3) ? '-' : number_format($aRow['debit_amount'], 4, '.', '');
				$row[] = ($aRow['tbl_type'] == 3) ? number_format($aRow['credit_amount'], 4, '.', '') : '-';
				$row[] = number_format($aRow['to_coin_balance'], 4, '.', '');
				$row[] = (($aRow['remark'] != '') ? $aRow['remark'] : '-');
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function wallet_balance()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN)) {
			$this->lang->load('account_general', get_language());
			
			if($_POST) {
				$this->session->set_userdata(array('report_username' => trim($this->input->post('username', TRUE)), 'report_password' => trim($this->input->post('security_password', TRUE))));
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_wallet_balance_report');
				
				if($this->session->userdata('report_password') == SECURITY_KEY_FOR_VIEW_REPORT) {
					$where = '';
					if($this->session->userdata('report_username') != '') {
						$member_id = 0;
						$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('report_username')));
						if($query->num_rows() > 0) {
							$row = $query->row();
							$member_id = $row->member_id;
						}
						$where = " AND (a.introducer_parent_ref LIKE '%,$member_id,%' OR a.member_id = $member_id)";
					}
					
					$_POST['balance'] = $this->db->query("SELECT SUM(b.v_unit) AS v_unit, SUM(b.v_unit2) AS v_unit2, SUM(b.v_unit3) AS v_unit3, SUM(b.v_unit4) AS v_unit4 FROM sw_members a, sw_member_wallets b WHERE (a.member_id = b.member_id)" . $where)->row_array();
					// $_POST['reinvest_balance'] = $this->db->query("SELECT SUM(b.reinvest_v_unit) AS v_unit, SUM(b.reinvest_v_unit2) AS v_unit2, SUM(b.reinvest_v_unit3) AS v_unit3 FROM sw_members a, sw_member_reinvest b WHERE (a.member_id = b.member_id)" . $where)->row_array();
				}
				
				$this->load->view('report_wallet_balance', $_POST);
			}
			else {
				$this->session->unset_userdata(array('report_username' => '', 'report_password' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_wallet_balance_report');
				
				$this->load->view('report_wallet_balance', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	// public function add_pool_bonus()
	// {
		// save_current_url(base_url() . 'report/add_pool_bonus/');
		
		// if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			// $this->lang->load('account_general', get_language());
			
			// $data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_add_pool_bonus');
			
			// $this->load->view('report_add_pool_bonus', $data);
		// }
		// else {
			// redirect('login', 'refresh');
		// }
	// }
	
	// public function pool_bonus_submit()
	// {
		// if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			// if($_POST) {
				// $this->lang->load('account_general', get_language());
			
				// $_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_report') . ' - ' .$this->lang->line('hf_add_pool_bonus');
			
				// $this->load->model('user_model');
				
				// $rs = $this->user_model->member_add_pool_bonus();
					
				// if($rs) {
					// redirect('report/add_pool_bonus/', 'refresh');
				// }
				// else {
					// $this->load->view('report_add_pool_bonus', $_POST);
				// }
			// }
			// else {
				// redirect('report/add_pool_bonus/', 'refresh');
			// }	
		// }
		// else {
			// redirect('login', 'refresh');
		// }
	// }
}

/* End of file report.php */
/* Location: ./application/controllers/report.php */