<link rel="stylesheet" href="css/new_mxerp.css">
    <script type="text/javascript">
    $(document).ready(function(){
        $('input[name="to"]').click(function(){
            if($(this).val()=="received"){
                $(".issue").css('display','none');
                $(".received").css('display','block');
                $("#issue_to").attr('disabled','disabled');
                $("#received_by").removeAttr('disabled');
            }
            if($(this).val()=="issue"){
                $(".issue").css('display','block');
                $(".received").css('display','none');
                $("#received_by").attr('disabled','disabled');
                $("#issue_to").removeAttr('disabled');
            }
           });
       $('#search_form').submit(function(){
        	
        	if($('#project').val()=='')
        	{
        		alert("Please Select Site")
        		return false;
        	}
        });

    });
</script>

<style type="text/css">
#amc-tab{}
  #amc-tab table{border:none; border-collapse:collapse; }
  #amc-tab table tr td{border-right:#808080 1px solid; border-bottom:#808080 1px solid;}
  #amc-tab table tr td:last-child{border-right:none;}
  #amc-tab table tr:last-child td{border-bottom:none;}
  #amc-tab table tr td > table tr td{border-right:#808080 1px solid; border-bottom:#808080 1px solid;}
  #amc-tab table tr td > table tr td:last-child{border-right:none;}
  #amc-tab table tr td > table tr:last-child td{border-bottom:none;}
