<?php 
$url_cancel= "index.php?route=ToolPlants/Transaction/asset_breakdown_testing_index".$rurl;
include('includes/Class_Branch.php');
include('includes/AccountGroupMaster_Class.php');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$rurl="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=ToolPlants/Transaction/asset_breakdown_completion_index".$rurl;
$msgErr=array();
//$_SESSION['manvi'];
?>

<?php
		
//Add Progress
if($_REQUEST['action']=='addProdress'){
   //echo $_REQUEST['rid2'];
  $jobid=$s->getField('tpl_breakdown_mgmt_assig','id',$_REQUEST['rid2'],'job_id');

	$con ="id='".$jobid."'";
	$Get_id = $s->GetRecordById('tpl_breakdown_mgmt',$con);
	$var = DB_fetch_array($Get_id);
	
	$sqlAset = "SELECT * FROM machine_center WHERE ID= ".$_REQUEST['assetID']."";
	$resultDisplay = DB_query($sqlAset,$db);
	$assetRows = DB_fetch_array($resultDisplay);
	// asset group
	$reqDet_sql1 = "SELECT * FROM machine_group_master WHERE id= ".$assetRows['under_group']."";
		$result1 = DB_query($reqDet_sql1,$db);
		$assetRow1 = DB_fetch_array($result1);
		
	// unit of measurement
	$sqlU=DB_query("Select * from unitsofmeasure where unitid='".$assetRows['unit']."' ",$db); 
		 $myrow_unit=DB_fetch_array($sqlU);
		 
	// end meter reading
	 $sqlH=DB_query("SELECT MAX(end_meter_reading) AS HighestPrice FROM tpl_fixed_asset_logbook_details where asset_code=".$assetRows['id']." ",$db); 
		 $myrow_mx=DB_fetch_array($sqlH);
}

	if(isset($_POST['addProdress']))
    {
	
	        $delete = DB_query("delete from tpl_breakdown_mgmt_details where
		       master_id = '".$_REQUEST['rid2']."'",$db);

	for($l=0; $l<count($_REQUEST['p_status']); $l++)
		{	
				
    
      $data5['break_type'] = $_REQUEST['sub_break_down1'][$l];
		$data5['break_sub_type'] = $_REQUEST['sub_break_down'][$l];
		$data5['break_reason'] = $_REQUEST['break_reason'][$l];
		$data5['technician'] = $_REQUEST['technician'][$l];
		$data5['rec_date'] = FormatDateFORSQL($_REQUEST['rec_date'][$l]);
      $data5['completed_date']=FormatDateFORSQL($_REQUEST['completed_date'][$l]);
		$data5['rec_time'] = $_REQUEST['rec_time'][$l];
		$data5['com_date'] = FormatDateFORSQL($_REQUEST['com_date'][$l]);
     	$data5['remarks'] = $_REQUEST['p_Remarks'][$l];
       $data5['safty'] = $_REQUEST['break_safty'][$l];
    	$data5['master_id'] = $_REQUEST['rid2'];
		$data5['progress_status'] = $_REQUEST['p_status'][$l];
		$data5['remarks'] = $_REQUEST['p_Remarks'][$l];
      $data5['tested_date'] = FormatDateFORSQL($_REQUEST['tested_date'][$l]);
      $data5['test_details'] = $_REQUEST['test_details'][$l];
	
		$s->RecordInsert('tpl_breakdown_mgmt_details',$data5);
		
		
		 
		}
      
      $sql_check=DB_query("select * from tpl_breakdown_mgmt_details where master_id='".$_REQUEST['rid2']."'",$db);
      $numrow1=DB_num_rows($sql_check);
      $flag=0;
      if($numrow1>0)
      {
       while($row1=DB_fetch_array($sql_check))
       {
          if($row1['test_details']=='Y')
           $flag=$flag+1;
       }  
       
       if($numrow1==$flag)
       {
        
        $update=DB_query("update tpl_breakdown_mgmt_assig set test_status=1
                        where id='".$_REQUEST['rid2']."'",$db);
        
       }
       
      }
       echo "<script>location.href='$url_cancel'</script>";
      }
	
	
	
?>

<style type="text/css">
 .search {
width:auto;
padding-top:3px;
font-size:12px;
font-weight:bold;
background-color:#ddd4d4;
background-repeat: repeat-x;
padding-bottom:3px;
text-align:center;
border:#333333 solid 1px;
-moz-border-radius:3px;
/*-moz-box-shadow: 1px 2px 2px -1px #858585;*/

 }
 
 /* some styling for the page */
        body { font-size: 10px; /* for the widget natural size */ }
        #content { font-size: 1.2em; /* for the rest of the page to show at a normal size */
                   font-family: "Lucida Sans Unicode", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif;
                   width: 950px; margin: auto;
        }
        .code { margin: 6px; padding: 9px; background-color: #fdf5ce; border: 1px solid #c77405; }
        fieldset { padding: 0.5em 2em }
        hr { margin: 0.5em 0; clear: both }
        a { cursor: pointer; }
        #requirements li { line-height: 1.6em; }
</style>

<link rel="stylesheet" href="js/jquery-ui-1.10.0.custom.min.css" type="text/css" />
    <link rel="stylesheet" href="js/jquery.ui.timepicker.css?v=0.3.2" type="text/css" />
<link rel="stylesheet" href="css/new_mxerp.css">
    <!--<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>-->
<script type="text/javascript">jQuery.noConflict();</script>
<script type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.tabs.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.timepicker.js?v=0.3.2"></script>
	
<!--<script src="ui/jquery.ui.timepicker.js"></script>-->
<!--<script type="text/javascript" language="javascript">
$('#timepicker1').timepicker();
</script>-->
        
<script>
function addRow(tableID) 
{ 
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {	
	
	var newcell = row.insertCell(i);
	//newcell.style.background='#e7e7e7';
	newcell.className='tabledata';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id_hidden = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id_chk = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
}
</script>

<script>
function showSubBreak(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)
    {
    document.getElementById("txtHint1").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getsubBreak.php?q="+str,true);
xmlhttp.send();
}
</script>

<div class="body_contain">
<div class="tab_div">
<h1>Add Process</h1>
<form method="post" name="tech_master">
<h3><strong>General Details</strong></h3>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right">Branch <font style="color:#FF0000;">*</font></td>
    <td width="20%"><select disabled id = "site" name="site" style="width:98%" >
        <option value = "">--Select--</option>
        <?php 
            $site_sql=DB_query("Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
                        while($site_row=DB_fetch_array($site_sql))
						{
						$p='';
						if($site_row['loccode']==$var['site'])
						$p="selected";
			?>
        <option value="<?php echo $site_row['loccode']?>"<?php echo $p;?>><?php echo $site_row['locationname'];?></option>
        <?php } ?>
      </select></td>
    <td width="13%" align="right"><strong>Asset Received Date <font style="color:#FF0000;">*</font></strong></td>
    <td width="20%"><input type="text" disabled style="width:98%;" name = "recieving_date" class="datepicker" value="<?php if($var['asset_rec_date_time']!='') echo date('d/m/y',strtotime($var['asset_rec_date_time'])); else echo "";?>"></td>
    <td width="13%" align="right">Job Card No. <font style="color:#FF0000;">*</font></td>
    <td><input type="text" value = "<?php echo $var['tt_no'];?>" disabled  name = "ttno" style="width:98%;"></td>
  </tr>
  <tr class="row1">
    <td align="right">Received From <font style="color:#FF0000;">*</font></td>
    <td><input type="text" disabled value = "<?php echo $var['rec_from']; ?>" name = "rec_from" style="width:98%;"></td>
    <td align="right">Received By <font style="color:#FF0000;">*</font></td>
    <td><input type="text" disabled id ="oo" value = "<?php echo $var['rec_by']; ?>" name = "received_by" style="width:98%;"></td>
    <td colspan="2"></td>
    </tr>
</table>

<br>
<p align="left">
<!--<input type="button" disabled name="profile_popup" value="Pick Machinery Items" id="profile_popup"  onclick="open_items()" accesskey="I" class="search" />-->

  </p>
<h3><strong>Asset Details</strong></h3>
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid; border-collapse:collapse;">
  <tr>
    <th width="10%">Asset Code</th>
    <th width="16%">Asset Name</th>
    <th width="16%">Asset Group</th>    
    <th width="12%">Unit</th>
    <?php 
   $title='';
   $title1=DB_query("select * from tbl_title_master order by serial_no ASC limit 0,3",$db);
   while($r=DB_fetch_array($title1))
   { 
    $title[]=$r['db_field_name'];
    ?>
    <th><strong><?php echo $r['title_name'];?></strong></th>
    <?php } ?>
  </tr>

  <tr bgcolor="#fff"><input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
<input type="hidden" name="" value="" id="edit_cnt" />
<input type="hidden" name="item_code" id="itemid" />
    <td><input type="text" disabled  value= "<?php echo $assetRows['code']; ?>" name="asset_code" id="assetcode"  style="width:98%;" readonly /></td>
    <td><input type="text" value= "<?php echo $assetRows['name']; ?>" name="Input" id="assetname"  style="width:98%;" readonly="" /></td>
    <td><input type="text" value = "<?php  echo $assetRow1['machine_gp_location'];?>" name="" id="asset_group" style="width:98%;" readonly="" /></td>    
    <td><input type="text" value = "<?php echo $myrow_unit['unitname']; ?>" name="" id="asset_unit"  style="width:98%;" readonly="" /></td>
    <td><input type="text" value= "<?php echo $assetRows[$title[0]]; ?>" name="" id="asset_model"  style="width:98%;" readonly="" /></td>
    <td><input type="text" value= "<?php echo $assetRows[$title[1]]; ?>" id = "asset_vechile" style="width:98%;" readonly="" ></td>
    <td><input type="text" value= "<?php echo $assetRows[$title[2]]; ?>" id = "asset_meter" style="width:98%;" readonly="" ></td>
  </tr>
</table>
<br>
<!--<p align="left">
  <input name="button" disabled type="button" class="search" align = "left" accesskey="A" onClick="addRow('dataTable')" value="Add Rows" />
  </p>-->
 
<h3><strong>Add Breakdown Details</strong></h3>
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid; border-collapse:collapse;">
  <tr>
    <th width="10%">Breakdown Type <font style="color:#FF0000;">*</font></th>
    <th width="10%">Breakdown Sub Type</th>
    <th width="10%">Breakdown Reason <font style="color:#FF0000;">*</font></th>
    <th>Technician Name</th>
    <th width="10%">Safety Details</th>
    <th width="8%">Received date & Time</th>
    <th width="6%">Exp.Com. Date</th>
    <th width="6%">Com. Date</th>
    <th width="7%">Current Status</th>
    <th width="15%">Remarks</th>
    <th width="6%">Testing Date</th>
    <th colspan="2">Testing Status</th>
  </tr>
  <?php 
  if($_REQUEST['action']=='addProdress')
{
  // Breakdown Details
  $h=0;
	$breakSql = "SELECT * FROM tpl_breakdown_mgmt_details WHERE master_id= ".$_REQUEST['rid2']."";
	$resultBd = DB_query($breakSql,$db);
	while($rowBd=DB_fetch_array($resultBd))
	{
	$h++;
	
   ?>
    <tbody id="dataTable">
  <tr bgcolor="#fff">
       <td><select name="sub_break_down1[]" readonly id = "ab" style="width:98%" onChange="showSubBreak(this.value)">
         <option value = "" >-- Select--</option>
         <?php 
            $site_sql1=DB_query("Select id,breakdown_reason from break_down_type where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
                        while($site_row1=DB_fetch_array($site_sql1)){
						$s='';
						if($site_row1['id']==$rowBd['break_type'])
						$s="selected";
			?> <div style="width:1200px; overflow:auto">
         <option value="<?php echo $site_row1['id']; ?>"<?php echo $s;?>><?php echo $site_row1['breakdown_reason']; ?></option>
         <?php } ?>
       </select></td>
       <td><div id= "txtHint1"><select name="sub_break_down[]" readonly style="width:98%;"> 
         <option value="">--Select--</option>
         <?php
	$id_query1=DB_query("Select * from tbl_sub_break_type where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
	while($id_row_type=DB_fetch_array($id_query1))
	 {
	 $q='';
	if($id_row_type['id']==$rowBd['break_sub_type'])
	$q="selected";
	 ?>
         <option value="<?php echo $id_row_type['id']; ?>"<?php echo $q;?>><?php echo $id_row_type['sub_breaktype']; ?></option>
         <?php } ?>
       </select></div></td>
       <td><input type="text" value = "<?php echo $rowBd['break_reason'];  ?>"  readonly id = "res" name = "break_reason[]" style="width:98%;"></td>
    <td>	<select name="technician[]" readonly style="width:98%;">
	<option value="0">--Select--</option>
	<?php
	$id_query2=DB_query("Select * from technicians", $db);
	while($id_row_technician=DB_fetch_array($id_query2))
	 {
	  $g='';
	if($id_row_technician['id']==$rowBd['technician'])
	$g="selected";
	 ?>
	<option value="<?php echo $id_row_technician['id']; ?>"<?php echo $g;?>><?php echo $id_row_technician['technician']; ?></option>
	<?php } ?>
	</select></td>
     <td>	<select name="break_safty[]" style="width:98%;">
	<option value="0">--Select--</option>
	<?php
	$sql2=DB_query("Select * from prd_safty where CompanyId='".$_SESSION['ACGCompanyId']."'", $db);
	while($row_safty=DB_fetch_array($sql2))
	 {
	  $g1='';
	if($row_safty['id']==$rowBd['safty'])
	$g1="selected";
	 ?>
	<option value="<?php echo $row_safty['id']; ?>"<?php echo $g1;?>><?php echo $row_safty['description']; ?></option>
	<?php } ?>
	</select></td>
    <td><input type="text" readonly value="<?php if($rowBd['rec_date']!='') echo date('d/m/y',strtotime($rowBd['rec_date'])); else echo "";?>" name = "rec_date[]" style="width:64%; float:left; margin-right:3px;" id= "<?php echo $r; ?>" class="datepicker"/><input type="text" value="<?php if($rowBd['rec_time']!='') echo $rowBd['rec_time']; else echo "";?>" id="timepicker.[1]" name = "rec_time[]" style="width:30%; float:left;" /></td>
    <td><input type="text" value="<?php if($rowBd['com_date']!='') echo date('d/m/y',strtotime($rowBd['com_date'])); else echo "";?>" style="width:98%;" readonly name = "com_date[]" id= "<?php echo $r; ?>" /></td>
	 <td><input type="text" value="<?php if($rowBd['completed_date']!='') echo date('d/m/y',strtotime($rowBd['completed_date'])); else echo "";?>" style="width:98%;" readonly name = "completed_date[]" class="datepicker" /></td>
	<td>
	<select name = "p_status[]" style="width:98%;">
  <option value="">--Select--</option>
  <option value="WIP"<?php echo $rowBd['progress_status'] == 'WIP' ? ' selected="selected"' : '';?>>WIP</option>
  <option value="Done"<?php echo $rowBd['progress_status'] == 'Done' ? ' selected="selected"' : '';?>>Done</option>
</select>
	</td>
	<td>
	<textarea name = "p_Remarks[]" rows="2" cols="15" style="width:98%; height:60px;"><?php if($rowBd['remarks']!="") { echo $rowBd['remarks']; } else { echo ''; } ?></textarea>
	</td>
    <td><input type="text"  value="<?php if($rowBd['completed_date']!='') echo date('d/m/y',strtotime($rowBd['completed_date'])); else echo "";?>" style="width:98%;" readonly name = "completed_date[]"  class="datepicker" /></td>
	<td width="6%">
	<select name = "test_details[]" style="width:98%;">
  <option value="">--Select--</option>
  <option value="Y"<?php echo $rowBd['test_details'] == 'Y' ? ' selected="selected"' : '';?>>Ok</option>
  <option value="N"<?php echo $rowBd['test_details'] == 'N' ? ' selected="selected"' : '';?>>Not Ok</option>
</select>
	</td>
    <td width="6%"><input type="text" value="<?php if($rowBd['tested_date']!='')
     echo date('d/m/y',strtotime($rowBd['tested_date'])); else echo "";?>" style="width:98%;" readonly name = "tested_date[]"  class="datepicker"></td>
  </tr>
 </tbody>
 <?php }
 } ?>
  <tr bgcolor="#fff">
    <td colspan="13" align="center" valign="top"><p align="center">
      <input name="addProdress" class="search" type="submit" onClick="return validates()" value="Save" />
      &nbsp;
      <a href="<?php echo $url_cancel;?>">
      <input name="" class="search" value="Cancel" type="button"></a></p></td>
  </tr>
</table>

</form>
 </div>
</div>
<script type="text/javascript">
function validates() {

	if(document.tech_master.site.value=='')
	{
		alert("Please Select Branch First");
		(document.tech_master.site).focus();
		return false;
	}
	else if(document.tech_master.recieving_date.value=='')
	{
		alert("Please Enter Asset Receiving Date& Time");
		(document.tech_master.recieving_date).focus();
		return false;
	}
	else if(document.tech_master.ttno.value=='')
	{
		alert("Please Enter TT No");
		(document.tech_master.ttno).focus();
		return false;
	}
	else if(document.tech_master.rec_from.value=='')
	{
		alert("Please Enter Received  From Value");
		(document.tech_master.rec_from).focus();
		return false;
	}
	else if(document.tech_master.received_by.value=='')
	{
			alert("Please Enter Received by Value");
		(document.tech_master.received_by).focus();
		return false;
	}
		else if(document.getElementById("assetcode").value =='')
	{
		alert("Select at least 1 Asset");
		return false;
	}
	else if(document.getElementById("ab").value=='')
	{
		alert("Please Enter Breakdown Type");
		(document.getElementById("ab")).focus();
		return false;
	}
	else if(document.getElementById("res").value=='')
	{
		alert("Please Enter Breakdown Reason");
		(document.getElementById("res")).focus();
		return false;
	}
	
}
</script>

<script language="JavaScript">
function open_items()
{ 

window.open("fixedAssetItems.php?&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>","itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}

jQuery.noConflict();	
	jQuery(document).ready(function(){	
	jQuery("#rec_dateid,#rec_dateid_1,#rec_dateid_2,#rec_dateid_3").datepicker({dateFormat: 'dd-mm-yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
	});

</script>
</div>
</div>
