<?php


session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);

if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("Form_GridBudget.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querylog="SELECT FI_QtyBudget as loginaccess, ExporttoExcel, Print from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
	
if ($ExporttoExcel==1)
	$ExporttoExcelStatus= 'button';
else
	$ExporttoExcelStatus= 'hidden';
	

$printaccess=$Print;


$querysetup ="select base1, base2, if(DefaultBase=1, base1, base2) as defaultcurr from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
$rowsetup=mysql_fetch_row($resultsetup);
$defaultcurr = $rowsetup[2];


$querydflproject="select project_code as defaultproject from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
$querydflcostcent="select costcent_code as defaultcostcent from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((empty($project_code)) && $Module_Project == 1)
	$project_code = $defaultproject;
if ((empty($costcent_code)) && $Module_CostCenter == 1)
	$costcent_code = $defaultcostcent;	
	
if ($ShowReport == '') $ShowReport = 0;
print <<< HERE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Quantity Budget</title>
<script>
function showlistProject(str,index) {
	window.open('List_SorProject.php?frm='+str+'&index='+index,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function showlistCostcenter(str,index){
  window.open('List_SorCostcenter.php?frm='+str+'&index='+index,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistitem(str,index){
	
	window.open('select_item.php?item='+str+'&index='+index,'mywindow','width=900,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');	
	
}

function generateBudget(){
	var x = document.qtybudget;
	
	if (x.fromdate.value == '' || x.todate.value == ''){
		alert("From/To Date is Empty!");
		return 0;
	} else if (x.project_code.value == '') {
		alert("Project is Empty!");
		return 0;
	} else {
		x.action = 'Form_BudgetQuantity_Action.php?mode=generateBudget';
		x.submit();
	}
}


function deleteBudget(BQdt_Id, index){
	let bqId = document.qtybudget.bqId.value;
	document.getElementById('loader').style.display = 'block';
	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("[BRK]");
			if (data[1] != '') alert(data[1]);
			document.getElementById('loader').style.display = 'none';
			document.getElementById('budgetGrid').innerHTML=data[2];
		}
	  }
	xmlhttp.open("GET","Form_BudgetQuantity_Action.php?mode=deleteBudgetdt&bqId="+bqId+"&bqdt_Id="+BQdt_Id,true);
	xmlhttp.send();  
	
}

function submitQty(BQdt_Id, index){
	let bqId = document.qtybudget.bqId.value;
	let qty = document.getElementById('qty'+BQdt_Id).value;
	document.getElementById('loader').style.display = 'block';
	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("[BRK]");
			if (data[1] != '') alert(data[1]);
			document.getElementById('loader').style.display = 'none';
			document.getElementById('budgetGrid').innerHTML=data[2];
		}
	  }
	xmlhttp.open("GET","Form_BudgetQuantity_Action.php?mode=addQty&bqId="+bqId+"&bqdt_Id="+BQdt_Id+"&qty="+qty,true);
	xmlhttp.send();  
	
}

function showReport(){
	document.qtybudget.ShowReport.value = 1;
	document.qtybudget.message.value = '';
	document.qtybudget.bqId.value = '';
	document.qtybudget.submit();
	
}
function PrintBudget(exp){
	let fromdate 		= document.getElementById('fromdate').value;
	let todate 			= document.getElementById('todate').value;
	
	let project_code 	= document.getElementById('project_code').value;
	let costcent_code 	= document.getElementById('costcent_code').value;
	
	var str = "";
	
	if (exp == 1)
		str = "Exp_QtyBudget.php";
	else 
	str = "Rep_QtyBudget.php";
	
	if (fromdate == '' && todate == ''){
		alert('Please Select From/To Date!');
		return 0;
	} else if (project_code == ''){
		alert('Please Select Project Code!');
		return 0;
	} else {
		str = str+"?fromdate="+fromdate+"&todate="+todate+"&project_code="+project_code+"&costcent_code="+costcent_code;
		window.open(str);
	}
}

function clearPage(){
	var x = document.qtybudget;
	x.fromdate.value = '';
	x.todate.value = '';
	
	x.project_code.value = '';
	x.costcent_code.value = '';
	
	x.remark.value = '';
	
	bqId.value = '';
	
	x.ShowReport.value = 0;
	x.action = "Form_BudgetQuantity.php";
	x.submit();
	
}

function RefreshPage(){
	let bqId = document.qtybudget.bqId.value;
	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("[BRK]");
			document.getElementById('budgetGrid').innerHTML=data[1];
		}
	  }
	var perpage = document.getElementById('perpage').value;
	
	xmlhttp.open("GET","Form_BudgetQuantity_Action.php?mode=RefreshPage&bqId="+bqId+"&perpage="+perpage,true);
	xmlhttp.send();  
}

