<?php 
ob_start();
session_name('CAKEPHP'); session_start();
//echo $rurl;
 if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}	unset($_SESSION['success_msg']);


if($_SESSION['UserType']=='SuperAdmin')
{
	$sqlchkprhead=DB_query("select * from dept_indet_approval where ApprovalType='POApproval'",$db);
}	
else
{
	$sqlchkprhead=DB_query("select * from dept_indet_approval where ApprovalType='POApproval' and find_in_set('".$_SESSION['UserID']."',userid)",$db);
}

$Numrows=DB_num_rows($sqlchkprhead);


if($_REQUEST['Save']){

	for($i=0;$i<count($_REQUEST['purchid']);$i++)
	{
		if($_REQUEST['approve'][$i]==1){
		
		$data['Purchase_Approval_status']=1;
		$data['Approved_by'] = $_SESSION['UserID'];
		$cond="id='".$_REQUEST['purchid'][$i]."'";
		$row= $s->RecordEdit('purchorders',$data,$cond);
		}
	}
	$_SESSION['success_msg']="Approved Successfully";
	$s->pageLocation("index.php?route=POModule/purchase_head_order_approval".$rurl);
}

 ?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Purchase Orders Approval</h1>
<div class="centre" id="slidingDiv">
<form method="post" action="">	
<table width="100%" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1"><strong>Branch</strong></td>
	<td><select style="width:150px;" name="Site" id="Site">
			<?php
			$SiteName=DB_query("Select * from locations where mst_companyid='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
			while($SiteRow=DB_fetch_array($SiteName))
			{ ?>
			<option value="<?php echo $SiteRow['loccode']?>"<?php echo($_REQUEST['Site']==$SiteRow['loccode']?'selected':'');?>><?php echo $SiteRow['locationname']?></option>	
				
		<?php } ?>
		</select>
	</td>
	<!--<td class="tabledata1"><strong>Department</strong></td>
	<td><select style="width:150px;" name="deptId">
			<?php $DeptName=DB_query("SELECT * FROM deptmaster where deptname='STORE' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			while($DeptIdRow=DB_fetch_array($DeptName))
			{ ?>
			<option value="<?php echo $DeptIdRow['id']?>"<?php echo($_REQUEST['deptId']==$DeptIdRow['id']?'selected':'');?>><?php echo $DeptIdRow['deptname']?></option>	
				
		<?php } ?>
		</select>
	</td>-->
	<td class="tabledata1"><strong>From Date</strong></td>
	<td><input type="text" name="fdate" id="fdate" style="width:150px;"  class="datepicker" value="<?php echo $_REQUEST['fdate']?>"/></td>
	<td class="tabledata1"><strong>To Date</strong></td>
	<td><input type="text" name="todate" id="todate" style="width:150px;" class="datepicker"  value="<?php echo $_REQUEST['todate']?>"  /></td>
	
	
</tr>
<tr class="row1">
  <td colspan="8" style="text-align:center;"><input name="Go" type="submit" value="Go" onclick="return frmvalidate()" class="search" /></td>
</tr>
</table>
</form>
<?php 

if($Numrows>0)
{
if($_REQUEST['Go']){
if($_REQUEST['Site']!='')
{
	$filter[]="site='".$_REQUEST['Site']."'";
}
if($_REQUEST['fdate']!='' && $_REQUEST['todate']!='')
{
	$filter[]="po_date between '".FormatDateFORSQL($_REQUEST['fdate'])."' and '".FormatDateFORSQL($_REQUEST['todate'])."'";
}
if($filter!='')
{
	$filter1=implode(" and ",$filter);
	echo "SELECT * FROM purchorders where Purchase_Approval_status=0 and $filter1";
	$sqlPoApp=DB_query("SELECT * FROM purchorders where Purchase_Approval_status=0 and $filter1",$db);
        
}
?>
<form method="post">
<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border-collapse:collapse; border:#999999 solid 1px;">
<tr align="center" bgcolor="#d4d4d4">
	<td class="tabledata"><strong>Sr No.</strong></td>
	<td class="tabledata"><strong>PO No</strong></td>
	<td class="tabledata"><strong>PO Date</strong></td>
	<td class="tabledata"><strong>Vendor</strong></td>
	<td class="tabledata"><strong>Valid Till</strong></td>
	<td class="tabledata"><strong>PO Type</strong></td>
	<td class="tabledata"><strong>Approve</strong></td>
</tr>
<?php 
$i=0; 
if(DB_num_rows($sqlPoApp)>0){
while($PoApp=DB_fetch_array($sqlPoApp)) {?>
<tr bgcolor="#c1c1c1">
	<td><?php echo $i+1; ?></td>
	<td><a href="index.php?route=POModule/view_purchase_order_edit<?php echo $rurl; ?>&id=<?php echo $PoApp['id'] ?>&action=edit&modify=viewonly"><?php echo $PoApp['purchase_ordNo']; ?></a></td>
	<td><?php echo ConvertSQLDate($PoApp['po_date']); ?></td>
	<td><?php echo $s->GetFieldValue('suppliers',"id='".$PoApp['vendorId']."'",'vendor_name'); ?></td>
	<td><?php echo ConvertSQLDate($PoApp['valid_till']); ?></td>
	<td><?php echo $PoApp['purchase_ord_Type']; ?></td>
	<td>
	<input type="hidden" name="purchid[<?php echo $i; ?>]" value="<?php echo $PoApp['id'] ?>">
	<input type="checkbox" name="approve[<?php echo $i; ?>]" value="1" id="approve"></td>
</tr>
<?php $i++;}?>
<tr class="row1">
<td colspan="7"><p align="center"><input type="submit" name="Save" value="Save"  class="search" ></p></td>
</tr>
<?php  }else{?>
<tr class="row1">
	<td colspan="7" style="color:#FF0000;">No Record Found</td>
</tr>
<?php  }?>
</table>
</form>
<?php  }

}else{
?>
<p align="center"><font style="font-size:12px; color:#FF0000;"><strong>You Have Not Permission To Approve The Purchase Orders</strong></font></p>
<?php }?>
</div>
</div>
</div>
