<?php

session_start();
header("Content-type: text/html; charset=windows-1256");
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");
include("Form_House_Function.php");
include("Functions_UserAccess.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");

$actionmode=$_GET['actionmode'];
$mainaction=$_GET['mainaction'];



if ($actionmode==   'deletestaff_house'){
	$house_code=$_GET['housecode'];
	$del1 = "delete from staff_housedt where house_code = '$house_code';";
	$res1 = mysql_query($del1,$connection);
	
	$del2 = "delete from staff_housegen where house_code = '$house_code';";
	$res2 = mysql_query($del2,$connection);
	
	$del3 = "delete from staff_others where house_code = '$house_code';";
	$res3 = mysql_query($del3,$connection);
	
	$del4 = "delete from staff_housediesel where house_code = '$house_code';";
	$res4 = mysql_query($del4,$connection);
	
	$deletehousequery="delete from staff_house where house_code='$house_code'";
	$deletehousequery=mysql_query($deletehousequery,$connection);
	
	if($deletehousequery) $message = "Staff was Successfully deleted!";
	header("Location:./Form_HouseSearch.php?message=$message");
	exit;
}

if($actionmode==  'edit'){
	
	$edithousequery ="update staff_house set House_name='$housename', House_security=".strformat($security).",
				Hosue_Internet=".strformat($internet).", House_Satellite=".strformat($satellite).", House_generator=".strformat($generator).",
				House_RentDueDate=".strformat($duedate).", House_landLord=".strformat($landlord).", House_rentamount=".strformat($rentamount).",
				House_DieselTank=".strformat($diesel).", house_address=".strformat($adress).", House_ServiceCharges = ".strformat($serviceChrg)." where house_code='$housecode'";
//echo $edithousequery;
	$edithouse=mysql_query($edithousequery,$connection);
	echo 0;
	echo "[BRK]";
	echo "";
	exit;
	
}
if($actionmode==  'insert'){
	$landlord	 = $_GET['landlord'];
	$satellite	 = $_GET['satellite'];
	$diesel		 = $_GET['diesel'];
	$rentamount	 = $_GET['rentamount'];
	$internet	 = $_GET['internet'];
	$adress		 = $_GET['adress'];
	$generator	 = $_GET['generator'];
	$duedate	 = $_GET['duedate'];
	$housename	 = $_GET['housename'];
	$security	 = $_GET['security'];
	$housecode	 = $_GET['housecode'];
	$serviceChrg = $_GET['serviceChrg'];
	
	$search = "select 1 from staff_house where house_code = '$housecode';";
	$res = mysql_query($search,$connection);
	$codeexists = mysql_num_rows($res);
	
	if($codeexists != 0) {
		echo 1;
		echo "[BRK]";
		echo "$housecode Already Exists!";
		exit;
	}
	$addhouse="insert into staff_house (house_code, House_name, Staff_code, House_security, Hosue_Internet, House_Satellite, House_generator, House_RentDueDate, House_landLord, House_rentamount, 
				House_DieselTank, house_address, House_ServiceCharges) values
			('$housecode',".strformat($housename).",".strformat('').",".strformat($security).",".strformat($internet).",". strformat($satellite).","
			.strformat($generator).",".strformat($duedate).",".strformat($landlord).",".strformat($rentamount).",".strformat($diesel).",".strformat($adress).", ".strformat($serviceChrg).")";
	
	$addnew=mysql_query($addhouse,$connection);
		echo 0;
		echo "[BRK]";
		echo "";
		exit;
}
if($actionmode=='deletestaff_in_house'){
	$house_code=$_GET['house_code'];
	$staff_code=$_GET['staff_code'];
	
	$deletestaffquery="delete from staff_housedt where staff_code='$staff_code' ";
	$deletestaff=mysql_query($deletestaffquery,$connection);
	draw_staffinhouse($house_code,'', $connection);
}
if($actionmode== 'getstaff_in_house'){
	$house_code=$_GET['house_code'];
	$staff_code=$_GET['staff_code'];
	//echo $house_code.$staff_code;
	draw_staffinhouse($house_code,$staff_code, $connection);
	exit;
	
}
if($actionmode== 'update_staff_inhouse'){
	$house_code=$_GET['house_code'];
	$newstaffname=$_GET['newstaffname'];
	$newstaffcode=$_GET['newstaffcode'];
	$staffhousetdid=$_GET['staffhousetdid'];
	
	$updatestaffquery="update staff_housedt  set  Staff_code='$newstaffcode', Staff_name='$newstaffname' 
					where Staff_housedtid='$staffhousetdid'";
	$updatestaff=mysql_query($updatestaffquery,$connection);
	draw_staffinhouse($house_code,'', $connection);
	exit;
	
}
if($actionmode== 'insert_staff_inhouse'){
	$house_code=$_GET['house_code'];
	$newstaffname=$_GET['newstaffname'];
	$newstaffcode=$_GET['newstaffcode'];
	$staffhousetdid=$_GET['staffhousetdid'];
	$insertstaffquery="insert into staff_housedt (House_code, Staff_code, Staff_name) values('$house_code','$newstaffcode',".strformat($newstaffname).")";
	$insertstaff=mysql_query($insertstaffquery,$connection);
	draw_staffinhouse($house_code,'', $connection);
	exit;
}
if ($actionmode== 'getgenerator'){
	$housecode=$_GET['housecode'];
	$gen_id=$_GET['gen_id'];
	draw_Generator($housecode,$gen_id, $connection);
	exit;
}
if($actionmode== 'insert_generator'){
	$housecode=$_GET['housecode'];
	$Gen_ServiceDate = $_GET['Gen_ServiceDate'];
	$Gen_remark		 = $_GET['Gen_remark'];
	$gen_hourServ	 = $_GET['gen_hourServ'];
	$gen_nxthourServ = $_GET['gen_nxthourServ'];
	$gen_remainServ	 = $_GET['gen_remainServ'];
	$gen_amount		 = $_GET['gen_amount'];
	$gen_id			 = $_GET['gen_id'];
	$gen_capacity	 = $_GET['gen_capacity'];
	
	$insertgeneratorquery="insert into  staff_housegen (House_code, Gen_ServiceDate, gen_hourServiced, gen_nxtHourServiced, gen_RemainingService, gen_amount, Gen_remark, gen_capacity) values ".
						  " ('$housecode',".strformat($Gen_ServiceDate).",".strformat($gen_hourServ).",".strformat($gen_nxthourServ).",".strformat($gen_remainServ).",".strformat($gen_amount).", 
							".strformat($Gen_remark).", ".strformat($gen_capacity).")";
	$insertgenerator=mysql_query($insertgeneratorquery,$connection);
	draw_Generator($housecode,'', $connection);
	exit;
}
if($actionmode== 'updategenerator'){
	$housecode		 = $_GET['housecode'];
	$Gen_ServiceDate = $_GET['Gen_ServiceDate'];
	$Gen_ServiceDate = strformat($Gen_ServiceDate);
	$gen_hourServ	 = $_GET['gen_hourServ'];
	$gen_nxthourServ = $_GET['gen_nxthourServ'];
	$gen_remainServ	 = $_GET['gen_remainServ'];
	$gen_amount		 = $_GET['gen_amount'];
	$Gen_remark		 = $_GET['Gen_remark'];
	$gen_id			 = $_GET['gen_id'];
	$gen_capacity	 = $_GET['gen_capacity'];

	$updategeneratorquery="update staff_housegen set Gen_ServiceDate=$Gen_ServiceDate, gen_hourServiced = ".strformat($gen_hourServ).", gen_nxtHourServiced = ".strformat($gen_nxthourServ).",
						gen_RemainingService = ".strformat($gen_remainServ).", gen_amount = ".strformat($gen_amount).",	Gen_remark=".strformat($Gen_remark).", gen_capacity = ".strformat($gen_capacity)." 
						where gen_id=$gen_id ";
	
	$updategenerator=mysql_query($updategeneratorquery,$connection);
	draw_Generator($housecode,'', $connection);
	exit;
}
if($actionmode== 'deletegenerator'){
	$housecode=$_GET['housecode'];
	$gen_id=$_GET['gen_id'];
	$deletegeneratorquery="delete from staff_housegen where gen_id='$gen_id'";
	$deletegenerator=mysql_query($deletegeneratorquery,$connection);
	draw_Generator($housecode,'', $connection);
	exit;
}