</style>
<div class="body_contain">
<div class="tab_div">
<h1>Asset Return Register</h1>
<form name="" id="search_form" method="POST">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
	<td width="13%" align="right"><strong>Branch:</strong></td>         
    <td width="20%">
    	<select name="project" id="project" class="detailedViewTextBox" style="width:98%;">
          <option value="">Select Branch</option>
          <?php
		    $query4=DB_query("Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			while($row4=DB_fetch_array($query4))
				{ ?>
					<option value=<?php echo $row4['loccode'];?> <?php if($_REQUEST['project']==$row4['loccode']){ echo 'selected="selected"';} ?>><?php echo $row4['locationname'];?></option>
		  <?php } ?>
        </select> 
    </td>
    <td width="13%" style="text-align:right;"><strong>Employee:</strong>
        <!-- <label><input type="radio" name="to" <?php  if($_REQUEST['to']=="received") echo "checked"; else echo "checked";  ?> value="received"><strong>Employeer</strong></label>
        <label><input type="radio" name="to" <?php  if($_REQUEST['to']=="issue") echo "checked";  ?> value="issue"><strong>Contractor</strong></label> -->
    </td>
	<?php
	//$g1="style='display:block'";
	//$g2=" style='display:none'";
	 ?>
    <td width="20%" class="received">
    	<select name="received_by" id="received_by" style="width:98%;">
          <option value="">Select Employee</option>
          <?php
		  $query2=DB_query("Select * from empmaster where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			while($row2=DB_fetch_array($query2))
				{?>
					<option value="<?php echo $row2['id'];?>" <?php if($_REQUEST['received_by']==$row2['id']){ echo 'selected="selected"';}?>><?php echo $row2['emp_name'];?></option>
		  <?php } ?>
        </select>
    </td>   
    <td width="13%" align="right"><strong>Asset:</strong></td>
    <td>
    	<select name="asset_name"  id="asset_name" class="detailedViewTextBox" style="width:98%;">
          <option value="">All</option>
    <?php $SQLMachine=db_query("SELECT * FROM machine_center where CompanyId='".$_SESSION['ACGCompanyId']."'",$db );
		 while($MachineNa=db_fetch_array($SQLMachine))
		   {?>
		 	<option value="<?php echo $MachineNa['id']?>" <?php if($MachineNa['id']==$_REQUEST['asset_name']) { echo 'selected="selected"'; } ?>><?php echo $MachineNa['name']; ?></option>
	 <?php } ?>
        </select>
    </td>
</tr>
<tr class="row1">
	<td align="right"><strong>From Date:</strong></td>
    <td><input type="text" name="frmdate" id="frmdate" style="width:98%;" class="datepicker" value="<?php echo $_REQUEST['frmdate'] ?>" /></td>
    <td align="right"><strong>To Date:</strong></td>
    <td><input type="text" name="todate" id="todate" style="width:98%;" value="<?php echo $_REQUEST['todate'] ?>" class="datepicker" /></td>
    <td align="right"><strong>Return No.:</strong></td>
    <td><input type="text" name="returnno" list="return" id="returnno" style="width:98%;" value="" />
    	<datalist id="return">
    		<?php 
    		$return_query=DB_query("select * from asset_return",$db); 
    		while($return_result=DB_fetch_assoc($return_query)) {
    		?>
    		<option value="<?php echo $return_result['return_slipno'] ?>"></option>
    		<?php } ?>
    	</datalist>
    </td>
</tr>
<tr class="row1">
	<td  colspan="6" style="text-align:center;">
	<input type="submit" name="Search" class="search" value="Search" /> 
	<?php if(isset($_REQUEST['Search'])){?><a href="asset_return_stock_print.php" target="_blank"><input type="button" name="print" value="Print" class="search"></a><?php } ?></td>
</tr>
</table>
<br>
	<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid; border-collapse:collapse;">
  	<tr>
  		<th width="3%">S. No.</th>
  		<th width="6%">Return Date</th>
  		<th width="10%">Return No.</th>
  		<th width="12%">Return From</th>
	  	<th width="110">Return Code</th>
	    <th width="300">Asset Name</th>
	   	<th width="60">Unit</th>
	   	<th>Remarks</th>
   </tr>
<?php
 if(isset($_REQUEST['Search']))
   {
        $fordate   = $_POST['frmdate'];		
	    $todate    = $_POST['todate'];		
	    $fromdate  = explode("/", $fordate);
		$daydate   = $fromdate['0'];
		$Monthdate = $fromdate['1'];
		$yeardate  = $fromdate['2'];
		$fdate     = $yeardate."-".$Monthdate."-".$daydate;							   
		$tredate   = explode("/", $todate);
		$dayda     = $tredate['0'];
		$Monthda   = $tredate['1'];
		$yearda    = $tredate['2'];
		$toyrdate  = $yearda."-".$Monthda."-".$dayda;	
	    $filter    = array();

	if($_REQUEST['project']!= '')	
		$filter[]="a.site='".$_REQUEST['project']."'";
    if($_REQUEST['received_by']!='')
    { 
    	$filter[]="a.receivefrom='".$_REQUEST['received_by']."'";
    }
     elseif($_REQUEST['issue_to']!='') 
     { 
      $filter[]="a.issueto='".$_REQUEST['issue_to']."'";
     }
	
	if($_REQUEST['todate']!='' && $_REQUEST['frmdate']!= '') 
		$filter[]="a.returndate  between '".$fdate."' and '".$toyrdate."'";
   
    if($_REQUEST['returnno']!='')	
		$filter[]="a.return_slipno like '%".$_REQUEST['returnno']."%'";

	if($filter!='')
	{			
	$filter1 = implode(" and ",$filter);
       //print_r($filter);	
	}
	if($filter1!='' && $_REQUEST['asset_name']=='')
	{
	$search_query="SELECT a.* FROM asset_return a, asset_return_details b WHERE a.id=b.parentId and $filter1"." group by a.id";
	}
	else if($filter1!='' && $_REQUEST['asset_name']!= '')
	{
	$search_query="SELECT a.* FROM asset_return a, asset_return_details b ,asset_issue_details c WHERE b.parentId=a.id AND b.asset_issue_detail_id=c.id AND c.mst_itemid='".$_REQUEST['asset_name']."' and $filter1"." group by a.id";
	}
	else
	{
		 $search_query="SELECT a.* FROM asset_return a, asset_return_details b WHERE a.id=b.parentId"." group by a.id";
	}	

$_SESSION['return_report_sql'] = $search_query;	
$sql=DB_query($search_query,$db);
$i=1;
$num=DB_num_rows($sql);
if($num>0)
{
while($row=DB_fetch_array($sql))
	{ ?>
<tr bgcolor="#fff">
	    <td><?php echo $i; ?></td>
	    <td><?php echo ConvertSQLDate($row['returndate']); ?></td>
	    <td><?php echo $row['return_slipno']; ?></td>
	    <td>
	   <?php if($row['issueto']=='0'){ $issue_name=DB_fetch_array(DB_query("select emp_name from empmaster where id='".$row['receivefrom']."'",$db)); echo $issue_name['emp_name'];  } 
	    else{ $issue_name=DB_fetch_array(DB_query("select vendor_name from suppliers where id='".$row['issueto']."'",$db)); //echo $issue_name['emp_name']; 
	    echo $issue_name['vendor_name']; } ?>
	    </td>
	    <td colspan="5" style="padding:0px;">
	      <div id="amc-tab">
	    	<table width="100%" cellpadding="3" cellspacing="0" border="0">
	    		<?php $detail_que=DB_query("select * from asset_return_details where parentId='".$row['id']."'",$db);
	    		while($detail_res=DB_fetch_assoc($detail_que))
	    		{
	    		$det1=DB_fetch_assoc(DB_query("select * from asset_issue_details where id='".$detail_res['asset_issue_detail_id']."'",$db));
	    		$asset_detail=DB_fetch_assoc(DB_query("select * from machine_center where id='".$det1['mst_itemid']."'",$db));
	    		$unit=DB_fetch_assoc(DB_query("select * from unitsofmeasure where unitid='".$asset_detail['unit']."'",$db));
	    		?>
	    		<tr>
	    			<td width="110"><?php echo $asset_detail['code'] ?></td>
	    			<td width="300"><?php echo $asset_detail['name'] ?></td>
	    			<td width="60"><?php echo $unit['unitname'] ?></td>
	    			<!--<td style="border-right:#808080 1px solid;" width="68"><?php //echo ConvertSQLDate($det1['return_date']) ?></td>-->
	    			<td><?php echo $detail_res['remarks'] ?></td>
	    		</tr>
	    		<?php } ?>
	    	</table>
	    </td>
</tr>

<?php $i++; }} else { ?>
<tr bgcolor="#fff">
  <td colspan="11" style="text-align: center;"><font color="#FF0000" style="font-weight:bold;"><?php echo "No Records Found"; ?></font></td>
</tr>	 
	<?php } } ?> 
</table>
</form>
</div></div>
<script type="text/javascript">
	$('.datepicker').removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
</script>