<?php

session_start();

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

include("config.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'];
$fromitem	 	= $_POST['fromitem'];
$toitem			= $_POST['toitem'];

if ($mode == 'generateBudget'){
	$ErrorExists = 0;
	$message = '';
	
	$project_code = "".$project_code;
	$costcent_code = "".$costcent_code;
	
	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 ($fromitem != '') {
		$sub_query = " Select Count(Item_Code) From Items ".
					 " Where Item_Code = '".trim(strtoupper($fromitem))."'";
		$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 Item Code.';
		}
	}
	
	if ($toitem != '') {
		$sub_query = " Select Count(Item_Code) From Items ".
					 " Where Item_Code = '".trim(strtoupper($toitem))."'";
		$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 Item Code.';
		}
	}
	
	if ($ErrorExists == 0){
		$queryitem = "select item_code
						from items
						where 1=1 and if ('$fromitem' = '', 1=1, item_code between '$fromitem' and '$toitem') 
						order by item_code";
		$resitem = mysql_query($queryitem, $connection);
		
		$project_code1 = $project_code;
		$costcent_code1 = $costcent_code;
		
		if ($project_code == '') $project_code = "NULL";
		else $project_code = "'".$project_code."'";

		if ($costcent_code == '') $costcent_code = "NULL";
		else $costcent_code = "'".$costcent_code."'";

		$ndate = explode('/', $fromdate);
		$fromdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];

		$ndate = explode('/', $todate);
		$todate = $ndate[2]."-".$ndate[1]."-".$ndate[0];

		while ($rwitem = mysql_fetch_array($resitem)){
			$querychk = "select 1 from qty_budget where item_code = '$rwitem[0]' and if('$project_code1'='', project_code is null, project_code = '$project_code1')
							and if('$costcent_code1'='', costcent_code is null, costcent_code = '$costcent_code1') ";
			$reschk = mysql_query($querychk, $connection);
			$numchk = mysql_num_rows($reschk);
			if ($numchk == 0){
				$queryins = "insert into qty_budget(sdate, edate, item_code, project_code, costcent_code) values 
							('$fromdate', '$todate', '$rwitem[0]', $project_code, $costcent_code)";
				
				$resins = mysql_query($queryins, $connection);
			}
			
		}	
	}
	if ($message == '')
		header('Location:Form_BudgetQuantity.php');
	else header("Location:Form_BudgetQuantity.php?message=$message");
	exit;
}

