<link rel="stylesheet" href="css/new_mxerp.css">

<div class="body_contain">
<div class="tab_div">
<h1><?php  echo  $page_title;
include('POModule/Class-ItemMaster.php');
?></h1>
<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  	<tr class="row1">
	  	<td width="13%" align="right">Branch</td>
	  	<td width="20%"><select name="site" id="site">
	  		<option value="">Select Branch</option>
	  		<?php
			$sql="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')";
			$sitesql=DB_query($sql,$db);
	         while($siteres=DB_fetch_array($sitesql))  	
			 { ?>
			 	<option value="<?php echo $siteres['loccode'];?>"<?php echo($_REQUEST['site']==$siteres['loccode']?'selected':'')?>><?php echo $siteres['locationname'];?></option>
				<?php } ?>  		
	  		</select></td>
	    <td width="13%" align="right">Item</td>
	    <td width="20%"><select name="ItemList"><option value="000">select</option><?php $itmsql=DB_query("SELECT DISTINCT(mst_itemid) FROM requisitionorderdetails",$db);
	                while($itmid=DB_fetch_assoc($itmsql)){ $itmdet=DB_fetch_assoc(DB_query("SELECT * FROM item_master where id='".$itmid['mst_itemid']."' ",$db)); ?>
	  	<option value="<?php echo $itmid['mst_itemid']; ?>"<?php if($itmid['mst_itemid']==$_REQUEST['ItemList']) { echo "selected"; } ?>><?php echo $itmdet['item_name'] ?></option>
	                <?php  }
	//echo $ClassItem->GetItem($_REQUEST['ItemList']);	
		?></select></td>
	    <td colspan="2">&nbsp;</td>
	</tr>
    <tr class="row1">	
		<td align="right">From Date</td>
	    <td><input name="fromdate" class="datepicker"type="text"  id="fromdate" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" onchange="javascript:checkdate(this.value)" readonly="true"></td>
	    <td align="right">To Date</td>
	    <td><input name="todate" class="datepicker"type="text" readonly="true" 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>
	    <td width="13%" align="right">Req. No.</td>
	    <td><input name="Reqno" type="text" value="<?php echo $_REQUEST['Reqno'] ?>"></td>
	</tr>
	<tr class="row1">
		<td colspan="6" align="center"><input name="Go" type="submit" class="search" value="Search" onclick="return validate();"></td>
  	</tr>   
</table>
</form>

