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

class Supplier_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        $this->load->model('product_model');
     
      
         
        }
        
         function index(){
            
        $data['content'] = ('pages/supplier'); 
		$this->load->view('template',$data);
            
        }
        
   	// supp data
    	
                function sup_viw(){
                    
                   // $data=$this->product_model->product_list();
                    
            $this->db->from('mes_suppliers');
            $this->db->select("mes_suppliers.*, mes_currancy.*,mes_country.*");
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_suppliers.sup_currency", 'inner');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_suppliers.sup_country", 'inner');
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_supplier' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New Supplier</button></br>";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>Supplier</th>
														<th>Contact Details</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]->sup_tax==1){$tax="&#10003;";}else{$tax="";}
                    if($data[$i]->sup_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]->sup_conperson2!=""){$con2 = "<br>".$data[$i]->sup_conperson2;}else{$con2="";}
                    $dat =  "
                              <tr scope='row'><td >".$data[$i]->sup_idtext.$data[$i]->sup_id."</td>
                               
                                <td>".$data[$i]->sup_company."</td>
                                <td>".$data[$i]->sup_addres.' '.$data[$i]->sup_add1.' '.$data[$i]->sup_add2.'<br>'.$data[$i]->cnty_name.'<br>'.$data[$i]->sup_contact.$con2.' - '.$data[$i]->sup_conperson.'<br>'.$data[$i]->sup_email."</td>
                                
                                
                                <td style='color:Green;font-size:20px;'>".$tax."</td>
                                <td>".$data[$i]->curr_symble."</td>
                                <td>".$data[$i]->sup_type."</td>
                                $sts
                                
                                
                                <td>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_supplier/".$data[$i]->sup_id."' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
                function sup_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();
                    
                    
                    
                    
                    
                    $det="";
	                $det.='
	        
	                    <div class="form-group row">
                                <label class="col-md-2 col-form-label">Name / Company</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_name" id="sup_name" class="form-control" value="">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line I</label>
                                <div class="col-md-10">
                                  <!--textarea id="sup_add" name="sup_add" rows="4" placeholder="" class="form-control"></textarea-->
                                  <input type="text" name="sup_add" id="sup_add" class="form-control" value="">
	        
                                 </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line II</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_add1" id="sup_add1" class="form-control" value="">
                                 </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line III</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_add2" id="sup_add2" class="form-control" value="">
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Number I</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 Number II</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_con2" id="sup_con2" 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">Email</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_emil" id="sup_emil" class="form-control" value="">
                                 </div>
                            </div>
                            
                            
             <div class="form-group row">
               
               <input type="hidden" name="table" id="table" class="form-control" readonly="" value="save_supplier">
                                
                                
                                
                                <label class="col-md-2 col-form-label">Country</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="sup_country" name="sup_country">
                                  <option value="" selected>Select</option>'; 
                                  
                                  
                                     foreach($cuntry as $value) {
                                                $det.='<option value="'.$value->cnty_id.'">'.$value->cnty_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                          
                                           
                                   
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Currency</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="sup_cury" name="sup_cury">
                                  <option value="" selected>Select</option>'; 
                                     foreach($currncy as $value) {
                                                $det.='<option value="'.$value->curr_id.'">'.$value->curr_symble.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Tax</label>
                                <div class="col-md-10">
                                  <input type="checkbox" id="sup_tax" name="sup_tax" value="1" class="form-check-input">Taxable
                                
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Type</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="sup_type" name="sup_type">
                                  <option value="" selected>Select</option>
                                  <option value="Local" selected>Local</option>
                                  <option value="Foreign" selected>Foreign</option>
                                        </select>
                                 </div>
                            </div> 
                            ';
                        	
                       echo json_encode($det);
                }
                
                function sup_sav(){
                    
                    $this->db->select("*");
                    $this->db->from('mes_suppliers');
                    $this->db->where('sup_company',$this->input->post('sup_name'));
                    $this->db->where('sup_country',$this->input->post('sup_country'));
                    $detail = $this->db->get();
                    //$dup = $detail->result();
                    
                    $this->form_validation->set_error_delimiters('', ''); 
                    
                    if($this->input->post('sup_type')=="Foreign"){
                        
                         $this->form_validation->set_rules('sup_con', 'Contact Number', 'required');
                         $this->form_validation->set_rules('sup_emil', 'Email', 'required');
                         
                         
                    }else{
                        
                              //$this->form_validation->set_rules('sup_name', 'Name', 'required');
                                $this->form_validation->set_rules('sup_add', 'Address', 'required');
                                $this->form_validation->set_rules('sup_type', 'Type', 'required');
                                $this->form_validation->set_rules('sup_cury', 'Currency', 'required');
                                $this->form_validation->set_rules('sup_country', 'Country', 'required');
                    }
                        
                
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
                   
    	   }else if($detail->num_rows()>0){
    	       $ret[0] = "Same Supplier cannot be saved";
    	       $ret[2] = "error";
    	       }else{
    	       
    	      // if ($this->input->post('sup_tax')== checked){$taxval = "1";}else{$taxval = "0";}
    	       $taxval = isset($_POST['sup_tax']) && $_POST['sup_tax']  ? "1" : "0";
    	       
    	       $data = array(
                'sup_company' => $this->input->post('sup_name'), 
                'sup_addres'  => $this->input->post('sup_add'),
                'sup_add1'  => $this->input->post('sup_add1'),
                'sup_add2'  => $this->input->post('sup_add2'),
                'sup_contact'  => $this->input->post('sup_con'),
                'sup_conperson2'  => $this->input->post('sup_con2'),
                'sup_email'  => $this->input->post('sup_emil'),
                'sup_country'  => $this->input->post('sup_country'),
                'sup_currency'  => $this->input->post('sup_cury'),
                'sup_type'  => $this->input->post('sup_type'),
                'sup_tax'  => $taxval,
                'sup_conperson'  => $this->input->post('sup_conname'),
                'sup_cuser'  => "admin",
                'sup_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_suppliers',$data);
                if($result){
                    $ret[0] = "New Supplier Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
                   

          
    	   
    	        $ret[1] = "supplier_view";
            echo json_encode($ret);
                }
                
                
                
                function sup_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_suppliers'); 
            $this->db->select("mes_suppliers.*, mes_currancy.*,mes_country.*");
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_suppliers.sup_currency", 'inner');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_suppliers.sup_country", 'inner');
            $this->db->where('sup_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = $Local= $Foreign="" ;
            if($data[0]->sup_status =="1"){$act = "selected";}else{$inact = "selected";}
            if($data[0]->sup_type =="Local"){$Local = "selected";}else{$Foreign = "selected";}
            if($data[0]->sup_tax =="1"){$tax = 'checked';}else{$tax = "";}
    	    
                    $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();
                    
                    
                    
                    
                    
                    $det="";
	                $det.='
	        
	                    <div class="form-group row">
                                <label class="col-md-2 col-form-label">Name / Company</label>
                                <div class="col-md-10">
                                  <input type="text" name="esup_name" id="esup_name" class="form-control" value="'.$data[0]->sup_company.'" >
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line I</label>
                                <div class="col-md-10">
                                  <!--textarea id="esup_add" name="esup_add" rows="4" placeholder="" class="form-control">'.$data[0]->sup_addres.'</textarea-->
                                  <input type="text" name="esup_add" id="esup_add" class="form-control" value="'.$data[0]->sup_addres.'">
	        
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line II</label>
                                <div class="col-md-10">
                                  <!--textarea id="esup_add" name="esup_add" rows="4" placeholder="" class="form-control">'.$data[0]->sup_addres.'</textarea-->
                                  <input type="text" name="esup_add1" id="esup_add1" class="form-control" value="'.$data[0]->sup_add1.'">
	        
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line III</label>
                                <div class="col-md-10">
                                  <!--textarea id="esup_add" name="esup_add" rows="4" placeholder="" class="form-control">'.$data[0]->sup_addres.'</textarea-->
                                  <input type="text" name="esup_add2" id="esup_add2" class="form-control" value="'.$data[0]->sup_add2.'">
	        
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Number I</label>
                                <div class="col-md-10">
                                  <input type="text" name="esup_con" id="esup_con" class="form-control" value="'.$data[0]->sup_contact.'">
                                 </div>
                            </div>
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Number II</label>
                                <div class="col-md-10">
                                  <input type="text" name="esup_con2" id="esup_con2" class="form-control" value="'.$data[0]->sup_conperson2.'">
                                 </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="esup_conname" id="esup_conname" class="form-control" value="'.$data[0]->sup_conperson.'">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Email</label>
                                <div class="col-md-10">
                                  <input type="text" name="esup_emil" id="esup_emil" class="form-control" value="'.$data[0]->sup_email.'">
                                 </div>
                            </div>
                            
                            
             <div class="form-group row">
               
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_supplier">
                                <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->sup_id.'">
                                
                                
                                <label class="col-md-2 col-form-label">Country</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="esup_country" name="esup_country">'; 
                                  $det.='<option value="'.$data[0]->sup_country.'" selected>'.$data[0]->cnty_name.'</option>';
                                  
                                  
                                     foreach($cuntry as $value) {
                                                $det.='<option value="'.$value->cnty_id.'">'.$value->cnty_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                          
                                           
                                   
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Currency</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="esup_cury" name="esup_cury">'; 
                                  $det.='<option value="'.$data[0]->sup_currency.'">'.$data[0]->curr_symble.'</option>';
                                     foreach($currncy as $value) {
                                                $det.='<option value="'.$value->curr_id.'">'.$value->curr_symble.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Tax</label>
                                <div class="col-md-10">
                                  <input type="checkbox" id="esup_tax" name="esup_tax"  class="form-check-input" '.$tax.' value="1">Taxable
                                
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Type</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="esup_type" name="esup_type">
                                  <option value="" selected>Select</option>
                                  <option value="Local" '.$Local.'>Local</option>
                                  <option value="Foreign" '.$Foreign.'>Foreign</option>
                                        </select>
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Status</label>
                                <div class="col-md-10">
                                      <select class="form-control" id="estatus" name="estatus">
                                     
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>
                            
                   
                          
                            
                            ';
                        	
                       echo json_encode($det);
    	}
    	
    	    	function sup_updt(){
    	    	    
    	    	    $this->db->select("sup_company");
                    $this->db->from('mes_suppliers');
                    $this->db->where('sup_company',$this->input->post('esup_name'));
                    $this->db->where('sup_country',$this->input->post('esup_country'));
                    $detail = $this->db->get();
    	    	    
    	    	    $ret[0]="";
    	    	    $this->form_validation->set_error_delimiters('', ''); 
    	    	    
    	    	     if($this->input->post('esup_type')=="Foreign"){
                        
                         $this->form_validation->set_rules('esup_con', 'Contact Number', 'required');
                         $this->form_validation->set_rules('esup_emil', 'Email', 'required');
                         
                    }else{
                        
                        $this->form_validation->set_rules('esup_name', 'Name', 'required');
                        $this->form_validation->set_rules('esup_add', 'Address', 'required');
                        $this->form_validation->set_rules('esup_type', 'Type', 'required');
                        $this->form_validation->set_rules('esup_cury', 'Currency', 'required');
                        $this->form_validation->set_rules('esup_country', 'Country', 'required');
                    }
                
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
            	       if($detail->num_rows()>0){
            	           $ret[0].="Only unique Company and Country will be saved
            	           ";
            	       
            	       }else{
            	            $this->db->set('sup_company', $this->input->post('esup_name'));
            	        $this->db->set('sup_country', $this->input->post('esup_country'));
            	       }
    	       
    	       if($this->input->post('esup_tax')==="1"){$tax = "1";}else{$tax = "0";}
    	       
    	       
                $this->db->set('sup_addres', $this->input->post('esup_add'));
                $this->db->set('sup_add1', $this->input->post('esup_add1'));
                $this->db->set('sup_add2', $this->input->post('esup_add2'));
                $this->db->set('sup_contact', $this->input->post('esup_con'));
                $this->db->set('sup_conperson2', $this->input->post('esup_con2'));
                $this->db->set('sup_email', $this->input->post('esup_emil'));
                $this->db->set('sup_currency', $this->input->post('esup_cury'));
                $this->db->set('sup_type', $this->input->post('esup_type'));
                $this->db->set('sup_tax', $tax);
                $this->db->set('sup_conperson', $this->input->post('esup_conname'));
                $this->db->set('sup_status', $this->input->post('estatus'));
                $this->db->set('sup_euser', "admin");
                $this->db->set('sup_edate', date('Y-m-d'));
                
      
                
                
                $this->db->where('sup_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_suppliers');
                
                if($result){
                    $ret[0].= "Supplier Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
                        
                   
            
          
        
    	    $ret[1] = "supplier_view";
    	    echo json_encode($ret);	
    	}
 
            function calculator(){
                $data['content'] = ('pages/cal');
                $this->load->view('template',$data);
            }
        
}
        
?>