if (isset($data['recalculate_start_date']) and isset($data['recalculate_end_date']) and $data['recalculate_start_date'] < $data['recalculate_end_date']) {
                     Redirect::Page(URLBuilder::getURL(array('action' => 'recalculate_accrual_policy', 'data' => array('accrual_policy_id' => $ap_id, 'start_date' => $data['recalculate_start_date'], 'end_date' => $data['recalculate_end_date']), 'next_page' => urlencode(URLBuilder::getURL(NULL, '../policy/AccrualPolicyList.php'))), '../progress_bar/ProgressBarControl.php'), FALSE);
                 }
             }
             Redirect::Page(URLBuilder::getURL(NULL, 'AccrualPolicyList.php'));
             break;
         }
     }
     $apf->FailTransaction();
 default:
     if (isset($id)) {
         BreadCrumb::setCrumb($title);
         $aplf = new AccrualPolicyListFactory();
         $apmlf = new AccrualPolicyMilestoneListFactory();
         $aplf->getByIdAndCompanyID($id, $current_company->getID());
         if ($aplf->getRecordCount() > 0) {
             $apmlf->getByAccrualPolicyId($id);
             if ($apmlf->getRecordCount() > 0) {
                 foreach ($apmlf as $apm_obj) {
                     $milestone_rows[$apm_obj->getId()] = array('id' => $apm_obj->getId(), 'length_of_service' => $apm_obj->getLengthOfService(), 'length_of_service_unit_id' => $apm_obj->getLengthOfServiceUnit(), 'accrual_rate' => $apm_obj->getAccrualRate(), 'maximum_time' => $apm_obj->getMaximumTime());
                 }
             } else {
                 $milestone_rows[-1] = array('id' => -1, 'length_of_service' => 0, 'accrual_rate' => 0, 'minimum_time' => 0, 'maximum_time' => 0);
             }
             foreach ($aplf as $ap_obj) {
                 //Debug::Arr($station,'Department', __FILE__, __LINE__, __METHOD__,10);
                 $data = array('id' => $ap_obj->getId(), 'name' => $ap_obj->getName(), 'type_id' => $ap_obj->getType(), 'enable_pay_stub_balance_display' => $ap_obj->getEnablePayStubBalanceDisplay(), 'apply_frequency_id' => $ap_obj->getApplyFrequency(), 'apply_frequency_month' => $ap_obj->getApplyFrequencyMonth(), 'apply_frequency_day_of_month' => $ap_obj->getApplyFrequencyDayOfMonth(), 'apply_frequency_day_of_week' => $ap_obj->getApplyFrequencyDayOfWeek(), 'milestone_rollover_hire_date' => $ap_obj->getMilestoneRolloverHireDate(), 'milestone_rollover_month' => $ap_obj->getMilestoneRolloverMonth(), 'milestone_rollover_day_of_month' => $ap_obj->getMilestoneRolloverDayOfMonth(), 'minimum_employed_days' => $ap_obj->getMinimumEmployedDays(), 'recalculate_start_date' => TTDate::getBeginMonthEpoch(time()), 'recalculate_end_date' => TTDate::getEndMonthEpoch(time()), 'milestone_rows' => $milestone_rows, 'created_date' => $ap_obj->getCreatedDate(), 'created_by' => $ap_obj->getCreatedBy(), 'updated_date' => $ap_obj->getUpdatedDate(), 'updated_by' => $ap_obj->getUpdatedBy(), 'deleted_date' => $ap_obj->getDeletedDate(), 'deleted_by' => $ap_obj->getDeletedBy());
             }
         }
     } elseif ($action == 'add_milestone') {
         Debug::Text('Adding Blank Week', __FILE__, __LINE__, __METHOD__, 10);
 function getAbsencePolicyData($absence_policy_id)
 {
     global $current_company;
     if (!is_object($current_company)) {
         return FALSE;
     }
     $aplf = new AbsencePolicyListFactory();
     $aplf->getByIdAndCompanyId($absence_policy_id, $current_company->getId());
     if ($aplf->getRecordCount() > 0) {
         $ap_obj = $aplf->getCurrent();
         $ap_data = $ap_obj->getObjectAsArray();
         $aplf = new AccrualPolicyListFactory();
         $aplf->getByIdAndCompanyId($ap_obj->getAccrualPolicyID(), $current_company->getId());
         if ($aplf->getRecordCount() > 0) {
             $ap_data['accrual_policy_name'] = $aplf->getCurrent()->getName();
         } else {
             $ap_data['accrual_policy_name'] = 'None';
         }
         return $ap_data;
     }
     return FALSE;
 }
 function calcAccrualPolicy()
 {
     //FIXME: There is a minor bug for hour based accruals that if a milestone has a maximum limit,
     //  and an employee recalculates there timesheet, and the limit is reached midweek, if its recalculated
     //  again, the days that get the accrual time won't always be in order because the accrual balance is deleted
     //  only for the day currently being calculated, so on Monday it will delete 1hr of accrual, but the balance will
     //  still include Tue,Wed,Thu and the limit may already be reached.
     //We still need to calculate accruals even if the total time is 0, because we may want to override a
     //policy to 0hrs, and if we skip entries with TotalTime() == 0, the accruals won't be updated.
     if ($this->getDeleted() == FALSE) {
         Debug::text('Calculating Accrual Policies... Total Time: ' . $this->getTotalTime() . ' Date: ' . TTDate::getDate('DATE', $this->getUserDateObject()->getDateStamp()), __FILE__, __LINE__, __METHOD__, 10);
         //Calculate accrual policies assigned to other overtime/premium/absence policies
         //Debug::text('ID: '. $this->getId() .' Overtime Policy ID: '. (int)$this->getOverTimePolicyID()  .' Premium Policy ID: '. (int)$this->getPremiumPolicyID() .' Absence Policy ID: '. (int)$this->getAbsencePolicyID(), __FILE__, __LINE__, __METHOD__, 10);
         //If overtime, premium or absence policy is an accrual, handle that now.
         if ($this->getOverTimePolicyID() != FALSE) {
             $accrual_policy_id = $this->getOverTimePolicyObject()->getAccrualPolicyID();
             Debug::text('Over Time Accrual Policy ID: ' . $accrual_policy_id, __FILE__, __LINE__, __METHOD__, 10);
             if ($accrual_policy_id > 0) {
                 Debug::text('Over Time Accrual Rate: ' . $this->getOverTimePolicyObject()->getAccrualRate() . ' Policy ID: ' . $this->getOverTimePolicyObject()->getAccrualPolicyID(), __FILE__, __LINE__, __METHOD__, 10);
                 $af = new AccrualFactory();
                 $af->setUser($this->getUserDateObject()->getUser());
                 $af->setAccrualPolicyID($accrual_policy_id);
                 $af->setUserDateTotalID($this->getID());
                 $accrual_amount = bcmul($this->getTotalTime(), $this->getOverTimePolicyObject()->getAccrualRate());
                 if ($accrual_amount > 0) {
                     $af->setType(10);
                     //Banked
                 } else {
                     $af->setType(20);
                     //Used
                 }
                 $af->setAmount($accrual_amount);
                 $af->setEnableCalcBalance(TRUE);
                 if ($af->isValid()) {
                     $af->Save();
                 }
                 unset($accrual_amount);
             } else {
                 Debug::text('Skipping Over Time Accrual Policy ID: ' . $accrual_policy_id, __FILE__, __LINE__, __METHOD__, 10);
             }
         }
         if ($this->getPremiumPolicyID() != FALSE) {
             $accrual_policy_id = $this->getPremiumPolicyObject()->getAccrualPolicyID();
             Debug::text('Premium Accrual Policy ID: ' . $accrual_policy_id, __FILE__, __LINE__, __METHOD__, 10);
             if ($accrual_policy_id > 0) {
                 $af = new AccrualFactory();
                 $af->setUser($this->getUserDateObject()->getUser());
                 $af->setAccrualPolicyID($accrual_policy_id);
                 $af->setUserDateTotalID($this->getID());
                 $accrual_amount = bcmul($this->getTotalTime(), $this->getPremiumPolicyObject()->getAccrualRate());
                 if ($accrual_amount > 0) {
                     $af->setType(10);
                     //Banked
                 } else {
                     $af->setType(20);
                     //Used
                 }
                 $af->setAmount($accrual_amount);
                 $af->setEnableCalcBalance(TRUE);
                 if ($af->isValid()) {
                     $af->Save();
                 }
                 unset($accrual_amount);
             }
         }
         if ($this->getAbsencePolicyID() != FALSE) {
             $accrual_policy_id = $this->getAbsencePolicyObject()->getAccrualPolicyID();
             Debug::text('Absence Accrual Policy ID: ' . $accrual_policy_id, __FILE__, __LINE__, __METHOD__, 10);
             if ($accrual_policy_id > 0) {
                 $af = new AccrualFactory();
                 $af->setUser($this->getUserDateObject()->getUser());
                 $af->setAccrualPolicyID($accrual_policy_id);
                 $af->setUserDateTotalID($this->getID());
                 //By default we withdraw from accrual policy, so if there is a negative rate, deposit instead.
                 $accrual_amount = bcmul($this->getTotalTime(), bcmul($this->getAbsencePolicyObject()->getAccrualRate(), -1));
                 if ($accrual_amount > 0) {
                     $af->setType(10);
                     //Banked
                 } else {
                     $af->setType(20);
                     //Used
                 }
                 $af->setAmount($accrual_amount);
                 $af->setEnableCalcBalance(TRUE);
                 if ($af->isValid()) {
                     $af->Save();
                 }
             }
         }
         unset($af, $accrual_policy_id);
         //Calculate any hour based accrual policies.
         //if ( $this->getType() == 10 AND $this->getStatus() == 10 ) {
         if ($this->getStatus() == 10 and in_array($this->getType(), array(20, 30))) {
             //Calculate hour based accruals on regular/overtime only.
             $aplf = new AccrualPolicyListFactory();
             $aplf->getByPolicyGroupUserIdAndType($this->getUserDateObject()->getUser(), 30);
             if ($aplf->getRecordCount() > 0) {
                 Debug::text('Found Hour Based Accrual Policies to apply.', __FILE__, __LINE__, __METHOD__, 10);
                 foreach ($aplf as $ap_obj) {
                     if ($ap_obj->getMinimumEmployedDays() == 0 or TTDate::getDays($this->getUserDateObject()->getDateStamp() - $this->getUserDateObject()->getUserObject()->getHireDate()) >= $ap_obj->getMinimumEmployedDays()) {
                         Debug::Text('&nbsp;&nbsp;User has been employed long enough.', __FILE__, __LINE__, __METHOD__, 10);
                         $milestone_obj = $ap_obj->getActiveMilestoneObject($this->getUserDateObject()->getUserObject(), $this->getUserDateObject()->getDateStamp());
                         $accrual_balance = $ap_obj->getCurrentAccrualBalance($this->getUserDateObject()->getUserObject()->getId(), $ap_obj->getId());
                         //If Maximum time is set to 0, make that unlimited.
                         if (is_object($milestone_obj) and ($milestone_obj->getMaximumTime() == 0 or $accrual_balance < $milestone_obj->getMaximumTime())) {
                             $accrual_amount = $ap_obj->calcAccrualAmount($milestone_obj, $this->getTotalTime(), 0);
                             if ($accrual_amount > 0) {
                                 $new_accrual_balance = bcadd($accrual_balance, $accrual_amount);
                                 //If Maximum time is set to 0, make that unlimited.
                                 if ($milestone_obj->getMaximumTime() > 0 and $new_accrual_balance > $milestone_obj->getMaximumTime()) {
                                     $accrual_amount = bcsub($milestone_obj->getMaximumTime(), $accrual_balance, 4);
                                 }
                                 Debug::Text('&nbsp;&nbsp; Min/Max Adjusted Accrual Amount: ' . $accrual_amount . ' Limits: Min: ' . $milestone_obj->getMinimumTime() . ' Max: ' . $milestone_obj->getMaximumTime(), __FILE__, __LINE__, __METHOD__, 10);
                                 $af = new AccrualFactory();
                                 $af->setUser($this->getUserDateObject()->getUserObject()->getId());
                                 $af->setType(75);
                                 //Accrual Policy
                                 $af->setAccrualPolicyID($ap_obj->getId());
                                 $af->setUserDateTotalID($this->getID());
                                 $af->setAmount($accrual_amount);
                                 $af->setTimeStamp($this->getUserDateObject()->getDateStamp());
                                 $af->setEnableCalcBalance(TRUE);
                                 if ($af->isValid()) {
                                     $af->Save();
                                 }
                                 unset($accrual_amount, $accrual_balance, $new_accrual_balance);
                             } else {
                                 Debug::Text('&nbsp;&nbsp; Accrual Amount is 0...', __FILE__, __LINE__, __METHOD__, 10);
                             }
                         } else {
                             Debug::Text('&nbsp;&nbsp; Accrual Balance is outside Milestone Range. Or no milestone found. Skipping...', __FILE__, __LINE__, __METHOD__, 10);
                         }
                     } else {
                         Debug::Text('&nbsp;&nbsp;User has only been employed: ' . TTDate::getDays($this->getUserDateObject()->getDateStamp() - $this->getUserDateObject()->getUserObject()->getHireDate()) . ' Days, not enough.', __FILE__, __LINE__, __METHOD__, 10);
                     }
                 }
             } else {
                 Debug::text('No Hour Based Accrual Policies to apply.', __FILE__, __LINE__, __METHOD__, 10);
             }
         } else {
             Debug::text('No worked time on this day or not proper type/status, skipping hour based accrual policies...', __FILE__, __LINE__, __METHOD__, 10);
         }
     }
     /*
     //FIXME: Figure a better way to re-calculate accrual policies assigned to absences to update accrual balances
     if ( $this->getEnableCalcAccrualPolicy() == TRUE ) {
     	Debug::text('Recalculating Accruals assigned to absence policies...', __FILE__, __LINE__, __METHOD__, 10);
     
     	$udtlf = new UserDateTotalListFactory();
     	$udtlf->getByUserDateIdAndStatus( $this->getUserDateID(), 30 ); //Absences only
     	if ( $udtlf->getRecordCount() > 0 ) {
     		foreach( $udtlf as $udt_obj ) {
     			$accrual_policy_id = $udt_obj->getAbsencePolicyObject()->getAccrualPolicyID();
     			Debug::text('Absence Accrual Policy ID: '. $accrual_policy_id, __FILE__, __LINE__, __METHOD__, 10);
     
     			if ( $accrual_policy_id > 0 AND $this->getTotalTime() > 0 ) {
     				$af = new AccrualFactory();
     				$af->setUser( $this->getUserDateObject()->getUser() );
     				$af->setAccrualPolicyID( $accrual_policy_id );
     				$af->setType(20);
     				$af->setUserDateTotalID( $udt_obj->getID() );
     				$af->setAmount( bcmul( $udt_obj->getTotalTime(), -1 ) );
     				$af->setEnableCalcBalance(TRUE);
     				if ( $af->isValid() ) {
     					$af->Save();
     				}
     			}
     		}
     	}
     } else {
     	Debug::text('NOT Recalculating Accruals assigned to absence policies...', __FILE__, __LINE__, __METHOD__, 10);
     }
     */
     return TRUE;
 }
 function getProjectedAbsencePolicyBalance($absence_policy_id, $user_id, $epoch, $amount, $previous_amount = 0)
 {
     if ($absence_policy_id == '') {
         return $this->returnHandler(FALSE);
     }
     if ($user_id == '') {
         return $this->returnHandler(FALSE);
     }
     $epoch = TTDate::parseDateTime($epoch);
     $aplf = TTnew('AbsencePolicyListFactory');
     $aplf->getByIdAndCompanyId($absence_policy_id, $this->getCurrentCompanyObject()->getId());
     if ($aplf->getRecordCount() > 0) {
         $ap_obj = $aplf->getCurrent();
         if ($ap_obj->getAccrualPolicyID() != '') {
             $acplf = new AccrualPolicyListFactory();
             $acplf->getById($ap_obj->getAccrualPolicyID());
             if ($acplf->getRecordCount() > 0) {
                 $acp_obj = $acplf->getCurrent();
                 $ulf = TTnew('UserListFactory');
                 $ulf->getByIDAndCompanyID($user_id, $this->getCurrentCompanyObject()->getId());
                 if ($ulf->getRecordCount() == 1) {
                     $u_obj = $ulf->getCurrent();
                     return $this->returnHandler($acp_obj->getAccrualBalanceWithProjection($u_obj, $epoch, $amount, $previous_amount));
                 }
             }
         }
     }
     return $this->returnHandler(FALSE);
 }
 function getAccrualPolicyObject()
 {
     if (is_object($this->accrual_policy_obj)) {
         return $this->accrual_policy_obj;
     } else {
         $aplf = new AccrualPolicyListFactory();
         $aplf->getById($this->getAccrualPolicyID());
         if ($aplf->getRecordCount() > 0) {
             $this->accrual_policy_obj = $aplf->getCurrent();
             return $this->accrual_policy_obj;
         }
         return FALSE;
     }
 }