<?php
session_start();
$username = $_SESSION['staff'];

include ("connect.php");
	$slipID = $_POST["slipID"];
	$month = $_POST["month"];
	$startdate = $_POST["startdate"];
	$enddate = $_POST["enddate"];
	$workingday = $_POST["workingday"];
	$name = $_POST["name"];
	$basicsalary = $_POST["basicsalary"];
	$epfrate1 = $_POST["epfe"];
	$epfrate2 = $_POST["epfc"];
	$socso1 = $_POST["socsoe"];
	$socso2 = $_POST["socsoc"];
	$pcb = $_POST["pcb"];
	$grosspay = $_POST["grosspay"];
	$allowance1 = $_POST["allowance1"];
	$allowance2 = $_POST["allowance2"];
	$allowance3 = $_POST["allowance3"];
	$staffNoPayHour = $_POST["staffNoPayHour"];
	$leaveRM = $_POST["leaveRM"];
	$netpay = $_POST["netpay"];
	
	if($_POST["submit"] == "submit")
	{
	$result = mysql_query("INSERT INTO salaryslip(slipNo, month, startdate, enddate, workingday, name, basicsalary, epfrate1, epfrate2, socso1, socso2, pcb, grosspay, allowance1, allowance2, allowance3, staffNoPayHour, leaveRM, netpay)
	VALUES
	('$slipID', '$month', '$startdate', '$enddate', '$workingday', '$name', '$basicsalary', '$epfrate1', '$epfrate2', '$socso1', '$socso2', '$pcb', '$grosspay', '$allowance1', '$allowance2', '$allowance3', '$staffNoPayHour', '$leaveRM', '$netpay')");
	
	
	$result1 = mysql_query("UPDATE staffleave SET leaveUpdate = 'YES' WHERE staffName = '$name'");
	
	
	$db = mysql_close();
	echo "<script language='javascript'>alert('Salary Slip $slipID is create.')</script>";
	echo "<script language='javascript'>window.location='printsalary.php?slipID=".$slipID."'</script>";
}
	
if($_POST["cancel"] == "cancel")
{
	echo "<script language='javascript'>window.location='salarylist.php'</script>";
}
if($_SESSION['staff']!= null)
{
?>
	
<html>
<head>
<title>Daily Bookstore - Management System</title>
<!--include css file-->
<link href="bookstaff.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
</head>

<body>

<?php
include ("stafffunctionbar.php");
?>
<center>
<h2>Daily Bookstore Management - Calculate Salary</h2>
</center>

<div id="mainpage">
<table width="90%">
<tr>
<td width="15%" valign="top">
<fieldset>
<legend><img src="Picture/salary.gif" width="30" height="30">Salary</legend>
<table>
<td valign="top">
<a href="salarylist.php"><img src="Picture/salary.gif" width="30" height="30" alt="EPF"><a href="salarylist.php">Salary List</a><br/>
<br/>
<a href="addsalary.php"><img src="Picture/addsalary.jpg" width="30" height="30" alt="EPF"><a href="addsalary.php">Add Salary</a><br/>
<br/>
<a href="editepf.php"><img src="Picture/epf.gif" width="30" height="30" alt="EPF"><a href="editepf.php">EPF</a><br/>
<br/>
<a href="socsolist.php"><img src="Picture/socso.gif" width="30" height="30" alt="Socso"><a href="socsolist.php">Socso</a><br/>
</td>
</tr>
</table>
</fieldset>
</td>

<td width="75%">
<center>
<form name="salarypay" method="post">
<fieldset style="width:80%">
<legend><img src="Picture/salary.gif" width="30" height="30">Calculate Salary</legend>

<table align="center">
<tr>
<td>Salary No: </td>
<td>
<?php
include("salaryID.php");
$slipID = "$index";
echo "<input type='hidden' name='slipID' ID='slipID' value='$slipID'>$slipID";
?>
</td>
</tr>

<tr>
<td>Month: </td>
<td><select name="month" id="month" onchange="months()">
	<option value="0">Choose One</option>
	<option value="1">January</option>
	<option value="2">February</option>
	<option value="3">March</option>
	<option value="4">April</option>
	<option value="5">May</option>
	<option value="6">June</option>
	<option value="7">July</option>
	<option value="8">August</option>
	<option value="9">September</option>
	<option value="10">October</option>
	<option value="11">November</option>
	<option value="12">December</option>
	</select>
</td>
</tr>

<tr>
<td>Period: </td>
<td><input type="text" name="startdate" id="startdate" readonly> To <input type="text" name="enddate" id="enddate" readonly></td>
</tr>

<tr>
<td>Month Working Day: </td>
<td><input type="text" name="workingday" id="workingday" readonly></td>
</tr>

<tr>
<td colspan="2"><br/></td>
</tr>

<tr>
<td>Employee Name: </td>
<td><input type="text" name="name" ID="name" size="25" onfocus="passstaff()" onblur="rate()"></td>
</tr>
<script language="Javascript">
$().ready(function() {
	$("#name").autocomplete("staffauto.php", {
		width: 260,
		matchContains: true,
		//mustMatch: true,
		//minChars: 0,
		//multiple: true,
		//highlight: false,
		//multipleSeparator: ",",
		selectFirst: false
	});
});

var xmlHttp;
function GetXmlHttpObject(){
    
    var xmlHttp=null;
    try {
        
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     } catch (e) {
         
     //Internet Explorer
        try {
        
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
        
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    return xmlHttp;
}

function months(){
	
    xmlHttp=GetXmlHttpObject();
    if(xmlHttp==null) {
        
        alert ("Browser does not support HTTP Request");
        return;
    }
    var month=document.getElementById("month");
	//alert(name.value);
    var url="getmonth.php?";
    var n="value="+month.value;
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    xmlHttp.send(n);

function stateChanged() { 
    
    if (xmlHttp.readyState=="4" || xmlHttp.readyState=="200"){
    
        var str_text=xmlHttp.responseText;
        if (str_text != ""){
            //alert("没有这个名字的数据");
			//}
        //else {
            
            str_text = str_text.split('|');
			
            document.getElementById("workingday").value = str_text[0];
			document.getElementById("startdate").value = str_text[1];
			document.getElementById("enddate").value = str_text[2];
			//document.getElementById("epfc").value = ((+(str_text[0]) * +(str_text[1])).toFixed(2));
			//document.getElementById("subtotal"+p+"").value = ((+(str_text[1])).toFixed(2));
			//document.getElementById("total").value = ((+(document.getElementById("total").value) + +(str_text[1])).toFixed(2));
        }
		}
    } 
}

function passstaff(){
	
    xmlHttp=GetXmlHttpObject();
    if(xmlHttp==null) {
        
        alert ("Browser does not support HTTP Request");
        return;
    }
    var name=document.getElementById("name");
	//alert(name.value);
    var url="getstaff.php?";
    var postdate="value="+name.value;
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    xmlHttp.send(postdate);

function stateChanged() { 
    
    if (xmlHttp.readyState=="4" || xmlHttp.readyState=="200"){
    
        var str_text=xmlHttp.responseText;
        if (str_text != ""){
            //alert("没有这个名字的数据");
			//}
        //else {
            
            str_text = str_text.split('|');
			
            document.getElementById("basicsalary").value = ((+(str_text[0])).toFixed(2));
			//document.getElementById("staffHour").value = str_text[1];
			//document.getElementById("staffPayLeave").value = str_text[3];
			document.getElementById("pcbrate").value = str_text[2];
			//document.getElementById("staffNP").value = ((+(str_text[0]) / +(str_text[1]) / +(document.getElementById("workingday").value)).toFixed(2));
			document.getElementById("staffNoPayHour").value = str_text[4];
			document.getElementById("leaveRM").value = ((+(str_text[0]) / +(str_text[1]) / +(document.getElementById("workingday").value) * +(str_text[4])).toFixed(2));
			document.getElementById("grosspay").value = ((+(str_text[0]) - ((+(str_text[0]) / +(str_text[1]) / +(document.getElementById("workingday").value) * +(str_text[4])))).toFixed(2));
			//document.getElementById("epfc").value = ((+(str_text[0]) * +(str_text[1])).toFixed(2));
			//document.getElementById("subtotal"+p+"").value = ((+(str_text[1])).toFixed(2));
			//document.getElementById("total").value = ((+(document.getElementById("total").value) + +(str_text[1])).toFixed(2));
        }
		}
    } 
}

function rate(){
	
    xmlHttp=GetXmlHttpObject();
    if(xmlHttp==null) {
        
        alert ("Browser does not support HTTP Request");
        return;
    }
    var basicsalary=document.getElementById("basicsalary");
	//alert(name.value);
    var url="getvalue.php?";
    var postdate="value="+basicsalary.value;
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    xmlHttp.send(postdate);

function stateChanged() { 
    
    if (xmlHttp.readyState=="4" || xmlHttp.readyState=="200"){
    
        var str_text=xmlHttp.responseText;
        if (str_text != ""){
            //alert("没有这个名字的数据");
			//}
        //else {
            
            str_text = str_text.split('|');
			
            //document.getElementById("basicpay").value = ((+(str_text[0])).toFixed(2));
			document.getElementById("epfrate1").value = (+(str_text[0])).toFixed(2);
			document.getElementById("epfrate2").value = (+(str_text[1])).toFixed(2);
			document.getElementById("epfe").value = ((+(document.getElementById("grosspay").value) * +(str_text[0])).toFixed(2));
			document.getElementById("epfc").value = ((+(document.getElementById("grosspay").value) * +(str_text[1])).toFixed(2));
			document.getElementById("socsoe").value = ((+(str_text[2])).toFixed(2));
			document.getElementById("socsoc").value = ((+(str_text[3])).toFixed(2));
			document.getElementById("pcb").value = ((+(document.getElementById("grosspay").value) * +(document.getElementById("pcbrate").value)).toFixed(2));
			document.getElementById("netpay").value = ((+(document.getElementById("basicsalary").value) - +(document.getElementById("epfe").value)  - +(document.getElementById("pcb").value)- +(document.getElementById("socsoe").value) + +(document.getElementById("allowance1").value) + +(document.getElementById("allowance2").value) + +(document.getElementById("allowance3").value) - +(document.getElementById("leaveRM").value)).toFixed(2));
			//document.getElementById("leave").value = ((+(str_text[2])).toFixed(2));
			//document.getElementById("total").value = ((+(document.getElementById("total").value) + +(str_text[1])).toFixed(2));
        }
		}
    } 
}
//(allowance1.value,allowance2.value,allowance3.value,grosspay.value,grosspay,epfrate1.value,epfe,epfrate2.value,epfc,socso1.value,socsoe,socso2.value,socsoc,pcbrate.value,pcb,netpay
function addallowance(allow1,allow2,allow3,gross,grosspay,epf1,epfef,epf2,epfcf,soce,pcbv,pcbf,netpayf,leavepay){
var d = grosspay;
d.value = (+(gross) + +(allow1) + +(allow2) + +(allow3) - +(leavepay)).toFixed(2);

var e = epfef;
e.value = ((+(gross) + +(allow1) + +(allow2) + +(allow3) - +(leavepay)) * +(epf1)).toFixed(2);

var f = epfcf;
f.value = ((+(gross) + +(allow1) + +(allow2) + +(allow3) - +(leavepay)) * +(epf2)).toFixed(2);

var i = pcbf;
i.value = ((+(gross) + +(allow1) + +(allow2) + +(allow3) - +(leavepay)) * +(pcbv)).toFixed(2);

var j = netpayf;
j.value = ((+(gross) + +(allow1) + +(allow2) + +(allow3) - +(leavepay)) - ((+(gross) + +(allow1) + +(allow2) + +(allow3) - +(leavepay)) * +(epf1)) - soce - ((+(gross) + +(allow1) + +(allow2) + +(allow3) - +(leavepay)) * +(pcbv))).toFixed(2);
}

</script>

<tr>
<td>Basic Salary: </td>
<td>RM <input type="text" name="basicsalary" ID="basicsalary" size="20" value='' readonly></td>
</tr>

<tr>
<td colspan="2"><br/></td>
</tr>

<?php
	$result1 = mysql_query("SELECT * FROM epf WHERE typeepf = 'Staff Rate'");
	 
	$row1 = mysql_fetch_array($result1);
	
	$result2 = mysql_query("SELECT * FROM epf WHERE typeepf = 'Owner Rate'");
	 
	$row2 = mysql_fetch_array($result2);
	
	$result2 = mysql_query("SELECT * FROM epf WHERE typeepf = 'Owner Rate'");
	 
	$row2 = mysql_fetch_array($result2);

echo "<tr>";
echo "<td colspan='2'>Allowance</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Transport: </td>";
echo "<td>RM <input type='text' name='allowance1' ID='allowance1' value='0' size='20' onchange='addallowance(allowance1.value,allowance2.value,allowance3.value,basicsalary.value,grosspay,epfrate1.value,epfe,epfrate2.value,epfc,socsoe.value,pcbrate.value,pcb,netpay,leaveRM.value)'></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Handphone: </td>";
echo "<td>RM <input type='text' name='allowance2' ID='allowance2' value='0' size='20' onchange='addallowance(allowance1.value,allowance2.value,allowance3.value,basicsalary.value,grosspay,epfrate1.value,epfe,epfrate2.value,epfc,socsoe.value,pcbrate.value,pcb,netpay,leaveRM.value)'></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Meal: </td>";
echo "<td>RM <input type='text' name='allowance3' ID='allowance3' value='0' size='20' onchange='addallowance(allowance1.value,allowance2.value,allowance3.value,basicsalary.value,grosspay,epfrate1.value,epfe,epfrate2.value,epfc,socsoe.value,pcbrate.value,pcb,netpay,leaveRM.value)'></td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='2'><br/></td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='2'>No Pay Leave</td>";
echo "</tr>";

echo "<tr>";
echo "<td>No Pay Leave Hour(s): </td>";
echo "<td>RM <input type='text' name='staffNoPayHour' ID='staffNoPayHour' value='0' size='20' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td>No Pay Leave: </td>";
echo "<td>RM ";
echo "<input type='text' name='leaveRM' ID='leaveRM' value='0' size='20'>";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Gross Pay: </td>";
echo "<td>RM <input type='text' name='grosspay' ID='grosspay' value='0' size='20' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='2'><br/></td>";
echo "</tr>";

echo "<tr>";
echo "<td>EPF (Employee): </td>";
echo "<td>RM <input type='hidden' name='epfrate1' id='epfrate1' value=''><input type='text' name='epfe' ID='epfe' size='20' value='0 'readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td>EPF (Company): </td>";
echo "<td>RM <input type='hidden' name='epfrate2' id='epfrate2' value=''><input type='text' name='epfc' ID='epfc' size='20' value='0' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Socso (Employee): </td>";
echo "<td>RM <input type='hidden' name='socso1' id='socso1' value=''><input type='text' name='socsoe' ID='socsoe' size='20' value='0' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Socso (Company): </td>";
echo "<td>RM <input type='hidden' name='socso2' id='socso2' value=''><input type='text' name='socsoc' ID='socsoc' size='20' value='0' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td>PCB: </td>";
echo "<td>RM <input type='hidden' name='pcbrate' id='pcbrate' value=''><input type='text' name='pcb' ID='pcb' size='20' value='0' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='2'><br/></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Net Pay: </td>";
echo "<td>RM <input type='text' name='netpay' ID='netpay' value='0' size='20' readonly></td>";
echo "</tr>";
?>

<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="submit">
<input type="submit" name="cancel" value="cancel">
</td>
</tr>

</table>
</fieldset>
</form>
</center>
</body>
</html>
<?php
}else{
	echo "<script language='javascript'>alert ('Please login.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>