<?php	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Master_sale_data_report extends CI_Controller {

	function __construct()	{
		parent::__construct();
		//$this->lang->load('masters', '');
		 $language = $this->session->userdata('language'); 
        if ($language=='hindi') {
               $this->lang->load('hindi', '');
        }else{
              $this->lang->load('masters', '');
        }
  
      // error_reporting(E_ALL);
	}

	function index()	{	
		$this->master_sales_data_report();
	}
	function master_sales_data_report(){

 		$this->_page_data['lable_sales_report_branch']=$this->lang->line('lable_sales_report_branch', FALSE);
 		$this->_page_data['lable_sales_report_date_from']=$this->lang->line('lable_sales_report_date_from', FALSE);
 		$this->_page_data['lable_sales_report_date_to']=$this->lang->line('lable_sales_report_date_to', FALSE);
 		$this->_page_data['lable_sales_report_sales_by']=$this->lang->line('lable_sales_report_sales_by', FALSE);
 		$this->_page_data['lable_sales_report_dispenser_name']=$this->lang->line('lable_sales_report_dispenser_name', FALSE);

		$this->_page_data['product_category'] =$this->fetch_product_category();																									 
		$this->_page_data['page_title']="Sale Report";
		$this->_page_data['branch']=$this->fetch_branch_name();
		$this->_page_data['bredcrum']=array(
			                            'Reports'=>'#',
			                            'Sales Report'=>'#',
										//'Manage Customer Master'=>'#',
									
										);
		$this->load->view('gc/master_sale_data_report.php',$this->_page_data);
	}
	function fetch_product_category(){

		admin_auth();

		
		$condition[] = array('join',TABLE_PRODUCT.' as p','p.product_category_id = a.product_category_id','inner');
		$condition[] = array('where_not_in','p.product_type','Refill');
		$condition[] = array('where','a.is_active','1');
    	$selected_array = array( 
			                   'table_name' =>TABLE_PRODUCT_CATEGORY. ' as a',
			                   'table_fields' =>'a.product_category_id,a.product_category_name,a.is_active',
			                   'condition'=>$condition,
							   'sort_by'=>'a.product_category_name',
								'order_by_field'=>'a.product_category_name',
								'order'=>'ASC',
                               'limit'=>'',
                               'group_by_field'=>'a.product_category_id',
                               'offset'=>''

		);

		return $this->master_model->select_data($selected_array);
    }							   
	function master_sales_data_report_list($get_customer_id='0',$query_id = 0, $sort_by = 'dispenser_sales_id', $sort_order = 'asc', $offset = 0, $sort_type=''){
		

	// echo "query=".$query_id."<br>";
	// echo "sort_by=".$sort_by."<br>";
	// echo "sort_order=".$sort_order."<br>";
	// echo "offset=".$offset."<br>";
	// echo "sort_type=".$sort_type."<br>";
	// echo "get_customer_id=".$get_customer_id."<br>";
   	admin_auth();
    $limit = LIMIT_PER_PAGE;
   	//error_reporting(E_ALL);
    $this->_page_data['get_customer_id']=$get_customer_id;
	$this->_page_data['page_title']="List of Sale Report";
	$this->_page_data['bredcrum']=array(
			                            'Reports'=>'#',
			                            'Sales Report'=>'#',
										'List of Sale Report'=>'#',
									
										);	

	    $this->load->library('NF_Query_search');
		$this->nf_query_search->load_query($query_id);

		$query_array = array(
 				'branch_id'=>$this->input->get('branch_id'),
 				'from'=>$this->input->get('from'),
 				'to'=>$this->input->get('to'),
 				'sale'=>$this->input->get('sale'),
 				'product_name_dispenser'=>$this->input->get('product_name_dispenser'),
 				'customer_id'=>$this->input->get('customer_id'),
				'product_category_id'=>$this->input->get('product_category_id'),

		);
		$branch_id=check_injections_filter($query_array['branch_id']);
		$from=check_injections_filter($query_array['from']);
		$to=check_injections_filter($query_array['to']);
		$sale=check_injections_filter($query_array['sale']);
		$product_name_dispenser=check_injections_filter($query_array['product_name_dispenser']);
		$product_category_id=check_injections_filter($query_array['product_category_id']);
	// $branch_id=$this->input->post('branch_id');
	// $from=$this->input->post('from');
	// $to=$this->input->post('to');
	// $sale=$this->input->post('sale');
	// $product_name_dispenser=$this->input->post('product_name_dispenser');
 	
	if($get_customer_id=='0')
	{
		//$customer_id=$this->input->post('customer_id');
		$customer_id=check_injections_filter($query_array['customer_id']);
	}
	else
	{
			
		//$customer_id=$this->encrypt->decode($get_customer_id);
		$customer_id=encrypt_decrypt_parameters($get_customer_id,'decrypt');
	}
	
	$user_type= $this->session->userdata('user_type');
	$access_branch= $this->session->userdata('access_branch');
	$user_type_id= $this->session->userdata('user_type_id');
    $acb =  explode( ',',$access_branch);

    $this->_page_data['excel_branch_id']=$branch_id;
    $this->_page_data['excel_from']=$from;
    $this->_page_data['excel_to']=$to;
    $this->_page_data['excel_sale']=$sale;
    $this->_page_data['excel_product_name_dispenser']=$product_name_dispenser;
    $this->_page_data['excel_customer_id']=$customer_id;
	$this->_page_data['excel_product_category_id']=$product_category_id;

	
	// $export=$this->input->post('excel_export');
	// if ($export=='1') {
	// $branch_id=$this->input->post('excel_branch_id');
	// $from=$this->input->post('excel_from');
	// $to=$this->input->post('excel_to');
	// $sale=$this->input->post('excel_sale');
	// $product_name_dispenser=$this->input->post('excel_product_name_dispenser');
	// }
	//$access_branch= $this->session->userdata('access_branch');

	$this->_page_data['company_branch_name']=''; 
	$this->_page_data['from']='';  
	$this->_page_data['to']=''; 
	$this->_page_data['get_sale_person_firstname']=''; 
    $this->_page_data['get_sale_person_lastname']=''; 
    $this->_page_data['get_dispenser']='';
    $this->_page_data['customer_id']='';
	$this->_page_data['product_category_id']='';

    $access_branch= $this->session->userdata('access_branch');
    $acb =  explode( ',',$access_branch);
	

    if ($branch_id>"0") {  
    	$company_branch_names=get_data_value(TABLE_COMPANY_BRANCH,'company_branch_name','company_branch_id',$branch_id,'where');
		$this->_page_data['company_branch_names']=$company_branch_names;   	
		$condition[] = array('where','d.company_branch_id',$branch_id);
	}else{
			$this->_page_data['company_branch_names']=''; 
	}
	if ($from>"0" and $to<"0") { 
			$this->_page_data['from']=$from;       	
			//$condition[] = array('where','r.refill_date',$from);
			$condition[] = array('where','d.installation_date',date('Y-m-d', strtotime($from)));
	}else{
	$this->_page_data['from']='';  	
	}
	
	if ($to>"0" and $from<"0") {
        	$this->_page_data['to']=$to;  	
			//$condition[] = array('where','r.next_refill_date',$to);
			$condition[] = array('where','d.installation_date',date('Y-m-d', strtotime($to)));
	}else{
		$this->_page_data['to']='';  	
	}
	
	if($from>"0" and $to>"0"){
		$this->_page_data['to']=$to; 
		$this->_page_data['from']=$from;  
		$query_part='d.installation_date BETWEEN "'.date('Y-m-d', strtotime($from)).'" AND "'.date('Y-m-d', strtotime($to)).'"';
			//$condition[] = array('where','r.refill_date',$from);
			$condition[] = array('where',$query_part);
	}else{
		$this->_page_data['from']='';  
	   $this->_page_data['to']=''; 
    
	}
	if ($sale>"0") {
        	$get_first_name=get_data_value(TABLE_USERS,'first_name','user_id',$sale,'where');
        	$get_last_name=get_data_value(TABLE_USERS,'last_name','user_id',$sale,'where');
        	$this->_page_data['get_sale_person_firstname']=$get_first_name; 
        	$this->_page_data['get_sale_person_lastname']=$get_last_name; 
			$condition[] = array('where','u.user_id',$sale);
	}else{
		$this->_page_data['get_sale_person_firstname']=''; 
    $this->_page_data['get_sale_person_lastname']=''; 
	}
	if ($product_name_dispenser>"0") {
        	$get_dispenser=get_data_value(TABLE_PRODUCT,'product_name','product_id',$product_name_dispenser,'where');
        	$this->_page_data['get_dispenser']=$get_dispenser; 
			$condition[] = array('where','d.product_id',$product_name_dispenser);
	}else{
		$this->_page_data['get_dispenser']='';
	}
	if ($customer_id>"0") {
        	$get_customer=get_data_value(TABLE_CUSTOMER,'customer_name','customer_id',$customer_id,'where');
        	$this->_page_data['customer_id']=$get_customer; 
			$condition[] = array('where','d.customer_id',$customer_id);
	}else{	
    $this->_page_data['customer_id']='';
	}
	
	if ($product_category_id>"0") {
			$get_product_category=get_data_value(TABLE_PRODUCT_CATEGORY,'product_category_name','product_category_id',$product_category_id,'where');	
        	$this->_page_data['get_product_category']=$get_product_category; 
        	$this->_page_data['product_category_id']=$product_category_id; 
			$condition[] = array('where','p.product_category_id',$product_category_id);
	}else{	
    $this->_page_data['get_product_category']='';
    $this->_page_data['product_category_id']='';
	}
	

	 $this->_page_data['fields'] = array(
							'customer_name' => 'Customer Name',
							'address_line1' => 'Address',
							'city_name' => 'City',
							'company_branch_name' => 'Branch',
							'first_name' => 'Sales Person Name',
							'product_name' => 'Product Name',
							'product_type' => 'Product Type',
							'installation_date' => 'Installation Date',
							'price' => 'Price',
							'sales_type' => 'Sales Type',
							'stock_quantity' => 'Quantity',
							'refill_location' => 'Location',
							//'comments' => 'Comments',
						);
		
 	$condition[] = array('join',TABLE_CUSTOMER.' as c','c.customer_id = d.customer_id','inner');
 	$condition[] = array('join',TABLE_CUSTOMER_ADDRESSS.' as ca','ca.customer_id = c.customer_id','left');
 	$condition[] = array('join',TABLE_CITY.' as cy','cy.city_id = ca.city_id','left');
 	$condition[] = array('join',TABLE_COMPANY_BRANCH.' as b','b.company_branch_id = c.company_branch_id','left');
 	$condition[] = array('join',TABLE_PRODUCT.' as p','p.product_id = d.product_id','left');
 	$condition[] = array('join',TABLE_USERS.' as u','u.user_id = d.sales_person_id','left');
	$condition[] = array('join',TABLE_DISPENSER_REFILL_DETAILS.' as r','r.dispenser_sales_id = d.dispenser_sales_id','INNER');
	$condition[] = array('where','r.is_active','1');
	$condition[] = array('where','d.is_active','1');
	//$condition[] = array('where','d.dispenser_quantity >','0');
	
	if ($user_type=='Sales Person') {
		// $condition[] = array('join',TABLE_USER_TYPE.' as ut','ut.user_type_id = u.user_type_id ','left');
        // $condition[] = array('where','u.user_type_id',$user_type_id);
    }
    // if ($access_branch!='') {
    //      $condition[] = array('where_in','d.company_branch_id',$acb);
    // }

	 //$condition[] = array('where','u.user_type_id','6');
	
     // $selected_array=array(
     //                        'table_name'=>TABLE_DISPENSER_SALES.' as d',
     //                        'table_fields'=>'c.customer_name, ca.address_line1, ca.address_line2, cy.city_name, u.first_name, u.last_name, b.company_branch_name, p.product_name, p.product_type, d.sales_type,d.dispenser_quantity as stock_quantity, d.comments, r.refill_location,d.price AS total',
     //                        'condition'=>$condition,
     //                        'limit'=>'',
     //                        'order_by_field'=>'c.customer_name',
     //                        'group_by_field'=>'c.customer_name',
     //                        'offset'=>''
     //                     );
    $sort_columns = array('customer_name','product_name','first_name','last_name','city_name','product_type','refill_location','stock_quantity','installation_date','comments');
			$search_array=array(
							'limit' => $limit,
							'offset' => $offset,
							'sort_by' => $sort_by,
							'sort_type' => $sort_type,
							'sort_order' => $sort_order,
							'sort_columns'=> $sort_columns,
							'table_fields'=>'d.dispenser_sales_id,c.customer_name,c.customer_id,ca.address_line1, ca.address_line2, cy.city_name, u.first_name, u.last_name, b.company_branch_name, p.product_name, p.product_type, d.sales_type,d.dispenser_quantity as stock_quantity, d.comments, r.refill_location,d.price AS total,d.dispenser_sales_id,d.installation_date,r.dispenser_refill_details_id,p.product_id',
							'group_by_field'=>'d.dispenser_sales_id',
							'table_name'=>TABLE_DISPENSER_SALES.' as d',
							'cond'=> $condition,
							);     	
        	$results = $this->master_model->search($search_array);
		//dump($this->db->last_query());
			
			$selected_array=array(
                            'table_name'=>TABLE_DISPENSER_SALES.' as d',
                            'table_fields'=>'d.dispenser_sales_id',
                            'condition'=>$condition,
                            'limit'=>'',
                            'order_by_field'=>'',
                            'order'=>'DESC',
                            'group_by_field'=>'d.dispenser_sales_id',
                            'offset'=>''
                              );
			$sale_data = $this->master_model->select_data($selected_array);
		  
			$cnt_all_rows=count($sale_data);
			
			/* $search_full_array=array(
							'limit' => '',
							'offset' => '',
							'sort_by' => $sort_by,
							'sort_type' => $sort_type,
							'sort_order' => $sort_order,
							'sort_columns'=> $sort_columns,
							'table_fields'=>'c.customer_name, ca.address_line1, ca.address_line2, cy.city_name, u.first_name, u.last_name, b.company_branch_name, p.product_name, p.product_type, d.sales_type,d.dispenser_quantity as stock_quantity, d.comments, r.refill_location,d.price AS total,d.dispenser_sales_id,d.installation_date,r.dispenser_refill_details_id',
							'group_by_field'=>'',
							'table_name'=>TABLE_DISPENSER_SALES.' as d',
							'condition'=> $condition,
							);       	
        	$search_data=$this->master_model->select_data($search_full_array);			
		    $cnt_all_rows=count($search_data); */
			
			
			if(!empty($results)&& is_array($results))
			{
				$this->_page_data['sale_report'] = $results['rows'];
				$this->_page_data['num_results'] = $cnt_all_rows;
			}
			else
			{
				$results='';
			}
			$this->_page_data['offset'] = $offset;
			/// pagination
			$this->load->library('pagination');
			$config = array();
			

			$config['base_url'] = site_url("gc/master_sale_data_report/master_sales_data_report_list/$get_customer_id/$query_id/$sort_by/$sort_order/");
			
			$config['total_rows'] = $this->_page_data['num_results'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 8;
			$config['full_tag_open'] = '<span class=\'paging\'>';
			$config['full_tag_close'] = '</span>';
			$config['first_link'] = 'First';
			$config['prev_link'] = '<<';
			$config['next_link'] = '>>';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			
			$this->_page_data['pagination'] = $this->pagination->create_links();
			$this->_page_data['sort_by'] = $sort_by;
			$this->_page_data['sort_order'] = $sort_order;
			$this->_page_data['sort_type'] = $sort_type;
			$this->_page_data['get_customer_id'] = $get_customer_id;

			
			//$data['purpose'] = $purpose;
			$this->_page_data['query_id'] = $query_id;
  	    // if ($export=='1') {
  	    	
  	    // 	$this->load->view('gc/master_sale_data_report_list_excel.php',$this->_page_data);
       //  }
  	   // else{
			$this->load->view('gc/master_sale_data_report_list.php',$this->_page_data);
  	  // }

		
	}
	
	function search_sale_report()
	{


      
		$query_array = array(
						'branch_id' =>check_injections_filter($this->input->post('branch_id')),
						'from' => check_injections_filter($this->input->post('from')),
						'to' => check_injections_filter($this->input->post('to')),
						'sale' => check_injections_filter($this->input->post('sale')),
						'product_name_dispenser' => check_injections_filter($this->input->post('product_name_dispenser')),
						'customer_id' => check_injections_filter($this->input->post('customer_id')),
						'product_category_id' => check_injections_filter($this->input->post('product_category_id')),
						
					);
		$this->load->library('NF_Query_search');
		$query_id = $this->nf_query_search->save_query($query_array);			
		
		redirect('gc/master_sale_data_report/master_sales_data_report_list/0/'.$query_id);		
	}
	function excel_sale(){
		//error_reporting(E_ALL);
		// $export=$this->input->post('excel_export');
		// 	if ($export=='1') {
					$branch_id=$this->input->post('excel_branch_id');
					$from=$this->input->post('excel_from');
					$to=$this->input->post('excel_to');
					$sale=$this->input->post('excel_sale');
					$product_name_dispenser=$this->input->post('excel_product_name_dispenser');
					$customer_id=$this->input->post('excel_customer_id');
					$product_category_id=$this->input->post('excel_product_category_id');

					
	        // }else{
	        // 	$excel_branch_id='';
	        // 	$excel_from='';
	        // 	$excel_to='';
	        // 	$excel_sale='';
	        // 	$excel_product_name_dispenser='';
	        // 	$excel_customer_id='';
	        // }
	// $this->_page_data['get_dispenser']=''; 
	// $this->_page_data['from']='';
	// $this->_page_data['to']='';
	// $this->_page_data['get_sale_person_firstname']=''; 
 //    $this->_page_data['get_sale_person_lastname']=''; 
 //    $this->_page_data['customer_id']=''; 
	  
	if ($branch_id>"0") {  
    	$company_branch_name=get_data_value(TABLE_COMPANY_BRANCH,'company_branch_name','company_branch_id',$branch_id,'where');
		$this->_page_data['company_branch_name']=$company_branch_name;   	
		$condition[] = array('where','d.company_branch_id',$branch_id);
	}else{
			$this->_page_data['company_branch_name']=''; 
	}
	if ($from>"0" and $to<"0") { 
			$this->_page_data['from']=$from;       	
			//$condition[] = array('where','r.refill_date',$from);
			$condition[] = array('where','d.installation_date',date('Y-m-d', strtotime($from)));
	}else{
	$this->_page_data['from']='';  	
	}
	
	if ($to>"0" and $from<"0") {
        	$this->_page_data['to']=$to;  	
			//$condition[] = array('where','r.next_refill_date',$to);
			$condition[] = array('where','d.installation_date',date('Y-m-d', strtotime($to)));
	}else{
		$this->_page_data['to']='';  	
	}
	
	if($from>"0" and $to>"0"){
		$this->_page_data['to']=$to; 
		$this->_page_data['from']=$from;  
		$query_part='d.installation_date BETWEEN "'.date('Y-m-d', strtotime($from)).'" AND "'.date('Y-m-d', strtotime($to)).'"';
			//$condition[] = array('where','r.refill_date',$from);
			$condition[] = array('where',$query_part);
	}else{
		$this->_page_data['from']='';  
	   $this->_page_data['to']=''; 
    
	}
	if ($sale>"0") {
        	$get_first_name=get_data_value(TABLE_USERS,'first_name','user_id',$sale,'where');
        	$get_last_name=get_data_value(TABLE_USERS,'last_name','user_id',$sale,'where');
        	$this->_page_data['get_sale_person_firstname']=$get_first_name; 
        	$this->_page_data['get_sale_person_lastname']=$get_last_name; 
			$condition[] = array('where','u.user_id',$sale);
	}else{
		$this->_page_data['get_sale_person_firstname']=''; 
       $this->_page_data['get_sale_person_lastname']=''; 
	}
	if ($product_name_dispenser>"0") {
        	$get_dispenser=get_data_value(TABLE_PRODUCT,'product_name','product_id',$product_name_dispenser,'where');
        	$this->_page_data['get_dispenser']=$get_dispenser; 
			$condition[] = array('where','d.product_id',$product_name_dispenser);
	}else{
		$this->_page_data['get_dispenser']='';
	}
	if ($product_category_id>"0") {
        	$get_product_category=get_data_value(TABLE_PRODUCT_CATEGORY,'product_category_name','product_category_id',$product_category_id,'where');
        	$this->_page_data['get_product_category']=$get_product_category; 
			$condition[] = array('where','p.product_category_id',$product_category_id);
	}else{
		$this->_page_data['product_category_id']='';
		$this->_page_data['get_product_category']='';
	}
	if ($customer_id>"0") {
        	$get_customer=get_data_value(TABLE_CUSTOMER,'customer_name','customer_id',$customer_id,'where');
        	$this->_page_data['customer_id']=$get_customer; 
			$condition[] = array('where','d.customer_id',$customer_id);
	}else{	
    $this->_page_data['customer_id']='';
	}

 	$condition[] = array('join',TABLE_CUSTOMER.' as c','c.customer_id = d.customer_id','inner');
 	$condition[] = array('join',TABLE_CUSTOMER_ADDRESSS.' as ca','ca.customer_id = c.customer_id','left');
 	$condition[] = array('join',TABLE_CITY.' as cy','cy.city_id = ca.city_id','left');
 	$condition[] = array('join',TABLE_COMPANY_BRANCH.' as b','b.company_branch_id = c.company_branch_id','left');
 	$condition[] = array('join',TABLE_PRODUCT.' as p','p.product_id = d.product_id','left');
 	$condition[] = array('join',TABLE_USERS.' as u','u.user_id = d.sales_person_id','left');
	$condition[] = array('join',TABLE_DISPENSER_REFILL_DETAILS.' as r','r.dispenser_sales_id = d.dispenser_sales_id','inner');
	// if ($user_type=='Sales Person') {
	// 	 $condition[] = array('join',TABLE_USER_TYPE.' as ut','ut.user_type_id = u.user_type_id ','inner');
 //         $condition[] = array('where','u.user_type_id',$user_type_id);
 //    }
 //    if ($access_branch!='') {
 //         $condition[] = array('where_in','b.company_branch_id',$acb);
 //    }
    $selected_array=array(
                            'table_name'=>TABLE_DISPENSER_SALES.' as d',
                            'table_fields'=>'c.customer_name, ca.address_line1, ca.address_line2, cy.city_name, u.first_name, u.last_name, b.company_branch_name,d.installation_date, p.product_name, p.product_type, d.sales_type,d.dispenser_quantity as stock_quantity, d.comments, r.refill_location,d.price AS total',
                            'condition'=>$condition,
                            'limit'=>'',
                            'order_by_field'=>'c.customer_name',
                            'group_by_field'=>'d.dispenser_sales_id',
                            'offset'=>''
                         );
     $sale_report['sale_report'] = $this->master_model->select_data($selected_array);
     if(!empty($sale_report['sale_report'] )&& is_array($sale_report['sale_report']))
		{
			$this->_page_data['sale_report'] = $sale_report['sale_report'];		 	
  	    }
	       $this->load->view('gc/master_sale_data_report_list_excel.php',$this->_page_data);
	}

	function fetch_branch_name(){
      admin_auth();
      $access_branch= $this->session->userdata('access_branch');     
       if ($access_branch!='') {
       		$acb =  explode(',',$access_branch);
          $condition[] = array('where_in','company_branch_id',$acb);
       }
       $condition[] = array('where','is_active','1');
       $selected_array = array( 
                         'table_name' =>TABLE_COMPANY_BRANCH,
                         'table_fields' => 'company_branch_id,company_branch_name,is_active',
                         'condition'=>$condition,
                        'order_by_field'=>'company_branch_name',
						'order'=>'ASC', 
                         'limit'=>'',
                         'offset'=>''
		);
		
		return $this->master_model->select_data($selected_array);
		//dump($this->db->last_query());
	
    }
    function get_sale_person()
	{

		$branch_id=$this->input->post('branch_id');
		//$access_branch= $this->session->userdata('access_branch');
       
	    $cond = array();
        $cond[] = array('join',TABLE_USERS.' as b','b.user_id = a.sales_person_id','inner');
		$cond[] = array('where','a.company_branch_id',$branch_id);
		// if ($access_branch!='') {
  //            $salse_person_cond[]  = array('where_in','b.company_branch_id',$access_branch);
  //       }
		//$cond[] = array('where','is_active','1');
		$select_array = array(
								'table_name'=>TABLE_DISPENSER_SALES.' as a',
								'table_fields'=>'b.first_name, b.last_name,b.user_id',
								'condition'=>$cond,
								'sort_by'=>'b.first_name',
								'order_by_field'=>'b.first_name',
								'group_by_field'=>'b.first_name,b.last_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$salse_person_data = $this->master_model->select_data($select_array);
		$output='<option value="">Sales By</option>';
		
		foreach($salse_person_data as $row)
		{
		  $output .= '<option value='.$row['user_id'].'>'.$row['first_name']." ".$row['last_name'].'</option>';

		}
          echo $output;	
    }
    function get_dispenser()
	{
        
		$user_id=$this->input->post('user_id');       
	    $cond = array();
        $cond[] = array('join',TABLE_PRODUCT.' as b','b.product_id = a.product_id','inner');

		$cond[] = array('where','b.is_active','1');
		//$cond[] = array('where','b.product_type','dispenser');
		$cond[] = array('where_in','.b.product_type',array('Dispenser','Diffuser','Oil'));
		if($user_id>0)
		{
			$cond[] = array('where','a.sales_person_id',$user_id);
		}
		$select_array = array(
								'table_name'=>TABLE_DISPENSER_SALES.' as a',
								'table_fields'=>'b.product_name,b.product_id',
								'condition'=>$cond,
								'sort_by'=>'b.product_name',
								'order_by_field'=>'b.product_name',
								'group_by_field'=>'b.product_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$salse_person_data = $this->master_model->select_data($select_array);
		//dump($this->db->last_query());
		$output='<option value="">Select Dispenser Name</option>';
		
		foreach($salse_person_data as $row)
		{
		  $output .= '<option value='.$row['product_id'].'>'.$row['product_name'].'</option>';

		}
          echo $output;	
    }
	
	function get_tacking_numbers()
	{
		$dispenser_refill_details_id=$this->input->post('dispenser_refill_details_id');       
	    $cond = array();
		$cond[] = array('where','a.dispenser_refill_details_id',$dispenser_refill_details_id);
		//$cond[] = array('where','a.is_returned','no');
		$select_array = array(
								'table_name'=>TABLE_TRACKING_DETAILS.' as a',
								'table_fields'=>'a.tracking_number',
								'condition'=>$cond,
								'sort_by'=>'a.dispenser_refill_details_id',
								'order_by_field'=>'a.dispenser_refill_details_id',
								'group_by_field'=>'a.tracking_number',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$salse_person_data = $this->master_model->select_data($select_array);
		//dump($this->db->last_query());		
		foreach($salse_person_data as $row)
		{
			$tracking_number=$this->encrypt->encode($row['tracking_number']);
		  $output .= ' , <a href="'.base_url().'index.php/gc/refill_track/list_refill_track/'.$tracking_number.'">'.$row['tracking_number'].'</a>';

		}
          echo trim($output," , ");	
		
	}
	function customer(){
		$access_branch= $this->session->userdata('access_branch');
		$acb =  explode( ',',$access_branch);
			
    	$customer_cond = array();
		$customer_cond[] = array('where','is_active','1');
		$customer_cond[] = array('where_in','company_branch_id',$acb);
		$select_array = array(
								'table_name'=>TABLE_CUSTOMER,
								'table_fields'=>'customer_name,customer_id',
								'condition'=>$customer_cond,
								'sort_by'=>'customer_id',
								'order_by_field'=>'customer_name',
								'group_by_field'=>'customer_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$customer_data=$this->master_model->select_data($select_array);
		//dump($this->db->last_query());
		$customer_cnt_data=count($customer_data);
		if($customer_cnt_data > 0)
		{
			$output='';
		 		foreach($customer_data as $row)
                     {
                  		 // $output .= '<option value="'.$row->state_id.'">'.$row->state_name.'</option>';
                  		  $output .= '<option value='.$row['customer_id'].'>'.$row['customer_name'].'</option>';

                     }

          echo $output;
		}
    }

  function refill_sale_data_report($id,$customer_id,$dispenser_sales_id){ 

    admin_auth();
   //error_reporting(E_ALL);

    //$limit = LIMIT_PER_PAGE;

    //$p_id = $this->encrypt->decode($id);
   // $c_id = $this->encrypt->decode($customer_id);
    //$dispenser_sales_id_search = $this->encrypt->decode($dispenser_sales_id);

	$p_id=encrypt_decrypt_parameters($id,'decrypt');
	$c_id=encrypt_decrypt_parameters($customer_id,'decrypt');
	$dispenser_sales_id_search=encrypt_decrypt_parameters($dispenser_sales_id,'decrypt');
  
    //echo $c_id;
    $customer_name=get_data_value(TABLE_CUSTOMER,'customer_name','customer_id',$c_id,'where');
	$this->page_data['customer_name']=$customer_name;
	$dispenser_product_name=get_data_value(TABLE_PRODUCT,'product_name','product_id',$p_id,'where');

	//$stock_quantity=get_data_value(TABLE_PRODUCT,'stock_quantity','product_id',$p_id,'where');
	$this->page_data['dispenser_product_name']=$dispenser_product_name;
	//$this->page_data['dispenser_quantity']=$stock_quantity;      
    //$limit = LIMIT_PER_PAGE;
   
   // $p_ids[]=array();
	$this->page_data['page_title']="List of Refill Transactions";
	$this->page_data['bredcrum']=array(
			                            'Reports'=>'#',
			                            'Sales Report'=>'#',
										'List of Refill Transactions'=>'#',
									
										);	
	$condition[] = array('where','product_id',$p_id);
	//$condition[] = array('where','product_type','Refill');

 //    }
    $selected_array=array(
                            'table_name'=>TABLE_PRODUCT,
                            'table_fields'=>'product_id,product_name as Refill,refill_ids',
                            'condition'=>$condition,
                            'limit'=>'',
                            'order_by_field'=>'product_name',
                            'group_by_field'=>'product_name',
                            'offset'=>''
                         );
     $sale_report['sale_report'] = $this->master_model->select_data($selected_array);
     
    
     // foreach ($sale_report['sale_report'] as $value) {

     //  	$p_ids=$value['refill_ids'];	
     //  }
     //  if ($p_ids!='' && $c_id!='') {
     //  	$p_id = explode(",", $p_ids);
     //  	$con[] = array('where_in','a.product_id',$p_id);
     // }
     
      $con[] = array('where','a.dispenser_sales_id',$dispenser_sales_id_search);  	   
      $con[] = array('join',TABLE_PRODUCT. ' as p','p.product_id=a.product_id','inner');
      $con[] = array('join',TABLE_DISPENSER_SALES. ' as b','b.dispenser_sales_id=a.dispenser_sales_id','inner');
     // $con[] = array('join',TABLE_TRACKING_DETAILS. ' as 	c','c.dispenser_refill_details_id=a.dispenser_refill_details_id','left');
		$con[] = array('where','a.is_active','1'); 
		$con[] = array('where','b.is_active','1'); 
      // $this->page_data['fields'] = array(
						// 	'product_name' => 'Refill Name',
						// 	'quantity' => 'Quantity',
						// 	'duration_days' => 'Programming Duration Days',
						// 	'refill_date' => 'Refill Date',
						// 	'next_refill_date' => 'Next Due Date',
						// 	'comments' => 'Comments',
						// );

  	    

      $selected_array=array(
                            'table_name'=>TABLE_DISPENSER_REFILL_DETAILS.' as a',
                            'table_fields'=>'a.dispenser_refill_details_id,a.product_id,a.duration_days,a.quantity,a.refill_date,a.next_refill_date,a.comments,p.product_name,a.stop_refill,a.dispenser_refill_details_id,unit_of_measurment',
                            'condition'=>$con,
                            'limit'=>'',
                            'order_by_field'=>'refill_date',
                            'group_by_field'=>'',
                            'order'=>'DESC',
                            'offset'=>''
                         );
    	$refill_sale_report['refill_sale_report'] = $this->master_model->select_data($selected_array);
    	//dump($this->db->last_query());
   		if(!empty($refill_sale_report['refill_sale_report'])&& is_array($refill_sale_report['refill_sale_report'])){

   		  $this->page_data['refill_transaction'] = $refill_sale_report['refill_sale_report'];
    	}else{
          $refill_transaction='';
    	}
   		$this->load->view('gc/refill_sale_data_report.php',$this->page_data);
	}
  //}
}