function calcPremiumPolicyTotalTime($udt_meal_policy_adjustment_arr, $udt_break_policy_adjustment_arr, $daily_total_time = FALSE) { global $profiler; $profiler->startTimer("UserDateTotal::calcPremiumPolicyTotalTime() - Part 1"); if ($daily_total_time === FALSE) { $daily_total_time = $this->getDailyTotalTime(); } $pplf = new PremiumPolicyListFactory(); $pplf->getByPolicyGroupUserId($this->getUserDateObject()->getUser()); if ($pplf->getRecordCount() > 0) { Debug::text('Found Premium Policies to apply.', __FILE__, __LINE__, __METHOD__, 10); foreach ($pplf as $pp_obj) { Debug::text('Found Premium Policy: ID: ' . $pp_obj->getId() . ' Type: ' . $pp_obj->getType(), __FILE__, __LINE__, __METHOD__, 10); //FIXME: Support manually setting a premium policy through the Edit Hours page? //In those cases, just skip auto-calculating it and accept it? switch ($pp_obj->getType()) { case 10: //Date/Time Debug::text(' Date/Time Premium Policy...', __FILE__, __LINE__, __METHOD__, 10); //Make sure this is a valid day //Take into account shifts that span midnight though, where one half of the shift is eligilble for premium time. //ie: Premium Policy starts 7AM to 7PM on Sat/Sun. Punches in at 9PM Friday and out at 9AM Sat, we need to check if both days are valid. //FIXME: Handle shifts that are longer than 24hrs in length. if ($pp_obj->isActive($this->getUserDateObject()->getDateStamp() - 86400, $this->getUserDateObject()->getDateStamp() + 86400)) { Debug::text(' Premium Policy Is Active On OR Around This Day.', __FILE__, __LINE__, __METHOD__, 10); $total_daily_time_used = 0; $daily_trigger_time = 0; $udtlf = new UserDateTotalListFactory(); if ($pp_obj->isHourRestricted() == TRUE) { if ($pp_obj->getWeeklyTriggerTime() > 0) { //Get Pay Period Schedule info if (is_object($this->getUserDateObject()->getPayPeriodObject()) and is_object($this->getUserDateObject()->getPayPeriodObject()->getPayPeriodScheduleObject())) { $start_week_day_id = $this->getUserDateObject()->getPayPeriodObject()->getPayPeriodScheduleObject()->getStartWeekDay(); } else { $start_week_day_id = 0; } Debug::text('Start Week Day ID: ' . $start_week_day_id, __FILE__, __LINE__, __METHOD__, 10); $weekly_total_time = $udtlf->getWeekRegularTimeSumByUserIDAndEpochAndStartWeekEpoch($this->getUserDateObject()->getUser(), $this->getUserDateObject()->getDateStamp(), TTDate::getBeginWeekEpoch($this->getUserDateObject()->getDateStamp(), $start_week_day_id)); if ($weekly_total_time > $pp_obj->getWeeklyTriggerTime()) { $daily_trigger_time = 0; } else { $daily_trigger_time = $pp_obj->getWeeklyTriggerTime() - $weekly_total_time; } Debug::text(' Weekly Trigger Time: ' . $daily_trigger_time, __FILE__, __LINE__, __METHOD__, 10); } if ($pp_obj->getDailyTriggerTime() > 0 and $pp_obj->getDailyTriggerTime() > $daily_trigger_time) { $daily_trigger_time = $pp_obj->getDailyTriggerTime(); } } Debug::text(' Daily Trigger Time: ' . $daily_trigger_time, __FILE__, __LINE__, __METHOD__, 10); //Loop through all worked (status: 20) UserDateTotalRows $udtlf->getByUserDateIdAndStatus($this->getUserDateID(), 20); $i = 1; if ($udtlf->getRecordCount() > 0) { Debug::text('Found Total Hours to attempt to apply premium policy... Record Count: ' . $udtlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10); foreach ($udtlf as $udt_obj) { Debug::text('UserDateTotal ID: ' . $udt_obj->getID() . ' Total Time: ' . $udt_obj->getTotalTime(), __FILE__, __LINE__, __METHOD__, 10); //Ignore incomplete punches if ($udt_obj->getTotalTime() == 0) { continue; } //How do we handle actual shifts for premium time? //So if premium policy starts at 1PM for shifts, to not //include employees who return from lunch at 1:30PM. //Create a function that takes all punches for a day, and returns //the first in and last out time for a given shift when taking //into account minimum time between shifts, as well as the total time for that shift. //We can then use that time for ActiveTime on premium policies, and determine if a //punch falls within the active time, then we add it to the total. if ($pp_obj->isTimeRestricted() == TRUE and $udt_obj->getPunchControlID() != FALSE) { Debug::text('Time Restricted Premium Policy, lookup punches to get times.', __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getIncludePartialPunch() == FALSE) { $shift_data = $this->getShiftDataByUserDateID($this->getUserDateID()); } $plf = new PunchListFactory(); $plf->getByPunchControlId($udt_obj->getPunchControlID()); if ($plf->getRecordCount() > 0) { Debug::text('Found Punches: ' . $plf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10); foreach ($plf as $punch_obj) { if ($pp_obj->getIncludePartialPunch() == TRUE) { //Debug::text('Including Partial Punches...', __FILE__, __LINE__, __METHOD__, 10); if ($punch_obj->getStatus() == 10) { $punch_times['in'] = $punch_obj->getTimeStamp(); } elseif ($punch_obj->getStatus() == 20) { $punch_times['out'] = $punch_obj->getTimeStamp(); } } else { if (isset($shift_data) and is_array($shift_data)) { foreach ($shift_data as $shift) { if ($punch_obj->getTimeStamp() >= $shift['first_in'] and $punch_obj->getTimeStamp() <= $shift['last_out']) { //Debug::Arr($shift,'Shift Data...', __FILE__, __LINE__, __METHOD__, 10); Debug::text('Punch (' . TTDate::getDate('DATE+TIME', $punch_obj->getTimeStamp()) . ') inside shift time...', __FILE__, __LINE__, __METHOD__, 10); $punch_times['in'] = $shift['first_in']; $punch_times['out'] = $shift['last_out']; break; } else { Debug::text('Punch (' . TTDate::getDate('DATE+TIME', $punch_obj->getTimeStamp()) . ') outside shift time...', __FILE__, __LINE__, __METHOD__, 10); } } } } } if (isset($punch_times) and count($punch_times) == 2 and $pp_obj->isActiveTime($punch_times['in'], $punch_times['out']) == TRUE) { //Debug::Arr($punch_times, 'Punch Times: ', __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = $pp_obj->getPartialPunchTotalTime($punch_times['in'], $punch_times['out'], $udt_obj->getTotalTime()); Debug::text('Valid Punch pair in active time, Partial Punch Total Time: ' . $punch_total_time, __FILE__, __LINE__, __METHOD__, 10); } else { Debug::text('InValid Punch Pair or outside Active Time...', __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = 0; } } } elseif ($pp_obj->isActive($udt_obj->getUserDateObject()->getDateStamp()) == TRUE) { $punch_total_time = $udt_obj->getTotalTime(); } else { $punch_total_time = 0; } Debug::text('aPunch Total Time: ' . $punch_total_time, __FILE__, __LINE__, __METHOD__, 10); //Apply meal policy adjustment as early as possible. if ($pp_obj->getIncludeMealPolicy() == TRUE and isset($udt_meal_policy_adjustment_arr[$udt_obj->getId()])) { Debug::text(' Meal Policy Adjustment Found: ' . $udt_meal_policy_adjustment_arr[$udt_obj->getId()], __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = bcadd($punch_total_time, $udt_meal_policy_adjustment_arr[$udt_obj->getId()]); $tmp_punch_total_time = bcadd($udt_obj->getTotalTime(), $udt_meal_policy_adjustment_arr[$udt_obj->getId()]); } else { $tmp_punch_total_time = $udt_obj->getTotalTime(); } Debug::text('bPunch Total Time: ' . $punch_total_time, __FILE__, __LINE__, __METHOD__, 10); //Apply break policy adjustment as early as possible. if ($pp_obj->getIncludeBreakPolicy() == TRUE and isset($udt_break_policy_adjustment_arr[$udt_obj->getId()])) { Debug::text(' Break Policy Adjustment Found: ' . $udt_break_policy_adjustment_arr[$udt_obj->getId()], __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = bcadd($punch_total_time, $udt_break_policy_adjustment_arr[$udt_obj->getId()]); $tmp_punch_total_time = bcadd($udt_obj->getTotalTime(), $udt_break_policy_adjustment_arr[$udt_obj->getId()]); } else { $tmp_punch_total_time = $udt_obj->getTotalTime(); } Debug::text('cPunch Total Time: ' . $punch_total_time, __FILE__, __LINE__, __METHOD__, 10); $total_daily_time_used += $tmp_punch_total_time; Debug::text('Daily Total Time Used: ' . $total_daily_time_used, __FILE__, __LINE__, __METHOD__, 10); //FIXME: Should the daily/weekly trigger time be >= instead of >. //That way if the policy is active after 7.5hrs, punch time of exactly 7.5hrs will still //activate the policy, rather then requiring 7.501hrs+ if ($punch_total_time > 0 and $total_daily_time_used > $daily_trigger_time) { Debug::text('Past Trigger Time!!', __FILE__, __LINE__, __METHOD__, 10); //Calculate how far past trigger time we are. $past_trigger_time = $total_daily_time_used - $daily_trigger_time; if ($punch_total_time > $past_trigger_time) { $punch_total_time = $past_trigger_time; Debug::text('Using Past Trigger Time as punch total time: ' . $past_trigger_time, __FILE__, __LINE__, __METHOD__, 10); } else { Debug::text('NOT Using Past Trigger Time as punch total time: ' . $past_trigger_time, __FILE__, __LINE__, __METHOD__, 10); } $total_time = $punch_total_time; if ($pp_obj->getMinimumTime() > 0 or $pp_obj->getMaximumTime() > 0) { $premium_policy_daily_total_time = (int) $udtlf->getPremiumPolicySumByUserDateIDAndPremiumPolicyID($this->getUserDateID(), $pp_obj->getId()); Debug::text(' Premium Policy Daily Total Time: ' . $premium_policy_daily_total_time . ' Minimum Time: ' . $pp_obj->getMinimumTime() . ' Maximum Time: ' . $pp_obj->getMaximumTime() . ' Total Time: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getMinimumTime() > 0) { //FIXME: Split the minimum time up between all the punches somehow. //Apply the minimum time on the last punch, otherwise if there are two punch pairs of 15min each //and a 1hr minimum time, if the minimum time is applied to the first, it will be 1hr and 15min //for the day. If its applied to the last it will be just 1hr. //Min & Max time is based on the shift time, rather then per punch pair time. //FIXME: If there is a minimum time set to say 9hrs, and the punches go like this: // In: 7:00AM Out: 3:00:PM, Out: 3:30PM (missing 2nd In Punch), the minimum time won't be calculated due to the invalid punch pair. if ($i == $udtlf->getRecordCount() and bcadd($premium_policy_daily_total_time, $total_time) < $pp_obj->getMinimumTime()) { $total_time = bcsub($pp_obj->getMinimumTime(), $premium_policy_daily_total_time); } } Debug::text(' Total Time After Minimum is applied: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getMaximumTime() > 0) { //Min & Max time is based on the shift time, rather then per punch pair time. if (bcadd($premium_policy_daily_total_time, $total_time) > $pp_obj->getMaximumTime()) { Debug::text(' bMore than Maximum Time...', __FILE__, __LINE__, __METHOD__, 10); $total_time = bcsub($total_time, bcsub(bcadd($premium_policy_daily_total_time, $total_time), $pp_obj->getMaximumTime())); } } } Debug::text(' Premium Punch Total Time: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); if ($total_time > 0) { Debug::text(' Applying Premium Time!: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); $udtf = new UserDateTotalFactory(); $udtf->setUserDateID($this->getUserDateID()); $udtf->setStatus(10); //System $udtf->setType(40); //Premium $udtf->setPremiumPolicyId($pp_obj->getId()); $udtf->setBranch($udt_obj->getBranch()); $udtf->setDepartment($udt_obj->getDepartment()); $udtf->setJob($udt_obj->getJob()); $udtf->setJobItem($udt_obj->getJobItem()); $udtf->setQuantity($udt_obj->getQuantity()); $udtf->setBadQuantity($udt_obj->getBadQuantity()); $udtf->setTotalTime($total_time); $udtf->setEnableCalcSystemTotalTime(FALSE); if ($udtf->isValid() == TRUE) { $udtf->Save(); } unset($udtf); } else { Debug::text(' Premium Punch Total Time is 0...', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text('Not Past Trigger Time Yet or Punch Time is 0...', __FILE__, __LINE__, __METHOD__, 10); } $i++; } } } break; case 20: //Differential Debug::text(' Differential Premium Policy...', __FILE__, __LINE__, __METHOD__, 10); //Loop through all worked (status: 20) UserDateTotalRows $udtlf = new UserDateTotalListFactory(); $udtlf->getByUserDateIdAndStatus($this->getUserDateID(), 20); if ($udtlf->getRecordCount() > 0) { Debug::text('Found Total Hours to attempt to apply premium policy... Record Count: ' . $udtlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10); foreach ($udtlf as $udt_obj) { //Ignore incomplete punches if ($udt_obj->getTotalTime() == 0) { continue; } if ($pp_obj->getBranchSelectionType() == 10 and ($pp_obj->getExcludeDefaultBranch() == FALSE or $pp_obj->getExcludeDefaultBranch() == TRUE and $udt_obj->getBranch() != $this->getUserDateObject()->getUserObject()->getDefaultBranch()) or $pp_obj->getBranchSelectionType() == 20 and in_array($udt_obj->getBranch(), (array) $pp_obj->getBranch()) and ($pp_obj->getExcludeDefaultBranch() == FALSE or $pp_obj->getExcludeDefaultBranch() == TRUE and $udt_obj->getBranch() != $this->getUserDateObject()->getUserObject()->getDefaultBranch()) or $pp_obj->getBranchSelectionType() == 30 and !in_array($udt_obj->getBranch(), (array) $pp_obj->getBranch()) and ($pp_obj->getExcludeDefaultBranch() == FALSE or $pp_obj->getExcludeDefaultBranch() == TRUE and $udt_obj->getBranch() != $this->getUserDateObject()->getUserObject()->getDefaultBranch())) { Debug::text(' Shift Differential... Meets Branch Criteria! Select Type: ' . $pp_obj->getBranchSelectionType() . ' Exclude Default Branch: ' . (int) $pp_obj->getExcludeDefaultBranch() . ' Default Branch: ' . $this->getUserDateObject()->getUserObject()->getDefaultBranch(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getDepartmentSelectionType() == 10 and ($pp_obj->getExcludeDefaultDepartment() == FALSE or $pp_obj->getExcludeDefaultDepartment() == TRUE and $udt_obj->getDepartment() != $this->getUserDateObject()->getUserObject()->getDefaultDepartment()) or $pp_obj->getDepartmentSelectionType() == 20 and in_array($udt_obj->getDepartment(), (array) $pp_obj->getDepartment()) and ($pp_obj->getExcludeDefaultDepartment() == FALSE or $pp_obj->getExcludeDefaultDepartment() == TRUE and $udt_obj->getDepartment() != $this->getUserDateObject()->getUserObject()->getDefaultDepartment()) or $pp_obj->getDepartmentSelectionType() == 30 and !in_array($udt_obj->getDepartment(), (array) $pp_obj->getDepartment()) and ($pp_obj->getExcludeDefaultDepartment() == FALSE or $pp_obj->getExcludeDefaultDepartment() == TRUE and $udt_obj->getDepartment() != $this->getUserDateObject()->getUserObject()->getDefaultDepartment())) { Debug::text(' Shift Differential... Meets Department Criteria! Select Type: ' . $pp_obj->getDepartmentSelectionType() . ' Exclude Default Department: ' . (int) $pp_obj->getExcludeDefaultDepartment() . ' Default Department: ' . $this->getUserDateObject()->getUserObject()->getDefaultDepartment(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getJobGroupSelectionType() == 10 or $pp_obj->getJobGroupSelectionType() == 20 and (is_object($udt_obj->getJobObject()) and in_array($udt_obj->getJobObject()->getGroup(), (array) $pp_obj->getJobGroup())) or $pp_obj->getJobGroupSelectionType() == 30 and (is_object($udt_obj->getJobObject()) and !in_array($udt_obj->getJobObject()->getGroup(), (array) $pp_obj->getJobGroup()))) { Debug::text(' Shift Differential... Meets Job Group Criteria! Select Type: ' . $pp_obj->getJobGroupSelectionType(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getJobSelectionType() == 10 or $pp_obj->getJobSelectionType() == 20 and in_array($udt_obj->getJob(), (array) $pp_obj->getJob()) or $pp_obj->getJobSelectionType() == 30 and !in_array($udt_obj->getJob(), (array) $pp_obj->getJob())) { Debug::text(' Shift Differential... Meets Job Criteria! Select Type: ' . $pp_obj->getJobSelectionType(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getJobItemGroupSelectionType() == 10 or $pp_obj->getJobItemGroupSelectionType() == 20 and (is_object($udt_obj->getJobItemObject()) and in_array($udt_obj->getJobItemObject()->getGroup(), (array) $pp_obj->getJobItemGroup())) or $pp_obj->getJobItemGroupSelectionType() == 30 and (is_object($udt_obj->getJobItemObject()) and !in_array($udt_obj->getJobItemObject()->getGroup(), (array) $pp_obj->getJobItemGroup()))) { Debug::text(' Shift Differential... Meets Task Group Criteria! Select Type: ' . $pp_obj->getJobItemGroupSelectionType(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getJobItemSelectionType() == 10 or $pp_obj->getJobItemSelectionType() == 20 and in_array($udt_obj->getJobItem(), (array) $pp_obj->getJobItem()) or $pp_obj->getJobItemSelectionType() == 30 and !in_array($udt_obj->getJobItem(), (array) $pp_obj->getJobItem())) { Debug::text(' Shift Differential... Meets Task Criteria! Select Type: ' . $pp_obj->getJobSelectionType(), __FILE__, __LINE__, __METHOD__, 10); $premium_policy_daily_total_time = 0; $punch_total_time = $udt_obj->getTotalTime(); $total_time = 0; //Apply meal policy adjustment BEFORE min/max times if ($pp_obj->getIncludeMealPolicy() == TRUE and isset($udt_meal_policy_adjustment_arr[$udt_obj->getId()])) { Debug::text(' Meal Policy Adjustment Found: ' . $udt_meal_policy_adjustment_arr[$udt_obj->getId()], __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = bcadd($punch_total_time, $udt_meal_policy_adjustment_arr[$udt_obj->getId()]); } if ($pp_obj->getIncludeBreakPolicy() == TRUE and isset($udt_break_policy_adjustment_arr[$udt_obj->getId()])) { Debug::text(' Break Policy Adjustment Found: ' . $udt_break_policy_adjustment_arr[$udt_obj->getId()], __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = bcadd($punch_total_time, $udt_break_policy_adjustment_arr[$udt_obj->getId()]); } if ($pp_obj->getMinimumTime() > 0 or $pp_obj->getMaximumTime() > 0) { $premium_policy_daily_total_time = $udtlf->getPremiumPolicySumByUserDateIDAndPremiumPolicyID($this->getUserDateID(), $pp_obj->getId()); Debug::text(' Premium Policy Daily Total Time: ' . $premium_policy_daily_total_time . ' Minimum Time: ' . $pp_obj->getMinimumTime() . ' Maximum Time: ' . $pp_obj->getMaximumTime(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getMinimumTime() > 0) { if ($daily_total_time < $pp_obj->getMinimumTime()) { //Split the minimum time up between all punches //We only get IN punches, so we don't need to divide $total_punches by 2. //This won't calculate the proper amount if punches aren't paired, but everything //is broken then anyways. $total_time = bcdiv($pp_obj->getMinimumTime(), $total_punches); Debug::text(' Daily Total Time is less the Minimum, using: ' . $total_time . ' Total Punches: ' . $total_punches, __FILE__, __LINE__, __METHOD__, 10); } else { Debug::text(' Daily Total is more then minimum...', __FILE__, __LINE__, __METHOD__, 10); $total_time = $punch_total_time; } } else { $total_time = $punch_total_time; } Debug::text(' Total Time After Minimum is applied: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getMaximumTime() > 0) { if ($total_time > $pp_obj->getMaximumTime()) { Debug::text(' aMore than Maximum Time...', __FILE__, __LINE__, __METHOD__, 10); $total_time = $pp_obj->getMaximumTime(); } elseif (bcadd($premium_policy_daily_total_time, $total_time) > $pp_obj->getMaximumTime()) { Debug::text(' bMore than Maximum Time...', __FILE__, __LINE__, __METHOD__, 10); $total_time = bcsub(bcadd($premium_policy_daily_total_time, $total_time), $pp_obj->getMaximumTime()); } } } else { $total_time = $punch_total_time; } Debug::text(' Premium Punch Total Time: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); if ($total_time > 0) { Debug::text(' Applying Premium Time!: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); $udtf = new UserDateTotalFactory(); $udtf->setUserDateID($this->getUserDateID()); $udtf->setStatus(10); //System $udtf->setType(40); //Premium $udtf->setPremiumPolicyId($pp_obj->getId()); $udtf->setBranch($udt_obj->getBranch()); $udtf->setDepartment($udt_obj->getDepartment()); $udtf->setJob($udt_obj->getJob()); $udtf->setJobItem($udt_obj->getJobItem()); $udtf->setQuantity($udt_obj->getQuantity()); $udtf->setBadQuantity($udt_obj->getBadQuantity()); $udtf->setTotalTime($total_time); $udtf->setEnableCalcSystemTotalTime(FALSE); if ($udtf->isValid() == TRUE) { $udtf->Save(); } unset($udtf); } else { Debug::text(' Premium Punch Total Time is 0...', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Task Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Task Group Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Job Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Job Group Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Department Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Branch Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } } break; case 30: //Meal/Break Debug::text(' Meal/Break Premium Policy...', __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getDailyTriggerTime() == 0 or $pp_obj->getDailyTriggerTime() > 0 and $daily_total_time >= $pp_obj->getDailyTriggerTime()) { //Find maximum worked without a break. $plf = new PunchListFactory(); $plf->getByUserDateId($this->getUserDateID()); //Get all punches for the day. if ($plf->getRecordCount() > 0) { Debug::text('Found Punches: ' . $plf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10); foreach ($plf as $p_obj) { Debug::text('TimeStamp: ' . $p_obj->getTimeStamp() . ' Status: ' . $p_obj->getStatus(), __FILE__, __LINE__, __METHOD__, 10); $punch_pairs[$p_obj->getPunchControlID()][] = array('status_id' => $p_obj->getStatus(), 'punch_control_id' => $p_obj->getPunchControlID(), 'time_stamp' => $p_obj->getTimeStamp()); } if (isset($punch_pairs)) { $prev_punch_timestamp = NULL; $maximum_time_worked_without_break = 0; foreach ($punch_pairs as $punch_pair) { if (count($punch_pair) > 1) { //Total Punch Time $total_punch_pair_time = $punch_pair[1]['time_stamp'] - $punch_pair[0]['time_stamp']; $maximum_time_worked_without_break += $total_punch_pair_time; Debug::text('Total Punch Pair Time: ' . $total_punch_pair_time . ' Maximum No Break Time: ' . $maximum_time_worked_without_break, __FILE__, __LINE__, __METHOD__, 10); if ($prev_punch_timestamp !== NULL) { $break_time = $punch_pair[0]['time_stamp'] - $prev_punch_timestamp; if ($break_time > $pp_obj->getMinimumBreakTime()) { Debug::text('Exceeded Minimum Break Time: ' . $break_time . ' Minimum: ' . $pp_obj->getMinimumBreakTime(), __FILE__, __LINE__, __METHOD__, 10); $maximum_time_worked_without_break = 0; } } if ($maximum_time_worked_without_break > $pp_obj->getMaximumNoBreakTime()) { Debug::text('Exceeded maximum no break time!', __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getMaximumTime() > $pp_obj->getMinimumTime()) { $total_time = $pp_obj->getMaximumTime(); } else { $total_time = $pp_obj->getMinimumTime(); } if ($total_time > 0) { Debug::text(' Applying Meal/Break Premium Time!: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); //Get Punch Control obj. $pclf = new PunchControlListFactory(); $pclf->getById($punch_pair[0]['punch_control_id']); if ($pclf->getRecordCount() > 0) { $pc_obj = $pclf->getCurrent(); } $udtf = new UserDateTotalFactory(); $udtf->setUserDateID($this->getUserDateID()); $udtf->setStatus(10); //System $udtf->setType(40); //Premium $udtf->setPremiumPolicyId($pp_obj->getId()); if (isset($pc_obj) and is_object($pc_obj)) { $udtf->setBranch($pc_obj->getBranch()); $udtf->setDepartment($pc_obj->getDepartment()); $udtf->setJob($pc_obj->getJob()); $udtf->setJobItem($pc_obj->getJobItem()); } $udtf->setTotalTime($total_time); $udtf->setEnableCalcSystemTotalTime(FALSE); if ($udtf->isValid() == TRUE) { $udtf->Save(); } unset($udtf); break; //Stop looping through punches. } } else { Debug::text('Did not exceed maximum no break time yet...', __FILE__, __LINE__, __METHOD__, 10); } $prev_punch_timestamp = $punch_pair[1]['time_stamp']; } else { Debug::text('Found UnPaired Punch, Ignorning...', __FILE__, __LINE__, __METHOD__, 10); } } unset($plf, $punch_pairs, $punch_pair, $prev_punch_timestamp, $maximum_time_worked_without_break, $total_time); } } } else { Debug::text(' Not within Daily Total Time: ' . $daily_total_time . ' Trigger Time: ' . $pp_obj->getDailyTriggerTime(), __FILE__, __LINE__, __METHOD__, 10); } break; case 100: //Advanced Debug::text(' Advanced Premium Policy...', __FILE__, __LINE__, __METHOD__, 10); //Make sure this is a valid day if ($pp_obj->isActive($this->getUserDateObject()->getDateStamp() - 86400, $this->getUserDateObject()->getDateStamp() + 86400)) { Debug::text(' Premium Policy Is Active On This Day.', __FILE__, __LINE__, __METHOD__, 10); $total_daily_time_used = 0; $daily_trigger_time = 0; $udtlf = new UserDateTotalListFactory(); if ($pp_obj->isHourRestricted() == TRUE) { if ($pp_obj->getWeeklyTriggerTime() > 0) { //Get Pay Period Schedule info if (is_object($this->getUserDateObject()->getPayPeriodObject()) and is_object($this->getUserDateObject()->getPayPeriodObject()->getPayPeriodScheduleObject())) { $start_week_day_id = $this->getUserDateObject()->getPayPeriodObject()->getPayPeriodScheduleObject()->getStartWeekDay(); } else { $start_week_day_id = 0; } Debug::text('Start Week Day ID: ' . $start_week_day_id, __FILE__, __LINE__, __METHOD__, 10); $weekly_total_time = $udtlf->getWeekRegularTimeSumByUserIDAndEpochAndStartWeekEpoch($this->getUserDateObject()->getUser(), $this->getUserDateObject()->getDateStamp(), TTDate::getBeginWeekEpoch($this->getUserDateObject()->getDateStamp(), $start_week_day_id)); if ($weekly_total_time > $pp_obj->getWeeklyTriggerTime()) { $daily_trigger_time = 0; } else { $daily_trigger_time = $pp_obj->getWeeklyTriggerTime() - $weekly_total_time; } Debug::text(' Weekly Trigger Time: ' . $daily_trigger_time, __FILE__, __LINE__, __METHOD__, 10); } if ($pp_obj->getDailyTriggerTime() > 0 and $pp_obj->getDailyTriggerTime() > $daily_trigger_time) { $daily_trigger_time = $pp_obj->getDailyTriggerTime(); } } Debug::text(' Daily Trigger Time: ' . $daily_trigger_time, __FILE__, __LINE__, __METHOD__, 10); //Loop through all worked (status: 20) UserDateTotalRows $udtlf->getByUserDateIdAndStatus($this->getUserDateID(), 20); $i = 1; if ($udtlf->getRecordCount() > 0) { Debug::text('Found Total Hours to attempt to apply premium policy... Record Count: ' . $udtlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10); foreach ($udtlf as $udt_obj) { //Ignore incomplete punches if ($udt_obj->getTotalTime() == 0) { continue; } //Check Shift Differential criteria before calculatating daily/weekly time which //is more resource intensive. if ($pp_obj->getBranchSelectionType() == 10 and ($pp_obj->getExcludeDefaultBranch() == FALSE or $pp_obj->getExcludeDefaultBranch() == TRUE and $udt_obj->getBranch() != $this->getUserDateObject()->getUserObject()->getDefaultBranch()) or $pp_obj->getBranchSelectionType() == 20 and in_array($udt_obj->getBranch(), (array) $pp_obj->getBranch()) and ($pp_obj->getExcludeDefaultBranch() == FALSE or $pp_obj->getExcludeDefaultBranch() == TRUE and $udt_obj->getBranch() != $this->getUserDateObject()->getUserObject()->getDefaultBranch()) or $pp_obj->getBranchSelectionType() == 30 and !in_array($udt_obj->getBranch(), (array) $pp_obj->getBranch()) and ($pp_obj->getExcludeDefaultBranch() == FALSE or $pp_obj->getExcludeDefaultBranch() == TRUE and $udt_obj->getBranch() != $this->getUserDateObject()->getUserObject()->getDefaultBranch())) { Debug::text(' Shift Differential... Meets Branch Criteria! Select Type: ' . $pp_obj->getBranchSelectionType() . ' Exclude Default Branch: ' . (int) $pp_obj->getExcludeDefaultBranch() . ' Default Branch: ' . $this->getUserDateObject()->getUserObject()->getDefaultBranch(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getDepartmentSelectionType() == 10 and ($pp_obj->getExcludeDefaultDepartment() == FALSE or $pp_obj->getExcludeDefaultDepartment() == TRUE and $udt_obj->getDepartment() != $this->getUserDateObject()->getUserObject()->getDefaultDepartment()) or $pp_obj->getDepartmentSelectionType() == 20 and in_array($udt_obj->getDepartment(), (array) $pp_obj->getDepartment()) and ($pp_obj->getExcludeDefaultDepartment() == FALSE or $pp_obj->getExcludeDefaultDepartment() == TRUE and $udt_obj->getDepartment() != $this->getUserDateObject()->getUserObject()->getDefaultDepartment()) or $pp_obj->getDepartmentSelectionType() == 30 and !in_array($udt_obj->getDepartment(), (array) $pp_obj->getDepartment()) and ($pp_obj->getExcludeDefaultDepartment() == FALSE or $pp_obj->getExcludeDefaultDepartment() == TRUE and $udt_obj->getDepartment() != $this->getUserDateObject()->getUserObject()->getDefaultDepartment())) { Debug::text(' Shift Differential... Meets Department Criteria! Select Type: ' . $pp_obj->getDepartmentSelectionType() . ' Exclude Default Department: ' . (int) $pp_obj->getExcludeDefaultDepartment() . ' Default Department: ' . $this->getUserDateObject()->getUserObject()->getDefaultDepartment(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getJobGroupSelectionType() == 10 or $pp_obj->getJobGroupSelectionType() == 20 and is_object($udt_obj->getJobObject()) and in_array($udt_obj->getJobObject()->getGroup(), (array) $pp_obj->getJobGroup()) or $pp_obj->getJobGroupSelectionType() == 30 and is_object($udt_obj->getJobObject()) and !in_array($udt_obj->getJobObject()->getGroup(), (array) $pp_obj->getJobGroup())) { Debug::text(' Shift Differential... Meets Job Group Criteria! Select Type: ' . $pp_obj->getJobGroupSelectionType(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getJobSelectionType() == 10 or $pp_obj->getJobSelectionType() == 20 and in_array($udt_obj->getJob(), (array) $pp_obj->getJob()) or $pp_obj->getJobSelectionType() == 30 and !in_array($udt_obj->getJob(), (array) $pp_obj->getJob())) { Debug::text(' Shift Differential... Meets Job Criteria! Select Type: ' . $pp_obj->getJobSelectionType(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getJobItemGroupSelectionType() == 10 or $pp_obj->getJobItemGroupSelectionType() == 20 and is_object($udt_obj->getJobItemObject()) and in_array($udt_obj->getJobItemObject()->getGroup(), (array) $pp_obj->getJobItemGroup()) or $pp_obj->getJobItemGroupSelectionType() == 30 and is_object($udt_obj->getJobItemObject()) and !in_array($udt_obj->getJobItemObject()->getGroup(), (array) $pp_obj->getJobItemGroup())) { Debug::text(' Shift Differential... Meets Task Group Criteria! Select Type: ' . $pp_obj->getJobItemGroupSelectionType(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getJobItemSelectionType() == 10 or $pp_obj->getJobItemSelectionType() == 20 and in_array($udt_obj->getJobItem(), (array) $pp_obj->getJobItem()) or $pp_obj->getJobItemSelectionType() == 30 and !in_array($udt_obj->getJobItem(), (array) $pp_obj->getJobItem())) { Debug::text(' Shift Differential... Meets Task Criteria! Select Type: ' . $pp_obj->getJobSelectionType(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->isTimeRestricted() == TRUE and $udt_obj->getPunchControlID() != FALSE) { Debug::text('Time Restricted Premium Policy, lookup punches to get times.', __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getIncludePartialPunch() == FALSE) { $shift_data = $this->getShiftDataByUserDateID($this->getUserDateID()); } $plf = new PunchListFactory(); $plf->getByPunchControlId($udt_obj->getPunchControlID()); if ($plf->getRecordCount() > 0) { Debug::text('Found Punches: ' . $plf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10); foreach ($plf as $punch_obj) { if ($pp_obj->getIncludePartialPunch() == TRUE) { //Debug::text('Including Partial Punches...', __FILE__, __LINE__, __METHOD__, 10); if ($punch_obj->getStatus() == 10) { $punch_times['in'] = $punch_obj->getTimeStamp(); } elseif ($punch_obj->getStatus() == 20) { $punch_times['out'] = $punch_obj->getTimeStamp(); } } else { if (isset($shift_data) and is_array($shift_data)) { foreach ($shift_data as $shift) { if ($punch_obj->getTimeStamp() >= $shift['first_in'] and $punch_obj->getTimeStamp() <= $shift['last_out']) { //Debug::Arr($shift,'Shift Data...', __FILE__, __LINE__, __METHOD__, 10); Debug::text('Punch (' . TTDate::getDate('DATE+TIME', $punch_obj->getTimeStamp()) . ') inside shift time...', __FILE__, __LINE__, __METHOD__, 10); $punch_times['in'] = $shift['first_in']; $punch_times['out'] = $shift['last_out']; break; } else { Debug::text('Punch (' . TTDate::getDate('DATE+TIME', $punch_obj->getTimeStamp()) . ') outside shift time...', __FILE__, __LINE__, __METHOD__, 10); } } } } } if (isset($punch_times) and count($punch_times) == 2 and $pp_obj->isActiveTime($punch_times['in'], $punch_times['out']) == TRUE) { //Debug::Arr($punch_times, 'Punch Times: ', __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = $pp_obj->getPartialPunchTotalTime($punch_times['in'], $punch_times['out'], $udt_obj->getTotalTime()); Debug::text('Valid Punch pair in active time, Partial Punch Total Time: ' . $punch_total_time, __FILE__, __LINE__, __METHOD__, 10); } else { Debug::text('InValid Punch Pair or outside Active Time...', __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = 0; } } } elseif ($pp_obj->isActive($udt_obj->getUserDateObject()->getDateStamp()) == TRUE) { $punch_total_time = $udt_obj->getTotalTime(); } else { $punch_total_time = 0; } Debug::text('aPunch Total Time: ' . $punch_total_time, __FILE__, __LINE__, __METHOD__, 10); //Apply meal policy adjustment as early as possible. if ($pp_obj->getIncludeMealPolicy() == TRUE and isset($udt_meal_policy_adjustment_arr[$udt_obj->getId()])) { Debug::text(' Meal Policy Adjustment Found: ' . $udt_meal_policy_adjustment_arr[$udt_obj->getId()], __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = bcadd($punch_total_time, $udt_meal_policy_adjustment_arr[$udt_obj->getId()]); $tmp_punch_total_time = bcadd($udt_obj->getTotalTime(), $udt_meal_policy_adjustment_arr[$udt_obj->getId()]); } else { $tmp_punch_total_time = $udt_obj->getTotalTime(); } Debug::text('bPunch Total Time: ' . $punch_total_time, __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getIncludeBreakPolicy() == TRUE and isset($udt_break_policy_adjustment_arr[$udt_obj->getId()])) { Debug::text(' Break Policy Adjustment Found: ' . $udt_break_policy_adjustment_arr[$udt_obj->getId()], __FILE__, __LINE__, __METHOD__, 10); $punch_total_time = bcadd($punch_total_time, $udt_break_policy_adjustment_arr[$udt_obj->getId()]); $tmp_punch_total_time = bcadd($udt_obj->getTotalTime(), $udt_break_policy_adjustment_arr[$udt_obj->getId()]); } else { $tmp_punch_total_time = $udt_obj->getTotalTime(); } Debug::text('cPunch Total Time: ' . $punch_total_time, __FILE__, __LINE__, __METHOD__, 10); $total_daily_time_used += $tmp_punch_total_time; Debug::text('Daily Total Time Used: ' . $total_daily_time_used, __FILE__, __LINE__, __METHOD__, 10); if ($punch_total_time > 0 and $total_daily_time_used > $daily_trigger_time) { Debug::text('Past Trigger Time!!', __FILE__, __LINE__, __METHOD__, 10); //Calculate how far past trigger time we are. $past_trigger_time = $total_daily_time_used - $daily_trigger_time; if ($punch_total_time > $past_trigger_time) { $punch_total_time = $past_trigger_time; Debug::text('Using Past Trigger Time as punch total time: ' . $past_trigger_time, __FILE__, __LINE__, __METHOD__, 10); } else { Debug::text('NOT Using Past Trigger Time as punch total time: ' . $past_trigger_time, __FILE__, __LINE__, __METHOD__, 10); } $total_time = $punch_total_time; if ($pp_obj->getMinimumTime() > 0 or $pp_obj->getMaximumTime() > 0) { $premium_policy_daily_total_time = (int) $udtlf->getPremiumPolicySumByUserDateIDAndPremiumPolicyID($this->getUserDateID(), $pp_obj->getId()); Debug::text(' Premium Policy Daily Total Time: ' . $premium_policy_daily_total_time . ' Minimum Time: ' . $pp_obj->getMinimumTime() . ' Maximum Time: ' . $pp_obj->getMaximumTime(), __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getMinimumTime() > 0) { //FIXME: Split the minimum time up between all the punches somehow. if ($i == $udtlf->getRecordCount() and bcadd($premium_policy_daily_total_time, $total_time) < $pp_obj->getMinimumTime()) { $total_time = bcsub($pp_obj->getMinimumTime(), $premium_policy_daily_total_time); } } Debug::text(' Total Time After Minimum is applied: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); if ($pp_obj->getMaximumTime() > 0) { if ($total_time > $pp_obj->getMaximumTime()) { Debug::text(' aMore than Maximum Time...', __FILE__, __LINE__, __METHOD__, 10); $total_time = $pp_obj->getMaximumTime(); } elseif (bcadd($premium_policy_daily_total_time, $total_time) > $pp_obj->getMaximumTime()) { Debug::text(' bMore than Maximum Time...', __FILE__, __LINE__, __METHOD__, 10); //$total_time = bcsub( bcadd( $premium_policy_daily_total_time, $total_time ), $pp_obj->getMaximumTime() ); $total_time = bcsub($total_time, bcsub(bcadd($premium_policy_daily_total_time, $total_time), $pp_obj->getMaximumTime())); } } } Debug::text(' Premium Punch Total Time: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); if ($total_time > 0) { Debug::text(' Applying Premium Time!: ' . $total_time, __FILE__, __LINE__, __METHOD__, 10); $udtf = new UserDateTotalFactory(); $udtf->setUserDateID($this->getUserDateID()); $udtf->setStatus(10); //System $udtf->setType(40); //Premium $udtf->setPremiumPolicyId($pp_obj->getId()); $udtf->setBranch($udt_obj->getBranch()); $udtf->setDepartment($udt_obj->getDepartment()); $udtf->setJob($udt_obj->getJob()); $udtf->setJobItem($udt_obj->getJobItem()); $udtf->setQuantity($udt_obj->getQuantity()); $udtf->setBadQuantity($udt_obj->getBadQuantity()); $udtf->setTotalTime($total_time); $udtf->setEnableCalcSystemTotalTime(FALSE); if ($udtf->isValid() == TRUE) { $udtf->Save(); } unset($udtf); } else { Debug::text(' Premium Punch Total Time is 0...', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text('Not Past Trigger Time Yet or Punch Time is 0...', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Task Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Task Group Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Job Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Job Group Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Department Criteria!', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::text(' Shift Differential... DOES NOT Meet Branch Criteria!', __FILE__, __LINE__, __METHOD__, 10); } $i++; } } } break; } } } $profiler->stopTimer("UserDateTotal::calcPremiumPolicyTotalTime() - Part 1"); return TRUE; }