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

class Adjustment 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('adjustment/request', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function request()
	{
		save_current_url(base_url() . 'adjustment/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_adjustment') . ' - ' .$this->lang->line('hf_account_adjustment');
			
			$this->load->view('adjustment_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_adjustment') . ' - ' .$this->lang->line('hf_account_adjustment');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->member_adjustment();
					
				if($rs) {
					redirect('adjustment/request/', 'refresh');
				}
				else {
					$this->load->view('adjustment_request', $_POST);
				}
			}
			else {
				redirect('adjustment/request/', 'refresh');
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}

	public function listing()
	{
		save_current_url(base_url() . 'adjustment/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_adjustment_date' => trim($this->input->post('search_adjustment_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_remark' => trim($this->input->post('search_remark', TRUE))
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_adjustment') . ' - ' .$this->lang->line('hf_adjustment_history');
			
				$this->load->view('adjustment_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_adjustment_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => '', 'member_search_remark' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_adjustment') . ' - ' .$this->lang->line('hf_adjustment_history');
				
				$this->load->view('adjustment_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_adjustment_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_adjustment') . '.transaction_date',
									$this->db->dbprefix('member_adjustment') . '.transaction_type',
									$this->db->dbprefix('member_adjustment') . '.credit_amount',
									$this->db->dbprefix('member_adjustment') . '.to_member_username',
									$this->db->dbprefix('member_adjustment') . '.remark',
									$this->db->dbprefix('member_adjustment') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_adjustment') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_type',
									'credit_amount',
									'to_member_username',
									'remark',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_adjustment';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_adjustment_date') != '') {
				if($this->session->userdata('member_search_adjustment_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_adjustment') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_adjustment') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" . $this->session->userdata('member_search_username') . "'";
				$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);
			}
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_adjustment_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 5: $row[] = $this->lang->line('wallet_5'); break;
					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['credit_amount'], 2, '.', '');
				$row[] = $aRow['to_member_username'];
				$row[] = ($aRow['remark'] != '') ? $aRow['remark'] : '-';
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('adjustment_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('adjustment_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('adjustment_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('adjustment_pending') . '</span>'; break;
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function adjustment_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('adjustment_transaction_date'),
								$this->lang->line('adjustment_currency'),
								$this->lang->line('adjustment_amount'),
								$this->lang->line('adjustment_to_member'),
								$this->lang->line('adjustment_remark'),
								$this->lang->line('adjustment_status')
							);
			
			//Get Data
			if($this->session->userdata('member_search_adjustment_date') != '') {
				$aColumns = array(
									$this->db->dbprefix('member_adjustment') . '.transaction_date',
									$this->db->dbprefix('member_adjustment') . '.transaction_type',
									$this->db->dbprefix('member_adjustment') . '.credit_amount',
									$this->db->dbprefix('member_adjustment') . '.to_member_username',
									$this->db->dbprefix('member_adjustment') . '.remark',
									$this->db->dbprefix('member_adjustment') . '.status'
								);
								
				$jColumns = $this->db->dbprefix('member_adjustment') . '.to_member_id = ' . $this->db->dbprefix('members') . '.member_id';				
			}
			else { 
				$aColumns = array(
									'transaction_date',
									'transaction_type',
									'credit_amount',
									'to_member_username',
									'remark',
									'status'
								);
			}	
			
			// DB table to use
			$sTable = 'member_adjustment';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_adjustment_date') != '') {
				if($this->session->userdata('member_search_adjustment_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_adjustment') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_adjustment') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('members') . ".username = '" . $this->session->userdata('member_search_username') . "'";
				$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->order_by($this->db->dbprefix('member_adjustment') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_adjustment_date') != '') {
				$this->db->join($jTable, $jColumns);
			}
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$transaction_type = '';
				switch($aRow['transaction_type']) {
					case 5: $transaction_type = $this->lang->line('wallet_5'); break;
					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('adjustment_pending'); break;
					case 1: $status = $this->lang->line('adjustment_confirmed'); break;
					case 2: $status = $this->lang->line('adjustment_cancelled'); break;
					default: $status = $this->lang->line('adjustment_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									$transaction_type,
									number_format($aRow['credit_amount'], 2, '.', ''),
									$aRow['to_member_username'],
									(($aRow['remark'] != '') ? $aRow['remark'] : '-'),
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Adjuestment List');
			$xls->addArray($data);
			$xls->generateXML('member_adjustment_list');
		}
	}
	
	public function platform_adjustment()
	{
		save_current_url(base_url() . 'adjustment/platform_adjustment/');
		
		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_adjustment') . ' - ' .$this->lang->line('hf_platform_adjustment');
			
			$this->load->view('adjustment_platform', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function platform_adjustment_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_adjustment') . ' - ' .$this->lang->line('hf_platform_adjustment');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->member_platform_adjustment();
					
				if($rs) {
					redirect('adjustment/platform_adjustment/', 'refresh');
				}
				else {
					$this->load->view('adjustment_platform', $_POST);
				}
			}
			else {
				redirect('adjustment/platform_adjustment/', 'refresh');
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function platform_listing_1()
	{
		save_current_url(base_url() . 'adjustment/platform_listing_1/');
		
		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_adjustment_date' => trim($this->input->post('search_adjustment_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)), 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_adjustment') . ' - ' .$this->lang->line('hf_platform_adjustment_history_1');
			
				$this->load->view('adjustment_history_platform_1', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_adjustment_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_adjustment') . ' - ' .$this->lang->line('hf_platform_adjustment_history_1');
				
				$this->load->view('adjustment_history_platform_1', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_platform_list_1()
    {
		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());
			
			$otherdb = $this->load->database('otherdb1', TRUE);
		
			/* 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(
								'report_date',
								'withdrawal_amount',
								'username',
								'remark',
								'1 AS status'
							);
			
			// DB table to use
			$sTable = 'cash_transfer_report';
			//
			
			if($this->session->userdata('member_search_adjustment_date') != '') {
				if($this->session->userdata('member_search_adjustment_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 = "(report_date >= '" .  $start_date . "' AND report_date <= '" .  $end_date . "')";
					$otherdb->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = "username = '" . $this->session->userdata('member_search_username') . "'";
				$otherdb->where($where);
			}
			
			$otherdb->where("transfer_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')
			{
				$otherdb->limit($otherdb->escape_str($iDisplayLength), $otherdb->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')
					{
						$otherdb->order_by($aColumns[intval($otherdb->escape_str($iSortCol))], $otherdb->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')
					{
						$otherdb->or_like($aColumns[$i], $otherdb->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$otherdb->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_adjustment_date') != '') {
				$otherdb->join($jTable, $jColumns);
			}
			$rResult = $otherdb->get($sTable);
		
			// Data set length after filtering
			$otherdb->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $otherdb->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $otherdb->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['report_date']);
				$row[] = number_format($aRow['withdrawal_amount'], 2, '.', '');
				$row[] = $aRow['username'];
				$row[] = $aRow['remark'];
				$row[] = '<span class="label label-success">' . $this->lang->line('adjustment_confirmed') . '</span>';
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function adjustment_platform_export_1()
	{
		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());
			
			$otherdb = $this->load->database('otherdb1', TRUE);
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('adjustment_transaction_date'),
								$this->lang->line('adjustment_amount'),
								$this->lang->line('adjustment_to_member'),
								$this->lang->line('adjustment_remark'),
								$this->lang->line('adjustment_status')
							);
			
			//Get Data
			$aColumns = array(
								'report_date',
								'withdrawal_amount',
								'username',
								'remark',
								'1 AS status'
							);
			
			// DB table to use
			$sTable = 'cash_transfer_report';
			//
			
			if($this->session->userdata('member_search_adjustment_date') != '') {
				if($this->session->userdata('member_search_adjustment_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 = "(report_date >= '" .  $start_date . "' AND report_date <= '" .  $end_date . "')";
					$otherdb->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = "username = '" . $this->session->userdata('member_search_username') . "'";
				$otherdb->where($where);
			}
			
			$otherdb->where("transfer_type", 2);
			$otherdb->order_by('report_date', 'asc');
			
			$otherdb->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_adjustment_date') != '') {
				$otherdb->join($jTable, $jColumns);
			}
			$rResult = $otherdb->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['report_date']),
									number_format($aRow['withdrawal_amount'], 2, '.', ''),
									$aRow['username'],
									$aRow['remark'],
									$this->lang->line('adjustment_confirmed')
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Asiagaming1688 Adjustment List');
			$xls->addArray($data);
			$xls->generateXML('asiagaming1688_adjustment_list');
		}
	}
	
	public function platform_listing_2()
	{
		save_current_url(base_url() . 'adjustment/platform_listing_2/');
		
		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_adjustment_date' => trim($this->input->post('search_adjustment_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)), 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_adjustment') . ' - ' .$this->lang->line('hf_platform_adjustment_history_2');
			
				$this->load->view('adjustment_history_platform_2', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_adjustment_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_adjustment') . ' - ' .$this->lang->line('hf_platform_adjustment_history_2');
				
				$this->load->view('adjustment_history_platform_2', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_platform_list_2()
    {
		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());
			
			$otherdb = $this->load->database('otherdb2', TRUE);
		
			/* 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(
								'report_date',
								'withdrawal_amount',
								'username',
								'remark',
								'1 AS status'
							);
			
			// DB table to use
			$sTable = 'cash_transfer_report';
			//
			
			if($this->session->userdata('member_search_adjustment_date') != '') {
				if($this->session->userdata('member_search_adjustment_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 = "(report_date >= '" .  $start_date . "' AND report_date <= '" .  $end_date . "')";
					$otherdb->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = "username = '" . $this->session->userdata('member_search_username') . "'";
				$otherdb->where($where);
			}
			
			$otherdb->where("transfer_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')
			{
				$otherdb->limit($otherdb->escape_str($iDisplayLength), $otherdb->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')
					{
						$otherdb->order_by($aColumns[intval($otherdb->escape_str($iSortCol))], $otherdb->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')
					{
						$otherdb->or_like($aColumns[$i], $otherdb->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$otherdb->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_adjustment_date') != '') {
				$otherdb->join($jTable, $jColumns);
			}
			$rResult = $otherdb->get($sTable);
		
			// Data set length after filtering
			$otherdb->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $otherdb->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $otherdb->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['report_date']);
				$row[] = number_format($aRow['withdrawal_amount'], 2, '.', '');
				$row[] = $aRow['username'];
				$row[] = $aRow['remark'];
				$row[] = '<span class="label label-success">' . $this->lang->line('adjustment_confirmed') . '</span>';
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function adjustment_platform_export_2()
	{
		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());
			
			$otherdb = $this->load->database('otherdb2', TRUE);
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('adjustment_transaction_date'),
								$this->lang->line('adjustment_amount'),
								$this->lang->line('adjustment_to_member'),
								$this->lang->line('adjustment_remark'),
								$this->lang->line('adjustment_status')
							);
			
			//Get Data
			$aColumns = array(
								'report_date',
								'withdrawal_amount',
								'username',
								'remark',
								'1 AS status'
							);
			
			// DB table to use
			$sTable = 'cash_transfer_report';
			//
			
			if($this->session->userdata('member_search_adjustment_date') != '') {
				if($this->session->userdata('member_search_adjustment_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 = "(report_date >= '" .  $start_date . "' AND report_date <= '" .  $end_date . "')";
					$otherdb->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = "username = '" . $this->session->userdata('member_search_username') . "'";
				$otherdb->where($where);
			}
			
			$otherdb->where("transfer_type", 2);
			$otherdb->order_by('report_date', 'asc');
			
			$otherdb->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			if($this->session->userdata('member_search_adjustment_date') != '') {
				$otherdb->join($jTable, $jColumns);
			}
			$rResult = $otherdb->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['report_date']),
									number_format($aRow['withdrawal_amount'], 2, '.', ''),
									$aRow['username'],
									$aRow['remark'],
									$this->lang->line('adjustment_confirmed')
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Luckyb3t Adjustment List');
			$xls->addArray($data);
			$xls->generateXML('luckyb3t_adjustment_list');
		}
	}
}

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