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

class Convert 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('convert/listing', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function listing()
	{
		save_current_url(base_url() . 'convert/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_from_username' => trim($this->input->post('search_from_username', TRUE)), 
						'member_search_convert_date' => trim($this->input->post('search_convert_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_wallet_type' => ($this->input->post('search_wallet_type', TRUE) != '') ? implode(',', $this->input->post('search_wallet_type', TRUE)) : '', 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_convert') . ' - ' .$this->lang->line('hf_convert_history');
			
				$this->load->view('convert_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_from_username' => '', 'member_search_convert_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_wallet_type' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_convert') . ' - ' .$this->lang->line('hf_convert_history');
			
				$this->load->view('convert_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(
								'transaction_date',
								'transaction_type',
								'credit_amount',
								'debit_amount',
								'from_member_username',
							);
			
			// DB table to use
			$sTable = 'sw_member_converts';
			//
			
			if($this->session->userdata('member_search_convert_date') != '') {
				if($this->session->userdata('member_search_convert_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 = "(" . "transaction_date >= '" .  $start_date . "' AND " . "transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_from_username') != '') {
				$where = "from_member_username = '" . $this->session->userdata('member_search_from_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_wallet_type') != '') {
				$where = "transaction_type IN(" .  $this->session->userdata('member_search_wallet_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], $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);
			$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 2: $row[] = $this->lang->line('wallet_1') . ' => ' . $this->lang->line('wallet_3'); break;
					default: $row[] = $this->lang->line('wallet_2') . ' => ' . $this->lang->line('wallet_1'); break;
				}
				
				$row[] = number_format($aRow['credit_amount'], 2, '.', '');
				$row[] = number_format($aRow['debit_amount'], 2, '.', '');
				$row[] = $aRow['from_member_username'];
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function convert_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('convert_transaction_date'),
								$this->lang->line('convert_type'),
								$this->lang->line('convert_out'),
								$this->lang->line('convert_in'),
								$this->lang->line('convert_from_member')
							);
			
			//Get Data
			$aColumns = array(
								'transaction_date',
								'transaction_type',
								'credit_amount',
								'debit_amount',
								'from_member_username',
							);
			
			// DB table to use
			$sTable = 'sw_member_converts';
			//
			
			if($this->session->userdata('member_search_convert_date') != '') {
				if($this->session->userdata('member_search_convert_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 = "(" . "transaction_date >= '" .  $start_date . "' AND " . "transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_from_username') != '') {
				$where = "from_member_username = '" . $this->session->userdata('member_search_from_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_wallet_type') != '') {
				$where = "transaction_type IN(" .  $this->session->userdata('member_search_wallet_type') . ")";
				$this->db->where($where);
			}
			
			$this->db->order_by('transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$transaction_type = '';
				switch($aRow['transaction_type']) {
					case 2: $transaction_type = $this->lang->line('wallet_1') . ' => ' . $this->lang->line('wallet_3'); break;
					default: $transaction_type = $this->lang->line('wallet_2') . ' => ' . $this->lang->line('wallet_1'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									$transaction_type,
									number_format($aRow['credit_amount'], 2, '.', ''),
									number_format($aRow['debit_amount'], 2, '.', ''),
									$aRow['from_member_username']
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Convert List');
			$xls->addArray($data);
			$xls->generateXML('member_convert_list');
		}
	}
}

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