<?php 
 if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}	unset($_SESSION['success_msg']);
$SQLex=DB_query("select * from pr_assign_approval_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 count=document.getElementById("count1").value;
	var status=0;
	for(var j=1;j<count;j++)
	{
		if(document.getElementById("Approval_"+j).checked==true)
		{
			status=1;
			//return false;
		}
	}
	
	if(status=='0')
	{
		alert("Please Check At Least One For Approval");
		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)
    {  
	//var data=xmlhttp.responseText.split("__");	
	//alert(data);
	//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>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<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 class="tabledata1"><strong>Branch</strong></td>
	<td><select style="width:150px;" 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"><strong>Department</strong></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 class="tabledata1"><strong>From Date</strong></td>
        <td><input type="text" readonly name="fdate" id="fdate" style="width:150px;"  class="datepicker" value="<?php if($_REQUEST['fdate']=='') { echo ConvertSQLDate($_SESSION['financialYrStart']);} else { echo $_REQUEST['fdate'];} ?>"/></td>
	<td class="tabledata1"><strong>To Date</strong></td>
        <td><input type="text" name="todate" readonly id="todate" style="width:150px;" class="datepicker"  value="<?php if($_REQUEST['todate']=='') { echo date("d/m/Y"); } else { echo $_REQUEST['todate']; } ?>"  /></td>
	
	<td class="tabledata1"><strong>Indent By</strong></td>
	<td ><div id="UserIndentdep">
            <?php
        $EMPSQL=DB_query("SELECT * FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND emp_site IN ('".$_SESSION['AssignedBranch']."')",$db);?>
	<select style="width:150px;" 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['sr_no']; ?>" />
</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>
</div>
<br />
<?php
//echo $user_ids;
if($_REQUEST['Save'])
{
    
     
	//$arry=array();
    for($i=1;$i<=count($_REQUEST['requiNo']);$i++)
	{
    if($_REQUEST['Approval'][$i]==1){
        
       if($_REQUEST['qty'][$i]==$_REQUEST['app_qty'][$i])
	{
	$sts='3';
	}
	else
	{
	$sts='2';
	}
	if($_REQUEST['app_qty1'][$i]=='')
	{
	$Totalqty=$_REQUEST['app_qty'][$i];
	}
	else
	{
	$Totalqty=$_REQUEST['app_qty'][$i]+$_REQUEST['app_qty1'][$i];
	}
//        echo "select id from pr_head_approval where requisition_id='".$_REQUEST['appby'][$i]."' and requiNo='".$_REQUEST['requiNo'][$i]."'";
//        exit;
        $sql=DB_query("select id from pr_head_approval where requisition_id='".$_REQUEST['appby'][$i]."' and requiNo='".$_REQUEST['requiNo'][$i]."' 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);
        }else{
	
	//$arry[]=$_REQUEST['appby'][$i];
        $data['requisition_id']=$_REQUEST['appby'][$i];
	$data['requiNo']=$_REQUEST['requiNo'][$i];
	$data['app_quantity']=$Totalqty;
	$data['approvalStatus']=$sts;
        $data['sr_no']=$_REQUEST['hidsrno'];
        $data['approved_by']=$_SESSION['UserID'];
   
	$s->RecordInsert('pr_head_approval',$data);	
        }
    }  
    }
}
?>
<?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="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse; border:#999999 solid 1px;">
		<?php if($_REQUEST['fdate']!='' && $_REQUEST['todate']!='') {?>
		<tr>
		<td colspan="10" style="color:#006699;"><strong>UnApproved Purchase Requisition List From <?php echo ConvertSQLDate($fromdate);?> TO <?php echo ConvertSQLDate($todate)?></strong></td>
		</tr>
		<tr>
		<td colspan="10" style="height:5px;"></td>
		</tr>
		<?php } ?>
</table>
    
            
           
<?php
 $cateGoryQury="select category from materialrequisitions where CompanyId='".$_SESSION['ACGCompanyId']."'  GROUP BY category";
 $cateQueryExec=DB_query($cateGoryQury,$db);
 $p=0;
  while($cateData=DB_fetch_array($cateQueryExec))
    { 
      $category=$cateData['category'];
      ?>
    <fieldset><legend><strong><?php echo $category; ?></strong></legend>             
             <?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']."' ORDER BY requisitionno DESC";
	}
	else
	{
	//echo $getlead= "SELECT * FROM materialrequisitions where $filter1 and CompanyId='".$_SESSION['ACGCompanyId']."' and requition_by in($DeptWiseEmplist) ORDER BY requisitionno DESC"
        $getlead= "SELECT * FROM materialrequisitions where $filter1 and category='".$category."' and CompanyId='".$_SESSION['ACGCompanyId']."' ORDER BY requisitionno DESC";
	}
	}
        
        $getlead1=DB_query($getlead,$db);
        while($TelRes=DB_fetch_array($getlead1))
		{
            
            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
             { //echo "select id from pr_head_approval where requisition_id='".$TelRes['requisitionno']."' and approvalStatus!=1 and sr_no='".$sr."'";
             $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);  
        ?>
         <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse; border:#999999 solid 1px;">
             <tr align="center" bgcolor="#d4d4d4">
	<td colspan="4" style="font-size:12px;"><strong>Requisition No & Date :</strong>&nbsp;&nbsp;&nbsp;<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"><font size="2"><?php echo '<b>'.$TelRes['Reqno'].'</b>';?></font></a><strong><?php echo "  ".ConvertSQLDate($TelRes['prdate']); ?></strong></td>
	   <td  colspan="3"  style="font-size:12px;"><strong>Indent By:&nbsp;
	  <?php echo  $ClientName=$s->GetFieldValue('empmaster',"id='".$TelRes['requition_by']."'",'emp_name');?></strong></td>
			<td colspan="3"><strong>Created By :</strong>&nbsp;&nbsp;<strong><?php  echo  ucfirst($s->GetFieldValue('www_users',"id='".$TelRes['initiator']."'" ,'realname')); ?></strong></td>
	  </tr>
          <tr>
		<td class="tabledata">S. No.</td>
		<td class="tabledata">Part Code</td>
		<td class="tabledata">Part Name</td>	
		<td class="tabledata">Uom</td>
		<td class="tabledata">Indent/PR Qty</td>
		<td class="tabledata">Approve Qty</td>
		<td class="tabledata">Pending Qty</td>
		<td class="tabledata">Approved By</td>
		<td class="tabledata">Ckeck For Approval </td>		
	  </tr>
          <?php 
          while($itemRow=DB_fetch_array($itemSql))
              { 
              $p++;
              //echo $p;
              $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
             {     
//                 echo "select app_quantity from pr_head_approval where requiNo='".$itemRow['id']."' and approvalStatus !=1
//             and sr_no='".$sr."'";
             $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');?>
          
          <tr bgcolor="#c1c1c1">
		<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><input type="hidden" name="requiNo[<?php echo $p; ?>]" value="<?php echo $itemRow['id']?>"  />
			<input type="checkbox" name="Approval[<?php echo $p?>]" id="Approval_<?php echo $p;?>" onclick="checkapproveqty(<?php echo $p;?>)" value="1">
		</td>
		</tr>
              <?php } } } ?>
         </table>
                <?php } ?>
         
                
 </fieldset> 
    <br>
       <?php } ?>
    
  
 <?php if($p>0){?>
    <input type="hidden" name="hidsrno" id="hidsr_no" value="<?php echo $record['sr_no']; ?>" />
	<p align="center"><input type="submit" name="Save" value="Approve" onclick="return CheckApprovalBox()"  class="search" >
        </p>
<?php	}
          
 } ?>  
</form>
</div>
</div>
</div>
<!--main div Close -->

