<?php 
$url_cancel= "index.php?route=ToolPlants/Transaction/asset_breakdown_management".$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_management".$rurl;
$msgErr=array();
//$_SESSION['manvi'];
?>

<?php
if($_REQUEST['Save'])
{
	$startYr = explode('-', $_SESSION['financialYrStart']);
	 $endYr = explode('-', $_SESSION['financialYrEnd']);
	  $startYr[0];
	  $endYr[0];

	  $financial=substr($startYr[0],-2)."-".substr($endYr[0],-2); 
	  $prefixcode=$s->getField('prefix_tbl','prefix_name',$_REQUEST['frmid'],'prefix_code');

      $sql="select tt_no from tpl_breakdown_mgmt where site = '".$_REQUEST['site']."'
     and INSTR(`tt_no`, '$financial') > 0 order by id DESC LIMIT 0,1";
	$result = DB_query($sql,$db);
	 $numrows = DB_num_rows($result);
	if($numrows>0)
	{
		$myrow=DB_Fetch_array($result);
     	$ExplBill=explode("/",$myrow['tt_no']);
		$maxval = $ExplBill[3];
	}
	else
	{$maxval=0;}	
	
		$strChar=$_REQUEST['site']."/".$prefixcode."/".$financial."/";
		 $strFinal = $maxval+1;
		$strBothFinal = $strChar.str_pad($strFinal,5,"0",STR_PAD_LEFT); 
	
	$data['site']=$_REQUEST['site'];
	$data['asset_rec_date_time'] = FormatDateFORSQL($_REQUEST['recieving_date']);
	$data['tt_no'] = $strBothFinal;
	$data['rec_from'] = $_REQUEST['rec_from'];
	$data['rec_by'] = $_REQUEST['received_by'];
	$data['CompanyId'] = $_SESSION['ACGCompanyId'];
	$data['created_by'] = $_SESSION['UserID'];
    $data['remarks']=$_REQUEST['remarks'];
	$data['maintenance_type'] =$_REQUEST['maintenance_type'];
	$data['maintance_date']=$_REQUEST['maint_date'];
	$data['date'] = date('Y-m-d');

	$s->RecordInsert('tpl_breakdown_mgmt',$data);
	$last_id=DB_Last_Insert_Id($db,"tpl_breakdown_mgmt","id");
   //$last_id=1;
	if($last_id !='')
	{ 
	$data1['asset_id'] = $_REQUEST['item_code'];
	$data1['master_id'] = $last_id;
	$s->RecordInsert('tpl_breakdown_mgmt_machine',$data1);
    $subbreakdown=explode(",",$_REQUEST['sub_breakdown_id']);
    for($i=0;$i<count($subbreakdown);$i++)
    {
    $data2['parent_id']=$last_id;
    $data2['sub_breakdown_id']=$subbreakdown[$i];
    $data2['breakdown_id']=$_REQUEST['breakdown'];
    $s->RecordInsert('tpl_breakdown_mgmt_machine_breakdown',$data2);
    }

    }

	//it;	
   $s->pageLocation("index.php?route=ToolPlants/Transaction/asset_breakdown_management".$rurl);
		}	

// upadating Record
if($_REQUEST['action']=='edit')
{   
	$con ="id='".$_REQUEST['rid']."'";
	$Get_id = $s->GetRecordById('tpl_breakdown_mgmt',$con);
	$var = DB_fetch_array($Get_id);
	 $maintenance_type=$var['maintenance_type'];
	
	// asset details
	 $assetSql = "SELECT * FROM tpl_breakdown_mgmt_machine WHERE master_id= ".$var['id']."";
	$resultAsset = DB_query($assetSql,$db);
	$rowAsset=DB_fetch_array($resultAsset);
	
	$sqlAset = "SELECT * FROM machine_center WHERE ID= ".$rowAsset['asset_id']."";
	$resultDisplay = DB_query($sqlAset,$db);
	$assetRows = DB_fetch_array($resultDisplay);
	// asset group
		
	// 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);
		 //  tt no.
}
	// update query    
	if(isset($_REQUEST['Update'])){
	$data3['site']=$_REQUEST['site'];
	$data3['asset_rec_date_time']=FormatDateFORSQL($_REQUEST['recieving_date']);
	$data3['remarks']=$_REQUEST['remarks'];
	$data3['rec_from']=$_REQUEST['rec_from'];
	$data3['rec_by']=$_REQUEST['received_by'];
	$data3['CompanyId'] = $_SESSION['ACGCompanyId'];
	$data3['created_by']=$_REQUEST['uid'];
   $data3['maintenance_type']=$_REQUEST['maintenance_type'];
	$data3['modified_by']=$_SESSION['UserID'];
	$data3['date'] = date('Y-m-d');

	$cond="id='".$_REQUEST['rid']."'";	
	
	$s->RecordEdit('tpl_breakdown_mgmt', $data3, $cond);
	
	$_REQUEST['asset_code'];
	$sqlBD = "SELECT id FROM machine_center WHERE code= '".$_REQUEST['asset_code']."'";
	$resultBD = DB_query($sqlBD,$db);
	$BDRows = DB_fetch_array($resultBD);
	
	$UPDATE = DB_query("UPDATE tpl_breakdown_mgmt_machine SET 
						asset_id = '".$BDRows['id']."',
						master_id = '".$_REQUEST['rid']."'
						where master_id = '".$_REQUEST['rid']."'",$db); 
		
			echo "<script>location.href='$url_cancel'</script>";
		}
  
  if($_REQUEST['action']=='add')
  {

   $sqlAset = "SELECT * FROM machine_center WHERE ID= ".$_REQUEST['id']."";
	$resultDisplay = DB_query($sqlAset,$db);
	$assetRows = DB_fetch_array($resultDisplay);
   $var['site']=$assetRows['location'];
   $var['rec_from']=$assetRows['loc'];  
   
   $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);   
  }
