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

class Broker extends CI_Controller {
	
    var $main_menu_name = "broker";
	  var $sub_menu_name = "broker";

      function __construct() 
      {
        parent::__construct();
        $this->load->model('Broker_Model');
        $this->load->model('Common_Model');
		$this->load->model('Sequerty_Model');
        $this->load->library('form_validation');
		$this->load->model('User_Model');
      }
    public function index($e=0)
    {
      $data['error'] = $e;
      $data['main_menu_name'] = $this->main_menu_name;
      $data['sub_menu_name'] = "broker";
      $this->load->view('broker/broker_list',$data);
    }
    public function get_list_broker($value='')
    {
          $values = $this->Broker_Model->get_all_active_inactive_brokers();
            $data = array();
            if (!empty($values)) {
                foreach ($values as $purchases) {
                $row = array();
                    $row[] = $purchases->broker_code;
                    $row[] = $purchases->broker_name;
					$row[] = $purchases->broker_address;
                    $row[] = $purchases->broker_contact_no;
                    $row[] = '<div class="text-center"><div class="btn-group text-left">
                    <button data-toggle="dropdown" class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                    <ul role="menu" class="dropdown-menu pull-right">
                    <li><a onClick="click_broker_update_btn('.$purchases->broker_id.')" data-toggle="modal" href="#">Edit Broker</a></li>
                     <li><a href="'.base_url().'broker/broker_payment?broker_id='.$purchases->broker_id.'"><i class="fa fa-file-text-o"></i> Broker Details</a></li>
                    </ul></div>
                    </div>';
                    $data[] = $row;
                }
                $output = array('data' =>$data);
                echo json_encode($output);
            }else{
                $output = array('data' =>'');
                echo json_encode($output);
            }
       }
    public function create_broker()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'create_broker';
		$data['grn_item_id']='';
		$data['type']='A';
		$data['pageName']='CREATE BROKER';
		$data['btnText']='Create Broker';
		$data['details']= array();
        $this->load->view('broker/create_broker',$data);	
	}
	public function save_broker()
	{
	    $this->load->library('form_validation'); //form validation lib
	    $this->form_validation->set_rules('broker_code', 'Broker Code', 'is_unique[brokers.broker_code]');
		$this->form_validation->set_rules('broker_name', 'Name', 'required');
		$type=$this->input->post('type');
		$broker_code=$this->input->post('broker_code');
		$broker_name=$this->input->post('broker_name');
		$broker_address=$this->input->post('broker_address');
		$broker_contact_no=$this->input->post('broker_contact_no');
		if($broker_code)
		{}else{
		$broker_code=$this->Common_Model->gen_ref_number('broker_id','brokers','BRO');
		}
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
		$data=array(
			'broker_code'=>$broker_code,
			'broker_name'=>$broker_name,
			'broker_address'=>$broker_address,
			'broker_contact_no'=>$broker_contact_no,
			'added_by'=>$this->session->userdata('ss_user_id'),
		);
		$result=$this->Broker_Model->save_broker($data);
		$lastid=$this->db->insert_id();
		if($type=='A'){
			if ($lastid) {
				echo json_encode(array('id'=>$lastid,'type'=>$type,'status' =>1));
			} else {
				echo json_encode(array('status'=>'0'));
			}
		}
		if($type=='E'){
			echo json_encode(array('type'=>$type,'status' =>1));
		}	
		}
	}
	 public function update_broker()
	{
        $data['id'] = 1;
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'update_broker';
		
        if (isset($_GET['broker_id'])) {
			$broker_id=$_GET['broker_id'];
		}
		else {
			$broker_id='';
		}
		if($broker_id){
			$data['broker_id']=$broker_id;
			$data['type']='A';
			$data['pageName']='UPDATE BROKER';
			$data['btnText']='Update Broker';
			$data['details']= $this->Broker_Model->get_broker_details($broker_id);			}
        $this->load->view('broker/update_broker',$data);	
	}
	public function save_update_broker()
	{
	    $broker_id=$this->input->post('broker_id');
	    $broker_code=$this->input->post('broker_code');
		$broker_name=$this->input->post('broker_name');
		$broker_address=$this->input->post('broker_address');
		$broker_contact_no=$this->input->post('broker_contact_no');
	    $data=array(
			'broker_name'=>$broker_name,
			'broker_address'=>$broker_address,
			'broker_contact_no'=>$broker_contact_no,
			'added_by'=>$this->session->userdata('ss_user_id'),
		);
		$result=$this->Broker_Model->update_broker($broker_id,$data);
			if($result==1){
				echo json_encode(array('id'=>'','status' =>1,'message'=>"Done"));
			} else {
			   echo json_encode(array('id'=>'','status' =>0,'message'=>"Fail"));
			}
	}
	public function enable_discount()
	{		
		$product_id=$this->input->get('id');
		$data=array(
		'product_id'=>$product_id,
		'start_range_value'=>1,
		'end_range_value'=>1000000,
		'discount_value'=>0,
		'is_precentage'=>0,	
		);		
		$result=$this->Broker_Model->insert_update_master_table($data,'');
		if($result==true)
		{
		$st = array('error'=>0,'status' =>1,'message'=>"Done");	
		}
		else
		{
		$st = array('error'=>1,'status' =>0,'message'=>"Error ! Data not saved");		
		}
           echo json_encode($st);
	}
	 public function edit_discount($discount_id)
    {
        $data['discount_details'] = $this->Broker_Model->get_discount_detals_by_product_dis_id($discount_id);
        $this->load->view('broker/create_discount',$data);
    }
    public function update_discount()
    {		
		$dicount_tble_id=$this->input->post('dicount_tble_id');
		$start_ds=$this->input->post('start_ds');
		$end_ds=$this->input->post('end_ds');
		$discount=$this->input->post('discount');
		$is_precentage=$this->input->post('is_precentage');
		
		$data=array(
		'start_range_value'=>$start_ds,
		'end_range_value'=>$end_ds,
		'discount_value'=>$discount,
		'is_precentage'=>$is_precentage,	
		);
               if ($this->Broker_Model->insert_update_master_table($data,$dicount_tble_id)) {

                       $st = array('status' =>1,'validation' =>'Done!');
                       echo json_encode($st);
               } else {

                       $st = array('status' =>0,'validation' =>'error occurred please contact your system administrator');
                       echo json_encode($st);
			   }    
    }
    public function broker_payment($e=0)
    {
        $data['error'] = $e;
         $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name'] = "broker";
         if(isset($_GET['broker_id'])) {
			$broker_id=$_GET['broker_id'];
			$result_staus=$this->Broker_Model->process_pending_broker_discount_invoices($broker_id);
		}
		else {
			$broker_id='';
		}
		$data['broker_id']=$broker_id;
		$data['commssion_total']= $this->Broker_Model->get_broken_commssion_total($broker_id);
		$data['paid_total']= $this->Broker_Model->get_broken_paid_total($broker_id);
	    $data['details']= $this->Broker_Model->get_broker_details($broker_id);
        $this->load->view('broker/broker_payment_list',$data);
    }
     public function broker_payment_add()
	{
        $data['id'] = 1;
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'update_broker';
		
        if (isset($_GET['broker_id'])) {
			$broker_id=$_GET['broker_id'];
		}
		else {
			$broker_id='';
		}
		if($broker_id){
			$data['broker_id']=$broker_id;
			$data['type']='A';
			$data['pageName']='BROKER COMMSSION PAYNENT ADD';
			$data['btnText']='ADD PAYMENT';
			$data['details']= $this->Broker_Model->get_broker_details($broker_id);		
		}
        $this->load->view('broker/broker_payment_add',$data);	
	}
	public function save_broker_payment()
	{
	    $this->load->library('form_validation'); //form validation lib
	    //$this->form_validation->set_rules('broker_code', 'Broker Code', 'is_unique[broker_payment.broker_code]');
		$this->form_validation->set_rules('broker_id', 'Broker', 'required');
		$type=$this->input->post('type');
		$broker_id=$this->input->post('broker_id');
	    $payment_type=$this->input->post('payment_type');
		$manual_ref_no=$this->input->post('manual_ref_no');
		$payment_amount=$this->input->post('payment_amount');
		$remarks=$this->input->post('remarks');
		$code=$this->Common_Model->gen_ref_number('broker_payment_id','broker_payment','BRP');
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
	    $data=array(
			'broker_id'=>$broker_id,
			'broker_payment_ref'=>$code,
			'payment_type'=>$payment_type,
			'manual_ref_no'=>$manual_ref_no,
			'broker_payment_date_time'=>date('Y-m-d H:i:s'),
			'broker_payment_amount'=>$payment_amount,
			'remarks'=>$remarks,
			'paid_by'=>$this->session->userdata('ss_user_id'),
		);
		$result=$this->Broker_Model->save_broker_payment($data);
		$lastid=$this->db->insert_id();
		if($type=='A'){
			if ($lastid) {
				echo json_encode(array('id'=>$lastid,'type'=>$type,'status' =>1));
			} else {
				echo json_encode(array('status'=>'0'));
			}
		}
		if($type=='E'){
			echo json_encode(array('type'=>$type,'status' =>1));
		}	
		}
	}
	
	 public function get_paid_list_broker($value='')
    {
         $broker_id=$this->input->get('broker_id');
          $values = $this->Broker_Model->get_broker_payment_list($broker_id);
            $data = array();
            if (!empty($values)) {
                foreach ($values as $purchases) {
                $row = array();
                    $row[] = $purchases->broker_payment_ref;
                    $row[] = $purchases->manual_ref_no;
                    $row[] = $purchases->broker_payment_date_time;
					$row[] = $purchases->p_type_name;
                    $row[] = $purchases->user_first_name." ".$purchases->user_last_name;
                    $row[] = $purchases->remarks;
                    $row[] = $purchases->broker_payment_amount;
                
                    $data[] = $row;
                }
                $output = array('data' =>$data);
                echo json_encode($output);
            }else{
                $output = array('data' =>'');
                echo json_encode($output);
            }
       }
       
        public function get_commssion_list_broker($value='')
    {
         $broker_id=$this->input->get('broker_id');
          $values = $this->Broker_Model->get_broker_commssion_list($broker_id);
            $data = array();
            if (!empty($values)) {
                foreach ($values as $purchases) {
                $row = array();
                    $row[] = $purchases->sale_reference_no;
                    $row[] = $purchases->sale_datetime;
                    $row[] = $purchases->product_name;
                    $row[] = $purchases->commition_rate;
					$row[] = $purchases->commission_value;
                   
                
                    $data[] = $row;
                }
                $output = array('data' =>$data);
                echo json_encode($output);
            }else{
                $output = array('data' =>'');
                echo json_encode($output);
            }
       }
       
       
       
       
       
       //-------------------------------------------------------------------------------------
    
   


}