<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Android extends CI_Controller
{
    var $main_menu_name = "android";
    var $sub_menu_name = "android";
    public function __construct()
    {
        parent::__construct();
        ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
        $this->load->model('Common_Model');
        $this->load->model('User_Model');
		$this->load->model('Android_Model');
		
		date_default_timezone_set('asia/colombo');	
    }
    public function index()
    {
    }
    public function login()
    {	//get user name and password
        $user_username = $this->uri->segment('3');
        $password      = $this->uri->segment('4');
		//get user id
        $user_id       = $this->User_Model->login($user_username, $password);
        if ($user_id) {
            $data['user_details'] = $this->User_Model->get_user_info($user_id);
            $ss_user_username     = $data['user_details']['user_username'];
            $ss_user_id           = $data['user_details']['user_id'];
            $ss_group_id          = $data['user_details']['group_id'];
            $ss_warehouse_id      = $data['user_details']['warehouse_id'];
            $ss_user_first_name   = $data['user_details']['user_first_name'];
            $ss_user_last_name    = $data['user_details']['user_last_name'];
            $ss_user_group_name   = $data['user_details']['user_group_name'];
            //if cash collector owener super admin
            if ($ss_group_id == 4 || $ss_group_id == 1 || $ss_group_id == 2 || $ss_group_id == 3) {
                $status = true;
                $user   = array(
                    'ss_stutus' => $status,
                    'ss_user_id' => $user_id,
                    'ss_group_id' => $ss_group_id,
                    'ss_warehouse_id' => $ss_warehouse_id,
                    'ss_user_first_name' => $ss_user_first_name,
                    'ss_user_group_name' => $ss_user_group_name
                );
                echo json_encode($user);
            } else {
                $status =false;
                $user   = array(
                    'ss_stutus' => $status
                );
                echo json_encode($user);
            }
        } else {
            $status = false;
            $user   = array(
                'ss_stutus' => $status
            );
            echo json_encode($user);
        }
    }	
	
	public function list_loans()
    {	//imoport models
       $this->load->model('Loan_Payment_Received_Model');
	   $this->load->model('Loan_Model');
	   $this->load->model('Panalty_Model');
	   //get loan id
       $user_id=$this->uri->segment('3');	   
	   $user_route=$this->User_Model->get_user_route($user_id);      
       $route=$user_route['preferd_route'];
	   $payment_t=1;
	   $warehouse_id="";
	   //get list loan pending
	   $values = $this->Loan_Payment_Received_Model->getLoan_approved_issued_cash_collect($warehouse_id,$route,$payment_t);	   
	   $json=array();	   
	   $columns = array();
	        $data = array();
	        if (!empty($values)) {
	            foreach ($values as $users) {					
	           $row = array();
					$loan_id=$users->loan_id;				
					$payment_term=$users->loan_payment_term;
					$oustanding_value=0;
					$should_be_received_value=0;
					$total_paid_amount=0;	
					$a=0;
					$total_paid_amount=$this->Loan_Model->get_total_paid_by_loan_id($loan_id);	
					$total_panalty=$this->Loan_Payment_Received_Model->get_total_panalty_by_loan_id($loan_id);
					$total_due=$this->Loan_Payment_Received_Model->get_total_due_by_loan_id($loan_id);					
					$total_paid_panalty=$this->Loan_Payment_Received_Model->get_total_paid_panalty_by_loan_id($loan_id);
					$total_paid_due=$this->Loan_Payment_Received_Model->get_total_paid_due_by_loan_id($loan_id);
					$total_paid_loan=$this->Loan_Payment_Received_Model->get_total_paid_loan_by_loan_id($loan_id);	
					$total_paid_capital=$this->Loan_Payment_Received_Model->get_total_paid_capital_by_loan_id($loan_id);
					$today=date('Y-m-d');					
					//new calculation part added on 2019-03-13 start
					//outsanding calcilation start ------------------------------------------------------------------
					$data2=array(
					'loan_id'=>$loan_id ,
					'payment_term'=>$payment_term ,
					'total_paid_amount'=>$total_paid_amount ,
					'total_panalty'=>$total_panalty ,
					'total_due'=>$total_due ,
					'total_paid_panalty'=>$total_paid_panalty ,
					'total_paid_due'=>$total_paid_due ,
					'total_paid_loan'=>$total_paid_loan ,
					'users'=>$users					
					);					
					$oustanding_value=common_outstanding_calculation($data2);					
					// outsanding calcilation End ------------------------------------------------------------------
					
					//new calculation part added on 2019-03-13 end										
					if($total_paid_capital['capital_received']>0)
					{
					$tot_paid_capital=$total_paid_capital['capital_received'];						
					}
					else
					{
					$tot_paid_capital=0;
					}						
					if($total_panalty['panalty_amount']>0)
					{
					$panalty_charge=$total_panalty['panalty_amount'];	
					}
					else
					{
						$panalty_charge=0;
					}					
					if($total_due['due_amount']>0)
					{
					$due_charge=$total_due['due_amount'];	
					}
					else
					{
						$due_charge=0;
					}					
					if($total_paid_panalty['panalty_received']>0)
					{
					$paid_panalty_charge=$total_paid_panalty['panalty_received'];	
					}
					else
					{
				$paid_panalty_charge=0;
					}					
					if($total_paid_due['due_received']>0)
					{
						$paid_due_charge=$total_paid_due['due_received'];	
					}
					else
					{
					$paid_due_charge=0;
					}					
					if($total_paid_loan['loan_received']>0)
					{
						$paid_loan_only=$total_paid_loan['loan_received'];	
					}
					else
					{
					$paid_loan_only=0;
					}			
					$panalty_charge_total=0;								
					$panalty=$this->Panalty_Model->get_panalty_charge();					
					$panalty_charge_total=$panalty_charge;					
					if($users->loan_payment_term == 4)
					{ 					
					$pTname=$users->payment_term_name. " " .$users->loan_payment_term_qty." days" ;
					if($users->reduce_fron_ceque == 1)
					{
					$pTname=$users->payment_term_name." ( Custom duration ) ";	
					}					
					}
					else
					{
					$pTname=$users->payment_term_name;	
					}									
					$due_balance=$due_charge-$paid_due_charge;
					$panlty_balance=$panalty_charge_total-$paid_panalty_charge;
					$total_need_to_pay=$users->loan_payable_total_amount+$panalty_charge_total+$due_charge;					
					$json_itm = array(
                    'loan_id' => $users->loan_id,
                    'loan_ref' => $users->loan_ref,
                    'loan_payable_total_amount' => $users->loan_payable_total_amount,
                    'loan_term_amount' =>$users->loan_term_amount,
                    'totat_down_payment' => $users->loan_term_amount,
                    'loan_term_amount' => $users->loan_term_amount,
                    'loan_no_of_terms' => $users->loan_no_of_terms,
                    'loan_date' => $users->loan_date,
					'loan_close_date' => $users->loan_close_date,
					'cus_id' => $users->cus_id,
                    'customer_name' => $users->cus_name,
					'customer_nic'=>$users->cus_nic, 
					'location_latitude'=>$users->location_latitude, 
					'location_long'=>$users->location_long, 
					'location_status'=>$users->location_status, 
                    'loan_ref' => $users->loan_ref,
                    'outstanding' => number_format($oustanding_value+$due_balance+$panlty_balance, 2, '.', ''),
					'total_paid_amount'=>$total_paid_loan['loan_received'],
					'total_loan_amount'=>$users->loan_tot_amount,
					'due_total_amount'=>$users->loan_tot_amount-$tot_paid_capital,
					'cus_mobile'=>$users->cus_mobile, 
					'total_panalty'=>$panalty_charge_total,
					'total_due_charge'=>$due_charge,
					'arrears'=>$oustanding_value,
					'oustanding_panalty'=>$panlty_balance,
					'outstanding_due'=>$due_balance					
                );
                array_push($json, $json_itm);  	   
				}
				$data_array=array('loan_list'=>$json);				
				echo json_encode($data_array);				
				}
				//if emty value array
				else
				{
				$data_array=array('loan_list'=>$json);		
				echo json_encode($data_array);
				}            
    }
	
	
	
    public function savepayment()
    {	//import models
		$this->load->model('Loan_Payment_Received_Model');
	   	$this->load->model('Loan_Model');
	   	$this->load->model('Panalty_Model');		
		//get details start
        $loan_id                    = $this->input->post('loan_id');
        $amount                     = $this->input->post('amount');
        $user_id                    = $this->input->post('user_id');
        $tracking_number            = $this->input->post('tracking_number');
		$device_id           		= $this->input->post('device_id');
		$payment_type				= "Cash";
		$loan_issue_date=date("Y-m-d H:i:s");	
		//Validation start
		$this->load->library('form_validation'); //form validation lib
        $this->form_validation->set_rules('loan_id', 'Recode ID', 'required|trim|xss_clean');
		$this->form_validation->set_rules('amount', 'Amount', 'required|trim|xss_clean|greater_than[0]');
		$this->form_validation->set_rules('user_id', 'User', 'required|trim|xss_clean');
		$this->form_validation->set_rules('tracking_number', 'Tracking No', 'required|trim|xss_clean|is_unique[loan_payment.tracking_number]');
		$this->form_validation->set_rules('device_id', 'Currant Device Identification', 'required|trim|xss_clean');
        if ($this->form_validation->run() == FALSE)
        {
           $st = array('result' =>0,'message' => validation_errors());
           echo json_encode($st);
        }
        else
        {		
       //get details end	          
			//start : get_data from db relevent to loan
			$details_confirm = $this->Loan_Model->get_loan_details_by_loan_id($loan_id);			
			$paid= $this->Loan_Payment_Received_Model->get_loan_paid_total_by_loan_id($loan_id);
			$total_paid_amount=$this->Loan_Model->get_total_paid_by_loan_id($loan_id);	
			$total_panalty=$this->Loan_Payment_Received_Model->get_total_panalty_by_loan_id($loan_id);
			$total_due=$this->Loan_Payment_Received_Model->get_total_due_by_loan_id($loan_id);			
			$total_paid_panalty=$this->Loan_Payment_Received_Model->get_total_paid_panalty_by_loan_id($loan_id);
			$total_paid_due=$this->Loan_Payment_Received_Model->get_total_paid_due_by_loan_id($loan_id);
			$total_paid_loan=$this->Loan_Payment_Received_Model->get_total_paid_loan_by_loan_id($loan_id);			
			$total_paid_capital=$this->Loan_Payment_Received_Model->get_total_paid_capital_by_loan_id($loan_id);
			$total_paid_interest=$this->Loan_Payment_Received_Model->get_total_paid_interest_by_loan_id($loan_id);
			//End : get_data from db relevent to loan			
			//start:calculate currant recived capital and interest received %
			$tot_loan_amount=$details_confirm['loan_payable_total_amount'];
			$tot_paid_loan=$total_paid_loan['loan_received'];
			$tot_capital=$details_confirm['loan_tot_amount'];
			$tot_interest=$details_confirm['total_interest'];
			$tot_paid_capital=$total_paid_capital['capital_received'];
			$tot_paid_interest=$total_paid_interest['loan_interest_recived'];
			$tot_balance_capital=$tot_capital-$tot_paid_capital;
			$tot_balance_interest=$tot_interest-$tot_paid_interest;
			$tot_balance_loan=$tot_loan_amount-$tot_paid_loan;				
			if($tot_balance_loan==0)
			{
			$temp_tot_balance_loan=1;	
			}
			else
			{
				$temp_tot_balance_loan=$tot_balance_loan;
			}					
			$capital_presentage=($tot_balance_capital/$temp_tot_balance_loan)*100;
			$interest_presentage=($tot_balance_interest/$temp_tot_balance_loan)*100;		
			//end:calculate currant recived capital and interest received %			
			
			//start : validation amounts
			if($total_panalty['panalty_amount']>0)
			{
				$panalty_charge=$total_panalty['panalty_amount'];	
			}
			else
			{
				$panalty_charge=0;
			}					
			if($total_due['due_amount']>0)
			{
				$due_charge=$total_due['due_amount'];	
			}
			else
			{
				$due_charge=0;
			}		
			if($total_paid_panalty['panalty_received']>0)
			{
				$paid_panalty_charge=$total_paid_panalty['panalty_received'];	
			}
			else
			{
				$paid_panalty_charge=0;
			}					
			if($total_paid_due['due_received']>0)
			{
				$paid_due_charge=$total_paid_due['due_received'];	
			}
			else
			{
				$paid_due_charge=0;
			}
			if($paid['pymnt_amount']>0)
			{
			$paid_amount=$paid['pymnt_amount'];
			}
			else
			{
			$paid_amount=0;
			}			
			//END : validation amounts	
					
			// start : cal due and panalty payment 			
			$panalty_balance=$panalty_charge-$paid_panalty_charge;
			$due_balance=$due_charge-$paid_due_charge;			
			$currant_amount_balance=$amount;
			$total_loan_payment=0;
			$due_payment=0;
			$panalty_payment=0;	
			$loan_capital_payment=0;
			$loan_interest_payment=0;
			$loan_excess_payment=0;		
			//-------------------------------
			if($amount>$due_balance)
			{
				$due_payment=$due_balance;
			}
			else
			{
				if($amount>0)
				{
					$due_payment=$amount;					
				}				
			}			
			//-----------------------------------			
			$currant_amount_balance=$currant_amount_balance-$due_payment;			
			if($currant_amount_balance>$panalty_balance)
			{
				$panalty_payment=$panalty_balance;
			}
			else
			{				
				if($amount>0)
				{
					$panalty_payment=$currant_amount_balance;					
				}	
			}		
			$currant_amount_balance=$currant_amount_balance-$panalty_payment;			
			// end : cal due and panalty payment 
			//start : cal loan payment			
			if($currant_amount_balance>0)
			{
				$total_loan_payment=$currant_amount_balance;
				//start : cal capital and interest payment amount
				$loan_capital_payment=round(($total_loan_payment/100)*$capital_presentage,2);
				$loan_interest_payment=round(($total_loan_payment/100)*$interest_presentage,2);
				$loan_excess_payment=$total_loan_payment-$loan_capital_payment-$loan_interest_payment;	
				//end : cal capital and interest payment amount			
			}
			//end : cal loan payment			
			$c_pybl_total=$details_confirm['loan_payable_total_amount']+$panalty_balance+$due_balance;							
			$check= $c_pybl_total-$tot_paid_loan;
			//set to array				
			if($amount<=$check+1&& $amount>0){
			if($payment_type=="Cheque")
			{
			$data=array(
			'loan_id'=>$loan_id, 
			'cus_id'=>$details_confirm['cus_id'],
			'warehouse_id'=>$details_confirm['warehouse_id'],
			'route_id'=>$details_confirm['route_id'],
			'pymnt_paying_by'=>$payment_type,
			'payment_term'=>$details_confirm['loan_payment_term'],
			'pymnt_date_time'=>date('Y-m-d', strtotime($loan_issue_date)) ,			
			'pymnt_amount'=>$amount,
			'pymnt_cheque_no'=>$cheqe_number,
			'pymnt_cheque_date'=>date('Y-m-d', strtotime($chque_date)) ,
			'cheque_payee'=>$payee,
			'bank_id'=>$issue_bank,
			'pymnt_branch'=>$issue_brnach,
			'pymnt_added_date_time'=>date("Y-m-d H:i:s"),
			'user_id'=>$user_id,
			'pymnt_collected'=>1,	
			'panalty_received'=>$panalty_payment,
			'due_received'=>$due_payment,
			'loan_received'=>$total_loan_payment,
			'excess_received'=>$loan_excess_payment,
			'capital_received'=>$loan_capital_payment,
			'loan_interest_recived'=>$loan_interest_payment, 
			'pymnt_ref_no'=>$this->Common_Model->gen_ref_number('paymnt_id','loan_payment','RCPT/'), 			
			);
			}
			else
			{
			$data=array(
			'loan_id'=>$loan_id, 
			'cus_id'=>$details_confirm['cus_id'],
			'warehouse_id'=>$details_confirm['warehouse_id'],
			'route_id'=>$details_confirm['route_id'],
			'pymnt_paying_by'=>$payment_type,
			'pymnt_date_time'=>date('Y-m-d', strtotime($loan_issue_date)) ,			
			'pymnt_amount'=>$amount,			
			'pymnt_added_date_time'=>date("Y-m-d H:i:s"),
			'user_id'=>$user_id,	
			'pymnt_collected'=>1,
			'pymnt_deposited'=>1,
			'panalty_received'=>$panalty_payment,
			'due_received'=>$due_payment,
			'loan_received'=>$total_loan_payment,
			'excess_received'=>$loan_excess_payment,
			'capital_received'=>$loan_capital_payment,
			'loan_interest_recived'=>$loan_interest_payment,
			'pymnt_ref_no'=>$this->Common_Model->gen_ref_number('paymnt_id','loan_payment','RCPT/'),
			'tracking_number'=>$tracking_number,
			'device_id'=>$device_id,					
			);					
			}			
			$result1=$this->Loan_Payment_Received_Model->save_loan_payment($data);			
			//check whether loan is ready to close and if rady automatical close by the system			
			$paid= $this->Loan_Payment_Received_Model->get_loan_paid_total_by_loan_id($loan_id);
			if($paid['pymnt_amount']>0)
			{
			$paid_amount=$paid['pymnt_amount'];
			}
			else
			{
			$paid_amount=0;
			}	
			$tot_to_be_payable=$tot_loan_amount+$panalty_charge+$due_charge;			
			$tot_to_be_pay_balance_amount=$tot_to_be_payable-$paid_amount;
			
			if($tot_to_be_pay_balance_amount<=0)
			{	//echo"done";
				$data=array('is_closed'=>1,'system_close_date'=>date("Y-m-d"));
				$result2= $this->Loan_Payment_Received_Model->update_loan_close_status($loan_id,$data);				
			}
			if ($result1 == 1) {
            echo json_encode(array(
                'result' => '1',
				'loan_id'=>$loan_id,'amount'=>$amount ,'user_id'=> $user_id ,'message'=>"New Payment Added Successfully!"			
            ));
        }
        if ($result1 == 0) {
            echo json_encode(array(
                'result' => '0','message'=>"Error Found. Please Contact your system administrator"
            ));
        }			
			}
			else
			{
				echo json_encode(array(
                'result' => '0','message'=>"Error Found. Please Contact your system administrator"
           		 ));				
			}
		//validatin end tag
		}
		//validatin end tag
        //save after  
    }	
	
	
	public function loan_details()
	{
		//import models
		$this->load->model('Loan_Payment_Received_Model');
		$this->load->model('Loan_Panalty_Process_Model');	
		$this->load->model('Loan_Due_Process_Model');
		$this->load->model('Loan_Model');
		$this->load->model('Customer_Model');
		$this->load->model('Warehouse_Model');
		//get loan id
		$loan_id=$this->uri->segment('3');
		//set details
		$data['loan_details']= $this->Loan_Model->get_loan_details_by_id($loan_id);
		$data['loan_panalty']= $this->Loan_Panalty_Process_Model->get_loan_penalty_by_loan_id($loan_id);
		$data['loan_due']= $this->Loan_Due_Process_Model->get_loan_due_by_loan_id($loan_id);		
		$data['total_paid_amount']=$this->Loan_Model->get_total_paid_by_loan_id($loan_id);
		$data['customer_details']= $this->Customer_Model->get_customer_info($data['loan_details']->cus_id);
		$data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info($data['loan_details']->warehouse_id);
		$data['loan_payment_list']= $this->Loan_Model->get_loan_payments_by_loan_id($loan_id);
		$data['loan_id']=$loan_id;
		$t_l_d=$data['loan_details'];
			$loan_tt=$t_l_d->loan_payable_total_amount;	
			
			
			//get data
			$paid= $this->Loan_Payment_Received_Model->get_loan_paid_total_by_loan_id($loan_id);
			$total_paid_amount=$this->Loan_Model->get_total_paid_by_loan_id($loan_id);	
			$total_panalty=$this->Loan_Payment_Received_Model->get_total_panalty_by_loan_id($loan_id);
			$total_due=$this->Loan_Payment_Received_Model->get_total_due_by_loan_id($loan_id);			
			$total_paid_panalty=$this->Loan_Payment_Received_Model->get_total_paid_panalty_by_loan_id($loan_id);
			$total_paid_due=$this->Loan_Payment_Received_Model->get_total_paid_due_by_loan_id($loan_id);
			$total_paid_loan=$this->Loan_Payment_Received_Model->get_total_paid_loan_by_loan_id($loan_id);
			//set data
			$data['total_panalty']=$total_panalty['panalty_amount'];
			$data['total_due']=$total_due['due_amount'];
			$data['total_paid_panalty']=$total_paid_panalty['panalty_received'];
			$data['total_paid_due']=$total_paid_due['due_received'];
			$data['total_paid_loan']=$total_paid_loan['loan_received'];


			$loan_panatly_balance=$total_panalty['panalty_amount']-$total_paid_panalty['panalty_received'];
			$loan_due_balance=$total_due['due_amount']-$total_paid_due['due_received'];
			
			if($total_paid_loan['loan_received']>0)
			{
				$bal_p=$total_paid_loan['loan_received'];
			}else
			{
				$bal_p=0;
			}				
			$data['loan_due_total']=($loan_tt-$bal_p)+($loan_panatly_balance+$loan_due_balance);			
			echo json_encode($data);		
	}	
	
	public function get_user_summary()
	{
		$this->load->model('Report_Loan_Model');		
		$user_id=$this->uri->segment('3');
		$date=$this->uri->segment('4');	
		$route=$this->uri->segment('5');	
		$sel_date=date_create($date);		
		$sel_date=date_format($sel_date,"Y-m-d");		
		$data['payment_slip_list']=$this->Report_Loan_Model->get_user_summary($user_id,$sel_date,$route);
		echo json_encode($data);		
	}
	
	
		public function get_route_list()
	{
		$this->load->model('Route_Model');		
		$data['route_list']=$this->Route_Model->get_all_active_routes();
		echo json_encode($data);		
	}
	
	
	public function save_location()
	{
		$this->load->model('Customer_Model');
		$user_id                    = $this->input->post('user_id');
        $cus_id                     = $this->input->post('cus_id');
        $location_status            = $this->input->post('location_status');
        $location_latitude          = $this->input->post('location_latitude');
		$location_long           	= $this->input->post('location_long');		
		
		$data=array(
			'locaton_update_user_id'=>$user_id, 
			'location_status'=>$location_status,
			'location_latitude'=>$location_latitude,
			'location_long'=>$location_long,
			);	
			
		$result=$this->Customer_Model->customer_location_save($cus_id,$data);
		
		if($result==1){			
			$data_r=array(
			'status'=>1, 
			'error'=>0,
			'message'=>"Successfuly Updated",
			);			
		}
		else
		{
			
		$data_r=array(
			'status'=>0, 
			'error'=>1,
			'message'=>"Not Saved",
			);		
		}		
		echo json_encode($data_r);			
		
	}
	
	
	
	//transaction log start
	function save_transaction_log()
	{
		$transaction_id=$this->input->post('transaction_id');
		$transaction_customer=$this->input->post('transaction_customer');
		$transaction_user=$this->input->post('transaction_user');
		$transaction_recode_id=$this->input->post('transaction_recode_id');
		$transaction_type=$this->input->post('transaction_type');	
		$transaction_start_time=$this->input->post('transaction_start_time');
		$transaction_sending_type=$this->input->post('transaction_sending_type');
		$connection_status=$this->input->post('connection_status');
		$data_saving_type=$this->input->post('data_saving_type');
		$transaction_end_time=$this->input->post('transaction_end_time');	
		$response_status=$this->input->post('response_status');
		$response_result=$this->input->post('response_result');
		$response_message=$this->input->post('response_message');
		$device_id=$this->input->post('device_id');
		$transaction_location_latitude=$this->input->post('transaction_location_latitude');
		$transaction_location_longitude=$this->input->post('transaction_location_longitude');
		
		$log=$this->input->post('log');
		$added_date_time=date('Y-m-d H:i:s');
		
		$data=array(
				'transaction_id'=>$transaction_id,	
				'transaction_customer'=>$transaction_customer,
				'transaction_user'=>$transaction_user,
				'transaction_recode_id'=>$transaction_recode_id,
				'transaction_type'=>$transaction_type,
				'transaction_start_time'=>$transaction_start_time,
				'transaction_sending_type'=>$transaction_sending_type,
				'connection_status' => $connection_status,
				'data_saving_type' => $data_saving_type,
				'transaction_end_time' => $transaction_end_time,
				'response_status' => $response_status,
				'response_result' => $response_result,
				'response_message' => $response_message,
				'log' => $log,
				'device_id' => $device_id,
				'added_date_time' => $added_date_time,
				'transaction_location_latitude'=>$transaction_location_latitude,
				'transaction_location_longitude'=>$transaction_location_longitude,
			);
              $result=$this->Android_Model->save_transaction_log($data);
		
			
			if ($result==1) {
				echo json_encode(array('error'=>1,'message'=>"Transaction done",'status' =>1));
			} else {
				echo json_encode(array('error'=>0,'message'=>"Error occurd!",'status' =>0));
			}			
	}	
	//transaction log starendt
	
	function save_visit_location()
	{
		
		$date=$this->input->post('date');
		$user_id=$this->input->post('user_id');
		$cus_id=$this->input->post('cus_id');
		$location_latitude=$this->input->post('location_latitude');
		$location_long=$this->input->post('location_long');
		$device_id=$this->input->post('device_id');
		$time=$this->input->post('time');
		$transaction=$this->input->post('transaction');
		$system_date_time=date('Y-m-d H:i:s');
		$rout_p=$this->User_Model->get_user_route($user_id);
		
		if(isset($rout_p['preferd_route']))
		{
		$route_id=$rout_p['preferd_route'];			
		}else
		{
		$route_id=NULL;	
		}
		
		
		$data=array(
				'date'=>$date,	
				'user_id'=>$user_id,
				'cus_id'=>$cus_id,
				'location_latitude'=>$location_latitude,
				'location_long'=>$location_long,
				'device_id'=>$device_id,
				'time'=>$time,
				'transaction'=>$transaction,
				'route_id'=>$route_id,
				'system_date_time'=>$system_date_time,
				
			);
              $result=$this->Android_Model->save_visitlocation_log($data);
			  if ($result==1) {
				echo json_encode(array('error'=>1,'message'=>"Done",'status' =>1));
			} else {
				echo json_encode(array('error'=>0,'message'=>"Error occurd!",'status' =>0));
			}	
		
	}
	
	
	function save_new_transaction_log()
	{
		$log_id=$this->input->post('log_id');
		$system_date_time=$this->input->post('system_date_time');
		$user_id=$this->input->post('user_id');
		$action_ref_no=$this->input->post('action_ref_no');
		$device_id=$this->input->post('device_id');	
		$page_name=$this->input->post('page_name');
		$action=$this->input->post('action');
		$description=$this->input->post('description');
		$transaction_id=$this->input->post('transaction_id');
		$result=$this->input->post('result');	
		$message=$this->input->post('message');
		$amount=$this->input->post('amount');
		$loan_id=$this->input->post('loan_id');
		$date=$this->input->post('date');
		
		$data=array(
				'log_id'=>$log_id,	
				'system_date_time'=>date('Y-m-d H:i:s', strtotime($system_date_time)),
				'user_id'=>$user_id,
				'action_ref_no'=>$action_ref_no,
				'device_id'=>$device_id,
				'page_name'=>$page_name,
				'action'=>$action,
				'description' => $description,
				'transaction_id' => $transaction_id,
				'result' => $result,
				'message' => $message,
				'amount' => $amount,
				'loan_id' => $loan_id,
				'date' => date('Y-m-d H:i:s', strtotime($date)),
			);
              $result=$this->Android_Model->save_new_transaction_log($data);
		
			
			if ($result==1) {
				echo json_encode(array('error'=>1,'message'=>"Transaction done",'status' =>1));
			} else {
				echo json_encode(array('error'=>0,'message'=>"Error occurd!",'status' =>0));
			}			
	}	
	
	
	
	
	
	
	
	
	
	
	
}