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

class Payout 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('payout/request', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function request()
	{
		save_current_url(base_url() . 'payout/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_payout') . ' - ' .$this->lang->line('hf_payout_request');
			
			$this->load->view('payout_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_payout') . ' - ' .$this->lang->line('hf_payout_request');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->member_payout();
					
				if($rs) {
					redirect('payout/request/', 'refresh');
				}
				else {
					$this->load->view('payout_request', $_POST);
				}
			}
			else {
				redirect('payout/request/', 'refresh');
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}

	public function listing()
	{
		save_current_url(base_url() . 'payout/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_payout_date' => trim($this->input->post('search_payout_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_remark' => trim($this->input->post('search_remark', TRUE)), 
						'member_search_department' => ($this->input->post('search_department', TRUE) != '') ? implode(',', $this->input->post('search_department', TRUE)) : '',
						'member_search_payout_status' => ($this->input->post('search_payout_status', TRUE) != '') ? implode(',', $this->input->post('search_payout_status', TRUE)) : '',
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_payout') . ' - ' .$this->lang->line('hf_payout_history');
			
				$this->load->view('payout_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_payout_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_remark' => '', 'member_search_department' => '', 'member_search_payout_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_payout') . ' - ' .$this->lang->line('hf_payout_history');
				
				$this->load->view('payout_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(
								'created_date',
								'order_no',
								'payment_gateway_id',
								'currency_id',
								'bank_id',
								'holder_name',
								'account_no',
								'amount',
								'remark',
								'gateway_response',
								'status',
								'department_id',
								'created_by',
							);
		
			// DB table to use
			$sTable = 'member_payout';
			//
			
			if($this->session->userdata('member_search_payout_date') != '') {
				if($this->session->userdata('member_search_payout_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_payout') . ".created_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_payout') . ".created_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_reamrk') != '') {
				$where = $this->db->dbprefix('member_payout') . ".remark = '" .  $this->session->userdata('member_search_reamrk') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_department') != '') {
				$where = $this->db->dbprefix('member_payout') . ".department_id IN(" .  $this->session->userdata('member_search_department') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_payout_status') != '') {
				$where = $this->db->dbprefix('member_payout') . ".status IN(" .  $this->session->userdata('member_search_payout_status') . ")";
				$this->db->where($where);
			}
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = date('Y-m-d H:i:s', $aRow['created_date']);
				$row[] = $aRow['order_no'];
				
				switch($aRow['payment_gateway_id']) {
					default: $row[] = $this->lang->line('payment_gateway_3'); break;
				}
				
				switch($aRow['currency_id']) {
					default: $row[] = $this->lang->line('currency_myr_without_symbol'); break;
				}
				
				$row[] = $this->lang->line(get_malaysia_bank_3($aRow['bank_id']));
				$row[] = $aRow['holder_name'];
				$row[] = $aRow['account_no'];
				$row[] = number_format($aRow['amount'], 2, '.', '');
				$row[] = (($aRow['remark'] != '') ? $aRow['remark'] : '-');
				$row[] = (($aRow['gateway_response'] != '') ? $aRow['gateway_response'] : '-');
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('payout_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('payout_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('payout_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('payout_pending') . '</span>'; break;
				}
				
				$row[] = $this->lang->line(get_department_type($aRow['department_id']));
				$row[] = $aRow['created_by'];
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function payout_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('payout_payout_date'),
								$this->lang->line('payout_order_no'),
								$this->lang->line('payout_payment_gateway'),
								$this->lang->line('payout_currency'),
								$this->lang->line('payout_bank'),
								$this->lang->line('payout_holder_name'),
								$this->lang->line('payout_account_no'),
								$this->lang->line('payout_amount'),
								$this->lang->line('payout_remark'),
								$this->lang->line('payout_gateway_response'),
								$this->lang->line('payout_status'),
								$this->lang->line('payout_department'),
								$this->lang->line('payout_created_by')
							);
			
			$aColumns = array(
								'created_date',
								'order_no',
								'payment_gateway_id',
								'currency_id',
								'bank_id',
								'holder_name',
								'account_no',
								'amount',
								'remark',
								'gateway_response',
								'status',
								'department_id',
								'created_by',
							);
		
			// DB table to use
			$sTable = 'member_payout';
			//
			
			if($this->session->userdata('member_search_payout_date') != '') {
				if($this->session->userdata('member_search_payout_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_payout') . ".created_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_payout') . ".created_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_reamrk') != '') {
				$where = $this->db->dbprefix('member_payout') . ".remark = '" .  $this->session->userdata('member_search_reamrk') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_department') != '') {
				$where = $this->db->dbprefix('member_payout') . ".department_id IN(" .  $this->session->userdata('member_search_department') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_payout_status') != '') {
				$where = $this->db->dbprefix('member_payout') . ".status IN(" .  $this->session->userdata('member_search_payout_status') . ")";
				$this->db->where($where);
			}
			
			$this->db->order_by($this->db->dbprefix('member_topup') . '.created_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)
			{
				$payment_gateway = '';
				switch($aRow['payment_gateway_id']) {
					default: $payment_gateway = $this->lang->line('payment_gateway_3'); break;
				}
				
				$currency = '';
				switch($aRow['currency_id']) {
					default: $currency = $this->lang->line('currency_myr_without_symbol'); break;
				}
				
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('payout_pending'); break;
					case 1: $status = $this->lang->line('payout_confirmed'); break;
					case 2: $status = $this->lang->line('payout_cancelled'); break;
					default: $status = $this->lang->line('payout_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['created_date']),
									$payment_gateway,
									$currency,
									$this->lang->line(get_malaysia_bank_3($aRow['bank_id'])),
									$aRow['holder_name'],
									$aRow['account_no'],
									number_format($aRow['amount'], 2, '.', ''),
									(($aRow['remark'] != '') ? $aRow['remark'] : '-'),
									(($aRow['gateway_response'] != '') ? $aRow['gateway_response'] : '-'),
									$status,
									$this->lang->line(get_department_type($aRow['department_id'])),
									$aRow['created_by'],
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Payout List');
			$xls->addArray($data);
			$xls->generateXML('payout_list');
		}
	}
}

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