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

class Customer_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        $this->load->model('product_model');
        $this->load->model('common_model');
        $this->load->model('General');
     
      if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
         
        }
        
         function index(){
                    $data['province'] = $this->product_model->province();
                    $data['city'] = $this->product_model->cities();
                    $data['districts'] = $this->product_model->districts();
                  
            $this->db->select("*");
                    $this->db->from('mes_user');
                    $this->db->where('user_departmentid','Marketing');
                    
                    if($this->session->userdata['login_data']['userlevel'] =='20'){
                        
                        $this->db->where('user_id',$this->session->userdata['login_data']['user_id']);
                    }
                    
                    $detail = $this->db->get();
                   $data['dup'] = $detail->result(); 
                   
                    $data['ref'] = $this->product_model->salserep_list();
                   
                  
            
        $data['content'] = ('pages/customer'); 
		$this->load->view('template',$data);
            
        }
        
        //get distric
        
            
        
        function cust_viw(){
                    
                   // $data=$this->product_model->product_list();
                   
                   if($this->input->post('rep')!=0 || $this->input->post('cus') !=""){
                       
                       
            $this->db->from('mes_customer');
            $this->db->select("*");
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_customer.cust_currency", 'left');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'left');
            $this->db->join( "mes_user", "mes_user.user_id = mes_customer.cust_salseperson", 'left');
           // if($this->session->userdata['login_data']['userlevel'] > 12){
                
                //$this->db->where('cust_salseperson',$this->session->userdata['login_data']['user_id']);
           // }
           
           if($this->input->post('rep')!=0 && $this->input->post('cus') !=""){
               
               $this->db->where('cust_salseperson', $this->input->post('rep'));
               $this->db->like('cust_company', $this->input->post('cus'));
               
           }else if($this->input->post('rep')!=0){
               
               $this->db->where('cust_salseperson', $this->input->post('rep'));
               
           }else if($this->input->post('cus') !=""){
               
              $this->db->like('cust_company', $this->input->post('cus')); 
               
           }
        
            //$this->db->order_by('cust_id', 'desc');
            $this->db->order_by('cust_auth', 'asc');
            $this->db->order_by('cust_id', 'desc');
                //$this->db->limit(100);
            
            
            $detailtax = $this->db->get();
            $data = $detailtax->result();
                       
                       
                   }else{
                       
            $this->db->from('mes_customer');
            $this->db->select("*");
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_customer.cust_currency", 'left');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'left');
            $this->db->join( "mes_user", "mes_user.user_id = mes_customer.cust_salseperson", 'left');
            if($this->session->userdata['login_data']['userlevel'] > 15){
                
                $this->db->where('cust_salseperson',$this->session->userdata['login_data']['user_id']);
            }
        
           
            $this->db->order_by('cust_auth', 'asc');
             $this->db->order_by('cust_id', 'desc');
            $this->db->limit(100);
            $detailtax = $this->db->get();
            $data = $detailtax->result();
                       
                   }
                    
            
            
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_customer' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New Customer</button></br>";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>Customer</th>
														<th>Contact Details</th>
														<th>Rep</th>
													
													
														<!--th>Tax</th>
														<th>Currency</th>
														<th>Type</th-->
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    if($data[$i]->cust_tax==1){$tax="&#10003;";}else{$tax="";}
                    if($data[$i]->cust_status==1 && $data[$i]->cust_auth==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else if ($data[$i]->cust_auth==0){$sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    if($data[$i]->cust_conperson2!=""){$con2 = "<br>".$data[$i]->cust_conperson2;}else{$con2="";}
                    if($data[$i]->cust_deladd==1){$dadd ="<button type='button' class='btn btn-outline-secondary btn-sm' type='submit' id='view_cusaddress/".$data[$i]->cust_id."' onclick='dadd_view(this.id)' data-toggle='modal' data-target='#showdadd'>Show Delivery Address</button>";}
                    else{$dadd = "Same Delivery Address";}
                    
                    $city = $data[$i]->cust_city;
                    $cusref = $data[$i]->cust_salseperson;
                    
                    $this->db->select('*');
                    $this->db->from('customer_route');
                    $this->db->join( "mes_route", "mes_route.route_id = customer_route.route_id", 'left');
                    $this->db->where('customer_route.cus_id', $data[$i]->cust_id);
                    $query = $this->db->get();
                    $rcity = $query->result();
                    
                    if(isset($rcity[0]->route_name)){$rt = "Route - ".$rcity[0]->route_name;}else{$rt="";}
                    
                    if(($data[$i]->cust_auth==0 && $this->session->userdata['login_data']['userlevel']< 2)||($data[$i]->cust_auth==0 && $this->session->userdata['login_data']['userlevel']== 15))
                    {$button = "
                    <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_customer/".$data[$i]->cust_id."'  onclick='edit_view(this.id)' >Edit</button>
                    <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->cust_id."'  onclick='auth_cus(this.id)' >Authorize</button>
                    <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->cust_id."'  onclick='cancel_cus(this.id)' >Cancel</button>
                    
                              ";
                        
                    }else if(($this->session->userdata['login_data']['userlevel']< 2)||($this->session->userdata['login_data']['userlevel']==15)){
                        
                        $button = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_customer/".$data[$i]->cust_id."'  onclick='edit_view(this.id)' >Edit</button>
                        <a href = '".base_url()."cust_prof/".$data[$i]->cust_id."'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_customer/".$data[$i]->cust_id."' >View</button></a>";
                    }else{$button = " 
                               <a href = '".base_url()."cust_prof/".$data[$i]->cust_id."'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_customer/".$data[$i]->cust_id."' >View</button></a>
                               ";}
                    
                    
                    
                    $dat =  "
                              <tr scope='row'><td >".$data[$i]->cust_idtext.$data[$i]->cust_id."</td>
                               
                                <td>".$data[$i]->cust_company."<br>".$rt."<br>Credit Period <b>".$data[$i]->cust_creditper."</b></td>
                                <td>".$data[$i]->cust_addres.' '.$data[$i]->cust_add1.' '.$data[$i]->cust_add2.'<br>'.$data[$i]->cnty_name.'<br>'.$data[$i]->cust_contact.$con2.' - '.$data[$i]->cust_conperson.'<br>'.$data[$i]->cust_email."</td>
                                
                                <td>".$data[$i]->user_fname." ".$data[$i]->user_lname."</td>
                                <!--td style='color:Green;font-size:20px;'>".$tax."</td>
                                <td>".$data[$i]->curr_symble."</td>
                                <td>".$data[$i]->cust_type."</td-->
                                $sts
                                
                                <td>
                             $button
                             </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
       
        
        function cus_search(){
                    
                   // $data=$this->product_model->product_list();
                    
            $this->db->from('mes_customer');
            $this->db->select("*");
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_customer.cust_currency", 'left');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'left');
            $this->db->join( "mes_user", "mes_user.user_id = mes_customer.cust_salseperson", 'left');
           // if($this->session->userdata['login_data']['userlevel'] > 12){
                
                //$this->db->where('cust_salseperson',$this->session->userdata['login_data']['user_id']);
           // }
           
           if($this->input->post('depo')!=0 && $this->input->post('cust_name') !=""){
               
               $this->db->where('cust_salseperson', $this->input->post('depo'));
               $this->db->like('cust_company', $this->input->post('cust_name'));
               
           }else if($this->input->post('depo')!=0){
               
               $this->db->where('cust_salseperson', $this->input->post('depo'));
               
           }else if($this->input->post('cust_name') !=""){
               
              $this->db->like('cust_company', $this->input->post('cust_name')); 
               
           }
        
            //$this->db->order_by('cust_id', 'desc');
            $this->db->order_by('cust_auth', 'asc');
            
            if($this->input->post('depo')==0 && $this->input->post('cust_name') ==""){
               $this->db->order_by('cust_id', 'desc');
                $this->db->limit(100);
            }
            
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_customer' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New Customer</button></br>";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>Customer</th>
														<th>Contact Details</th>
														<th>Rep</th>
													
													
														<!--th>Tax</th>
														<th>Currency</th>
														<th>Type</th-->
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    if($data[$i]->cust_tax==1){$tax="&#10003;";}else{$tax="";}
                    if($data[$i]->cust_status==1 && $data[$i]->cust_auth==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else if ($data[$i]->cust_auth==0){$sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    //if($data[$i]->cust_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    if($data[$i]->cust_conperson2!=""){$con2 = "<br>".$data[$i]->cust_conperson2;}else{$con2="";}
                    if($data[$i]->cust_deladd==1){$dadd ="<button type='button' class='btn btn-outline-secondary btn-sm' type='submit' id='view_cusaddress/".$data[$i]->cust_id."' onclick='dadd_view(this.id)' data-toggle='modal' data-target='#showdadd'>Show Delivery Address</button>";}
                    else{$dadd = "Same Delivery Address";}
                    
                    $city = $data[$i]->cust_city;
                    $cusref = $data[$i]->cust_salseperson;
                   
                    
                    $this->db->select('*');
                    $this->db->from('customer_route');
                    $this->db->join( "mes_route", "mes_route.route_id = customer_route.route_id", 'left');
                    $this->db->where('customer_route.cus_id', $data[$i]->cust_id);
                    $query = $this->db->get();
                    $rcity = $query->result();
                    
                    if(isset($rcity[0]->route_name)){$rt = "Route - ".$rcity[0]->route_name;}else{$rt="";}
                    
                     if(($data[$i]->cust_auth==0 && $this->session->userdata['login_data']['userlevel']< 2)||($data[$i]->cust_auth==0 && $this->session->userdata['login_data']['userlevel']==15)) {
                    $button = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->cust_id."'  onclick='auth_cus(this.id)' >Authorize</button>
                    <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->cust_id."'  onclick='cancel_cus(this.id)' >Cancel</button>
                    <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_customer/".$data[$i]->cust_id."'  onclick='edit_view(this.id)' >Edit</button>
                              ";
                        
                    }else if(($this->session->userdata['login_data']['userlevel']< 2)||($this->session->userdata['login_data']['userlevel']==15)){
                        
                        $button = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_customer/".$data[$i]->cust_id."'  onclick='edit_view(this.id)' >Edit</button>
                        <a href = '".base_url()."cust_prof/".$data[$i]->cust_id."'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_customer/".$data[$i]->cust_id."' >View</button></a>";
                    }else{$button = " 
                               <a href = '".base_url()."cust_prof/".$data[$i]->cust_id."'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_customer/".$data[$i]->cust_id."' >View</button></a>
                               ";}
                    
                    
                    
                    $dat =  "
                              <tr scope='row'><td >".$data[$i]->cust_idtext.$data[$i]->cust_id."</td>
                               
                                <td>".$data[$i]->cust_company."<br>".$rt."<br>Credit Period <b>".$data[$i]->cust_creditper."</b></td>
                                <td>".$data[$i]->cust_addres.' '.$data[$i]->cust_add1.' '.$data[$i]->cust_add2.'<br>'.$data[$i]->cnty_name.'<br>'.$data[$i]->cust_contact.$con2.' - '.$data[$i]->cust_conperson.'<br>'.$data[$i]->cust_email."</td>
                                
                                <td>".$data[$i]->user_fname." ".$data[$i]->user_lname."</td>
                                <!--td style='color:Green;font-size:20px;'>".$tax."</td>
                                <td>".$data[$i]->curr_symble."</td>
                                <td>".$data[$i]->cust_type."</td-->
                                $sts
                                
                                <td>
                                    $button
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function cust_new(){
                    
                    $cat = $this->product_model->cat_list();
                    $item = $this->product_model->item_list();
                    $brand = $this->product_model->brand_list();
                    $unit = $this->product_model->unit_list();
                    $cuntry = $this->product_model->country_list();
                    $currncy = $this->product_model->currency_list();
                    $province = $this->product_model->province();
                    $city = $this->product_model->cities();
                    $districts = $this->product_model->districts();
                    
                    $this->db->select("*");
                    $this->db->from('mes_user');
                    $this->db->where('user_departmentid','Marketing');
                 
                    //$this->db->where('cust_country',$this->input->post('sup_country'));
                    $detail = $this->db->get();
                    $dup = $detail->result();
                    
                    
                    
                    
                    
                    $det="";
	                $det.='
	        
	 
	                    <div class="form-group row">
                                <label class="col-md-2 col-form-label">Name </label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_name" id="sup_name" class="form-control" value="" onclick="alert("zz");">
                                 </div>
                            </div>
                         
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address</label>
                                <div class="col-md-10">
                                  <textarea id="sup_add" name="sup_add" rows="4" placeholder="" class="form-control"></textarea>
                                 
	        
                                 </div>
                            </div>
                            
                             <div class="form-group row">
	          <label class="col-md-2 col-form-label">Province</label>
	          
                                <div class="col-md-10">
                                  <select class="form-control" id="sup_prov" name="sup_prov" onchange="sup_prov();">
                                  <option value="" selected>Select Province</option>'; 
                                  
                                  
                                    foreach($province as $value) {
                                                $det.='<option value="'.$value->id.'">'.$value->name_en.' </option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            </div>
                            
                             <div class="form-group row">
	          <label class="col-md-2 col-form-label">District</label>
	         
                                <div class="col-md-10">
                                  <select class="form-control" id="sup_dis" name="sup_dis">
                                  <option value="" selected>Select District</option>'; 
                               
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            </div>
                          
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Number</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_con" id="sup_con" class="form-control" value="">
                                 </div>
                            </div>
                         
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_conname" id="sup_conname" class="form-control" value="">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Credit Period</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_cperiod" id="sup_cperiod" class="form-control" value="">
                                 </div>
                            </div>
                            
                              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Discount</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_dsico" id="sup_dsico" class="form-control" value="">
                                 </div>
                            </div>
                            ';
                        	
                       echo json_encode($det);
                }
        
        function cust_sav(){
                    
                    $this->db->select("*");
                    $this->db->from('mes_customer');
                    $this->db->where('cust_company',$this->input->post('sup_name'));
                    $this->db->where('cust_addres',$this->input->post('sup_add'));
                    $detail = $this->db->get();
                    //$dup = $detail->result();
                    
                    $this->form_validation->set_error_delimiters('', ''); 
                    
                 
                        
                                $this->form_validation->set_rules('sup_name', 'Name', 'required');
                                $this->form_validation->set_rules('sup_add', 'Address', 'required');
                                $this->form_validation->set_rules('sup_prov', 'Province', 'required');
                                $this->form_validation->set_rules('sup_dis', 'District', 'required');
                                $this->form_validation->set_rules('sup_city', 'City', 'required');
                                $this->form_validation->set_rules('sup_sals', 'Salse Person', 'required');
                                $this->form_validation->set_rules('sup_route', 'Route', 'required');
                                
                               
                        
                
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
                   
    	   }else if($detail->num_rows()>0){
    	       $ret[0] = "Same Customer cannot be saved";
    	       $ret[2] = "error";
    	       }else{
    	           
    	            $this->db->select('*');
                    $this->db->from('districts');
                    $this->db->where('province_id',$this->input->post('sup_prov'));
                   // $this->db->where('user_status',1);
                    $hasil=$this->db->get();
                    
                    $this->db->select('*');
                    $this->db->from('cities');
                    $this->db->where('district_id',$this->input->post('sup_dis'));
                   // $this->db->where('user_status',1);
                    $hasilc=$this->db->get();
                    
                    if(($hasil->num_rows()<0) || ($hasilc->num_rows()<0) ){
                            $ret[0] = "Select Correct District or City";
    	                    $ret[2] = "error";
                    }else{
                        
                           $data = array(
                'cust_company' => $this->input->post('sup_name'), 
                'cust_addres'  => $this->input->post('sup_add'),
                'cust_salseperson'  => $this->input->post('sup_sals'),
                'cust_distc'  => $this->input->post('sup_dis'),
                'cust_prov'  => $this->input->post('sup_prov'),
                'cust_city'  => $this->input->post('sup_city'),
                'cust_contact'  => $this->input->post('sup_con'),
                'cust_discount'  => $this->input->post('sup_dsico'),
                'cust_creditper'  => $this->input->post('sup_cperiod'),
                'cust_uniqid'  => random_string('alnum',20),
                'cust_conperson'  => $this->input->post('sup_conname'),
                'cust_route'  => $this->input->post('sup_route'),
                'cust_credit'  => 93,
                'cust_cuser'  => $this->session->userdata['login_data']['username'], 
                'cust_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_customer',$data);
            $insert_id = $this->db->insert_id();
                if($result){
                    $ret[0] = "New Customer Detail Saved";
                             $rdata = array(
                                'cus_id' => $insert_id, 
                                'route_id'  => $this->input->post('sup_route')
                                );
                            $this->db->insert('customer_route',$rdata);
                    
                }else{
                    $ret[0] = "Please Re enter all details";
                }
                    }
    	       
    	     
    	       
    	    
    	   }
                   

          
    	   
    	        $ret[1] = "customer_view";
                echo json_encode($ret);
                }
                
        function cust_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->select("mes_customer.*,mes_customer.cust_uniqid as ud,mes_user.*,provinces.*,districts.*,cities.*,provinces.name_en as pro,
    	    districts.name_en as dis,cities.name_en as citynew,customer_route.route_id as routeid,mes_route.route_name as routename");
    	    $this->db->from("mes_customer");
            $this->db->join( "mes_user", "mes_user.user_id = mes_customer.cust_salseperson", 'left');
            $this->db->join( "provinces", "provinces.id = mes_customer.cust_prov", 'left');
            $this->db->join( "districts", "districts.id = mes_customer.cust_distc", 'left');
            $this->db->join( "cities", "cities.id = mes_customer.cust_city", 'left');
            $this->db->join( "customer_route", "customer_route.cus_id = mes_customer.cust_id", 'left');
            $this->db->join( "mes_route", "mes_route.route_id = customer_route.route_id", 'left');
            $this->db->where('cust_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $det['cusdata'] = $data ;
            
                    $this->db->select("*");
                    $this->db->from('mes_user');
                    $this->db->where('user_departmentid','Marketing');
                    //$this->db->where('cust_country',$this->input->post('sup_country'));
                    $detail = $this->db->get();
                    $dup = $detail->result();
                    
                    $det['salseman'] = $dup ;
                    
                    
         
            
            $acta = $act = $inact = $Local= $Foreign="" ;
            if($data[0]->cust_status =="1"){$act = "selected";}else{$inact = "selected";}
            if($data[0]->cust_status =="1"){$acta = "Active";}else{$acta = "Inactive";}
            
                    $cat = $this->product_model->cat_list();
                    $item = $this->product_model->item_list();
                    $brand = $this->product_model->brand_list();
                    $unit = $this->product_model->unit_list();
                    $cuntry = $this->product_model->country_list();
                    $currncy = $this->product_model->currency_list();
                    $province = $this->product_model->province();
                    $city = $this->product_model->cities();
                    $districts = $this->product_model->districts();
                    
                    $det['province'] = $province;
                    $det['active'] = $act;
                    $det['inactive'] = $inact;
                    $det['cusst'] = $acta;

                        	
                       echo json_encode($det);
    	}
    	
    	function cust_updt(){
    	    	    
    	    	    $ret[0]="";
    	    	    $this->form_validation->set_error_delimiters('', ''); 
    	    	    
    	    	  
                        
                        $this->form_validation->set_rules('sup_salse', 'Salse Person', 'required');
                        $this->form_validation->set_rules('sup_namee', 'Name', 'required');
                        $this->form_validation->set_rules('sup_adde', 'Address', 'required');
                        $this->form_validation->set_rules('sup_prove', 'Province', 'required');
                        $this->form_validation->set_rules('sup_dise', 'District', 'required');
                        $this->form_validation->set_rules('sup_citye', 'City', 'required');
                        $this->form_validation->set_rules('sup_routed', 'Route', 'required');
                        //$this->form_validation->set_rules('sup_cperiode', 'Credit Period', 'trim');
                       
                  
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
            	      
            	    $name = preg_replace('/\s+/', '', strtoupper($this->input->post('sup_namee'))); 
                    $add = preg_replace('/\s+/', '', strtoupper($this->input->post('sup_adde')));
                        
                    $this->db->from('mes_customer');
                    $this->db->where("REPLACE(cust_company, ' ', '')=",$name);
                    $this->db->where("REPLACE(cust_addres, ' ', '')=",$add);
                    $this->db->where('cust_uniqid!=', $this->input->post('uid'));
                    $hasil=$this->db->get();
                    $count = $hasil->num_rows(); 
                    
                   /* $this->db->from('mes_customer1');
                    $this->db->where("REPLACE(cust_addres, ' ', '')=",$add);
                    $this->db->where('cust_uniqid!=', $this->input->post('uid'));
                    $hasil2=$this->db->get();
                    $count2 = $hasil2->num_rows(); */
                    
                    if (($count>0)){
    	            $ret[0].= "Customer Cannot Be duplicate ";
    	            $ret[2] = "error";
    	            
                }else{ 
                    
                    
                     $this->db->select('*');
                    $this->db->from('districts');
                    $this->db->where('province_id',$this->input->post('sup_prove'));
                   // $this->db->where('user_status',1);
                    $hasil=$this->db->get();
                    
                    $this->db->select('*');
                    $this->db->from('cities');
                    $this->db->where('district_id',$this->input->post('sup_dise'));
                    $hasilc=$this->db->get();
                    
                    if(($hasil->num_rows()<0) || ($hasilc->num_rows()<0) ){
                            $ret[0] = "Select Correct District or City";
    	                    $ret[2] = "error";
                    }else{
                        
                    $this->db->set('cust_salseperson', $this->input->post('sup_salse'));
                    $this->db->set('cust_company', $this->input->post('sup_namee'));
                    $this->db->set('cust_addres', $this->input->post('sup_adde'));
                    $this->db->set('cust_prov', $this->input->post('sup_prove'));
                    $this->db->set('cust_distc', $this->input->post('sup_dise'));
                    $this->db->set('cust_city', $this->input->post('sup_citye'));
                    $this->db->set('cust_contact', $this->input->post('sup_cone'));
                    $this->db->set('cust_conperson', $this->input->post('sup_connamee'));
                    $this->db->set('cust_creditper', trim($this->input->post('sup_cperiode')));
                    $this->db->set('cust_discount', $this->input->post('sup_dsicoe'));
                    $this->db->set('cust_status', $this->input->post('sup_sts'));
                    $this->db->set('cust_route', $this->input->post('sup_routed'));
                    $this->db->set('cust_euser', $this->session->userdata['login_data']['username']);
                    $this->db->set('cust_edate', date('Y-m-d'));
                    
                        $this->db->where('cust_uniqid', $this->input->post('uid'));
                        $result=$this->db->update('mes_customer');
                        
                        $this->General->cal_credit_limit($this->input->post('uid'));
                
                        if($result){
                            $ret[0].= "Customer Details Updated";


                                $this->db->select('cust_id');
                                $this->db->from('mes_customer');
                                $this->db->where('cust_uniqid',$this->input->post('uid'));
                                $getcus=$this->db->get();
                                 $getcust = $getcus->result();
                            
                        $this->db->select('cus_id');
                                $this->db->from('customer_route');
                                $this->db->where('cus_id',$getcust[0]->cust_id);
                                $getcusrt=$this->db->get();
                                 $getcustrout = $getcusrt->result();
                        
                        if(count($getcustrout)>0){
                             $this->db->set('route_id', $this->input->post('sup_routed'));
                        $this->db->where('cus_id', $getcust[0]->cust_id);
                        $this->db->update('customer_route');
                        }else{
                            $datart = array(
                                'cus_id' => $getcust[0]->cust_id, 
                                'route_id'  => $this->input->post('sup_routed')
                            );
                            $this->db->insert('customer_route',$datart);
                        }         
                         
                       
                    
                        }else{
                            $ret[0].= "Please Re enter all details";
                        }
                    
                    }
                      }
    	       
                    }
                        
                   
            
          
        
    	    $ret[1] = "customer_view";
    	    echo json_encode($ret);	
    	}
    	
    	function cus_auth(){
    	    
    	     $ret[0]="";
    	    
    	            $this->db->set('cust_auth', 1);
                    $this->db->set('cust_auth_user', $this->session->userdata['login_data']['user_id']);
                    $this->db->set('cust_auth_date', date('Y-m-d'));
                    
                        $this->db->where('cust_id ', $this->input->post('id'));
                        $result=$this->db->update('mes_customer');
                        
                         if($result){
                            $ret[0].= "Customer Authorized";
                        }else{
                            $ret[0].= "Please Re enter all details";
                        }
                        
                        $ret[1] = "customer_view";
    	                echo json_encode($ret);	
    	}
    	
    	function cus_cancel(){
    	    
    	    //$cusid = $detl[0]->cusad_cusid;
    	      
                $this->db->select('po_id');
                                $this->db->from('mes_qto');
                                $this->db->where('po_sup_id',$this->input->post('id'));
                                $getcus=$this->db->get();
                                 $getcust = $getcus->result();
                                 
                                 if(count($getcust)>0){
                                     $ret[0].= "Please Re Try";
                                 }else{
                                       $this->db->where('cust_id', $this->input->post('id'));
                                        $result=$this->db->delete('mes_customer');
                                        
                                          if($result){
                                                    $ret[0].= "Customer Deleted";
                                                }else{
                                                    $ret[0].= "Please Re Try";
                                                }
                                 }
                        
                        $ret[1] = "customer_view";
    	                echo json_encode($ret);
    	}
    	
    	function prof_cust($id){
    	    
    	            $this->db->select("mes_customer.*,mes_user.*,mes_customer.cust_company as suctm, mes_user.user_fname as fnme, mes_user.user_lname as lnme, mes_customer.cust_id as cusid ");
                    $this->db->from('mes_customer');
                    $this->db->join( "mes_user", "mes_user.user_id = mes_customer.cust_salseperson", 'left');
                    $this->db->where('cust_id',$id);
                   // $this->db->where('cust_country',$this->input->post('esup_country'));
                    $detail = $this->db->get();
                    $custd = $detail->result();
                    
                   $data["custdata"] = $custd;
    	           $data['outstand'] = $this->common_model->outstand($id);
    	    
    	$data['content'] = ('pages/custprof'); 
		$this->load->view('template',$data);
    	}
    	
    	function cust_getinv(){
    	            
            $this->db->from('mes_qto');
            $this->db->select("*");
            $this->db->where('po_sup_id',$this->input->post('id'));
            $this->db->order_by("po_id", "desc");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            $det[0]="";
            $det[1] ="<tr>
														<th>ID</th>
														<th>Invoice ID</th>
														<th>Item</th>
														<th>Qty</th>
														<th>Free</th>
														<th>Total</th>
														<th>Discount</th>
														<th>Bal To Pay</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    $bal = $this->General->inv_balance($data[$i]->po_id);
                     $x = $i+1;
                    
                   // if($data[$i]->tax_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr>
                              <td>$x</td>
                              <td>".$data[$i]->po_code.$data[$i]->po_id."</td>
                                <td>".$data[$i]->po_tot_items."</td>
                                <td >".$data[$i]->po_tot_qty."</td>
                                <td >".$data[$i]->po_tot_freqty."</td>
                                <td >".$data[$i]->po_final_tot."</td>
                                <td >".$data[$i]->po_discount."</td>
                                <td >".$bal."</td>
                                
                                <td style='text-align: right;'>
                                 <a href = '".base_url()."inv_view_pdf/?id=".$data[$i]->uniq_id."' target='_blank'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_tx_vw/".$data[$i]->po_id."'  data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>View</button></a>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function cust_getrtn(){
            
          
            
            $this->db->select('*');
            $this->db->from('inv_return');
             $this->db->join( "mes_qto", "mes_qto.po_id = inv_return.ret_invnum", 'left');
             $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
             $this->db->where('mes_qto.po_sup_id',$this->input->post('id'));
             $this->db->where('inv_return.ret_proceed',1);
            $this->db->order_by("ret_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            $det[0]="";
            $det[1] ="<tr>
														<th>ID</th>
														<th>Return Number</th>
														<th>Invoice Number</th>
														<th>Reason</th>
														<th <td style='text-align: right;'>Qty</th>
														<th <td style='text-align: right;'>Amount</th>
														<th <td style='text-align: right;'>Date</th>
														<th <td style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
           
                for($i=0; $i<count($data); $i++){
                    
                     $x = $i+1;
                    
                   // if($data[$i]->tax_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr>
                              <td >".$x."</td>
                              <td>".$data[$i]->ret_code.$data[$i]->ret_id."</td>
                                <td>INV".$data[$i]->ret_invnum."</td>
                                <td >".$data[$i]->ret_reason."</td>
                                <td style='text-align: right;'>".$data[$i]->ret_totqty."</td>
                                <td style='text-align: right;'>".$data[$i]->ret_cdate."</td>
                                <td style='text-align: right;'>".$data[$i]->ret_amount."</td>
                               
                                
                                <td style='text-align: right;width:10%;'>
                                 <a href ='".base_url()."pdf_invrtn/".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                </td>
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function cust_getpay(){
            
          
            
            $this->db->select('*');
            $this->db->from('mes_payment_main');
             //$this->db->join( "mes_qto", "mes_qto.po_id = mes_payment.pay_invid", 'left');
             $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
             $this->db->where('mes_payment_main.pay_cust_id',$this->input->post('id'));
            $this->db->order_by("pay_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            $det[0]="";
            $det[1] ="<tr>
														<th>ID</th>
														<th>Slip ID</th>
														<th>Invoice ID</th>
														<th>Type</th>
														<th <td style='text-align: right;'>Value</th>
														<th <td style='text-align: right;'>Date</th>
														<th <td style='text-align: right;'>Status</th>
														<th <td style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
           
                for($i=0; $i<count($data); $i++){
                    
                    
                    $this->db->from('mes_payment');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";
                    for($j=0; $j<count($inv_data); $j++){
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." , ".$inv_;
                    }
                    
                    $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->bank_name."<br>Date : ".$data[$i]->pay_chkdate;
                    
                    
                     $x = $i+1;
                     if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; 
                     }elseif($data[$i]->pay_type==3){
                        $ptype = "Payment Excess"; 
                    }elseif($data[$i]->pay_type==4){
                        $ptype = "Return Excess"; 
                    }else{$ptype = "Cash";}
                    
                    if($data[$i]->pay_stetus==1){
                        if($data[$i]->pay_authrize==0){
                            $sts = "<td style='color:yellow; font-size:25px; text-align: center;'>&#9679;</td>";
                        }else{
                            $sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";
                        }
                    }else{
                        $sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                        
                       $bal = ""; 
                        if($data[$i]->pay_balance!=0.00){
                            $bal = "EXCESS - ".$data[$i]->pay_balance;
                        }
                    $dat =  "
                              <tr>
                              <td >".$x."</td>
                              <td>".$data[$i]->pay_code.$data[$i]->pay_id."</td>
                                <td>".$inv_."</td>
                                 <td>$ptype</td>
                                <td style='text-align: right;'>".number_format($data[$i]->pay_amount,2,".",",")."<br><span style='color:blue'>".$bal."</span></td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                               $sts
                                
                                <td style='text-align: right;width:10%;'>
                                <a href ='".base_url()."payslip/".$data[$i]->pay_uniqid."' target='_blank'>  <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."'   data-backdrop='static' data-keyboard='false'>View</button></a>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function cust_getbalnce(){
            
    //$this->db->select_sum('po_final_tot');
    $this->db->select("(`po_final_tot`) as tot,(`return_amnt`) as ret,po_id");
    $this->db->from('mes_qto');
    $this->db->where('po_sup_id',$this->input->post('id'));
    $this->db->where('po_status',1);
    $this->db->where('po_auth',1);
    //$this->db->limit(75);
    $query = $this->db->get();
    $data= $query->result();
    
    
    
    
            
                        //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            $det[0]="";
            $det[1] ="<tr>
													
														<th>Invoice</th>
														<th style='text-align: right;'>Invoice Total</th>
														<th style='text-align: right;'>Payment Total</th>
														<th style='text-align: right;'>Return Total</th>
														<th style='text-align: right;'>Outstanding Total</th>
													
														
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
              
                    
                     
                    
              for($i=0; $i<count($data); $i++){
                  $this->db->select_sum('pay_amount');
                    $this->db->from('mes_payment');
                            $this->db->where('pay_invid', $data[$i]->po_id);
                            $this->db->where('pay_stetus', 1);
                    $query = $this->db->get();
                    $data2= $query->result();
                    
                    
                    $bal = $data[$i]->tot - ($data[$i]->ret +  $data2[0]->pay_amount);
                    
                    if($bal>0){
                        
                         $dat =  "
                              <tr>
                              
                                <td>INV".$data[$i]->po_id."</td>
                               
                                <td style='text-align: right;'>".number_format($data[$i]->tot,2)."</td> 
                                <td style='text-align: right;'>".number_format($data2[0]->pay_amount,2)."</td>
                                <td style='text-align: right;'>".number_format($data[$i]->ret,2)."</td>
                                <td style='text-align: right;'>".number_format($bal,2)."</td>
                             
                              </tr>";
                              $det[2] =$det[2].$dat;
                    }
                  
                  
             
              }
              
              
                              echo json_encode($det);
        
        }
    	
    	function get_dict(){
           
                $this->db->select('*');
                    $this->db->from('districts');
                    $this->db->where('province_id',$this->input->post('part_id'));
                   // $this->db->where('user_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
         
         $sup = $sls;
            echo json_encode($sup);
        }
        
        function get_city(){
           
                    $this->db->select('*');
                    $this->db->from('cities');
                    $this->db->where('district_id',$this->input->post('part_id'));
                   // $this->db->where('user_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
         
         $sup = $sls;
            echo json_encode($sup);
        }
    	
    	function cust_add_sev(){
                    
                
                    
                    $this->form_validation->set_error_delimiters('', ''); 
       
                                //$this->form_validation->set_rules('sup_name', 'Name', 'required');
                                $this->form_validation->set_rules('cust_add', 'Address Line I', 'required');
                                $this->form_validation->set_rules('cust_add1', 'Address Line II', 'required');
                                $this->form_validation->set_rules('cust_add2', 'Address Line III', 'required');
                                
                   
                        
                
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
                   
    	   }else{
    	       
    	      // if ($this->input->post('sup_tax')== checked){$taxval = "1";}else{$taxval = "0";}
    	      // $taxval = isset($_POST['cust_tax']) && $_POST['cust_tax']  ? "1" : "0";
    	       
    	       $data = array(
                'cusad_cusid' => $this->input->post('cust_id'), 
                'cusad_add1'  => $this->input->post('cust_add'),
                'cusad_add2'  => $this->input->post('cust_add1'),
                'cusad_add3'  => $this->input->post('cust_add2')
            );
            $result = $this->db->insert('cust_address',$data);
                if($result){
                    $ret[0] = "New Customer Address Saved";
                    
                    $this->db->set('cust_deladd', 1);
                    $this->db->where('cust_id', $this->input->post('cust_id'));
                    $result=$this->db->update('mes_customer');
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
                   

          
    	   
    	      //  $ret[1] = "customer_view";
            echo json_encode($ret);
                }
                
        function cust_add_view(){
                    
                   // $data=$this->product_model->product_list();
                    
            $this->db->select("*");
            $this->db->from('cust_address');
            $this->db->where('cusad_cusid',$this->input->post('id'));
            //$this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_customer.cust_currency", 'left');
            //$this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'left');
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            

            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>Delivery Address</th>
													
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
              $x = $i +1;
                    $dat =  "
                              <tr scope='row'>
                              
                              <td >$x</td>
                               
                                <td><input type='hidden' name='cusad_id' id='cusad_id' class='form-control' value='".$data[$i]->cusad_id."' >
                                <input type='text' style='border: none;' name='cusad_add' id='cusad_add".$data[$i]->cusad_id."' class='form-control' value='".$data[$i]->cusad_add1."' >\n
                                <input type='text' style='border: none;' name='cusad_add1' id='cusad_addone".$data[$i]->cusad_id."' class='form-control' value='".$data[$i]->cusad_add2."' >\n
                                <input type='text' style='border: none;' name='cusad_add2' id='cusad_addtwo".$data[$i]->cusad_id."' class='form-control' value='".$data[$i]->cusad_add3."' ></td>
                               
                            
                                
                                
                               
                              
                                
                                
                                <td>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='update_cusaddress/".$data[$i]->cusad_id."' onclick='edit_savecus(this.id)' >Edit</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_delcusadd/".$data[$i]->cusad_id."' onclick='edit_delcus(this.id)' >Delete</button>
                               
                                </td>
                               
                            
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function cust_add_updt(){
    	    	    
    	    	    $ret[0]="";
    	    	    $this->form_validation->set_error_delimiters('', ''); 
    	    	    
    	    	  
                        
                        $this->form_validation->set_rules('add1', 'Address I', 'required');
                        $this->form_validation->set_rules('add2', 'Address II', 'required');
                        $this->form_validation->set_rules('add3', 'Address III', 'required');
                      
                  
                
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
            	       $this->db->set('cusad_add1', $this->input->post('add1'));
            	        $this->db->set('cusad_add2', $this->input->post('add2'));
            	        $this->db->set('cusad_add3', $this->input->post('add3'));
            	   
    	    $this->db->where('cusad_id', $this->input->post('adid'));
            $result=$this->db->update('cust_address');
                
                if($result){
                    $ret[0].= "Delivery Address Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
                        
                   
            
          
        
    	    $ret[1] = "customer_view";
    	  echo json_encode( $ret);	
    	}
    	
    	function delete_deladd(){
    	    
    	            $this->db->select("*");
                    $this->db->from('cust_address');
                    $this->db->where('cusad_id',$this->input->post('adid'));
                    $detail = $this->db->get();
                    $detl = $detail->result();
                   // print_r($detl);
    	      
    	       $cusid = $detl[0]->cusad_cusid;
    	      
                $this->db->where('cusad_id', $this->input->post('adid'));
                $result=$this->db->delete('cust_address');
                
                if($result){
                    $ret[0] = "Delivery Address Details Deleted";
                    
                    $this->db->select("*");
                    $this->db->from('cust_address');
                    $this->db->where('cusad_cusid',$cusid);
                    $detail = $this->db->get();
                    $count = $detail->num_rows();
                    if($count <1){
                        
                        $this->db->set('cust_deladd', 0);
                        $this->db->where('cust_id', $cusid);
                        $this->db->update('mes_customer');
                    }
                    
                }else{
                    $ret[0] = "Delete Error";
                }
                echo json_encode($ret);
                
               

    	}
    	
    	function cust_get(){
    	    
    	    $this->db->select("mes_customer.cust_id as custmid,mes_customer.cust_company as cusname,mes_customer.cust_addres as cusadrs");
    	    $this->db->from('mes_customer');
    	    //$this->db->join('customer_route', 'customer_route.cus_id   = mes_customer.cust_route', 'left');
            //$this->db->join('mes_route', 'customer_route.route_id   = mes_route.route_id', 'left');
    	    $this->db->where('cust_salseperson',$this->input->post('part_id'));
    	    $this->db->where('cust_status',1);
            $detailtax = $this->db->get();
            $cust = $detailtax->result();
            
             $this->db->select("*");
    	    $this->db->from('mes_route');
    	    //$this->db->join('customer_route', 'customer_route.cus_id   = mes_customer.cust_route', 'left');
            //$this->db->join('mes_route', 'customer_route.route_id   = mes_route.route_id', 'left');
    	    $this->db->where('ref_id',$this->input->post('part_id'));
    	    //$this->db->where('cust_status',1);
            $detailtax1 = $this->db->get();
            $rout = $detailtax1->result();
            
         
            
            
            $this->db->select('user_depoid');
                    $this->db->from('mes_user');
                    $this->db->where('user_id',$this->input->post('part_id'));
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
             $slsm = $sls[0]->user_depoid;
            
             if($slsm!=0){
                
                
                    
                $this->db->select('*');
                    $this->db->from('mes_depot_stock');
                    $this->db->where('dpstock_dpoid',$sls[0]->user_depoid);
                        $this->db->join('mes_item', 'mes_depot_stock.dpstock_itemid   = mes_item.item_id','left');
                        $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
                        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
                        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat",'left');
                        $this->db->where('mes_depot_stock.dpstock_qty>',0);
                        $this->db->where('mes_item.item_status',1);
                        $this->db->order_by("mes_item.item_id", "asc");
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
            }else{
                $this->db->select('*');
                    $this->db->from('mes_stock');
                        $this->db->join('mes_item', 'mes_stock.stc_itemid   = mes_item.item_id','left');
                        $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
                        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
                        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat",'left');
                        $this->db->where('mes_stock.stc_qty>','0');
                        $this->db->where('mes_item.item_status',1);
                        $this->db->order_by("mes_item.item_id", "asc");
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
            }
                    
                   
            
            $data['item'] = $resultitem;
            
            $data['customer'] = $cust;
            
            $data['route'] = $rout;
            
            echo json_encode($data);
    	}
                
    	function chk_credit_limit(){
    	    $detail = $this->General->credit_limit();
    	    echo $detail;
    	}
    	
    	function cust_credit_limit(){
    	    $detail = $this->General->cal_credit_limit(1862);
    	    echo $detail;
    	}
    	
    	function  get_route_cus(){
            
                    $this->db->select("*");
                    $this->db->from('mes_route');
                    $this->db->where('ref_id', $this->input->post('sup_id'));
                 
                    //$this->db->where('cust_country',$this->input->post('sup_country'));
                    $detail = $this->db->get();
                    $rot = $detail->result();
                    
                    $data[0] = $rot;
                    
                    $this->db->select("*");
                    $this->db->from('mes_route');
                    $this->db->where('ref_id', $this->input->post('tosup_id'));
                 
                    //$this->db->where('cust_country',$this->input->post('sup_country'));
                    $detail = $this->db->get();
                    $rot2 = $detail->result();
                    
                    $data[1] = $rot2;
                    
                    echo json_encode($data);
        }
        
            	function get_route(){
            
                    $this->db->select("*");
                    $this->db->from('mes_route');
                    $this->db->where('ref_id', $this->input->post('sup_id'));
                 
                    //$this->db->where('cust_country',$this->input->post('sup_country'));
                    $detail = $this->db->get();
                    $rot = $detail->result();
                    
                    $data = $rot;
                    
                    //$this->db->select("*");
                   // $this->db->from('mes_route');
                    //$this->db->where('ref_id', $this->input->post('tosup_id'));
                 
                    //$this->db->where('cust_country',$this->input->post('sup_country'));
                   // $detail = $this->db->get();
                   // $rot2 = $detail->result();
                    
                    //$data[1] = $rot2;
                    
                    echo json_encode($data);
        }
    	
    	
                
}