if ($actionmode =='getdiesel'){
	$shd_id=$_GET['shd_id'];
	$housecode=$_GET['housecode'];
	
	draw_Diesel($housecode,$shd_id, $connection);
	exit;
}
if($actionmode=='getothers'){
	
	$staff_id=$_GET['staff_id'];
	$housecode=$_GET['housecode'];
	draw_others($housecode,$staff_id, $connection);
	exit;
}

if($actionmode=='deleteothers'){
	$staff_id=$_GET['staff_id'];
	$housecode=$_GET['housecode'];
	$deleteothersquery="delete from staff_others where stf_id='$staff_id'";
	$deleteothers=mysql_query($deleteothersquery,$connection);
	draw_others($housecode,'' , $connection);
	exit;
}
if($actionmode=='insertothers'){
	$newdateothers=$_GET['newdateothers'];
	$newtypeothers=$_GET['newtypeothers'];
	$newremarkothers=$_GET['newremarkothers'];
	$housecode=$_GET['housecode'];
	//$stf_id=$_GET['stf_id'];
	$insertothersquery="insert into  staff_others (Description, Date_value, Remark, house_code) 
					values (".strformat($newtypeothers).",".strformat($newdateothers).",".strformat($newremarkothers).",'$housecode')";

	$insertothers=mysql_query($insertothersquery,$connection);
	draw_others($housecode,'' , $connection);
	exit;
}
if($actionmode=='updateothers'){
	$newdateothers=$_GET['newdateothers'];
	$newtypeothers=$_GET['newtypeothers'];
	$newremarkothers=$_GET['newremarkothers'];
	$staff_id=$_GET['staff_id'];
	$housecode=$_GET['housecode'];
	$updateothersquery="update staff_others set Description=".strformat($newtypeothers).", Date_value=".strformat($newdateothers).", 
					Remark=".strformat($newremarkothers)."  where stf_id='$staff_id' ";
	$deleteothers=mysql_query($updateothersquery,$connection);
	draw_others($housecode,'' , $connection);
	exit;
}
if($actionmode=='insertdiesel'){
	
	$housecode=$_GET['housecode'];
	$newshddate=$_GET['newshddate'];
	$newshddremark=$_GET['newshddremark'];
	$newpreviousltr=$_GET['newpreviousltr'];
	$newshdltr=$_GET['newshdltr'];
	$newcurrrentltr=$_GET['newcurrrentltr'];
		
	$insertdieselquery="insert into staff_housediesel (House_code, shd_date, Shd_remark, shd_prvLtr, Shd_ltr, Shd_curremt)
					values('$housecode',".strformat($newshddate).",".strformat($newshddremark).",".strformat($newpreviousltr).",".strformat($newshdltr).",".strformat($newcurrrentltr).")";

	$insertdiesel=mysql_query($insertdieselquery,$connection);
	draw_Diesel($housecode,'', $connection);
	exit;
}
	if($actionmode=='updatediesel'){
	
	$housecode=$_GET['housecode'];
	$newshddate=$_GET['newshddate'];
	$newshddremark=$_GET['newshddremark'];
	$newpreviousltr=$_GET['newpreviousltr'];
	$newshdltr=$_GET['newshdltr'];
	$shd_id=$_GET['shd_id'];
	$newcurrrentltr=$_GET['newcurrrentltr'];
		$updatetdieselquery="update staff_housediesel 
		set  shd_date='$newshddate', Shd_remark=".strformat($newshddremark).", shd_prvLtr=".strformat($newpreviousltr).", 
		Shd_ltr=".strformat($newshdltr).", Shd_curremt=".strformat($newcurrrentltr)." where shd_id='$shd_id' ";


	$updatediesel=mysql_query($updatetdieselquery,$connection);
	draw_Diesel($housecode,'', $connection);
	exit;
}
	if ($actionmode=='deletediesel'){
		$shd_id=$_GET['shd_id'];
		$deletedieselquery="delete from staff_housediesel where shd_id='$shd_id'";
		$deletediesel=mysql_query($deletedieselquery,$connection);
		draw_Diesel($housecode,'', $connection);
		exit;
	}
	