if ($mode == 'updateBudget'){
	$return_arr = array();
	$message = "";
	
	$ErrorExists = 0;
	
	$budget_id	  	= $_GET['budget_id'];
	$itemcode	 	= $_GET['itemcode'];
	$budgetdesc 	= $_GET['budgetdesc'];
	
	$month1 = $_GET['month1'];
	$month2 = $_GET['month2'];
	$month3 = $_GET['month3'];
	
	$month4 = $_GET['month4'];
	$month5 = $_GET['month5'];
	$month6 = $_GET['month6'];
	
	$month7 = $_GET['month7'];
	$month8 = $_GET['month8'];
	$month9 = $_GET['month9'];
	
	$month10 = $_GET['month10'];
	$month11 = $_GET['month11'];
	$month12 = $_GET['month12'];
	
	$projectcode	= $_GET['projectcode'];
	$costcentcode 	= $_GET['costcentcode'];
	$startdate 		= $_GET['startdate'];
	$enddate 		= $_GET['enddate'];
		
	$projectcode = "".$projectcode;				
	$costcentcode = "".$costcentcode;
	
	if ($costcentcode != '') {
		$sub_query = " Select Count(Costcent_code) From Costcent ".
					 " Where Costcent_code = '".$costcentcode."'";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row = mysql_fetch_row($sub_result);
		if ($sub_row[0] == 0) {
			$ErrorExists = 1;
			$message = 'Update not allowed. Please check for Costcenter Code.';
		}
	}
	
	if ($projectcode != '') {
		$sub_query = " Select Count(Project_Code) From Project ".
					 " Where Project_Code = '".$projectcode."'";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row = mysql_fetch_row($sub_result);
		if ($sub_row[0] == 0) {
			$ErrorExists = 1;
			$message = 'Update not allowed. Please check for Project Code.';
		}
	}
	
	if ($itemcode != '') {
		$sub_query = " Select Count(Item_Code) From Items ".
					 " Where Item_Code = '".trim(strtoupper($itemcode))."'";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row = mysql_fetch_row($sub_result);
		if ($sub_row[0] == 0) {
			$ErrorExists = 1;
			$message = 'Update not allowed. Please check for Item Code.';
		}
	}
	
	if ($month1 == '') $month1 = 0;
	if ($month2 == '') $month2 = 0;
	if ($month3 == '') $month3 = 0;
	
	if ($month4 == '') $month4 = 0;
	if ($month5 == '') $month5 = 0;
	if ($month6 == '') $month6 = 0;
	
	if ($month7 == '') $month7 = 0;
	if ($month8 == '') $month8 = 0;
	if ($month9 == '') $month9 = 0;
	
	if ($month10 == '') $month10 = 0;
	if ($month11 == '') $month11 = 0;
	if ($month12 == '') $month12 = 0;
	
	$ndate = explode('/', $startdate);
	$startdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];

	$ndate = explode('/', $enddate);
	$enddate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	
	if ($ErrorExists == 0){
		$chk = "select 1 from qty_budget where budget_id <> $budget_id and item_code = '$itemcode' and if('$projectcode' = '', project_code is null, project_code = '$projectcode') 
				and if('$costcentcode' = '', costcent_code is null, costcent_code = '$costcentcode') ";
		$res = mysql_query($chk, $connection);
		$numr = mysql_num_rows($res);
		
		if ( $numr > 0){
			$message = "Item Code Already Exists!";		
		} else {
			
			if ($projectcode == '') $projectcode = "NULL";
			else $projectcode = "'".$projectcode."'";

			if ($costcentcode == '') $costcentcode = "NULL";
			else $costcentcode = "'".$costcentcode."'";
			
			if ($budgetdesc == '') $budgetdesc = "NULL";
			else $budgetdesc = "'".$budgetdesc."'";
			
			$update = "update qty_budget set sdate = '$startdate',  edate = '$enddate', item_code = '$itemcode', budget_desc = $budgetdesc,
							project_code = $projectcode, costcent_code = $costcentcode, 
							qty_month1 = $month1, qty_month2 = $month2, qty_month3 = $month3, qty_month4 = $month4, qty_month5 = $month5, qty_month6 = $month6,
							qty_month7 = $month7, qty_month8 = $month8, qty_month9 = $month9, qty_month10 = $month10, qty_month11 = $month11, qty_month12 = $month12
						where budget_id = $budget_id ";
			$resup = mysql_query($update);	
			if (!$resup)
				$message = mysql_error();
		}	
	
	}
	$row_array['message'] = $message;
	array_push($return_arr,$row_array);
	echo json_encode($return_arr);
	exit;
}

if ($mode == 'deleteBudget'){
	$message = "";
		
	$budget_id	  	= $_GET['budget_id'];
	
	$querydel = "delete from qty_budget where budget_id = $budget_id ";
	$resdel = mysql_query($querydel, $connection);
	if (!($resdel)) $message = mysql_error();
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	getQtyBudget($perpage, $startrange, $connection);
	exit;
}

if ($mode == 'RefreshPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getQtyBudget($perpage, $startrange, $connection);
	exit;
	
}
if ($mode == 'SetNextPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getQtyBudget($perpage, $startrange, $connection);
	exit;
	
}
if ($mode == 'SetPrvPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getQtyBudget($perpage, $startrange, $connection);
	exit;
	
}
?>