'8.00',
'Excused-Unpaid' => '0.00',
'Injury-Paid' => '8.00',
'Injury-Unpaid' => '0.00',
'NoWork' => '0.00',
'Other-Paid' => '8.00',
'Other-Unpaid' => '0.00',
'Sick' => '0.00',
'Snow' => '0.00',
'Statutory' => '8.00',
'Suspended' => '0.00',
'Vacation' => '8.00'
);
if (empty($_REQUEST['empNo']))
$_REQUEST['empNo'] = $_SESSION['user']['empNo'];
$user = getEmployeeData($_REQUEST['empNo']);
if (empty($_REQUEST['wDate'])) $_REQUEST['wDate'] = date("Y-m-d");
$period = calcDates($_REQUEST['wDate']);
// in order to process internal or external timebank adjustments, the current values
// must be allowed for before applying the new values
// external adjustments are such things as bartering hours or "bonus" hours gifted from Paul.
// while there is no limit on deposits, one cannot withdraw more "hours" than they have (exceptions apply).
// we start by fetching the periods timebank data for this user
$oldExt = sqlSelect("timebank", "tDate='".$period['cEnd']."' and empNo='".$user['empNo']."'");
if ($oldExt['n'])
{ $oldExt = mysql_fetch_assoc($oldExt['r']);
$oldInt = $oldExt['intAdj']; // these MUST be in this order!
$oldExt = $oldExt['extAdj'];
}
else
{ $oldExt = 0;
$oldInt = 0;
}
// now we process any adjustments that are in the REQUEST array
if (array_key_exists("extAdj", $_REQUEST))
{ if (empty($_REQUEST['extAdj']))
$_REQUEST['extAdj'] = 0;
$openBal = $user['bank'] - $oldExt;
// unless authorized, you cannot spend more time than you have banked.
if ($_REQUEST['extAdj'] < 0 and $_SESSION['user']['empNo'] != '3')
{ if ($openBal < (-1 * $_REQUEST['extAdj']))
$_REQUEST['extAdj'] = (-1 * $openBal);
}
$openBal += $_REQUEST['extAdj'];
$user['bank'] = $openBal;
sqlReplace("timebank", "tDate='".$period['cEnd']."', empNo='".$user['empNo']."', extAdj='".$_REQUEST['extAdj']."', extComment='".$_REQUEST['extComment']."'", "tDate='".$period['cEnd']."' and empNo='".$user['empNo']."'");
sqlUpdate("employees", "bank='".sprintf("%01.2f", $openBal)."'", "empNo='".$user['empNo']."'");
}
// internal adjustments affect the timebank;
if (array_key_exists("intAdj", $_REQUEST))
{ if (empty($_REQUEST['intAdj']))
$_REQUEST['intAdj'] = 0;
$openBal = $user['bank'] - $oldInt;
// unless authorized, you cannot withdraw more time than you have banked.
if ($_REQUEST['intAdj'] < 0 and $_SESSION['user']['empNo'] != '3')
{ if ($openBal < (-1 * $_REQUEST['intAdj']))
$_REQUEST['intAdj'] = (-1 * $openBal);
}
$openBal += $_REQUEST['intAdj'];
$user['bank'] = $openBal;
sqlReplace("timebank", "tDate='".$period['cEnd']."', empNo='".$user['empNo']."', intAdj='".$_REQUEST['intAdj']."', intComment='".$_REQUEST['intComment']."'", "tDate='".$period['cEnd']."' and empNo='".$user['empNo']."'");
sqlUpdate("employees", "bank='".sprintf("%01.2f", $openBal)."'", "empNo='".$user['empNo']."'");
}
if ((!empty($_REQUEST['wDate']) and (!empty($_REQUEST['wStart']) and !empty($_REQUEST['wEnd'])) or !empty($_REQUEST['excuse'])))
{ // a timesheet entry has been submitted
// is it within the allowed window or is there an over ride permission?
if (true or $_SESSION['user']['access'] & 0x0080 or str_replace("-", "", $_REQUEST['wDate']) == date("Ymd") or (tomarrow($_REQUEST['wDate']) == date("Ymd") and $_SESSION['instance']['graceCutOff'] > date("Hi")))
{ // get hours of work for this day
if (empty($_REQUEST['wStart']) or empty($_REQUEST['wEnd']))
{ $tTime = 0;
$_REQUEST['wStart'] = "";
$_REQUEST['wEnd'] = "";
}
else
$tTime = processTimeDif($_REQUEST['wStart'], $_REQUEST['wEnd']);
if (!empty($_REQUEST['excuse']))
{ $tTime += $absences[$_REQUEST['excuse']];
}
// check for multiple entries for this date and adjust OT if necessary
$q = "empNo = '{$user['empNo']}' and wDate = '".str_replace(array(".","-","\\"," ","/"), "", $_REQUEST['wDate'])."'";
$allTime4Date = sqlSelect("times", $q);
if ($allTime4Date['n'])
{ while ($time4Date = mysql_fetch_assoc($allTime4Date['r']))
{ $tRegTime += $time4Date['tTime'];
if ($tRegTime < 8 and (1 * $time4Date['oTime']) > 0)
{ // unwarrented overtime - convert back to regular time
if ($tRegTime + $time4Date['oTime'] > 8) // is there ANY overtime?
{ $entOvrTime = $tRegTime + $time4Date['oTime'] - 8; // real cumulitive overtime
$entRegTime = $time4Date['tTime'] + $entOvrTime - $time4Date['oTime'];
}
else
{ // convert all overtime back to regular time
$entRegTime = $time4Date['tTime'] + $time4Date['oTime'];
$entOvrTime = 0;
}
// do update here
sqlUpdate("times", "tTime = '$entRegTime', oTime='$entOvrTime'", "tid='{$time4Date['tid']}'");
$tRegTime = $tRegTime - $time4Date['oTime'] + $entRegTime;
}
}
}
else
{ // no adjustments were required
$tRegTime = 0;
}
// split off overtime
if ($tRegTime + $tTime > 8)
{ // overtime is anything over 8 hours
//!TODO technically, if one works on a stat, it's all double time
// as is anything over 12 hours ..... still thinking about that one
$oTime = $tRegTime + $tTime - 8;
$tTime = 8 - $tRegTime;
}
else
$oTime = 0;
if (strlen($_REQUEST['dailyAdj']) == 0)
$_REQUEST['dailyAdj'] = "0.00";
$ab = $user['ab_min'] + $user['ab_percent'] + $user['ab_max'] ;
if (strcmp($_REQUEST['dailyAdj'], "0.00") == 0 and $ab and (($user['ab_threshold'] * 1) < ($oTime + $tTime)))
{
if ($user['ab_percent'] * 1)
$_REQUEST['dailyAdj'] = (($oTime + $tTime) * $user['ab_percent']) / 100;
else
$_REQUEST['dailyAdj'] = 0;
$_REQUEST['dailyAdj'] = max(($user['ab_min'] * 1), $_REQUEST['dailyAdj']);
$_REQUEST['dailyAdj'] = min(($user['ab_max'] * 1), $_REQUEST['dailyAdj']);
$_REQUEST['dailyAdj'] = min(($oTime + $tTime), $_REQUEST['dailyAdj']);
if (!$_REQUEST['dailyAdj'])
$_REQUEST['dailyAdj'] = "0.00";
}
else if ($user['ab_equalize'] * 1)
{
if (strcmp($_REQUEST['dailyAdj'], "0.00") == 0 and $user['ab_threshold'] > ($oTime + $tTime) and $user['ab_equalize'])
{ $_REQUEST['dailyAdj'] = ($oTime + $tTime) - $user['ab_equalize'];
}
}
if (($_REQUEST['dailyAdj'] * 1) < 0)
{ if ($user['bank'] < (-1 * $_REQUEST['dailyAdj']))
{ $_REQUEST['dailyAdj'] = (-1 * $user['bank']);
$msgs[] = "timebank ENTRY ADJUSTED: cannot withdraw more time than you have banked";
}
$tTime -= $_REQUEST['dailyAdj'];
}
else if (($_REQUEST['dailyAdj'] * 1) > 0)
{ $dailyAdj = $_REQUEST['dailyAdj'];
if ($oTime < $dailyAdj)
{ $dailyAdj -= $oTime;
$oTime = 0;
if ($tTime >= $dailyAdj)
$tTime -= $dailyAdj;
else
{ $_REQUEST['dailyAdj'] -= ($dailyAdj - $tTime);
$tTime = 0;
$msgs[] = "TIMEBANK ENTRY ADJUSTED: (Cannot deposit more time than you have worked)";
}
}
else
{ $oTime -= $dailyAdj;
}
}
}
else
{ $msgs[] = "ERROR: Date is outside allowed date window";
unset($_REQUEST['wDate']);
unset($_REQUEST['wStart']);
unset($_REQUEST['wEnd']);
unset($_REQUEST['excuse']);
}
// standardize working date
$wDate = str_replace(array(".","-","\\"," ","/"), "", $_REQUEST['wDate']);
// deliquent entries are flagged by setting "is_del" to a value of 1 (otherwise, it is empty)
// detection is fairly simple - empNo 3 makes an entry for anyone else on any date in the past
$todays_date = date("Ymd");
if ($_SESSION['user']['empNo'] == 3 and $user['empNo'] != 3 and $wDate < $todays_date and $_REQUEST['excuse'] != "Vacation")
$is_del = "1";
else
$is_del = "";
// before we can do an update of the user's timebank, we have to see if they had
// a prior bank entry for this date - if they did, it has to be allowed for in the
// calculation of the new total
$user['bank'] += $_REQUEST['dailyAdj'];
sqlUpdate("employees", "bank='".$user['bank']."'", "tid='".$user['tid']."'");
$tTime = sprintf("%01.2f", $tTime);
$oTime = sprintf("%01.2f", $oTime);
$dailyAdj = sprintf("%01.2f", $dailyAdj);
//$str = "wDate='".str_replace(array(".","-","\\"," ","/"), "", $_REQUEST['wDate'])."', ";
$str = "wDate='$wDate', ";
$str .= "excuse='".$_REQUEST['excuse']."', ";
$str .= "wStart='".str_replace(array(".", ":"), "", $_REQUEST['wStart'])."', ";
$str .= "wEnd='".str_replace(array(".", ":"), "", $_REQUEST['wEnd'])."', ";
$str .= "tTime='$tTime', ";
$str .= "oTime='$oTime', ";
$str .= "dailyAdj = '".$_REQUEST['dailyAdj']."', ";
$str .= "is_del='$is_del'";
sqlRecycle("times", "empNo='".$user['empNo']."', $str", "wDate = ''");
}
if (empty($_REQUEST['wDate']))
{ $_REQUEST['wDate'] = date("Y-m-d");
}
$period = calcDates($_REQUEST['wDate']);
$_REQUEST['period'] = $period;
$_REQUEST['wStart'] = "";
$_REQUEST['wEnd'] = "";
$_REQUEST['dailyAdj'] = "";
echo "
\n"; // supercontainer for timesheet and popCalendar
echo "
Time Sheet for ".getEmployeeName($user['empNo'], "full")."
\n";
echo "
\n";
echo "
\n";
// the last thing to do before listing everything is to extract any entry that is to be edited. if there is one, it's TID is in REQUEST['tLinkRef']
if (!empty($_REQUEST['tLinkRef']))
{ // if this is an edit, the date field should not be editable. to do this, we set the style to "READONLY"
if ((false and $_SESSION['user']['access'] & 0x0080) or $_SESSION['user']['empNo'] == "3") // this gives an administrative over-ride - it is currently disabled
$dateInputStyle = "";
else
$dateInputStyle = " READONLY";
$oldEnt = sqlSelect("times", "tid='".$_REQUEST['tLinkRef']."'");
$oldEnt = mysql_fetch_assoc($oldEnt['r']);
foreach ($oldEnt as $k => $v)
$_REQUEST[$k] = $v;
// remove the old entry by flagging it for recycle
sqlUpdate("times", "lastmod='', empNo='0', wDate='', excuse='', wStart='', wEnd='', tTime='0', oTime='0', dailyAdj='0'", "tid='".$_REQUEST['tLinkRef']."'", false);
// the timebank also has to be adjusted if there was an adj on this entry
if (!empty($oldEnt['dailyAdj']))
{ $user['bank'] -= $oldEnt['dailyAdj'];
sqlUpdate("employees", "bank='".$user['bank']."'", "empNo='".$user['empNo']."'");
}
//!TODO we should also look at the intAdj and extAdj to be sure that there is sufficient bank left to cover a withdrawl
}
echo "
Period "; if ($user['access'] & 0x002) echo "Sub"; echo "Total:
\n";
echo "
".sprintf("%01.2f", $timeTotal)."
\n";
if ($user['access'] & 0x002)
echo "
".sprintf("%01.2f", $dailyAdjTtl)."
\n";
echo "
\n";
echo "
".sprintf("%01.2f", $tTimeTtl)."
\n";
echo "
".sprintf("%01.2f", $oTimeTtl)."
\n";
echo "
\n";
if ($user['access'] & 0x002)
{ $oldEnt = sqlSelect("employees", "empNo='".$_REQUEST['empNo']."'");
$oldEnt = mysql_fetch_assoc($oldEnt['r']);
// before showing the "opening timebank balance, we have to fetch the internal and external adjustments and
// add them back in!
// this block fetches all banking with dates NEWER than the specified period so that it can be allowed for in the reporting
$cumAdj = 0;
$adjs = sqlSelect("timebank", "tDate>'".$period['cEnd']."' and empNo='".$user['empNo']."'"); // this will fetch all entries NEWER than the selected period
while ($adj = mysql_fetch_assoc($adjs['r']))
$cumAdj += ($adj['intAdj'] + $adj['extAdj']);
$adjs = sqlSelect("times", "wDate>'".$period['cEnd']."' and empNo='".$user['empNo']."'"); // this will fetch all entries NEWER than the selected period
while ($adj = mysql_fetch_assoc($adjs['r']))
$cumAdj += $adj['dailyAdj'];
$intAdj = sqlSelect("timebank", "tDate='".$period['cEnd']."' and empNo='".$user['empNo']."'");
if ($intAdj['n'])
{ $intAdj = mysql_fetch_assoc($intAdj['r']);
$extAdj = $intAdj['extAdj'];
$intComment = $intAdj['intComment'];
$extComment = $intAdj['extComment'];
$intAdj = $intAdj['intAdj']; // this must be LAST because the value is assigned to the container variable
}
else
{ $intAdj = 0;
$extAdj = 0;
$intComment = "";
$extComment = "";
}
$openBal = $oldEnt['bank'] - $intAdj - $extAdj - $dailyAdjTtl;
echo "
\n";
if ($_SESSION['user']['access'] & 0x0080)
echo "
\n"; // "user selector" space
echo "
Timebank Opening Balance:
\n";
echo "
".sprintf("%01.2f", ($openBal - $cumAdj))."
\n";
echo "
\n";
echo "\n";
echo "
\n";
if ($_SESSION['user']['access'] & 0x0080)
echo "