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

	function __construct()	{
		parent::__construct();
		 //error_reporting(E_ALL);
	}

	function index()	{	
		$this->refill_history();
	}
	function refill_history(){
		$this->_page_data['page_title']="Refill History";
		$this->_page_data['bredcrum']=array(
			                            'Reports'=>'#',
			                            'Refill History'=>'#',
										//'Manage Customer Master'=>'#',
									
										);

		$country_cond = array();
		$country_cond[] = array('where','is_active','1');
		$select_array = array(
								'table_name'=>TABLE_COUNTRY,
								'table_fields'=>'country_name,country_id',
								'condition'=>$country_cond,
								'sort_by'=>'country_id',
								'order_by_field'=>'country_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$customer_data=$this->master_model->select_data($select_array);
		$customer_cnt_data=count($customer_data);
		$this->_page_data['country_array'] = '';
		if($customer_cnt_data > 0)
		{
			foreach($customer_data as $cKey => $cValue)
			{
				$this->_page_data['country_array'][''] = 'Select Country Name';
				$this->_page_data['country_array'][$cValue['country_id']] = $cValue['country_name'];
			}
		}
		
		$customer_cond = array();
		$customer_cond[] = array('where','is_active','1');
		$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',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$customer_data=$this->master_model->select_data($select_array);
		$customer_cnt_data=count($customer_data);
		$this->_page_data['customer_array'] = '';
		if($customer_cnt_data > 0)
		{
			foreach($customer_data as $cKey => $cValue)
			{
				$this->_page_data['customer_array'][''] = 'Select Customer Name';
				$this->_page_data['customer_array'][$cValue['customer_id']] = $cValue['customer_name'];
			}
		
		}
		$refill_cond = array();
		$refill_cond[] = array('where','is_active','1');
		$refill_cond[] = array('where','product_type','dispenser');
		$select_array = array(
								'table_name'=>TABLE_PRODUCT,
								'table_fields'=>'product_name,product_id',
								'condition'=>$refill_cond,
								'sort_by'=>'product_id',
								'order_by_field'=>'product_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$refill_data=$this->master_model->select_data($select_array);
		$refill_cnt_data=count($refill_data);
		$this->_page_data['refill_array'] = '';
		if($refill_cnt_data > 0)
		{
			foreach($refill_data as $cKey => $cValue)
			{
				$this->_page_data['refill_array'][''] = 'Select Dispenser Name';
				$this->_page_data['refill_array'][$cValue['product_id']] = $cValue['product_name'];
			}
		}
		$this->load->view('gc/refill_history.php',$this->_page_data);
	}
	function refill_history_report(){
		$this->_page_data['page_title']="Refill History Report";
		$this->_page_data['bredcrum']=array(
										'Reports'=>'#',
										 'Refill History'=>'#',
			                            'Refill History Report'=>'#',
										//'Manage Customer Master'=>'#',
									
										);
		$country_id = check_injections_filter($this->input->post('country_id'));
		$state_id = check_injections_filter($this->input->post('state_id'));
		$city_id = check_injections_filter($this->input->post('city_id'));
		$customer_id = check_injections_filter($this->input->post('customer_id'));
		$dispenser_id = check_injections_filter($this->input->post('dispenser_id'));



		if($dispenser_id!="")
		{
			$product_name=get_data_value(TABLE_PRODUCT,'product_name','product_id',$dispenser_id,'where');
			$this->_page_data['dis_product_name']=$product_name;
			$condition[] = array('where','p.product_id',$dispenser_id);
		}

		if ($customer_id>"0") {
        		
			$customer_name=get_data_value(TABLE_CUSTOMER,'customer_name','customer_id',$customer_id,'where');
			$this->_page_data['dis_customer_name']=$customer_name;
			$condition[] = array('where','c.customer_id',$customer_id);
		}

		if ($country_id>"0") {
        	
			$country_name=get_data_value(TABLE_COUNTRY,'country_name','country_id',$country_id,'where');
			$this->_page_data['dis_country_name']=$country_name;
			$condition[] = array('where','ca.country_id',$country_id);
		}
		if ($state_id>"0") {
        		
			$state_name=get_data_value(TABLE_STATE,'state_name','state_id',$state_id,'where');
			$this->_page_data['dis_state_name']=$state_name;
			$condition[] = array('where','ca.state_id',$state_id);
		}
		if ($city_id>"0") {
        	
			$city_name=get_data_value(TABLE_CITY,'city_name','city_id',$city_id,'where');
			$this->_page_data['dis_city_name']=$city_name;
			$condition[] = array('where','ca.city_id',$city_id);
		}
		
		// $condition[] = array('join',TABLE_DISPENSER_REFILL_DETAILS.' as a','b.dispenser_sales_id = a.dispenser_sales_id','inner');

		$condition[] = array('join',TABLE_PRODUCT.' as p1','p1.dispenser_name = p.product_id','inner');
		$condition[] = array('join',TABLE_DISPENSER_REFILL_DETAILS.' as r','r.product_id = p1.product_id','inner');
		$condition[] = array('join',TABLE_DISPENSER_SALES.' as d','d.dispenser_sales_id = r.dispenser_sales_id','inner');

        $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','inner');
        
       //$condition[] = array('join',TABLE_CITY.' as e','d.city_id = e.city_id','inner');
        $selected_array=array(
                              'table_name'=>TABLE_PRODUCT.' as p',
                              'table_fields'=>'p1.product_name as refill,r.quantity,r.duration_days,r.refill_date,r.next_refill_date,r.refill_location,d.dispenser_sales_id,d.comments',
                               'condition'=>$condition,
                               'limit'=>'',
                               //'order_by_field'=>'a.branch_salable_geography_id',
                               //'group_by_field'=>'a.customer_name',
                               'offset'=>''
                              );
        $refill_history['refill_history'] = $this->master_model->select_data($selected_array);
        if(!empty($refill_history['refill_history'] )&& is_array($refill_history['refill_history'] )){


				  $this->_page_data['refill_history'] = $refill_history['refill_history'];
		 	
  	    } 

		$this->load->view('gc/refill_history_list.php',$this->_page_data);
	}
	function get_state()
	{
		$country_id=$this->input->post('country_id');
       
	    $salse_person_cond = array();
		$salse_person_cond[] = array('where','country_id',$country_id);
		$salse_person_cond[] = array('where','is_active','1');
		$select_array = array(
								'table_name'=>TABLE_STATE,
								'table_fields'=>'state_id,state_name',
								'condition'=>$salse_person_cond,
								'sort_by'=>'state_name',
								'order_by_field'=>'state_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$salse_person_data = $this->master_model->select_data($select_array);
		$output='<option value="">Select State</option>';
		
		foreach($salse_person_data as $row)
		{
		  $output .= '<option value='.$row['state_id'].'>'.$row['state_name'].'</option>';

		}
          echo $output;	
    }
	
	function get_city()
	{
		$state_id=$this->input->post('state_id');
       
	    $salse_person_cond = array();
		$salse_person_cond[] = array('where','state_id',$state_id);
		$salse_person_cond[] = array('where','is_active','1');
		$select_array = array(
								'table_name'=>TABLE_CITY,
								'table_fields'=>'city_id,city_name',
								'condition'=>$salse_person_cond,
								'sort_by'=>'city_name',
								'order_by_field'=>'city_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$salse_person_data = $this->master_model->select_data($select_array);
		$output='<option value="">Select City</option>';
		
		foreach($salse_person_data as $row)
		{
		  $output .= '<option value='.$row['city_id'].'>'.$row['city_name'].'</option>';

		}
          echo $output;	
    }
}