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

class Job_Controller extends CI_Controller {

        public function __construct() {
           
        parent::__construct();
        
          $this->load->model('Product_model');
          $this->load->model('General');
          $this->load->model('Common_model');
        }
        
    function index(){
         
         $data['category'] = $this->Product_model->cat_list();
           
            $data['curren'] = $this->Product_model->currency_list();
            $data['unit'] = $this->Product_model->unit_list();
         $data['content'] = ('pages/job'); 
		$this->load->view('template',$data);
         
     }
     
    function tech_view(){
            
            $this->db->from('mes_technician');
            $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
            
             $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tech' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New Technician</button></br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Full Name</th>
														<th>Type</th>
														<th>Contact</th>
														<th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    if($data[$i]->tech_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr scope='row'>
                                <td>".$data[$i]->tech_code.$data[$i]->tech_id."</td>
                                <td>".$data[$i]->tect_fname." ".$data[$i]->tech_lname."</td>
                                 <td>".$data[$i]->tech_deparment."</td>
                                 <td>".$data[$i]->tech_contact."</td>
                                 
                               
                                $sts
                                
                               <td style='text-align: right;'>
                               
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_tech/".$data[$i]->tech_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' >Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
        }
     
    function tech_add(){
              
            
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_tech'>
                                <label class='col-md-2 col-form-label'>Frist Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='f_name' id='f_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            <div class='form-group row'>
                             <label class='col-md-2 col-form-label'>Last Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='l_name' id='l_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                             <div class='form-group row'>
                             <label class='col-md-2 col-form-label'>Contact</label>
                                <div class='col-md-10'>
                                  <input type='text' name='cont' id='cont' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                              <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Type</label>
                                <div class='col-md-10'>
                                      <select class='form-control' id='status' name='status'>
                                            <option value='Internal'>Internal </option>
                                            <option value='External'>External</option>
                                        </select>
                                 </div>
                            </div> 
 
              ";
                        
                	
               echo json_encode($det);	
        }
        
    function tech_save(){
           // $name =  $this->input->post('tc_cat');
    	  // $des =  $this->input->post('tax_value');
    	   
                 $this->form_validation->set_error_delimiters('', ''); 

                    $this->form_validation->set_rules('f_name', 'First Name', 'required');
                    $this->form_validation->set_rules('l_name', 'Last Name', 'required');
                    
    	   
    	   if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       $data = array(
                'tect_fname' => $this->input->post('f_name') ,
                'tech_lname' => $this->input->post('l_name'),
                 'tech_contact' => $this->input->post('cont'),
                 'tech_deparment' => $this->input->post('status'),
                  	
                
                'tech_cuser' => "admin",
                'tech_cdate' => (date('Y-m-d'))
            );
            $result = $this->db->insert('mes_technician',$data);
                if($result){
                    $ret[0] = "New Technician Detail Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
    	   
    	        $ret[1] = "showalltech";
                echo json_encode($ret);	
        }
        
    function tech_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_technician'); 
            $this->db->select("*");
            //$this->db->join( "mes_user_lvl", "mes_user.user_lvl = mes_user_lvl.user_lvl_val", 'inner');
            $this->db->where('tech_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            
            $act = $inact = $acti = $inacti = "";
           if($data[0]->tech_status=="1"){$act = "selected";}else{$inact = "selected";}
           if($data[0]->tech_deparment=="Internal"){$acti = "selected";}else{$inacti = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->tech_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_tech">
               
                <div class="form-group row">
                                <label class="col-md-2 col-form-label">Frist Name</label>
                                <div class="col-md-10">
                                 <input type="text" name="ef_name" id="ef_name" class="form-control"  value="'.$data[0]->tect_fname.'">
                                 </div>
                            </div>
                            
                              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Last Name</label>
                                <div class="col-md-10">
                                 <input type="text" name="el_name" id="el_name" class="form-control"  value="'.$data[0]->tech_lname.'">
                                 </div>
                            </div>
           
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact</label>
                                <div class="col-md-10">
                                 <input type="text" name="el_con" id="el_con" class="form-control"  value="'.$data[0]->tech_contact.'">
                                 </div>
                            </div>
                       
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Type</label>
                                <div class="col-md-10">
                                      <select class="form-control" id="type" name="type">
                                            <option value="Internal" '.$acti.'>Internal</option>
                                            <option value= "External" '.$inacti.'>External</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 tech_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('ef_name', 'Frist Name', 'required');
                $this->form_validation->set_rules('el_name', 'Last Name', 'required');
               
              
                
                //$this->form_validation->set_rules('rnpwdt', 'New Password', 'trim');
                
                
                if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
    	   }else{
    	       
    	    
    	       
    	        $this->db->set('tect_fname', $this->input->post('ef_name'));
                $this->db->set('tech_lname', $this->input->post('el_name'));
                $this->db->set('tech_contact', $this->input->post('el_con'));
                $this->db->set('tech_deparment', $this->input->post('type'));
                $this->db->set('tech_status', $this->input->post('status'));
                
                $this->db->set('tech_euser', "sadmin");
                $this->db->set('tech_edate', date('Y-m-d'));
                
                $this->db->where('tech_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_technician');
                
                if($result){
                    $ret[0] = "Technician Details Updated";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "showalltech";
    	    echo json_encode($ret);	
    	}
    	
    function job_view(){
            
            $this->db->from('mes_job');
            $this->db->select("*");
            $this->db->order_by("job_id", "desc");
            $detail = $this->db->get();
            $data = $detail->result();
            
        
            
            
            
            $link = site_url('new_job');
             $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Job Card</button></a></br>";
            
            // $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_job' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New Job</button></br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Description</th>
														<th>Technician</th>
														<th>Start</th>
														<th>End</th>
														<th>Date</th>
														<th>Complete</th>
														<th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    $tid = $data[$i]->job_techid;
                    $tecdata = explode(",",$tid);
                    
                    $tecname="";
                    for($j=1; $j<count($tecdata); $j++){
                        
                        $this->db->from('mes_technician');
                        $this->db->select("*");
                        $this->db->where('tech_id',$tecdata[$j]);
                        $detail = $this->db->get();
                        $datatech = $detail->result();
                        
                        $tecname = $datatech[0]->tect_fname.' '.$datatech[0]->tech_lname.$tecname."<br>";
                        
                    }
                    
                    $this->db->select("job_items.jobitm_itmid");    
                    $this->db->from('job_items');
                    $this->db->join( "mes_job", "job_items.jobitm_jobid = mes_job.job_id", 'left');
                    $this->db->join( "mes_item", "job_items.jobitm_itmid = mes_item.item_id", 'left');
                    $this->db->where('job_items.jobitm_jobid', $data[$i]->job_id);
                    $this->db->where('job_items.jobitm_itmid!=',0 );
                    $this->db->where('mes_item.item_cat',10 );
                    $query = $this->db->get();
                            if ($query->num_rows() > 0) { $prtl = "<a href ='".base_url()."tool_pdf/".$data[$i]->job_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Tool Note</button></a>";
                                
                            }else{$prtl="<button type='button' class='btn btn-outline-secondary btn-sm' >No Tools</button>"; }
                    
                    $this->db->select("job_items.jobitm_itmid");    
                    $this->db->from('job_items');
                    $this->db->join( "mes_job", "job_items.jobitm_jobid = mes_job.job_id", 'left');
                    $this->db->join( "mes_item", "job_items.jobitm_itmid = mes_item.item_id", 'left');
                    $this->db->where('job_items.jobitm_jobid', $data[$i]->job_id);
                    $this->db->where('job_items.jobitm_itmid!=',0 );
                    $this->db->where('mes_item.item_cat!=',10 );
                    $query = $this->db->get();
                            
                    $prmt="";
                    if($data[$i]->job_status==0){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                   if($data[$i]->job_stock==1){$stk = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$stk = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>";}
                   
                   if($data[$i]->job_complete==1){
                       $cmp = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";
                       $com = "<button type='button' class='btn btn-outline-secondary btn-sm' id='".$data[$i]->job_id."' onclick='view_detail(this.id)'>Completed</button>";
                       
                    }else{
                            $cmp = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>";
                        
                       if ($query->num_rows() > 0) { 
                           $prmt = "<a href ='".base_url()."material_pdf/".$data[$i]->job_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Material Note</button></a>";
                                
                        }else{
                            $prmt="<button type='button' class='btn btn-outline-secondary btn-sm' >No Materials</button>"; }
                            $com = "<a href ='".base_url()."job_addpro/".$data[$i]->job_id."' > <button type='button' class='btn btn-outline-secondary btn-sm' >Add Tools & Equipment</button></a>
                                  <a href ='".base_url()."job_comp/".$data[$i]->job_id."' > <button type='button' class='btn btn-outline-secondary btn-sm' >Complete</button></a>";
                  
                   }
                   
                    $dat =  "
                              <tr scope='row'>
                                <td>".$data[$i]->job_code .$data[$i]->job_id."</td>
                                <td>".$data[$i]->job_cus.'<br>'.$data[$i]->job_add.'<br>'.$data[$i]->job_contct."</td>
                                 <td>".$tecname."</td>
                                 <td>".$data[$i]->job_sdate."</td>
                                 <td>".$data[$i]->job_enddate."</td>
                                 <td>".$data[$i]->job_cdate."</td>
                                  
                                   $cmp
                                   $sts
                                
                               <td style='text-align: right;'>
                               
                                <a href ='".base_url()."job_pdf/".$data[$i]->job_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                              
                              
                                
                                 <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_tech/".$data[$i]->job_id."' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button>
                                 <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_tech/".$data[$i]->job_id."' onclick='edit_view(this.id)' data-target='#Modal_edit'>Cancel</button>
                                  
                                    ".$com.$prmt.$prtl."
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
        }
        
    function newjob(){
        
            $columns_arr= array("*");            
            $where_arr = array('tech_status ='=> 1);                     
            $resultagentt = $this->General->getData('mes_technician',$columns_arr,$where_arr);
            $data['tccatdetailst'] = $resultagentt;
            
            $columns_arr= array("*");            
            //$where_arr = array('sup_status!='=> 0);                     
            $resultagent = $this->General->getData('mes_termcat',$columns_arr);
            $data['tccatdetails'] = $resultagent;
            
            $data['category'] = $this->Product_model->cat_list();
           
            $data['curren'] = $this->Product_model->currency_list();
            $data['unit'] = $this->Product_model->unit_list();
            $data['tax'] = $this->Product_model->tax_list();
       
        
        $data['content'] = ('pages/newjob'); 
		$this->load->view('template',$data); 
            
        }
    	
    function job_save(){
    	    
    	    // var_dump($_POST);
    	 
    	   $term =  $this->input->post('term');
    	   $length = count($term);
              $tc="";
            for ($i = 0; $i < $length; $i++) {
              $tc = $tc.",".$term[$i];
            
            }
    	   
    	    
    	    $data = array(
                'job_techid' => $tc ,
                 'job_Remarks' => $this->input->post('remark'),
                 'job_cus' => $this->input->post('job_cus'),
                  'job_title' => $this->input->post('title'),
                 'job_add' => $this->input->post('job_addrs'),
                 'job_contct' => $this->input->post('job_cont'),
                 'job_qty' => $this->input->post('inv_tot_item'),
                 'job_total' => $this->input->post('inv_total'),
                  'job_dis' => $this->input->post('dis_total'),
                  'job_net' => $this->input->post('net'),
                  'job_other' => $this->input->post('other_amnt'),
                  'job_grnd' => $this->input->post('final'),
                  'job_sdate' => $this->input->post('sdate'),
                   'job_enddate' => $this->input->post('edste'),
                   
                  
                
                'job_cuser' => "admin",
                'job_cdate' => (date('Y-m-d'))
            );
           $result = $this->db->insert('mes_job',$data);
           $lastid = $this->db->insert_id();
               if($result){
                   $ret = "New Job Detail Saved";
                   
                $des = $this->input->post('serdesc');
                $unit = $this->input->post('serid_id');
                $qty = $this->input->post('serqty');
                $uprice = $this->input->post('serprice');
                $itmtot = $this->input->post('itmtot');
                $itmid = $this->input->post('serid');
                
                
                    for ($i = 0; $i < count($des); $i++) {
                     
                        $data = array(
                        'jobitm_jobid' => $lastid ,
                         'jobitm_des' => $des[$i],
                         'jobitm_qty' => $qty[$i],
                         'jobitm_unt' => $unit[$i],
                         'jobitm_rate' => $uprice[$i],
                         'jobitm_tot' => $itmtot[$i],
                         'jobitm_itmid' => $itmid[$i],
                         
                     
                    );
            
                $result = $this->db->insert('job_items',$data);
                
                if($itmid[$i]!=""){
                    $data = $this->Common_model->cal_stock("JBC",$lastid,$itmid[$i],$qty[$i],"min",0);
                }
            }   
                   
                    
                   
                   
               }else{
                    $ret = "Please Re enter all details";
                }
                echo json_encode($ret);
    	    
    	}
    	
    function viewpdf_job($insert_id){
            
        $this->db->select("*");    
        $this->db->from('mes_job');
        $this->db->join( "job_items", "job_items.jobitm_jobid = mes_job.job_id", 'inner');
        $this->db->where('job_id', $insert_id );
        $this->db->where('job_items.jobitm_itmid',0 );
        $query = $this->db->get();
        $dataa=$query->result();
        $data['inv']=$query->result();
        
        $tec = explode(',',$data['inv'][0]->job_techid);
        $length = count($tec);
        
   
        
         for($i=0; $i<count($data['inv']); $i++){
                   
                    $inv[] = array($data['inv'][$i]->jobitm_des,$data['inv'][$i]->jobitm_unt,$data['inv'][$i]->jobitm_qty); 
                
        }
         $data['inv_itm'] = $inv;
        
        
        $tm[]="";
        for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_technician');
                $this->db->where('tech_id', $tec[$i]);
                //$this->db->order_by("mes_tmdes.tmdes_catid", "asc");
                $query = $this->db->get();
                $tm[] =$query->result();
            } 
        
        $data['tm'][0] = $tm;
        

                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
      
            
            $this->load->view('pages/jobpdf',$data);
            
        }
        
    function met_job($insert_id){
            
        $this->db->select("*");    
        $this->db->from('job_items');
        $this->db->join( "mes_job", "job_items.jobitm_jobid = mes_job.job_id", 'inner');
        $this->db->join( "mes_item", "job_items.jobitm_itmid = mes_item.item_id", 'inner');
        $this->db->join( "mes_unit", "job_items.jobitm_unt = mes_unit.unit_id", 'left');
        $this->db->where('job_items.jobitm_jobid', $insert_id );
        $this->db->where('job_items.jobitm_itmid!=',0 );
        $this->db->where('mes_item.item_cat!=',10 );
        $query = $this->db->get();
        $dataa=$query->result();
        $data['inv']=$query->result();
        
        $tec = explode(',',$data['inv'][0]->job_techid);
        $length = count($tec);
        
   
        
         for($i=0; $i<count($data['inv']); $i++){
                   
                    $inv[] = array($data['inv'][$i]->jobitm_des,$data['inv'][$i]->unit_symble,$data['inv'][$i]->jobitm_qty); 
                
        }
         $data['inv_itm'] = $inv;
        
        
        $tm[]="";
        for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_technician');
                $this->db->where('tech_id', $tec[$i]);
                //$this->db->order_by("mes_tmdes.tmdes_catid", "asc");
                $query = $this->db->get();
                $tm[] =$query->result();
            } 
        
        $data['tm'][0] = $tm;
        

                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
      
            
            $this->load->view('pages/materialpdf',$data);
            
        }
        
    function tool_job($insert_id){
            
        $this->db->select("*");    
        $this->db->from('job_items');
        $this->db->join( "mes_job", "job_items.jobitm_jobid = mes_job.job_id", 'left');
        $this->db->join( "mes_item", "job_items.jobitm_itmid = mes_item.item_id", 'left');
        $this->db->join( "mes_unit", "job_items.jobitm_unt = mes_unit.unit_id", 'left');
        $this->db->where('job_items.jobitm_jobid', $insert_id );
        $this->db->where('job_items.jobitm_itmid!=',0 );
        $this->db->where('mes_item.item_cat',10 );
        $query = $this->db->get();
                if ($query->num_rows() > 0) {
                    
        $dataa=$query->result();
        $data['inv']=$query->result();
        
       
        $tec = explode(',',$data['inv'][0]->job_techid);
        $length = count($tec);
        
   
        
         for($i=0; $i<count($data['inv']); $i++){
                   
                    $inv[] = array($data['inv'][$i]->jobitm_des,$data['inv'][$i]->unit_symble,$data['inv'][$i]->jobitm_qty); 
                
        }
         $data['inv_itm'] = $inv;
        
        
        $tm[]="";
        for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_technician');
                $this->db->where('tech_id', $tec[$i]);
                //$this->db->order_by("mes_tmdes.tmdes_catid", "asc");
                $query = $this->db->get();
                $tm[] =$query->result();
            } 
        
        $data['tm'][0] = $tm;
        

                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
      
            
            $this->load->view('pages/toolpdf',$data);
                }else{
                  echo "No data";  
                }
            
        }
        
    function addproduct_job($insert_id){
           
           $data['jobid'] = $insert_id;
           
           
           
           $columns_arr= array("*");            
            $where_arr = array('job_id ='=> $insert_id);                     
            $jobdt = $this->General->getData('mes_job',$columns_arr,$where_arr);
            $data['jobdetals'] = $jobdt;
           
            $columns_arr= array("*");            
            $where_arr = array('tech_status ='=> 1);                     
            $resultagentt = $this->General->getData('mes_technician',$columns_arr,$where_arr);
            $data['tccatdetailst'] = $resultagentt;
            
            $columns_arr= array("*");            
            //$where_arr = array('sup_status!='=> 0);                     
            $resultagent = $this->General->getData('mes_termcat',$columns_arr);
            $data['tccatdetails'] = $resultagent;
            
            $data['category'] = $this->Product_model->cat_list();
           
            $data['curren'] = $this->Product_model->currency_list();
            $data['unit'] = $this->Product_model->unit_list();
            $data['tax'] = $this->Product_model->tax_list();
           
        $data['content'] = ('pages/addjobitem'); 
		$this->load->view('template',$data); 
		
            
            
        }
    	
    function newitem(){
    	            
    	          //print_r($_POST);
    	          $item = $this->input->post('serid');
    	          $qty = $this->input->post('serqty');
                    $desc = $this->input->post('serdesc');
            
            
             for($i=0; $i<count($item); $i++){
                    $this->db->select("item_unit,item_selling_price");    
                    $this->db->from('mes_item');
                    $this->db->where('item_id', $item[$i]);
                    $query = $this->db->get();
                    $itmdata = $query->result();
                 
                  $dat = array(
                        'jobitm_jobid'  => $this->input->post('j_id'),
                        'jobitm_des' => $desc[$i],
                        'jobitm_itmid' => $item[$i],
                        'jobitm_qty'  => $qty[$i], 
                        'jobitm_unt'  => $itmdata[0]->item_unit,
                        'jobitm_rate'  => $itmdata[0]->item_selling_price,
                        'jobitm_tot'  => $itmdata[0]->item_selling_price*$qty[$i],
                        'jobitm_newitm'  => 1
                    );
                $data = $this->db->insert('job_items',$dat);
                
                //$this->Common_model->cal_stock("JBC",$this->input->post('j_id'),$item[$i],$qty[$i],"min",1);
             }
            
            
      
            $data = "Added";
       
        
       echo json_encode($data);
        }
    	 
    function view_job_com($id){
        
         $this->db->from('mes_job');
            $this->db->select("job_complete");
            $this->db->where('job_complete', 1);
            $this->db->where('job_id', $id);
            $detail = $this->db->get();
            $data = $detail->result(); 
            
            if($detail->num_rows() > 0){
                $ret = "Already Completed";
                $this->index();
            }else{
                 $data['id'] = $id;
                $data['content'] = ('pages/job_comp'); 
		        $this->load->view('template',$data); 
            }
        
       
    }
    
   function viewjob_product(){
        
        $this->db->select("*");    
        $this->db->from('job_items');
        $this->db->join( "mes_job", "job_items.jobitm_jobid = mes_job.job_id", 'left');
        $this->db->join( "mes_item", "job_items.jobitm_itmid = mes_item.item_id", 'left');
        $this->db->join('mes_unit', 'mes_item.item_unit   = mes_unit.unit_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->where('job_items.jobitm_jobid', $this->input->post('jo_id'));
        $this->db->where('job_items.jobitm_itmid!=',0 );
        $this->db->where('job_items.jobitm_newitm!=',0 );
        $query = $this->db->get();
        $data = $query->result();
        
        
        $det['item']="";
          
                for($i=0; $i<count($data); $i++){
                    if($data[$i]->item_cat!="10"){
                        $ret = "0";  $dis = "";
                    }else{$ret = $data[$i]->jobitm_qty; $dis = "readonly";}
                    
                  $dat =  "
                                 <tr>
                               <td >".($i+1)."</td>
                               <td >".$data[$i]->cat_shortcode."-".$data[$i]->item_catcount."</td>
                               <td >".$data[$i]->jobitm_des."</td>
                               <td style='text-align: center;'>".$data[$i]->unit_symble."</td>
                               <td style='text-align: right;'>".$data[$i]->jobitm_rate."</td>
                                <td style='text-align: right;'>".$data[$i]->jobitm_qty."</td>
                               <td><input type='text' id=".$data[$i]->jobitm_id."  name='po_val[]' placeholder='' value='".$ret."' ".$dis." class='col-xs-12 col-sm-12 input-sm form-control-sm form-control txt_val'/ style='text-align: right;' onkeyup='get_cal()'>
                               <input type='hidden'  name='item_id[]' value='".$data[$i]->jobitm_id."'>
                               </td>
                              
                                </tr>";
                                
                              $det['item'] =$det['item'].$dat;
                }  
                echo json_encode($det);
    }
    
    function complete_job(){
                    $item = $this->input->post('item_id');
                    $val = $this->input->post('po_val');
                    
                $this->db->from('mes_job');
            $this->db->select("job_complete");
            $this->db->where('job_complete', 1);
            $this->db->where('job_id', $this->input->post('job_id'));
            $detail = $this->db->get();
            $data = $detail->result(); 
            
            if($detail->num_rows() > 0){
                $ret = "Already Completed";
            }else{
                for($i=0; $i<count($item); $i++){
                if($item[$i]!=""){
                    $this->db->set('retn_qty', $val[$i]);
                    $this->db->where('jobitm_id', $item[$i]);
                    $result=$this->db->update('job_items');
                    
                            $this->db->from('job_items');
                            $this->db->select("*");
                            $this->db->where('jobitm_id', $item[$i]);
                            $detai = $this->db->get();
                            $dat = $detai->result();
                        $nqty = $dat[0]->jobitm_qty - $val[$i];
                        
                     $data = $this->Common_model->cal_stock("JBC",$dat[0]->jobitm_jobid,$dat[0]->jobitm_itmid,$nqty,"min",0);
                }
            
        }
                
                
                $this->db->set('job_complete', 1);
                $this->db->set('job_comp_date', $this->input->post('codate'));
                $this->db->where('job_id', $this->input->post('job_id'));
                $result=$this->db->update('mes_job');
                
                if($result){
                    $ret = "Job Completed";
                }else{
                    $ret = "Please Re enter all details";
                }
            }
        
        
                echo json_encode($ret);
    }
    
    function get_completed(){
            $this->db->from('mes_job');
            $this->db->select("job_comp_date");
            $this->db->where('job_complete', 1);
            $this->db->where('job_id', $this->input->post('id'));
            $detail = $this->db->get();
            $data = $detail->result(); 
            
            $this->db->from('job_items');
            $this->db->select("*");
            $this->db->where('jobitm_jobid', $this->input->post('id'));
            $itmdet = $this->db->get();
            $item = $itmdet->result();
            
            $det[0] = "";
            $det[0]= "Completed on  -".$data[0]->job_comp_date."<br>";
            
            $det[1]="";$det[2]="";$det[3]="";
            
            for($i=0; $i<count($item); $i++){
                if($item[$i]->jobitm_itmid==0){
                    $det[1] .="Job  -".$item[$i]->jobitm_des."<br>";
                }
                if(($item[$i]->jobitm_itmid!=0)&&($item[$i]->jobitm_newitm==0)){
                    $det[2] .="Items  -".$item[$i]->jobitm_des."<br>";
                }
                if($item[$i]->jobitm_newitm!=0){
                    $det[3] .="Meterials and Tools  -".$item[$i]->jobitm_des."<br>Reserved  -".$item[$i]->jobitm_qty."<br>Return  -".$item[$i]->retn_qty."<br>";
                }
            }
            
            $det[4] = "<button type='button' class='btn btn-outline-danger' ><a href='".base_url()."job_inv_pdf/".$this->input->post('id')."' target='_blank'>Print Invoice</a></button></br>";
            echo json_encode($det);
            
    }
    
    function viewcom_job($insert_id){
        $this->db->select("*");    
        $this->db->from('job_items');
        $this->db->where('jobitm_jobid', $insert_id);
        $this->db->where('jobitm_newitm!=', 1);
        $query = $this->db->get();
        $dt=$query->result();
        
         for($i=0; $i<count($dt); $i++){
                if($dt[$i]->jobitm_itmid==0){
                    $inv[] = array("","",$dt[$i]->jobitm_des,"LKR",$dt[$i]->jobitm_rate,$dt[$i]->jobitm_qty,"",$insert_id);    
                }else{
                    $this->db->select("*");
                    $this->db->from('mes_item');
                    $this->db->where('item_id', $dt[$i]->jobitm_itmid);
                    $this->db->join('item', 'item.itm_id   = mes_item.item_name');
                    $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
                    $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
                    $this->db->join('mes_partnumber', 'mes_item.item_id  = mes_partnumber.part_itmno','left');
                    $this->db->join('mes_unit', 'mes_item.item_unit   = mes_unit.unit_id');
                    $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
                    $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
                    $query = $this->db->get();
                    $dataa=$query->result();
                    
                    if($dataa[0]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[0]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[0]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[0]->brand_name.$spece.$dataa[0]->itm_name."\n".$dataa[0]->model_name.$spece2.$dataa[0]->part_no.$spece1.$dataa[0]->spec_spec;
                    $inv[] = array($dataa[0]->cat_shortcode,$dataa[0]->item_catcount,$itm,"LKR",$dt[$i]->jobitm_rate,$dt[$i]->jobitm_qty,$dt[$i]->jobitm_itmid,$insert_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("*");    
        $this->db->from('mes_job');
        $this->db->where('job_id', $insert_id);
        $query = $this->db->get();
        $data['inv']=$query->result();
    
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        $this->load->view('pages/jobcomp_pdf',$data);
    }
    
    	 
}