<script type="text/javascript">
  function checkapproval()
  {
    var noItems=$("input[name^='approve']:checkbox:checked").length;
    if(noItems==0)
    {
      alert("Please select Record");
      return false;
    }
  }
</script>

<?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']);
function checkPO($order_id)
{
    global $db;
    $flag1=0;
    $grandTotal1=DB_query("SELECT GrandTotal FROM purchorders WHERE id='".$order_id."'",$db);
    $grandTot=DB_fetch_array($grandTotal1);
    $sqlchkk=DB_query("SELECT A.seq_no,B.* FROM conf_purch_app_details AS A INNER JOIN conf_purch_app_po_range AS B ON A.id=B.purch_app_dtls_id WHERE A.purch_approval_id='".$_REQUEST['pid']."'",$db);
   while($recchkk=DB_fetch_array($sqlchkk)){
   if($grandTot['GrandTotal'] >=$recchkk['range_fm'] &&  $grandTot['GrandTotal']<=$recchkk['range_to']){
       $seqArr[]=$recchkk['seq_no'];
   }
   }
   $flag2=count($seqArr);
   foreach($seqArr AS $key=>$val){
     $sqlApp=DB_query("SELECT id FROM po_head_approval WHERE sr_no='".$seqArr[$key]."'",$db);
     $recApp=DB_fetch_array($sqlApp);
     if($recApp){
         $flag1++;
     }
   }
   if($flag1==$flag2){
         $sqlup=DB_query("UPDATE purchorders SET final_app_status='1' WHERE id='".$order_id."'",$db);
   }
   
}
if($_REQUEST['Save'])
{ for($i=0;$i<=count($_REQUEST['purchid']);$i++)
	{
    if($_REQUEST['approve'][$i]==1){
        $data['order_no']=$_REQUEST['purchid'][$i];
	$data['approval_status']=1;
        $data['sr_no']=$_REQUEST['hidsr_no'];
        $data['approved_by']=$_SESSION['UserID'];
	$s->RecordInsert('po_head_approval',$data);	
        checkPO($_REQUEST['purchid'][$i]);
    }  
    }
    $_SESSION['success_msg']="Approved Successfully";
}
  $SQLex=DB_query("select * from conf_purch_app_details where id='".$_REQUEST['id']."'",$db);
  $record=DB_fetch_array($SQLex);
  $SQLrange=DB_query("select range_fm,range_to from conf_purch_app_po_range where purch_app_dtls_id='".$_REQUEST['id']."'",$db);
  $rangeRecord=DB_fetch_array($SQLrange);
  $min=$rangeRecord['range_fm'];
  $max=$rangeRecord['range_to'];
 ?>

<!-- <h3><?php //echo $record['approval_name']?></h3> -->
<div class="centre" id="slidingDiv">
<form method="post" action="">	
<table width="100%" align="center" cellpadding="3" cellspacing="0" border="0">
  <tr class="row1">
  	<td width="13%" align="right">Branch</td>
  	<td width="20%"><select 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">Department</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 width="13%" align="right">From Date</td>
  	<td width="20%"><input type="text" name="fdate" class="datepicker" id="fromdate" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" onchange="javascript:checkdate(this.value)"/></td>
  	<td width="13%" align="right">To Date</td>
  	<td><input type="text" name="todate" class="datepicker" id="todate" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>"  onchange="javascript:checkdate(this.value)"  /> <input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" id='from'> <input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" id='to'></td>
  </tr>
  <tr class="row1">
    <td colspan="6" style="text-align:center;"><input type="hidden" name="hidsr_no" id="hidsr_no" value="<?php echo $record['seq_no']; ?>" /> <input name="Go" type="submit" value="Search" onclick="return frmvalidate()" class="search" /></td>
  </tr>
</table>
</form>
<br>
<?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);
       if($min==0 && ($max=='Max' || $max=='MAX' || $max=='max') )
        {
          $sqlPoApp=DB_query("SELECT * FROM purchorders WHERE $filter1",$db);
        }
        else if($min!=0 && ($max=='Max' || $max=='MAX' || $max=='max'))
        {
          $sqlPoApp=DB_query("SELECT * FROM purchorders WHERE $filter1 AND GrandTotal >='".$min."' and cancel_status='0' ",$db);
        }
        else
        {     
          $sqlPoApp=DB_query("SELECT * FROM purchorders WHERE $filter1 AND (GrandTotal BETWEEN '".$min."' AND '".$max."') and cancel_status='0' ",$db);
        }
    }
