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

class Topup 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('topup/request', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function request()
	{
		save_current_url(base_url() . 'topup/request/');
		
		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_topup') . ' - ' .$this->lang->line('hf_account_topup');
			
			$this->load->view('topup_request', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function 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_topup') . ' - ' .$this->lang->line('hf_account_topup');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->member_topup();
					
				if($rs) {
					redirect('topup/request/', 'refresh');
				}
				else {
					$this->load->view('topup_request', $_POST);
				}
			}
			else {
				redirect('topup/request/', 'refresh');
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}

	public function listing()
	{
		save_current_url(base_url() . 'topup/listing/');
		
		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(
						'member_search_to_username' => trim($this->input->post('search_to_username', TRUE)), 
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_topup_date' => trim($this->input->post('search_topup_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE)), 
						'member_search_member_type' => ($this->input->post('search_member_type', TRUE) != '') ? implode(',', $this->input->post('search_member_type', TRUE)) : '',
						'member_search_payment_gateway' => ($this->input->post('search_payment_gateway', TRUE) != '') ? implode(',', $this->input->post('search_payment_gateway', TRUE)) : '',
						'member_search_currency' => ($this->input->post('search_currency', TRUE) != '') ? implode(',', $this->input->post('search_currency', TRUE)) : '',
						'member_search_topup_status' => ($this->input->post('search_topup_status', TRUE) != '') ? implode(',', $this->input->post('search_topup_status', TRUE)) : '',
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_history');
			
				$this->load->view('topup_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_to_username' => '', 'member_search_username' => '', 'member_exclude_downline' => '', 'member_search_topup_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => '', 'member_search_payment_gateway' => '', 'member_search_currency' => '', 'member_search_topup_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_history');
				
				$this->load->view('topup_history', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function 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)
			 */
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.payment_method',
									$this->db->dbprefix('member_topup') . '.currency_id',
									$this->db->dbprefix('member_topup') . '.transaction_amount',
									$this->db->dbprefix('member_topup') . '.debit_amount',
									$this->db->dbprefix('member_topup') . '.currency_rate',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'payment_method',
									'currency_id',
									'transaction_amount',
									'debit_amount',
									'currency_rate',
									'to_member_username',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_payment_gateway') != '') {
				$where = $this->db->dbprefix('member_topup') . ".payment_method IN(" .  $this->session->userdata('member_search_payment_gateway') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_currency') != '') {
				$where = $this->db->dbprefix('member_topup') . ".currency_id IN(" .  $this->session->userdata('member_search_currency') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_status') != '') {
				$where = $this->db->dbprefix('member_topup') . ".status IN(" .  $this->session->userdata('member_search_topup_status') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".transaction_type = 1");
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method NOT IN (0,3,4,5,7)");
			
			$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], $sSearch);
						// $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);
			if($this->session->userdata('member_search_topup_date') != '') {
				$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[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				
				switch($aRow['payment_method']) {
					case 19: $row[] = $this->lang->line('payment_gateway_15'); break;
					case 18: $row[] = $this->lang->line('payment_gateway_14'); break;
					case 17: $row[] = $this->lang->line('payment_gateway_13'); break;
					case 16: $row[] = $this->lang->line('payment_gateway_12'); break;
					case 15: $row[] = $this->lang->line('payment_gateway_11'); break;
					case 14: $row[] = $this->lang->line('payment_gateway_10'); break;
					case 13: $row[] = $this->lang->line('payment_gateway_9'); break;
					case 12: $row[] = $this->lang->line('payment_gateway_8'); break;
					case 11: $row[] = $this->lang->line('payment_gateway_7'); break;
					case 10: $row[] = $this->lang->line('payment_gateway_6'); break;
					case 9: $row[] = $this->lang->line('payment_gateway_5'); break;
					case 8: $row[] = $this->lang->line('payment_gateway_4'); break;
					case 6: $row[] = $this->lang->line('payment_gateway_3'); break;
					case 2: $row[] = $this->lang->line('payment_gateway_2'); break;
					default: $row[] = $this->lang->line('payment_gateway_1'); break;
				}
				
				switch($aRow['currency_id']) {
					case 11: $row[] = $this->lang->line('currency_idr_without_symbol'); break;
					case 9: $row[] = $this->lang->line('currency_vnd_without_symbol'); break;
					case 6: $row[] = $this->lang->line('currency_thb_without_symbol'); break;
					case 3: $row[] = $this->lang->line('currency_myr_without_symbol'); break;
					default: $row[] = $this->lang->line('currency_cny_without_symbol'); break;
				}
				
				$row[] = number_format($aRow['transaction_amount'], 2, '.', '');
				$row[] = number_format($aRow['debit_amount'], 2, '.', '');
				$row[] = $aRow['currency_rate'];
				$row[] = $aRow['to_member_username'];
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('topup_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('topup_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function topup_export()
	{
		set_time_limit(0);
		
		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());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('topup_transaction_date'),
								$this->lang->line('topup_payment_gateway'),
								$this->lang->line('topup_topup_currency'),
								$this->lang->line('topup_actual_amount'),
								$this->lang->line('topup_topup_amount'),
								$this->lang->line('topup_currency_rate'),
								$this->lang->line('topup_to'),
								$this->lang->line('topup_status')
							);
			
			//Get Data
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.payment_method',
									$this->db->dbprefix('member_topup') . '.currency_id',
									$this->db->dbprefix('member_topup') . '.transaction_amount',
									$this->db->dbprefix('member_topup') . '.debit_amount',
									$this->db->dbprefix('member_topup') . '.currency_rate',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'payment_method',
									'currency_id',
									'transaction_amount',
									'debit_amount',
									'currency_rate',
									'to_member_username',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_payment_gateway') != '') {
				$where = $this->db->dbprefix('member_topup') . ".payment_method IN(" .  $this->session->userdata('member_search_payment_gateway') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_currency') != '') {
				$where = $this->db->dbprefix('member_topup') . ".currency_id IN(" .  $this->session->userdata('member_search_currency') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_status') != '') {
				$where = $this->db->dbprefix('member_topup') . ".status IN(" .  $this->session->userdata('member_search_topup_status') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".transaction_type = 1");
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method NOT IN (0,3,4,5,7)");
			$this->db->order_by($this->db->dbprefix('member_topup') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_topup_date') != '') {
				$this->db->join($jTable, $jColumns);
			}
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$payment_method = '';
				switch($aRow['payment_method']) {
					case 19: $payment_method = $this->lang->line('payment_gateway_15'); break;
					case 18: $payment_method = $this->lang->line('payment_gateway_14'); break;
					case 17: $payment_method = $this->lang->line('payment_gateway_13'); break;
					case 16: $payment_method = $this->lang->line('payment_gateway_12'); break;
					case 15: $payment_method = $this->lang->line('payment_gateway_11'); break;
					case 14: $payment_method = $this->lang->line('payment_gateway_10'); break;
					case 13: $payment_method = $this->lang->line('payment_gateway_9'); break;
					case 12: $payment_method = $this->lang->line('payment_gateway_8'); break;
					case 11: $payment_method = $this->lang->line('payment_gateway_7'); break;
					case 10: $payment_method = $this->lang->line('payment_gateway_6'); break;
					case 9: $payment_method = $this->lang->line('payment_gateway_5'); break;
					case 8: $payment_method = $this->lang->line('payment_gateway_4'); break;
					case 6: $payment_method = $this->lang->line('payment_gateway_3'); break;
					case 2: $payment_method = $this->lang->line('payment_gateway_2'); break;
					default: $payment_method = $this->lang->line('payment_gateway_1'); break;
				}
				
				$currency = '';
				switch($aRow['currency_id']) {
					case 11: $currency = $this->lang->line('currency_idr_without_symbol'); break;
					case 9: $currency = $this->lang->line('currency_vnd_without_symbol'); break;
					case 6: $currency = $this->lang->line('currency_thb_without_symbol'); break;
					case 3: $currency = $this->lang->line('currency_myr_without_symbol'); break;
					default: $currency = $this->lang->line('currency_cny_without_symbol'); break;
				}
				
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('topup_pending'); break;
					case 1: $status = $this->lang->line('topup_confirmed'); break;
					case 2: $status = $this->lang->line('topup_cancelled'); break;
					default: $status = $this->lang->line('topup_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									$payment_method,
									$currency,
									number_format($aRow['transaction_amount'], 2, '.', ''),
									number_format($aRow['debit_amount'], 2, '.', ''),
									$aRow['currency_rate'],
									$aRow['to_member_username'],
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Topup List');
			$xls->addArray($data);
			$xls->generateXML('member_topup_online_list');
		}
	}
	
	public function offline_listing()
	{
		save_current_url(base_url() . 'topup/offline_listing/');
		
		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(
						'member_search_to_username' => trim($this->input->post('search_to_username', TRUE)), 
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_topup_date' => trim($this->input->post('search_topup_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE)), 
						'member_search_member_type' => ($this->input->post('search_member_type', TRUE) != '') ? implode(',', $this->input->post('search_member_type', TRUE)) : '', 
						'member_search_wallet_type' => ($this->input->post('search_wallet_type', TRUE) != '') ? implode(',', $this->input->post('search_wallet_type', TRUE)) : '', 
						'member_search_remark' => trim($this->input->post('search_remark', TRUE)),
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_offline_history');
			
				$this->load->view('topup_offline_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_to_username' => '', 'member_search_username' => '', 'member_exclude_downline' => '', 'member_search_topup_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => '', 'member_search_wallet_type' => '', 'member_search_remark' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_offline_history');
				
				$this->load->view('topup_offline_history', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_offline_list()
    {
		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)
			 */
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.transaction_type',
									$this->db->dbprefix('member_topup') . '.debit_amount',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.remark',
									$this->db->dbprefix('member_topup') . '.remark_2',
									$this->db->dbprefix('member_topup') . '.status',
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_type',
									'debit_amount',
									'to_member_username',
									'remark',
									'remark_2',
									'status',
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_wallet_type') != '') {
				$where = $this->db->dbprefix('member_topup') . ".transaction_type IN(" .  $this->session->userdata('member_search_wallet_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_remark') != '') {
				$where = $this->db->dbprefix('member_topup') . ".remark = '" . $this->session->userdata('member_search_remark') . "'";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method = 0");
			
			$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], $sSearch);
						// $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);
			if($this->session->userdata('member_search_topup_date') != '') {
				$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[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				
				switch($aRow['transaction_type']) {
					case 4: $row[] = $this->lang->line('wallet_4'); break;
					case 3: $row[] = $this->lang->line('wallet_3'); break;
					case 2: $row[] = $this->lang->line('wallet_2'); break;
					default: $row[] = $this->lang->line('wallet_1'); break;
				}
				
				$row[] = number_format($aRow['debit_amount'], 2, '.', '');
				$row[] = $aRow['to_member_username'];
				$row[] = ($aRow['remark'] != '') ? $aRow['remark'] : '-';
				$row[] = ($aRow['remark_2'] != '') ? $aRow['remark_2'] : '-';
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('topup_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('topup_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function topup_offline_export()
	{
		set_time_limit(0);
		
		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());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('topup_transaction_date'),
								$this->lang->line('topup_topup_type'),
								$this->lang->line('topup_topup_amount'),
								$this->lang->line('topup_to'),
								$this->lang->line('topup_remark'),
								$this->lang->line('topup_remark') . ' 2',
								$this->lang->line('topup_status')
							);
			
			//Get Data
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.transaction_type',
									$this->db->dbprefix('member_topup') . '.debit_amount',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.remark',
									$this->db->dbprefix('member_topup') . '.remark_2',
									$this->db->dbprefix('member_topup') . '.status',
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_type',
									'debit_amount',
									'to_member_username',
									'remark',
									'remark_2',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_wallet_type') != '') {
				$where = $this->db->dbprefix('member_topup') . ".transaction_type IN(" .  $this->session->userdata('member_search_wallet_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_remark') != '') {
				$where = $this->db->dbprefix('member_topup') . ".remark = '" . $this->session->userdata('member_search_remark') . "'";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method = 0");
			$this->db->order_by($this->db->dbprefix('member_topup') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_topup_date') != '') {
				$this->db->join($jTable, $jColumns);
			}
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$transaction_type = '';
				switch($aRow['transaction_type']) {
					case 4: $transaction_type = $this->lang->line('wallet_4'); break;
					case 3: $transaction_type = $this->lang->line('wallet_3'); break;
					case 2: $transaction_type = $this->lang->line('wallet_2'); break;
					default: $transaction_type = $this->lang->line('wallet_1'); break;
				}
				
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('topup_pending'); break;
					case 1: $status = $this->lang->line('topup_confirmed'); break;
					case 2: $status = $this->lang->line('topup_cancelled'); break;
					default: $status = $this->lang->line('topup_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									$transaction_type,
									number_format($aRow['debit_amount'], 2, '.', ''),
									$aRow['to_member_username'],
									(($aRow['remark'] != '') ? $aRow['remark'] : '-'),
									(($aRow['remark_2'] != '') ? $aRow['remark_2'] : '-'),
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Topup Offline List');
			$xls->addArray($data);
			$xls->generateXML('member_topup_offline_list');
		}
	}
	
	public function usdt_listing()
	{
		save_current_url(base_url() . 'topup/usdt_listing/');
		
		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(
						'member_search_to_username' => trim($this->input->post('search_to_username', TRUE)), 
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_topup_date' => trim($this->input->post('search_topup_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE)), 
						'member_search_member_type' => ($this->input->post('search_member_type', TRUE) != '') ? implode(',', $this->input->post('search_member_type', TRUE)) : '', 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_usdt_history');
			
				$this->load->view('topup_usdt_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_to_username' => '', 'member_search_username' => '', 'member_exclude_downline' => '', 'member_search_topup_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_usdt_history');
				
				$this->load->view('topup_usdt_history', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_usdt_list()
    {
		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)
			 */
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.transaction_amount',
									$this->db->dbprefix('member_topup') . '.debit_amount',
									$this->db->dbprefix('member_topup') . '.currency_rate',
									$this->db->dbprefix('member_topup') . '.debit_amount_2',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_amount',
									'debit_amount',
									'currency_rate',
									'debit_amount_2',
									'to_member_username',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".transaction_type = 1");
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method = 4");
			
			$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], $sSearch);
						// $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);
			if($this->session->userdata('member_search_topup_date') != '') {
				$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[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = number_format($aRow['transaction_amount'], 2, '.', '');
				$row[] = number_format($aRow['debit_amount'], 2, '.', '');
				$row[] = $aRow['currency_rate'];
				$row[] = number_format($aRow['debit_amount_2'], 2, '.', '');
				$row[] = $aRow['to_member_username'];
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('topup_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('topup_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
				}
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function topup_usdt_export()
	{
		set_time_limit(0);
		
		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());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('topup_transaction_date'),
								$this->lang->line('topup_actual_amount'),
								$this->lang->line('topup_topup_amount'),
								$this->lang->line('topup_currency_rate'),
								$this->lang->line('wallet_4'),
								$this->lang->line('topup_to'),
								$this->lang->line('topup_status')
							);
			
			//Get Data
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.transaction_amount',
									$this->db->dbprefix('member_topup') . '.debit_amount',
									$this->db->dbprefix('member_topup') . '.currency_rate',
									$this->db->dbprefix('member_topup') . '.debit_amount_2',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_amount',
									'debit_amount',
									'currency_rate',
									'debit_amount_2',
									'to_member_username',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".transaction_type = 1");
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method = 4");
			$this->db->order_by($this->db->dbprefix('member_topup') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_topup_date') != '') {
				$this->db->join($jTable, $jColumns);
			}
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('topup_pending'); break;
					case 1: $status = $this->lang->line('topup_confirmed'); break;
					case 2: $status = $this->lang->line('topup_cancelled'); break;
					default: $status = $this->lang->line('topup_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									number_format($aRow['transaction_amount'], 2, '.', ''),
									number_format($aRow['debit_amount'], 2, '.', ''),
									$aRow['currency_rate'],
									number_format($aRow['debit_amount_2'], 2, '.', ''),
									$aRow['to_member_username'],
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Topup USDT List');
			$xls->addArray($data);
			$xls->generateXML('member_topup_usdt_list');
		}
	}
	
	public function sands_listing()
	{
		save_current_url(base_url() . 'topup/sands_listing/');
		
		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(
						'member_search_to_username' => trim($this->input->post('search_to_username', TRUE)), 
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_topup_date' => trim($this->input->post('search_topup_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE)), 
						'member_search_member_type' => ($this->input->post('search_member_type', TRUE) != '') ? implode(',', $this->input->post('search_member_type', TRUE)) : '', 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_sands_history');
			
				$this->load->view('topup_sands_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_to_username' => '', 'member_search_username' => '', 'member_exclude_downline' => '', 'member_search_topup_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_sands_history');
				
				$this->load->view('topup_sands_history', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_sands_list()
    {
		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)
			 */
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.transaction_amount',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_amount',
									'to_member_username',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".transaction_type = 1");
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method = 5");
			
			$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], $sSearch);
						// $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);
			if($this->session->userdata('member_search_topup_date') != '') {
				$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[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = number_format($aRow['transaction_amount'], 2, '.', '');
				$row[] = $aRow['to_member_username'];
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('topup_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('topup_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
				}
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function topup_sands_export()
	{
		set_time_limit(0);
		
		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());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('topup_transaction_date'),
								$this->lang->line('topup_topup_amount'),
								$this->lang->line('topup_to'),
								$this->lang->line('topup_status')
							);
			
			//Get Data
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.transaction_amount',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_amount',
									'to_member_username',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".transaction_type = 1");
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method = 5");
			$this->db->order_by($this->db->dbprefix('member_topup') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_topup_date') != '') {
				$this->db->join($jTable, $jColumns);
			}
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('topup_pending'); break;
					case 1: $status = $this->lang->line('topup_confirmed'); break;
					case 2: $status = $this->lang->line('topup_cancelled'); break;
					default: $status = $this->lang->line('topup_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									number_format($aRow['transaction_amount'], 2, '.', ''),
									$aRow['to_member_username'],
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Topup Sands List');
			$xls->addArray($data);
			$xls->generateXML('member_topup_sands_list');
		}
	}
	
	public function ping_listing()
	{
		save_current_url(base_url() . 'topup/ping_listing/');
		
		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(
						'member_search_to_username' => trim($this->input->post('search_to_username', TRUE)), 
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_topup_date' => trim($this->input->post('search_topup_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE)), 
						'member_search_member_type' => ($this->input->post('search_member_type', TRUE) != '') ? implode(',', $this->input->post('search_member_type', TRUE)) : '', 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_ping_history');
			
				$this->load->view('topup_ping_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_to_username' => '', 'member_search_username' => '', 'member_exclude_downline' => '', 'member_search_topup_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_topup') . ' - ' .$this->lang->line('hf_topup_ping_history');
				
				$this->load->view('topup_ping_history', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_ping_list()
    {
		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)
			 */
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.transaction_amount',
									$this->db->dbprefix('member_topup') . '.debit_amount',
									$this->db->dbprefix('member_topup') . '.currency_rate',
									$this->db->dbprefix('member_topup') . '.debit_amount_2',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_amount',
									'debit_amount',
									'currency_rate',
									'debit_amount_2',
									'to_member_username',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".transaction_type = 1");
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method = 7");
			
			$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], $sSearch);
						// $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);
			if($this->session->userdata('member_search_topup_date') != '') {
				$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[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = number_format($aRow['transaction_amount'], 2, '.', '');
				$row[] = number_format($aRow['debit_amount'], 2, '.', '');
				$row[] = $aRow['currency_rate'];
				$row[] = number_format($aRow['debit_amount_2'], 2, '.', '');
				$row[] = $aRow['to_member_username'];
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('topup_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('topup_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('topup_pending') . '</span>'; break;
				}
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function topup_ping_export()
	{
		set_time_limit(0);
		
		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());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('topup_transaction_date'),
								$this->lang->line('topup_actual_amount'),
								$this->lang->line('topup_topup_amount'),
								$this->lang->line('topup_currency_rate'),
								$this->lang->line('wallet_4'),
								$this->lang->line('topup_to'),
								$this->lang->line('topup_status')
							);
			
			//Get Data
			if($this->session->userdata('member_search_topup_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_topup') . '.transaction_date',
									$this->db->dbprefix('member_topup') . '.transaction_amount',
									$this->db->dbprefix('member_topup') . '.debit_amount',
									$this->db->dbprefix('member_topup') . '.currency_rate',
									$this->db->dbprefix('member_topup') . '.debit_amount_2',
									$this->db->dbprefix('member_topup') . '.to_member_username',
									$this->db->dbprefix('member_topup') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_topup') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_amount',
									'debit_amount',
									'currency_rate',
									'debit_amount_2',
									'to_member_username',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_topup';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_to_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" .  $this->session->userdata('member_search_to_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_date') != '') {
				if($this->session->userdata('member_search_topup_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_topup') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('member_topup') . ".transaction_type = 1");
			$this->db->where($this->db->dbprefix('member_topup') . ".payment_method = 7");
			$this->db->order_by($this->db->dbprefix('member_topup') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_topup_date') != '') {
				$this->db->join($jTable, $jColumns);
			}
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('topup_pending'); break;
					case 1: $status = $this->lang->line('topup_confirmed'); break;
					case 2: $status = $this->lang->line('topup_cancelled'); break;
					default: $status = $this->lang->line('topup_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									number_format($aRow['transaction_amount'], 2, '.', ''),
									number_format($aRow['debit_amount'], 2, '.', ''),
									$aRow['currency_rate'],
									number_format($aRow['debit_amount_2'], 2, '.', ''),
									$aRow['to_member_username'],
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Topup PING List');
			$xls->addArray($data);
			$xls->generateXML('member_topup_ping_list');
		}
	}
}

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