by lostsoul82 » Thu May 27, 2010 2:57 pm
I played around with the approval, rejected, cancelled etc and found out that it's not a bug and do not intend to change any more coding.
However, I fixed my original code with some bug fixes.
I have added Ricardo's code in but I still not able to fix the bug if you apply for next year's leave.
$query="SELECT * FROM hs_hr_employee_leave_quota where employee_id = $employee_id and leave_type_id = 'LTY001' and year = $currentyear";
this line of code will allow the database to search for employee's current leave for the current year.
Since i can't transform javascript to php coding, it would seem hard for me to solve the bug.
Which means if the user select leave from 28/12/2010 - 01/03/2011 they would minus the leave balance from the employee in the current year
and the application would work.
But if user tries to apply leave from 01/01/2011 when the current year is 2010 there would be problems.
It's not a major problem for me, I'm still able to bear with it.
Hoping to hear more opinions and solutions.
Hope that helps. =)
CODE FIXES:
- Does not allow user to select Sat, Sun or PH
- PH means that I can define any weekdays or weekends as public holiday.
Example if Labour Day falls on 1st of May and is a Saturday, I will add Monday(3rd of May) as a public holiday (it's my company policy, you can define your own)
When user tries to apply on leave on 3rd May, it will prompt user that it is a public holiday and user cannot select that.
This is to solve the bug if user select Sat or Sun when he applies for leave, the leave calculation would be inaccurate.
Just Insert this code in \OrangeHRM\2.5.0.2\htdocs\orangehrm-2.5.0.2\templates\leave\leaveApply.php line 131
CODE TO INSERT:
//Added by IT Admin Ryan : These code is to calculate the number of days applied by employee minus PH,Sat,Sun
//Add these codes in \OrangeHRM\2.5.0.2\htdocs\orangehrm-2.5.0.2\templates\leave\leaveApply.php line 131
//with the number of leave that he is currently having, if lesser will prompt error message.
//variables to store PH, no_of_days_alloted and leave_taken
var holidays = new Array();
var no_of_days_allotted;
var leave_taken;
<?php
//get employee_id, mutltiply by 1 to remove leading zeros
$employee_id = $_SESSION['empID'] * 1;
if ($employee_id != NULL) { //Added by me cos if i didn't add this when admin try assign leave there would be errors
$username="root"; //username root
$password= NULL; //using default password of database which is NULL
$database="hr_mysql";
mysql_connect("127.0.0.1",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM hs_hr_holidays";
mysql_query($query); //Extract SQL query from database
$result=mysql_query($query); //put contents of query into an array $result
$num=mysql_num_rows($result); //$num counts the number of rows in the query
// for loop to extract PH date to array $holidays[] and copy variable from PHP to javascript
$i=0;
while ($i < $num) {
$holidays[]=mysql_result($result,$i,"date"); ?>
var j = <?= $i ?>;
holidays[j] = "<?= $holidays[$i] ?>";
<?php $i++;
}
?>
//workdays is number of working days after minus off sat/sun and PH
var workdays = calcBusinessDays(date1,date2,holidays);
//Show error message if user selected Sat / Sun / PH
if (workdays == -2)
{
err = true;
msg += " - <?php echo $lang_Leave_Error_RestDay; ?>\n" //Define our own error message
}
//Check this condition only when Annual Leave is selected
if (document.frmLeaveApp.sltLeaveType.value == "LTY001") {
<?php
//calculates the current year
$currentyear = date('Y');
//LTY001 is my annual leave
$query="SELECT * FROM hs_hr_employee_leave_quota where employee_id = $employee_id and leave_type_id = 'LTY001' and year = $currentyear";
mysql_query($query);
$result=mysql_query($query);
mysql_close();
//We do not have to use the loop while $i < $num as it contains only one row
//this 2 variables is used to calculate remaining leave balance of employee (no_of_days_alloted - leave_taken)
$no_of_days_allotted=mysql_result($result,0,"no_of_days_allotted");
$leave_taken=mysql_result($result,0,"leave_taken");
?>
//convert php coding to javascript
no_of_days_allotted = "<?= $no_of_days_allotted ?>";
leave_taken = "<?= $leave_taken ?>";
//reject the leave application and pop up error message if leave balance is not enough
if ((no_of_days_allotted - leave_taken) < workdays) {
err = true;
msg += " - <?php echo $lang_Leave_Error_LeaveExceeded; ?>\n" //Define our own error message
}
} //close Annual Leave Condition Loop
<?php } ?> //End If Loop for PHP coding if ($employee_id != NULL)
//This function calculates business dates by subtracting sat and sun and PH
function calcBusinessDays(dDate1, dDate2, dHolidays) { // input given as Date objects
//This formula helps to substract sat and sun with To and From Dates
var iWeeks, iDateDiff, iAdjust = 0;
var FormattedDate1 = new Date(dDate1.replace(/-/g,"/"));
var FormattedDate2 = new Date(dDate2.replace(/-/g,"/"));
if (FormattedDate2 < FormattedDate1) return -1; // error code if dates transposed
var iWeekday1 = FormattedDate1.getDay(); // day of week
var iWeekday2 = FormattedDate2.getDay();
if (iWeekday1 == 6 || iWeekday1 == 0 || iWeekday2 == 6 || iWeekday2 == 0) return -2; //if date falls on Sat or Sun return error
iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
// calculate difference in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((FormattedDate2.getTime() - FormattedDate1.getTime()) / 604800000)
if (iWeekday1 <= iWeekday2) {
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
}
else {
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}
iDateDiff -= iAdjust // take into account both days on weekend
//this formula below helps to calculate public holidays from the To and From Dates using strToDate
//If PH falls on Saturday or Sunday it is not subtracted, I assume that we add in the PH manually in the leave system as weekday
var Formatholidays;
var Dformat = YAHOO.OrangeHRM.calendar.format;
var Strholidays;
var k=0;
for (k=0; k < dHolidays.length ;k++)
{
Strholidays = strToDate(dHolidays[k], Dformat);
Formatholidays = new Date(dHolidays[k].replace(/-/g,"/"));
if (strToDate(dDate1,Dformat) == Strholidays || strToDate(dDate2,Dformat) == Strholidays) return -2;//return error if PH selected
if (strToDate(dDate1,Dformat) <= Strholidays && Strholidays <= strToDate(dDate2,Dformat)
&& Formatholidays.getDay() != 6 && Formatholidays.getDay() != 0) iDateDiff--;
}
return (iDateDiff + 1); // add 1 because dates are inclusive
}
Insert this code in \OrangeHRM\2.5.0.2\htdocs\orangehrm-2.5.0.2\language\default\language_default_full.php line 1834
CODE TO INSERT:
$lang_Leave_Error_LeaveExceeded = "You have exceeded your leave quota";
$lang_Leave_Error_RestDay = "You have selected Sat / Sun or PH ";