<?php	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customer_master 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', '');
            }

	}

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

	function list_customer_master($query_id = 0, $sort_by = 'customer_id', $sort_order = 'desc', $offset = 0,$sort_type=0) {
		//error_reporting(E_ALL);
		admin_auth();
		$submit=check_injections_filter($this->input->post('search')); 
		$access_branch= $this->session->userdata('access_branch');
		
		//$export = check_injections_filter($this->input->post('excel'));
		$limit = LIMIT_PER_PAGE;
		$this->page_data['page_title']="List of Customers";
		$this->page_data['bredcrum']=array(
			                            'Customer'=>'#',
										'Manage Customer'=>'#',
										'List Customers'=>'#',
										);
		if ($access_branch!='') 
		{
			 $acb =  explode( ',',$access_branch);
			 $condition[] = array('where_in','branch.company_branch_id',$acb);
		}
        $condition[] = array('join',TABLE_COMPANY_BRANCH.' as branch','branch.company_branch_id=a.company_branch_id');
        $condition[] = array('join',TABLE_CUSTOMER_CONTACT.' as contact','contact.customer_id=a.customer_id','left');
        $condition[] = array('join',TABLE_CUSTOMER_ADDRESSS.' as adr','adr.customer_id=a.customer_id','left');
        $condition[] = array('join',TABLE_CITY.' as city','city.city_id = adr.city_id','left');
       // $condition[] = array('join',TABLE_STATE.' as state','state.state_id = adr.state_id','left');
       // $condition[] = array('join',TABLE_COUNTRY.' as country','country.country_id = adr.country_id','left');
        $condition[] = array('join',TABLE_USERS.' as sales_person','sales_person.user_id = a.sales_person_id','left');

        $this->page_data['cutomer_sale_person']='';
        $this->page_data['customer_name']='';
        $this->page_data['cutomer_mobile']='';
        $this->page_data['cutomer_email']='';
        $this->page_data['cutomer_address']='';
        $this->page_data['cutomer_city']='';
        $this->page_data['cutomer_email']='';
        $this->page_data['cutomer_city']='';
        $this->page_data['branch_id']='';
        $this->page_data['branch_name']='';


    // if(isset($submit) && !empty($submit))
		//{
			
		$this->load->library('NF_Query_search');
		$this->nf_query_search->load_query($query_id);
		$query_array = array(
							'cutomer_sale_person' =>  $this->input->get('cutomer_sale_person'),
							'customer_name' => $this->input->get('customer_name'),
							'cutomer_mobile' => $this->input->get('cutomer_mobile'),
							'cutomer_email' => $this->input->get('cutomer_email'),
							'cutomer_address' => $this->input->get('cutomer_address'),
							'cutomer_city' => $this->input->get('cutomer_city'),
							'branch_id' => $this->input->get('branch_id'),
							'sale_status_id' => $this->input->get('sale_status_id'),
							);
		$cutomer_sale_person=check_injections_filter($query_array['cutomer_sale_person']);
		$customer_name=check_injections_filter($query_array['customer_name']);
		$cutomer_mobile=check_injections_filter($query_array['cutomer_mobile']);
		$cutomer_email=check_injections_filter($query_array['cutomer_email']);
		$cutomer_address=check_injections_filter($query_array['cutomer_address']);
		$cutomer_city=check_injections_filter($query_array['cutomer_city']);
		$branch_id=check_injections_filter($query_array['branch_id']);
		$sale_status_id=check_injections_filter($query_array['sale_status_id']);
		
	$this->page_data['excel_cutomer_sale_person']=$cutomer_sale_person;
    $this->page_data['excel_customer_name']=$customer_name;
    $this->page_data['excel_cutomer_mobile']=$cutomer_mobile;
    $this->page_data['excel_cutomer_email']=$cutomer_email;
    $this->page_data['excel_cutomer_address']=$cutomer_address;
    $this->page_data['excel_cutomer_city']=$cutomer_city;
    $this->page_data['excel_branch_id']=$branch_id;
    $this->page_data['excel_sale_status_id']=$sale_status_id;

		 /* $cutomer_sale_person=check_injections_filter($this->input->post('cutomer_sale_person'));
         $customer_name=check_injections_filter($this->input->post('customer_name'));
         $cutomer_mobile=check_injections_filter($this->input->post('cutomer_mobile'));
         $cutomer_email=check_injections_filter($this->input->post('cutomer_email'));
         $cutomer_address=check_injections_filter($this->input->post('cutomer_address'));
         $cutomer_city=check_injections_filter($this->input->post('cutomer_city'));
         $branch_id=check_injections_filter($this->input->post('branch_id')); */

        if ($cutomer_sale_person!='') {
        	$condition[]=array('like','sales_person.first_name',$cutomer_sale_person);
        	$this->page_data['cutomer_sale_person']=$cutomer_sale_person;
        }else{
        	$this->page_data['cutomer_sale_person']='';
        }
        if ($customer_name!='') {
        	//$customer_name=get_data_value(TABLE_CUSTOMER,'customer_name','customer_id',$customer_id,'where');
        	$condition[]=array('like','a.customer_name',$customer_name);
        	//$this->page_data['customer_name']=$customer_name;
        	$this->page_data['customer_name']=$customer_name;
        }else{
        	$this->page_data['customer_name']='';
        }
        if ($cutomer_mobile!='') {
        	$condition[]=array('like','contact.mobile_no',$cutomer_mobile);
        	$this->page_data['cutomer_mobile']=$cutomer_mobile;
        }else{
        	$this->page_data['cutomer_mobile']='';
        }
        if ($cutomer_email!='') {
        	$condition[]=array('like','contact.email_id',$cutomer_email);
        	$this->page_data['cutomer_email']=$cutomer_email;
        }else{
        	$this->page_data['cutomer_email']='';
        }
        if ($cutomer_address!='') {
        	$condition[]=array('like','adr.address_line1',$cutomer_address);
        	$this->page_data['cutomer_address']=$cutomer_address;
        }else{
        	$this->page_data['cutomer_email']='';
        }
        if ($cutomer_city!='') {
        	$condition[]=array('like','city.city_name',$cutomer_city);
        	$this->page_data['cutomer_city']=$cutomer_city;
        }else{
        	$this->page_data['cutomer_city']='';
        }
        if ($branch_id!='') {
        	$branch_name=get_data_value(TABLE_COMPANY_BRANCH,'company_branch_name','company_branch_id',$branch_id,'where');
        	$condition[]=array('where','a.company_branch_id',$branch_id);
        	$this->page_data['branch_id']=$branch_id;
        	$this->page_data['branch_name']=$branch_name;
        	
        }else{
        	$this->page_data['branch_id']='';
        	$this->page_data['branch_name']='';
        }
         if ($sale_status_id!='') {
        	if ($sale_status_id=='1') {
        		$condition[]=array('where','a.sales_status','pre_sales');
        		$this->page_data['sale_status_id']=$sale_status_id;
        	}else if($sale_status_id=='2'){
        	    $condition[]=array('where','a.sales_status','won');
        		$this->page_data['sale_status_id']=$sale_status_id;
        	}
        	
        }else{
        	$this->page_data['sale_status_id']='';
        
        }
       
	//}
	
		$this->page_data['fields'] = array(
							'company_branch_name' => 'Branch Company Name',
							'customer_name' => 'Customer',
							'contact_person_name' => 'Customer Contact Name',
							'first_name' => 'Sales Person Name',
							'city_name' => 'City',
							'address_line1' => 'Address',
							'postal_code' => 'Pincode',
							'mobile_no' => 'Contact Number',
							'email_id' => 'Email Id',
							'date_of_enrollment' => 'Date of Enrollment',
							'sales_status' => 'Sales Status',
						);

        
         $sort_columns = array('company_branch_name','customer_name','city_name','date_of_enrollment','first_name','postal_code','email_id','mobile_no','sales_status','contact_person_name');
			$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'=>'a.customer_id, branch.company_branch_name, a.is_active,contact.contact_person_name, contact.email_id, city.city_name,a.customer_name,a.date_of_enrollment,a.customer_pan_no,a.customer_gst_no,adr.address_line1,adr.address_line2,adr.postal_code,contact.contact_person_name,contact.mobile_no,contact.std_code,contact.designation,sales_person.first_name,sales_person.last_name,a.sales_status',
							'group_by_field'=> 'a.customer_id',
							'table_name'=>TABLE_CUSTOMER.' as a',
							'cond'=> $condition,
							);

        //$customer['customer'] = $this->master_model->select_data($selected_array);
		$results = $this->master_model->search($search_array);
		//dump($this->db->last_query());
			// $search_full_array=array(
			// 				'limit' =>'',
			// 				'offset' =>'',
			// 				'sort_by' => $sort_by,
			// 				'sort_type' => $sort_type,
			// 				'sort_order' => $sort_order,
			// 				'sort_columns'=> $sort_columns,
			// 				'table_fields'=>'a.customer_id, branch.company_branch_name, a.is_active,contact.contact_person_name, contact.email_id, city.city_name,a.customer_name,a.date_of_enrollment,a.customer_pan_no,a.customer_gst_no,adr.address_line1,adr.address_line2,adr.postal_code,contact.contact_person_name,contact.mobile_no,contact.std_code,contact.designation,sales_person.first_name',
			// 				'group_by_field'=> 'a.customer_id',
			// 				//'order_by_field'=>'branch.company_branch_name,a.customer_name',
			// 				'table_name'=>TABLE_CUSTOMER.' as a',
			// 				'condition'=> $condition,
			// 				);

			//$search_data=$this->master_model->select_data($search_full_array);			
		    //$cnt_all_rows=count($search_data);
		    $cnt_all_rows=get_cust_count($condition);




		//dump($this->db->last_query());
		
		//$cnt_all_rows=$this->db->count_all_results(TABLE_CUSTOMER);
		if(!empty($results)&& is_array($results))
		{
			$this->page_data['customer'] = $results['rows'];
			$this->page_data['num_results'] = $cnt_all_rows;
		}
		else
		{
			$results='';
		}
		
		//echo "#######".$this->page_data['num_results'];
		$this->page_data['offset'] = $offset;
			/// pagination
			$this->load->library('pagination');
			$config = array();
			$config['base_url'] = site_url("gc/customer_master/list_customer_master/$query_id/$sort_by/$sort_order");
			$config['total_rows'] = $this->page_data['num_results'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 7;
			$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;
			//$data['purpose'] = $purpose;
			$this->page_data['query_id'] = $query_id;
  	    
		// if ($export=='1') {
  // 	    	$this->load->view('gc/customer_list_excel',$this->page_data);
  // 	    }else{
			$this->load->view('gc/customer_master_list',$this->page_data);
  	    //}
	
	}
	
	function search_customer()
	{

		$query_array = array(
						'branch_id' =>check_injections_filter($this->input->post('branch_id')),
						'customer_name' => check_injections_filter($this->input->post('customer_name')),
						'cutomer_mobile' => check_injections_filter($this->input->post('cutomer_mobile')),
						'cutomer_email' => check_injections_filter($this->input->post('cutomer_email')),
						'cutomer_address' => check_injections_filter($this->input->post('cutomer_address')),
						'cutomer_city' => check_injections_filter($this->input->post('cutomer_city')),
						'cutomer_sale_person' => check_injections_filter($this->input->post('cutomer_sale_person')),
						'sale_status_id' => check_injections_filter($this->input->post('sale_status_id')),
					);
		$this->load->library('NF_Query_search');
		$query_id = $this->nf_query_search->save_query($query_array);			
		
		redirect('gc/customer_master/list_customer_master/'.$query_id);		
	}
	
	function excel_list(){

	

    $cutomer_sale_person=check_injections_filter($this->input->post('excel_cutomer_sale_person'));
    $customer_name=check_injections_filter($this->input->post('excel_customer_name'));
    $cutomer_mobile=check_injections_filter($this->input->post('excel_cutomer_mobile'));
    $cutomer_email=check_injections_filter($this->input->post('excel_cutomer_email'));
    $cutomer_address=check_injections_filter($this->input->post('excel_cutomer_address'));
    $cutomer_city=check_injections_filter($this->input->post('excel_cutomer_city'));
    $branch_id=check_injections_filter($this->input->post('excel_branch_id'));
    $sale_status_id=check_injections_filter($this->input->post('excel_sale_status_id'));
	
	$access_branch= $this->session->userdata('access_branch');
								
		if ($access_branch!='') 
		{
			 $acb =  explode( ',',$access_branch);
			 $condition[] = array('where_in','branch.company_branch_id',$acb);
		}

    if ($cutomer_sale_person!='') {
        	$condition[]=array('like','sales_person.first_name',$cutomer_sale_person);
        	$this->page_data['cutomer_sale_person']=$cutomer_sale_person;
        }else{
        	$this->page_data['cutomer_sale_person']='';
        }
        if ($customer_name!='') {
        	//$customer_name=get_data_value(TABLE_CUSTOMER,'customer_name','customer_id',$customer_id,'where');
        	$condition[]=array('like','a.customer_name',$customer_name);
        	//$this->page_data['customer_name']=$customer_name;
        	$this->page_data['customer_name']=$customer_name;
        }else{
        	$this->page_data['customer_name']='';
        }
        if ($cutomer_mobile!='') {
        	$condition[]=array('like','contact.mobile_no',$cutomer_mobile);
        	$this->page_data['cutomer_mobile']=$cutomer_mobile;
        }else{
        	$this->page_data['cutomer_mobile']='';
        }
        if ($cutomer_email!='') {
        	$condition[]=array('like','contact.email_id',$cutomer_email);
        	$this->page_data['cutomer_email']=$cutomer_email;
        }else{
        	$this->page_data['cutomer_email']='';
        }
        if ($cutomer_address!='') {
        	$condition[]=array('like','adr.address_line1',$cutomer_address);
        	$this->page_data['cutomer_address']=$cutomer_address;
        }else{
        	$this->page_data['cutomer_address']='';
        }
        if ($cutomer_city!='') {
        	$condition[]=array('like','city.city_name',$cutomer_city);
        	$this->page_data['cutomer_city']=$cutomer_city;
        }else{
        	$this->page_data['cutomer_city']='';
        }
        if ($branch_id!='') {
        	$branch_name=get_data_value(TABLE_COMPANY_BRANCH,'company_branch_name','company_branch_id',$branch_id,'where');
        	$condition[]=array('where','a.company_branch_id',$branch_id);
        	$this->page_data['branch_id']=$branch_id;
        	$this->page_data['branch_name']=$branch_name;
        	
        }else{
        	$this->page_data['branch_id']='';
        	$this->page_data['branch_name']='';
        }
         if ($sale_status_id!='') {
        	if ($sale_status_id=='1') {
        		$condition[]=array('where','a.sales_status','pre_sales');
        		$this->page_data['sale_status_id']=$sale_status_id;
        	}else if($sale_status_id=='2'){
        	    $condition[]=array('where','a.sales_status','Won');
        		$this->page_data['sale_status_id']=$sale_status_id;
        	}
        	
        }else{
        	$this->page_data['sale_status_id']='';
        
        }


	
		$condition[] = array('join',TABLE_COMPANY_BRANCH.' as branch','branch.company_branch_id=a.company_branch_id');
        $condition[] = array('join',TABLE_CUSTOMER_CONTACT.' as contact','contact.customer_id=a.customer_id and contact.is_active=1','left');
        $condition[] = array('join',TABLE_CUSTOMER_ADDRESSS.' as adr','adr.customer_id=a.customer_id','left');
         $condition[] = array('join',TABLE_CITY.' as city','city.city_id = adr.city_id','left');
         $condition[] = array('join',TABLE_STATE.' as state','state.state_id = adr.state_id','left');
         $condition[] = array('join',TABLE_COUNTRY.' as country','country.country_id = adr.country_id','left');
         $condition[] = array('join',TABLE_USERS.' as sales_person','sales_person.user_id = a.sales_person_id','left');

		 $selected_array=array(
                              'table_name'=>TABLE_CUSTOMER.' as a',
                              'table_fields'=>'a.customer_id, branch.company_branch_name, a.is_active,contact.contact_person_name, contact.email_id, city.city_name,a.customer_name,a.date_of_enrollment,a.customer_pan_no,a.customer_gst_no,adr.address_line1,adr.address_line2,adr.postal_code,contact.contact_person_name,contact.mobile_no,contact.std_code,contact.designation,country.country_name,state.state_name,sales_person.first_name,sales_person.last_name,sales_status',
                               'condition'=>$condition,
                               'limit'=>'',
                               'order_by_field'=>'a.customer_name',
                               'order'=>'DESC',
							   'group_by_field'=> 'a.customer_id',
                               'offset'=>''
                              );

		 $customers['customers'] = $this->master_model->select_data($selected_array);
		

		     if(!empty($customers['customers'] )&& is_array($customers['customers'] )){
							
				  $this->page_data['customers'] = $customers['customers'];
		 	
  	    } 
		
  	    $this->load->view('gc/customer_list_excel',$this->page_data);
	

	}
	function view_customer_master($customer_id) {
		admin_auth();
		$this->page_data['page_title']="View Customers ";
		//$id=$this->encrypt->decode($customer_id);
		$id=encrypt_decrypt_parameters($customer_id,'decrypt');
		$this->page_data['bredcrum']=array(
										'Customer'=>'#',
										'List Customers'=>site_url('gc/customer_master/list_customer_master'),
										'View Customers'=>'#',
										);

		$condition[] = array('join',TABLE_COMPANY_BRANCH.' as branch','branch.company_branch_id=a.company_branch_id','left');

        $condition[] = array('join',TABLE_CUSTOMER_CONTACT.' as contact','contact.customer_id=a.customer_id  and contact.is_active=1','left');

        $condition[] = array('join',TABLE_CUSTOMER_ADDRESSS.' as adr','adr.customer_id=a.customer_id','left');

        $condition[] = array('join',TABLE_CITY.' as city','city.city_id = adr.city_id','left');
        $condition[] = array('join',TABLE_STATE.' as state','state.state_id = adr.state_id','left');
        $condition[] = array('join',TABLE_COUNTRY.' as country','country.country_id = adr.country_id','left');
        $condition[] = array('join',TABLE_USERS.' as sales_person','sales_person.user_id = a.sales_person_id','left');
        $condition[] = array('where','a.customer_id',$id);

        $selected_array=array(
                              'table_name'=>TABLE_CUSTOMER.' as a',
                              'table_fields'=>'a.customer_id,branch.company_branch_name,a.customer_name,adr.address_line1,adr.address_line2,a.is_active, contact.contact_person_name, contact.email_id, city.city_name,a.customer_name,a.date_of_enrollment,state.state_name,country.country_name,a.customer_pan_no,a.customer_gst_no,adr.postal_code,contact.designation,contact.mobile_no,contact.std_code,sales_person.first_name,sales_person.last_name',
                               'condition'=>$condition,
                               'limit'=>'',
                               //'order_by_field'=>'a.branch_salable_geography_id',
                               // 'group_by_field'=>'b.state_name,c.city_name',
                               'offset'=>''
                              );
        $customer['customer'] = $this->master_model->select_data($selected_array);
          if(!empty($customer['customer'] )&& is_array($customer['customer'] )){
							
				  $this->page_data['customer'] = $customer['customer'];
		 	
  	    }
		
		$condition=array();
		$condition[] = array('join',TABLE_COMPANY_BRANCH.' as branch','branch.company_branch_id=a.company_branch_id','left');
        $condition[] = array('join',TABLE_CUSTOMER_ADDRESSS.' as adr','adr.customer_id=a.customer_id','left');
        $condition[] = array('join',TABLE_CITY.' as city','city.city_id = adr.city_id','left');
        $condition[] = array('join',TABLE_STATE.' as state','state.state_id = adr.state_id','left');
        $condition[] = array('join',TABLE_COUNTRY.' as country','country.country_id = adr.country_id','left');
        $condition[] = array('join',TABLE_USERS.' as sales_person','sales_person.user_id = a.sales_person_id','left');
        $condition[] = array('where','a.customer_id',$id);
        $selected_array=array(
                              'table_name'=>TABLE_CUSTOMER.' as a',
                              'table_fields'=>'a.customer_id,branch.company_branch_name,a.customer_name,adr.address_line1,adr.address_line2,a.is_active,city.city_name,a.customer_name,a.date_of_enrollment,state.state_name,country.country_name,a.customer_pan_no,a.customer_gst_no,adr.postal_code,sales_person.first_name,sales_person.last_name',
                               'condition'=>$condition,
                               'limit'=>'',
                               //'order_by_field'=>'a.branch_salable_geography_id',
                               // 'group_by_field'=>'b.state_name,c.city_name',
                               'offset'=>''
                              );
        $customer['customer'] = $this->master_model->select_data($selected_array);
          if(!empty($customer['customer'] )&& is_array($customer['customer'] )){
							
				  $this->page_data['customer1'] = $customer['customer'];
		 	
  	    }


		$this->load->view('gc/customer_master_view.php',$this->page_data);
	}
	
	function manage_customer_master($customer_id="") {
		admin_auth();
		//error_reporting(E_ALL);
		//$id=$this->encrypt->decode($customer_id);
		$id=encrypt_decrypt_parameters($customer_id,'decrypt');
		$this->_page_data['country']=$this->fetch_country();
		$this->_page_data['sale_person_name']=$this->fetch_sale_person_name();
		
		if ($id) 
			{
				$this->_page_data['page_title']="Edit Customer ";
					$this->_page_data['bredcrum']=array(
										'Customer'=>'#',
										'Manage Customer'=>'#',
										'List Customer'=>site_url(CUSTOMER_MASTER.'list_customer_master'),
										'Edit Customer'=>'#',
										);
                    $this->_page_data['check']="edit";
		}else{
			$this->_page_data['page_title']="Add Customers ";
				$this->_page_data['bredcrum']=array(
										'Customer'=>'#',
										'Manage Customer'=>'#',
										'Add Customer'=>'#',
										);
		}
		

 		$c_Company_Details=$this->lang->line('c_Company_Details', FALSE);
		$c_Branch_Company_Name=$this->lang->line('c_Branch_Company_Name', FALSE);
		$c_Customer_PAN_Number=$this->lang->line('c_Customer_PAN_Number', FALSE);
		$c_Date_Of_Enrollment=$this->lang->line('c_Date_Of_Enrollment', FALSE);
		$c_Customer_GST_Number=$this->lang->line('c_Customer_GST_Number', FALSE);
		$c_Mobile_Number=$this->lang->line('c_Mobile_Number', FALSE);
		$c_Email_Id=$this->lang->line('c_Email_Id', FALSE);
		$c_Customer_Address_line_1=$this->lang->line('c_Customer_Address_line_1', FALSE);
		$c_Customer_Address_line_2=$this->lang->line('c_Customer_Address_line_2', FALSE);
		$c_State=$this->lang->line('c_State', FALSE);
		$c_Postal_Code=$this->lang->line('c_Postal_Code', FALSE);		
		$c_Country=$this->lang->line('c_Country', FALSE);
		$c_City=$this->lang->line('c_City', FALSE);

		$this->_page_data['c_Company_Details']=$c_Company_Details;
		$this->_page_data['c_Branch_Company_Name']=$c_Branch_Company_Name;
		$this->_page_data['c_Customer_PAN_Number']=$c_Customer_PAN_Number;
		$this->_page_data['c_Date_Of_Enrollment']=$c_Date_Of_Enrollment;
		$this->_page_data['c_Customer_GST_Number']=$c_Customer_GST_Number;
		$this->_page_data['c_Mobile_Number']=$c_Mobile_Number;
		$this->_page_data['c_Email_Id']=$c_Email_Id;
		$this->_page_data['c_Customer_Address_line_1']=$c_Customer_Address_line_1;
		$this->_page_data['c_Customer_Address_line_2']=$c_Customer_Address_line_2;
		$this->_page_data['c_State']=$c_State;
		$this->_page_data['c_Postal_Code']=$c_Postal_Code;
		$this->_page_data['c_Country']=$c_Country;
		$this->_page_data['c_City']=$c_City;

		$this->_page_data['c_Customer_Name']=$this->lang->line('c_Customer_Name', FALSE);
		$this->_page_data['c_Designation']=$this->lang->line('c_Designation', FALSE);
		$this->_page_data['c_Location_Details']=$this->lang->line('c_Location_Details', FALSE);
		$this->_page_data['c_Contact_Details']=$this->lang->line('c_Contact_Details', FALSE);

		$this->_page_data['c_sale_person_name']=$this->lang->line('c_sale_person_name', FALSE);
		$this->_page_data['c_contact_person_name']=$this->lang->line('c_contact_person_name', FALSE);
		
		$this->_page_data['branch_name']=$this->fetch_branch_name();
		


		if (!$id) {
            
	       $this->load->view('gc/customer_master_manage.php',$this->_page_data);

		}else{
	     $condition[] = array('join',TABLE_COMPANY_BRANCH.' as branch','branch.company_branch_id=a.company_branch_id','left');

        $condition[] = array('join',TABLE_CUSTOMER_CONTACT.' as contact','contact.customer_id=a.customer_id and contact.is_active=1','left');

        $condition[] = array('join',TABLE_CUSTOMER_ADDRESSS.' as adr','adr.customer_id=a.customer_id','left');

        $condition[] = array('join',TABLE_CITY.' as city','city.city_id = adr.city_id','left');
        $condition[] = array('join',TABLE_STATE.' as state','state.state_id = adr.state_id','left');
        $condition[] = array('join',TABLE_COUNTRY.' as country','country.country_id = adr.country_id','left');
         $condition[] = array('join',TABLE_USERS.' as u','u.user_id = a.sales_person_id','left');
        $condition[] = array('where','a.customer_id',$id);

        $selected_array=array(
                              'table_name'=>TABLE_CUSTOMER.' as a',
                              'table_fields'=>'a.customer_id,branch.company_branch_name,a.customer_name,adr.address_line1,adr.address_line2,a.is_active, contact.contact_person_name, contact.email_id, city.city_name,a.customer_name,a.date_of_enrollment,state.state_name,country.country_name,a.customer_pan_no,a.customer_gst_no,city.city_id,branch.company_branch_id,state.state_id,country.country_id,adr.postal_code,contact.mobile_no,contact.designation,contact.std_code,u.user_id,contact.customer_contact_id',
                               'condition'=>$condition,
                               'limit'=>'',
                               //'order_by_field'=>'a.branch_salable_geography_id',
                               // 'group_by_field'=>'b.state_name,c.city_name',
                               'offset'=>''
                              );
        $customer['customer'] = $this->master_model->select_data($selected_array);
          if(!empty($customer['customer'] )&& is_array($customer['customer'] )){
							
				  $this->_page_data['customer'] = $customer['customer'];
		 	
  	    }
		
		$this->load->view('gc/customer_master_manage.php',$this->_page_data);
	 }
	
}
	
	
	
