Skip to content

Commit

Permalink
Fix calculation with extra_counter_time
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 21, 2023
1 parent 1584c46 commit c019338
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/AppBundle/EventListener/TimeLogEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,26 +301,30 @@ private function deleteShiftLogs(Shift $shift, Membership $member)
*/
private function createCycleBeginningLog(Membership $member, \DateTime $date)
{
// decrease member shiftTime by due_duration_by_cycle
$log = $this->container->get('time_log_service')->initCycleBeginningTimeLog($member);
$this->em->persist($log);
$this->em->flush();

$this->em->refresh($member); // added to prevent from returning cached (old) data

$member_counter_now = $member->getShiftTimeCount();
$extra_counter_time = $member_counter_now - ($this->due_duration_by_cycle + $this->max_time_at_end_of_shift);
$extra_counter_time = $member_counter_now - $this->max_time_at_end_of_shift;

if ($extra_counter_time > 0) {
// member did extra work
if ($member_counter_now > 0 && $extra_counter_time > 0) {
// remove the extra_time from the shiftTime
$log = $this->container->get('time_log_service')->initRegulateOptionalShiftsTimeLog($member, -1 * $extra_counter_time);
$this->em->persist($log);
if ($this->use_time_log_saving) {
// increment the savingTimeCount
// add the extra_time to the savingTime
$log = $this->container->get('time_log_service')->initSavingTimeLog($member, 1 * $extra_counter_time);
$this->em->persist($log);
}
} elseif ($extra_counter_time < 0) {
// member has a negative shiftTimeCount...
} elseif ($member_counter_now < 0) {
// we can *maybe* use the member's savingTime to bring his shiftTime back to 0
if ($this->use_time_log_saving) {
// retrieve member's savings
$member_saving_now = $member->getSavingTimeCount();
if ($member_saving_now > 0) {
$date_minus_one_day = clone($date)->modify("-1 days");
Expand All @@ -332,12 +336,12 @@ private function createCycleBeginningLog(Membership $member, \DateTime $date)
// - the member has no missed shifts in the previous cycle
// - the member has no freed shifts within the min_time_in_advance
if ($previous_cycle_missed_shifts_count == 0 && $previous_cycle_freed_shifts_less_than_min_time_in_advance_count == 0) {
$missing_due_time = ($member_counter_now > 0) ? $this->due_duration_by_cycle - $member_counter_now : $this->due_duration_by_cycle;
$missing_due_time = -1 * $member_counter_now;
$withdraw_from_saving = min($member_saving_now, $missing_due_time);
// first decrement the savingTimeCount
// first decrement the savingTime
$log = $this->container->get('time_log_service')->initSavingTimeLog($member, -1 * $withdraw_from_saving);
$this->em->persist($log);
// then increment the shiftTimeCount
// then increment the shiftTime
$log = $this->container->get('time_log_service')->initCycleEndSavingTimeLog($member, 1 * $withdraw_from_saving);
$this->em->persist($log);
} else {
Expand Down

0 comments on commit c019338

Please sign in to comment.