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

<?php ob_start();
$PageSecurity = 2;
$title = _('Quotation Comparison List');
include('includes/SQL_CommonFunctions.inc');
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'], $_SESSION['ACGRoleId']);
$_SESSION['frmView']=$frmpermission[3];
$_SESSION['frmDelete']=$frmpermission[2];
?>
<link rel="stylesheet" href="css/jquery.ui.all.css">
<script src="js/jquery-1.5.1.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>
<script src="ui/jquery.ui.datepicker.js"></script>
<script>
$(function() {
	$(".datepicker").datepicker({
		showOtherMonths: true,
		selectOtherMonths: true
	});
});
</script>
<script type="text/javascript">
function showSlidingDiv(){
$("#slidingDiv").animate({"height": "toggle"}, { duration: 900 });
}
function searchRecord(){
	if(document.getElementById('datepicker').value==''){
		alert('Pick start date');
		document.getElementById('datepicker').focus();
		return false;
	}
	if(document.getElementById('datepicker1').value==''){
		alert('Pick end date');
		document.getElementById('datepicker1').focus();
		return false;
	}
	return true;
}
</script>

<?php
if (empty($_GET['identifier'])) {
			 $identifier=date('U');
		} else {
			$identifier=$_GET['identifier'];
		}
if(isset($_GET['id'])!="" && isset($_GET['delete'])!="" && isset($_GET['delete'])=='yes')
{
	$SQL = "DELETE From comprative_detail Where comprative_id='".$_GET['id']."'";
	$Result = DB_query($SQL,$db,$ErrMsg);
	prnMsg(_('Comprative ') .' ' . _('has been deleted') ,'success');
	echo "<script>location.href='index.php?route=Comperitive_listing&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
if (isset($_GET['SelectedStockItem'])){
	$SelectedStockItem=trim($_GET['SelectedStockItem']);
} elseif (isset($_POST['SelectedStockItem'])){
	$SelectedStockItem=trim($_POST['SelectedStockItem']);
}
if (isset($_GET['RequisitionNumber'])){
	$RequisitionNumber=trim($_GET['RequisitionNumber']);
} elseif (isset($_POST['RequisitionNumber'])){
	$RequisitionNumber=trim($_POST['RequisitionNumber']);
}
if (isset($_GET['SelectedSupplier'])){
	$SelectedSupplier=trim($_GET['SelectedSupplier']);
} elseif (isset($_POST['SelectedSupplier'])){
	$SelectedSupplier=trim($_POST['SelectedSupplier']);
}
        $type="paging";
		$rowsPerPage = 5;
		$pageNum = 1;
		if(isset($_GET['page']))
		{
			$pageNum = $_GET['page'];
		}
		$offset = ($pageNum - 1) * $rowsPerPage;
echo '<form action="' . $_SERVER['PHP_SELF'] . '?' . SID . '" method=post>';
if (isset($_POST['ResetPart'])){
     unset($SelectedStockItem);
}
if (isset($RequisitionNumber) && $RequisitionNumber!='') 
{
	if (!is_numeric($RequisitionNumber)){
		  echo '<br><b>' . _('The Requisition Number entered') . ' <U>' . _('MUST') . '</U> ' . _('be numeric') . '.</b><br>';
		  unset ($RequisitionNumber);
	} }
else 
{
if (isset($SelectedSupplier)) {
echo '<br><div class="page_help_text">' . _('For supplier') . ': ' . $SelectedSupplier . ' ' . _('and') . ' ';
echo '<input type=hidden name="SelectedSupplier" value="' . $SelectedSupplier . '"></div>';
}
if (isset($SelectedStockItem)) {
 echo '<input type=hidden name="SelectedStockItem" value="' . $SelectedStockItem . '">';
}}
if(isset($_GET['status']) and $_GET['status'] == "1")
{  
  if(isset($_POST['Approved']))
      {
	     $sql='SELECT id FROM requisitionorderdetails WHERE requsitionno='."'".$_GET['RequisitionNo']."'"; 
		 $result=DB_query($sql,$db);
		 $i=1;
		   $reqid = $_POST['reqid'];
		 $totalRec = $_POST['record_count'];
		 for($ct = 1; $ct<$totalRec; $ct++) 
		   {
				   $quantity = $_POST['AppQut'.$ct];
				   $id = $_POST['productid'.$ct];
			 $sql=" UPDATE requisitionorderdetails SET 
			 			App_quantity='$quantity', approved_date='".date('Y-m-d')."', status='1' WHERE id='$id' and requsitionno='$reqid' " ;
			$result=DB_query($sql, $db);
			if($result){
				header("Location: MR_SelectOSMaterialRequi.php?identifier=".$_REQUEST['identifier']."&status=1&RequisitionNo=".$reqid);
			}
			$i++;		  
		   }		  
	  }
  }

?>

<div class="body_contain">
<div class="tab_div">

<?php
  
	if(!isset($RequisitionNumber) or $RequisitionNumber!='')
	{
	
		echo '<h1>' . ' ' . $title. '</h1>' ; 
		echo '<a href="' . $rootpath . '/MR_Header.php?' .SID . '&NewOrder=Yes" onClick="showSlidingDiv(); return false;">' . _('Search') . '</a>';
		?>




		<div class="centre" id="slidingDiv" style="display:none">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr class="row1">
			<td class="tabledata1">Requistion Number</td>
			<td><input type="text" name="RequisitionNumber" style="width:120px;"></td>
			<td class="tabledata1">Site</td>
			<td><select name="StockLocation"  style="width:120px;"><option value="000">---Select Site---</option>
		<?php
		$sql = "SELECT loccode, locationname FROM locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$_SESSION['AssignedBranch']."'";
		$resultStkLocs = DB_query($sql,$db);
		while($myrow=DB_fetch_array($resultStkLocs))
		{?>
			<option value="<?php echo $myrow['loccode'];?>"><?php echo $myrow['locationname'];?></option>
  <?php } ?>
		</select></td></tr>
		
   <tr class="row1">
    <td class="tabledata1">From</td>
    <td><input type="text" name="fromdate" class="datepicker"  style="width:120px;" value="<?php echo date('d/m/Y');?>" /></td>
    <td class="tabledata1">To</td>
    <td><input type="text" name="todate"  style="width:120px;" class="datepicker"  value="<?php echo date('d/m/Y');?>"/></td>
  </tr>
		
		
		<!--<input type=submit name="SearchOrders" VALUE="Search">-->
<?php }
	?>
   </tr>
   </table>

<p align="left" style="padding-left:10px;"><input type="submit" name="Search" VALUE="Search" onclick="return searchRecord()"></p>
<br />
</div>

<?php
if(isset($_REQUEST['Search']))
{
$filter = array();
$SQL = "select * from comprative_detail";
$resultpaging_SQL = "select * from comprative_detail";

if(isset($_POST['RequisitionNumber']) && $_POST['RequisitionNumber'] != '')
$filter[] = "RequisitionNo LIKE '%".$_POST['RequisitionNumber']."'";
if(isset($_POST['StockLocation']) && $_POST['StockLocation'] != '000')
$filter[] = "sitecode='".$_POST['StockLocation']."'";
if($_POST['fromdate'] != '' && $_POST['todate'] != '')
$filter = "Comprative_date between '".FormatDateForSQL($_POST['fromdate'])."' and '".FormatDateForSQL($_POST['todate'])."' ";

if($filter != '')
{
$SQL=$SQL." where CompanyId='".$_SESSION['ACGCompanyId']."' and ".implode(" and ",$filter);
}/*."and initiated_by='".$_SESSION[UserID]."'"*/
} 
else 
{
$SQL = "select * from comprative_detail where CompanyId='".$_SESSION['ACGCompanyId']."' order by Comprative_date  DESC ";
$resultpaging_SQL = "select * from comprative_detail where CompanyId='".$_SESSION['ACGCompanyId']."'";
} //end not order number selected



$ErrMsg = _('No orders were returned by the SQL because');
$purchordersResult = DB_query($SQL,$db,$ErrMsg);
$resultpaging = DB_query($resultpaging_SQL,$db,$ErrMsg);
/*show a table of the orders returned by the SQL */?>

<table cellpadding="3" cellspacing="0" width="100%" border="1">
<tr>
<td class="tabledata">Comparative Number</td>
<td class="tabledata">Date</td>
<td class="tabledata">Branch</td>
<td class="tabledata">Requisition No.</td>
<td class="tabledata">Vendor Quotation No.</td>
<td class="tabledata">Party</td>
<td class="tabledata">View</td>
<td class="tabledata">Delete</td>
</tr><?php 
$j = 1;
$k=0; //row colour counter
if(DB_num_rows($purchordersResult) >0)
{
while ($myrow=DB_fetch_array($purchordersResult)) 
{ 
if ($k==1){ /*alternate bgcolour of row for highlighting */
echo '<tr class="EvenTableRows">';
$k=0;
} else {
echo '<tr class="OddTableRows">';
$k++;
}
$View_Comprative  = '<a href="index.php?route=View_Comperitive&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'&RequisitionNo='.$myrow['RequisitionNo'].'&id='.$myrow['comprative_id'].'&mod=show">'.('View').'</a>';

if($myrow['Status']=='No')
{	

$OrderForPurchase = '<a href="' . $rootpath . '/PO_Comperitive.php?' . SID . '&RequisitionNo=' . $myrow['RequisitionNo'] .'&id=' . $myrow['comprative_id']. '&mod=Edit">' . ('Order For Purchase') . '</a>';
}
else
{
$OrderForPurchase = "Don't Make PO Order Again";
}
$Delete =  '<a href="' . $rootpath . '/Comperitive_listing.php?' . SID . '&id=' . $myrow['comprative_id'] . '&delete=1">' . ('Delete') . '</a>';

$s2 = '<a target="_blank" href="' . $rootpath . '/MR_PDFMaterialRequisition.php?' . SID . '&RequisitionNo=' . $myrow['requisitionno'] . '&realorderno=' . $myrow['realorderno'] . '&ViewingOnly=1">' . $myrow['realorderno']. '</a>';		

$SQL2 = "Select vernder_quotation from venderquotations Where id='".$myrow['Vendor_Quot_No']."' ";
$RES2 = DB_query($SQL2, $db);
$REQ_RES2 = DB_fetch_array($RES2);
$vendor_quotation = $REQ_RES2['vernder_quotation'];
$comp_date = $myrow['Comprative_date'];
$ModifyOrderNumber=$myrow["requisition_site"];

$site_sql="select locationname from locations where loccode='".$myrow['sitecode']."' ";
$site_result = DB_query($site_sql,$db);
while($site_row = DB_fetch_array($site_result))
{?>
<tr bgcolor="#c1c1c1">
<td><?php echo $myrow['comprative_no'];?></td>
<td><?php echo ConvertSQLDate($comp_date);?></td>
<td><?php echo $site_row['locationname'];?></td>
<td><?php 
echo $fnLib->getField('materialrequisitions','requisitionno',$myrow['RequisitionNo'],'Reqno');
?></td>
<td><?php echo $vendor_quotation;?></td>
<td><?php 
//echo $fnLib->getField('venderquotations','id',$myrow['Vendor_Quot_No'],'vendercode');
echo $fnLib->getField('suppliers','vendor_code',$fnLib->getField('venderquotations','id',$myrow['Vendor_Quot_No'],'vendercode'),'vendor_name');
//echo $myrow['vendoe_name'];?></td>
<td><?php if($_SESSION['frmView']=='1'){echo $View_Comprative;}else {echo "No Permission";}?></td>
<td><?php 
if($_SESSION['frmDelete']=='1')
{
if($myrow['status_active']==0){				
echo "<a href='index.php?route=Comperitive_listing&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&id=".$myrow['comprative_id']."&delete=yes' onclick=\"return confirm('" . _('Are you sure you want to delete this Comparative Detail') . "');\">" . _('Delete') . ""; 
} 
else{
echo "Delete";
}
}
else
{echo "No Permission";}?></td>
</tr>
<?php 
$j++;
If ($j == 12){
$j=1;
echo $TableHeader;
}
}	
//end of page full new headings if
}
}
else
{
?>
<tr>
<td colspan="12"><font color="#FF0000"><h2 align="center">No Record</h2></font></td>
</tr>
<?php }
//end of while loop
echo '<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>';
echo '<tr>';
echo '<td colspan=11 style="text-align:center;">';
?>
<?php 
$rows = DB_num_rows($resultpaging);
$maxPage = ceil($rows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
$nav  = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; 
}
else
{
$nav .= " <a href=\"$self?page=$page\" class=\"gllink\">$page</a> ";
} 
}
if ($pageNum > 1)
{
$page  = $pageNum - 1;
echo $prev  = " <a href=\"$self?page=$page\" class=\"gllink\">[Prev]</a> ";
echo $first = " <a href=\"$self?page=1\" class=\"gllink\">[First Page]</a> ";
} 
else
{
echo $prev  = "&nbsp;"; 
echo $first = "&nbsp;";
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
echo $next = " <a href=\"$self?page=$page\" class=\"gllink\">[Next]</a>";
echo $last = " <a href=\"$self?page=$maxPage\" class=\"gllink\">[Last Page]</a>";
} 
else
{
echo $next = "&nbsp;";
echo $last = "&nbsp;"; 
}
?>
<?php
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<input type="hidden" name="ReqNo" value="'.$_POST['ShowRequisition'].'">';
echo "<script>defaultControl(document.forms[0].StockCode);</script>";

echo '</div></div>';

echo '</form>';
//include('includes/footer.inc');
?>