function add_customer($customer_id=""){	 
	admin_auth();

		$id = check_injections_filter($this->input->post('hidden'));
		$branch_name= check_injections_filter($this->input->post('branch_name'));
    	$customer_name = check_injections_filter($this->input->post('cutomer_name'));
    	$date_of_enroll = check_injections_filter($this->input->post('date_of_enroll'));
    	$std_code = check_injections_filter($this->input->post('std_code'));
    	$sales_person_id = check_injections_filter($this->input->post('sale_person_name'));
    	

    	$cutomer_pan= check_injections_filter($this->input->post('cutomer_pan'));
    	$cutomer_gst = check_injections_filter($this->input->post('cutomer_gst'));

    	$state_name= check_injections_filter($this->input->post('customer_name'));

    	$contact_person_name= check_injections_filter($this->input->post('contact_person_name'));
    	$cutomer_designation = check_injections_filter($this->input->post('cutomer_designation'));

    	$cutomer_mobile= check_injections_filter($this->input->post('cutomer_mobile'));
    	$cutomer_email = check_injections_filter($this->input->post('cutomer_email'));

    	$customer_addressl1= check_injections_filter($this->input->post('customer_addressl1'));
    	$customer_addressl2 = check_injections_filter($this->input->post('customer_addressl2'));
    	$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'));
    	$postalcode = check_injections_filter($this->input->post('postalcode'));

      // =============== addmore=====================
    	$contact_person_name1= check_injections_filter($this->input->post('contact_person_name1'));
    	$std_code_1 = check_injections_filter($this->input->post('std_code_1'));
    	$cutomer_mobile1= check_injections_filter($this->input->post('cutomer_mobile1'));
    	$cutomer_email1 = check_injections_filter($this->input->post('cutomer_email1'));
    	$cutomer_designation1 = check_injections_filter($this->input->post('designation1'));
    	$customer_contact_id = check_injections_filter($this->input->post('customer_contact_id'));
    	$customer_contact_id1 = check_injections_filter($this->input->post('customer_contact_id1'));
    	// =============== addmore=====================
    	$user_id=$this->session->userdata('user_id');

        /* if ($cutomer_gst!='' or $cutomer_pan!=''){
        	 //$check_condition[] = array('where','cutomer_email',$cutomer_email);
        	 //$check_condition[] = array('where','customer_pan_no',$cutomer_pan);
        	// $check_condition[] = array('where','customer_pan_no',$cutomer_pan);
          	if ($id) {

                $check_condition[] = array('where_not_in','customer_id',$id);
            }

             $selected_array=array(
                'table_name' =>TABLE_CUSTOMER,             
                'table_fields'=>'customer_gst_no,customer_pan_no',
                'order_by_field'=>'',
                'limit'=>'',
                'offset'=>'',
                'condition'=>$check_condition           
              );
       
    $check_customer['check_customer'] = $this->master_model->select_data($selected_array);
    


        }  */

    /* if(!empty($check_customer['check_customer']))
    {
        $this->session->set_flashdata('message','Customer already present');
        redirect('gc/customer_master/manage_customer_master');
    }else
	{ */

    						$customer_data=array(
								'company_branch_id' => $branch_name,
								'customer_name' => $customer_name,
								'customer_pan_no' => $cutomer_pan,
								'customer_gst_no' => $cutomer_gst,
								'date_of_enrollment' =>$date_of_enroll,
								'sales_person_id'=>$sales_person_id,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>'',	
								'modified_on' => date("Y-m-d H:i:s")
								
					        );

    	if (!$id) {
    		$get = $this->master_model->insert_record(TABLE_CUSTOMER,$customer_data);
    		$address_customer_data=array(
								'customer_id' => $get,
								'address_line1' => $customer_addressl1,
								'address_line2' => $customer_addressl2,
								'country_id' => $country_id,
								'state_id' => $state_id,
								'city_id' =>$city_id,
								'postal_code'=>$postalcode,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>'',	
								'modified_on' => date("Y-m-d H:i:s")
								
					        );
    		$this->master_model->insert_record(TABLE_CUSTOMER_ADDRESSS,$address_customer_data);
            $add_customer_data=array(
								'customer_id' => $get,
								'contact_person_name'=>$contact_person_name,
								'mobile_no' => $cutomer_mobile,
								'std_code'=>$std_code,
								'email_id' => $cutomer_email,
								'designation' =>$cutomer_designation,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>'',	
								'modified_on' => date("Y-m-d H:i:s")
								
					              );
            $this->master_model->insert_record(TABLE_CUSTOMER_CONTACT,$add_customer_data);
            if ($contact_person_name1) {
            $add_customer_more_data=array(
								'customer_id' =>$get,
								'contact_person_name'=>$contact_person_name1,
								'mobile_no' => $cutomer_mobile1,
								'email_id' => $cutomer_email1,
								'std_code'=>$std_code_1,
								'designation' =>$cutomer_designation1,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' => $user_id,	
								'modified_on' => date("Y-m-d H:i:s")
								
					        );
            $this->master_model->insert_record(TABLE_CUSTOMER_CONTACT,$add_customer_more_data);
            }
            $this->session->set_flashdata('message','Customer created successfully');
		    redirect('gc/customer_master/list_customer_master');

    	}else{
    		$condition=array('customer_id'=>$id);
    		$customer_data=array(

								'company_branch_id' => $branch_name,
								'customer_name' => $customer_name,
								'customer_pan_no' => $cutomer_pan,
								'customer_gst_no' => $cutomer_gst,
								'date_of_enrollment' =>$date_of_enroll,
								'sales_person_id'=>$sales_person_id,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>$user_id,	
								'modified_on' => date("Y-m-d H:i:s")	
					            );
    		
			$this->master_model->update_record(TABLE_CUSTOMER,$customer_data,$condition);
			$add_customer_data=array(
								'customer_id' =>$id,
								'contact_person_name'=>$contact_person_name,
								'mobile_no' => $cutomer_mobile,
								'std_code'=>$std_code,
								'email_id' => $cutomer_email,
								'designation' =>$cutomer_designation,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' => $user_id,	
								'modified_on' => date("Y-m-d H:i:s")	
					            );
			$condition_contact=array('customer_contact_id'=>$customer_contact_id);
			$this->master_model->update_record(TABLE_CUSTOMER_CONTACT,$add_customer_data,$condition_contact);
			$address_customer_data=array(
								'customer_id' => $id,
								'address_line1' => $customer_addressl1,
								'address_line2' => $customer_addressl2,
								'country_id' => $country_id,
								'state_id' => $state_id,
								'city_id' =>$city_id,
								'postal_code'=>$postalcode,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' => $user_id,	
								'modified_on' => date("Y-m-d H:i:s")
								
					             );
			$this->master_model->update_record(TABLE_CUSTOMER_ADDRESSS,	$address_customer_data,$condition);
			//if ($contact_person_name1) {
				
				if($customer_contact_id1=="")
				{
					$add_customer_more_data=array(
										'customer_id' =>$id,
										'contact_person_name'=>$contact_person_name1,
										'mobile_no' => $cutomer_mobile1,
										'email_id' => $cutomer_email1,
										'std_code'=>$std_code_1,
										'designation' =>$cutomer_designation1,
										'created_on' => date("Y-m-d H:i:s"),
										'created_by' =>$user_id,
										'modified_by' => $user_id,	
										'modified_on' => date("Y-m-d H:i:s")
										
									);
					$this->master_model->insert_record(TABLE_CUSTOMER_CONTACT,$add_customer_more_data);
				}
				else
				{
					
					if($contact_person_name1=="")
					{
						$add_customer_data=array(
										'is_active' =>2,
										'modified_by' => $user_id,	
										'modified_on' => date("Y-m-d H:i:s")	
										);
						$condition_contact=array('customer_contact_id'=>$customer_contact_id1);
						$this->master_model->update_record(TABLE_CUSTOMER_CONTACT,$add_customer_data,$condition_contact);
					}
					else
					{
					$add_customer_data=array(
										'customer_id' =>$id,
										'contact_person_name'=>$contact_person_name1,
										'mobile_no' => $cutomer_mobile1,
										'std_code'=>$std_code_1,
										'email_id' => $cutomer_email1,
										'designation' =>$cutomer_designation1,
										'created_on' => date("Y-m-d H:i:s"),
										'created_by' =>$user_id,
										'modified_by' => $user_id,	
										'modified_on' => date("Y-m-d H:i:s")	
										);
					$condition_contact=array('customer_contact_id'=>$customer_contact_id1);
					$this->master_model->update_record(TABLE_CUSTOMER_CONTACT,$add_customer_data,$condition_contact);
					}
				}
            //}
            $this->session->set_flashdata('message','Customer updated successfully');
		    redirect('gc/customer_master/list_customer_master');
    	}
  // }

							    	  
}

    function fetch_country(){
    
    	$condition[] = array('where','is_active','1');
    	$selected_array = array( 
			                   'table_name' =>TABLE_COUNTRY,
			                   'table_fields' => 'country_id,country_name,is_active',
			                   'condition'=>$condition,
			                   'limit'=>'',
			                   'offset'=>'',
			                   'order_by_field'=>'country_name',
			                   'order'=>'ASC'
		);

		return $this->master_model->select_data($selected_array);		         
    }
  //   function fetch_state(){
  //   	admin_auth();
  //   	$country_id=$this->input->post('country_id');
  //   	$condition[] = array('where','country_id',$country_id);
  //   	$condition[] = array('where','is_active','1');
  //   	$selected_array = array( 
		// 	                   'table_name' =>TABLE_STATE,
		// 	                   'table_fields' => 'state_id,state_name,is_active',
		// 	                   'condition'=>$condition,
		// 	                   'limit'=>'',
		// 	                   'offset'=>'',
		// 	                   'order_by_field'=>'Asc',
			                
		// );
	 //    $state = $this->master_model->select_data($selected_array);
	 //    $output='';
		
		// foreach($state as $row)
		// {
		//   $output .= '<option value='.$row['state_id'].'>'.$row['state_name'].'</option>';

		// }
  //         echo $output;				         
  //   }
    function fetch_branch_name(){
    
    	$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',
			                   'limit'=>'',
			                   'offset'=>'',
			                   'condition'=>$condition,
			                   'order_by_field'=>'company_branch_name',
			                   'order'=>'ASC'
		);

		return $this->master_model->select_data($selected_array);	

    }
    function fetch_sale_person_name(){
    	
  //   	$access_branch= $this->session->userdata('access_branch');

		// if ($access_branch!='') {
  //            $condition[] = array('where_in','company_branch_id',$access_branch);
  //       }
    	$condition[] = array('where','is_active','1');
    	$condition[] = array('where','user_type_id','2');
    	$selected_array = array( 
			                   'table_name' =>TABLE_USERS,
			                   'table_fields' => 'first_name,last_name,user_id',
			                   'limit'=>'',
			                   'offset'=>'',
			                   'condition'=>$condition,
			                   'order_by_field'=>'first_name',
			                   'order'=>'ASC'
		);

		return $this->master_model->select_data($selected_array);		         
    }

    function disable_customer_master($customer_id){
    	
    	//$id=$this->encrypt->decode($customer_id);
		$id=encrypt_decrypt_parameters($customer_id,'decrypt');
    	$user_id=$this->session->userdata('user_id');

     $customer_data=array(
								'is_active' =>'0',								
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>$user_id,	
								'modified_on' => date("Y-m-d H:i:s")								
					    );
     	$condition=array('customer_id'=>$id);
     	$this->session->set_flashdata('message','Customer is disable');
		$this->master_model->update_record(TABLE_CUSTOMER,$customer_data,$condition);
		//echo "enable";
		//$this->list_customer_master();
		redirect('gc/customer_master/list_customer_master');
    }
    function enable_customer_master($customer_id){
     
     // $id=$this->encrypt->decode($customer_id);
	  $id=encrypt_decrypt_parameters($customer_id,'decrypt');
      $user_id=$this->session->userdata('user_id');
      $customer_data=array(
								'is_active' =>'1',								
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>$user_id,	
								'modified_on' => date("Y-m-d H:i:s")								
					    );
     	$condition=array('customer_id'=>$id);
     	$this->session->set_flashdata('message',' Customer is enable');
		$this->master_model->update_record(TABLE_CUSTOMER,$customer_data,$condition);
		//echo "enable";
		redirect('gc/customer_master/list_customer_master');
    }
    function get_state()
	{
		$country_id=$this->input->post('country_id');
        $state_id=$this->input->post('state_id');
		//$access_branch= $this->session->userdata('access_branch');
       
	    $salse_person_cond = array();
		$salse_person_cond[] = array('where','country_id',$country_id);
		// if ($access_branch!='') {
  //            $salse_person_cond[]  = array('where_in','b.company_branch_id',$access_branch);
  //       }
		$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)
		{
            $selected='';

            if ($row['state_id']==$state_id) 

            {

                $selected='selected="selected"';

            }

		  $output .= '<option value='.$row['state_id'].' '.$selected.'>'.$row['state_name'].'</option>';

		}
          echo $output;	
    }
    function get_std_code()
	{
		$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_COUNTRY,
								'table_fields'=>'std_code',
								'condition'=>$salse_person_cond,
								'limit'=>'',
								'offset'=>''
								);
		$salse_person_data = $this->master_model->select_data($select_array);
		
		$output='';
		
		foreach($salse_person_data as $row)
		{
		  $output .= $row['std_code'];

		}
          echo $output;	
    }
	
	function get_city()
	{

 		// $condition[] = array('join',TABLE_CUSTOMER_ADDRESSS.' as ca','ca.city_id = c.city_id','left');
 		// $condition[] = array('join',TABLE_CUSTOMER.' as cu','cu.customer_id = ca.customer_id','left');

		$state_id=$this->input->post('state_id');
         $city_id=$this->input->post('city_id');
		//$access_branch= $this->session->userdata('access_branch');
	    $salse_person_cond = array();
		$salse_person_cond[] = array('where','state_id',$state_id);
		// if ($access_branch!='') {
  //            $salse_person_cond[]  = array('where_in','cu.company_branch_id',$access_branch);
  //       }
		$salse_person_cond[] = array('where','is_active','1');
		$select_array = array(
								'table_name'=>TABLE_CITY,
								'table_fields'=>'city_name,city_id,is_active,state_id',
								'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);
		//print_r($salse_person_data);
		$output='<option value="">Select City</option>';
		
		foreach($salse_person_data as $row)
		{
            $selected='';

            if ($row['city_id']==$city_id) 

            {

                $selected='selected="selected"';

            }
		  $output .= '<option value='.$row['city_id'].' '.$selected.'>'.$row['city_name'].'</option>';

		}
          echo $output;	
    }
    function branch(){
	 	
	 	$branch_id=check_injections_filter($this->input->post('branch_id'));
	 	$access_branch= $this->session->userdata('access_branch');
		

		
	 	//echo $customer_id;	
    	$customer_cond = array();
		$customer_cond[] = array('where','is_active','1');
		$customer_cond[] = array('like','company_branch_name',$branch_id);
		if ($access_branch!='') {
			$acb =  explode( ',',$access_branch);
			$customer_cond[] = array('where_in','company_branch_id',$acb);
		}
		
		
		$select_array = array(
								'table_name'=>TABLE_COMPANY_BRANCH,
								'table_fields'=>'company_branch_name,company_branch_id',
								'condition'=>$customer_cond,
								'sort_by'=>'company_branch_id',
								'order_by_field'=>'company_branch_name',
								'group_by_field'=>'company_branch_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)
		{
			$result=[];
			$output='';
		 		foreach($customer_data as $row)
                     {
						  $result[] = ['id'=>$row['company_branch_id'], 'text'=>$row['company_branch_name']];
						 //$result['id']=$row['customer_id'];
						// $result['name']=$row['customer_name'];
                  		 // $output .= '<option value='.$row['customer_id'].'>'.$row['customer_name'].'</option>';

                     }

          echo json_encode($result);
		}
    }
		
}