<?php ob_start();
$PageSecurity = 2;
include('includes/session.inc');
$title = _('View/Search Material Receipt');
include('includes/header.inc');
$year = date('Y');
echo '<a href="'. $rootpath . '/index.php?' . SID . "identifier=".$identifier.'">'. _('Main Menu'). '>></a>';
//echo $year;
if($_REQUEST['action']=='delete' && isset($_GET['id']))
{
	$del_sql1="Delete From material_receipt_details where material_receipt_id='".$_GET['id']."' ";
	$del_res1=DB_query($del_sql1,$db);
	$del_sql="Delete From material_receipt where id='".$_GET['id']."' ";
	$del_res=DB_query($del_sql,$db);
	prnMsg( _('Material Issue Record Deleted!'),'success');
}

if($_REQUEST['Search'])
{
				   $fromdate = $_POST['from_voucher_date'];
				   $brk_fromdate = explode("/", $fromdate);
				   $f_day = $brk_fromdate['0'];
				   $f_month = $brk_fromdate['1'];
				   $f_year = $brk_fromdate['2'];
				   $fromdate_1 = $f_year."-".$f_month."-".$f_day;
					   
				   $todate = $_POST['to_voucher_date'];
				   $brk_todate   = explode("/", $todate);
				   $t_day     = $brk_todate['0'];
				   $t_month   = $brk_todate['1'];
				   $t_year    = $brk_todate['2'];
				   $todate_1  = $t_year."-".$t_month."-".$t_day;
	
	$filter=array();
	$query="Select * from material_receipt";
	
	if(isset($_POST['from_site']) && $_POST['from_site'] != '000')
	$filter[]="from_site='".$_POST['from_site']."'";
	if(isset($_POST['to_site']) && $_POST['to_site'] != '000')
	$filter[]="to_site='".$_POST['to_site']."'";
	if(isset($_POST['from_voucher_date']) && $_POST['from_voucher_date'] != '' && isset($_POST['to_voucher_date']) && $_POST['to_voucher_date'] != '')
	$filter[]="voucher_date BETWEEN '".$fromdate_1."' AND '".$todate_1."' ";
	if(isset($_POST['voucher_no']) && $_POST['voucher_no'] != '')
	$filter[]="voucher_no LIKE '%".$_POST['voucher_no']."%'";
	
	if($filter != '')
	{
		$query=$query." where ".implode(" and ",$filter);
	}
	//echo $query;
}
else
{
	$query="Select * from material_receipt order by receive_date DESC";
}

$result=DB_query($query,$db);
?><head>
<h2 align="center"><strong>Material Receipt</strong></h2></head>
<form action="" method="post">
<table width="51%" border="0" cellspacing="1" cellpadding="2" align="center">
  <tr>
    <td width="28%" scope="row"><strong>RECEIVE FROM</strong></td>
    <td width="30%"><select name="from_site" id="from_site">
      <option value="000">---Select Site---</option>
      <?php
	$site_query=DB_query("Select loccode,locationname from locations",$db);
	while($site_row=DB_fetch_array($site_query)) 
	{	$p='';
		if($_POST['from_site']==$site_row['loccode'])
		$p="selected";
	?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo $p;?>><?php echo $site_row['locationname'];?></option>
      <?php } ?>
    </select></td>
    <td width="8%"><strong>TO</strong> </td>
    <td width="34%"><select name="to_site" id="to_site">
      <option value="000">---Select Site---</option>
      <?php
	$site_query=DB_query("Select loccode,locationname from locations",$db);
	while($site_row=DB_fetch_array($site_query)) 
	{
		$p='';
		if($_POST['to_site']==$site_row['loccode'])
		$p="selected";
	?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo $p;?>><?php echo $site_row['locationname'];?></option>
      <?php } ?>
    </select></td>
  </tr>
  <tr>
    <td><strong>VOUCHER DATE</strong></td>
    <td><input type="text" name="from_voucher_date" id="from_voucher_date" class="datepicker" value="<?php if(isset($_POST['from_voucher_date'])) {echo $_POST['from_voucher_date'];} else {echo "01/04/".$year;}?>"/></td>
    <td><strong>TO</strong> </td>
    <td><input type="text" name="to_voucher_date" id="to_voucher_date" class="datepicker" value="<?php if(isset($_POST['to_voucher_date'])) {echo $_POST['to_voucher_date'];} else {echo "31/03/".(++$year);}?>"/></td>
  </tr>
  <tr>
    <td><strong>VOUCHER NO. </strong></td>
    <td><input type="text" name="voucher_no" id="voucher_no" value="<?php echo $_POST['voucher_no'];?>" /></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<p align="center"><input type="submit" name="Search" value="Search" /></p>
</form>
<br>
<h3 align="center"><a href="material_receipt.php">ADD NEW MATERIAL RECEIPT</a></h3>
<hr>
<br>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th><strong>VOUCHER NO. </strong></th>
	<th><strong>ISSUE NO. </strong></th>
    <th><strong>VOUCHER DATE </strong></th>
	<th><strong>ISSUE DATE </strong></th>
    <th><strong>FROM SITE </strong></th>
    <th><strong>TO SITE </strong></th>
    <th><strong>STORE</strong></th>
	<th colspan="3"><strong>ACTION</strong></th>
  </tr>
  <?php
  if(DB_num_rows($result)>0)
  {
  while($myrow=DB_fetch_array($result))
  {
  ?>
  <tr>
    <td><?php echo $myrow['receipt_voucher_code'];?></td>
	<td><?php echo $myrow['issue_voucher_no'];?></td>
    <td><?php echo ConvertSQLDate($myrow['receive_date']);?></td>
	<td><?php echo ConvertSQLDate($myrow['issue_date']);?></td>
    <td><?php 
	$site_sql1="Select locationname from locations where loccode='".$myrow['from_site']."'";
	$site_res1=DB_query($site_sql1,$db);
	$site_row1=DB_fetch_array($site_res1);
	echo $site_row1['locationname'];?></td>
    <td><?php 
	$site_sql2="Select locationname from locations where loccode='".$myrow['to_site']."'";
	$site_res2=DB_query($site_sql2,$db);
	$site_row2=DB_fetch_array($site_res2);
	echo $site_row2['locationname'];?></td>
    <td><?php 
	$store_sql="select distinct StoreName from storetype where StoreCode='0".$myrow['store']."' ";
	$store_res=DB_query($store_sql,$db);
	$store_row=DB_fetch_array($store_res);
	echo $store_row['StoreName'];?></td>
	<td width="4%"><a href="view_material_receipt.php?id=<?php echo $myrow['id']?>&action=view">View</a></td>
	<td width="3%"><a href="edit_material_receipt.php?id=<?php echo $myrow['id']?>&action=edit">Edit</a></td>
	<td width="6%"><a href="material_receipt_list.php?id=<?php echo $myrow['id']?>&action=delete" onclick="return confirm('Are You Sure You Want To Delete This Record?')">Delete</a></td>
  </tr>
  <?php } 
  }
  else
  {?>
  	<tr>
	<td colspan="9"><h3 align="center"><font color="#FF0000">No Record Found</font></h3></td>
	</tr>
<?php }?>
</table>
<?php 
include('includes/footer.inc');
?>