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

class Payment extends CI_Controller {

	public function __construct() {
		parent::__construct();
		date_default_timezone_set("Asia/Kolkata");
		$this->load->model('Company_model');
		
		if(!$this->session->userdata('logged_in')) {
			redirect(base_url('Login'));
		}
 	}

 	function getPayDetails(){
 		$company_id = '';
 		if($this->session->userdata('user_type') == 1){
 			$company_id=(isset($_POST['company_id'])&&!empty($_POST['company_id']))?$_POST['company_id']:'';
 		}else{
	 		$company_id = $this->session->userdata('id');
	 		$template['company_id'] = $company_id;
 		}
		$template['page'] = 'Payment/payment_details';
        $template['page_desc'] = "Payment Details";
        $template['page_title'] = "Payment Management";
        
		$template['menu'] = "Payment Management";
		$template['sub_menu'] = "Payment Details";

 		$template['company_id'] = $company_id;
 		$template['user_type'] = $this->session->userdata('user_type');
 		$template['company_data'] = $this->Company_model->getCompany();
 		$template['payment_details'] = $this->Company_model->getPayDetails($company_id);
 		$template['broker_pay_details'] = $this->Company_model->getBrokerPayDetails($company_id);
		$this->load->view('template',$template);
 	}
	
	function updatePaymentDate(){
		$retStatus = array('status'=>'0');
		if(!isset($_POST) || empty($_POST) || 
		   !isset($_POST['broker_id']) || empty($_POST['broker_id']) || 
		   !isset($_POST['last_paid_date']) || empty($_POST['last_paid_date'])){
			echo json_encode($retStatus);exit;
		}
		$_POST['broker_id'] = decode_param($_POST['broker_id']);
		$_POST['company_id'] = $this->session->userdata['id'];
		$_POST['last_paid_date'] = date('Y-m-d 00:00:00',strtotime($_POST['last_paid_date']));

        $retStatus['status'] = $this->Company_model->updatePaymentDate($_POST);

        if($retStatus['status']){
        	$data = $this->Company_model->getBrokerPayDetails($_POST['company_id']);
        	if(!isset($data[$_POST['broker_id']]) || empty($data[$_POST['broker_id']])){
        		$retStatus['status'] = 0;
        	} else {
        		$data[$_POST['broker_id']]['last_paid_date'] = date('d-M-Y',strtotime($data[$_POST['broker_id']]['last_paid_date']));
        		$retStatus['data'] = $data[$_POST['broker_id']];
        	}
        }
        echo json_encode($retStatus);exit;
	}
}
?>