예제 #1
0
 function reCalculate()
 {
     //Re-generate final pay stub
     //get current pay period based off their last day of work
     $pplf = new PayPeriodListFactory();
     $pay_period_id = $pplf->getByUserIdAndEndDate($this->getUser(), $this->getLastDate())->getCurrent()->getId();
     Debug::Text('Pay Period ID: ' . $pay_period_id, __FILE__, __LINE__, __METHOD__, 10);
     if (is_numeric($pay_period_id) == FALSE) {
         UserGenericStatusFactory::queueGenericStatus($this->getUserObject()->getFullName(TRUE) . ' - ' . TTi18n::gettext('Pay Stub'), 10, TTi18n::gettext('Pay Period is invalid!'), NULL);
         return FALSE;
     }
     if ($this->getEnableGeneratePayStub() == TRUE) {
         //Find out if a pay stub is already generated for the pay period we are currently in.
         //If it is, delete it so we can start from fresh
         $pslf = new PayStubListFactory();
         $pslf->getByUserIdAndPayPeriodId($this->getUser(), $pay_period_id);
         foreach ($pslf as $pay_stub) {
             Debug::Text('Found Pay Stub ID: ' . $pay_stub->getId(), __FILE__, __LINE__, __METHOD__, 10);
             //Do not delete PAID pay stubs!
             if ($pay_stub->getStatus() == 10) {
                 Debug::Text('Last Pay Stub Exists: ' . $pay_stub->getId(), __FILE__, __LINE__, __METHOD__, 10);
                 $pay_stub->setDeleted(TRUE);
                 $pay_stub->Save();
             }
         }
         //FIXME: Make sure user isn't already in-active! Otherwise pay stub won't generate.
         //Check if pay stub is already generated as well, if it is, and marked paid, then
         //we can't re-generate it, we need to skip this step.
         Debug::Text('Calculating Pay Stub...', __FILE__, __LINE__, __METHOD__, 10);
         $cps = new CalculatePayStub();
         $cps->setUser($this->getUser());
         $cps->setPayPeriod($pay_period_id);
         $cps->calculate();
         Debug::Text('Done Calculating Pay Stub', __FILE__, __LINE__, __METHOD__, 10);
     } else {
         UserGenericStatusFactory::queueGenericStatus($this->getUserObject()->getFullName(TRUE), 20, TTi18n::gettext('Not generating final pay stub!'), NULL);
     }
     //FIXME: Alert the user if they don't have enough information in TimeTrex to get accurate values.
     //Get insurable hours, earnings, and vacation pay now that the final pay stub is generated
     $ugdlf = new UserGenericDataListFactory();
     $ugdlf->getByCompanyIdAndScriptAndDefault($this->getUserObject()->getCompany(), $this->getTable());
     if ($ugdlf->getRecordCount() > 0) {
         Debug::Text('Found Company Form Setup!', __FILE__, __LINE__, __METHOD__, 10);
         $ugd_obj = $ugdlf->getCurrent();
         $setup_data = $ugd_obj->getData();
     }
     unset($ugd_obj);
     if (isset($setup_data)) {
         //var_dump($setup_data);
         if (isset($setup_data['insurable_earnings_psea_ids'])) {
             $insurable_earnings_psea_ids = $setup_data['insurable_earnings_psea_ids'];
         } else {
             //Fall back to Total Gross.
             $insurable_earnings_psea_ids = $this->getPayStubEntryAccountLinkObject()->getTotalGross();
         }
     }
     //Find out the date of how far back we have to go to get insurable values.
     //Insurable Hours
     $insurable_hours_start_date = $this->getInsurablePayPeriodStartDate($this->getInsurableHoursReportPayPeriods());
     //Insurable hours dont include stats!Just get total signed in hours.
     //If someone has just a negative bank time balance, those are NOT insurable.
     $udtlf = new UserDateTotalListFactory();
     $worked_total_time = $udtlf->getWorkedTimeSumByUserIDAndStartDateAndEndDate($this->getUser(), $insurable_hours_start_date, $this->getLastDate());
     Debug::text('Worked Total Time: ' . $worked_total_time, __FILE__, __LINE__, __METHOD__, 10);
     $total_hours = Misc::MoneyFormat(TTDate::getHours($worked_total_time), FALSE);
     Debug::Text('Total Insurable Hours: ' . $total_hours, __FILE__, __LINE__, __METHOD__, 10);
     $insurable_earnings_start_date = $this->getInsurablePayPeriodStartDate($this->getInsurableEarningsReportPayPeriods());
     $pself = new PayStubEntryListFactory();
     $total_earnings = $this->getTotalInsurableEarnings();
     //$total_earnings = $pself->getAmountSumByUserIdAndEntryNameIdAndStartDateAndEndDate( $this->getUser(), $insurable_earnings_psea_ids, $insurable_earnings_start_date, $this->getLastDate(), 0 );
     //$total_earnings = $total_earnings['amount'];
     //Note, this includes the current pay stub we just generated
     Debug::Text('Total Insurable Earnings: ' . $total_earnings, __FILE__, __LINE__, __METHOD__, 10);
     UserGenericStatusFactory::queueGenericStatus($this->getUserObject()->getFullName(TRUE) . ' - ' . TTi18n::gettext('Record of Employment'), 30, TTi18n::gettext('Insurable Hours:') . ' ' . $total_hours . ' ' . TTi18n::gettext('Insurable Earnings:') . ' ' . $total_earnings, NULL);
     //ReSave these
     if ($this->getId() != '') {
         $rlf = new ROEListFactory();
         $rlf->getById($this->getId());
         if ($rlf->getRecordCount() > 0) {
             $roe_obj = $rlf->getCurrent();
             $roe_obj->setInsurableHours($total_hours);
             $roe_obj->setInsurableEarnings($total_earnings);
             if ($roe_obj->isValid()) {
                 $roe_obj->Save();
             }
         }
     }
     return TRUE;
 }
