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

class Agent extends MY_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->output->disable_cache();
		$this->load_global();
		$this->load->model('agent_model','agent');
	}

	public function index(){
		$this->permission_check('places_view');
		$data=$this->data;
		$data['page_title']="Agent List";
		$this->load->view('agent-list', $data);
	}

	public function payments(){
		$this->permission_check('places_view');
		$data=$this->data;
		$data['page_title']="Agent Payments";
		$this->load->view('agent_payments', $data);
	}

	public function profit_details_get(){
		$payment_type = $this->input->post('payment_type');
		$agent_id = $this->input->post('agent_id');
		$to_date = $this->input->post('to_date');
		$from_date = $this->input->post('from_date');
		$platform_id = $this->input->post('platform_id');

		if($agent_id=="all"&&$platform_id=="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales, db_salespayments  WHERE db_salesitems.sales_id = db_sales.id AND db_salespayments.sales_id = db_sales.id AND (DATE(db_salespayments.payment_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id,  db_sales.sales_code, db_salesitems.sales_qty, db_salesitems.price_per_unit, db_salesitems.item_a_price, db_salesitems.id AS itm_s_id, db_sales.other_charges_amt, db_salesitems.item_cost, db_sales.sales_code, db_sales.sales_date, db_salespayments.payment_date, db_sales.waybill, db_sales.agent AS agent_id, (db_salesitems.sales_qty * db_salesitems.item_a_price) AS TotalInvoiceAmount,  ( ( db_salesitems.sales_qty * db_salesitems.item_a_price ) - (db_salesitems.sales_qty * db_salesitems.sale_price_2) ) AS AgentCommition, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) - ( db_salesitems.sales_qty * db_salesitems.item_cost ) AS AdminProfit, db_customers.customer_name, db_stock.sku, db_agent.agent FROM db_sales INNER JOIN db_salesitems ON (db_sales.id = db_salesitems.sales_id) INNER JOIN db_salespayments ON (db_salespayments.sales_id = db_sales.id) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) LEFT JOIN db_customers ON (db_sales.customer_id = db_customers.id) INNER JOIN db_stock ON (db_stock.id = db_salesitems.stock_id) WHERE ( DATE(db_salespayments.payment_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.return_bit IS NULL AND db_salesitems.commision_paid='0'");
			$res['itm_info'] = $query->result();
			$return_c_query = $this->db->query("SELECT COUNT(db_return_free.id) AS free_count FROM db_return_free, db_sales WHERE db_sales.id = db_return_free.sales_id AND (DATE(db_return_free.added_date) BETWEEN '$from_date' AND '$to_date') AND db_return_free.status='0';");// AND db_sales.payment_status='$payment_type'
			$res['data_free_count'] = $return_c_query->result();
			$return_query=$this->db->query("SELECT db_return_free.id , db_return_free.return_id , db_sales.sales_date , db_salesreturn.return_date, db_return_free.sales_id , db_return_free.agent_id , db_return_free.other_charge , db_agent.agent , db_sales.sales_code , db_salesreturn.return_code FROM db_sales INNER JOIN db_return_free ON (db_sales.id = db_return_free.sales_id) INNER JOIN db_salesreturn ON (db_return_free.return_id = db_salesreturn.id) INNER JOIN db_agent ON (db_agent.id = db_sales.agent) WHERE db_return_free.status='0' AND (DATE(db_return_free.added_date) BETWEEN '$from_date' AND '$to_date')"); // AND db_sales.payment_status='$payment_type'
			$res['return_info'] = $return_query->result();
		}else if($agent_id!="all"&&$platform_id=="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales, db_salespayments  WHERE db_salesitems.sales_id = db_sales.id AND db_salespayments.sales_id = db_sales.id AND (DATE(db_salespayments.payment_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id,  db_sales.sales_code, db_salesitems.sales_qty, db_salesitems.price_per_unit, db_salesitems.item_a_price, db_salesitems.id AS itm_s_id, db_sales.other_charges_amt, db_salesitems.item_cost, db_sales.sales_code, db_sales.sales_date, db_salespayments.payment_date, db_sales.waybill, db_sales.agent AS agent_id, (db_salesitems.sales_qty * db_salesitems.item_a_price) AS TotalInvoiceAmount,  ( ( db_salesitems.sales_qty * db_salesitems.item_a_price ) - (db_salesitems.sales_qty * db_salesitems.sale_price_2) ) AS AgentCommition, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) - ( db_salesitems.sales_qty * db_salesitems.item_cost ) AS AdminProfit, db_customers.customer_name, db_stock.sku, db_agent.agent FROM db_sales INNER JOIN db_salesitems ON (db_sales.id = db_salesitems.sales_id) INNER JOIN db_salespayments ON (db_salespayments.sales_id = db_sales.id) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) LEFT JOIN db_customers ON (db_sales.customer_id = db_customers.id) INNER JOIN db_stock ON (db_stock.id = db_salesitems.stock_id) WHERE ( DATE(db_salespayments.payment_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.return_bit IS NULL AND db_sales.agent='$agent_id' AND db_salesitems.commision_paid='0'");
			$res['itm_info'] = $query->result();
			$return_c_query = $this->db->query("SELECT COUNT(db_return_free.id) AS free_count FROM db_return_free, db_sales WHERE db_sales.id = db_return_free.sales_id AND db_return_free.agent_id='$agent_id' AND (DATE(db_return_free.added_date) BETWEEN '$from_date' AND '$to_date') AND db_return_free.status='0';");// AND db_sales.payment_status='$payment_type'
			$res['data_free_count'] = $return_c_query->result();
			$return_query=$this->db->query("SELECT db_return_free.id , db_return_free.return_id , db_sales.sales_date , db_salesreturn.return_date, db_return_free.sales_id , db_return_free.agent_id , db_return_free.other_charge , db_agent.agent , db_sales.sales_code , db_salesreturn.return_code FROM db_sales INNER JOIN db_return_free ON (db_sales.id = db_return_free.sales_id) INNER JOIN db_salesreturn ON (db_return_free.return_id = db_salesreturn.id) INNER JOIN db_agent ON (db_agent.id = db_sales.agent) WHERE db_return_free.status='0' AND db_return_free.agent_id='$agent_id' AND (DATE(db_return_free.added_date) BETWEEN '$from_date' AND '$to_date')"); // AND db_sales.payment_status='$payment_type'
			$res['return_info'] = $return_query->result();
		}else if($agent_id=="all"&&$platform_id!="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales, db_salespayments  WHERE db_salesitems.sales_id = db_sales.id AND db_salespayments.sales_id = db_sales.id AND (DATE(db_salespayments.payment_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.platform='$platform_id';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id,  db_sales.sales_code, db_salesitems.sales_qty, db_salesitems.price_per_unit, db_salesitems.item_a_price, db_salesitems.id AS itm_s_id, db_sales.other_charges_amt, db_salesitems.item_cost, db_sales.sales_code, db_sales.sales_date, db_salespayments.payment_date, db_sales.waybill, db_sales.agent AS agent_id, (db_salesitems.sales_qty * db_salesitems.item_a_price) AS TotalInvoiceAmount,  ( ( db_salesitems.sales_qty * db_salesitems.item_a_price ) - (db_salesitems.sales_qty * db_salesitems.sale_price_2) ) AS AgentCommition, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) - ( db_salesitems.sales_qty * db_salesitems.item_cost ) AS AdminProfit, db_customers.customer_name, db_stock.sku, db_agent.agent FROM db_sales INNER JOIN db_salesitems ON (db_sales.id = db_salesitems.sales_id) INNER JOIN db_salespayments ON (db_salespayments.sales_id = db_sales.id) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) LEFT JOIN db_customers ON (db_sales.customer_id = db_customers.id) INNER JOIN db_stock ON (db_stock.id = db_salesitems.stock_id) WHERE ( DATE(db_salespayments.payment_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.return_bit IS NULL AND db_sales.platform='$platform_id' AND db_salesitems.commision_paid='0'");
			$res['itm_info'] = $query->result();
			$return_c_query = $this->db->query("SELECT COUNT(db_return_free.id) AS free_count FROM db_return_free, db_sales WHERE db_sales.id = db_return_free.sales_id AND (DATE(db_return_free.added_date) BETWEEN '$from_date' AND '$to_date') AND db_return_free.status='0';");// AND db_sales.payment_status='$payment_type'
			$res['data_free_count'] = $return_c_query->result();
			$return_query=$this->db->query("SELECT db_return_free.id , db_return_free.return_id , db_sales.sales_date , db_salesreturn.return_date, db_return_free.sales_id , db_return_free.agent_id , db_return_free.other_charge , db_agent.agent , db_sales.sales_code , db_salesreturn.return_code FROM db_sales INNER JOIN db_return_free ON (db_sales.id = db_return_free.sales_id) INNER JOIN db_salesreturn ON (db_return_free.return_id = db_salesreturn.id) INNER JOIN db_agent ON (db_agent.id = db_sales.agent) WHERE db_return_free.status='0' AND (DATE(db_return_free.added_date) BETWEEN '$from_date' AND '$to_date')"); // AND db_sales.payment_status='$payment_type'
			$res['return_info'] = $return_query->result();
		}else if($agent_id!="all"&&$platform_id!="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales, db_salespayments  WHERE db_salesitems.sales_id = db_sales.id AND db_salespayments.sales_id = db_sales.id AND (DATE(db_salespayments.payment_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id' AND db_sales.platform='$platform_id';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id,  db_sales.sales_code, db_salesitems.sales_qty, db_salesitems.price_per_unit, db_salesitems.item_a_price, db_salesitems.id AS itm_s_id, db_sales.other_charges_amt, db_salesitems.item_cost, db_sales.sales_code, db_sales.sales_date, db_salespayments.payment_date, db_sales.waybill, db_sales.agent AS agent_id, (db_salesitems.sales_qty * db_salesitems.item_a_price) AS TotalInvoiceAmount,  ( ( db_salesitems.sales_qty * db_salesitems.item_a_price ) - (db_salesitems.sales_qty * db_salesitems.sale_price_2) ) AS AgentCommition, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) - ( db_salesitems.sales_qty * db_salesitems.item_cost ) AS AdminProfit, db_customers.customer_name, db_stock.sku, db_agent.agent FROM db_sales INNER JOIN db_salesitems ON (db_sales.id = db_salesitems.sales_id) INNER JOIN db_salespayments ON (db_salespayments.sales_id = db_sales.id) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) LEFT JOIN db_customers ON (db_sales.customer_id = db_customers.id) INNER JOIN db_stock ON (db_stock.id = db_salesitems.stock_id) WHERE ( DATE(db_salespayments.payment_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.return_bit IS NULL AND db_sales.agent='$agent_id' AND db_sales.platform='$platform_id' AND db_salesitems.commision_paid='0'");
			$res['itm_info'] = $query->result();
			$return_c_query = $this->db->query("SELECT COUNT(db_return_free.id) AS free_count FROM db_return_free, db_sales WHERE db_sales.id = db_return_free.sales_id AND db_return_free.agent_id='$agent_id' AND (DATE(db_return_free.added_date) BETWEEN '$from_date' AND '$to_date') AND db_return_free.status='0';");// AND db_sales.payment_status='$payment_type'
			$res['data_free_count'] = $return_c_query->result();
			$return_query=$this->db->query("SELECT db_return_free.id , db_return_free.return_id , db_sales.sales_date , db_salesreturn.return_date, db_return_free.sales_id , db_return_free.agent_id , db_return_free.other_charge , db_agent.agent , db_sales.sales_code , db_salesreturn.return_code FROM db_sales INNER JOIN db_return_free ON (db_sales.id = db_return_free.sales_id) INNER JOIN db_salesreturn ON (db_return_free.return_id = db_salesreturn.id) INNER JOIN db_agent ON (db_agent.id = db_sales.agent) WHERE db_return_free.status='0' AND db_return_free.agent_id='$agent_id' AND (DATE(db_return_free.added_date) BETWEEN '$from_date' AND '$to_date')"); // AND db_sales.payment_status='$payment_type'
			$res['return_info'] = $return_query->result();
		}

		
		echo json_encode($res);
	}

	public function profit_details_get_2(){
		 $payment_type = $this->input->post('payment_type');
		// $agent_id = $this->input->post('agent_id');
		$to_date = $this->input->post('to_date');
		$from_date = $this->input->post('from_date');
		// $platform_id = $this->input->post('platform_id');

		// if($agent_id=="all"&&$platform_id=="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales WHERE db_salesitems.sales_id = db_sales.id AND (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id, db_salesitems.sales_qty, db_salesitems.price_per_unit, db_stock.cost_price, db_salesitems.sale_price_1, db_salesitems.sale_price_2, db_salesitems.id AS itm_s_id, db_sales.other_charges_amt, db_sales.sales_code, db_sales.sales_date, db_sales.payment_status, ( db_salesitems.sales_qty * db_salesitems.sale_price_1 ) AS TotalInvoiceAmount, ( db_salesitems.sales_qty * db_salesitems.price_per_unit ) AS TotalSaleAmount, ( db_salesitems.sales_qty * db_salesitems.price_per_unit ) - ( db_salesitems.sales_qty * db_stock.cost_price ) AS AdminProfit, db_customers.customer_name, db_stock.sku, db_items.item_name FROM db_salesitems INNER JOIN db_sales ON ( db_salesitems.sales_id = db_sales.id ) INNER JOIN db_stock ON ( db_salesitems.stock_id = db_stock.id ) LEFT JOIN db_customers ON ( db_sales.customer_id = db_customers.id ) INNER JOIN db_items ON (db_stock.item_id = db_items.id) WHERE ( DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date' ) AND db_sales.return_bit IS NULL");
			$res['itm_info'] = $query->result();
		// }else if($agent_id!="all"&&$platform_id=="all"){
		// 	$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales WHERE db_salesitems.sales_id = db_sales.id AND (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id';");
		// 	$res['data_count_info'] = $c_query->result();
		// 	$query=$this->db->query("SELECT db_sales.id, db_salesitems.sales_qty, db_salesitems.price_per_unit, db_salesitems.item_a_price, db_salesitems.id AS itm_s_id, db_sales.other_charges_amt, db_salesitems.item_cost, db_sales.sales_code, db_sales.sales_date, db_sales.payment_status, db_sales.waybill, db_sales.agent AS agent_id, ( db_salesitems.sales_qty * db_salesitems.item_a_price ) AS TotalInvoiceAmount, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) AS TotalSaleAmount,  ( ( db_salesitems.sales_qty * db_salesitems.item_a_price ) - (db_salesitems.sales_qty * db_salesitems.sale_price_2) ) AS AgentCommition, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) - ( db_salesitems.sales_qty * db_salesitems.item_cost ) AS AdminProfit, db_customers.customer_name, db_stock.sku, db_agent.agent FROM db_salesitems INNER JOIN db_sales ON ( db_salesitems.sales_id = db_sales.id ) INNER JOIN db_stock ON ( db_salesitems.stock_id = db_stock.id ) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) LEFT JOIN db_customers ON ( db_sales.customer_id = db_customers.id ) WHERE ( DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id' AND db_salesitems.commision_paid='0'");
		// 	$res['itm_info'] = $query->result();
		// }else if($agent_id=="all"&&$platform_id!="all"){
		// 	$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales WHERE db_salesitems.sales_id = db_sales.id AND (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.platform='$platform_id';");
		// 	$res['data_count_info'] = $c_query->result();
		// 	$query=$this->db->query("SELECT db_sales.id, db_salesitems.sales_qty, db_salesitems.price_per_unit, db_salesitems.item_a_price, db_salesitems.id AS itm_s_id, db_sales.other_charges_amt, db_salesitems.item_cost, db_sales.sales_code, db_sales.sales_date, db_sales.payment_status, db_sales.waybill, db_sales.agent AS agent_id, ( db_salesitems.sales_qty * db_salesitems.item_a_price ) AS TotalInvoiceAmount, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) AS TotalSaleAmount,  ( ( db_salesitems.sales_qty * db_salesitems.item_a_price ) - (db_salesitems.sales_qty * db_salesitems.sale_price_2) ) AS AgentCommition, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) - ( db_salesitems.sales_qty * db_salesitems.item_cost ) AS AdminProfit, db_customers.customer_name, db_stock.sku, db_agent.agent FROM db_salesitems INNER JOIN db_sales ON ( db_salesitems.sales_id = db_sales.id ) INNER JOIN db_stock ON ( db_salesitems.stock_id = db_stock.id ) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) LEFT JOIN db_customers ON ( db_sales.customer_id = db_customers.id ) WHERE ( DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.platform='$platform_id' AND db_salesitems.commision_paid='0'");
		// 	$res['itm_info'] = $query->result();
		// }else if($agent_id!="all"&&$platform_id!="all"){
		// 	$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales WHERE db_salesitems.sales_id = db_sales.id AND (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id' AND db_sales.platform='$platform_id';");
		// 	$res['data_count_info'] = $c_query->result();
		// 	$query=$this->db->query("SELECT db_sales.id, db_salesitems.sales_qty, db_salesitems.price_per_unit, db_salesitems.item_a_price, db_salesitems.id AS itm_s_id, db_sales.other_charges_amt, db_salesitems.item_cost, db_sales.sales_code, db_sales.sales_date, db_sales.payment_status, db_sales.waybill, db_sales.agent AS agent_id, ( db_salesitems.sales_qty * db_salesitems.item_a_price ) AS TotalInvoiceAmount, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) AS TotalSaleAmount,  ( ( db_salesitems.sales_qty * db_salesitems.item_a_price ) - (db_salesitems.sales_qty * db_salesitems.sale_price_2) ) AS AgentCommition, ( db_salesitems.sales_qty * db_salesitems.sale_price_2 ) - ( db_salesitems.sales_qty * db_salesitems.item_cost ) AS AdminProfit, db_customers.customer_name, db_stock.sku, db_agent.agent FROM db_salesitems INNER JOIN db_sales ON ( db_salesitems.sales_id = db_sales.id ) INNER JOIN db_stock ON ( db_salesitems.stock_id = db_stock.id ) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) LEFT JOIN db_customers ON ( db_sales.customer_id = db_customers.id ) WHERE ( DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id' AND db_sales.platform='$platform_id' AND db_salesitems.commision_paid='0'");
		// 	$res['itm_info'] = $query->result();
		// }

		
		echo json_encode($res);
	}

	public function agent_Comissions(){
		//$this->permission_check('places_add');
		$data=$this->data;
		$data['page_title']="Agent Commissions";
		$this->load->view('agent-comissions', $data);
	}


	public function add(){
		$this->permission_check('places_add');
		$data=$this->data;
		$data['page_title']='Agent';
		$this->load->view('agent', $data);
	}

	public function sw_new_agent(){
		$agent=$this->input->post('agent');
		$result=$this->agent->agent_save($agent);
		if ($result=="exist") {
            $output="exist";
        }elseif ($result=="success") {
            $output="success";
        }elseif ($result=="failed") {
            $output="failed";
        }
		echo json_encode($output);
	}

	public function newagent(){
		$this->form_validation->set_rules('agent_name', 'agent', 'trim|required');
		if ($this->form_validation->run() == TRUE) {
			$result=$this->agent->verify_and_save();
			echo $result;
		} else {
			echo "Please Enter agent name.";
		}
	}

	public function commission_update(){
		$data=$this->input->post('data_table');
		$sale_itemSet=$this->input->post('sale_itemSet');
		$return_set=$this->input->post('return_set');
		$result=$this->agent->commission_update($data,$sale_itemSet,$return_set);
		if($result>0){
			$output="success";
		}else{
			$output="failed";
		}
		echo json_encode($output);
	}

	public function update($id){
		$this->permission_check('places_edit');
		$result=$this->agent->get_details($id);
		$data=array_merge($this->data,$result);
		$data['page_title']="Agent";
		echo($id);
		//$this->load->view('agent', $data);
	}
	public function update_agent(){
		
		$this->form_validation->set_rules('agent_name', 'agent', 'trim|required');
		$this->form_validation->set_rules('q_id', '', 'trim|required');

		if ($this->form_validation->run() == TRUE) {
			$result=$this->agent->update_agent();
			echo $result;
		} else {
			echo "Please Enter agent name.";
		}
	}

	public function ajax_list()
	{
		$list = $this->agent->get_datatables();
		
		$data = array();
		$no = $_POST['start'];
		foreach ($list as $agent) {
			$no++;
			$row = array();
			$row[] = $agent->agent;
			

			 		if($agent->status==1){ 
			 			$str= "<span onclick='update_status(".$agent->id.",0)' id='span_".$agent->id."'  class='label label-success' style='cursor:pointer'>Active </span>";}
					else{ 
						$str = "<span onclick='update_status(".$agent->id.",1)' id='span_".$agent->id."'  class='label label-danger' style='cursor:pointer'> Inactive </span>";
					}
			$row[] = $str;			
			         $str2 = '<div class="btn-group" title="View Account">
										<a class="btn btn-primary btn-o dropdown-toggle" data-toggle="dropdown" href="#">
											Action <span class="caret"></span>
										</a>
										<ul role="menu" class="dropdown-menu dropdown-light pull-right">';

											if($this->permissions('places_edit'))
											$str2.='<li>
												<a title="Edit Record ?" href="agent/update/'.$agent->id.'">
													<i class="fa fa-fw fa-edit text-blue"></i>Edit
												</a>
											</li>';

											if($this->permissions('places_delete'))
											$str2.='<li>
												<a style="cursor:pointer" title="Delete Record ?" onclick="delete_agent('.$agent->id.')">
													<i class="fa fa-fw fa-trash text-red"></i>Delete
												</a>
											</li>
											
										</ul>
									</div>';			
			$row[] = $str2;

			$data[] = $row;
		}

		$output = array(
						"draw" => $_POST['draw'],
						"recordsTotal" => $this->agent->count_all(),
						"recordsFiltered" => $this->agent->count_filtered(),
						"data" => $data,
				);
		//output to json format
		echo json_encode($output);
	}


	public function search_agent_com_data()
	{
		$month = $this->input->post('month');
		$com_rate = (float)$this->input->post('com_rate');
		$agent_id = $this->input->post('agent_id');

	    $query=$this->db->query("SELECT db_sales.id , db_sales.sales_date , db_sales.waybill , db_sales.agent , db_platform.platform , db_sales.grand_total , db_sales.other_charges_amt , db_customers.customer_name , ((db_sales.grand_total - db_sales.other_charges_amt)*($com_rate/100)) AS com_amt , (db_sales.grand_total - db_sales.other_charges_amt) AS sale_amt FROM db_customers INNER JOIN db_sales ON (db_customers.id = db_sales.customer_id) INNER JOIN db_platform ON (db_sales.platform = db_platform.id) WHERE db_sales.agent='$agent_id' AND db_sales.sales_date LIKE '$month%'");
		$res['agent_com_info'] = $query->result();

		echo json_encode($res);
	}

	public function update_status(){
		$this->permission_check_with_msg('places_edit');
		$id=$this->input->post('id');
		$status=$this->input->post('status');
		$result=$this->agent->update_status($id,$status);
		return $result;
	}
	public function delete_agent(){
		$this->permission_check_with_msg('places_delete');
		$id=$this->input->post('q_id');
		$result=$this->agent->delete_agent($id);
		return $result;
	}
}