?>
<link rel="stylesheet" href="css/new_mxerp.css">

<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;
 }
#content { font-size: 1.2em;
           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" /> -->
    <!--<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>
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='#fff';
	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>

<form method="post" name="tech_master">
<div class="body_contain">
<div class="tab_div">
<h1>General Details</h1>
<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%"><div id= "txtHint">
      <select  id="site" name="site"  >
        <option value = "0">--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>
    </div></td>
    <td width="13%" align="right">Receiving Date <font style="color:#FF0000;">*</font></td>
    <td width="20%"><input type="text"  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 date('d/m/Y');?>"></td>
    <td width="13%" align="right">Job Card No. <font style="color:#FF0000;">*</font></td>
    <td><input type="text" value = "will be generated Automatically" readonly="" name = "ttno" ></td>
  </tr>
  <tr class="row1">
    <td align="right">Break Down Loc. <font style="color:#FF0000;">*</font></td>
    <td><input type="text" value = "<?php echo $var['rec_from']; ?>" name = "rec_from" ></td>
    <td align="right">Received By <font style="color:#FF0000;">*</font></td>
    <td><input type="text" id ="oo" value = "<?php echo $var['rec_by']; ?>" name = "received_by" ></td>
    <td align="right">Maintenance Type</td>
    <td><select name="maintenance_type" id="maintenance_type" >
    	<?php if($_REQUEST['action']=='add' && $_REQUEST['maintenance_type']=='Preventive') { ?>
    		<option value="Preventive">Preventive</option>	
    	<?php } else { ?>
      <option value="Break down" <?php if($maintenance_type=='Break down') { ?> selected="selected" <?php } ?>>Break down</option>
      <option value="Preventive"<?php if($maintenance_type=='Preventive') { ?> selected="selected" <?php } ?>>Preventive</option>
      <option value="Productive" <?php if($maintenance_type=='Productive') { ?> selected="selected" <?php } ?>>Productive</option>
      <?php }?>
     </select></td>
  </tr>
</table>
<p align="left">
<?php if($_REQUEST['action']!='add') { ?>
<input type="button" name="profile_popup" value="Pick Asset" id="profile_popup" onclick="open_items()" accesskey="I" class="search" />
<?php } ?>
</p>

<h3>Asset Details</h3>

<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <th width="10%">Asset Code</th>
    <th>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 width="12%"><?php echo $r['title_name'];?></th>
    <?php } ?>
  </tr>
  <tr>
	  <input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
	  <input type="hidden" name="" value="" id="edit_cnt" />
	  <input type="hidden" name="item_code" id="itemid" value="<?php echo $_REQUEST['id']; ?>" />
    <td><input type="text"  value= "<?php echo $assetRows['code']; ?>" name="asset_code" id="assetcode"   readonly /></td>
    <td align="left" valign="top"><input type="text" value= "<?php echo $assetRows['name']; ?>" name="Input" id="assetname"   readonly="" /></td>
    <td align="left" valign="top"><input type="text" value = "<?php  echo $s->getField('machine_group_master','id',$assetRows['under_group'],'machine_gp_location') ;?>" name="" id="asset_group"  readonly="" /></td>    
    <td align="left" valign="top"><input type="text" value = "<?php echo $myrow_unit['unitname']; ?>" name="" id="asset_unit"   readonly="" /></td>
    <td align="left" valign="top"><input type="text" value= "<?php echo $assetRows[$title[0]]; ?>" name="" id="title1"   readonly="" /></td>
    <td align="left" valign="top"><input type="text" value= "<?php echo $assetRows[$title[1]]; ?>" id = "title2"  readonly="" ></td>
    <td align="left" valign="top"><input type="text" value= "<?php echo $assetRows[$title[2]]; ?>" id = "title3"  readonly="" ></td>
  </tr>  
</table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
   <tr class="row1">
   <td width="13%">Break Down Descriptions</strong></td>
   <td><textarea name="remarks" id="remarks" style="width:99%; height:50px;"><?php echo $var['remarks'] ?></textarea></td>
</tr>  
</table>

   <p align="center">
      <input name="<?php if($_REQUEST['action']=='edit') { echo 'Update';}else { echo 'Save'; }?>" class="search" type="submit" onClick="return validates()" value="<?php if($_REQUEST['action']=='edit') { echo 'Update';}else { echo 'Save'; }?>" /> <a href="<?php echo $url_cancel;?>"><input name="" class="search" value="Cancel" type="button"></a></p>
</div>
</div>
<script type="text/javascript">
function validates()
 {

	if(document.tech_master.site.value=='0')
	{
		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 Break Down Loc 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=450,width=700,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>