예제 #2
0
     $pptsvf->setStatus(30);
     //Pending Auth
     $pptsvf->setAuthorized(FALSE);
     if ($pptsvf->isValid()) {
         $pptsvf->Save();
     }
     Redirect::Page(URLBuilder::getURL(NULL, '../timesheet/ViewUserTimeSheet.php'));
     break;
 case 'recalculate pay stub':
     //Debug::setVerbosity(11);
     //Find out if a pay stub is already generated for the pay period we are currently in.
     //If it is, delete it so we can start from fresh
     $pplf = new PayPeriodListFactory();
     $pplf->getByIdAndCompanyId($pay_period_id, $current_company->getId());
     $pslf = new PayStubListFactory();
     $pslf->getByUserIdAndPayPeriodId($filter_data['user_id'], $pay_period_id);
     foreach ($pslf as $pay_stub) {
         Debug::Text('Found Pay Stub ID: ' . $pay_stub->getId(), __FILE__, __LINE__, __METHOD__, 10);
         //Do not delete PAID pay stubs!
         if ($pay_stub->getStatus() <= 25 and $pay_stub->getTainted() === FALSE and $pay_stub->getEndDate() == $pay_period_obj->getEndDate()) {
             Debug::Text('Last Pay Stub Exists: ' . $pay_stub->getId(), __FILE__, __LINE__, __METHOD__, 10);
             $pay_stub->setDeleted(TRUE);
             $pay_stub->Save();
         } else {
             Debug::Text('aNot Deleting Pay Stub: ' . $pay_stub->getId(), __FILE__, __LINE__, __METHOD__, 10);
         }
     }
     TTLog::addEntry($filter_data['user_id'], 'Notice', TTi18n::gettext('Calculating Employee Pay Stub for Pay Period:') . ' ' . $pay_period_id, $current_user->getID(), 'pay_stub');
     //FIXME: Make sure user isn't already in-active! Otherwise pay stub won't generate.
     Debug::Text('Calculating Pay Stub...', __FILE__, __LINE__, __METHOD__, 10);
     $profiler->startTimer("Calculating Pay Stub");
 function getPayStub($pay_period_id = FALSE)
 {
     if ($pay_period_id == FALSE) {
         $pay_period_id = $this->pay_period_objs[0]->getId();
     }
     $pslf = new PayStubListFactory();
     $pslf->getByUserIdAndPayPeriodId($this->user_id, $pay_period_id);
     if ($pslf->getRecordCount() > 0) {
         return $pslf->getCurrent()->getId();
     }
     return FALSE;
 }
 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;
 }