// Electricity Tab
if($actionmode=='insertElectricity'){
	$elect_date 		= $_GET['elect_date'];
	$elect_prevMeter 	= $_GET['elect_prevMeter'];
	$elect_curMeter 	= $_GET['elect_curMeter'];
	$elect_amount 		= $_GET['elect_amount'];
	$housecode			= $_GET['housecode'];
	
	$queryins = "insert into House_Electricity(Electricity_Date, Electricity_PrevMeterReading, Electricity_CurMeterReading, Electricity_Amount, House_code) values ".
				" (".strformat($elect_date).", ".strformat($elect_prevMeter).", ".strformat($elect_curMeter).", ".strformat($elect_amount).", ".strformat($housecode).") ";
	$resins = mysql_query($queryins, $connection);
	draw_electricity($housecode,'', $connection);
	exit;
}
if($actionmode=='updateElectricity'){
	$elect_id 			= $_GET['elect_id'];
	$elect_date 		= $_GET['elect_date'];
	$elect_prevMeter 	= $_GET['elect_prevMeter'];
	$elect_curMeter 	= $_GET['elect_curMeter'];
	$elect_amount 		= $_GET['elect_amount'];
	$housecode			= $_GET['housecode'];
	
	$queryup = "update House_Electricity set  Electricity_Date = ".strformat($elect_date).", Electricity_PrevMeterReading = ".strformat($elect_prevMeter).", 
				Electricity_CurMeterReading = ".strformat($elect_curMeter).", Electricity_Amount = ".strformat($elect_amount)."
				where electricity_id = $elect_id";
	$resup = mysql_query($queryup, $connection);
	draw_electricity($housecode,'', $connection);
	exit;
}	
if($actionmode=='deleteElectricity'){
	$elect_id 	= $_GET['elect_id'];
	$housecode	= $_GET['housecode'];
	
	$querydel = "delete from House_Electricity  where electricity_id = $elect_id";
	$resdel = mysql_query($querydel, $connection);
	draw_electricity($housecode,$elect_id, $connection);
	exit;
}
if($actionmode=='getElectricity' || $actionmode == 'clearElectricity'){
	$elect_id 	= $_GET['elect_id'];
	$housecode	= $_GET['housecode'];
	draw_electricity($housecode,$elect_id, $connection);
	exit;
}


function formatdate($dateval){
	if($dateval != ''){
	$ndate = explode("/", $dateval);
	$dateval = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	$dateval = "'".$dateval."'";
	} else $dateval = "NULL";
	return $dateval;
}
function strformat($str){
	if($str == '') $str = "NULL";
	else $str = "'".ereg_replace("'", "\'", trim($str))."'";
	return $str;
}
?>