Exemplo n.º 1
0
             foreach ($udlf as $ud_obj) {
                 $ud_obj->setDeleted($delete);
                 if ($ud_obj->isValid()) {
                     $ud_obj->Save();
                 }
             }
         }
     }
     Redirect::Page(URLBuilder::getURL(array('user_id' => $user_id), 'UserDeductionList.php'));
     break;
 default:
     BreadCrumb::setCrumb($title);
     //Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
     $hlf = new HierarchyListFactory();
     $permission_children_ids = $hlf->getHierarchyChildrenByCompanyIdAndUserIdAndObjectTypeID($current_company->getId(), $current_user->getId());
     $udlf = new UserDeductionListFactory();
     $udlf->getByCompanyIdAndUserId($current_company->getId(), $user_id);
     $pager = new Pager($udlf);
     $ulf->getByIdAndCompanyId($user_id, $current_company->getId());
     if ($ulf->getRecordCount() > 0) {
         $user_obj = $ulf->getCurrent();
         if (is_object($user_obj)) {
             $is_owner = $permission->isOwner($user_obj->getCreatedBy(), $user_obj->getID());
             $is_child = $permission->isChild($user_obj->getId(), $permission_children_ids);
             if ($permission->Check('user_tax_deduction', 'view') or $permission->Check('user_tax_deduction', 'view_own') and $is_owner === TRUE or $permission->Check('user_tax_deduction', 'view_child') and $is_child === TRUE) {
                 foreach ($udlf as $ud_obj) {
                     $cd_obj = $ud_obj->getCompanyDeductionObject();
                     $rows[] = array('id' => $ud_obj->getId(), 'status_id' => $cd_obj->getStatus(), 'user_id' => $ud_obj->getUser(), 'name' => $cd_obj->getName(), 'type_id' => $cd_obj->getType(), 'type' => Option::getByKey($cd_obj->getType(), $cd_obj->getOptions('type')), 'calculation' => Option::getByKey($cd_obj->getCalculation(), $cd_obj->getOptions('calculation')), 'is_owner' => $is_owner, 'is_child' => $is_child, 'deleted' => $ud_obj->getDeleted());
                 }
             }
         }
 function postSave()
 {
     $this->removeCache($this->getId());
     $this->removeCache('include_pay_stub_entry-' . $this->getId());
     $this->removeCache('exclude_pay_stub_entry-' . $this->getId());
     if ($this->getDeleted() == TRUE) {
         //Check if any users are assigned to this, if so, delete mappings.
         $udlf = new UserDeductionListFactory();
         $udlf->StartTransaction();
         $udlf->getByCompanyIdAndCompanyDeductionId($this->getCompany(), $this->getId());
         if ($udlf->getRecordCount()) {
             foreach ($udlf as $ud_obj) {
                 $ud_obj->setDeleted(TRUE);
                 if ($ud_obj->isValid()) {
                     $ud_obj->Save();
                 }
             }
         }
         $udlf->CommitTransaction();
     }
     return TRUE;
 }
Exemplo n.º 3
0
 }
 $ulf = new UserListFactory();
 $user_obj = $ulf->getById($row['user_id'])->getCurrent();
 $t4ee = new T4Employee();
 $t4ee->setSin($row['sin']);
 $t4ee->setFirstName($user_obj->getFirstName());
 $t4ee->setMiddleName($user_obj->getMiddleName());
 $t4ee->setLastName($user_obj->getLastName());
 $t4ee->setAddress1($user_obj->getAddress1());
 $t4ee->setAddress2($user_obj->getAddress2());
 $t4ee->setCity($user_obj->getCity());
 $t4ee->setProvince($user_obj->getProvince());
 $t4ee->setPostalCode($user_obj->getPostalCode());
 //$t4ee->setEmployementCode( );
 //Get User Tax / Deductions by Pay Stub Account.
 $udlf = new UserDeductionListFactory();
 if (isset($setup_data['employee_cpp_psea_id'])) {
     $udlf->getByUserIdAndPayStubEntryAccountID($user_obj->getId(), $setup_data['employee_cpp_psea_id']);
     if ($setup_data['employee_cpp_psea_id'] != 0 and $udlf->getRecordCount() == 0) {
         Debug::Text('CPP Exempt!', __FILE__, __LINE__, __METHOD__, 10);
         $t4ee->setExemptCPP(TRUE);
     }
 }
 if (isset($setup_data['employee_ei_psea_id'])) {
     $udlf->getByUserIdAndPayStubEntryAccountID($user_obj->getId(), $setup_data['employee_ei_psea_id']);
     if ($setup_data['employee_ei_psea_id'] != 0 and $udlf->getRecordCount() == 0) {
         Debug::Text('EI Exempt!', __FILE__, __LINE__, __METHOD__, 10);
         $t4ee->setExemptEI(TRUE);
     }
 }
 $t4ee->setEmploymentIncome($row['income']);