//echo "SELECT * FROM purchorders WHERE $filter1 AND (GrandTotal BETWEEN '".$min."' AND '".$max."')";
?>
<form method="post">
<table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
<tr>
  <th width="4%">S. No.</th>
	<th width="12%">Created Date</th>
	<th width="12%">PO No.</th>
	<th width="8%">PO Date</th>
  <th width="12%">Quotations</th>
	<th>Vendor</th>
	<th width="8%">Valid Till</th>
	<th width="12%">PO Type</th>
	<th width="8%">Approve</th>
</tr>
<?php 
$i=0; $flag=0;$seqArr=array();
if(DB_num_rows($sqlPoApp)>0){
while($PoApp=DB_fetch_array($sqlPoApp)) {
    
$sqlqry=DB_query("select id from po_head_approval where order_no='".$PoApp['id']."' and approval_status ='1' 
  and sr_no='".$_REQUEST['hidsr_no']."'",$db);
if(DB_num_rows($sqlqry)==0 )
    {
    if($_REQUEST['hidsr_no']>1)
        {
             // $sr=$_REQUEST['hidsr_no']-1;
     // echo "SELECT B.* FROM conf_purch_app_details AS A INNER JOIN conf_purch_app_po_range AS B ON A.id=B.purch_app_dtls_id WHERE A.purch_approval_id='".$_REQUEST['pid']."'";
   $sqlchkprev=DB_query("SELECT A.seq_no,B.* FROM conf_purch_app_details AS A INNER JOIN conf_purch_app_po_range AS B ON A.id=B.purch_app_dtls_id WHERE A.purch_approval_id='".$_REQUEST['pid']."'",$db);
   while($recchkprev=DB_fetch_array($sqlchkprev)) {
   if($PoApp['GrandTotal'] >=$recchkprev['range_fm'] &&  $PoApp['GrandTotal']<=$recchkprev['range_to']){
       $seqArr[]=$recchkprev['seq_no'];
   }
   }
   //print_r($seqArr);
   foreach($seqArr AS $key=>$val){
  if($_REQUEST['hidsr_no']==$val)
      {
       $t=$key-1;
       $sr=$seqArr[$t];          
      }
   }   
  }
        else
        {
         $sr='';
        }  
             if($sr=='')
             { 
             $numrow2=1;
             }
             else 
             {  //echo "select id from po_head_approval where order_no='".$PoApp['id']."' and approval_status='1' and sr_no='".$sr."'";
             $sqlqry2=DB_query("select id from po_head_approval where order_no='".$PoApp['id']."' 
              and approval_status='1' and sr_no='".$sr."'",$db);
             $numrow2=DB_num_rows($sqlqry2);          
             }
             if($numrow2>0)
             {
                 //&& ($PoApp['GrandTotal'] >=$min ||  $PoApp['GrandTotal']<=$max) ?>
<tr>
	<td><?php echo $i+1; ?></td>
  <td><?php echo $PoApp['created_date']; ?></td>
	<td><a href="index.php?route=POModule/view_purchase_order_process_gst<?php echo $rurl; ?>&id=<?php echo $PoApp['id'] ?>&action=edit&modify=viewonly" target="_blank"><?php echo $PoApp['purchase_ordNo']; ?></a></td>
	<td><?php echo ConvertSQLDate($PoApp['po_date']); ?></td>
  <td><a href="index.php?route=POModule/Comperitive_new_gst_view<?php echo $rurl; ?>&id=<?php echo $PoApp['id'] ?>&action=edit&modify=viewonly" target="_blank"><?php echo $PoApp['purchase_ordNo']; ?></a></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++;$flag++;}}}?>
<?php if($flag==0){ echo "<tr><td colspan='9' align='center'>No Record Found</td>
</tr>"; }else{?>
<tr>
<td colspan="9"><input type="hidden" name="hidsr_no" id="hidsr_no" value="<?php echo $record['seq_no']; ?>" /><p align="center"><input type="submit" name="Save" value="Save"  class="search" onclick="return checkapproval()" ></p></td>
</tr>
<?php } }else{?>
<tr>
	<td colspan="9" align="center">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>

