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

class cheque_management extends CI_Controller {

    var $main_menu_name = "cheque_management";
	var $sub_menu_name = "";

	public function __construct()
	{
		parent::__construct();
		
		
		$this->load->model('Cheque_Manage_Model');		
		$this->load->model('Warehouse_Model');
		$this->load->model('Bank_Model');
		$this->load->model('Common_Model');
		$this->load->model('Customer_Model');
		$this->load->model('Menu_Model');
		$this->load->model('User_Model');
		$this->load->model('System_Settings_Model');
		$this->load->model('Unit_Model');
		$this->load->model('Menu_Items_List_Model');
		date_default_timezone_set('asia/colombo');		
	}
	
	//undifine index
	public function index()
	{ 
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
        $this->load->view('loan',$data);
	}
	
	 // -------------------------------------------------------------------------------------
	  
	  public function cheque_manage()
	{	
		$this->load->model('Route_Model'); 	 
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "cheque_manage";
		$data['routes_list']=$this->Route_Model->get_all_active_routes();
		$data['customer_list'] = $this->Customer_Model->get_all_customers();
        $this->load->view('cheque_manage',$data);
	}
	
		  
	  
	  public function get_all_pending_cheques()
	{		
	
		$route=$this->input->get('route');
		$cus_id=$this->input->get('cus_id');
		$srh_to_date='';
		$srh_from_date='';
		$srh_warehouse_id=$this->input->get('srh_warehouse_id');
		if($this->input->get('srh_to_date')){
			$srh_to_date=date('Y-m-d', strtotime($this->input->get('srh_to_date')));
		}
		if($this->input->get('srh_from_date')){
			$srh_from_date=date('Y-m-d', strtotime($this->input->get('srh_from_date')));
		}
		
		
		
		$wareh_id='';
		if($this->session->userdata('ss_group_id')==2){			
		}else {
			$wareh_id=$this->session->userdata('ss_warehouse_id');
		}
	        $values = $this->Cheque_Manage_Model->get_all_pending_realize_cheque($value='',$route,$cus_id,$srh_from_date,$srh_to_date);
			
			//print_r( $values);
			
			$columns = array();
	        $data = array();
	        if (!empty($values)) {
	            foreach ($values as $users) {					
	            $row = array();
					$row[] = $users['pymnt_cheque_date'];
					$row[] = $users['pymnt_cheque_no'];
					$row[] = $users['pymnt_amount'];
					$row[] = $users['bank_code'];					
					$row[] = $users['pymnt_branch'];
	                $row[] = $users['loan_ref'];
	                $row[] = $users['cus_name'];					
					$row[] = $users['route_name'];
																								
				$diposit_st='                     
					 <button  class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button" onclick="cheque_deposit('.$users['paymnt_id'].')">Diposit </button>';
					 
				$realize_st='                      
					 <button  class="btn btn-default btn-xs btn-success dropdown-toggle" type="button" onclick="cheque_realize('.$users['paymnt_id'].')">Realize </button>';
					 
				$reject_st='                       
					 <button  class="btn btn-default btn-xs btn-danger dropdown-toggle" type="button" onclick="cheque_reject('.$users['paymnt_id'].')">Rejected </button>';
					 
				
				if($users['pymnt_deposited']==0)
				{
				$rowst=$diposit_st;	
				}
				else
				{
					$rowst=$realize_st." ".$reject_st;
				}
				
					 
					 						
				$row[]=$rowst;				
				$data[] = $row;
	            }
	            $output = array('data' =>$data);
	            echo json_encode($output);
	        }else{
	            $output = array('data' =>'');
	            echo json_encode($output);
	        }
	   }
	   
	   
	   
	   //-------------------------------------------------------------
	   

	 function update_deposit()
	 {
		 $pay_id=$this->input->post('pay_id');
		 $data=array(
			'pymnt_deposited'=>1, 	
			);						
			$result1=$this->Cheque_Manage_Model->update_cheque_status($pay_id,$data); 
	 }
	 
	 function update_realize()
	 {
		 $pay_id=$this->input->post('pay_id');
		 $data=array(
			'pymnt_realize'=>1, 	
			);						
			$result1=$this->Cheque_Manage_Model->update_cheque_status($pay_id,$data);	 
	 }
	 
	  function update_reject()
	 {
		 $pay_id=$this->input->post('pay_id');
		 $data=array(
			'pymnt_returned'=>1, 	
			);						
			$result1=$this->Cheque_Manage_Model->update_cheque_status($pay_id,$data);	 
	 }
	
	
	
	
	

}