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

class Route_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        
         
         $this->load->model('Product_model');
        //$this->load->model('Common_model');
        //$this->load->model('General');
        //$this->load->model('Report_model');
         if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
        }
        
        
                 public function index()
	{
	      
	      
	      $data['ref'] = $this->Product_model->salserep_list();
                
                
                $data['content'] = ('pages/changeroute'); 
		        $this->load->view('template',$data);
	}
	
	function update_route(){
	    
	    	    $ret[0]="";
    	    	    $this->form_validation->set_error_delimiters('', ''); 
    	    	    
    	    	  
                        
                        $this->form_validation->set_rules('ref', 'From Rep', 'required');
                        $this->form_validation->set_rules('sup_route', 'Route', 'required');
                        $this->form_validation->set_rules('tref', 'To Rep', 'required');
                       
                       
                  
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	       if($this->input->post('tosup_route')==""){
    	           
    	                $this->db->set('ref_id', $this->input->post('tref'));
                        //$this->db->set('cust_edate', date('Y-m-d'));
                    
                        //$this->db->where('ref_id', $this->input->post('ref'));
                        $this->db->where('route_id', $this->input->post('sup_route'));
                        $result=$this->db->update('mes_route');
                        
                            if($result){
                             
                    $this->db->from('customer_route'); 
                    $this->db->select("*");
                    $this->db->where('route_id',$this->input->post('sup_route'));
                    $deta = $this->db->get();
                    $serv = $deta->result(); 
                   
                  
                   
                    for($i=0;$i<count($serv);$i++){
                        
                        $this->db->set('cust_salseperson', $this->input->post('tref'));
                        $this->db->where('cust_id', $serv[$i]->cus_id);
                        $this->db->update('mes_customer');
                
                    }
                             
                             
                            $ret[0].= "Route Details Updated";
                        }else{
                            $ret[0].= "Please Re enter all details";
                        }
                        
                        
    	       }else{
    	           
    	            $this->db->from('customer_route'); 
                    $this->db->select("*");
                    $this->db->where('route_id',$this->input->post('sup_route'));
                    $deta = $this->db->get();
                    $serv = $deta->result(); 
                    
                             for($i=0;$i<count($serv);$i++){
                        
                        $this->db->set('cust_salseperson', $this->input->post('tref'));
                        $this->db->where('cust_id', $serv[$i]->cus_id);
                        $this->db->update('mes_customer');
                
                        }
            
            
                    $this->db->from('customer_route'); 
                    $this->db->select("*");
                    $this->db->where('route_id',$this->input->post('sup_route'));
                    $deta = $this->db->get();
                    $serv = $deta->result(); 
                    
                    $updt = "";
                    
                             for($i=0;$i<count($serv);$i++){
                        
                        $this->db->set('route_id', $this->input->post('tosup_route'));
                        $this->db->where('cus_id', $serv[$i]->cus_id);
                        $updt = $this->db->update('customer_route');
                
                   }
            
                    if($updt){
                    
                         $ret[0].= "Route Details Updated";
                     }else{
                         
                        $ret[0].= "Please Re enter all details";
                      }
            
            
    	           
    	       }
                     
    	   }
    	   
    	   echo json_encode($ret);
	}
	
	function update_route_old(){
	    
	    	    $ret[0]="";
    	    	    $this->form_validation->set_error_delimiters('', ''); 
    	    	    
    	    	  
                        
                        $this->form_validation->set_rules('ref', 'From Rep', 'required');
                        $this->form_validation->set_rules('sup_route', 'Route', 'required');
                        $this->form_validation->set_rules('tref', 'To Rep', 'required');
                       
                       
                  
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	       
    	                $this->db->set('ref_id', $this->input->post('tref'));
                        //$this->db->set('cust_edate', date('Y-m-d'));
                    
                        //$this->db->where('ref_id', $this->input->post('ref'));
                        $this->db->where('route_id', $this->input->post('sup_route'));
                        $result=$this->db->update('mes_route');
                        
                        
                        
                         if($result){
                             
                    $this->db->from('customer_route'); 
                   $this->db->select("*");
                   $this->db->where('route_id',$this->input->post('sup_route'));
                   $deta = $this->db->get();
                   $serv = $deta->result(); 
                   
                  
                   
                    for($i=0;$i<count($serv);$i++){
                        
                        $this->db->set('cust_salseperson', $this->input->post('tref'));
                        $this->db->where('cust_id', $serv[$i]->cus_id);
                        $this->db->update('mes_customer');
                
            }
                             
                             
                            $ret[0].= "Route Details Updated";
                        }else{
                            $ret[0].= "Please Re enter all details";
                        }
    	   }
    	   
    	   echo json_encode($ret);
	}
        
}