<?php 
 if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}	unset($_SESSION['success_msg']);
error_reporting(0);
$SQLex=DB_query("select * from conf_purch_app_details where id='".$_REQUEST['id']."'",$db);
$record=DB_fetch_array($SQLex);

//$ssql = DB_query("Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
//while($rrow=DB_fetch_array($ssql))
//{
//    $arrloc[]=$rrow['loccode'];
//}
//$imploc=implode("','",$arrloc);
?>
<script type="text/javascript" language="javascript">
function checkqty(i,str)
{
        var totlqty=parseFloat(document.getElementById("pending_qty_"+i).value);
	var Appqty=parseFloat(document.getElementById("app_qty_"+i).value);
        if(parseFloat(Appqty)>parseFloat(totlqty))
	{
            alert("Approval Qty is Not Greater Than Total Qty");
	document.getElementById("app_qty_"+i).focus();
	document.getElementById("app_qty_"+i).value=0;
        }
}
	// var courter=document.getElementById("count1").value;
	// var courter1=courter-1;
	// for(var i=1;i<=courter1;i++)
	// {
	// var totlqty=parseFloat(document.getElementById("pending_qty_"+i).value);
	// var Appqty=parseFloat(document.getElementById("app_qty_"+i).value);
	// if(Appqty>totlqty)
	// {
	// alert("Approval Qty is Not Greater Than Total Qty");
	// document.getElementById("app_qty_"+i).focus();
	// document.getElementById("app_qty_"+i).value=0;
	// }
	// }


function frmvalidate()
{
	if(document.getElementById("Site").value=='0')
	{
		alert("Please Select The Branch First !");
		document.getElementById("Site").focus()
		return false;
	}
	if(document.getElementById("fdate").value=='')
	{
		alert("Please Select The From Date First !");
		document.getElementById("fdate").focus()
		return false;
	}
	if(document.getElementById("todate").value=='')
	{
		alert("Please Select The To Date First !");
		document.getElementById("todate").focus()
		return false;
	}
}
function CheckApprovalBox()
{
	
    var txt;
	var count12=0;
 	
	jQuery('input[id^=Approval_]').each(function(){

		var id = this.id;
		var idk = id.split("_");
		//var ch = document.getElementById("ch_"+idk[1]).checked==true;
		
		if(document.getElementById("Approval_"+idk[1]).checked==true)
		{
			count12=count12+1;
		}
		else
		{
		   count12=count12;
		}
	}); 

	 if(count12>0)
	 {
	 	alert("Approved Successfully");
	 	return true;
     }
     else
     {
     	alert("Please Select at leastone checkbox");
     	return false;
     }

}
function checkapproveqty(i)
{
	if(document.getElementById("Approval_"+i).checked==true)
	{
		if(document.getElementById("app_qty_"+i).value=='' || document.getElementById("app_qty_"+i).value=='0')
		{
		document.getElementById("app_qty_"+i).value=document.getElementById("qty_"+i).value;
		}
	}
        else
            {
                document.getElementById("app_qty_"+i).value='';
            }
}
function showIndent(str)
{ 
var xmlhttp;
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("vender").innerHTML= xmlhttp.responseText;
    document.getElementById("UserIndentdep").innerHTML=xmlhttp.responseText;
	
    }
  }
xmlhttp.open("GET","check_data_fin.php?section=IndentUserDepHApp&site="+str,true);
xmlhttp.send();
}
</script>

