<?php
if(isset($_REQUEST['save']))
  {
    
     $data['branch']        =$_REQUEST['site'];
     $data['mrs_no']        =$_REQUEST['mrs_no'];
     $data['mrs_date']      =FormatDateForSQL($_REQUEST['mrs_date']);  
     $data['mrs_type']      =$_REQUEST['mrs_type'];
     $data['jobcard_id']    =$_REQUEST['jobcard'];
     $data['jobcard_date']  =FormatDateForSQL($_REQUEST['jobcard_date']); 
     $data['sparekit']      =$_REQUEST['sparekit'];
     $data['reading']       =$_REQUEST['reading'];
     $data['reading_unit']  =$_REQUEST['reading_unit'];
     $data['remark']        =$_REQUEST['remark'];
     $data['CompanyId']     =$_SESSION['ACGCompanyId'];
     $data['modified_by']   =$_SESSION['UserID'];   
     $data['modified_date'] =date('Y-d-y');

     
    $cond = "id='".$_REQUEST['mrs_id']."'"; 
    $s->RecordEdit('material_request_slip',$data,$cond);

    $sql1 = "DELETE FROM material_request_slip_details WHERE mrs_id='".$_REQUEST['mrs_id']."'";
    $records1 = DB_query($sql1,$db);

    foreach($_REQUEST['asset'] as $key=>$val)
    {
           foreach($_REQUEST['qty'][$val] as $key1=>$val1)
           {
                  
                      if($val1!='')
                      {
                        $data3['mrs_id']=$_REQUEST['mrs_id'];
                        $data3['asset_id']=$val;
                        $data3['spare_id']=$_REQUEST['spare'][$val][$key1];
                        $data3['item_id']=$_REQUEST['item'][$val][$key1];
                        $data3['item_id']=$_REQUEST['item'][$val][$key1];
                        $data3['unit']=$_REQUEST['unit'][$val][$key1];
                        $data3['qty']=$val1;
                        $s->RecordInsert('material_request_slip_details', $data3);
                      }
                    
           }
    }    
  $s->pageLocation("index.php?route=ToolPlants/fixed_assets/material_request_slip_index".$rurl);
}

$sql = "SELECT * FROM material_request_slip WHERE id='".$_REQUEST['mrs_id']."'";
$records = DB_query($sql,$db);
$row = DB_fetch_array($records);
extract($row);

?>