function SetNextPage(startrange,perpage){
	let bqId = document.qtybudget.bqId.value;
	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("[BRK]");
			document.getElementById('budgetGrid').innerHTML=data[1];
		}
	  }
	
	xmlhttp.open("GET","Form_BudgetQuantity_Action.php?mode=SetNextPage&bqId="+bqId+"&perpage="+perpage+"&startrange="+startrange,true);
	xmlhttp.send();  
	
}

function SetPrvPage(startrange,perpage){
	let bqId = document.qtybudget.bqId.value;
	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("[BRK]");
			document.getElementById('budgetGrid').innerHTML=data[1];
		}
	  }
	
	xmlhttp.open("GET","Form_BudgetQuantity_Action.php?mode=SetPrvPage&bqId="+bqId+"&perpage="+perpage+"&startrange="+startrange,true);
	xmlhttp.send(); 
}

function searchBudget(){
	window.open("Form_SearchSavedQtyBudget.php","Search", "width=850,height=500");
}
</script>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
.style1 {
	color: #000000;
    font-family: "Times New Roman", Times, serif;
    font-size: 16px;
    font-weight: bold;
}
.style8 {font-size: 11px; color: #999999; font-weight: bold; }
.style11 {font-size: 11px}
body {
	margin-top: 0px;
}
.details1{
	font-family: "Times New Roman", Times, serif;
    font-size: 14px;
}

#loader {
	width: 90px;
    position: absolute;
    right: 50%;
	display: none;
}
-->
/* Hide HTML5 Up and Down arrows. */
input[type="number"]::-webkit-outer-spin-button, input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type="number"] {
-moz-appearance: textfield;
}

</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
	<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
</head>
<body>
HERE;

echo "<form id='qtybudget' name='qtybudget' method='post' action='' >
	<input type='hidden' name='message' value='$message'>	
	<input type='hidden' name='ShowReport' id='ShowReport' value='$ShowReport'>
	<input type='hidden' name='bqId' id='bqId' value='$bqId'>";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Quantity Budget</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";
		
	echo" <button type='reset' id='btnClear' name='btnClear'  title='Clear Page' onClick=\"clearPage()\";><span><span>Clear</span></span></button> ";
	if ($ShowReport == 0)
	{	
		echo"<button type='button' id='btnGenerate' name='btnGenerate'  onClick=\"generateBudget()\"; title='Generate Budget'><span><span>Load Template</span></span></button> ";
		if ($bqId == '')
			echo"<button type='button' id='btnSearch' name='btnSearch'  onClick=\"searchBudget()\"; title='Search Saved Budget'><span><span>Search</span></span></button> ";
		echo"<button type='button' id='btnReport' name='btnReport'  onClick=\"showReport()\"; title='Report'><span><span>Report</span></span></button> ";
	}
	if ($ShowReport == 1)
	echo" <button type='button' id='btnPrint' name='btnPrint' onClick=\"PrintBudget('0')\"; title='Print Budget'><span><span>Print</span></span></button>
		   <button type='button' id='btnExcel' name='btnExcel' onClick=\"PrintBudget('1')\"; title='Print Budget'><span><span>Excel</span></span></button>";
	
echo"	</td></tr></table>";
echo"</td></tr>";				

if ($bqId != ''){
	$query = "select Project_code, costcenter_code, date_format(BQ_FromDate,'%d/%m/%Y'), date_format(BQ_ToDate,'%d/%m/%Y'), BQ_Remark from budget_qtyhd where BQ_Id = $bqId ";
	$res = mysql_query($query, $connection);
	$row = mysql_fetch_row($res);
	$project_code 	= $row[0];
	$costcent_code 	= $row[1];
	$fromdate 		= $row[2];
	$todate 		= $row[3];
	$remark 		= $row[4];
}	
echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=1100px class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>";				
echo '  <table border="0" align="center" valign="top" width="1100px">';
echo "<col width='60px'><col width='120px'><col width='60px'><col width='120px'><col width='60px'><col width='180px'><col width='60px'><col width='180px'><col width='60px'><col width='200px'>";
echo" <tr height='15px'  bgcolor='#F5F5F5'>";	  
echo"   <td><span class='style8'>From</span></td>";
echo"   <td><span class='style8'>";
echo"      <input type='text' name='fromdate' id='fromdate' value='$fromdate' maxlength='10' style='width:80px;' readonly>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.qtybudget.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
		</td>";

