<?php

session_start();

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

include("config.inc.php");
include("Form_GridBudget.php");
include("function.inc.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");

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

//get msg
$msg=$_GET['msg'];
$ledger_number=$_GET['ledger_number'];


if (!function_exists('ConvertCurrency')) {
	function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
	{	
		$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";//echo $query;
		$result = mysql_query($query, $connection);
		$row = mysql_fetch_row($result);
		//return $query; 
		return	$row[0];

	}
}

if($msg == 'setamount'){
	$return_arr = array();
	
	$amount	= $_GET['amount'];
	$curr	= $_GET['curr'];
	$base1	= $_GET['base1'];
	$base2	= $_GET['base2'];
	
	$vDate = date('Y-m-d');
	$amountb1 = ConvertCurrency($curr, $base1, $vDate, $amount, $connection);
	$amountb2 = ConvertCurrency($curr, $base2, $vDate, $amount, $connection);
	
	$row_array['amountb1'] = $amountb1;
	$row_array['amountb2'] = $amountb2;
	array_push($return_arr,$row_array);
	echo json_encode($return_arr);
	exit;
}

if($msg == 'generate'){
	
		$fromledger			= $_POST['fromledger'];
		$toledger			= $_POST['toledger'];	
		$fromdate			= $_POST['fromdate'];	
		$todate				= $_POST['todate']; 
		$project_code1		= $_POST['fromproject']; 
		$cost_center_code1	= $_POST['fromcostcent']; 
		$debit_credit1		= $_POST['debit_credit1']; 
		$detailgroup 		= $_POST['detailgroup']; 
		
		$ndate = explode('/', $fromdate);
		$fromdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];

		$ndate = explode('/', $todate);
		$todate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
		
		$project_code1 		= "".$project_code1;
		$cost_center_code1 	= "".$cost_center_code1;
		
		if ( $project_code1 == '' ) $project_code1= "NULL";
		else $project_code1 = "'".$project_code1."'";
		
		if ( $cost_center_code1 == '' ) $cost_center_code1= "NULL";
		else $cost_center_code1 = "'".$cost_center_code1."'";
		
		if ( $debit_credit1 == '' ) $debit_credit1= "NULL";
		else $debit_credit1 = "'".$debit_credit1."'";
		
		$flag='insert';
		
		$query_ledger_form="select ledger_number,project_code,costcenter_code from ledger 
							where ledger_number between '$fromledger' AND '$toledger' and if('$detailgroup'='' , 1=1 , ledger_grpdet='$detailgroup' ) 
							order by ledger_number	";
		//echo $query_ledger_form;exit;
		$result_ledger_form =mysql_query($query_ledger_form,$connection) or die ("Could not find DB");
		
			while($row_ledger=mysql_fetch_row($result_ledger_form)){
				$query_budget="select ledger_number,project_code,costcenter_code from budget";
				$result=mysql_query($query_budget,$connection);
				
				while ($row=mysql_fetch_row($result)){
				
					if($row_ledger[0] == $row[0] && $row_ledger[1] == $row[1] && $row_ledger[2] == $row[2]){
						
						$flag='skip';
						break;
					}
					else{
					
						$flag='insert';
					}
				} 
				if($flag=='insert'){
					$query_insert="insert into budget (Budget_Sdate, Budget_Edate, Ledger_number, Project_Code,Budget_DBCD, CostCenter_Code, Budget_date)
								values ('$fromdate','$todate','$row_ledger[0]',$project_code1,$debit_credit1,$cost_center_code1, now())";// echo $query_insert; exit;
					$result_insert=mysql_query($query_insert,$connection);
							
				}
			
			}
			//exit;
		header('Location:form_budget.php');
		exit;
	}

	