Exemplo n.º 4
0
 $language_options = TTi18n::getLanguageArray();
 $date_format_options = $upf->getOptions('date_format');
 $time_format_options = $upf->getOptions('time_format');
 $time_unit_format_options = $upf->getOptions('time_unit_format');
 $timesheet_view_options = $upf->getOptions('timesheet_view');
 $start_week_day_options = $upf->getOptions('start_week_day');
 $time_zone_options = $upf->getOptions('time_zone');
 $uwlf = new UserWageListFactory();
 $uwlf->getLastWageByUserIdAndDate($filter_data['user_ids'], TTDate::getTime());
 if ($uwlf->getRecordCount() > 0) {
     foreach ($uwlf as $uw_obj) {
         $user_wage[$uw_obj->getUser()] = array('type_id' => $uw_obj->getType(), 'type' => Option::getByKey($uw_obj->getType(), $uw_obj->getOptions('type')), 'wage' => $uw_obj->getWage(), 'effective_date' => $uw_obj->getEffectiveDate());
     }
     //var_dump($user_wage);
 }
 $udlf = new UserDeductionListFactory();
 $udlf->getByCompanyIdAndUserId($current_company->getId(), $filter_data['user_ids']);
 if ($udlf->getRecordCount() > 0) {
     foreach ($udlf as $ud_obj) {
         //Get UserValue options
         $user_value_1_options = $ud_obj->getCompanyDeductionObject()->getUserValue1Options();
         if ($ud_obj->getUserValue1() !== FALSE) {
             $tmp_user_value = $ud_obj->getUserValue1();
         } elseif ($ud_obj->getCompanyDeductionObject()->getUserValue1()) {
             $tmp_user_value = $ud_obj->getCompanyDeductionObject()->getUserValue1();
         } else {
             $tmp_user_value = NULL;
         }
         if (is_array($user_value_1_options)) {
             $user_values[] = Option::getByKey($tmp_user_value, $user_value_1_options);
         } else {
Exemplo n.º 5
0
             } else {
                 $permission->Redirect(FALSE);
                 //Redirect
                 exit;
             }
         }
     }
 } else {
     Debug::Text('Adding... ', __FILE__, __LINE__, __METHOD__, 10);
     //Adding User Deductions...
     $data['add'] = 1;
     $data['user_id'] = $user_id;
     //Get all Company Deductions for drop down box.
     $cdlf = new CompanyDeductionListFactory();
     $data['deduction_options'] = $cdlf->getByCompanyIdAndStatusIdArray($current_company->getId(), 10, FALSE);
     $udlf = new UserDeductionListFactory();
     $udlf->getByCompanyIdAndUserId($current_company->getId(), $user_id);
     if ($udlf->getRecordCount() > 0) {
         //Remove deductions from select box that are already assigned to user.
         $deduction_ids = array_keys($data['deduction_options']);
         foreach ($udlf as $ud_obj) {
             if (in_array($ud_obj->getCompanyDeduction(), $deduction_ids)) {
                 unset($data['deduction_options'][$ud_obj->getCompanyDeduction()]);
             }
         }
     }
 }
 //Get user full name
 $ulf = new UserListFactory();
 $ulf->getByIdAndCompanyId($data['user_id'], $current_company->getId());
 if ($ulf->getRecordCount() > 0) {
 function getDeductionAmount($user_id, $pay_stub_obj, $pay_period_obj)
 {
     if ($user_id == '') {
         Debug::Text('Missing User ID: ', __FILE__, __LINE__, __METHOD__, 10);
         return FALSE;
     }
     if (!is_object($pay_stub_obj)) {
         Debug::Text('Missing Pay Stub Object: ', __FILE__, __LINE__, __METHOD__, 10);
         return FALSE;
     }
     if (!is_object($pay_period_obj)) {
         Debug::Text('Missing Pay Period Object: ', __FILE__, __LINE__, __METHOD__, 10);
         return FALSE;
     }
     //Calculates the deduction.
     $cd_obj = $this->getCompanyDeductionObject();
     $annual_pay_periods = $pay_period_obj->getPayPeriodScheduleObject()->getAnnualPayPeriods();
     if (!is_object($cd_obj)) {
         return FALSE;
     }
     require_once Environment::getBasePath() . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'payroll_deduction' . DIRECTORY_SEPARATOR . 'PayrollDeduction.class.php';
     $retval = 0;
     Debug::Text('Company Deduction: ID: ' . $cd_obj->getID() . ' Name: ' . $cd_obj->getName() . ' Calculation ID: ' . $cd_obj->getCalculation(), __FILE__, __LINE__, __METHOD__, 10);
     switch ($cd_obj->getCalculation()) {
         case 10:
             //Basic Percent
             if ($this->getUserValue1() == '') {
                 $percent = $cd_obj->getUserValue1();
             } else {
                 $percent = $this->getUserValue1();
             }
             $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
             $retval = bcmul($amount, bcdiv($percent, 100));
             break;
         case 15:
             //Advanced Percent
             if ($this->getUserValue1() == '') {
                 $percent = $cd_obj->getUserValue1();
             } else {
                 $percent = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $wage_base = $cd_obj->getUserValue2();
             } else {
                 $wage_base = $this->getUserValue2();
             }
             if ($this->getUserValue3() == '') {
                 $exempt_amount = $cd_obj->getUserValue3();
             } else {
                 $exempt_amount = $this->getUserValue3();
             }
             //Annual Wage Base is the maximum earnings that an employee can earn before they are no longer eligible for this deduction
             //Annual Deduction Amount
             Debug::Text('Percent: ' . $percent . ' Wage Base: ' . $wage_base . ' Exempt Amount: ' . $exempt_amount, __FILE__, __LINE__, __METHOD__, 10);
             if ($percent != 0) {
                 if ($exempt_amount > 0) {
                     $amount = bcsub($cd_obj->getCalculationPayStubAmount($pay_stub_obj), bcdiv($exempt_amount, $annual_pay_periods));
                     Debug::Text('Amount After Exemption: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                 } else {
                     $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
                     Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                 }
                 if ($wage_base > 0) {
                     //*NOTE: If the first pay stub in TimeTrex is near the end of the year, and the employee has already exceeded the wage base amount
                     //the payroll admin needs to make sure they add a YTD Adjustment for each Include PS Accounts that this calculation is based on,
                     //NOT the total amount they have paid for the resulting calculation, as that has no effect whatsoever.
                     //getCalculationYTDAmount is the previous pay stub YTD amount, but it includes any YTD Adjustments in the current pay stub too.
                     $ytd_amount = $cd_obj->getCalculationYTDAmount($pay_stub_obj);
                     Debug::Text('Wage Base is set: ' . $wage_base . ' Amount: ' . $amount . ' Current YTD: ' . $ytd_amount, __FILE__, __LINE__, __METHOD__, 10);
                     //Possible calcations:
                     //
                     //Wage Base: 3000
                     //Amount: 500 YTD: 0		= 500
                     //Amount: 500 YTD: 2900		= 100
                     //Amount: 500 YTD: 3100		= 0
                     //Amount: 3500 YTD: 0		= 3000
                     //AMount: 3500 YTD: 2900	= 100
                     //Amount: 3500 YTD: 3100	= 0
                     //Check to see if YTD is less than wage base.
                     $remaining_wage_base = bcsub($wage_base, $ytd_amount);
                     Debug::Text('Remaining Wage Base to be calculated: ' . $remaining_wage_base, __FILE__, __LINE__, __METHOD__, 10);
                     if ($remaining_wage_base > 0) {
                         if ($amount > $remaining_wage_base) {
                             $amount = $remaining_wage_base;
                         }
                     } else {
                         $amount = 0;
                         //Exceeded wage base, nothing to calculate.
                     }
                     unset($remaining_wage_base);
                 } else {
                     Debug::Text('Wage Base is NOT set: ' . $wage_base, __FILE__, __LINE__, __METHOD__, 10);
                 }
                 $retval = bcmul($amount, bcdiv($percent, 100));
             } else {
                 $retval = 0;
             }
             if ($percent >= 0 and $retval < 0) {
                 $retval = 0;
             }
             unset($amount, $ytd_amount, $percent, $wage_base);
             break;
         case 17:
             //Advanced Percent (Range Bracket)
             if ($this->getUserValue1() == '') {
                 $percent = $cd_obj->getUserValue1();
             } else {
                 $percent = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $min_wage = $cd_obj->getUserValue2();
             } else {
                 $min_wage = $this->getUserValue2();
             }
             if ($this->getUserValue3() == '') {
                 $max_wage = $cd_obj->getUserValue3();
             } else {
                 $max_wage = $this->getUserValue3();
             }
             if ($this->getUserValue4() == '') {
                 $annual_deduction_amount = $cd_obj->getUserValue4();
             } else {
                 $annual_deduction_amount = $this->getUserValue4();
             }
             if ($this->getUserValue5() == '') {
                 $annual_fixed_amount = $cd_obj->getUserValue5();
             } else {
                 $annual_fixed_amount = $this->getUserValue5();
             }
             $min_wage = bcdiv($min_wage, $annual_pay_periods);
             $max_wage = bcdiv($max_wage, $annual_pay_periods);
             $annual_deduction_amount = bcdiv($annual_deduction_amount, $annual_pay_periods);
             $annual_fixed_amount = bcdiv($annual_fixed_amount, $annual_pay_periods);
             Debug::Text('Percent: ' . $percent . ' Min Wage: ' . $min_wage . ' Max Wage: ' . $max_wage . ' Annual Deduction: ' . $annual_deduction_amount, __FILE__, __LINE__, __METHOD__, 10);
             if ($percent != 0) {
                 $amount = bcsub($cd_obj->getCalculationPayStubAmount($pay_stub_obj), $annual_deduction_amount);
                 Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                 if ($amount >= $min_wage and $amount <= $max_wage) {
                     $retval = bcadd(bcmul($amount, bcdiv($percent, 100)), $annual_fixed_amount);
                 }
             } else {
                 $retval = 0;
             }
             if ($percent >= 0 and $retval < 0) {
                 $retval = 0;
             }
             unset($amount, $percent, $min_wage, $max_wage, $annual_deduction_amount, $annual_fixed_amount);
             break;
         case 18:
             //Advanced Percent (Tax Bracket)
             if ($this->getUserValue1() == '') {
                 $percent = $cd_obj->getUserValue1();
             } else {
                 $percent = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $wage_base = $cd_obj->getUserValue2();
             } else {
                 $wage_base = $this->getUserValue2();
             }
             if ($this->getUserValue3() == '') {
                 $exempt_amount = $cd_obj->getUserValue3();
             } else {
                 $exempt_amount = $this->getUserValue3();
             }
             if ($this->getUserValue4() == '') {
                 $annual_deduction_amount = $cd_obj->getUserValue4();
             } else {
                 $annual_deduction_amount = $this->getUserValue4();
             }
             Debug::Text('Percent: ' . $percent . ' Wage Base: ' . $wage_base . ' Exempt Amount: ' . $exempt_amount, __FILE__, __LINE__, __METHOD__, 10);
             if ($percent != 0) {
                 if ($exempt_amount > 0) {
                     $pp_exempt_amount = bcdiv($exempt_amount, $annual_pay_periods);
                 } else {
                     $pp_exempt_amount = 0;
                 }
                 //Debug::Text('PP Exempt Amount: '. $pp_exempt_amount, __FILE__, __LINE__, __METHOD__,10);
                 if ($wage_base > 0) {
                     $pp_wage_base_amount = bcdiv($wage_base, $annual_pay_periods);
                 } else {
                     $pp_wage_base_amount = 0;
                 }
                 if ($annual_deduction_amount > 0) {
                     $pp_annual_deduction_amount = bcdiv($annual_deduction_amount, $annual_pay_periods);
                 } else {
                     $pp_annual_deduction_amount = 0;
                 }
                 //Debug::Text('PP Wage Base Base Amount: '. $pp_wage_base_amount, __FILE__, __LINE__, __METHOD__,10);
                 $amount = bcsub($cd_obj->getCalculationPayStubAmount($pay_stub_obj), $pp_annual_deduction_amount);
                 //Debug::Text('Calculation Pay Stub Amount: '. $cd_obj->getCalculationPayStubAmount( $pay_stub_obj ), __FILE__, __LINE__, __METHOD__,10);
                 if ($pp_wage_base_amount > 0 and $amount > $pp_wage_base_amount) {
                     //Debug::Text('Exceeds Wage Base...'. $amount, __FILE__, __LINE__, __METHOD__,10);
                     $amount = bcsub($pp_wage_base_amount, $pp_exempt_amount);
                 } else {
                     //Debug::Text('Under Wage Base...'. $amount, __FILE__, __LINE__, __METHOD__,10);
                     $amount = bcsub($amount, $pp_exempt_amount);
                 }
                 Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                 $retval = bcmul($amount, bcdiv($percent, 100));
             } else {
                 $retval = 0;
             }
             if ($percent >= 0 and $retval < 0) {
                 $retval = 0;
             }
             unset($amount, $percent, $wage_base, $pp_wage_base_amount, $pp_exempt_amount, $annual_deduction_amount, $pp_annual_deduction_amount);
             break;
         case 19:
             //Advanced Percent (Tax Bracket Alternate)
             /*
             	This is designed to be used for single line item tax calculations, in that the formula looks like this,
             	where only ONE bracket would be applied to the employee, NOT all:
             	Wage between 0 - 10,000 calculate 10%
             	Wage between 10,001 - 20,000 calculate 15% + $1000 (10% of 10,000 as per above)
             	Wage between 20,001 - 30,000 calculate 20% + $2500 (10% of 10,000 as first bracket, and 15% of 10,000 as per 2nd bracket)
             */
             if ($this->getUserValue1() == '') {
                 $percent = $cd_obj->getUserValue1();
             } else {
                 $percent = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $min_wage = $cd_obj->getUserValue2();
             } else {
                 $min_wage = $this->getUserValue2();
             }
             if ($this->getUserValue3() == '') {
                 $max_wage = $cd_obj->getUserValue3();
             } else {
                 $max_wage = $this->getUserValue3();
             }
             if ($this->getUserValue4() == '') {
                 $annual_deduction_amount = $cd_obj->getUserValue4();
             } else {
                 $annual_deduction_amount = $this->getUserValue4();
             }
             if ($this->getUserValue5() == '') {
                 $annual_fixed_amount = $cd_obj->getUserValue5();
             } else {
                 $annual_fixed_amount = $this->getUserValue5();
             }
             $min_wage = bcdiv($min_wage, $annual_pay_periods);
             $max_wage = bcdiv($max_wage, $annual_pay_periods);
             $annual_deduction_amount = bcdiv($annual_deduction_amount, $annual_pay_periods);
             $annual_fixed_amount = bcdiv($annual_fixed_amount, $annual_pay_periods);
             Debug::Text('Percent: ' . $percent . ' Min Wage: ' . $min_wage . ' Max Wage: ' . $max_wage . ' Annual Deduction: ' . $annual_deduction_amount, __FILE__, __LINE__, __METHOD__, 10);
             if ($percent != 0) {
                 $amount = bcsub($cd_obj->getCalculationPayStubAmount($pay_stub_obj), $annual_deduction_amount);
                 Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                 if ($amount >= $min_wage and $amount <= $max_wage) {
                     $retval = bcadd(bcmul(bcsub($amount, $min_wage), bcdiv($percent, 100)), $annual_fixed_amount);
                 }
             } else {
                 $retval = 0;
             }
             if ($percent >= 0 and $retval < 0) {
                 $retval = 0;
             }
             unset($amount, $percent, $min_wage, $max_wage, $annual_deduction_amount, $annual_fixed_amount);
             break;
         case 20:
             //Fixed amount
             if ($this->getUserValue1() == FALSE) {
                 $amount = $cd_obj->getUserValue1();
             } else {
                 $amount = $this->getUserValue1();
             }
             $retval = $amount;
             unset($amount);
             break;
         case 30:
             //Fixed Amount (Range Bracket)
             if ($this->getUserValue1() == '') {
                 $fixed_amount = $cd_obj->getUserValue1();
             } else {
                 $fixed_amount = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $min_wage = $cd_obj->getUserValue2();
             } else {
                 $min_wage = $this->getUserValue2();
             }
             if ($this->getUserValue3() == '') {
                 $max_wage = $cd_obj->getUserValue3();
             } else {
                 $max_wage = $this->getUserValue3();
             }
             if ($this->getUserValue4() == '') {
                 $annual_deduction_amount = $cd_obj->getUserValue4();
             } else {
                 $annual_deduction_amount = $this->getUserValue4();
             }
             $min_wage = bcdiv($min_wage, $annual_pay_periods);
             $max_wage = bcdiv($max_wage, $annual_pay_periods);
             $annual_deduction_amount = bcdiv($annual_deduction_amount, $annual_pay_periods);
             Debug::Text('Amount: ' . $fixed_amount . ' Min Wage: ' . $min_wage . ' Max Wage: ' . $max_wage . ' Annual Deduction: ' . $annual_deduction_amount, __FILE__, __LINE__, __METHOD__, 10);
             if ($fixed_amount != 0) {
                 $amount = bcsub($cd_obj->getCalculationPayStubAmount($pay_stub_obj), $annual_deduction_amount);
                 Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                 if ($amount >= $min_wage and $amount <= $max_wage) {
                     $retval = $fixed_amount;
                 }
             } else {
                 $retval = 0;
             }
             unset($fixed_amount, $amount, $percent, $min_wage, $max_wage, $annual_deduction_amount);
             break;
         case 52:
             //Fixed Amount (w/Limit)
             if ($this->getUserValue1() == '') {
                 $fixed_amount = $cd_obj->getUserValue1();
             } else {
                 $fixed_amount = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $target_amount = $cd_obj->getUserValue2();
             } else {
                 $target_amount = $this->getUserValue2();
             }
             Debug::Text('Amount: ' . $fixed_amount . ' Target Amount: ' . $target_amount, __FILE__, __LINE__, __METHOD__, 10);
             $retval = 0;
             if ($fixed_amount != 0) {
                 $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
                 Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                 if ($amount !== $target_amount) {
                     if (abs($fixed_amount) < abs(bcsub($amount, $target_amount))) {
                         //Use full fixed amount
                         Debug::Text('Not within reach of target, use full fixed amount...', __FILE__, __LINE__, __METHOD__, 10);
                         $retval = $fixed_amount;
                     } else {
                         Debug::Text('Within reach of target, use partial fixed amount...', __FILE__, __LINE__, __METHOD__, 10);
                         //Use partial fixed_amount
                         $retval = bcadd(abs($amount), $target_amount);
                     }
                 }
             }
             $retval = abs($retval);
             unset($fixed_amount, $tmp_amount, $amount, $min_limit, $max_limit);
             break;
         case 80:
             //US Earning Income Credit (EIC)
             if ($this->getUserValue1() == '') {
                 $user_value1 = $cd_obj->getUserValue1();
             } else {
                 $user_value1 = $this->getUserValue1();
             }
             Debug::Text('UserValue1: ' . $user_value1, __FILE__, __LINE__, __METHOD__, 10);
             $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
             Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('Annual Pay Periods: ' . $annual_pay_periods, __FILE__, __LINE__, __METHOD__, 10);
             $pd_obj = new PayrollDeduction('US', NULL);
             $pd_obj->setCompany($this->getUserObject()->getCompany());
             $pd_obj->setUser($this->getUser());
             $pd_obj->setDate($pay_period_obj->getTransactionDate());
             $pd_obj->setAnnualPayPeriods($annual_pay_periods);
             if (is_object($this->getUserObject())) {
                 $currency_id = $this->getUserObject()->getCurrency();
                 $pd_obj->setUserCurrency($currency_id);
                 Debug::Text('User Currency ID: ' . $currency_id, __FILE__, __LINE__, __METHOD__, 10);
             }
             $pd_obj->setEICFilingStatus($user_value1);
             $pd_obj->setGrossPayPeriodIncome($amount);
             //Allow negative value, infact it always should be.
             $retval = $pd_obj->getEIC();
             break;
         case 90:
             //Canada - CPP
             $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
             Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('Annual Pay Periods: ' . $annual_pay_periods, __FILE__, __LINE__, __METHOD__, 10);
             $pd_obj = new PayrollDeduction('CA', NULL);
             $pd_obj->setCompany($this->getUserObject()->getCompany());
             $pd_obj->setUser($this->getUser());
             $pd_obj->setDate($pay_period_obj->getTransactionDate());
             $pd_obj->setAnnualPayPeriods($annual_pay_periods);
             $pd_obj->setEnableCPPAndEIDeduction(TRUE);
             if ($this->getPayStubEntryAccountLinkObject()->getEmployeeCPP() != '') {
                 Debug::Text('Found Employee CPP account link!: ', __FILE__, __LINE__, __METHOD__, 10);
                 $previous_ytd_cpp_arr = $pay_stub_obj->getSumByEntriesArrayAndTypeIDAndPayStubAccountID('previous', NULL, $this->getPayStubEntryAccountLinkObject()->getEmployeeCPP());
                 $current_ytd_cpp_arr = $pay_stub_obj->getSumByEntriesArrayAndTypeIDAndPayStubAccountID('current', NULL, $this->getPayStubEntryAccountLinkObject()->getEmployeeCPP());
                 Debug::text('YTD CPP Contribution: Previous Amount: ' . $previous_ytd_cpp_arr['ytd_amount'] . ' Current Amount: ' . $current_ytd_cpp_arr['amount'], __FILE__, __LINE__, __METHOD__, 10);
                 $pd_obj->setYearToDateCPPContribution(bcadd($previous_ytd_cpp_arr['ytd_amount'], $current_ytd_cpp_arr['ytd_amount']));
                 unset($previous_ytd_cpp_arr, $current_ytd_cpp_arr);
             }
             $pd_obj->setGrossPayPeriodIncome($amount);
             $retval = $pd_obj->getEmployeeCPP();
             if ($retval < 0) {
                 $retval = 0;
             }
             break;
         case 91:
             //Canada - EI
             $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
             Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('Annual Pay Periods: ' . $annual_pay_periods, __FILE__, __LINE__, __METHOD__, 10);
             $pd_obj = new PayrollDeduction('CA', NULL);
             $pd_obj->setCompany($this->getUserObject()->getCompany());
             $pd_obj->setUser($this->getUser());
             $pd_obj->setDate($pay_period_obj->getTransactionDate());
             $pd_obj->setAnnualPayPeriods($annual_pay_periods);
             $pd_obj->setEnableCPPAndEIDeduction(TRUE);
             if ($this->getPayStubEntryAccountLinkObject()->getEmployeeEI() != '') {
                 Debug::Text('Found Employee EI account link!: ', __FILE__, __LINE__, __METHOD__, 10);
                 $previous_ytd_ei_arr = $pay_stub_obj->getSumByEntriesArrayAndTypeIDAndPayStubAccountID('previous', NULL, $this->getPayStubEntryAccountLinkObject()->getEmployeeEI());
                 $current_ytd_ei_arr = $pay_stub_obj->getSumByEntriesArrayAndTypeIDAndPayStubAccountID('current', NULL, $this->getPayStubEntryAccountLinkObject()->getEmployeeEI());
                 Debug::text('YTD EI Contribution: Previous Amount: ' . $previous_ytd_ei_arr['ytd_amount'] . ' Current Amount: ' . $current_ytd_ei_arr['amount'], __FILE__, __LINE__, __METHOD__, 10);
                 $pd_obj->setYearToDateEIContribution(bcadd($previous_ytd_ei_arr['ytd_amount'], $current_ytd_ei_arr['ytd_amount']));
                 unset($previous_ytd_ei_arr, $current_ytd_ei_arr);
             }
             $pd_obj->setGrossPayPeriodIncome($amount);
             $retval = $pd_obj->getEmployeeEI();
             if ($retval < 0) {
                 $retval = 0;
             }
             break;
         case 100:
             //Federal Income Tax
             if ($this->getUserValue1() == '') {
                 $user_value1 = $cd_obj->getUserValue1();
             } else {
                 $user_value1 = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $user_value2 = $cd_obj->getUserValue2();
             } else {
                 $user_value2 = $this->getUserValue2();
             }
             Debug::Text('UserValue1: ' . $user_value1, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('UserValue2: ' . $user_value2, __FILE__, __LINE__, __METHOD__, 10);
             $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
             Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('Annual Pay Periods: ' . $annual_pay_periods, __FILE__, __LINE__, __METHOD__, 10);
             $pd_obj = new PayrollDeduction($this->getCompanyDeductionObject()->getCountry(), NULL);
             $pd_obj->setCompany($this->getUserObject()->getCompany());
             $pd_obj->setUser($this->getUser());
             $pd_obj->setDate($pay_period_obj->getTransactionDate());
             $pd_obj->setAnnualPayPeriods($annual_pay_periods);
             if (is_object($this->getUserObject())) {
                 $currency_id = $this->getUserObject()->getCurrency();
                 $pd_obj->setUserCurrency($currency_id);
                 Debug::Text('User Currency ID: ' . $currency_id, __FILE__, __LINE__, __METHOD__, 10);
             }
             if ($this->getCompanyDeductionObject()->getCountry() == 'CA') {
                 //CA
                 $pd_obj->setFederalTotalClaimAmount($user_value1);
                 $pd_obj->setEnableCPPAndEIDeduction(TRUE);
                 //$pself = new PayStubEntryListFactory();
                 if ($this->getPayStubEntryAccountLinkObject()->getEmployeeCPP() != '') {
                     Debug::Text('Found Employee CPP account link!: ', __FILE__, __LINE__, __METHOD__, 10);
                     //$ytd_cpp_arr = $pself->getYTDAmountSumByUserIdAndEntryNameIDAndYear( $this->getUser(), $this->getPayStubEntryAccountLinkObject()->getEmployeeCPP(), $pay_period_obj->getTransactionDate() );
                     $ytd_cpp_arr = $pay_stub_obj->getSumByEntriesArrayAndTypeIDAndPayStubAccountID('previous', NULL, $this->getPayStubEntryAccountLinkObject()->getEmployeeCPP());
                     Debug::text('YTD CPP Contribution: ' . $ytd_cpp_arr['ytd_amount'], __FILE__, __LINE__, __METHOD__, 10);
                     $pd_obj->setYearToDateCPPContribution($ytd_cpp_arr['ytd_amount']);
                     unset($ytd_cpp_arr);
                 }
                 if ($this->getPayStubEntryAccountLinkObject()->getEmployeeEI() != '') {
                     Debug::Text('Found Employee EI account link!: ', __FILE__, __LINE__, __METHOD__, 10);
                     //$ytd_ei_arr = $pself->getYTDAmountSumByUserIdAndEntryNameIDAndYear( $this->getUser(), $this->getPayStubEntryAccountLinkObject()->getEmployeeEI(), $pay_period_obj->getTransactionDate() );
                     $ytd_ei_arr = $pay_stub_obj->getSumByEntriesArrayAndTypeIDAndPayStubAccountID('previous', NULL, $this->getPayStubEntryAccountLinkObject()->getEmployeeEI());
                     Debug::text('YTD EI Contribution: ' . $ytd_ei_arr['ytd_amount'], __FILE__, __LINE__, __METHOD__, 10);
                     $pd_obj->setYearToDateEIContribution($ytd_ei_arr['ytd_amount']);
                     unset($ytd_ei_arr);
                 }
             } elseif ($this->getCompanyDeductionObject()->getCountry() == 'US') {
                 //US
                 $pd_obj->setFederalFilingStatus($user_value1);
                 $pd_obj->setFederalAllowance($user_value2);
             } elseif ($this->getCompanyDeductionObject()->getCountry() == 'CR') {
                 //CR
                 $pd_obj->setFederalFilingStatus($user_value1);
                 //Single/Married
                 $pd_obj->setFederalAllowance($user_value2);
                 //Allownces/Children
             }
             $pd_obj->setGrossPayPeriodIncome($amount);
             $retval = $pd_obj->getFederalPayPeriodDeductions();
             if ($retval < 0) {
                 $retval = 0;
             }
             break;
         case 200:
             //Province Income Tax
             if ($this->getUserValue1() == '') {
                 $user_value1 = $cd_obj->getUserValue1();
             } else {
                 $user_value1 = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $user_value2 = $cd_obj->getUserValue2();
             } else {
                 $user_value2 = $this->getUserValue2();
             }
             if ($this->getUserValue3() == '') {
                 $user_value3 = $cd_obj->getUserValue3();
             } else {
                 $user_value3 = $this->getUserValue3();
             }
             Debug::Text('UserValue1: ' . $user_value1, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('UserValue2: ' . $user_value2, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('UserValue3: ' . $user_value3, __FILE__, __LINE__, __METHOD__, 10);
             $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
             Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('Annual Pay Periods: ' . $annual_pay_periods, __FILE__, __LINE__, __METHOD__, 10);
             $pd_obj = new PayrollDeduction($this->getCompanyDeductionObject()->getCountry(), $this->getCompanyDeductionObject()->getProvince());
             $pd_obj->setCompany($this->getUserObject()->getCompany());
             $pd_obj->setUser($this->getUser());
             $pd_obj->setDate($pay_period_obj->getTransactionDate());
             $pd_obj->setAnnualPayPeriods($annual_pay_periods);
             $pd_obj->setGrossPayPeriodIncome($amount);
             if ($this->getCompanyDeductionObject()->getCountry() == 'CA') {
                 Debug::Text('Canada Pay Period Deductions...', __FILE__, __LINE__, __METHOD__, 10);
                 $pd_obj->setProvincialTotalClaimAmount($user_value1);
                 $pd_obj->setEnableCPPAndEIDeduction(TRUE);
                 //$pself = new PayStubEntryListFactory();
                 if ($this->getPayStubEntryAccountLinkObject()->getEmployeeCPP() != '') {
                     Debug::Text('Found Employee CPP account link!: ', __FILE__, __LINE__, __METHOD__, 10);
                     $ytd_cpp_arr = $pay_stub_obj->getSumByEntriesArrayAndTypeIDAndPayStubAccountID('previous', NULL, $this->getPayStubEntryAccountLinkObject()->getEmployeeCPP());
                     Debug::text('YTD CPP Contribution: ' . $ytd_cpp_arr['ytd_amount'], __FILE__, __LINE__, __METHOD__, 10);
                     $pd_obj->setYearToDateCPPContribution($ytd_cpp_arr['ytd_amount']);
                     unset($ytd_cpp_arr);
                 }
                 if ($this->getPayStubEntryAccountLinkObject()->getEmployeeEI() != '') {
                     Debug::Text('Found Employee EI account link!: ', __FILE__, __LINE__, __METHOD__, 10);
                     //$ytd_ei_arr = $pself->getYTDAmountSumByUserIdAndEntryNameIDAndYear( $this->getUser(), $this->getPayStubEntryAccountLinkObject()->getEmployeeEI(), $pay_period_obj->getTransactionDate() );
                     $ytd_ei_arr = $pay_stub_obj->getSumByEntriesArrayAndTypeIDAndPayStubAccountID('previous', NULL, $this->getPayStubEntryAccountLinkObject()->getEmployeeEI());
                     Debug::text('YTD EI Contribution: ' . $ytd_ei_arr['ytd_amount'], __FILE__, __LINE__, __METHOD__, 10);
                     $pd_obj->setYearToDateEIContribution($ytd_ei_arr['ytd_amount']);
                     unset($ytd_ei_arr);
                 }
                 $retval = $pd_obj->getProvincialPayPeriodDeductions();
             } elseif ($this->getCompanyDeductionObject()->getCountry() == 'US') {
                 Debug::Text('US Pay Period Deductions...', __FILE__, __LINE__, __METHOD__, 10);
                 //Need to set Federal settings here.
                 $udlf = new UserDeductionListFactory();
                 $udlf->getByUserIdAndCountryID($user_id, $this->getCompanyDeductionObject()->getCountry());
                 if ($udlf->getRecordCount() > 0) {
                     Debug::Text('Found Federal User Deduction...', __FILE__, __LINE__, __METHOD__, 10);
                     $tmp_ud_obj = $udlf->getCurrent();
                     if ($tmp_ud_obj->getUserValue1() == '') {
                         $tmp_user_value1 = $tmp_ud_obj->getCompanyDeductionObject()->getUserValue1();
                     } else {
                         $tmp_user_value1 = $tmp_ud_obj->getUserValue1();
                     }
                     if ($tmp_ud_obj->getUserValue2() == '') {
                         $tmp_user_value2 = $tmp_ud_obj->getCompanyDeductionObject()->getUserValue2();
                     } else {
                         $tmp_user_value2 = $tmp_ud_obj->getUserValue2();
                     }
                     Debug::Text('TmpUserValue1: ' . $tmp_user_value1, __FILE__, __LINE__, __METHOD__, 10);
                     Debug::Text('TmpUserValue2: ' . $tmp_user_value2, __FILE__, __LINE__, __METHOD__, 10);
                     $pd_obj->setFederalFilingStatus($tmp_user_value1);
                     $pd_obj->setFederalAllowance($tmp_user_value2);
                     unset($tmp_ud_obj, $tmp_user_value1, $tmp_user_value1);
                 }
                 unset($udlf);
                 $pd_obj->setStateFilingStatus($user_value1);
                 $pd_obj->setStateAllowance($user_value2);
                 $pd_obj->setUserValue1($user_value1);
                 $pd_obj->setUserValue2($user_value2);
                 $pd_obj->setUserValue3($user_value3);
                 $retval = $pd_obj->getStatePayPeriodDeductions();
             }
             if ($retval < 0) {
                 $retval = 0;
             }
             break;
         case 300:
             //District Income Tax
             if ($this->getUserValue1() == '') {
                 $user_value1 = $cd_obj->getUserValue1();
             } else {
                 $user_value1 = $this->getUserValue1();
             }
             if ($this->getUserValue2() == '') {
                 $user_value2 = $cd_obj->getUserValue2();
             } else {
                 $user_value2 = $this->getUserValue2();
             }
             if ($this->getUserValue3() == '') {
                 $user_value3 = $cd_obj->getUserValue3();
             } else {
                 $user_value3 = $this->getUserValue3();
             }
             Debug::Text('UserValue1: ' . $user_value1, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('UserValue2: ' . $user_value2, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('UserValue3: ' . $user_value3, __FILE__, __LINE__, __METHOD__, 10);
             $amount = $cd_obj->getCalculationPayStubAmount($pay_stub_obj);
             Debug::Text('Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('Annual Pay Periods: ' . $annual_pay_periods, __FILE__, __LINE__, __METHOD__, 10);
             Debug::Text('District: ' . $this->getCompanyDeductionObject()->getDistrict(), __FILE__, __LINE__, __METHOD__, 10);
             $pd_obj = new PayrollDeduction($this->getCompanyDeductionObject()->getCountry(), $this->getCompanyDeductionObject()->getProvince(), $this->getCompanyDeductionObject()->getDistrict());
             $pd_obj->setCompany($this->getUserObject()->getCompany());
             $pd_obj->setUser($this->getUser());
             $pd_obj->setDate($pay_period_obj->getTransactionDate());
             $pd_obj->setAnnualPayPeriods($annual_pay_periods);
             $pd_obj->setDistrictFilingStatus($user_value1);
             $pd_obj->setDistrictAllowance($user_value2);
             $pd_obj->setUserValue1($user_value1);
             $pd_obj->setUserValue2($user_value2);
             $pd_obj->setUserValue3($user_value3);
             $pd_obj->setGrossPayPeriodIncome($amount);
             $retval = $pd_obj->getDistrictPayPeriodDeductions();
             if ($retval < 0) {
                 $retval = 0;
             }
             break;
     }
     Debug::Text('Deduction Amount: ' . $retval, __FILE__, __LINE__, __METHOD__, 10);
     //Allow negative values, as some advanced tax bracket setups require this.
     if ($retval < 0) {
         //Debug::Text('Deduction was negative, setting to 0...', __FILE__, __LINE__, __METHOD__,10);
         Debug::Text('Deduction was negative...', __FILE__, __LINE__, __METHOD__, 10);
         //$retval = 0;
     }
     return $retval;
 }
 function calculate($epoch = NULL)
 {
     if ($this->getUserObject() == FALSE or $this->getUserObject()->getStatus() !== 10) {
         return FALSE;
     }
     $generic_queue_status_label = $this->getUserObject()->getFullName(TRUE) . ' - ' . TTi18n::gettext('Pay Stub');
     if ($epoch == NULL or $epoch == '') {
         $epoch = TTDate::getTime();
     }
     if ($this->getPayPeriodObject() == FALSE) {
         return FALSE;
     }
     Debug::text('bbUser Id: ' . $this->getUser() . ' Pay Period End Date: ' . TTDate::getDate('DATE+TIME', $this->getPayPeriodObject()->getEndDate()), __FILE__, __LINE__, __METHOD__, 10);
     $pay_stub = new PayStubFactory();
     $pay_stub->StartTransaction();
     $old_pay_stub_id = NULL;
     if ($this->getEnableCorrection() == TRUE) {
         Debug::text('Correction Enabled!', __FILE__, __LINE__, __METHOD__, 10);
         $pay_stub->setTemp(TRUE);
         //Check for current pay stub ID so we can compare against it.
         $pslf = new PayStubListFactory();
         $pslf->getByUserIdAndPayPeriodId($this->getUser(), $this->getPayPeriod());
         if ($pslf->getRecordCount() > 0) {
             $old_pay_stub_id = $pslf->getCurrent()->getId();
             Debug::text('Comparing Against Pay Stub ID: ' . $old_pay_stub_id, __FILE__, __LINE__, __METHOD__, 10);
         }
     }
     $pay_stub->setUser($this->getUser());
     $pay_stub->setPayPeriod($this->getPayPeriod());
     $pay_stub->setCurrency($this->getUserObject()->getCurrency());
     $pay_stub->setStatus('NEW');
     //Use User Termination Date instead of ROE.
     if ($this->getUserObject()->getTerminationDate() != '' and $this->getUserObject()->getTerminationDate() >= $this->getPayPeriodObject()->getStartDate() and $this->getUserObject()->getTerminationDate() <= $this->getPayPeriodObject()->getEndDate()) {
         Debug::text('User has been terminated in this pay period!', __FILE__, __LINE__, __METHOD__, 10);
         $is_terminated = TRUE;
     } else {
         $is_terminated = FALSE;
     }
     if ($is_terminated == TRUE) {
         Debug::text('User is Terminated, assuming final pay, setting End Date to terminated date: ' . TTDate::getDate('DATE+TIME', $this->getUserObject()->getTerminationDate()), __FILE__, __LINE__, __METHOD__, 10);
         $pay_stub->setStartDate($pay_stub->getPayPeriodObject()->getStartDate());
         $pay_stub->setEndDate($this->getUserObject()->getTerminationDate());
         //Use the PS generation date instead of terminated date...
         //Unlikely they would pay someone before the pay stub is generated.
         //Perhaps still use the pay period transaction date for this too?
         //Anything we set won't be correct for everyone. Maybe a later date is better though?
         //Perhaps add to the user factory under Termination Date a: "Final Transaction Date" for this purpose?
         //Use the end of the current date for the transaction date, as if the employee is terminated
         //on the same day they are generating the pay stub, the transaction date could be before the end date
         //as the end date is at 11:59PM
         $pay_stub->setTransactionDate(TTDate::getEndDayEpoch(TTDate::getTime()));
     } else {
         Debug::text('User Termination Date is NOT set, assuming normal pay.', __FILE__, __LINE__, __METHOD__, 10);
         $pay_stub->setDefaultDates();
     }
     //This must go after setting advance
     if ($this->getEnableCorrection() == FALSE and $pay_stub->IsUniquePayStub() == FALSE) {
         Debug::text('Pay Stub already exists', __FILE__, __LINE__, __METHOD__, 10);
         $this->CommitTransaction();
         UserGenericStatusFactory::queueGenericStatus($generic_queue_status_label, 20, TTi18n::gettext('Pay Stub for this employee already exists, skipping...'), NULL);
         return FALSE;
     }
     if ($pay_stub->isValid() == TRUE) {
         $pay_stub->Save(FALSE);
         $pay_stub->setStatus('Open');
     } else {
         Debug::text('Pay Stub isValid failed!', __FILE__, __LINE__, __METHOD__, 10);
         UserGenericStatusFactory::queueGenericStatus($generic_queue_status_label, 10, $pay_stub->Validator->getTextErrors(), NULL);
         $this->FailTransaction();
         $this->CommitTransaction();
         return FALSE;
     }
     $pay_stub->loadPreviousPayStub();
     $user_date_total_arr = $this->getWageObject()->getUserDateTotalArray();
     if (isset($user_date_total_arr['entries']) and is_array($user_date_total_arr['entries'])) {
         foreach ($user_date_total_arr['entries'] as $udt_arr) {
             //Allow negative amounts so flat rate premium policies can reduce an employees wage if need be.
             if ($udt_arr['amount'] != 0) {
                 Debug::text('Adding Pay Stub Entry: ' . $udt_arr['pay_stub_entry'] . ' Amount: ' . $udt_arr['amount'], __FILE__, __LINE__, __METHOD__, 10);
                 $pay_stub->addEntry($udt_arr['pay_stub_entry'], $udt_arr['amount'], TTDate::getHours($udt_arr['total_time']), $udt_arr['rate']);
             } else {
                 Debug::text('NOT Adding ($0 amount) Pay Stub Entry: ' . $udt_arr['pay_stub_entry'] . ' Amount: ' . $udt_arr['amount'], __FILE__, __LINE__, __METHOD__, 10);
             }
         }
     } else {
         //No Earnings, CHECK FOR PS AMENDMENTS next for earnings.
         Debug::text('NO TimeSheet EARNINGS ON PAY STUB... Checking for PS amendments', __FILE__, __LINE__, __METHOD__, 10);
     }
     //Get all PS amendments and Tax / Deductions so we can determine the proper order to calculate them in.
     $psalf = new PayStubAmendmentListFactory();
     $psalf->getByUserIdAndAuthorizedAndStartDateAndEndDate($this->getUser(), TRUE, $this->getPayPeriodObject()->getStartDate(), $this->getPayPeriodObject()->getEndDate());
     $udlf = new UserDeductionListFactory();
     $udlf->getByCompanyIdAndUserId($this->getUserObject()->getCompany(), $this->getUserObject()->getId());
     $deduction_order_arr = $this->getOrderedDeductionAndPSAmendment($udlf, $psalf);
     if (is_array($deduction_order_arr) and count($deduction_order_arr) > 0) {
         foreach ($deduction_order_arr as $calculation_order => $data_arr) {
             Debug::text('Found PS Amendment/Deduction: Type: ' . $data_arr['type'] . ' Name: ' . $data_arr['name'] . ' Order: ' . $calculation_order, __FILE__, __LINE__, __METHOD__, 10);
             if (isset($data_arr['obj']) and is_object($data_arr['obj'])) {
                 if ($data_arr['type'] == 'UserDeductionListFactory') {
                     $ud_obj = $data_arr['obj'];
                     //Determine if this deduction is valid based on start/end dates.
                     //Determine if this deduction is valid based on min/max length of service.
                     if ($ud_obj->getCompanyDeductionObject()->isActiveDate($pay_stub->getPayPeriodObject()->getEndDate()) == TRUE and $ud_obj->getCompanyDeductionObject()->isActiveLengthOfService($this->getUserObject(), $pay_stub->getPayPeriodObject()->getEndDate()) == TRUE) {
                         $amount = $ud_obj->getDeductionAmount($this->getUserObject()->getId(), $pay_stub, $this->getPayPeriodObject());
                         Debug::text('User Deduction: ' . $ud_obj->getCompanyDeductionObject()->getName() . ' Amount: ' . $amount . ' Calculation Order: ' . $ud_obj->getCompanyDeductionObject()->getCalculationOrder(), __FILE__, __LINE__, __METHOD__, 10);
                         //Allow negative amounts, so they can reduce previously calculated deductions or something.
                         if (isset($amount) and $amount != 0) {
                             $pay_stub->addEntry($ud_obj->getCompanyDeductionObject()->getPayStubEntryAccount(), $amount);
                         } else {
                             Debug::text('Amount is 0, skipping...', __FILE__, __LINE__, __METHOD__, 10);
                         }
                     }
                     unset($amount, $ud_obj);
                 } elseif ($data_arr['type'] == 'PayStubAmendmentListFactory') {
                     $psa_obj = $data_arr['obj'];
                     Debug::text('Found Pay Stub Amendment: ID: ' . $psa_obj->getID() . ' Entry Name ID: ' . $psa_obj->getPayStubEntryNameId() . ' Type: ' . $psa_obj->getType(), __FILE__, __LINE__, __METHOD__, 10);
                     $amount = $psa_obj->getCalculatedAmount($pay_stub);
                     if (isset($amount) and $amount != 0) {
                         Debug::text('Pay Stub Amendment Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                         $pay_stub->addEntry($psa_obj->getPayStubEntryNameId(), $amount, $psa_obj->getUnits(), $psa_obj->getRate(), $psa_obj->getDescription(), $psa_obj->getID(), NULL, NULL, $psa_obj->getYTDAdjustment());
                         //Keep in mind this causes pay stubs to be re-generated every time, as this modifies the updated time
                         //to slightly more then the pay stub creation time.
                         $psa_obj->setStatus('IN USE');
                         $psa_obj->Save();
                     } else {
                         Debug::text('bPay Stub Amendment Amount is not set...', __FILE__, __LINE__, __METHOD__, 10);
                     }
                     unset($amount, $psa_obj);
                 }
             }
         }
     }
     unset($deduction_order_arr, $calculation_order, $data_arr);
     $pay_stub_id = $pay_stub->getId();
     $pay_stub->setEnableProcessEntries(TRUE);
     $pay_stub->processEntries();
     if ($pay_stub->isValid() == TRUE) {
         Debug::text('Pay Stub is valid, final save.', __FILE__, __LINE__, __METHOD__, 10);
         $pay_stub->Save();
         if ($this->getEnableCorrection() == TRUE) {
             if (isset($old_pay_stub_id)) {
                 Debug::text('bCorrection Enabled - Doing Comparison here', __FILE__, __LINE__, __METHOD__, 10);
                 PayStubFactory::CalcDifferences($pay_stub_id, $old_pay_stub_id);
             }
             //Delete newly created temp paystub.
             //This used to be in the above IF block that depended on $old_pay_stub_id
             //being set, however in cases where the old pay stub didn't exist
             //TimeTrex wouldn't delete these temporary pay stubs.
             //Moving this code outside that IF statement so it only depends on EnableCorrection()
             //to be TRUE should fix that issue.
             $pslf = new PayStubListFactory();
             $pslf->getById($pay_stub_id);
             if ($pslf->getRecordCount() > 0) {
                 $tmp_ps_obj = $pslf->getCurrent();
                 $tmp_ps_obj->setDeleted(TRUE);
                 $tmp_ps_obj->Save();
                 unset($tmp_ps_obj);
             }
         }
         $pay_stub->CommitTransaction();
         UserGenericStatusFactory::queueGenericStatus($generic_queue_status_label, 30, NULL, NULL);
         return TRUE;
     }
     Debug::text('Pay Stub is NOT valid returning FALSE', __FILE__, __LINE__, __METHOD__, 10);
     UserGenericStatusFactory::queueGenericStatus($generic_queue_status_label, 10, $pay_stub->Validator->getTextErrors(), NULL);
     $pay_stub->FailTransaction();
     //Reduce transaction count by one.
     //$pay_stub->FailTransaction(); //Reduce transaction count by one.
     $pay_stub->CommitTransaction();
     return FALSE;
 }