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

class Withdrawal 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('withdrawal/listing', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}

	public function listing()
	{
		save_current_url(base_url() . 'withdrawal/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_username' => trim($this->input->post('search_username', TRUE)), 
						'member_search_downline' => trim($this->input->post('search_downline', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_withdrawal_date' => trim($this->input->post('search_withdrawal_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_bank_country' => trim($this->input->post('search_bank_country', TRUE)), 
						'member_search_bank_account_no' => trim($this->input->post('search_bank_account_no', TRUE)), 
						'member_search_withdrawal_status' => ($this->input->post('search_withdrawal_status', TRUE) != '') ? implode(',', $this->input->post('search_withdrawal_status', TRUE)) : ''
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_withdrawal') . ' - ' .$this->lang->line('hf_withdraw_history');
			
				$this->load->view('withdraw_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_downline' => '', 'member_exclude_downline' => '', 'member_search_withdrawal_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_bank_country' => '', 'member_search_bank_account_no' => '', 'member_search_withdrawal_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_withdrawal') . ' - ' .$this->lang->line('hf_withdraw_history');
			
				$this->load->view('withdraw_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)
			 */
			$aColumns = array(
								$this->db->dbprefix('member_withdraw') . '.member_transaction_id',
								$this->db->dbprefix('member_withdraw') . '.transaction_date',
								$this->db->dbprefix('member_withdraw') . '.withdrawal_amount',
								$this->db->dbprefix('member_withdraw') . '.credit_amount',
								$this->db->dbprefix('member_withdraw') . '.charges',
								$this->db->dbprefix('member_withdraw') . '.from_member_username',
								$this->db->dbprefix('member_withdraw') . '.to_bank_name',
								$this->db->dbprefix('member_withdraw') . '.status',
								$this->db->dbprefix('member_withdraw') . '.to_bank_account_no',
								$this->db->dbprefix('member_withdraw') . '.to_account_holder_name',
								$this->db->dbprefix('member_withdraw') . '.to_bank_address'
							);
			
			$jColumns = $this->db->dbprefix('member_withdraw') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_withdraw';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_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_withdraw') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_withdraw') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_bank_country') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".to_bank_country = '" . strtolower($this->session->userdata('member_search_bank_country')) . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_bank_account_no') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".to_bank_account_no = '" . $this->session->userdata('member_search_bank_account_no') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_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->where($this->db->dbprefix('member_withdraw') . '.transaction_type', 1);
			
			$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')
					{
						if($aColumns[intval($this->db->escape_str($iSortCol))] == '(credit_amount - charges) AS actual_amount') {
							$this->db->order_by('actual_amount', $this->db->escape_str($sSortDir));
						}
						else  {
							$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();
				
				if($aRow['status'] == 0 || $aRow['status'] == 4) {
					$row[] = '<input type="checkbox" name="chkbox[]" value="' . $aRow['member_transaction_id'] . '" onchange="checkOrUncheck(this);" />';
				}
				else {
					$row[] = '';
				}
				
				$row[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = number_format($aRow['withdrawal_amount'], 2, '.', '');
				$row[] = number_format($aRow['credit_amount'], 2, '.', '');
				$row[] = number_format($aRow['charges'], 2, '.', '');
				$row[] = $aRow['from_member_username'];
				
				$bank_info = $this->lang->line('registration_bank_name') . ': ' . $aRow['to_bank_name'] . '<br />';
				$bank_info .= $this->lang->line('registration_bank_account_no') . ': ' . $aRow['to_bank_account_no'] . '<br />';
				$bank_info .= $this->lang->line('registration_account_holder_name') . ': ' . $aRow['to_account_holder_name'] . '<br />';
				$bank_info .= $this->lang->line('registration_bank_address') . ': ' . $aRow['to_bank_address'] . '<br />';
				
				$row[] = $bank_info;
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('withdrawal_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('withdrawal_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
				}
				
				if($aRow['status'] == 0) {
					$row[] = '<select id="update_status" name="update_status" class="small m-wrap" onchange="update_withdrawal_status(' . $aRow['member_transaction_id'] . ', this.value)">
								<option value="0">' . $this->lang->line('withdrawal_pending_withdrawal') . '</option>
								<option value="1">' . $this->lang->line('withdrawal_approve_withdrawal') . '</option>
								<option value="2">' . $this->lang->line('withdrawal_cancel_withdrawal') . '</option>
							</select>';
				}
				else {
					$row[] = '';
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function withdrawal_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('withdraw_request_date'),
								$this->lang->line('withdraw_actual_amount'),
								$this->lang->line('withdraw_withdrawal_rate'),
								$this->lang->line('withdraw_charges'),
								$this->lang->line('withdraw_from_member'),
								$this->lang->line('registration_bank_name'),
								$this->lang->line('registration_bank_account_no'),
								$this->lang->line('registration_account_holder_name'),
								$this->lang->line('registration_bank_address'),
								$this->lang->line('withdraw_status')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('member_withdraw') . '.member_transaction_id',
								$this->db->dbprefix('member_withdraw') . '.transaction_date',
								$this->db->dbprefix('member_withdraw') . '.withdrawal_amount',
								$this->db->dbprefix('member_withdraw') . '.credit_amount',
								$this->db->dbprefix('member_withdraw') . '.charges',
								$this->db->dbprefix('member_withdraw') . '.from_member_username',
								$this->db->dbprefix('member_withdraw') . '.to_bank_name',
								$this->db->dbprefix('member_withdraw') . '.status',
								$this->db->dbprefix('member_withdraw') . '.to_bank_account_no',
								$this->db->dbprefix('member_withdraw') . '.to_account_holder_name',
								$this->db->dbprefix('member_withdraw') . '.to_bank_address',
							);				
			
			$jColumns = $this->db->dbprefix('member_withdraw') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_withdraw';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_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_withdraw') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_withdraw') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_bank_country') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".to_bank_country = '" . strtolower($this->session->userdata('member_search_bank_country')) . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_bank_account_no') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".to_bank_account_no = '" . $this->session->userdata('member_search_bank_account_no') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_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->where($this->db->dbprefix('member_withdraw') . '.transaction_type', 1);
			
			$this->db->order_by($this->db->dbprefix('member_withdraw') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$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('withdrawal_pending'); break;
					case 1: $status = $this->lang->line('withdrawal_confirmed'); break;
					case 2: $status = $this->lang->line('withdrawal_cancelled'); break;
					default: $status = $this->lang->line('withdrawal_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									number_format($aRow['withdrawal_amount'], 2, '.', ''),
									number_format($aRow['credit_amount'], 2, '.', ''),
									number_format($aRow['charges'], 1, '.', ''),
									$aRow['from_member_username'],
									$aRow['to_bank_name'],
									$aRow['to_bank_account_no'],
									$aRow['to_account_holder_name'],
									$aRow['to_bank_address'],
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Withdrawal List');
			$xls->addArray($data);
			$xls->generateXML('member_withdrawal_list');
		}
	}
	
	public function update_withdrawal($transaction_id = NULL, $status = NULL)
	{
		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());
			
			$this->load->model('user_model');
			
			$rs = $this->user_model->update_withdrawal_status($transaction_id, $status);
				
			redirect('withdrawal/listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function listing_update()
	{
		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());
			
			$this->load->model('user_model');
			
			#Recursive deletion
			if(isset($_POST['chkbox'])) {
				for($i=0;$i<sizeof($_POST['chkbox']);$i++) {
					if($_POST['chkbox'][$i] != 'on') {
						$this->user_model->update_withdrawal_status(trim($_POST['chkbox'][$i]), trim($_POST['update_status']));
					}
				}
			}
			
			redirect('withdrawal/listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function coin_listing()
	{
		save_current_url(base_url() . 'withdrawal/coin_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_username' => trim($this->input->post('search_username', TRUE)), 
						'member_search_downline' => trim($this->input->post('search_downline', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_withdrawal_date' => trim($this->input->post('search_withdrawal_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_withdrawal_status' => ($this->input->post('search_withdrawal_status', TRUE) != '') ? implode(',', $this->input->post('search_withdrawal_status', TRUE)) : ''
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_withdrawal') . ' - ' .$this->lang->line('hf_withdraw_coin_history');
			
				$this->load->view('withdraw_coin_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_downline' => '', 'member_exclude_downline' => '', 'member_search_withdrawal_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_withdrawal_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_withdrawal') . ' - ' .$this->lang->line('hf_withdraw_coin_history');
			
				$this->load->view('withdraw_coin_history', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getCoinList()
    {
		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_withdraw') . '.member_transaction_id',
								$this->db->dbprefix('member_withdraw') . '.transaction_date',
								$this->db->dbprefix('member_withdraw') . '.withdrawal_amount',
								$this->db->dbprefix('member_withdraw') . '.credit_amount',
								$this->db->dbprefix('member_withdraw') . '.charges',
								$this->db->dbprefix('member_withdraw') . '.from_member_username',
								$this->db->dbprefix('members') . '.wallet_address',
								$this->db->dbprefix('member_withdraw') . '.status'
							);
			
			$jColumns = $this->db->dbprefix('member_withdraw') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_withdraw';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_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_withdraw') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_withdraw') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_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->where($this->db->dbprefix('member_withdraw') . '.transaction_type', 2);
			
			$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')
					{
						if($aColumns[intval($this->db->escape_str($iSortCol))] == '(credit_amount - charges) AS actual_amount') {
							$this->db->order_by('actual_amount', $this->db->escape_str($sSortDir));
						}
						else  {
							$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();
				
				if($aRow['status'] == 0 || $aRow['status'] == 4) {
					$row[] = '<input type="checkbox" name="chkbox[]" value="' . $aRow['member_transaction_id'] . '" onchange="checkOrUncheck(this);" />';
				}
				else {
					$row[] = '';
				}
				
				$row[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = number_format($aRow['withdrawal_amount'], 2, '.', '');
				$row[] = number_format($aRow['credit_amount'], 2, '.', '');
				$row[] = number_format($aRow['charges'], 2, '.', '');
				$row[] = $aRow['from_member_username'];
				$row[] = $aRow['wallet_address'];
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('withdrawal_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('withdrawal_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
				}
				
				if($aRow['status'] == 0) {
					$row[] = '<select id="update_status" name="update_status" class="small m-wrap" onchange="update_withdrawal_coin_status(' . $aRow['member_transaction_id'] . ', this.value)">
								<option value="0">' . $this->lang->line('withdrawal_pending_withdrawal') . '</option>
								<option value="1">' . $this->lang->line('withdrawal_approve_withdrawal') . '</option>
								<option value="2">' . $this->lang->line('withdrawal_cancel_withdrawal') . '</option>
							</select>';
				}
				else {
					$row[] = '';
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function withdrawal_coin_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('withdraw_request_date'),
								$this->lang->line('withdraw_actual_amount'),
								$this->lang->line('withdraw_withdrawal_rate'),
								$this->lang->line('withdraw_charges'),
								$this->lang->line('withdraw_from_member'),
								$this->lang->line('registration_coin_wallet_address'),
								$this->lang->line('withdraw_status')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('member_withdraw') . '.member_transaction_id',
								$this->db->dbprefix('member_withdraw') . '.transaction_date',
								$this->db->dbprefix('member_withdraw') . '.withdrawal_amount',
								$this->db->dbprefix('member_withdraw') . '.credit_amount',
								$this->db->dbprefix('member_withdraw') . '.charges',
								$this->db->dbprefix('member_withdraw') . '.from_member_username',
								$this->db->dbprefix('members') . '.wallet_address',
								$this->db->dbprefix('member_withdraw') . '.status'
							);				
			
			$jColumns = $this->db->dbprefix('member_withdraw') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_withdraw';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_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_withdraw') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_withdraw') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_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->where($this->db->dbprefix('member_withdraw') . '.transaction_type', 2);
			
			$this->db->order_by($this->db->dbprefix('member_withdraw') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$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('withdrawal_pending'); break;
					case 1: $status = $this->lang->line('withdrawal_confirmed'); break;
					case 2: $status = $this->lang->line('withdrawal_cancelled'); break;
					default: $status = $this->lang->line('withdrawal_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									number_format($aRow['withdrawal_amount'], 2, '.', ''),
									number_format($aRow['credit_amount'], 2, '.', ''),
									number_format($aRow['charges'], 1, '.', ''),
									$aRow['from_member_username'],
									$aRow['wallet_address'],
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Withdrawal Coin List');
			$xls->addArray($data);
			$xls->generateXML('member_withdrawal_coin_list');
		}
	}
	
	public function update_withdrawal_coin($transaction_id = NULL, $status = NULL)
	{
		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());
			
			$this->load->model('user_model');
			
			$rs = $this->user_model->update_withdrawal_status($transaction_id, $status);
				
			redirect('withdrawal/coin_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function coin_listing_update()
	{
		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());
			
			$this->load->model('user_model');
			
			#Recursive deletion
			if(isset($_POST['chkbox'])) {
				for($i=0;$i<sizeof($_POST['chkbox']);$i++) {
					if($_POST['chkbox'][$i] != 'on') {
						$this->user_model->update_withdrawal_status(trim($_POST['chkbox'][$i]), trim($_POST['update_status']));
					}
				}
			}
			
			redirect('withdrawal/coin_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function chip_listing()
	{
		save_current_url(base_url() . 'withdrawal/chip_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_username' => trim($this->input->post('search_username', TRUE)), 
						'member_search_downline' => trim($this->input->post('search_downline', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_withdrawal_date' => trim($this->input->post('search_withdrawal_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_withdrawal_status' => ($this->input->post('search_withdrawal_status', TRUE) != '') ? implode(',', $this->input->post('search_withdrawal_status', TRUE)) : ''
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_withdrawal') . ' - ' .$this->lang->line('hf_withdraw_chip_history');
			
				$this->load->view('withdraw_chip_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_downline' => '', 'member_exclude_downline' => '', 'member_search_withdrawal_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_withdrawal_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_withdrawal') . ' - ' .$this->lang->line('hf_withdraw_chip_history');
			
				$this->load->view('withdraw_chip_history', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getChipList()
    {
		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_withdraw') . '.member_transaction_id',
								$this->db->dbprefix('member_withdraw') . '.transaction_date',
								$this->db->dbprefix('member_withdraw') . '.withdrawal_amount',
								$this->db->dbprefix('member_withdraw') . '.credit_amount',
								$this->db->dbprefix('member_withdraw') . '.charges',
								$this->db->dbprefix('member_withdraw') . '.from_member_username',
								$this->db->dbprefix('member_withdraw') . '.status'
							);
			
			$jColumns = $this->db->dbprefix('member_withdraw') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_withdraw';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_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_withdraw') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_withdraw') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_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->where($this->db->dbprefix('member_withdraw') . '.transaction_type', 3);
			
			$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')
					{
						if($aColumns[intval($this->db->escape_str($iSortCol))] == '(credit_amount - charges) AS actual_amount') {
							$this->db->order_by('actual_amount', $this->db->escape_str($sSortDir));
						}
						else  {
							$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();
				
				if($aRow['status'] == 0 || $aRow['status'] == 4) {
					$row[] = '<input type="checkbox" name="chkbox[]" value="' . $aRow['member_transaction_id'] . '" onchange="checkOrUncheck(this);" />';
				}
				else {
					$row[] = '';
				}
				
				$row[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = number_format($aRow['withdrawal_amount'], 2, '.', '');
				$row[] = number_format($aRow['credit_amount'], 2, '.', '');
				$row[] = number_format($aRow['charges'], 2, '.', '');
				$row[] = $aRow['from_member_username'];
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('withdrawal_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('withdrawal_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
				}
				
				if($aRow['status'] == 0) {
					$row[] = '<select id="update_status" name="update_status" class="small m-wrap" onchange="update_withdrawal_chip_status(' . $aRow['member_transaction_id'] . ', this.value)">
								<option value="0">' . $this->lang->line('withdrawal_pending_withdrawal') . '</option>
								<option value="1">' . $this->lang->line('withdrawal_approve_withdrawal') . '</option>
								<option value="2">' . $this->lang->line('withdrawal_cancel_withdrawal') . '</option>
							</select>';
				}
				else {
					$row[] = '';
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function withdrawal_chip_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('withdraw_request_date'),
								$this->lang->line('withdraw_actual_amount'),
								$this->lang->line('withdraw_withdrawal_rate'),
								$this->lang->line('withdraw_charges'),
								$this->lang->line('withdraw_from_member'),
								$this->lang->line('withdraw_status')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('member_withdraw') . '.member_transaction_id',
								$this->db->dbprefix('member_withdraw') . '.transaction_date',
								$this->db->dbprefix('member_withdraw') . '.withdrawal_amount',
								$this->db->dbprefix('member_withdraw') . '.credit_amount',
								$this->db->dbprefix('member_withdraw') . '.charges',
								$this->db->dbprefix('member_withdraw') . '.from_member_username',
								$this->db->dbprefix('member_withdraw') . '.status'
							);				
			
			$jColumns = $this->db->dbprefix('member_withdraw') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_withdraw';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_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_withdraw') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_withdraw') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_withdraw') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_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->where($this->db->dbprefix('member_withdraw') . '.transaction_type', 3);
			
			$this->db->order_by($this->db->dbprefix('member_withdraw') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$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('withdrawal_pending'); break;
					case 1: $status = $this->lang->line('withdrawal_confirmed'); break;
					case 2: $status = $this->lang->line('withdrawal_cancelled'); break;
					default: $status = $this->lang->line('withdrawal_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									number_format($aRow['withdrawal_amount'], 2, '.', ''),
									number_format($aRow['credit_amount'], 2, '.', ''),
									number_format($aRow['charges'], 1, '.', ''),
									$aRow['from_member_username'],
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Withdrawal Chip List');
			$xls->addArray($data);
			$xls->generateXML('member_withdrawal_chip_list');
		}
	}
	
	public function update_withdrawal_chip($transaction_id = NULL, $status = NULL)
	{
		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());
			
			$this->load->model('user_model');
			
			$rs = $this->user_model->update_withdrawal_status($transaction_id, $status);
				
			redirect('withdrawal/chip_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function chip_listing_update()
	{
		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());
			
			$this->load->model('user_model');
			
			#Recursive deletion
			if(isset($_POST['chkbox'])) {
				for($i=0;$i<sizeof($_POST['chkbox']);$i++) {
					if($_POST['chkbox'][$i] != 'on') {
						$this->user_model->update_withdrawal_status(trim($_POST['chkbox'][$i]), trim($_POST['update_status']));
					}
				}
			}
			
			redirect('withdrawal/chip_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
}

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