<h1><?php echo $record['approval_name']?></h1>
<div class="centre" id="slidingDiv">
<form method="post" action="">	
<table width="100%" cellpadding="3" cellspacing="0">
	<tr class="row1">
		<td width="10%" align="right">Branch</td>
		<td width="15%"><select name="Site" id="Site" onchange="showIndent(this.value)">
				<?php
				$ss1 = "Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
			
				$SiteName=DB_query($ss1,$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='PURCHASE' 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="10%" align="right">From Date</td>
        <td width="15%"><input type="text" readonly name="fdate" id="fdate" class="datepicker" value="<?php if($_REQUEST['fdate']=='') { echo ConvertSQLDate($_SESSION['financialYrStart']);} else { echo $_REQUEST['fdate'];} ?>" /></td>
	    <td width="10%" align="right">To Date</td>
        <td width="15%"><input type="text" name="todate" readonly id="todate" class="datepicker" value="<?php if($_REQUEST['todate']=='') { echo date("d/m/Y"); } else { echo $_REQUEST['todate']; } ?>" /></td>
	    <td width="10%" align="right">Indent By</td>
	    <td><div id="UserIndentdep">
            <?php $EMPSQL=DB_query("SELECT * FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND emp_site IN ('".$_SESSION['AssignedBranch']."')",$db);?>
			<select name="user" id="user">
			<option value="0">All</option>
			<?php while($GetEmp=DB_fetch_array($EMPSQL)) {?>
			<option value="<?php echo $GetEmp['id']; ?>"<?php echo($_REQUEST['user']==$GetEmp['id']?'selected':''); ?>><?php echo $GetEmp['emp_name']; ?></option>
			<?php } ?>
			</select>
            </div>
		</td><input type="hidden" name="hidsr_no" id="hidsr_no" value="<?php echo $record['seq_no']; ?>" />
	</tr>
	<tr class="row1">
	  <td colspan="8" style="text-align:center;"><input name="Go" type="submit" value="Search" onclick="return frmvalidate()" class="search" /></td>
	</tr>
</table>
</form>
</div>

<?php
//echo $user_ids;
function checkPR($pr_id,$cid,$qty)
{    
    global $db;
    $flag=0; $flag1=0;
    //echo "SELECT no_of_assignee FROM conf_purch_approval WHERE doc_type='PR' AND company_id='".$_SESSION['ACGCompanyId']."'";

    $seq_no=DB_query("SELECT no_of_assignee FROM conf_purch_approval WHERE doc_type='PR' AND company_id='".$_SESSION['ACGCompanyId']."'",$db);
    $row_no=DB_fetch_array($seq_no);
     $sqlSub=DB_query("select id from requisitionorderdetails WHERE requsitionno='".$pr_id."'",$db);
     $num=DB_num_rows($sqlSub);
     while($rowSub=DB_fetch_array($sqlSub))
       {  //echo "SELECT approvalStatus FROM pr_head_approval WHERE requisition_id='".$pr_id."' AND requiNo='".$rowSub['id']."' AND sr_no='".$row_no['no_of_assignee']."'";

        $sqlChild=DB_query("SELECT approvalStatus,app_quantity FROM pr_head_approval WHERE requisition_id='".$pr_id."' AND requiNo='".$rowSub['id']."' AND sr_no='".$row_no['no_of_assignee']."'",$db);
        while($rowChild=DB_fetch_array($sqlChild))
	{
		
	if($rowChild['approvalStatus']=='3'){
	$flag=$flag+1;
    }  //echo "UPDATE requisitionorderdetails SET approval_status='".$rowChild['approvalStatus']."' WHERE requsitionno='".$pr_id."' AND id='".$rowSub['id']."'";
      // exit;
       $sqlstatupdate=DB_query("UPDATE requisitionorderdetails SET approval_status='".$rowChild['approvalStatus']."' WHERE requsitionno='".$pr_id."' AND id='".$rowSub['id']."'",$db);
       $sqlupdate=DB_query("UPDATE requisitionorderdetails SET App_quantity='".$rowChild['app_quantity']."' WHERE requsitionno='".$pr_id."' AND id='".$rowSub['id']."'",$db);
    }
   }
   if($flag>0 && $flag==$num){
        $sqlup=DB_query("UPDATE materialrequisitions SET final_app_status='3' WHERE requisitionno='".$pr_id."'",$db);
   }else{
   $sqlPend=DB_query("SELECT approvalStatus FROM pr_head_approval WHERE requisition_id='".$pr_id."' AND approvalStatus='2'",$db);
   if($sqlPend){
   $sqlup=DB_query("UPDATE materialrequisitions SET final_app_status='2' WHERE requisitionno='".$pr_id."'",$db);
    }
   }
}
if($_REQUEST['Save'])
{
	
     foreach($_REQUEST['Approval'] as $key=>$value)
	{

    if($value==1){
        
    if($_REQUEST['qty'][$key]==$_REQUEST['app_qty'][$key])
	{
	$sts='3';
	}
	else
	{
	$sts='2';
	}
	if($_REQUEST['app_qty1'][$key]=='')
	{
	$Totalqty=$_REQUEST['app_qty'][$key];
	}
	else
	{
	$Totalqty=$_REQUEST['app_qty'][$key]+$_REQUEST['app_qty1'][$key];
	}
//echo "select id from pr_head_approval where requisition_id='".$_REQUEST['appby'][$key]."' and requiNo='".$_REQUEST['requiNo'][$key]."' and sr_no='".$_REQUEST['hidsrno']."'";
     // exit;
        $sql=DB_query("select id from pr_head_approval where requisition_id='".$_REQUEST['appby'][$key]."' and requiNo='".$_REQUEST['requiNo'][$key]."' and sr_no='".$_REQUEST['hidsrno']."'",$db);
        $record=DB_fetch_array($sql);
        if(DB_num_rows($sql)>0){
	    $data['app_quantity']=$Totalqty;
	    $data['approvalStatus']=$sts;
        $cond="id='".$record['id']."'";
        $s->RecordEdit('pr_head_approval',$data,$cond);
        $sql_chk=DB_query("select id from pr_head_approval where requisition_id='".$_REQUEST['appby'][$key]."' and requiNo='".$_REQUEST['requiNo'][$key]."' and sr_no='".($_REQUEST['hidsrno']+1)."' and approvalStatus='3'",$db);
        if($sql_chk){
        DB_query("UPDATE pr_head_approval SET approvalStatus='2' WHERE requisition_id='".$_REQUEST['appby'][$key]."' AND requiNo='".$_REQUEST['requiNo'][$key]."' AND sr_no='".($_REQUEST['hidsrno']+1)."' AND approvalStatus='3'",$db);
        }
        checkPR($_REQUEST['appby'][$key],$_REQUEST['requiNo'][$key],$Totalqty);
        }else{
	//$arry[]=$_REQUEST['appby'][$i];
    $data['requisition_id']=$_REQUEST['appby'][$key];
	$data['requiNo']=$_REQUEST['requiNo'][$key];
	$data['app_quantity']=$Totalqty;
	$data['approvalStatus']=$sts;
    $data['sr_no']=$_REQUEST['hidsrno'];
    $data['approved_by']=$_SESSION['UserID'];
	$s->RecordInsert('pr_head_approval',$data);	
	 checkPR($_REQUEST['appby'][$key],$_REQUEST['requiNo'][$key],$Totalqty);

        }
    } 
  
$chld_cnt=DB_fetch_assoc(DB_query("select count(*) as req_cnt from  requisitionorderdetails where requsitionno='".$_REQUEST['appby'][$key]."'",$db));
		$app_cnt=DB_fetch_assoc(DB_query("select no_of_assignee from conf_purch_approval where doc_type='PR' and company_id='".$_SESSION['ACGCompanyId']."'",$db));
		
		$pr_approval_cnt=DB_query("select * from pr_head_approval where requisition_id='".$_REQUEST['appby'][$key]."' and approvalStatus='3' ",$db);
		
		$tot=$app_cnt['no_of_assignee'] * $chld_cnt['req_cnt'];
		if($tot==DB_num_rows($pr_approval_cnt))
		{
			DB_query("update materialrequisitions set fnl_req_status='y' where requisitionno='".$_REQUEST['appby'][$key]."'",$db);
		}
    }
}
?>
<?php if($_REQUEST['daction']==1){?>
<div class="success"><?php echo "Record Approved Successfully";?></div>
	<?php } ?>
<div style="width:100%; margin-left:auto; margin-right:auto;">
<form method="post" action="">
<?php
//if(in_array($_SESSION['UserID'],$Explode))
//{
if($_REQUEST['Go'])
{
	$todate=ConvertSQLDate($_REQUEST['todate']);
	$fromdate=ConvertSQLDate($_REQUEST['fdate']);
	$Site=$_REQUEST['Site'];
	if($_REQUEST['todate']!='' && $_REQUEST['fdate']!='' )
	{
	$filter[]="prdate between '".$fromdate."' and '".$todate."'";
	}
	if($_REQUEST['Site']!='0')
	{
	$filter[]="sitecode='".$Site."'";
	}
	if($_REQUEST['user']!='0')
	{
	$filter[]="requition_by='".$_REQUEST['user']."'";
	}
	$i=1;
	$p=1;
?>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
		<?php if($_REQUEST['fdate']!='' && $_REQUEST['todate']!='') {?>
		<tr>
		<th style="color:#006699;"><strong>UnApproved Purchase Requisition List From <?php echo ConvertSQLDate($fromdate);?> to <?php echo ConvertSQLDate($todate)?></strong></th>
		</tr>
		<?php } ?>
</table>     
<?php
 $cat1='';
 $sql1=DB_query("select count(*) from materialrequisitions where CompanyId='".$_SESSION['ACGCompanyId']."'  and  fnl_req_status='n' and sitecode='".$_REQUEST['Site']."' GROUP BY category",$db);
  $numrow=DB_num_rows($sql1);	

  $cateGoryQury="select category from materialrequisitions where CompanyId='".$_SESSION['ACGCompanyId']."'  and  fnl_req_status='n' and sitecode='".$_REQUEST['Site']."' GROUP BY category";
 $cateQueryExec=DB_query($cateGoryQury,$db);
 $p=0;

  while($cateData=DB_fetch_array($cateQueryExec))
    { 
      $category=$cateData['category'];
      
      ?>
               
             <?php 
      if(!empty($filter))
	{
	$filter1=implode(" and ",$filter);
	
	if($_SESSION['UserType']=='SuperAdmin')
	{
	$getlead= "SELECT * FROM materialrequisitions where $filter1 and category='".$category."' and CompanyId='".$_SESSION['ACGCompanyId']."' 
	and fnl_req_status='n' ORDER BY requisitionno DESC";
	}
	else
	{
    $getlead= "SELECT * FROM materialrequisitions where $filter1 and category='".$category."' and CompanyId='".$_SESSION['ACGCompanyId']."' 
     and fnl_req_status='n' ORDER BY requisitionno DESC";
	}
	}

        $getlead1=DB_query($getlead,$db);
        $conutNum=0;
        while($TelRes=DB_fetch_array($getlead1))
		{
			$req_users_res='';
			$req_users_res1='';
			$user_flag=1;			
			$details=DB_fetch_assoc(DB_query("select * from conf_purch_app_details where id='".$_REQUEST['id']."'",$db));
			if($details['dept_wise']=='true')
			{
			 $deptId=$s->GetFieldValue('www_users',"id='".$TelRes['initiator']."'",'department');
			
			  $req_users_sql=DB_query("select * from www_users where id in  (".$details['approval_by'].") and department='".$deptId."' ",$db);
			  while($req_users_res1=DB_fetch_assoc($req_users_sql)){
			  	$req_users_res[$req_users_res1['id']]=$req_users_res1;
			  }
			}
			else if($details['dept_wise']=='false')
			{
			$req_users_sql=DB_query("select * from www_users where id in  (".$details['approval_by'].")",$db);
			  while($req_users_res1=DB_fetch_assoc($req_users_sql)){
			  	$req_users_res[$req_users_res1['id']]=$req_users_res1;
			  }	
			}
            if($_SESSION['UserType']!='SuperAdmin')
            {
            	//echo count($req_users_res);
            	//	echo "<pre>";
            	//	print_r($req_users_res);

             if(!empty($req_users_res)){
			if(array_key_exists($_SESSION['UserID'], $req_users_res))
				$user_flag=0;
			else
				$user_flag=1;
			}
			else
			{
				$user_flag=1;
			}
		    }
		    else
		    {
		    $user_flag=0;	
		    }

			
			if($user_flag==0){
			$req_flag=0;			
            $chld_cnt=DB_query("select * from  requisitionorderdetails
             where requsitionno='".$TelRes['requisitionno']."'",$db);
            if($seq!=1)
            {
            	$prev=$seq-1;
            }
             $seq=$s->GetFieldValue('conf_purch_app_details',"id='".$_REQUEST['id']."'",'seq_no');
            $app_cnt=DB_query("select * from pr_head_approval where approvalStatus='3' and requisition_id='".$TelRes['requisitionno']."' 
            	and sr_no='".$seq."' ",$db);
             //$app_cnt1=DB_query("select * from pr_head_approval where approvalStatus='3' and requisition_id='".$TelRes['requisitionno']."' 
             	//and sr_no='".$prev."' ",$db);
            if($seq!=1)
            {
            $prev=$seq-1;
            $app_cnt1=DB_query("select * from pr_head_approval where  requisition_id='".$TelRes['requisitionno']."' and sr_no='".$prev."' ",$db);
            $prev_cnt=DB_num_rows($app_cnt1);
            }
        	else
        	{	
        		$prev=$seq;
        		$prev_cnt=1;
        	}    
        	        
            if((DB_num_rows($chld_cnt)==DB_num_rows($app_cnt)) || ($prev_cnt==0))
            	$req_flag=1;
            else  if((DB_num_rows($app_cnt)==DB_num_rows($app_cnt1)) && (DB_num_rows($app_cnt)!=0 && DB_num_rows($app_cnt1)!=0) || ($prev_cnt==0))
            	$req_flag=1;
            else
            	$req_flag=0;

            if($req_flag==0) 
            {
              if($_REQUEST['hidsr_no']>1){
              $sr=$_REQUEST['hidsr_no']-1;
             }else
             {
             $sr='';
             }  
             if($sr=='')
             {             
             $sqlqry2=DB_fetch_array(DB_query("select count(id) AS record_no from requisitionorderdetails where requsitionno='".$TelRes['requisitionno']."'",$db));
             $numroww1=$sqlqry2['record_no'];
             $sqlqry3=DB_query("select id from pr_head_approval where requisition_id='".$TelRes['requisitionno']."' and approvalStatus=3 and sr_no='".$_REQUEST['hidsr_no']."'",$db);
             $numroww2=DB_num_rows($sqlqry3);
             }
             else
             { 
		         $sqlqry2=DB_query("select id from pr_head_approval where requisition_id='".$TelRes['requisitionno']."' and approvalStatus!=1 and sr_no='".$sr."'",$db);
		         $numroww1=DB_num_rows($sqlqry2);
		         $sqlqry3=DB_query("select id from pr_head_approval where requisition_id='".$TelRes['requisitionno']."' and approvalStatus=3 and sr_no='".$_REQUEST['hidsr_no']."'",$db);
		         $numroww2=DB_num_rows($sqlqry3);
             }
             $k=1;
        $q="Select S1.initiator,S1.prdate, S2.* from materialrequisitions AS S1,requisitionorderdetails AS S2 where S2.requsitionno ='".$TelRes['requisitionno']."' and S1.requisitionno=S2.requsitionno";
	   $itemSql=DB_query($q ,$db);  
	  
	     ?>
	    
         <h3><?php echo $category; ?></h3> 
         <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
         <thead>
         <tr>
	     <th colspan="10" ><strong>Requisition No. &amp; Date :</strong> <a href="index.php?route=POModule/purchase_requisition_view&modid=<?php echo $_REQUEST['modid'] ?>&frmid=<?php echo $_REQUEST['frmid'] ?>&RequisitionNo=<?php echo $TelRes['requisitionno'];?>" target="_blank"><?php echo $TelRes['Reqno'];?></a> - <?php echo "  ".ConvertSQLDate($TelRes['prdate']); ?>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<strong>Indent By :</strong> <?php echo  $ClientName=$s->GetFieldValue('empmaster',"id='".$TelRes['requition_by']."'",'emp_name');?>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<strong>Created By :</strong> <?php  echo  ucfirst($s->GetFieldValue('www_users',"id='".$TelRes['initiator']."'" ,'realname')); ?></th>
	  </tr>
        <tr>
        <th width="3%">S. No.</th>
		<th width="10%">Item Code</th>
		<th>Item Name</th>	
		<th width="5%">Unit</th>
		<th width="7%">Indent/PR Qty</th>
		<th width="7%">Approve Qty</th>
		<th width="7%">Pending Qty</th>
		<th width="7%">Approved By</th>
		<th width="20%">Remarks</th>
		<th width="6%">Check For Approval <input type="checkbox" name=ch[] id="ch_<?php echo $TelRes['requisitionno']  ?>" value="<?php echo $TelRes['Reqno']; ?>" onclick="checkall(this.id)"></th>		
	  </tr>
	  </thead>
	  <tbody>
          <?php 
          while($itemRow=DB_fetch_array($itemSql))
            { 
          $sqlqry=DB_query("select app_quantity from pr_head_approval where requiNo='".$itemRow['id']."'
           and approvalStatus =3 and sr_no='".$_REQUEST['hidsr_no']."'",$db);
          $numrow=DB_num_rows($sqlqry);
         if($numrow==0)
         {
            if($_REQUEST['hidsr_no']>1){
             $sr=$_REQUEST['hidsr_no']-1;
             }
             else
             {
             $sr='';
             }  
             if($sr=='')
             {
             $numrow2=1;
             }
             else
             { 
             $sqlqry2=DB_query("select app_quantity from pr_head_approval 
             	where requiNo='".$itemRow['id']."' and approvalStatus !=1
             and sr_no='".$sr."'",$db);         
             $numrow2=DB_num_rows($sqlqry2);
             }     
             if($numrow2>0)
             {
              if($_REQUEST['hidsr_no']>1){
              $q2="select app_quantity from pr_head_approval where requiNo='".$itemRow['id']."' and sr_no='".$sr."'";
              $Row=DB_fetch_array(DB_query($q2,$db));
              if($Row['app_quantity']!='')	
				{  
	                 $qty=$Row['app_quantity'];
	                 $q3="select app_quantity from pr_head_approval where requiNo='".$itemRow['id']."' and sr_no='".$_REQUEST['hidsr_no']."'";
	                 $Row3=DB_fetch_array(DB_query($q3,$db));
	                 $previous_qty=$Row3['app_quantity'];
	                   if($Row3['app_quantity']=='')	
				{  
				$Finalqty=$Row['app_quantity'];
				}
				else
				{ 
				$Finalqty=$Row['app_quantity']-$Row3['app_quantity'];
				}
				}               
              }else
              {
              $q2="select app_quantity from pr_head_approval where requiNo='".$itemRow['id']."'";
              $Row=DB_fetch_array(DB_query($q2,$db));
               $qty=$itemRow['requisition_qty'];
                $previous_qty=$Row['app_quantity'];
              if($Row['app_quantity']=='')	
				{  
				$Finalqty=$itemRow['requisition_qty'];
				}
				else
				{ 
				$Finalqty=$itemRow['requisition_qty']-$Row['app_quantity'];
				}
              }
              $UnitId=$s->GetFieldValue('item_master',"id='".$itemRow['mst_itemid']."'",'mst_unitid');
              $p++;
              // echo $p."--".$k;
  
              ?>
        <tr>
		<td><?php echo $k;?></td>
		<td><?php echo 	$s->GetFieldValue('item_master',"id='".$itemRow['mst_itemid']."'",'StockId'); ?></td>
		<td><?php echo 	$s->GetFieldValue('item_master',"id='".$itemRow['mst_itemid']."'",'item_name');?></td>
		<td><?php echo 	$s->GetFieldValue('unitsofmeasure',"unitid='".$UnitId."'",'unitname');?></td>
		<td><?php echo $qty; ?>
<input type="hidden" name="appby[<?php echo $p?>]" value="<?php echo $TelRes['requisitionno']; ?>"  />
		<input type="hidden" name="qty[<?php echo $p?>]" id="qty_<?php echo $p;?>" value="<?php echo $Finalqty; ?>" />
		</td>
		<td><input type="text" name="app_qty[<?php echo $p?>]" id="app_qty_<?php echo $p;?>" onkeyup="checkqty('<?php echo $p;?>',this.value)" />
		<input type="hidden" name="app_qty1[<?php echo $p?>]" id="app_qty1_<?php echo $p;?>" value="<?php echo  $previous_qty; ?>" />
		</td>
		<td><?php echo $Finalqty; ?><input type="hidden" style="width:107px;" name="pending_qty[<?php echo $p ?>]" id="pending_qty_<?php echo $p;?>" value="<?php echo $Finalqty; ?>" ></td>
		
		<td><?php echo $UserId=$s->GetFieldValue('www_users',"id='".$_SESSION['UserID']."'",'realname');?>
			<input type="hidden" name="Approved_by[<?php echo $p; ?>]" id="Approved_by" value="<?php echo $_SESSION['UserID']; ?>" />
		</td>
		<td><?php echo $itemRow['Remarks'] ?></td>
		<td><input type="hidden" name="requiNo[<?php echo $p; ?>]" value="<?php echo $itemRow['id'];?>"  />
			<input type="checkbox" name="Approval[<?php echo $p?>]" class="Approval_<?php echo $TelRes['requisitionno']; ?>" id="Approval_<?php echo $p;?>" onclick="checkapproveqty(<?php echo $p;?>)" value="1">
		</td>
		</tr><?php $k++; } } } ?>
		</tbody>
         </table><?php  } }   } ?>
        <?php  } ?>    
  
 <?php if($p>0){?>
    <input type="hidden" name="hidsrno" id="hidsr_no" value="<?php echo $record['seq_no']; ?>" />
	<p class="text-center"><input type="submit" name="Save" value="Approve" onclick="return CheckApprovalBox()"  class="search" ></p>
<?php	} 
          
 }?>  
</form>
</div>
<!--main div Close -->

<script type="text/javascript">
function checkall(str)
{

	//alert(str);
	var id=str.split("_");
	//alert(id[1]);
    if(document.getElementById(""+str).checked==true)
    {
    	//alert("go");
      $('input:checkbox.Approval_'+id[1]).not(this).attr('checked', true);
      $(".Approval_"+id[1]).each(function()
      {
           var q=($(this).attr('id'));
           var p=q.split("_");
            checkapproveqty(p[1]);
      });
      
    }  
    else
    {
    	$('input:checkbox.Approval_'+id[1]).not(this).attr('checked', false);
    	$(".Approval_"+id[1]).each(function()
      {
           var q=($(this).attr('id'));
           var p=q.split("_");
            checkapproveqty(p[1]);
      });
    }

}	

</script>
<style>
	.tab_div {    
        width: 100%;
    }
</style>