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 2 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
28 changes: 16 additions & 12 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,7 +247,9 @@ 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
$this->em->refresh($shift); // added to prevent from returning cached (old) data
$member = $shift->getShifter()->getMembership();

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

Expand Down 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_date = $member->getShiftTimeCount($date);
$extra_counter_time = $member_counter_date - ($this->due_duration_by_cycle + $this->max_time_at_end_of_shift);
$member_counter_now = $member->getShiftTimeCount();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

la ligne la plus importante de cette PR :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, y'a pas un effet de bord sur d'autres coop ici ?

Je m'explique : quand tu désactives l'option de valider les créneaux via la badgeuse, ils sont automatiquement validés à la date future du créneau. Si tu prends un créneau sur le cycle N+1, il ne faut pas qu'à la fin du cycle N, il soit pris en compte dans le calcul.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je vois... Peut-être remettre le paramètre $date du coup, mais on rajoutant 1 minute par exemple pour être sûr d'avoir le time count à jour ?

Copy link
Member Author

@raphodn raphodn Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petitalb j'ai rebasé, et rajouté ce commit pour modifier légèrement la $date : 5b06140

image

$extra_counter_time = $member_counter_now - $this->max_time_at_end_of_shift;
petitalb marked this conversation as resolved.
Show resolved Hide resolved

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_date > 0) ? $this->due_duration_by_cycle - $member_counter_date : $this->due_duration_by_cycle;
$missing_due_time = -1 * $member_counter_now;
petitalb marked this conversation as resolved.
Show resolved Hide resolved
$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
Loading