<?php

session_start();

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

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

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$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");


$mode 			= $_GET['mode'];
$fromdate 		= $_POST['fromdate'];
$todate 		= $_POST['todate'];
$project_code 	= $_POST['project_code'];
$costcent_code 	= $_POST['costcent_code'];
$remark			= $_POST['remark'];
$bqId			= $_POST['bqId']; if ($bqId == '') $bqId = $_GET['bqId'];

$project_code = "".$project_code;
$costcent_code = "".$costcent_code;
	


if ($mode == 'generateBudget' ){
	$message = '';
	$ErrorExists = 0;
	
	if ($costcent_code != '') {
		$sub_query = " Select Count(Costcent_code) From Costcent ".
					 " Where Costcent_code = '".$costcent_code."'";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row = mysql_fetch_row($sub_result);
		if ($sub_row[0] == 0) {
			$ErrorExists = 1;
			$message = 'Insert not allowed. Please check for Costcenter Code.';
		}
	}
	
	if ($project_code != '') {
		$sub_query = " Select Count(Project_Code) From Project ".
					 " Where Project_Code = '".$project_code."'";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row = mysql_fetch_row($sub_result);
		if ($sub_row[0] == 0) {
			$ErrorExists = 1;
			$message = 'Insert not allowed. Please check for Project Code.';
		}
	}
	if ($project_code != '') {
		$sub_query = " Select 1 From budget_qtyhd ".
					 " Where Project_Code = '".$project_code."' and if('$bqId' = '', 1=1, BQ_Id <> '$bqId') "; 
		$sub_result = mysql_query($sub_query, $connection);
		$numrow = mysql_num_rows($sub_result);
		if ($numrow > 0) {
			$ErrorExists = 1;
			$message = 'Insert not allowed. Please Project Already Exists.';
		}
		
	}
	
	if ($ErrorExists == 0){
		if ($bqId == ''){
			$queryins = "insert into budget_qtyhd (Project_code, costcenter_code, BQ_FromDate, BQ_ToDate, BQ_Remark) 
						values (".GetSQLValueString($project_code,'text').",".GetSQLValueString($costcent_code,'text').",
						".GetSQLValueString(formatDate($fromdate),'text').",".GetSQLValueString(formatDate($todate),'text').",
						".GetSQLValueString($remark,'text').") ";
			$resins = mysql_query($queryins, $connection);
			$message = mysql_error();
			$bqId = mysql_insert_id();
		}
		else {
			$queryup = "update budget_qtyhd set Project_code = ".GetSQLValueString($project_code,'text').", 
						costcenter_code = ".GetSQLValueString($costcent_code,'text').", 
						BQ_FromDate = ".GetSQLValueString(formatDate($fromdate),'text').", 
						BQ_ToDate = ".GetSQLValueString(formatDate($todate),'text').",
						BQ_Remark = ".GetSQLValueString($remark,'text')."
						where BQ_Id = $bqId ";
			$resup = mysql_query($queryup, $connection);
			$message = mysql_error();
		}
		if ($bqId != ''){
			$querydt = "select item_code from Budget_ItemsTemplate
						where item_code not in (select item_code from budget_qtydt where bq_id = '$bqId')";
			$resdt = mysql_query($querydt, $connection);
			while ($rowdt = mysql_fetch_array($resdt)){
				$insdt = "insert into budget_qtydt(bq_id, item_code) values ($bqId,'$rowdt[0]') ";
				$resinsdt = mysql_query($insdt, $connection);
			}
		}
	}
}

if ($mode == 'deleteBudget' ){
	
	fbegin();
	$querydt = "delete  from  budget_qtydt where BQ_Id = $bqId ";
	$resdt = mysql_query($querydt, $connection);
	if ($resdt){
		$query = "delete  from budget_qtyhd where BQ_Id = $bqId ";
		$res = mysql_query($query);
		if ($res){
			fcommit();
		} else {
			$message = mysql_error();
			frollback();
		}
		
	} else {
		$message = mysql_error();
		frollback();
	}
	
	header("Location:./Form_SearchSavedQtyBudget.php?message=$message");
	exit;
}

if ($mode == 'deleteBudgetdt'){
	$message = "";
		
	$bqdt_Id = $_GET['bqdt_Id'];
	
	$querydel = "delete from budget_qtydt where bqdt_Id = $bqdt_Id ";
	$resdel = mysql_query($querydel, $connection);
	if (!($resdel)) $message = mysql_error();
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	getQtyBudgetDetails($bqId, $perpage, $startrange, $connection);
	exit;
}

if ($mode == 'addQty'){
	$bqdt_Id = $_GET['bqdt_Id'];
	$qty	 = $_GET['qty'];
	
	if ($qty == '') $qty = 0;
	
	$query = "update budget_qtydt set qty = '$qty' where bqdt_Id = $bqdt_Id ";
	$res = mysql_query($query, $connection);
	if (!($resdel)) $message = mysql_error();
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	getQtyBudgetDetails($bqId, $perpage, $startrange, $connection);
	exit;
	
}

if ($mode == 'RefreshPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getQtyBudgetDetails($bqId, $perpage, $startrange, $connection);
	exit;
	
}
if ($mode == 'SetNextPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getQtyBudgetDetails($bqId, $perpage, $startrange, $connection);
	exit;
	
}
if ($mode == 'SetPrvPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getQtyBudgetDetails($bqId, $perpage, $startrange, $connection);
	exit;
	
}
 
echo "<form name='bqAction' method='POST' action='Form_BudgetQuantity.php'>
	<input type='hidden' name='message' value='$message'>	
	<input type='hidden' name='ShowReport' value='$ShowReport'>	
	<input type='hidden' name='bqId' value='$bqId'>	
</form>";
echo "<script>document.bqAction.submit();</script>";

function formatDate($date){
	if($date !=''){
		$ndate = explode('/', $date);
		$date = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	}
	return $date;
	
}

function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}	
?>