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

class Settings_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        
        
        $this->load->model('Product_model');
         if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
        }
        
	public function index()
	{
	    
	  
		//$this->load->view('pages/setting');
		 $data['content'] = ('pages/setting'); 
		$this->load->view('template',$data);
	}
      public function test()
	{
	    
	  
		//$this->load->view('pages/setting');
		 //$data['content'] = ('pages/test'); 
		$this->load->view('pages/test');
	}
        
        
        public function logout(){
            //$this->session->unset_userdata('login_data');
            //$this->index();
        }
        
        function tax_view(){
             $this->db->from('mes_tax');
            $this->db->select("*");
            $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[1] ="<tr>
														<th >ID</th>
														<th>Tax Name</th>
														<th style='text-align: right;'>Tax Value (%)</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    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>".$data[$i]->tax_code.$data[$i]->tax_id."</td>
                                <td>".$data[$i]->tax_name."</td>
                                <td style='text-align: right;'>".$data[$i]->tax_value."</td>
                                $sts
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_tx_vw/".$data[$i]->tax_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function tax_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_tax'>
                                <label class='col-md-2 col-form-label'>Tax Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='tax_name' id='tax_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Tax Value (%)</label>
                                <div class='col-md-10'>
                                  <input type='text' name='tax_value' id='tax_value' class='form-control' placeholder=''>
                                 </div>
                            </div>
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function tax_sv(){
            $name =  $this->input->post('tax_name');
    	   $des =  $this->input->post('tax_value');
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('tax_name', 'Tax Name', 'trim|required|is_unique[mes_tax.tax_name]|strtoupper');
                $this->form_validation->set_rules('tax_value', 'Tax Value', 'numeric|required');
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'tax_name' => strtoupper($name), 
                'tax_value'  => $des,
                'tax_cuser'  => "admin",
                'tax_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_tax',$data);
                if($result){
                    $ret[0] = "New Tax Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_tax";
                echo json_encode($ret);	
        }
        
        function tax_edt(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_tax'); 
            $this->db->select("*");
            $this->db->where('tax_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->tax_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->tax_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="tax_sv_data">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Tax Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="editname" id="editname" class="form-control" placeholder="" value="'.$data[0]->tax_name .'">
                                 </div>
                            </div>
                <div class="form-group row">
                                <label class="col-md-2 col-form-label">Tax Value</label>
                                <div class="col-md-10">
                                  <input type="text" name="editval" id="editval" class="form-control" placeholder="" value="'.$data[0]->tax_value.'">
                                 </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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>             
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
        
        function tax_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editname', 'Tax Name', 'required');
                $this->form_validation->set_rules('editval', 'Tax Value', 'numeric|required');
                
                
             $ret[0] = "";
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	       $this->form_validation->set_rules('editname', 'Name', 'required|is_unique[mes_tax.tax_name]');
    	       if ($this->form_validation->run('editname') == FALSE){
    	            $ret[0].= "Unique Name wont updated.
    	            ";
                }else{
                    $this->db->set('tax_name', strtoupper($this->input->post('editname')));
                }
    	       
                $this->db->set('tax_value', $this->input->post('editval'));
               // $this->db->set('tax_status', $this->input->post('status'));
                $this->db->set('tax_status', $this->input->post('status'));
                $this->db->set('tax_euser', "sadmin");
                $this->db->set('tax_edate', date('Y-m-d'));
                $this->db->where('tax_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_tax');
                
                if($result){
                    $ret[0].= "Tax Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_tax";
    	    echo json_encode($ret);	
    	}
    	
    	//city 
    	
    		function city_view(){
            
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_city' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New City</button></br>";
            
            $det[1] ="";
            
            $det[2]="";
               
                
            
            	
           echo json_encode($det);
        }
        
            function city_add(){
                
            $this->db->from('districts');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data1 = $detailtax->result();
            
            
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_city'>
                                <label class='col-md-2 col-form-label'>City Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='city_name' id='city_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                       <div class='form-group row'>
                             <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_termsdes'>
                                <label class='col-md-2 col-form-label'>District</label>
                                <div class='col-md-10'>
                                  <select class='form-control' id='dict' name='dict'>
                                  <option value=''>Select District</option>";
                                     foreach($data1 as $value) {
                                                $det.='<option value="'.$value->id.'">'.$value->name_en.'</option>';
                                            }     
                                  
            $det.="                              
                                        </select>
                                 </div>
                            </div>
                                     ";
                        
                	
               echo json_encode($det);	
        }
        
            function city_save(){
                
            $name =  $this->input->post('bnk_name');
    	  
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('city_name', 'City Name', 'trim|required|is_unique[cities.name_en]|strtoupper');
                $this->form_validation->set_rules('dict', 'District', 'required');
                
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'district_id' => $this->input->post('dict'), 
                'name_en'  => $this->input->post('city_name')
            );
            $result = $this->db->insert('cities',$data);
                if($result){
                    $ret[0] = "New City Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_city";
                echo json_encode($ret);	
        }
    	
    	//bank 
    	
    	function bank_view(){
             $this->db->from('mes_bank');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_bank' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Bank</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Bank Name</th>
													
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    if($data[$i]->bank_stetus==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>".$data[$i]->bank_code.$data[$i]->bank_id."</td>
                                <td>".$data[$i]->bank_name."</td>
                                
                                $sts
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_bank/".$data[$i]->bank_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function bank_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_bank'>
                                <label class='col-md-2 col-form-label'>Bank Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='bnk_name' id='bnk_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                         
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function bank_sv(){
            $name =  $this->input->post('bnk_name');
    	  
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('bnk_name', 'Bank Name', 'trim|required|is_unique[mes_bank.bank_name]|strtoupper');
                
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'bank_name' => strtoupper($name), 
                'bank_uid'  => random_string('alnum',20),
                'bank_cuser'  => $this->session->userdata['login_data']['username'],
                'bank_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_bank',$data);
                if($result){
                    $ret[0] = "New Bank Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_bank";
                echo json_encode($ret);	
        }
        
        function bank_edt(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_bank'); 
            $this->db->select("*");
            $this->db->where('bank_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->bank_stetus=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->bank_uid.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="updt_bank">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Bank Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="editbname" id="editbname" class="form-control" placeholder="" value="'.$data[0]->bank_name .'">
                                 </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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>             
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	function bank_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editbname', 'Bank Name', 'required');
           
                
                
             $ret[0] = "";
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	       $this->form_validation->set_rules('editbname', 'Name', 'required|is_unique[mes_bank.bank_name]');
    	       if ($this->form_validation->run('editbname') == FALSE){
    	            $ret[0].= "Unique Name wont updated.
    	            ";
                }else{
                    $this->db->set('bank_name', strtoupper($this->input->post('editbname')));
                }
    	       
                $this->db->set('bank_name', strtoupper($this->input->post('editbname')));
               // $this->db->set('tax_status', $this->input->post('status'));
                $this->db->set('bank_stetus', $this->input->post('status'));
                $this->db->set('bank_euser', $this->session->userdata['login_data']['username']);
                $this->db->set('bank_edate', date('Y-m-d'));
                $this->db->where('bank_uid', $this->input->post('data_id'));
                $result=$this->db->update('mes_bank');
                
                if($result){
                    $ret[0].= "Bank Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_bank";
    	    echo json_encode($ret);	
    	}
    	
    	        //expenses
        
        function exp_view(){
             $this->db->from('mes_expreson');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_exp' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Expenses Type</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Exp Type</th>
													
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    if($data[$i]->exp_stetus==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>".$data[$i]->exp_code.$data[$i]->exp_id ."</td>
                                <td>".$data[$i]->exp_reson."</td>
                                
                                $sts
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_exp/".$data[$i]->exp_id ."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function exp_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_exp'>
                                <label class='col-md-2 col-form-label'>Expenses Type</label>
                                <div class='col-md-10'>
                                  <input type='text' name='exp_name' id='exp_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                         
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function exp_save(){
            $name =  $this->input->post('exp_name');
    	  
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('exp_name', 'Expenses Type', 'trim|required|is_unique[mes_bank.bank_name]');
                
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'exp_reson' => $name, 
               // 'bank_uid'  => random_string('alnum',20),
                'exp_cuser'  => $this->session->userdata['login_data']['username'],
                'exp_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_expreson',$data);
                if($result){
                    $ret[0] = "New Expenses Type Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_exp";
                echo json_encode($ret);	
        }
        
        function exp_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_expreson'); 
            $this->db->select("*");
            $this->db->where('exp_id ',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->exp_stetus=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->exp_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="updt_exp">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Expenses Type</label>
                                <div class="col-md-10">
                                  <input type="text" name="editexname" id="editexname" class="form-control" placeholder="" value="'.$data[0]->exp_reson .'">
                                 </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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>             
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	function exp_update(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editexname', 'Expenses Type', 'required');
           
                
                
             $ret[0] = "";
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	       $this->form_validation->set_rules('editexname', 'Expenses Type', 'required|is_unique[mes_expreson.exp_reson]');
    	       if ($this->form_validation->run('editexname') == FALSE){
    	            $ret[0].= "Unique Expenses Type wont updated.
    	            ";
                }else{
                    $this->db->set('exp_reson', ($this->input->post('editexname')));
                }
    	       
                $this->db->set('exp_reson', ($this->input->post('editexname')));
               // $this->db->set('tax_status', $this->input->post('status'));
                $this->db->set('exp_stetus', $this->input->post('status'));
                $this->db->set('exp_euser', $this->session->userdata['login_data']['username']);
                $this->db->set('exp_edate', date('Y-m-d'));
                $this->db->where('exp_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_expreson');
                
                if($result){
                    $ret[0].= "Expenses Type Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_exp";
    	    echo json_encode($ret);	
    	}
        
        
    	
    	//depot
    	
    	            function dpo_view(){
             $this->db->from('mes_depot');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_dpo' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Depot</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Depot Name</th>
														<th >Depot City</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    if($data[$i]->dep_stetus==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>".$data[$i]->dep_code.$data[$i]->dep_id."</td>
                                <td>".$data[$i]->dep_name."</td>
                                <td >".$data[$i]->dep_city."</td>
                                $sts
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_dpo/".$data[$i]->dep_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
                    function dpo_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_dpo'>
                                <label class='col-md-2 col-form-label'>Depot Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='dpo_name' id='dpo_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Depot City</label>
                                <div class='col-md-10'>
                                  <input type='text' name='dpo_city' id='dpo_city' class='form-control' placeholder=''>
                                 </div>
                            </div>
                        
              ";
                        
                	
               echo json_encode($det);	
        }
                    
                    function dpo_sv(){
           // $name =  $this->input->post('tax_name');
    	   //$des =  $this->input->post('tax_value');
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('dpo_name', 'Depot Name', 'required|is_unique[mes_depot.dep_name]');
                //$this->form_validation->set_rules('tax_value', 'Tax Value', 'numeric|required');
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'dep_name' => $this->input->post('dpo_name'), 
                'dep_city'  => $this->input->post('dpo_city'),
                'dep_cuser'  => "admin",
                'dep_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_depot',$data);
                if($result){
                    $ret[0] = "New Depot Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_dpo";
                echo json_encode($ret);	
        }
        
                    function dpo_edt(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_depot'); 
            $this->db->select("*");
            $this->db->where('dep_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->dep_stetus=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->dep_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="updt_dpo">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Depot Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="editname" id="editname" class="form-control" placeholder="" value="'.$data[0]->dep_name .'">
                                 </div>
                            </div>
                <div class="form-group row">
                                <label class="col-md-2 col-form-label">Depot City</label>
                                <div class="col-md-10">
                                  <input type="text" name="editval" id="editval" class="form-control" placeholder="" value="'.$data[0]->dep_city.'">
                                 </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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>             
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	            function dpo_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editname', 'Depot Name', 'required');
                //$this->form_validation->set_rules('editval', 'Tax Value', 'numeric|required');
                
                
             $ret[0] = "";
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	       $this->form_validation->set_rules('editname', 'Depot Name', 'required|is_unique[mes_depot.dep_name]');
    	       if ($this->form_validation->run('editname') == FALSE){
    	            $ret[0].= "Unique Name wont updated.
    	            ";
                }else{
                    $this->db->set('dep_name', strtoupper($this->input->post('editname')));
                }
    	       
                $this->db->set('dep_name', $this->input->post('editname'));
                $this->db->set('dep_city', $this->input->post('editval'));
                $this->db->set('dep_stetus', $this->input->post('status'));
                $this->db->set('dep_euser', "sadmin");
                $this->db->set('dep_edate', date('Y-m-d'));
                $this->db->where('dep_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_depot');
                
                if($result){
                    $ret[0].= "Depot Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_dpo";
    	    echo json_encode($ret);	
    	}
        
        
        //return reason 
        
        function rereson_view(){
             $this->db->from('mes_ret_reason');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='add_rereson' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Return Reason</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Return Reason</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    if($data[$i]->ret_reason_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>".$data[$i]->ret_reason_code.$data[$i]->ret_reason_id."</td>
                                <td>".$data[$i]->ret_reason."</td>
                               
                                $sts
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_rereson/".$data[$i]->ret_reason_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function rereson_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='sv_rereson'>
                                <label class='col-md-2 col-form-label'>Return Reason</label>
                                <div class='col-md-10'>
                                  <input type='text' name='res_name' id='res_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <!--div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Tax Value (%)</label>
                                <div class='col-md-10'>
                                  <input type='text' name='tax_value' id='tax_value' class='form-control' placeholder=''>
                                 </div>
                            </div-->
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function rereson_save(){
            $rereson =  $this->input->post('res_name');
    	   //$des =  $this->input->post('tax_value');
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('res_name', 'Tax Name', 'required|is_unique[mes_ret_reason.ret_reason]');
                
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'ret_reason' => $this->input->post('res_name'), 
                'ret_reason_cuser'  => "admin",
                'ret_reason_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_ret_reason',$data);
                if($result){
                    $ret[0] = "New Reason Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_rereson";
                echo json_encode($ret);	
        }
        
        function rereson_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_ret_reason'); 
            $this->db->select("*");
            $this->db->where('ret_reason_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->ret_reason_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->ret_reason_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="updt_rereson">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Return Reason</label>
                                <div class="col-md-10">
                                  <input type="text" name="editrereson" id="editrereson" class="form-control" placeholder="" value="'.$data[0]->ret_reason .'">
                                 </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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>             
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	function rereson_updt(){
            
               $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editrereson', 'Return Reason', 'required|is_unique[mes_ret_reason.ret_reason]');
                
                
                if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $this->db->set('ret_reason', $this->input->post('editrereson'));
                
                $this->db->where('ret_reason_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_ret_reason');
                
                if($result){
                    $ret[0] = "Return Reason Updated";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_rereson";
    	    echo json_encode($ret);
    	}
        
        //trms data
        
        function termscat_view(){
             $this->db->from('mes_termcat');
            $this->db->select("*");
            $this->db->order_by('tcat_name','ASC');
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='add_termscat' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New TC Catagory</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>TC Catagory</th>
														<th style='text-align: center;'>Action</th>
														
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
            
                    $dat =  "
                              <tr><td>".$data[$i]->tcat_id."</td>
                                <td>".$data[$i]->tcat_name."</td>
                                
                               
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_termscat/".$data[$i]->tcat_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function termscat_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_termscatr'>
                                <label class='col-md-2 col-form-label'>TC Catagory</label>
                                <div class='col-md-10'>
                                  <input type='text' name='tc_cat' id='tc_cat' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Module Types</label>
                                <div class='col-md-10'>
                                  <input type='checkbox' id='modtype' name='modtype[]' value='1' class='form-check-input'>Purchase Order
                                  <input type='checkbox' id='modtype' name='modtype[]' value='2' class='form-check-input'>Quotation
                                  <input type='checkbox' id='modtype' name='modtype[]' value='3' class='form-check-input'>Delivery Note
                                  <input type='checkbox' id='modtype' name='modtype[]' value='4' class='form-check-input'>Invoice
                                 </div>
                            </div>
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function termscat_sv(){
           // $name =  $this->input->post('tc_cat');
    	  // $des =  $this->input->post('tax_value');
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('tc_cat', 'TC Catagory', 'required|is_unique[mes_termcat.tcat_name]');
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	   $module = $this->input->post('modtype');
    	                for($i=0; $i<count($module); $i++){
                      
                        $rr = $rr.",".$module[$i];
                    }
    	   
    	       $data = array(
                'tcat_name' => $this->input->post('tc_cat'), 
                'tcat_modtype' => $rr 
                
                
            );
            $result = $this->db->insert('mes_termcat',$data);
                if($result){
                    $ret[0] = "New Terms & Conditions Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_termscat";
                echo json_encode($ret);	
        }
        
         function termscat_edt(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_termcat'); 
            $this->db->select("*");
            $this->db->where('tcat_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            $tc = $data[0]->tcat_modtype;
            $tcat = explode($tc,",");
            
            $chk1=$chk2=$chk3=$chk4="";
            for($i=1; $i<count($tcat); $i++){
               $i = "checked";
            }
            if (in_array("1", $tcat)) {$chk1= "checked";}else{$chk1 = "";}if (in_array("2", $tcat)) {$chk2="checked";}
            if (in_array("3", $tcat)) {$chk3="checked";}if (in_array("4", $tcat)) {$chk4="checked";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->tcat_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_termscat">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">TC Catagory</label>
                                <div class="col-md-10">
                                  <input type="text" name="etc_cat" id="etc_cat" class="form-control" placeholder="" value="'.$data[0]->tcat_name .'">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Module Types</label>
                                <div class="col-md-10">
                                  <input type="checkbox" checked="'.$chk1.'" id="modtype" name="modtype[]" value="1" class="form-check-input">Purchase Order
                                  <input type="checkbox" checked="'.$chk2.'" id="modtype" name="modtype[]" value="2" class="form-check-input">Quotation
                                  <input type="checkbox" checked="'.$chk3.'" id="modtype" name="modtype[]" value="3" class="form-check-input">Delivery Note
                                  <input type="checkbox" checked="'.$chk4.'" id="modtype" name="modtype[]" value="4" class="form-check-input">Invoice
                                 
                                 </div>
                            </div>
                
                            
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	function termscat_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('etc_cat', 'TC Catagory', 'required|is_unique[mes_termcat.tcat_name]');
                
                
                if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $this->db->set('tcat_name', $this->input->post('etc_cat'));
                
                $this->db->where('tcat_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_termcat');
                
                if($result){
                    $ret[0] = "TC Catagory Updated";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_termscat";
    	    echo json_encode($ret);	
    	}
        
        //tc des data
        function termsdes_view(){
             $this->db->from('mes_tmdes');
            $this->db->select("*");
            $this->db->join( "mes_termcat", "mes_tmdes.tmdes_catid = mes_termcat.tcat_id", 'inner');
            $this->db->order_by("mes_termcat.tcat_name", "asc");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='add_termsdes' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Conditions</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>TC Catagory</th>
														<th>TC Description</th>
														<th style='text-align: center;'>Action</th>
														
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
            
                    $dat =  "
                              <tr><td>".$data[$i]->tmdes_id."</td>
                                <td>".$data[$i]->tcat_name."</td>
                                <td>".$data[$i]->tmdes_des."</td>
                                
                               
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_termsdes/".$data[$i]->tmdes_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function termsdes_add(){
            
            $this->db->from('mes_termcat');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det="
            
                             <div class='form-group row'>
                             <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_termsdes'>
                                <label class='col-md-2 col-form-label'>TC Catagory</label>
                                <div class='col-md-10'>
                                  <select class='form-control' id='tc_Cat' name='tc_Cat'>
                                  <option value=''>Select TC Catagory</option>";
                                     foreach($data as $value) {
                                                $det.='<option value="'.$value->tcat_id.'">'.$value->tcat_name.'</option>';
                                            }     
                                  
            $det.="                              
                                        </select>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>TC Description</label>
                                <div class='col-md-10'>
                                 <textarea name='tc_ses' id='tc_ses' rows='2' placeholder='' class='form-control'></textarea>
                                 </div>
                            </div>
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function termsdes_sv(){
          
                    $this->db->select("*");
                    $this->db->from('mes_tmdes');
                    $this->db->where('tmdes_des',$this->input->post('tc_ses'));
                    $this->db->where('tmdes_catid',$this->input->post('tc_Cat'));
                    $detail = $this->db->get();
          
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('tc_Cat', 'TC Catagory', 'required');
               $this->form_validation->set_rules('tc_ses', 'TC Description', 'required');
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else if($detail->num_rows()>0){
    	       $ret[0] = "Same Description cannot be saved";
    	       $ret[2] = "error";
    	       }else{
    	       $data = array(
                'tmdes_catid' => $this->input->post('tc_Cat') ,
                'tmdes_des' => $this->input->post('tc_ses') 
            );
            $result = $this->db->insert('mes_tmdes',$data);
                if($result){
                    $ret[0] = "New Terms & Conditions Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_termsdes";
                echo json_encode($ret);	
        }
        
        function termsdes_edt(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_tmdes'); 
            $this->db->select("*");
            $this->db->join( "mes_termcat", "mes_tmdes.tmdes_catid = mes_termcat.tcat_id", 'inner');
            $this->db->where('tmdes_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $this->db->from('mes_termcat');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data1 = $detailtax->result();
            
            $act = $inact = "";
           // if($data[0]->tax_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->tmdes_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_termsdes">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">TC Catagory</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="tc_eCat" name="tc_eCat">';
                                  
                                  $det.='<option value="'.$data[0]->tmdes_catid.'" selected>'.$data[0]->tcat_name.'</option>';
                                  
                                     foreach($data1 as $value) {
                                                $det.='<option value="'.$value->tcat_id.'">'.$value->tcat_name.'</option>';
                                            }     
                                  
            $det.='                      
                                        </select>
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">TC Description</label>
                                <div class="col-md-10">
                                 <textarea name="tc_ses" id="tc_ses" rows="2" placeholder="" class="form-control">'.$data[0]->tmdes_des.'</textarea>
                                 </div>
                            </div>
                
                            
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	function termsdes_updt(){
    	    
    	    $this->db->select("*");
                    $this->db->from('mes_tmdes');
                    $this->db->where('tmdes_des',$this->input->post('tc_ses'));
                    $this->db->where('tmdes_catid',$this->input->post('tc_eCat'));
                    $detail = $this->db->get();
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('tc_eCat', 'TC Catagory', 'required');
                $this->form_validation->set_rules('tc_ses', 'TC Description', 'required');
                
                
                
                if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else if($detail->num_rows()>0){
    	       $ret[0] = "Same Description cannot be saved";
    	       $ret[2] = "error";
    	       }else{
    	       $this->db->set('tmdes_catid', $this->input->post('tc_eCat'));
    	       $this->db->set('tmdes_des', $this->input->post('tc_ses'));
    	        
                
                $this->db->where('tmdes_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_tmdes');
                
                if($result){
                    $ret[0] = "Tax TC Description Updated";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_termsdes";
    	    echo json_encode($ret);	
    	}
        
        //curency data
        
                function curr_view(){
             $this->db->from('mes_currancy');
            $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
            
             $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='add_curr' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Currency</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Currency Type</th>
														<th style='text-align: right;'>Currency (Symbol)</th>
														<th style='text-align: right;'>Value</th>
														<th style='text-align: center;'>Status</th>
													    <th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                     if($data[$i]->curr_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>".$data[$i]->curr_code.$data[$i]->curr_id."</td>
                                <td>".$data[$i]->curr_description."</td>
                                <td style='text-align: right;'>".$data[$i]->curr_symble."</td>
                                <td style='text-align: right;'>".$data[$i]->curr_value."</td>
                                $sts
                                <td style='text-align: right;'>
                               
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_curr/".$data[$i]->curr_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
        }
        
                function curr_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='sv_curr'>
                                <label class='col-md-2 col-form-label'>Currency Type</label>
                                <div class='col-md-10'>
                                  <input type='text' name='curr_Des' id='curr_Des' class='form-control' placeholder=''>
                                 </div>
                            </div>
                             <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Currency (Symbol)</label>
                                <div class='col-md-10'>
                                  <input type='text' name='curr_symbol' id='curr_symbol' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Value</label>
                                <div class='col-md-10'>
                                  <input type='text' name='curr_value' id='curr_value' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                           
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
                function curr_sv(){
            $des =  $this->input->post('curr_Des');
    	   $val=  $this->input->post('curr_value');
    	   $syb =  $this->input->post('curr_symbol');
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('curr_symbol', 'Symbol', 'trim|required|is_unique[mes_currancy.curr_symble]|min_length[3]|max_length[3]');
                $this->form_validation->set_rules('curr_Des', 'Description', 'required|is_unique[mes_currancy.curr_description]');
                $this->form_validation->set_rules('curr_value', 'Value', 'trim|required');
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'curr_description' => $des, 
                'curr_value'  => $val,
                'curr_symble'  => strtoupper($syb),
                'curr_cuser'  => "admin",
                'curr_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_currancy',$data);
                if($result){
                    $ret[0] = "New Currency Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_curr";
                echo json_encode($ret);	
        }
        
                function curr_edt(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_currancy'); 
            $this->db->select("*");
            $this->db->where('curr_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->curr_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->curr_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="curr_update">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Currency Type</label>
                                <div class="col-md-10">
                                  <input type="text" name="editdes" id="editdes" class="form-control" placeholder="" value="'.$data[0]->curr_description .'">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Currency (Symbol)</label>
                                <div class="col-md-10">
                                  <input type="text" name="editsyb" id="editsyb" class="form-control" placeholder="" value="'.$data[0]->curr_symble.'">
                                 </div>
                            </div> 
                <div class="form-group row">
                                <label class="col-md-2 col-form-label">Value</label>
                                <div class="col-md-10">
                                  <input type="text" name="editval" id="editval" class="form-control" placeholder="" value="'.$data[0]->curr_value.'">
                                 </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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>             
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	        function curr_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editdes', 'Description', 'required');
                $this->form_validation->set_rules('editval', 'Value', 'trim|required|decimal');
                $this->form_validation->set_rules('editsyb', 'Symbol', 'trim|required|min_length[3]|max_length[3]');
                
                $ret[0]="";
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	     
    	     
    	      $chk_des =  $this->Product_model->chk_dup('mes_currancy','curr_id',array('curr_description'=>$this->input->post('editdes')) );
    	       
    	       if ($chk_des==TRUE){
    	            $this->db->set('curr_description', $this->input->post('editdes'));
                }else{  $ret[0].= "Unique description wont update.
    	            ";  }
    	     
    	     
    	      $chk_sym =  $this->Product_model->chk_dup('mes_currancy','curr_id',array('curr_symble'=>$this->input->post('editsyb')) );
                
    	       if ($chk_sym==TRUE){ 
    	          $this->db->set('curr_symble', strtoupper($this->input->post('editsyb')));
    	            
                }else{
                    $ret[0].= "Unique symbol wont update.
    	            ";
                }
    	       
    	       
                $this->db->set('curr_value', $this->input->post('editval'));
                $this->db->set('curr_status', $this->input->post('status'));
                $this->db->set('curr_euser', "sadmin");
                $this->db->set('curr_edate', date('Y-m-d'));
                
                $this->db->where('curr_id', $this->input->post('data_id'));
                $result = $this->db->update('mes_currancy');
                
                if($result){
                    $ret[0].= "Currency Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_curr";
    	    echo json_encode($ret);	
    	}
    	
    	
    	function unit_view(){
             $this->db->from('mes_unit');
            $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
            
             $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='add_unit' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Unit</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Unit Name</th>
														<th>Symbol</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=1; $i<count($data); $i++){
                    
                    if($data[$i]->unit_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>".$data[$i]->unit_code.$data[$i]->unit_id."</td>
                                <td>".$data[$i]->unit_name."</td>
                                <td>".$data[$i]->unit_symble."</td>
                                $sts
                                <td style='text-align: right;'>
                               
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_unit/".$data[$i]->unit_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
        }
        
        function unit_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='sv_unit'>
                                <label class='col-md-2 col-form-label'>Unit Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='unit_name' id='unit_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Symbol</label>
                                <div class='col-md-10'>
                                  <input type='text' name='unit_sym' id='unit_sym' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                           
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function unit_save(){
            $uname =  $this->input->post('unit_name');
    	    $syb=  $this->input->post('unit_sym');
    	  
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('unit_sym', 'Symbol', 'trim|required|is_unique[mes_unit.unit_symble]');
                $this->form_validation->set_rules('unit_name', 'Name', 'required|is_unique[mes_unit.unit_name]');
                //$this->form_validation->set_rules('curr_value', 'Value', 'trim|required|decimal');
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'unit_name' => $uname, 
                'unit_symble'  => $syb,
                'unit_cuser'  => "admin",
                'unit_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_unit',$data);
                if($result){
                    $ret[0] = "New Unit Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_unit";    
               echo json_encode($ret);	
        }
        
        function unit_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_unit'); 
            $this->db->select("*");
            $this->db->where('unit_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->unit_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->unit_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="unit_update">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Unit Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="editname" id="editname" class="form-control" placeholder="" value="'.$data[0]->unit_name .'">
                                 </div>
                            </div>
                <div class="form-group row">
                                <label class="col-md-2 col-form-label">Symbol</label>
                                <div class="col-md-10">
                                  <input type="text" name="editsyb" id="editsyb" class="form-control" placeholder="" value="'.$data[0]->unit_symble.'">
                                 </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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>             
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	function unit_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editname', 'Unit Name', 'required');
                $this->form_validation->set_rules('editsyb', 'Symbol', 'trim|required');
            $ret[0]="";    
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
                
    	       $chk_des =  $this->Product_model->chk_dup('mes_unit','unit_id',array('unit_name'=>$this->input->post('editname')) );
    	       
    	       if ($chk_des == FALSE){
    	            $ret[0].= "Unique Unit Name wont update.
    	            ";
                }else{  $this->db->set('unit_name', $this->input->post('editname'));  }
                
                
    	       $chk_sym =  $this->Product_model->chk_dup('mes_unit','unit_id',array('unit_symble'=>$this->input->post('editsyb')) );
                if ($chk_sym == FALSE){
    	            $ret[0].= "Unique Unit Symbol wont update.
    	            ";
                }else{  $this->db->set('unit_symble', $this->input->post('editsyb')); }
    	       
    	      
               // $this->db->set('unit_symble', $this->input->post('editsyb'));
                $this->db->set('unit_status', $this->input->post('status'));
                $this->db->set('unit_euser', "sadmin");
                $this->db->set('unit_edate', date('Y-m-d'));
                
                $this->db->where('unit_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_unit');
                
                if($result){
                    $ret[0].= "Unit Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_unit";
    	    echo json_encode($ret);	
    	}
    	
    	        function cont_view(){
             $this->db->from('mes_country');
            $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
            
             $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='add_country' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Country</button></br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Country Name</th>
														<th>Short Code</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    if($data[$i]->cnty_stetus==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 scope='row'>
                                <td>".$data[$i]->cnty_code.$data[$i]->cnty_id."</td>
                                <td>".$data[$i]->cnty_name."</td>
                                <td>".$data[$i]->cnty_short."</td>
                                $sts
                                
                               <td style='text-align: right;'>
                               
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_country/".$data[$i]->cnty_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
        }
        
                function cont_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_country'>
                                <label class='col-md-2 col-form-label'>Country Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='cnt_name' id='cnt_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Short Code</label>
                                <div class='col-md-10'>
                                  <input type='text' name='cnt_shot' id='cnt_shot' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            <!--div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Currency</label>
                                <div class='col-md-10'>
                                  <input type='text' name='cnt_cury' id='cnt_cury' class='form-control' placeholder=''>
                                 </div>
                            </div-->
                            
                            
                           
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
                function cont_sv(){
            $des =  $this->input->post('curr_Des');
    	   $val=  $this->input->post('curr_value');
    	   $syb =  $this->input->post('curr_symbol');
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('cnt_name', 'Country Name', 'required|is_unique[mes_country.cnty_name]');
                $this->form_validation->set_rules('cnt_shot', 'Short Code', 'trim|required|is_unique[mes_country.cnty_short]|min_length[2]|max_length[2]');
               // $this->form_validation->set_rules('cnt_cury', 'Currency', 'trim|required|is_unique[mes_country.cnty_cur]');
               
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	        $datatm = array(
                'tmdes_catid' => "5", 
                'tmdes_des'  => $this->input->post('cnt_name')
                
            );
            $this->db->insert('mes_tmdes',$datatm);
    	       
    	       $data = array(
                'cnty_name' => $this->input->post('cnt_name'), 
                'cnty_short'  => strtoupper($this->input->post('cnt_shot')),
                //'cnty_cur'  => $this->input->post('cnt_cury'),
                'cnty_cuser'  => "admin",
                'cnty_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_country',$data);
                if($result){
                    $ret[0] = "New Country Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_country";
                echo json_encode($ret);	
        }
        
                function cont_edt(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_country'); 
            $this->db->select("*");
            $this->db->where('cnty_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->cnty_stetus=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->cnty_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_country">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Country Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="editcname" id="editcname" class="form-control" placeholder="" value="'.$data[0]->cnty_name .'">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Short Code</label>
                                <div class="col-md-10">
                                  <input type="text" name="editcode" id="editcode" class="form-control" placeholder="" value="'.$data[0]->cnty_short.'">
                                 </div>
                            </div> 
                <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Currency</label>
                                <div class="col-md-10">
                                  <input type="text" name="editcry" id="editcry" class="form-control" placeholder="" value="'.$data[0]->cnty_cur.'">
                                 </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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>             
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
            	function cont_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editcname', 'Country Name', 'required');
                $this->form_validation->set_rules('editcode', 'Short Code', 'trim|required|min_length[2]|max_length[2]');
               // $this->form_validation->set_rules('editcry', 'Currency', 'trim|required');
                
            $ret[0]="";    
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	       $chk_conty =  $this->Product_model->chk_dup('mes_country','cnty_name',array('cnty_name'=>$this->input->post('editcname')) );
    	       
    	       if ($chk_conty == FALSE){
    	            $ret[0].= "Unique Country Name wont update.
    	            ";
                }else{ $this->db->set('cnty_name', $this->input->post('editcname'));   }
                
                
    	       
    	       
                $chk_ccode =  $this->Product_model->chk_dup('mes_country','cnty_short',array('cnty_short'=>$this->input->post('editcname')) );
                
    	       if ($chk_ccode == FALSE){
    	            $ret[0].= "Unique Short Code wont update.
    	            ";
                }else{ $this->db->set('cnty_short', strtoupper($this->input->post('editcode')));   }
                
                
    	       
                $this->db->set('cnty_stetus', $this->input->post('status'));
                
                $this->db->set('cnty_euser', "sadmin");
                $this->db->set('cnty_edate', date('Y-m-d'));
                
                $this->db->where('cnty_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_country');
                
                if($result){
                    $ret[0].= "Country Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_country";
    	    echo json_encode($ret);	
    	}
}