rendered paste body<?// Functions for Timesheet Program# Created by Mitch Mahan, 11/9/2007#---------------------------------------------------------------## Start 'User' Functionsfunction 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 Functionfunction 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 Functionfunction 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 userfunction 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 dayfunction 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 weekfunction 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 functionfunction 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 '<table border="0" width="170">'; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $day = date('l',strtotime($row['date'])); echo " <tr style='border-bottom: dotted 1px;'> <td>$day</td> <td>Hours: <b>{$row['hours']}</b></td> <td>Ot: <b>{$row['ot']}</b></td> </tr>"; } // 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 " <tr style='border-bottom: dotted 1px;'> <td><b>Total</b></td> <td>Hours: <b>{$total_hours}</b></td> <td>Ot: <b>{$total_ot}</b></td> </tr> </table>";}// End getWeekstats Function// Get stats for the pay monthfunction getMonthstats(){}// Get stats for the pay yearfunction getYearstats(){}// Get the difference between time_in and time_out (start,end) for timesheetsfunction 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 inif (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.<br /> Please log in.";write_log_in( $text );}} // end verify functionfunction write_log_in( $text ){ echo " <div width='300'><p>$text</p> <form method='post' action='index.php'> User ID: <input type='text' name='login' /> Pass: <input type='password' name='password' /> <input type='submit' value='Login' /> </form> </div> ";} // end write_log_in functionfunction writeTimesheet($timesheet,$user){ if( $timesheet ) { // Format Times / Dates $today = date('m/d/Y',(strtotime($timesheet->date))); } // Write out html echo '<div style="position:absolute;left:450px;top:58px;">'. 'Employee: <b>'. $user->full_name .'</b><br />'. 'PTO Days: <b>'. $user->pto_remain .'</b><br/><br/>'. 'Today:<b> '. "$today" .'</b><br/>'. '<p style="font-size:.7em;"> # All Times are converted to 24-Hour format<br/> # Enter Lunch/Break value as a decimal or 30min/1hr</p><br/>'. '<form method="post" action="index.php">'. '<input type="hidden" name="update" value="1">'. '<input type="hidden" name="period_id" value="'.$timesheet->period_id.'">'. '<input type="hidden" name="timesheet_date" value="'.$timesheet->date.'">'. 'In:<input type="text" name="time_in" style="position:relative;left:22px;margin-bottom:5px;" value="'.$timesheet->time_in .'" /> <br/>'. 'Out:<input type="text" name="time_out" style="position:relative;left:13px;margin-bottom:5px;" value="'. $timesheet->time_out.'"/><br/>'. 'Lunch:<input type="text" name="lunch" value="'. $timesheet->lunch .'"/><br/>'. 'PTO: <input name="pto" type="checkbox"'; if($timesheet->pto == '1'){ echo 'checked'; } echo '> '. 'Holiday: <input name="holiday" type="checkbox"'; if($timesheet->holiday == '1'){ echo 'checked'; } echo '>'. '<input type="submit" value="Update"></form>'; echo '<div style="position:relative;left:0px;top:20px;">'. 'Hours Today:<b> '. $timesheet->hours .' hour(s)</b><br>'. 'Overtime Today:<b> '. $timesheet->ot . ' hour(s)<br><br>' . '</b> </div> </div>';}?>