if($msg =='updateBudget'){
	$return_arr = array();
	$message = "";
	
	$ledgercode		= $_GET['ledgercode'];
	
	$projectcode	= $_GET['projectcode'];
	$costcentcode 	= $_GET['costcentcode'];
	$startdate 		= $_GET['startdate'];
	$enddate 		= $_GET['enddate'];
	
	$budgetCur		= $_GET['budgetCur'];
	$budgetAmnt		= $_GET['budgetAmnt'];
	$budgetAmntB1	= $_GET['budgetAmntB1'];
	$budgetAmntB2	= $_GET['budgetAmntB2'];
	$budgetDBCD		= $_GET['budgetDBCD'];
	
	$month1b1 = $_GET['month1b1'];
	$month2b1 = $_GET['month2b1'];
	$month3b1 = $_GET['month3b1'];
	
	$month4b1 = $_GET['month4b1'];
	$month5b1 = $_GET['month5b1'];
	$month6b1 = $_GET['month6b1'];
	
	$month7b1 = $_GET['month7b1'];
	$month8b1 = $_GET['month8b1'];
	$month9b1 = $_GET['month9b1'];
	
	$month10b1 = $_GET['month10b1'];
	$month11b1 = $_GET['month11b1'];
	$month12b1 = $_GET['month12b1'];
	
	$month1b2 = $_GET['month1b2'];
	$month2b2 = $_GET['month2b2'];
	$month3b2 = $_GET['month3b2'];
	
	$month4b2 = $_GET['month4b2'];
	$month5b2 = $_GET['month5b2'];
	$month6b2 = $_GET['month6b2'];
	
	$month7b2 = $_GET['month7b2'];
	$month8b2 = $_GET['month8b2'];
	$month9b2 = $_GET['month9b2'];
	
	$month10b2 = $_GET['month10b2'];
	$month11b2 = $_GET['month11b2'];
	$month12b2 = $_GET['month12b2'];

	
	$projectcode = "".$projectcode;				
	$costcentcode = "".$costcentcode;
	
	$ErrorExists = 0;
	
	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 ($ledgercode != '') {
		$sub_query = " Select Count(Ledger_number) From ledger ".
					 " Where Ledger_number = '".trim(strtoupper($ledgercode))."'";
		$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 Ledger Code.';
		}
	}
	
	if ($month1b1 == '') $month1b1 = 0;
	if ($month2b1 == '') $month2b1 = 0;
	if ($month3b1 == '') $month3b1 = 0;
	
	if ($month4b1 == '') $month4b1 = 0;
	if ($month5b1 == '') $month5b1 = 0;
	if ($month6b1 == '') $month6b1 = 0;
	
	if ($month7b1 == '') $month7b1 = 0;
	if ($month8b1 == '') $month8b1 = 0;
	if ($month9b1 == '') $month9b1 = 0;
	
	if ($month10b1 == '') $month10b1 = 0;
	if ($month11b1 == '') $month11b1 = 0;
	if ($month12b1 == '') $month12b1 = 0;
	
	if ($month1b2 == '') $month1b2 = 0;
	if ($month2b2 == '') $month2b2 = 0;
	if ($month3b2 == '') $month3b2 = 0;
	
	if ($month4b2 == '') $month4b2 = 0;
	if ($month5b2 == '') $month5b2 = 0;
	if ($month6b2 == '') $month6b2 = 0;
	
	if ($month7b2 == '') $month7b2 = 0;
	if ($month8b2 == '') $month8b2 = 0;
	if ($month9b2 == '') $month9b2 = 0;
	
	if ($month10b2 == '') $month10b2 = 0;
	if ($month11b2 == '') $month11b2 = 0;
	if ($month12b2 == '') $month12b2 = 0;
	
	if ($budgetAmnt == '') 		$budgetAmnt		= 0;
	if ($budgetAmntB1 == '') 	$budgetAmntB1	= 0;
	if ($budgetAmntB2 == '')	$budgetAmntB2	= 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 budget where budget_id <> $budget_id and Ledger_number = '$ledgercode' 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 = "Ledger 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."'";
			
			if ($budgetCur == '') $budgetCur = "NULL";
			else $budgetCur = "'".$budgetCur."'";
			
			if ($budgetDBCD == '') $budgetDBCD = "NULL";
			else $budgetDBCD = "'".$budgetDBCD."'";
			
			$update = "update budget set ledger_number = '$ledgercode', Budget_Description = $budgetdesc, currency = $budgetCur, amount = $budgetAmnt, base1 = $budgetAmntB1 , base2 = $budgetAmntB2, 
						project_code = $projectcode, costCenter_code = $costcentcode, Budget_DBCD = $budgetDBCD,
						Month1_base1 = $month1b1, Month2_base1 = $month2b1, Month3_base1 = $month3b1,
						Month4_base1 = $month4b1, Month5_base1 = $month5b1, Month6_base1 = $month6b1,
						Month7_base1 = $month7b1, Month8_base1 = $month8b1, Month9_base1 = $month9b1,
						Month10_base1 = $month10b1, Month11_base1 = $month11b1, Month12_base1 = $month12b1,

						Month1_base2 = $month1b2, Month2_base2 = $month2b2, Month3_base2 = $month3b2,
						Month4_base2 = $month4b2, Month5_base2 = $month5b2, Month6_base2 = $month6b2,
						Month7_base2 = $month7b2, Month8_base2 = $month8b2, Month9_base2 = $month9b2,
						Month10_base2 = $month10b2, Month11_base2 = $month11b2, Month12_base2 = $month12b2,
						
						Budget_Sdate = '$startdate', Budget_Edate = '$enddate'
						where budget_id =  $budget_id "; 
			$resup = mysql_query($update, $connection);
			if (!$resup)
				$message = mysql_error();
		}
		
	}
	$row_array['message'] = $message;
	array_push($return_arr,$row_array);
	echo json_encode($return_arr);		
	exit;
}

if($msg == 'deleteBudget'){
	$querydel = "delete from budget where budget_id = $budget_id ";
	$resdel = mysql_query($querydel, $connection);
	if (!($resdel)) $message = mysql_error();
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	getBudget($base1, $base2, $Decimalbase1, $Decimalbase2, $perpage, $startrange, $connection);
	
exit;
}


if ($msg == 'RefreshPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getBudget($base1, $base2, $Decimalbase1, $Decimalbase2, $perpage, $startrange, $connection);
	exit;
	
}
if ($msg == 'SetNextPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getBudget($base1, $base2, $Decimalbase1, $Decimalbase2, $perpage, $startrange, $connection);
	exit;
	
}
if ($msg == 'SetPrvPage'){
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	
	echo "[BRK]";
	getBudget($base1, $base2, $Decimalbase1, $Decimalbase2, $perpage, $startrange, $connection);
	exit;
	
}

?>