echo"   <td><span class='style8'>To</span></td>";
echo"   <td><span class='style8'>";
echo"      <input type='text' name='todate' id='todate' value='$todate' maxlength='10' style='width:80px;' readonly>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.qtybudget.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
	</td>";	
		
		
echo"   <td colspan='2' style='padding-left:20px;'><span class='style8'>";
echo "<input type='hidden' name='showMonth' id='showMonth' value='0' >
			<input type='hidden' name='showQtyExists' id='showQtyExists' value='0' >";
echo "</span></td>";				

echo"   <td><span class='style8'>Project</span></td>";				
echo"   <td><input type='text' name='project_code' id='project_code' value='$project_code' maxlength='20' style='width:130px;' >
			<label><span class='style1'><img valign='bottom' src='img/btnopen.jpg' title=' Select Project ' id='lstProjectCodeH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick=\"showlistProject('40','');\"  /></span></label>
		</td><td></td><td></td>";
		
echo "<input type='hidden' name='costcent_code' id='costcent_code' value='$costcent_code' maxlength='20' style='width:130px;' >";		
/* echo"   <td><span class='style8'>Costcenter</span></td>";				
echo"   <td><input type='text' name='costcent_code' id='costcent_code' value='$costcent_code' maxlength='20' style='width:130px;' >
			<label><span class='style1'><img valign='bottom' src='img/btnopen.jpg' title=' Select Costcenter ' id='lstCostcentCodeH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick=\"showlistCostcenter('36','');\"  /></span></label>
		</td>";
 */
echo" </tr>";


echo "<tr height='10px'><td colspan='10'></td></tr>";
/* 
echo" <tr bgcolor='#F5F5F5'>";	  
echo"   <td><span class='style8'>From</span></td>";
echo"   <td colspan='4'><input type='text' name='fromitem' id='fromitem' value='$fromitem' maxlength='20' style='width:110px;' >
						<input type='text' name='fromitemdesc' id='fromitemdesc' value='$fromitemdesc' style='width:180px;' >
						<label><span class='style1'><img valign='bottom' src='img/btnopen.jpg' title=' Select Item ' id='lstfromitem' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick=\"showlistitem('14','');\"  /></span></label>
		</td>";
echo"   <td></td>";				
echo"   <td colspan='4'><span class='style8'>To</span> &nbsp;&nbsp;&nbsp;";
echo"   	<input type='text' name='toitem' id='toitem' value='$toitem' maxlength='20' style='width:110px;' >
			<input type='text' name='toitemdesc' id='toitemdesc' value='$toitemdesc' style='width:180px;' >
			<label><span class='style1'><img valign='bottom' src='img/btnopen.jpg' title=' Select Item ' id='lsttoitem' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick=\"showlistitem('15','');\"  /></span></label>
		</td>"; */
echo "<tr height='10px'><td><span class='style8'>Remark</span></td><td colspan='9'><input type='text' name='remark' id='remark' value='".htmlentities($remark,ENT_QUOTES)."'  maxlength='250' style='width:900px;' ></td></tr>";		
echo "</tr>";

echo "<tr height='20px'><td colspan='10'><div id='loader'><img src='img/Loading_icon.gif'></div></td></tr>";

// show budget details
$bright = "border-right: 1px solid black";
$bleft = "border-left: 1px solid black";
$btop = "border-tot: 1px solid black";
$bbottom = "border-bottom: 1px solid black";

echo "<tr bgcolor='#F5F5F5'><td colspan='10'><div id='budgetGrid' style='height:650px;width:1050px;'>";
if ($ShowReport == 0)
getQtyBudgetDetails($bqId, $perpage, $startrange, $connection);

echo "</div></td></tr>";
echo" </table></td>";

echo" <td colspan='3' bgcolor='#F5F5F5'>&nbsp;</td>";		
echo" </tr>";


	echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>"; 
 echo" </table>
 
</form>
</body>
</html>";

?>