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

class Product_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        $this->load->model('Product_model');
        $this->load->model('Common_model');
     if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
      
         
        }
        
        function index(){
            if($this->session->userdata['login_data']['userlevel']>8) { 
                $data['content'] = ('pages/product_restricted'); 
		        $this->load->view('template',$data);
            }else{
                $data['content'] = ('pages/product'); 
		$this->load->view('template',$data);
            }
        
            
        }
        
        //batch 
        
        
        function show_batch(){
            
            $this->db->select('*');
            $this->db->from('mes_depot');
            $hasil=$this->db->get();
            $depo= $hasil->result(); 
            
            $data['depo'] = $depo;
            
        $data['item_list'] = $this->Product_model->product_list();
            
        $data['content'] = ('pages/batch'); 
		$this->load->view('template',$data);
		
        }
        
        function show_batch_list(){
            
            if($this->input->post('depo')>0){
                $this->db->from('batch_depo');
                $this->db->select("*");
                $this->db->where('product',$this->input->post('id'));
                $this->db->where('depo',$this->input->post('depo'));
                $this->db->order_by("man_date","DESC");
                $detailtax = $this->db->get();
                $data = $detailtax->result();
            }else{
                $this->db->from('batch');
                $this->db->select("*");
                $this->db->where('product',$this->input->post('id'));
                $this->db->order_by("man_date","DESC");
                $detailtax = $this->db->get();
                $data = $detailtax->result();
            }
            
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_brnd' onclick='add_data(this.id)' data-toggle='modal' data-backdrop='static' data-keyboard='false' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Category</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th style='text-align: right;'>Batch</th>
														<th style='text-align: right;'>Buying</th>
														<th style='text-align: right;'>Selling</th>
														<th style='text-align: right;'>Retail</th>
														<th style='text-align: right;'>Mafd</th>
														
														<th style='text-align: right;'>Exp</th>
														<th style='text-align: right;'>Qty</th>
														<th style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    // <!--button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_batch/".$data[$i]->batch."' data-backdrop='static' data-keyboard='false' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button-->
                    
                    if($this->session->userdata['login_data']['userlevel']<10){
                      //  $sts = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_batch/".$data[$i]->batch_id."' data-backdrop='static' data-keyboard='false' onclick='edit_batch(this.id)' data-target='#modal_batch'>Edit</button>";
                    $sts = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_batch/".$data[$i]->batch_id."' data-backdrop='static' data-keyboard='false' onclick='edit_batch(this.id)' data-target='#modal_batch'>Edit</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='adjust_batch/".$data[$i]->batch_id."' data-backdrop='static' data-keyboard='false'  onclick='adjust_batch(this.id)' data-target='#modal_Adjust'>Adjustment</button>";
                        
                    }
                                
                    else{$sts = "";}
                    $dat =  "
                              <tr><td></td>
                                <td>".$data[$i]->batch."</td>
                                <td style='text-align: right;'>".$data[$i]->buying_price."</td>
                                <td style='text-align: right;'>".$data[$i]->selling_price."</td>
                                <td style='text-align: right;'>".$data[$i]->retail_price."</td>
                                <td style='text-align: right;'>".$data[$i]->man_date."</td>
                                <td style='text-align: right;'>".$data[$i]->exp_date."</td>
                                <td style='text-align: right;'>".$data[$i]->avl_qty."</td>
                               
                              
                                
                                <td style='text-align: right;'>
                               $sts
                              </td>
                               
                              
                              </tr>
                           ";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            
        }
        
        function batch_edit(){
            
             if($this->input->post('depo')>0){
                 $this->db->select("batch_depo.*,mes_item.*,batch_depo.retail_price as rtl");
                    $this->db->from('batch_depo');
                    $this->db->join('mes_item', 'batch_depo.product   = mes_item.item_id','left');
                    $this->db->where('batch_id =',$this->input->post('id'));
                    $this->db->where('depo =',$this->input->post('depo'));
                    $detailtax = $this->db->get();
                    $data = $detailtax->result();
                 
             }else{
                 $this->db->select("batch.*,mes_item.*,batch.retail_price as rtl");
                    $this->db->from('batch');
                    $this->db->join('mes_item', 'batch.product   = mes_item.item_id','left');
                    $this->db->where('batch_id =',$this->input->post('id'));
                    $detailtax = $this->db->get();
                    $data = $detailtax->result();
             }
            
            
            
           $dataa = array($data[0]->batch_id,$data[0]->buying_price,$data[0]->selling_price,$data[0]->rtl,$data[0]->man_date,$data[0]->exp_date,$data[0]->avl_qty,$data[0]->item_exp,$data[0]->product);
            
           
           
            echo json_encode($dataa);
            
            
        }
        
        function batch_adjust(){
            
             	    $id = $this->input->post('id');
    	    
    	    
    	    if($this->input->post('hidd_depo')>0){
    	        $this->db->from('batch_depo'); 
                $this->db->select("*");
                $this->db->where('batch_id',$id);
                $this->db->where('depo',$this->input->post('hidd_depo'));
                $detail = $this->db->get();
                $data = $detail->result();
    	    }else{
    	        $this->db->from('batch'); 
                $this->db->select("*");
                $this->db->where('batch_id',$id);
                $detail = $this->db->get();
                $data = $detail->result();
    	    }
    	    
            
            $act = $inact = "";
            //if($data[0]->brand_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="batch_id" id="batch_id" class="form-control" readonly="" value="'.$data[0]->batch_id.'">
              <input type="hidden" name="b_depo" id="b_depo" class="form-control" readonly="" value="'.$this->input->post('hidd_depo').'">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Stock</label>
                                <div class="col-md-10">
                                  <input type="text" name="editname" id="editname" class="form-control" placeholder="" value="'.$data[0]->avl_qty .'" readonly>
                                 </div>
                            </div>
               
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">New Stock</label>
                                <div class="col-md-10">
                                  <input type="text" name="enew_stock" id="enew_stock" class="form-control" placeholder="" value="">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Reason</label>
                                <div class="col-md-10">
                                  <!--input type="text" name="enew_stock" id="enew_stock" class="form-control" placeholder="" value=""-->
                                  <textarea id="rtreason" name="rtreason" rows="4" cols="100" class="form-control"></textarea>
                                 </div>
                            </div>
                            
                           
                         
                            
              ';
                        
                	
               echo json_encode($det);	
            
            
        }
        
        function batch_update(){
           $ret[0] = "";
            
             if(($this->session->userdata['login_data']['userlevel'])>1) { 
                
                        $ret[0].= "Unauthorized user level access";
            }else{
                
                $trl = $this->input->post('eRetail');
                
                 $er = "";
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('ebuying', 'Buying Price', 'required|trim');
                $this->form_validation->set_rules('eSelling', 'Seling Price', 'required|trim|greater_than[$trl]');
                $this->form_validation->set_rules('eRetail', 'Retail Price', 'required|trim');
                //$this->form_validation->set_rules('eMafd', 'Maf Date', 'trim');
                //$this->form_validation->set_rules('eExp', 'Exp Date', 'required|trim');
                $this->form_validation->set_rules('exp', 'Exp Date','callback_expdate_check');
                
                
                $ret[0] = "";
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	       
    	         $this->db->set('buying_price', $this->input->post('ebuying'));
    	         $this->db->set('selling_price', $this->input->post('eSelling'));
    	         $this->db->set('retail_price', $this->input->post('eRetail'));
    	         $this->db->set('man_date', $this->input->post('eMafd'));
    	         $this->db->set('exp_date', $this->input->post('eExp'));
    	         $this->db->where('batch_id', $this->input->post('batchid'));
    	         $result=$this->db->update('batch');
    	         
    	          $dataedit = array(
    	           
               
                'batch_edit_batch_id'  => $this->input->post('batchid'),
                'batch_edit_item_id'  => $this->input->post('proid'),
                'batch_edit_buying'  => $this->input->post('ebuying'),
                'batch_edit_selling'  => $this->input->post('eSelling'),
                'batch_edit_retail'  => $this->input->post('eRetail'),
                'batch_edit_mafd'  => $this->input->post('eMafd'),
                'batch_edit_exp'  => $this->input->post('eExp'),
                'batch_edit_comment'  => $this->input->post('editcomment'),
                'batch_edit_user'  => $this->session->userdata['login_data']['user_id'],
                'batch_edit_date'  => (date('Y-m-d'))
                
                    );
                    $this->db->insert('batch_edit_log',$dataedit);
    	         
    	         
                    $this->db->from('batch_depo');
                    $this->db->select("batch"); 
                    $this->db->where('batch_id',$this->input->post('batchid'));
                    $detailtax = $this->db->get();
                    $depo_b = $detailtax->result();
                    
                    if(count($depo_b)>0){
                        $this->db->set('buying_price', $this->input->post('ebuying'));
        	            $this->db->set('selling_price', $this->input->post('eSelling'));
        	            $this->db->set('retail_price', $this->input->post('eRetail'));
        	            $this->db->set('man_date', $this->input->post('eMafd'));
        	            $this->db->set('exp_date', $this->input->post('eExp'));
        	            $this->db->where('batch_id', $this->input->post('batchid'));
        	             $result=$this->db->update('batch_depo');
                    }
            
                 
    	         
                    if($result){
                        $ret[0].= "Batch Details Updated";
                    }else{
                        $ret[0].= "Please Re enter all details";
                    }
                    
    	   }
    	   
            }
            
             echo json_encode($ret);	 
           
            
        }
        
        public function expdate_check($str)
	{
		if ($str == '1')
		{
		 if($this->input->post('eExp')!=""){
		     return TRUE;
		 }else{
		     $this->form_validation->set_message('expdate_check', 'Expire Date Required');
			return FALSE;
		 }   
		}
		else
		{
			return TRUE;
		}
	}
        
        function adjust_update(){
           $ret[0] = "";
            
             if(($this->session->userdata['login_data']['userlevel'])>1) { 
                
                        $ret[0].= "Unauthorized user level access";
            }else if(!isset($this->session->userdata['login_data']['userlevel'])){
                $ret[0].= "No session Found. Please Login";
            }else{
                
              
                
                 $er = "";
            
                $this->form_validation->set_error_delimiters('', ''); 
               
                $this->form_validation->set_rules('enew_stock', 'Qty', 'required|trim');
                
                
                $ret[0] = "";
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	        }else{
    	       
    
        	        
        	        if($this->input->post('hidd_depo')>0){
        	           /* $this->db->set('avl_qty', $this->input->post('enew_stock'));
                        $this->db->where('batch_id', $this->input->post('batch_id'));
                        $this->db->where('depo', $this->input->post('b_depo'));
                        $result=$this->db->update('batch_depo'); */
                        
                        $this->db->select("avl_qty,product");    
                            $this->db->from('batch_depo');
                            $this->db->where('batch_id', $this->input->post('batch_id'));
                            $this->db->where('depo', $this->input->post('hidd_depo')); 
                            $query = $this->db->get();
                            $product = $query->result();    
                        
        	        }else{
        	          /*  $this->db->set('avl_qty', $this->input->post('enew_stock'));
                        $this->db->where('batch_id', $this->input->post('batch_id'));
                        $result=$this->db->update('batch'); */
                        
                        $this->db->select("avl_qty,product");    
                            $this->db->from('batch');
                            $this->db->where('batch_id', $this->input->post('batch_id'));
                            $query = $this->db->get();
                            $product = $query->result();
        	        } 
                        
                        $new = $this->input->post('enew_stock') - $product[0]->avl_qty;
                        if($new<0){
                             $new = abs($new); $cal = "min";
                        }else{ 
                            $cal = "plus";
                        }
                        $result = $this->Common_model->cal_stock($this->input->post('hidd_depo'),"ADJ",0,$product[0]->product,$new,$cal,0,$this->input->post('batch_id'));
        	        
                        
                        if($result){
                            $ret[0].= "Batch Details Updated";
                         
                            $dataedit = array(
                'batch_edit_batch_id'  => $this->input->post('batch_id'),
                'batch_edit_item_id'  => $product[0]->product,
                'batch_edit_buying'  => "0.00",
                'batch_edit_selling'  => "0.00",
                'batch_edit_retail'  => "0.00",
                'batch_edit_qty'  => $new,
                'batch_edit_comment'  => $this->input->post('rtreason'),
                'batch_edit_depo'  => $this->input->post('hidd_depo'),
                'batch_edit_user'  => $this->session->userdata['login_data']['user_id'],
                'batch_edit_date'  => (date('Y-m-d'))
                
                    );
                    $this->db->insert('batch_edit_log',$dataedit);
                        }else{
                            $ret[0].= "Please Re enter all details";
                        }
                    
              
    	        }
        
    	   // $ret[1] = "get_brnd";
    	   
            }
            
        
             echo json_encode($ret);	 
           
            
        }
        
        function batch_depot(){
            
            if($this->input->post('depo') == 0){
                
            $this->db->from('batch');
            $this->db->select("batch.product as p_id,item.itm_name as item");
            $this->db->group_by('product'); 
           // $this->db->where('avl_qty !=',0);
            $this->db->join('mes_item', 'batch.product   = mes_item.item_id','left');
            $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
            $detailtax = $this->db->get();
            $depo = $detailtax->result();
            
          //  $dataa = array($depo[0]->product,$depo[0]->itm_name);
            
            echo json_encode($depo);
            
            }else{
                $this->db->from('batch_depo');
            $this->db->select("batch_depo.product as p_id,item.itm_name as item");
            $this->db->group_by('product'); 
           // $this->db->where('avl_qty !=',0);
            $this->db->where('batch_depo.depo',$this->input->post('depo'));
            $this->db->join('mes_item', 'batch_depo.product   = mes_item.item_id','left');
            $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
            $detailtax = $this->db->get();
            $depo = $detailtax->result();
            
          //  $dataa = array($depo[0]->product,$depo[0]->itm_name);
            
            echo json_encode($depo);
            }
            
           
        }
        
        
        //brand data
                function brand_view(){
             $this->db->from('mes_brand');
            $this->db->select("*");
            $this->db->where('brand_id!=',0);
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            if($this->session->userdata['login_data']['userlevel']<6){
                
                $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_brnd' onclick='add_data(this.id)' data-toggle='modal' data-backdrop='static' data-keyboard='false' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Category</button></br>";
            
            }else{
                  $det[0]="";
            }
            
           
            $det[1] ="<tr>
														<th >ID</th>
														<th>Category Name</th>
														<th>Category Code</th>
														
														<th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                     
                   if($this->session->userdata['login_data']['userlevel']<6){
                       
                        $editbtn = "  <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_brnd/".$data[$i]->brand_id."' data-backdrop='static' data-keyboard='false' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button>";
                   
                   } else{
                       
                       $editbtn = "";
                   }           
                    
                    if($data[$i]->brand_status==1){$sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr><td>".$data[$i]->brand_code.$data[$i]->brand_id."</td>
                                <td>".$data[$i]->brand_name."</td>
                                <td>".$data[$i]->brand_shordcode."</td>
                               
                                $sts
                                
                                <td>
                                $editbtn
                               </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
                function brand_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='sev_brnd'>
                                <label class='col-md-2 col-form-label'>Category Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='brn_name' id='brn_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Category Code</label>
                                <div class='col-md-10'>
                                  <input type='text' name='brn_code' id='brn_code' class='form-control' placeholder=''>
                                 </div>
                            </div>
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
                function brand_sev(){
         
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('brn_name', 'Category Name', 'required|is_unique[mes_brand.brand_name]|trim');
                $this->form_validation->set_rules('brn_code', 'Category Code', 'trim|required|exact_length[3]|is_unique[mes_brand.brand_shordcode]');
                
                $itm = preg_replace('/\s+/', '', $this->input->post('brn_name')); ;
                        
                    $this->db->from('mes_brand');
                    $this->db->where("REPLACE(brand_name, ' ', '')=",$itm);
                    $hasil=$this->db->get();
                    $count = $hasil->num_rows();
                    
                    
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }elseif(0<$count){
    	       $ret[0] = "Duplicate Entry";
    	       $ret[2] = "error";
    	   }else{
    	       
    	       $data = array(
                'brand_name' => $this->input->post('brn_name'), 
                'brand_shordcode' => strtoupper($this->input->post('brn_code')), 
                
                //'brand_description'  => $this->input->post('brn_des'),
                'brand_cuser'  => "admin",
                'brand_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_brand',$data);
                if($result){
                    $ret[0] = "New Category Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "get_brnd";
               echo json_encode($ret);	
        }
        
                function brand_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_brand'); 
            $this->db->select("*");
            $this->db->where('brand_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->brand_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->brand_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="brnd_update">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Category Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="editname" id="editname" class="form-control" placeholder="" value="'.$data[0]->brand_name .'">
                                 </div>
                            </div>
                <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Description</label>
                                <div class="col-md-10">
                                  <input type="text" name="editdes" id="editdes" class="form-control" placeholder="" value="'.$data[0]->brand_description.'">
                                 </div>
                            </div--> 
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Category Code</label>
                                <div class="col-md-10">
                                  <input type="text" name="ebrn_code" id="ebrn_code" class="form-control" placeholder="" value="'.$data[0]->brand_shordcode.'">
                                 </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 brand_updt(){
            	    $er = "";
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editname', 'Category Name', 'required|trim');
                $this->form_validation->set_rules('ebrn_code', 'Category Code', 'trim|required|exact_length[3]');
                
                $ret[0] = "";
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
             
                //$this->form_validation->set_rules('editname', 'Brand Name', 'is_unique[mes_brand.brand_name]');
                
                 $itm = preg_replace('/\s+/', '', $this->input->post('editname')); 
                 $code = preg_replace('/\s+/', '', $this->input->post('ebrn_code'));
                        
                    $this->db->from('mes_brand');
                    $this->db->where("REPLACE(brand_name, ' ', '')=",$itm);
                    $this->db->where('brand_id!=', $this->input->post('data_id'));
                    $hasil=$this->db->get();
                    $count = $hasil->num_rows(); 
                    
                    $this->db->from('mes_brand');
                    $this->db->where("REPLACE(brand_shordcode, ' ', '')=",$code);
                    $this->db->where('brand_id!=', $this->input->post('data_id'));
                    $hasil2=$this->db->get();
                    $count2 = $hasil2->num_rows(); 
                
    	       if (($count>0) || ($count2>0) ){
    	            $ret[0].= "Category or Code Cannot Be duplicate ";
    	            $ret[2] = "error";
    	            
                }else{ 
                    $this->db->set('brand_name', $this->input->post('editname'));
                        $this->db->set('brand_shordcode', strtoupper(trim($this->input->post('ebrn_code'))));
                    
                
                
                    $this->db->set('brand_status', $this->input->post('status'));
                   // $this->db->set('brand_shordcode', strtoupper($this->input->post('ebrn_code')));
                    $this->db->where('brand_id', $this->input->post('data_id'));
                    $result=$this->db->update('mes_brand');
                    
                    if($result){
                        $ret[0].= "Category Details Updated";
                    }else{
                        $ret[0].= "Please Re enter all details";
                    }
                }
    	   }
        
    	    $ret[1] = "get_brnd";
    	    echo json_encode($ret);	
    	}
    	
        
        
    	 //category data
    	 
    	 function cat_view(){
             $this->db->from('mes_category');
            $this->db->select("*");
            //$this->db->order_by("cat_status desc");
           //$this->db->where('cat_live',0);
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_category' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Department</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Department Name</th>
														<th>Department Code</th>
														<th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=1; $i<count($data); $i++){
                    if($data[$i]->cat_status==1){$sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr><td>".$data[$i]->cat_code. $data[$i]->cat_id."</td>
                                <td>".$data[$i]->cat_name."</td>
                                <td>".$data[$i]->cat_shortcode."</td>
                                $sts
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_category/".$data[$i]->cat_id."' onclick='edit_view(this.id)' data-backdrop='static' data-keyboard='false' data-target='#Modal_edit'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function cat_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_category'>
                                <label class='col-md-2 col-form-label'>Department Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='cat_name' id='cat_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <!--div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Description</label>
                                <div class='col-md-10'>
                                  <input type='text' name='cat_des' id='cat_des' class='form-control' placeholder=''>
                                 </div>
                            </div-->
                            
                            <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Department Short Code</label>
                                <div class='col-md-10'>
                                  <input type='text' name='cat_code' id='cat_code' class='form-control' placeholder=''>
                                 </div>
                            </div>
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function cat_sev(){
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                $this->form_validation->set_rules('cat_name', 'Name', 'required|trim|is_unique[mes_category.cat_name]');
                $this->form_validation->set_rules('cat_code', 'Department Short Code', 'trim|required|is_unique[mes_category.cat_shortcode]|exact_length[3]');
               // $this->form_validation->set_rules('cat_des', 'Description', 'required');
               
                $itm = preg_replace('/\s+/', '', $this->input->post('cat_name')); ;
                        
                    $this->db->from('mes_category');
                    $this->db->where("REPLACE(cat_name, ' ', '')=",$itm);
                    $hasil=$this->db->get();
                    $count = $hasil->num_rows();
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }elseif(0<$count){
    	       $ret[0] = "Duplicate Entry";
    	       $ret[2] = "error";
    	   }else{
    	       $data = array(
                'cat_name' => $this->input->post('cat_name'), 
                //'cat_description'  => $this->input->post('cat_des'),
                'cat_shortcode' => strtoupper($this->input->post('cat_code')),
                'cat_cuser'  => "admin",
                'cat_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_category',$data);
                if($result){
                    $ret[0] = "New Department Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
                $ret[1] = "get_cat";   	   
               echo json_encode($ret);	
        }
        
        function cat_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_category'); 
            $this->db->select("*");
            $this->db->where('cat_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->cat_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->cat_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_category">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Department Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="editname" id="editname" class="form-control" placeholder="" value="'.$data[0]->cat_name .'">
                                 </div>
                            </div>
                <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Description</label>
                                <div class="col-md-10">
                                  <input type="text" name="editdes" id="editdes" class="form-control" placeholder="" value="'.$data[0]->cat_description.'">
                                 </div>
                            </div--> 
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Department Code</label>
                                <div class="col-md-10">
                                  <input type="text" name="editcode" id="editcode" class="form-control" placeholder="" value="'.$data[0]->cat_shortcode.'">
                                 </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 cat_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editname', 'Department Name', 'trim|required');
                $this->form_validation->set_rules('editcode', 'Department Code', 'trim|required|exact_length[3]');
               // $this->form_validation->set_rules('editdes', 'Category Description', '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_category.cat_name]');
                //$this->form_validation->set_rules('editcode', 'Category Short Code', 'trim|required|is_unique[mes_category.cat_shortcode]');
                
                     $itm = preg_replace('/\s+/', '', $this->input->post('editname')); 
                     $code = preg_replace('/\s+/', '', $this->input->post('editcode'));
                        
                    $this->db->from('mes_category');
                    $this->db->where("REPLACE(cat_name, ' ', '')=",$itm);
                    $this->db->where('cat_id!=', $this->input->post('data_id'));
                    $hasil=$this->db->get();
                    $count = $hasil->num_rows(); 
                    
                    $this->db->from('mes_category');
                    $this->db->where("REPLACE(cat_shortcode, ' ', '')=",$code);
                    $this->db->where('cat_id!=', $this->input->post('data_id'));
                    $hasil2=$this->db->get();
                    $count2 = $hasil2->num_rows(); 
                
    	       if (($count>0) || ($count2>0) ){
    	            $ret[0].= "Department or Code Cannot Be duplicate ";
    	            $ret[2] = "error";
    	            
                }else{ 
                        $this->db->set('cat_name', $this->input->post('editname'));
                        $this->db->set('cat_shortcode', strtoupper(trim($this->input->post('ebrn_code'))));
                    
                
                
                    $this->db->set('cat_status', $this->input->post('status'));
                   // $this->db->set('brand_shordcode', strtoupper($this->input->post('ebrn_code')));
                    $this->db->where('cat_id', $this->input->post('data_id'));
                    $result=$this->db->update('mes_category');
                    
                    if($result){
                        $ret[0].= "Department Details Updated";
                    }else{
                        $ret[0].= "Please Re enter all details";
                    }
                }
                
           
    	   }
        
    	    $ret[1] = "get_cat"; 
    	    echo json_encode($ret);	
    	}

    	
    	
    	//item data
    	
    	function item_view(){
            
            $data =$this->Product_model->item_list();
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_item' onclick='add_data(this.id)' data-toggle='modal' data-backdrop='static' data-keyboard='false' data-target='#addmodel'>Add New Item Description</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Item Description</th>
														<th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    if($data[$i]->itm_status==1){$sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr><td>".$data[$i]->itm_code.$data[$i]->itm_id."</td>
                                <td>".$data[$i]->itm_name."</td>
                                
                                $sts
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_item/".$data[$i]->itm_id."' onclick='edit_view(this.id)' data-backdrop='static' data-keyboard='false' data-target='#Modal_edit'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function item_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_item'>
                                <label class='col-md-2 col-form-label'>Item Description</label>
                                <div class='col-md-10'>
                                  <input type='text' name='itm_name' id='itm_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function item_sev(){
    	   
                 $this->form_validation->set_error_delimiters('', ''); 
                 $this->form_validation->set_rules('itm_name', 'Item Description', 'trim|required|is_unique[item.itm_name]');
               
               $itm = preg_replace('/\s+/', '', $this->input->post('itm_name')); ;
                        
                    $this->db->from('item');
                    $this->db->where("REPLACE(itm_name, ' ', '')=",$itm);
                    $hasil=$this->db->get();
                    $count = $hasil->num_rows();       
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }elseif(0<$count){
    	       $ret[0] = "Duplicate Entry";
    	       $ret[2] = "error";
    	   }else{
    	       $data = array(
                'itm_name' => $this->input->post('itm_name'), 
                //'itm_description'  => $this->input->post('itm_des'),
                'itm_cuser'  => "admin",
                'itm_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('item',$data);
                if($result){
                    $ret[0] = "New Item Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	   $ret[1] = "get_item";
               echo json_encode($ret);	
        }
        
        function item_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('item'); 
            $this->db->select("*");
            $this->db->where('itm_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $act = $inact = "";
            if($data[0]->itm_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->itm_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_item">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Item Description</label>
                                <div class="col-md-10">
                                  <input type="text" name="editname" id="editname" class="form-control" placeholder="" value="'.htmlentities($data[0]->itm_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 item_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editname', 'Item Description', 'required|trim');
              //  $this->form_validation->set_rules('editdes', 'Item Description', 'required');
            $ret[0]="";    
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       // $this->form_validation->set_rules('editname', 'Item Description', 'required|is_unique[item.itm_name]');
    	         $itm = preg_replace('/\s+/', '', $this->input->post('editname')); ;
                        
                    $this->db->from('item');
                    $this->db->where("REPLACE(itm_name, ' ', '')=",$itm);
                    $this->db->where('itm_id!=', $this->input->post('data_id'));
                    $hasil=$this->db->get();
                    $count = $hasil->num_rows(); 
    	        
    	       if ($count>0){
    	            $ret[0].= "Item Description Cannot Be duplicate ";
    	            
                }else{ $this->db->set('itm_name', $this->input->post('editname'));}
    	       
    	       
                $this->db->set('itm_status', $this->input->post('status'));
                
                $this->db->where('itm_id', $this->input->post('data_id'));
                $result=$this->db->update('item');
                
                if($result){
                    $ret[0].= "Item Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "get_item";
    	    echo json_encode($ret);	
    	}
    	
    	function item_restricted_view(){
            
               // $data=$this->Product_model->product_list();
            $data[] = array();        
            $dataa = $this->Product_model->product_list();
                $dt="";
                for($i=0; $i<count($dataa); $i++){
                    $dt = $this->Product_model->part_num($dataa[$i]->item_id);
                    $data[] = array($dataa[$i],$dt); 
                 
                }
            
            $det[0]="";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th >Code</th>
													
													
														<th>Item Des</th>
														<!--th>Part Number</th>
														<th >Model / Type</th>
														<th>Specification</th-->
											
														<th style='text-align: right;'>Selling (LKR)</th>
														<th style='text-align: right;'>Retail (LKR)</th>
														
														
														<th style='text-align: right;'>Free Items</th>
														<th style='text-align: right;'>Free Qty</th>
														<th>Stock</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]=""; $dat="";
                for($i=1; $i<count($data); $i++){
                   // $dis_part = '<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="edit_itm_part/'.$data[0]->item_id.'" onclick="edit_part(this.id)" data-target="#Modal_edit">View Part Numbers</button>';
                    if($data[$i][0]->item_status==1){$sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $prt ="";
                    foreach($data[$i][1] as $part){
                        $prt = $part->part_no."</br>".$prt;
                    }
                    
                        $this->db->select('stc_qty');
                        $this->db->from('mes_stock');
                        $this->db->where('stc_itemid', $data[$i][0]->item_id);
                        $detailstk = $this->db->get();
                        $datactk = $detailstk->result();
                        
                       // print_r($datactk[0]->stc_qty);
                       
                       if(isset($datactk[0]->stc_qty)){$stck = $datactk[0]->stc_qty; }else{$stck = "";}
         
                    $this->db->from('batch');
                    $this->db->select("MAX(`buying_price`) as buying, MAX(`selling_price`) as selling, MAX(`retail_price`) as retail");
                    $this->db->where('avl_qty >',0);
                    $this->db->where('product', $data[$i][0]->item_id);
                    $price = $this->db->get();
                    $pricelist = $price->result();
                    
                    $buy = $pricelist[0]->buying;
                    $trl = $pricelist[0]->retail;
                    $ssel = $pricelist[0]->selling;
                    
                  
                    
                    if(($ssel<1)||($ssel==null)){
                        
                    $this->db->from('batch');
                    $this->db->select("`buying_price`, `selling_price`,`retail_price`");
                    //$this->db->where('avl_qty >',0);
                    $this->db->where('product', $data[$i][0]->item_id);
                    $this->db->order_by('exp_date', 'DESC');
                    $price1 = $this->db->get();
                    $pricelist1 = $price1->result();
                   
                    if(count($pricelist1)>0){
                        $ssel = $pricelist1[0]->selling_price; $buy =  $pricelist1[0]->buying_price; $trl =  $pricelist1[0]->retail_price;
                        
                    }else{
                        $this->db->from('batch');
                        $this->db->select("MAX(`buying_price`) as buying, MAX(`selling_price`) as selling, MAX(`retail_price`) as retail");
                        $this->db->where('product', $data[$i][0]->item_id);
                        $price2 = $this->db->get();
                        $pricelist2 = $price->result();
                        
                         if($pricelist2[0]->selling){$ssel = $pricelist2[0]->selling; $buy =  $pricelist2[0]->buying; $trl =  $pricelist2[0]->retail;
                         }else{
                              $ssel = 0.00; $buy =  0.00; $trl =  0.00; 
                         }
                        
                    }
                    
                    }
                    
                    
                    $dat =  "
                              <tr scope='row'>
                                <td >".$data[$i][0]->item_code.$data[$i][0]->item_id."</td>
                                <td>".$data[$i][0]->cat_shortcode.'-'.$data[$i][0]->brand_shordcode.'-'.$data[$i][0]->item_catcount."</td>
                              
                                
                                <td>".$data[$i][0]->itm_name."</td>
                                <!--td>".$prt."<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_itm_part/".$data[$i][0]->item_id."' onclick='edit_part(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button></td-->
                                <!--td>".$data[$i][0]->model_name."</td-->
                                <!--td>".$data[$i][0]->spec_spec."</td-->
                           
                                <td style='text-align: right;'>".$ssel."</td>
                                <td style='text-align: right;'>".$trl."</td>
                              
                                <td style='text-align: right;'>".$data[$i][0]->item_freeitm."</td>
                                <td style='text-align: right;'>".$data[$i][0]->item_freqty."</td>
                                <td style='text-align: right;'><b>".$stck."</b></td>
                                $sts
                        
                                <td>
                                 </td>
                              </tr>";
                              
                          
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }

    	// product data
    	 
                function pro_viw(){
                    
                   // $data=$this->Product_model->product_list();
            $data[] = array();        
            $data = $this->Product_model->product_list();
                $dt="";
                //for($i=0; $i<count($data); $i++){
                    //$dt = $this->Product_model->part_num($data[$i]->item_id);
                    //$dataa[] = array($data[$i],$dt); 
                 
                //}
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_product' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Product</button></br>";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th >Code</th>
													
													
														<th>Item Des</th>
														<!--th>Part Number</th>
														<th >Model / Type</th>
														<th>Specification</th-->
											
														<th style='text-align: right;'>Buying (LKR)</th>
														<th style='text-align: right;'>Selling (LKR)</th>
														<th style='text-align: right;'>Retail (LKR)</th>
														
														<th style='text-align: right;'>MOQ</th>
														<th style='text-align: right;'>Free Items</th>
														<th style='text-align: right;'>Free Qty</th>
														<th>Stock</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]=""; $dat="";
                for($i=0; $i<count($data); $i++){
                   // $dis_part = '<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="edit_itm_part/'.$data[0]->item_id.'" onclick="edit_part(this.id)" data-target="#Modal_edit">View Part Numbers</button>';
                    if($data[$i]->item_status==1){$sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $prt ="";
                    //foreach($data[$i][1] as $part){
                      //  $prt = $part->part_no."</br>".$prt;
                   // }
                    
                        $this->db->select('stc_qty');
                        $this->db->from('mes_stock');
                        $this->db->where('stc_itemid', $data[$i]->item_id);
                        $detailstk = $this->db->get();
                        $datactk = $detailstk->result();
                        
                       // print_r($datactk[0]->stc_qty);
                    $ssel = 0.00; $buy =  0.00; $trl =  0.00;   
                       if(isset($datactk[0]->stc_qty)){$stck = $datactk[0]->stc_qty; }else{$stck = "";}
                       
                    $this->db->from('batch');
                    $this->db->select("MAX(`buying_price`) as buying, MAX(`selling_price`) as selling, MAX(`retail_price`) as retail");
                    $this->db->where('avl_qty >',0);
                    $this->db->where('product', $data[$i]->item_id);
                    $price = $this->db->get();
                    $pricelist = $price->result();
                    
                    $buy = $pricelist[0]->buying;
                    $trl = $pricelist[0]->retail;
                    $ssel = $pricelist[0]->selling;
                    
                  
                    
                    if(($ssel<1)||($ssel==null)){
                        
                    $this->db->from('batch');
                    $this->db->select("`buying_price`, `selling_price`,`retail_price`");
                    //$this->db->where('avl_qty >',0);
                    $this->db->where('product', $data[$i]->item_id);
                    $this->db->order_by('exp_date', 'DESC');
                    $price1 = $this->db->get();
                    $pricelist1 = $price1->result();
                   
                    if(count($pricelist1)>0){
                        $ssel = $pricelist1[0]->selling_price; $buy =  $pricelist1[0]->buying_price; $trl =  $pricelist1[0]->retail_price;
                        
                    }else{
                        $this->db->from('batch');
                        $this->db->select("MAX(`buying_price`) as buying, MAX(`selling_price`) as selling, MAX(`retail_price`) as retail");
                        $this->db->where('product', $data[$i]->item_id);
                        $price2 = $this->db->get();
                        $pricelist2 = $price->result();
                        
                         if($pricelist2[0]->selling){$ssel = $pricelist2[0]->selling; $buy =  $pricelist2[0]->buying; $trl =  $pricelist2[0]->retail;
                         }else{
                              $ssel = 0.00; $buy =  0.00; $trl =  0.00; 
                         }
                        
                    }
                    
                    }
                    
                    
                    //if($pricelist[0]->selling<1){$ssel = "0.00";}else{ $ssel = $pricelist[0]->selling;}
         
                    if($this->session->userdata['login_data']['userlevel']<1){$bprice = $buy; }else{$bprice = '0.00' ;}
                    
                    $dat =  "
                              <tr scope='row'>
                                <td >".$data[$i]->item_code.$data[$i]->item_id."</td>
                                <td>".$data[$i]->cat_shortcode.'-'.$data[$i]->brand_shordcode.'-'.$data[$i]->item_catcount."</td>
                              
                                
                                <td>".$data[$i]->itm_name."</td>
                                <!--td>".$prt."<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_itm_part/".$data[$i]->item_id."' onclick='edit_part(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button></td-->
                                <!--td>".$data[$i]->model_name."</td-->
                                <!--td>".$data[$i]->spec_spec."</td-->
                           
                                <td style='text-align: right;'>".$buy."</td>
                                <td style='text-align: right;'><b>".$ssel."</b></td>
                                <td style='text-align: right;'>".$trl."</td>
                              
                                <td style='text-align: right;'>".$data[$i]->item_moq."</td>
                                <td style='text-align: right;'>".$data[$i]->item_freeitm."</td>
                                <td style='text-align: right;'>".$data[$i]->item_freqty."</td>
                                <td style='text-align: right;'> <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='batch_product/".$data[$i]->item_id."' onclick='batch_view(this.id)' data-backdrop='static' data-keyboard='false' data-target='#Modal_edit'><b>".$stck."</b></button></td>
                                $sts
                        
                                <td>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_product/".$data[$i]->item_id."' onclick='edit_view(this.id)' data-backdrop='static' data-keyboard='false' data-target='#Modal_edit'>Edit</button>
                                </td>
                              </tr>";
                              
                          
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
                function pro_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();
                    $spec = $this->Product_model->spec_list();
                    $modellist = $this->Product_model->mes_model();
                    
                    
                    
                    $det="";
	                $det.='
	        
	                    <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Item Code</label>
                                <div class="col-md-10">
                                  <input type="text" name="item_code" id="item_code" class="form-control" value="">
                                 </div>
                            </div-->
	        
             <div class="form-group row">
               
               <input type="hidden" name="table" id="table" class="form-control" readonly="" value="save_product">
                                
                                
                                
                                <label class="col-md-2 col-form-label">Department</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="cat_type" name="cat_type">
                                  <option value="" selected>Select</option>'; 
                                  
                                  
                                     foreach($cat as $value) {
                                                $det.='<option value="'.$value->cat_id.'">'.$value->cat_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                         
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Category</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="brand" name="brand">
                                  <option value="" selected>Select</option>'; 
                                     foreach($brand as $value) {
                                                $det.='<option value="'.$value->brand_id.'">'.$value->brand_name.'- '.$value->brand_shordcode.'</option>'; 
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Model/Type</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="model" name="model">
                                  <option value="" selected>Select</option>'; 
                                     foreach($modellist as $value) {
                                                $det.='<option value="'.$value->model_id.'">'.$value->model_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div-->
                            
                             <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Specification</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="spec" name="spec">
                                  <option value="" selected>Select</option>'; 
                                     foreach($spec as $value) {
                                                $det.='<option value="'.$value->spec_id.'">'.$value->spec_spec.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select> 
                                 </div>
                            </div-->
                            
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Part Number</label>
                                <div class="col-md-10">
                                  <input type="text" name="add_num" id="add_num" class="form-control" value="" onkeyup="get_part();">
                                  <textarea id="part_num" name="part_num" rows="2" placeholder="" class="form-control"></textarea>
                                
                                 </div>
                                 </div-->
                                 
                              
                            
                                 
                                  <div class="form-group row">
                                <label class="col-md-2 col-form-label">Item Description</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="item" name="item">
                                  <option value="" selected>Select</option>'; 
                                     foreach($item as $value) {
                                                $det.='<option value="'.$value->itm_id.'">'.$value->itm_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            </div>
                            
                               <div class="form-group row">
                                <label class="col-md-2 col-form-label">Unit</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="unit" name="unit">
                                  <option value="" selected>Unit</option>'; 
              
              
                    foreach($unit as $value) {
                                                $det.='<option value="'.$value->unit_id.'">'.$value->unit_symble." - ".$value->unit_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Buying Price(LKR)</label>
                                <div class="col-md-10">
                                  <input type="text" name="buy_price" id="buy_price" class="form-control" value="">
                                 </div>
                            </div-->
                            
                             
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">USD Rate</label>
                                <div class="col-md-10">
                                  <input type="text" name="dol_price" id="dol_price" class="form-control" value="">
                                 </div>
                            </div-->
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Selling price(LKR)</label>
                                <div class="col-md-10">
                                  <input type="text" name="sel_price" id="sel_price" class="form-control" value="">
                                 </div>
                            </div-->
                            
                             <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Retail price(LKR)</label>
                                <div class="col-md-10">
                                  <input type="text" name="retl_price" id="retl_price" class="form-control" value="">
                                 </div>
                            </div-->
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">MOQ</label>
                                <div class="col-md-10">
                                  <input type="text" name="moq" id="moq" class="form-control" value="">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Free Items</label>
                                <div class="col-md-10">
                                  <input type="text" name="fitm" id="fitm" class="form-control" value="">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Items Qty</label>
                                <div class="col-md-10">
                                  <input type="text" name="fqty" id="fqty" class="form-control" value="">
                                 </div>
                            </div>
                        
              ';
              
              $det.='
               
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Expire Date</label>
                                <div class="col-md-10">
                                      <select class="form-control" id="xpd" name="xpd">
                                            <option value="1" >Yes</option>
                                            <option value="0" >No</option>
                                        </select>
                                 </div>
                            </div>
                            
                            
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Note</label>
                                <div class="col-md-10">
                                  <textarea name="desc" id="desc" rows="2" placeholder="" class="form-control"></textarea>  
                                 </div>
                            </div>
                            
                          
                            
                            ';
                        	
                       echo json_encode($det);
                }
                
                function pro_sav(){
                    
                     $this->form_validation->set_error_delimiters('', ''); 

                
                $this->form_validation->set_rules('cat_type', 'Department', 'required');
                $this->form_validation->set_rules('brand', 'Category', 'required');
                $this->form_validation->set_rules('item', 'Item Description', 'required|is_unique[mes_item.item_name]');
                 //$this->form_validation->set_rules('email', 'Item Description', 'required|is_unique[users.email]');
                
            $this->db->from('mes_item');
            $this->db->select("item_id");
            $this->db->where('item_cat', $this->input->post('cat_type'));
            $this->db->where('item_brand', $this->input->post('brand'));
            $this->db->where('item_name', $this->input->post('item'));
            $detail = $this->db->get();
            $countdet = $detail->num_rows();
    	    //$dt = $detail->result;
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }elseif($countdet<0){
    	       
    	            $ret[0] = "Duplicate Product detail";
                    $ret[2] = "error";
    	   }else{
    	       
    	   // $catid = $this->input->post('cat_type');
    	    $this->db->from('mes_item');
            $this->db->select("item_cat");
            $this->db->where('item_cat', $this->input->post('cat_type'));
            $detailtax = $this->db->get();
            $count = $detailtax->num_rows();
            $lastrow = $count+ 1;
            //$data = $detailtax->result();
            //$unit = $this->Product_model->product_list();
    	       
    	       $data = array(
    	           
                //'item_productnum' => $this->input->post('item_code'), 
                'item_cat'  => $this->input->post('cat_type'),
                'item_name'  => $this->input->post('item'),
                'item_brand'  => $this->input->post('brand'),
                //'item_buying_price'  => $this->input->post('buy_price'),
                //'item_selling_price'  => $this->input->post('sel_price'),
                //'item_retail_price'  => $this->input->post('retl_price'),
                'item_moq'  => $this->input->post('moq'),
                //'item_model'  => $this->input->post('model'),
                'item_unit'  => $this->input->post('unit'),
                //'item_spec'  => $this->input->post('spec'),
                'item_description'  => $this->input->post('desc'),
                'item_freeitm'  => $this->input->post('fitm'),
                'item_freqty'  => $this->input->post('fqty'),
                'item_catcount'  => $lastrow,
                'item_exp'  => $this->input->post('xpd'),
                'item_cuser'  => $this->session->userdata['login_data']['username'],
                'item_cdate'  => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_item',$data);
            
                if($result){
                    
                    $insert_id = $this->db->insert_id();
                
                    $ret[0] = "New Product Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
                
                $ret[2]="done";
                
                            //add to stock table
                           $dt_stk = array(
                            'stc_itemid'  => $insert_id
                        );
                        $this->db->insert('mes_stock',$dt_stk);
                           
                
    	   }
    	   
    	        $ret[1] = "product_view";
               echo json_encode($ret);
                }
                
                function pro_edit(){
    	    $id = $this->input->post('id');
    	    
            $data = $this->Product_model->single_product($id);
            $partno="";
            $prt =$this->Product_model->part_num($id);
                    foreach($prt as $part){
                        $partno= $part->part_no."</br>".$partno;
                    }
                    
            if($partno!=""){
                $dis_part = '<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="edit_itm_part/'.$data[0]->item_id.'" onclick="edit_part(this.id)" data-target="#Modal_edit" data-backdrop="static" data-keyboard="false">View Part Numbers</button>
                                ';
            }else{ $dis_part="";}        
            
            $act = $inact = $exact = $exinact = "";
            if($data[0]->item_status=="1"){$act = "selected";}else{$inact = "selected";}
            if($data[0]->item_exp=="1"){$exact = "selected";}else{$exinact = "selected";}
    	    
                    $cat = $this->Product_model->cat_list();
                    $item = $this->Product_model->item_list();
                    $brand = $this->Product_model->brand_list();
                    $unit = $this->Product_model->unit_list();
                    $spec = $this->Product_model->spec_list();
                    $modellist = $this->Product_model->mes_model();
                    
                    
                    $det="";
	        $det.='<input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->item_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_product">
	        
	                    <div class="form-group row">
                                <label class="col-md-2 col-form-label">Item Code</label>
                                <div class="col-md-10">
                                  <input type="text" name="item_code" id="item_code" class="form-control" value="'.$data[0]->cat_shortcode.$data[0]->item_catcount .'" disabled="">
                                 </div>
                            </div>
	        
             <div class="form-group row">
               
               <input type="hidden" name="table" id="table" class="form-control" readonly="" value="update_product">
                                
                                
                                
                                <label class="col-md-2 col-form-label">Department</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="cat_type" name="cat_type">
                                 '; 
                                  $det.='<option value="'.$data[0]->item_cat .'" selected>'.$data[0]->cat_name.'</option>
                                  ';
                                  
                                  
                                     foreach($cat as $value) {
                                                $det.='<option value="'.$value->cat_id.'">'.$value->cat_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            
                              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Category</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="edp_gage" name="edp_gage">'; 
                                  $det.='<option value="'.$data[0]->item_brand .'" selected>'.$data[0]->brand_name.'</option><option value="">No Brand</option>';
                                     foreach($brand as $value) {
                                                $det.='<option value="'.$value->brand_id.'">'.$value->brand_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                             
                          <div class="form-group row">
                                <label class="col-md-2 col-form-label">Item</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="item" name="item">'; 
                                  $det.='<option value="'.$data[0]->itm_id .'" selected>'.$data[0]->itm_name.'</option>';
                                     foreach($item as $value) {
                                                $det.='<option value="'.$value->itm_id.'">'.$value->itm_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Unit</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="unit" name="unit">'; 
                                  $det.='<option value="'.$data[0]->item_unit .'" selected>'.$data[0]->unit_symble." - ".$data[0]->unit_name.'</option>';
                                 
                                  
              
              
                    foreach($unit as $value) {
                                                $det.='<option value="'.$value->unit_id.'">'.$value->unit_symble." - ".$value->unit_name.'</option>';
                                            }     
                                  
            $det.='                              
                                        </select>
                                 </div>
                            </div>
                          
                            
                           
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Part Number</label>
                                <div class="col-md-10">
                                <span>'.$dis_part.'</span--> ';
                             
                       
            
                              
                $det.=   '<!--input type="text" name="add_num" id="add_num" class="form-control" value="" onkeyup="get_part();">
                                  <textarea id="part_num" name="part_num" rows="2" placeholder="" class="form-control">'.$data[0]->item_partnum.' </textarea-->
                                
                                 </div>
                            </div>
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Buying Price(LKR)</label>
                                <div class="col-md-10">
                                  <input type="text" name="buy_price" id="buy_price" class="form-control" value="'.$data[0]->item_buying_price.'">
                                 </div>
                            </div-->
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">USD Rate</label>
                                <div class="col-md-10">
                                  <input type="text" name="dol_price" id="dol_price" class="form-control" value="'.$data[0]->usd_rate.'">
                                 </div>
                            </div-->
                            <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Selling price(LKR)</label>
                                <div class="col-md-10">
                                  <input type="text" name="sel_price" id="sel_price" class="form-control" value="'.$data[0]->item_selling_price.'">
                                 </div>
                            </div-->
                            
                             <!--div class="form-group row">
                                <label class="col-md-2 col-form-label">Retail price(LKR)</label>
                                <div class="col-md-10">
                                  <input type="text" name="rtl_price" id="rtl_price" class="form-control" value="'.$data[0]->item_retail_price.'">
                                 </div>
                            </div-->
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">MOQ</label>
                                <div class="col-md-10">
                                  <input type="text" name="moq" id="moq" class="form-control" value="'.$data[0]->item_moq.'">
                                 </div>
                            </div>
                            
                              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Free Items</label>
                                <div class="col-md-10">
                                  <input type="text" name="fitm" id="fitm" class="form-control" value="'.$data[0]->item_freeitm.'">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Items Qty</label>
                                <div class="col-md-10">
                                  <input type="text" name="fqty" id="fqty" class="form-control" value="'.$data[0]->item_freqty.'">
                                 </div>
                            </div>
                            
                             
                        
              ';
              
              $det.='
              
                            
                             
                            
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Note</label>
                                <div class="col-md-10">
                                  <textarea name="desc" id="desc" rows="2" placeholder="" class="form-control">'.$data[0]->item_description .'</textarea>  
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Expire Date</label>
                                <div class="col-md-10">
                                      <select class="form-control" id="expd" name="expd">
                                            <option value="1" '.$exact.'>Yes</option>
                                            <option value="0" '.$exinact.'>No</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="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>  
                            
                          
                            
                            ';
                        	
                       echo json_encode($det);
    	}
    	
    	    	function pro_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                //$this->form_validation->set_rules('buy_price', 'Buying Pricee', 'required');
                //$this->form_validation->set_rules('buy_price', 'Selling price', 'required');
               // $this->form_validation->set_rules('dol_price', 'USD Rate', 'required');
               $this->form_validation->set_rules('cat_type', 'Department', 'required');
                $this->form_validation->set_rules('item', 'Item', 'required');
                
          
            
                
                
                if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	    $this->db->from('mes_item');
            $this->db->select("item_cat");
            $this->db->where('item_id', $this->input->post('data_id'));
            $detailcat1 = $this->db->get();
            $detailcat= $detailcat1->result();
          
            
            if($detailcat[0]->item_cat != $this->input->post('cat_type'))
            {
                $this->db->from('mes_item');
                $this->db->select("item_cat");
                $this->db->where('item_cat', $this->input->post('cat_type'));
                $detailtax = $this->db->get();
                 $count = $detailtax->num_rows();
                $lastrow = $count+ 1;
                
                $this->db->set('item_catcount', $lastrow);
                $this->db->where('item_id', $this->input->post('data_id'));
                $result3=$this->db->update('mes_item');
            }
    	       
    	       
    	       //$this->db->set('item_productnum', $this->input->post('item_code'));
    	       
    	       
                $this->db->set('item_cat', $this->input->post('cat_type'));
                $this->db->set('item_name', $this->input->post('item'));
                $this->db->set('item_brand', $this->input->post('edp_gage'));
                //$this->db->set('item_partnum', $this->input->post('part_num'));
                //$this->db->set('item_buying_price', $this->input->post('buy_price'));
                $this->db->set('item_model', $this->input->post('emodel'));
                //$this->db->set('item_selling_price', $this->input->post('sel_price'));
                //$this->db->set('item_retail_price', $this->input->post('rtl_price'));
                $this->db->set('item_moq', $this->input->post('moq'));
                $this->db->set('item_unit', $this->input->post('unit'));
                $this->db->set('item_exp', $this->input->post('expd'));
                //$this->db->set('item_spec', $this->input->post('spec'));
                $this->db->set('item_description', $this->input->post('desc'));
                $this->db->set('item_freeitm', $this->input->post('fitm'));
                $this->db->set('item_freqty', $this->input->post('fqty'));
                $this->db->set('item_status', $this->input->post('status'));
                $this->db->set('item_euser', $this->session->userdata['login_data']['username']);
                $this->db->set('item_edate', date('Y-m-d'));
                
                
                $this->db->where('item_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_item');
                
                if($result){
                    $ret[0] = "Product Details Updated";
                            $data = array(
                                'created' => $this->session->userdata['login_data']['username'], 
                                'item'  => $this->input->post('data_id'),
                               // 'itm_price'  => $this->input->post('sel_price'),
                               // 'itm_retlprice'  => $this->input->post('rtl_price'),
                                 'moq'  => $this->input->post('moq'),
                                 'fre_itm'  => $this->input->post('fitm'),
                                 'fre_qty'  => $this->input->post('fqty'),
                                 'stetus'  => $this->input->post('status')
                                
                                
                                
                                
                                
                                
                            );
                            $result = $this->db->insert('product_edit_log',$data);
                    
                }else{
                    $ret[0] = "Please Re enter all details";
                }
                $ret[2] = "r";
    	   }
        
    	    $ret[1] = "product_view";
    	    echo json_encode($ret);	
    	}
    	
    	function pro_batch(){
    	            
    	            //$id = $this->input->post('id');
    	            
    	            
    	            $this->db->from('batch');
                    $this->db->select("*");
                    $this->db->where('product', $this->input->post('id'));
                    $this->db->where('avl_qty!=', 0);
                    $this->db->join( "mes_item", "batch.product = mes_item.item_id", 'left');
                    $this->db->join( "item", "mes_item.item_name = item.itm_id", 'left');
                    $deta = $this->db->get();
                    $databatch = $deta->result();
    	            
    	            
    	             //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_product' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Product</button></br>";
            //$det[0]="";
            $det[0] ="
            
                                 <tr>
														<th scope='col'>ID</th>
														<th >Batch Code</th>
														<th >Price</th>
														<th>Exp Date</th>
														<th>Manf Date</th>
													    <th>Stock</th>                                         
																											
													</tr>
                                
                               
                             
           ";
           
           $det[1]="";
           $det[2]="";
           
           $i = 1;
                for($i=0; $i<count($databatch); $i++){
                    
                     $x = $i+1;
                    $dat =  "
                    
                              <tr><td>".$x."</td>
                               <td>".$databatch[$i]->batch."</td>
                               <td>".$databatch[$i]->selling_price."</td>
                                <td>".$databatch[$i]->exp_date."</td>
                                 <td>".$databatch[$i]->man_date."</td>
                                <td>".$databatch[$i]->avl_qty."</td>
                              </tr>
                              ";
                              $x++;
                              $det[1] =$det[1].$dat;
                              
                        $det[2]=$databatch[$i]->itm_name;
                          
                }
                
                
                    
 
            	
           echo json_encode($det);
    	            
    	    
                        	
                       
    	}
    	
    	function pro_bin(){
    	            
    	            //$id = $this->input->post('id');
    	            
    	            $this->db->from('ret_to_stock');
    	            $this->db->select("*");
                    $this->db->join( "batch", "batch.batch_id = ret_to_stock.batch", 'left');
                    $this->db->join( "mes_item", "batch.product = mes_item.item_id", 'left');
                    $this->db->join( "item", "mes_item.item_name = item.itm_id", 'left');
                    $this->db->where('batch.product', $this->input->post('id'));
                    $this->db->where('ret_to_stock.to_bin >', 0);
                    $deta = $this->db->get();
                    $databatch = $deta->result();  
    	            
    	            
    	            //$this->db->from('batch');
                    //$this->db->select("*");
                    //$this->db->where('product', $this->input->post('id'));
                    //$this->db->where('avl_qty!=', 0);
                   // $this->db->join( "mes_item", "batch.product = mes_item.item_id", 'left');
                    //$this->db->join( "item", "mes_item.item_name = item.itm_id", 'left');
                    //$deta = $this->db->get();
                    //$databatch = $deta->result();
    	            
    	            
    	             //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_product' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Product</button></br>";
            //$det[0]="";
            $det[0] ="
            
                                 <tr>
														<th scope='col'>ID</th>
														<th >Batch Code</th>
														<th >Bin Stock</th>
														<th >Bin Clear</th>
														                                         
																											
													</tr>
                                
                               
                             
           ";
           
           $det[1]="";
           $det[2]="";
           
           $i = 1;
                for($i=0; $i<count($databatch); $i++){
                    
                     $x = $i+1;
                    $dat =  "
                    
                              <tr><td>".$x."</td>
                               <td><input type='hidden' name='binrowid[]' id='binrowid' value='".$databatch[$i]->to_stock_id."' id='binqty'>".$databatch[$i]->batch."</td>
                               <td><input type='hidden' name='bin[]' id='bin' value='".$databatch[$i]->to_bin."' id='binqty'>".$databatch[$i]->to_bin."</td>
                                <td><input type='text' name='binqty[]' id='binqty' value='' id='binqty'></td>
                                
                              </tr>
                              ";
                              $x++;
                              $det[1] =$det[1].$dat;
                              
                        $det[2]=$databatch[$i]->itm_name;
                          
                }
                
                
                    
 
            	
           echo json_encode($det);
    	            
    	    
                        	
                       
    	}
    	
    	function pro_batch_depo(){
    	            
    	            //$id = $this->input->post('id');
    	            
    	            
    	            $this->db->from('batch_depo');
                    $this->db->select("*");
                    $this->db->where('product', $this->input->post('id'));
                    $this->db->where('depo', $this->input->post('depo'));
                    $this->db->where('avl_qty!=', 0);
                    //$this->db->join( "mes_suppliers", "mes_po.po_sup_id = mes_suppliers.sup_id", 'inner');
                    $deta = $this->db->get();
                    $databatch = $deta->result();
    	            
    	            
    	             //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_product' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Product</button></br>";
            //$det[0]="";
            $det[0] ="
            
                                 <tr>
														<th scope='col'>ID</th>
														<th >Batch Code</th>
													
													
														<th>Exp Date</th>
													
														<th>Manf Date</th>
													    <th>Stock</th>                                         
																											
													</tr>
                                
                               
                             
           ";
           
           $det[1]="";
           
           $i = 1;
                for($i=0; $i<count($databatch); $i++){
                    
                     $x = $i+1;
                    $dat =  "
                    
                              <tr><td>".$x."</td>
                               <td>".$databatch[$i]->batch."</td>
                                <td>".$databatch[$i]->exp_date."</td>
                                 <td>".$databatch[$i]->man_date."</td>
                                <td>".$databatch[$i]->avl_qty."</td>
                              </tr>
                              ";
                              $x++;
                              $det[1] =$det[1].$dat;
                          
                }
                
 
            	
           echo json_encode($det);
    	            
    	    
                        	
                       
    	}
    	
    	
    	
    	//po data
    	    function po_view(){
             $this->db->from('mes_po');
            $this->db->select("*");
            $this->db->join( "mes_suppliers", "mes_po.po_sup_id = mes_suppliers.sup_id", 'inner');
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="";
            $det[1] ="<tr>
														<th >POID</th>
														<th>Supplier</th>
														<th>Description</th>
														<th>Status</th>
														<th></th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    $dat =  "
                              <tr><td>".$data[$i][0]->po_id."</td>
                                <td>".$data[$i][0]->sup_name."</td>
                                <td>".$data[$i][0]->po_tot_price."</td>
                                <td>".$data[$i][0]->po_type."</td>
                                
                                <td>
                                <button type='button' class='btn btn-primary' type='button' id='edit_brnd/".$data[$i]->po_id."' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'></button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
            function part_del(){
            
            $this->db->set('part_status', "0");
                $this->db->where('part_id', $this->input->post('id'));
                $result=$this->db->update('mes_partnumber');
                
                if($result){
                    $ret = "Part Number Removed";
                }else{
                    $ret = "Please try again";
                }
                echo json_encode($ret);
                
        }
        
            function dis_part_edit(){
                $id = $this->input->post('id');
    	    
            
            $prt =$this->Product_model->part_num($id);
                    
                    $det="";
                    
                    $det.='<div class="form-group row">
                    
                                <div class="col-md-10"><input type="text" name="new_part" id="new_part" class="form-control" value=""></div>
                        <div class="col-md-2"><button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="new_part/'.$id.'" onclick="fnnew_part(this.id)" data-target="#Modal_edit" data-backdrop="static" data-keyboard="false">Add New</button>
                                </div></div><br>';
                                
                    
                    foreach($prt as $part){
                        $txtid = "txt".$part->part_id;
                        
                        
                        $det.=        '<div class="form-group row">
                                <div class="col-md-10"><input type="text" name="'.$txtid.'" id="'.$txtid.'" class="form-control" value="'.$part->part_no.'"></div>
                                <div class="col-md-2">
                                <button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="edit_part/'.$part->part_id.'" onclick="save_part(this.id)" data-target="#Modal_edit" data-backdrop="static" data-keyboard="false">Save</button>
                                <button type="button" class="btn btn-outline-danger btn-sm" type="button" id="delete_part/'.$part->part_id.'" onclick="del_part(this.id)" data-target="#Modal_edit" data-backdrop="static" data-keyboard="false">X</button>
                                
                                </div>
                                </div>
                                ';
                    }     
                        
                     echo json_encode($det);
            }
            
            function save_part_edited(){
                
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('num', 'Part Number', 'required');
            
                  if ($this->form_validation->run() == FALSE){
                   //$ret[0] = validation_errors();
                    $ret = "Enter Part Number";
    	   }else{
                $this->db->set('part_no',$this->input->post('num'));
                $this->db->where('part_id', $this->input->post('id'));
                $result=$this->db->update('mes_partnumber');
                
                if($result){
                    $ret = "Part Number Updated";
                }else{
                    $ret = "Please try again";
                }
    	   } 
                
                echo json_encode($ret);
            }
            
            function save_part_new(){
                
                $data = array(
                'part_no' => $this->input->post('num'), 
                'part_itmno'  => $this->input->post('id')
            );
            $result = $this->db->insert('mes_partnumber',$data);
                if($result){
                    $ret = "New Part Number Added";
                }else{
                    $ret = "Please Re enter all details";
                }
                
                echo json_encode($ret);
            }
            
            function dupli_pro(){
                    $this->db->from('mes_item');
                    $this->db->select("*");
                    $this->db->where('item_cat',$this->input->post('cat_type'));
                    $this->db->where('item_name',$this->input->post('item'));
                    $this->db->where('item_brand',$this->input->post('brand'));
                    $hasil=$this->db->get();
                    $data =  $hasil->result();
                    
                    
                    if($hasil->num_rows()<0){
    	            $ret = "error";
                    }else{
                        $ret = "ok";
                    }
                    echo json_encode($ret);
                    
            }
        
        }
?>