Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compte épargne : répare une erreur en début de cycle #990

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions src/AppBundle/EventListener/TimeLogEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ public function onMemberCycleEnd(MemberCycleEndEvent $event)
*/
private function createShiftValidatedTimeLog(Shift $shift, \DateTime $date = null, $description = null)
{
$member = $shift->getShifter()->getMembership();

$log = $this->container->get('time_log_service')->initShiftValidatedTimeLog($shift, $date, $description);
$this->em->persist($log);
$this->em->flush();
Expand All @@ -249,16 +247,19 @@ private function createShiftValidatedTimeLog(Shift $shift, \DateTime $date = nul
// - use_card_reader_to_validate_shifts (for the other coops, this will happen in onMemberCycleEnd)
// - there is extra time
if ($this->use_card_reader_to_validate_shifts && $this->use_time_log_saving) {
$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);
$this->em->refresh($shift); // added to prevent from returning cached (old) data
$member = $shift->getShifter()->getMembership();

$date_plus_one_minute = clone($date)->modify("+1 minute");
$member_counter_time = $member->getShiftTimeCount($date_plus_one_minute); // $date_plus_one_minute? to be sure we take the above log into account
$member_counter_extra_time = $member_counter_time - ($this->due_duration_by_cycle + $this->max_time_at_end_of_shift);

if ($extra_counter_time > 0) {
if ($member_counter_extra_time > 0) {
// first decrement the shiftTimeCount
$log = $this->container->get('time_log_service')->initRegulateOptionalShiftsTimeLog($member, -1 * $extra_counter_time);
$log = $this->container->get('time_log_service')->initRegulateOptionalShiftsTimeLog($member, -1 * $member_counter_extra_time);
$this->em->persist($log);
// then increment the savingTimeCount
$log = $this->container->get('time_log_service')->initSavingTimeLog($member, $extra_counter_time, $shift);
$log = $this->container->get('time_log_service')->initSavingTimeLog($member, $member_counter_extra_time, $shift);
$this->em->persist($log);
$this->em->flush();
}
Expand Down Expand Up @@ -301,28 +302,33 @@ private function deleteShiftLogs(Shift $shift, Membership $member)
*/
private function createCycleBeginningLog(Membership $member, \DateTime $date)
{
$log = $this->container->get('time_log_service')->initCycleBeginningTimeLog($member);
// decrease member shiftTime by due_duration_by_cycle
$log = $this->container->get('time_log_service')->initCycleBeginningTimeLog($member, $date);
$this->em->persist($log);
$this->em->flush();

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

$member_counter_date = $member->getShiftTimeCount($date);
$extra_counter_time = $member_counter_date - ($this->due_duration_by_cycle + $this->max_time_at_end_of_shift);
$date_plus_one_minute = clone($date)->modify("+1 minute");
$member_counter_time = $member->getShiftTimeCount($date_plus_one_minute); // $date_plus_one_minute? to be sure we take the above log into account
$member_counter_extra_time = $member_counter_time - $this->max_time_at_end_of_shift; // not $this->due_duration_by_cycle? already substracted in the above log

if ($extra_counter_time > 0) {
$log = $this->container->get('time_log_service')->initRegulateOptionalShiftsTimeLog($member, -1 * $extra_counter_time);
// member did extra work
if ($member_counter_time > 0 && $member_counter_extra_time > 0) {
// remove the extra_time from the shiftTime
$log = $this->container->get('time_log_service')->initRegulateOptionalShiftsTimeLog($member, -1 * $member_counter_extra_time);
$this->em->persist($log);
if ($this->use_time_log_saving) {
// increment the savingTimeCount
$log = $this->container->get('time_log_service')->initSavingTimeLog($member, 1 * $extra_counter_time);
// add the extra_time to the savingTime
$log = $this->container->get('time_log_service')->initSavingTimeLog($member, 1 * $member_counter_extra_time);
$this->em->persist($log);
}
} elseif ($extra_counter_time < 0) {
// member has a negative shiftTimeCount...
} elseif ($member_counter_time < 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) {
$member_saving_time = $member->getSavingTimeCount($date_plus_one_minute); // $date_plus_one_minute? to be sure we take the above log into account
if ($member_saving_time > 0) {
$date_minus_one_day = clone($date)->modify("-1 days");
// count missed shifts in the previous cycle
$previous_cycle_missed_shifts_count = $this->container->get('membership_service')->getCycleShiftMissedCount($member, $date_minus_one_day);
Expand All @@ -332,18 +338,18 @@ 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_date > 0) ? $this->due_duration_by_cycle - $member_counter_date : $this->due_duration_by_cycle;
$withdraw_from_saving = min($member_saving_now, $missing_due_time);
// first decrement the savingTimeCount
$missing_due_time = -1 * $member_counter_time;
$withdraw_from_saving = min($member_saving_time, $missing_due_time);
// 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 {
// not allowed to use member's saving
// give explanation
$description = "(compteur épargne (" . $member_saving_now . " minutes) non utilisé car ";
$description = "(compteur épargne (" . $member_saving_time . " minutes) non utilisé car ";
if ($previous_cycle_missed_shifts_count) {
$description = $description . $previous_cycle_missed_shifts_count . " créneau" . (($previous_cycle_missed_shifts_count > 1) ? 'x' : '') . " raté" . (($previous_cycle_missed_shifts_count > 1) ? 's' : '');
}
Expand Down
Loading