<?php 
  if($_REQUEST['Go']){
//$filter='1=1 ';
 
     if($_REQUEST['todate']!='' && $_REQUEST['fromdate']!='')
	 $filter.=" and A.prdate between '".FormatDateForSQL($_REQUEST['fromdate'])."' AND '".FormatDateForSQL($_REQUEST['todate']) ."' ";
	 
	 if($_REQUEST['ItemList']!='000' ) 
	 $filter.=" and B.mst_itemid='".$_REQUEST['ItemList']."' ";
	 if($_REQUEST['site']!='000')
	 {
	 	$filter.=" and A.sitecode='".$_REQUEST['site']."'";
	 }
	 if($_REQUEST['Reqno']!='000')
	 {
	 	$filter.=" and A.Reqno like '%".$_REQUEST['Reqno']."%'";
	 }
	
if($filter!='')
     {
      $sql2 = "select * ,(B.required_qty) as REQ , (B.App_quantity) as  APPREQ  from materialrequisitions A,requisitionorderdetails  B  where A.requisitionno=B.requsitionno    $filter  group by  A.requisitionno   ";
     }
else  if($_SESSION['AssignedSites']!='0')
      $sql2 = "select * ,(B.required_qty) as REQ , (B.App_quantity) as  APPREQ  from materialrequisitions A,requisitionorderdetails  B  where A.requisitionno=B.requsitionno  and  sitecode in (".$_SESSION['AssignedSites'].")   group by  A.requisitionno   ";
		
else $sql2 = "select * ,(B.required_qty) as REQ , (B.App_quantity) as  APPREQ  from materialrequisitions A,requisitionorderdetails  B  where A.requisitionno=B.requsitionno    group by  A.requisitionno   ";
		
  }
  else
  {
   if($_SESSION['AssignedSites']!='0'){ 
      $sql2 = "select * ,(B.required_qty) as REQ , (B.App_quantity) as  APPREQ  from materialrequisitions A,requisitionorderdetails  B  where A.requisitionno=B.requsitionno  and sitecode in (".$_SESSION['AssignedSites'].") group by  A.requisitionno  ";
  }
  else
 {
     $sql2 = "select * ,(B.required_qty) as REQ , (B.App_quantity) as  APPREQ  from materialrequisitions A,requisitionorderdetails  B  where A.requisitionno=B.requsitionno  group by  A.requisitionno  ";
 }
  }	
     $fromdate=$_REQUEST['fromdate'];
     $todate=$_REQUEST['todate'];	 
	 $fromdate_brk=explode('/',$fromdate);
	 $from=$fromdate_brk[2]."-".$fromdate_brk[1]."-".$fromdate_brk[0];
	 $todate_brk=explode('/',$todate);
	 $to=$todate_brk[2]."-".$todate_brk[1]."-".$todate_brk[0];
	 $sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');	
	
	if($_REQUEST['site']!='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	{
	$message="Showing Records of Branch <u><span class='fntBig'>".$sitename."</span></u> From <u><span class='fntBig'>".$from."</span></u> to <u><span class='fntBig'>".$to."</span></u>";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['fromdate']=='' && $_REQUEST['todate']=='')
	{
	$message="Showing Records of Branch <u><span class='fntBig'>".$sitename."</span></u>";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	{
	$message="Showing Records From <u><span class='fntBig'>".$from."</span></u> to <u><span class='fntBig'>".$to."</span></u>";	
	}	
	
  $_SESSION['poreport1']=$sql2;
  $_SESSION['mess']=$message;  
  $_SESSION['ItemList']=$_REQUEST['ItemList'];
  
?>
<?php if($_REQUEST['Go']!=''){ ?>
<!--<a href="tcpdf/Pdf_POReport.php?loccode=<?php echo $_REQUEST['site'];?>&itemname=<?php echo $_REQUEST['ItemList'];?>&todate=<?php echo $_REQUEST['todate'];?>&fromdate=<?php echo $_REQUEST['fromdate'];?>">PDF</a>
<a href="Export_POReport.php?loccode=<?php echo $_REQUEST['site'];?>&itemname=<?php echo $_REQUEST['ItemList'];?>&todate=<?php echo $_REQUEST['todate'];?>&fromdate=<?php echo $_REQUEST['fromdate'];?>">Export</a>-->


<p align="center"><strong><?php echo $message; ?></strong> <a href="POReport_print.php?loccode=<?php echo $_REQUEST['site'];?>&itemname=<?php echo $_REQUEST['ItemList'];?>&todate=<?php echo $_REQUEST['todate'];?>&fromdate=<?php echo $_REQUEST['fromdate'];?>" target="_blank" class="search" style="float:right;">Print</a></p>


<table width="100%" border="0" class="thborder" cellpadding="3" cellspacing="0">
  	<tr>
	    <th width="3%">S. No.</th>  
		<th width="7%">Date</th>
	    <th>Branch</th>     
		<th width="12%">Req. No.</th>
	    <!--<th width="10%">PR No. </th><th width="10%">PR Date </th>-->
	    <th width="120">Item Code</th>
	    <th width="300">Item Name</th>
	    <th width="55">Unit</th>
	    <th width="80" class="text-right">PR Qty.</th> 
	    <th width="80" class="text-right">PO Qty.</th>
  	</tr>
  
   <?php
	$i=1;
	 $res = DB_query($sql2,$db);
  	 while($row = DB_fetch_array($res))
	 { ?>
  	<tr onmouseover="this.style.backgroundColor='#fffeb3'; this.style.color='#00463f';" onmouseout="this.style.backgroundColor='#FFF'; this.style.color='#333333';">
	<td><?php echo $i++; ?></td>
	<td><?php echo ConvertSQLDate($row['prdate']); ?></td>
	<td><?php echo $s->getField('locations','loccode',$row['sitecode'],'locationname');	?></td>
	 <?php  //echo $row['APPREQ'];
	 // $getpodata="select orddate,orderno from purchorders where mst_reqino='".$row['requisitionno']."' and  FIND_IN_SET('".$row['mst_itemid']."',mst_vendor_pid);";
	 //$result = DB_query($getpodata,$db);
	   //$PODATA = DB_fetch_array($result);

	?> 
	<td><?php echo $row['Reqno']; ?></td>
	<!--<td><?php  echo $PODATA['orderno'];?></td>
	<td><?php  echo ConvertSQLDate($PODATA['orddate']);?></td>	-->				
	<td colspan="5" style="padding:0px;" valign="top">
		<div id="amc-tab">
			<table width="100%" border="0" cellpadding="3" cellspacing="0">
 <?php 
 
    if($_REQUEST['ItemList']!=000)
     $details_sql="Select * from requisitionorderdetails where requsitionno='".$row['requsitionno']."' and mst_itemid='".$_REQUEST['ItemList']."'";
	else
	  $details_sql="Select * from requisitionorderdetails where requsitionno='".$row['requsitionno']."'";
	$_SESSION['details_sql']=$details_sql;
	$details_res=DB_query($details_sql,$db);
	$issueqty[]='';
	$req_qty[]='';

	$numrow1=DB_num_rows($details_res);
	$tot_qty=0;
	if(DB_num_rows($details_res)>0){
		$apqty='';
	while($details_row=DB_fetch_array($details_res)){


		$quotation_id=$s->getField('venderquotations','indent_prno',$row['requisitionno'],'id');


  
		 $poqty1=DB_query("SELECT purchorderdetails.Quantity AS qty FROM purchorderdetails INNER JOIN purchorders
 ON purchorderdetails.parantId=purchorders.id WHERE 
   purchorderdetails.mst_itemid='".$details_row['mst_itemid']."' and quotationId='".$quotation_id."' ",$db);
	$poqty2=DB_fetch_array($poqty1);
	 if($details_row['App_quantity']!='0.00') 
	 { 
	?>
          	<tr>
	            <td width="120">
				<?php echo $s->getField('item_master','id',$details_row['mst_itemid'],'StockID'); ?></td>
	            <td width="300">
				<?php echo $s->getField('item_master','id',$details_row['mst_itemid'],'item_name'); ?></td>
	            <td width="55"><?php 
				 $unitname1 = $s->GetFieldValue('item_master',"id='".$details_row['mst_itemid']."'",'mst_unitid');
				echo $unitname = $s->GetFieldValue('unitsofmeasure',"unitid='".$unitname1."'",'unitname') ; ?> </td>
	            <td width="80" style="text-align:right;"><?php   echo $details_row['App_quantity'];
				     $apqty[]=$details_row['App_quantity'];
				     $tot_qty+=$details_row['App_quantity'];
					   ?></td>
	      		<td width="80" style="text-align:right;"><?php echo $poqty2['qty'];  ?></td> 
          	</tr>
        <?php } } if($tot_qty>0) { ?>
		    <tr>
		      <td style="text-align:right;" colspan="3"><strong>Total</strong></td>
		      <td style="text-align:right;"><strong><?php echo $tot_qty; ?></strong></td>
		      <td></td>
		    </tr>
        <?php }
        //if(!empty($apqty)){?>
          <!--<tr bgcolor="#FFFFFF" style="height: 25px">
        <td class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap; border-right:#666666 1px solid;" colspan="3"><strong>Total</strong></td>
     <td class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;" colspan="2"><span class="jjrs_tabledata1" style="white-space:nowrap;"><strong><?php echo $a[]=array_sum($apqty); ?></strong></span></td>
  
        </tr>--><?php //} ?>
          <?php }  ?>
        </table>
        </div>
        </td>
  	</tr>
         <?php } ?>
  
    <?php if($numrow1>0) { ?>
   <?php if(!empty($a)) { ?>

<!--	<table width="100%" cellpadding="0" cellspacing="0" border="0" style="border:solid 1px #666666;">
  <tr style="background-color:#CCCCCC; height: 30px">
	<td width="6%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;">&nbsp;</td>
	<td width="6%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;">&nbsp;</td>
	<td width="8%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;">&nbsp;</td>
	<td width="19%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;">&nbsp;</td>
		<td width="8%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;">&nbsp;</td>
	<td width="19%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;">&nbsp;</td>
        <td width="20%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;"><strong>Grand Total</strong></td>
        <td width="25%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;"><span class="jjrs_tabledata1" style=" white-space:nowrap;"><?php  echo array_sum($a); ?></span></td>
        <td width="6%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;">&nbsp;</td>
        <td width="6%" class="jjrs_tabledata1" style="padding-left:5px; padding-right:5px; white-space:nowrap;"></td>
  </tr>
</table> -->

      <?php } } else { ?>
	   <tr style="height: 25px">
       <td colspan="9" align="center"><strong>No Records</strong></td>
       </tr>	  
      <?php } ?>   
</table>
 <?php } ?>
 <script type="text/javascript">
	function validate()
	{
		if($("#site").val()=="")
		{
			alert("Select Branch");
			$("#site").get(0).focus();
			return false;
		}
	}

	function checkdate(date)
{
	var from =document.getElementById('from').value;
	var to =document.getElementById('to').value;
	
		var cdate=date.split("/");
		var sdate=from.split("/");
		var edate=to.split("/");

		cdate[1]=cdate[1]-1;
		sdate[1]=sdate[1]-1;
		edate[1]=edate[1]-1;
			 
		var dateC = new Date(cdate[2], cdate[1], cdate[0]); //Year, Month, Date 
		var dateS = new Date(sdate[2], sdate[1], sdate[0]); //Year, Month, Date
	    var dateE = new Date(edate[2], edate[1], edate[0]); //Year, Month, Date

	    if (dateS > dateE) {
	        alert("Start Date is not greather then Date End.");
	        document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	        
	    }

	    if (dateC > dateE) {
	        alert("Date Not greather than "+to);
	       document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	       
	    }

	    if (dateC < dateS) {
	        alert(" Date Not less than "+from);
	        document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	       
	    }	
	
	
}

</script>
</div>
</div>