<div style="display:none" id="div_id"></div>
<link rel="stylesheet" href="css/new_mxerp1.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $s->getField('modules_form','id',$_REQUEST['frmid'],'frm_name'); ?>  </h1>
<form action="" method="Post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%"><strong>Branch</strong></td>
    <td width="20%"><select name="site1" id="site1" style="width:100%;" onchange="show_jobcard(this.value)" disabled >
      <option value="0">Select</option>
      <?php
       $sql=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
       while($r=DB_fetch_array($sql))
       {
       ?>
       <option value="<?php echo $r['loccode']; ?>" <?php echo ($r['loccode']==$branch)?'selected':''; ?>><strong><?php echo $r['locationname']; ?></strong></option>
       <?php } ?>
       </select>
       <input type="hidden" name="site" id="site" style="width:100%;" value="<?php echo $branch;?>" readonly />    
    </td>
    <td width="13%"><strong>MRS No.</strong></td>
    <td width="20%">
      <input type="text" name="mrs_no" id="mrs_no" style="width:100%;" value="<?php echo $mrs_no;?>" readonly />
      <input type="hidden" name="mrs_id" id="mrs_id" style="width:100%;" value="<?php echo $id;?>" readonly />
    </td>
    <td width="13%"><strong>MRS Date</strong></td>
    <td><input type="text" name="mrs_date" id="mrs_date" style="width:100%;" class="datepicker" value="<?php echo ConvertSQLDate($mrs_date);?>" readonly /></td>
  </tr>
  <tr class="row1">
    <td width="13%"><strong>Type</strong></td>
    <td width="20%"><select name="mrs_type1" id="mrs_type1" style="width:100%;" onchange="show_jobcard(this.value)" disabled >
      <option value="" <?php echo ($mrs_type=='')?'selected':'';?>>Select</option>
      <option value="D" <?php echo ($mrs_type=='D')?'selected':'';?>>Direct</option>
      <option value="J" <?php echo ($mrs_type=='J')?'selected':'';?>>Job Card</option>
      </select> 
      <input type="hidden" name="mrs_type" id="mrs_type" style="width:100%;" value="<?php echo $mrs_type;?>" readonly />    
    </td>
    <?php if($mrs_type=='J'){?>
    <td><strong>Jobcard No.</strong></td>
    <td><select name="jobcard1" id="jobcard1" style="width:100%;" onchange="show_jobcard_info(this.value)" disabled>
      <option value="0">Select</option> 
      <?php $sql_machine=DB_query("select * from tpl_breakdown_mgmt where site='".$branch."' and status='1'",$db);
      while($row=DB_fetch_array($sql_machine))
      {?>
          <option value="<?php echo $row['id'];?>" <?php echo ($row['id']==$jobcard_id)?'selected':''; ?>><strong><?php echo $row['tt_no'];?></strong></option>
      <?php } ?>
    </select>
    <input type="hidden" name="jobcard" id="jobcard" style="width:100%;" value="<?php echo $jobcard_id;?>" readonly /> 
     </td>
    <td><strong>Jobcard Date</strong></td>
    <td><input type="text" name="jobcard_date" id="jobcard_date" value="<?php echo ConvertSQLDate($jobcard_date);?>" style="width:100%;" readonly /></td>
    <?php }else  if($mrs_type=='D'){ ?>
    <td><strong>Asset</strong></td>
    <td><select name="sparekit1" id="sparekit1" style="width:100%;" onchange="show_jobcard_info(this.value)" disabled>
      <option value="0">Select</option> 
      <?php $sql_machine=DB_query("select * from spare_part_kits",$db);
      while($row=DB_fetch_array($sql_machine))
      {?>
          <option value="<?php echo $row['id'];?>" <?php echo ($row['id']==$sparekit)?'selected':''; ?>><strong><?php echo $s->getField('machine_center','id',$row['kitid'],'code')."--".$s->getField('machine_center','id',$row['kitid'],'name')."--".$s->getField('machine_center','id',$row['kitid'],'sr_no');?></strong></option>
      <?php } ?>
    </select>
    <input type="hidden" name="sparekit" id="sparekit" style="width:100%;" value="<?php echo $sparekit;?>" readonly /> 
     </td>
    <td><strong>Reading KM/Hrs</strong></td>
    <td><input type="text" name="reading" id="reading" value="<?php echo $reading;?>" style="width:60%;" readonly /><input type="text" name="reading_unit" id="reading_unit" value="<?php echo $reading_unit;?>" style="width:35%;" readonly /></td>
    <?php } ?>
  </tr>
 
    <tr class="row1">
    <td colspan="6" style="padding:0px;" id="datashow">
      <?php
      $data1='';
    $data1.="<table cellpadding='0' border='0' width='100%'>";
    $data1.="<tr>";
    $data1.="<th width='4%'><strong>S. No.</strong></th>";
    $data1.="<th width='24%'><strong>Spare Code</strong></th>";
    $data1.="<th width='24%'><strong>Spare Name</strong></th>";
    $data1.="<th width='24%'><strong>Unit</strong></th>";
    $data1.="<th width='24%'><strong>QTY.</strong></th>";
    $data1.="</tr>";
    if($mrs_type=='J'){
        $sql_breakdown=DB_query("select asset_id from tpl_breakdown_mgmt_machine where  master_id='".$jobcard_id."'",$db);
    }else{
        $sql_breakdown=DB_query("select kitid from spare_part_kits where id='".$sparekit."'",$db);
    }
    $i=1;
    while($row_break=DB_fetch_array($sql_breakdown))
    {
        if($mrs_type=='D'){
            $row_break['asset_id']=$row_break['kitid'];
        }    
        $data1.="<tr>";
        $data1.="<th width='4%'><strong><input type='hidden' name='asset[]' value='".$row_break['asset_id']."' /> ".$i."</strong></th>";
        $data1.="<th width='24%'><strong>".$s->getField('machine_center','id',$row_break['asset_id'],'code')."</strong></th>";
        $data1.="<th width='24%'><strong>".$s->getField('machine_center','id',$row_break['asset_id'],'name')."</strong></th>";
        $data1.="<th width='24%'><strong></strong></th>";
        $data1.="<th width='24%'><strong></strong></th>";
        $data1.="</tr>";
        
        $kitid=$s->getField('spare_part_kits','kitid',$row_break['asset_id'],'id');

        $sql_breakdown1=DB_query("select * from spare_part_kits_details where parant_id='".$kitid."'",$db);
        $sql_row=DB_num_rows($sql_breakdown1);
        if($sql_row>0)
        {  
            $data1.="<tr>";
            $data1.="<td colspan='5'>";
            $data1.="<table cellpadding='0' border='0' width='100%'>";
            $m=1;
            while($row_break1=DB_fetch_array($sql_breakdown1))
            {
                $data1.="<tr>";
                $data1.="<td width='4%'><input type='hidden' name='spare[".$row_break['asset_id']."][]' value='".$row_break1['id']."' /><input type='hidden' name='item[".$row_break['asset_id']."][]' value='".$row_break1['item_id']."' />".$m."</td>";
                $data1.="<td width='24%'>".$s->getField('item_master','id',$row_break1['item_id'],'StockID')."</td>";
                $data1.="<td width='24%'>".$s->getField('item_master','id',$row_break1['item_id'],'item_name')."</td>";
                $data1.="<td width='24%'><input type='hidden' name='unit[".$row_break['asset_id']."][]' value='".$s->getField('item_master','id',$row_break1['item_id'],'mst_unitid')."' />".$s->getField('unitsofmeasure','unitid',$s->getField('item_master','id',$row_break1['item_id'],'mst_unitid'),'unitname')."</td>";
                //echo "select qty from material_request_slip_details where mrs_id='".$_REQUEST['mrs_id']."' and spare_id='".$row_break1['id']."'";
                $sql_qty=DB_query("select qty from material_request_slip_details where mrs_id='".$_REQUEST['mrs_id']."' and spare_id='".$row_break1['id']."'",$db);
                $sql_q=DB_fetch_array($sql_qty);
                $data1.="<td width='24%'><input type='text' name='qty[".$row_break['asset_id']."][]' value='".$sql_q['qty']."' /></td>";
                $data1.="</tr>";
                $m++;
            }
            $data1.="</table>";
            $data1.="</td>";
            $data1.="</tr>";
        }
      $i++;    
    }
  echo $data1.="</table>";
?>
    </td>
    </tr>
    <tr class="row1">
  <td>Remark</td>
   <td  colspan="8"><textarea name="remark"><?php echo $remark; ?></textarea></td>
 </tr> 
    <tr class="row1">
  <td style="text-align:center;"  colspan="9"><input type="submit" name="save" class="search" value="Save" /></td>
 </tr> 
</table>
</form>

</div></div>

<script>
function show_jobcard(str)
{
  $.ajax({
    url:"ajax_dynamic_jobcard.php",
    data:{n:str},
    type:"GET",
    success : function(data){
      $('#jobcard').html(data);
    }
  });
}

function show_jobcard_info(str)
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }else{// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {    
            var data=xmlhttp.responseText.split("__");
            document.getElementById("jobcard_date").value=data[0];
            document.getElementById("datashow").innerHTML=data[1];
        }
    }
    xmlhttp.open("GET","ajax_dynamic_jobcard.php?jobcard="+str,true);
    xmlhttp.send();
}

var jj=$.noConflict();
 jj(function() {  
jj(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
    changeYear: true,
    yearRange: '-80:+20'});
});

</script> 

