// Functions for Timesheet Program
# Created by Mitch Mahan, 11/9/2007
#---------------------------------------------------------------#
# Start 'User' Functions
function addUser($name, $values)
{
include 'db.inc';
$sql = "INSERT INTO `timesheet`.`user` (
`uid` ,
`login` ,
`full_name` ,
`first_name` ,
`last_name` ,
`dob` ,
`hired` ,
`pto_init` ,
`pto_remain`
) VALUES (
NULL , '$values->login', '$values->full_name', '$values->first_name', '$values->last_name', '$values->dob', '$values->hired', '$values->pto', '');";
$result = mysql_query($sql,$connection) or die(mysql_error());
return $result;
}// End addUser Function
function delUser($uid)
{
include 'db.inc';
$sql = "DELETE from `user` where `user`.`uid` = '$uid' LIMIT 1;";
$result = mysql_query($sql,$connection) or die(mysql_error());
return $result;
}// End delUser Function
function updateUser($uid, $values)
{
include 'db.inc';
$sql= "UPDATE `timesheet`.`user` SET
`login` = '$values->login',
`full_name` = '$values->full_name',
`first_name` = '$values->first_name',
`last_name` = '$values->last_name',
`dob` = '$values->dob',
`hired` = '$values->hired',
`pto_init` = '$values->pto',
`pto_remain` = '$values->ptor' WHERE `user`.`uid` ='$values->uid' LIMIT 1 ;";
$result = mysql_query($sql,$connection) or die(mysql_error());
return $result;
}// End updateUser Function
# End 'User' Functions
#---------------------------------------------------------------#
#---------------------------------------------------------------#
# Start 'Get' Functions
#
// Get information for a given user
function getUid($login)
{
include 'db.inc';
$sql = "SELECT uid FROM user WHERE login = '$login' LIMIT 1;";
$sql_result = mysql_query($sql,$connection) or die(mysql_error());
$result = mysql_fetch_object($sql_result);
return $result->uid;
}
function getUserinfo($uid)
{
include 'db.inc';
$sql = "SELECT * FROM user WHERE uid = '$uid' LIMIT 1;";
$sql_result = mysql_query($sql,$connection) or die(mysql_error());
$result = mysql_fetch_object($sql_result);
return $result;
}// End getUserinfo function
// Get information for a given day
function getTimesheet($uid, $day)
{
include 'db.inc';
$sql = "SELECT * FROM timesheets WHERE uid = '$uid' and date = '$day' LIMIT 1;";
$sql_result = mysql_query($sql,$connection) or die(mysql_error());
$result = mysql_fetch_object($sql_result);
#$result = array( 'time_in'=>$in, 'time_out'=>$out, 'hours'=>$total_hours, 'ot'=>$ot, 'pto'=>$pto, 'holiday'=>$holiday );
return $result;
}// End getTimesheet function
// Get information for a given pay week
function getPayperiod($uid, $period_id)
{
include 'db.inc';
$sql = "SELECT * FROM payperiods WHERE uid = '$uid' and $period_id = '$period_id' LIMIT 1;";
$sql_result = mysql_query($sql,$connection) or die(mysql_error());
$result = mysql_fetch_object($sql_result);
return $result;
}// End getPayperiod function
function getWeekstats($uid,$period_id)
{
include 'db.inc';
// Get the totals for each day of the week
$sql="SELECT * FROM `timesheets` WHERE uid ='$uid'AND period_id = '$period_id' ORDER BY `date` LIMIT 7; ";
$result = mysql_query($sql,$connection) or die(mysql_error());
echo '
';
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$day = date('l',strtotime($row['date']));
echo "
| $day |
Hours: {$row['hours']} |
Ot: {$row['ot']} |
";
}
// Get total hours and ot for the payperiod
$total_sql = "SELECT hrs_worked, hrs_otime FROM `payperiods` WHERE period_id = '$period_id' AND uid = '$uid' LIMIT 1 ;";
$sql_result = mysql_query($total_sql,$connection) or die(mysql_error());
$week_row = mysql_fetch_array($sql_result);
// Assign variables
$total_hours = $week_row['hrs_worked'];
$total_ot = $week_row['hrs_otime'];
echo "
| Total |
Hours: {$total_hours} |
Ot: {$total_ot} |
";
}// End getWeekstats Function
// Get stats for the pay month
function getMonthstats(){}
// Get stats for the pay year
function getYearstats(){}
// Get the difference between time_in and time_out (start,end) for timesheets
function get_time_difference( $start, $end, $lunch )
{
$uts['start'] = strtotime( $start );
$uts['end'] = strtotime( $end );
$uts['lunch'] = strtotime( $lunch );
if( $uts['start']!==-1 && $uts['end']!==-1 )
{
if( $uts['end'] >= $uts['start'] )
{
$diff = $uts['end'] - $uts['start'];
if( $days=intval((floor($diff/86400))) )
$diff = $diff % 86400;
if( $hours=intval((floor($diff/3600))) )
$diff = $diff % 3600;
if( $minutes=intval((floor($diff/60))) )
$diff = $diff % 60;
$diff = intval( $diff );
return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
}
else
{
$diff = ($uts['end'] - $uts['start']);
if( $days=intval((floor($diff/86400))) )
$diff = $diff % 86400;
if( $hours=intval((floor($diff/3600 + 24))) )
$diff = $diff % 3600;
if( $minutes=intval((floor($diff/60))) )
$diff = $diff % 60;
$diff = intval( $diff );
return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
}
}
else
{
trigger_error( "Invalid date/time data detected", E_USER_WARNING );
}
return( false );
}// End get_time_difference function
# End 'Get' Functions
#-------------------------------------------------------------#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Start 'Update/Insert' Functions
#
// Update a Timesheet
function updateTimesheet($uid, $date, $values)
{
include 'db.inc';
// Quick format of time input for 'ease of use' -> allows values such as '8am','5pm','00:00', etc.
$time_in = date('H\:i',strtotime("$values[time_in]"));
$time_out = date('H\:i',strtotime("$values[time_out]"));
if($values[lunch] == "30min")
{
$lunch = ".5";
}elseif($values[lunch] == "1hr")
{
$lunch = "1";
}else{
$lunch = $values[lunch];
}
#$lunch = date('H\:i',strtotime("$values[lunch]"));
$diff = @get_time_difference("$values[time_in]", "$values[time_out]");
#echo $diff['hours'];
#$total_hours = date('g \h\o\u\r\s i \m\i\n\s',strtotime($diff['hours']));
echo $values['pto'];
$diff['hours'] = $diff['hours'] - $lunch;
if($diff['hours'] > 12){
$ot = ($diff['hours'] - 12);
$diff['hours'] = 12;
}
$sql = "UPDATE `timesheet`.`timesheets`
SET `time_in` = '$time_in',
`time_out` = '$time_out',
`lunch` = '$lunch',
`hours` = '$diff[hours]',
`ot` = '$ot',
`holiday` = '$values[holiday]',
`pto` = '$values[pto]' WHERE `timesheets`.`uid` ='$uid' and `timesheets`.`date` ='$date' LIMIT 1;";
$result = mysql_query($sql,$connection) or die(mysql_error());
return $result;
}// End updateTimesheet function
function updatePayperiod($uid, $pay_period)
{
include 'db.inc';
// Get total hours and ot
$sql = "SELECT SUM(hours), SUM(ot), SUM(pto), SUM(holiday) FROM `timesheets` WHERE period_id = '$pay_period' AND uid = '$uid' LIMIT 1 ;";
$sql_result = mysql_query($sql,$connection) or die(mysql_error());
$row = mysql_fetch_array($sql_result);
// Assign variables
$hours = $row['SUM(hours)'];
$ot = $row['SUM(ot)'];
$holidays = $row['SUM(holiday)'];
$pto_days = $row['SUM(pto)'];
// > 40 hours is overtime
if ( $hours > 40 )
{
$add_ot = $hours - 40;
$ot = $ot + $add_ot;
$hours = 40;
}
$payperiod_sql = "UPDATE `timesheet`.`payperiods` SET `hrs_worked` = '$hours',
`hrs_otime` = '$ot',
`pto_days` = '$pto_days',
`holidays` = '$holidays' WHERE `payperiods`.`period_id` ='$pay_period' and `payperiods`.`uid` = '$uid' LIMIT 1 ;";
mysql_query($payperiod_sql,$connection) or die(mysql_error());
return $result;
}// End updatePayperiod function
function updatePto($uid, $pto_remain)
{
include 'db.inc';
$sql= "UPDATE `timesheet`.`user` SET `pto_remain` = '$pto_remain'
WHERE `user`.`uid` =1 LIMIT 1 ;";
$result = mysql_query($sql,$connection) or die(mysql_error());
return $result;
}// End updatePto function
# End 'Update/Insert' Functions
#------------------------------------------------------#
function verify()
{
include 'db.inc';
// check to see if they're already logged in
if (session_is_registered("valid_user")){
return true;
}
// check to see if visitor has just tried to log on
$login = $_POST["login"];
$password = $_POST["password"];
if ( $login && $password )
{
// verify password and log in to database
$select = "SELECT login FROM `timesheet`.`user` where login='$login' and password=PASSWORD('$password');";
$result = mysql_query($select,$connection) or die(mysql_error());
#echo mysql_num_rows($result);
if (mysql_num_rows($result) == 1)
{
// register session variable and exit the verify function
$valid_user = $login;
$_SESSION['valid_user'] = $valid_user;
return true;
}
else
{
// bad user and password
$text = "User Name and Password did not match";
write_log_in( $text );
}
}
else
{
// user must log in
$text = "This is a secure server.
Please log in.";
write_log_in( $text );
}
} // end verify function
function write_log_in( $text )
{
echo "
";
} // end write_log_in function
function writeTimesheet($timesheet,$user)
{
if( $timesheet )
{
// Format Times / Dates
$today = date('m/d/Y',(strtotime($timesheet->date)));
}
// Write out html
echo ''.
'Employee:
'. $user->full_name .''.
'PTO Days:
'. $user->pto_remain .''.
'Today:
'. "$today" .''.
'
# All Times are converted to 24-Hour format
# Enter Lunch/Break value as a decimal or 30min/1hr
'.
'
';
echo '
'.
'Hours Today: '. $timesheet->hours .' hour(s)
'.
'Overtime Today: '. $timesheet->ot . ' hour(s)
' . '
';
}
?>