diff --git a/src/AppBundle/Command/FixTimeLogSavingCommand.php b/src/AppBundle/Command/FixTimeLogSavingCommand.php new file mode 100644 index 000000000..995b58d2b --- /dev/null +++ b/src/AppBundle/Command/FixTimeLogSavingCommand.php @@ -0,0 +1,75 @@ +setName('app:user:fix_time_log_saving') + ->setDescription('Fix time logs saving data') + ->setHelp('This command allows you to fix time logs saving data'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $countSavingLogs = 0; + $due_duration_by_cycle = $this->getContainer()->getParameter('due_duration_by_cycle'); + $use_time_log_saving = $this->getContainer()->getParameter('use_time_log_saving'); + $em = $this->getContainer()->get('doctrine')->getManager(); + $members = $em->getRepository('AppBundle:Membership')->findAll(); + + if ($use_time_log_saving) { + $output->writeln('Parameter use_time_log_saving is true'); + $output->writeln('Parameter due_duration_by_cycle = '. $due_duration_by_cycle); + foreach ($members as $member) { + if (!$member->isWithdrawn() && $member->getFirstShiftDate()) { + $member_counter_now = $member->getShiftTimeCount(); + $extra_counter_time = $member_counter_now - $due_duration_by_cycle; + + // the extra time will go in the member's saving account + // same logic as TimeLogEventListener > createShiftValidatedTimeLog + if ($extra_counter_time > 0) { + $output->writeln('====='); + $output->writeln('Member ' . $member); + $output->writeln('counter_now before ' . $member_counter_now); + $output->writeln('extra_counter_time before = ' . $extra_counter_time); + + // get member last timelog + $last_timelog = $member->getTimeLogs()->first(); + $output->writeln('dernier timelog (id) ' . $last_timelog->getId()); + $output->writeln('créneau du dernier timelog ' . $last_timelog->getShift()); + + // first decrement the shiftTimeCount + $log = $this->getContainer()->get('time_log_service')->initRegulateOptionalShiftsTimeLog($member, -1 * $extra_counter_time); + $em->persist($log); + // then increment the savingTimeCount + $log = $this->getContainer()->get('time_log_service')->initSavingTimeLog($member, $extra_counter_time, $last_timelog->getShift()); + $em->persist($log); + $em->flush(); + + $member_counter_now = $member->getShiftTimeCount(); + $extra_counter_time = $member_counter_now - $due_duration_by_cycle; + + $output->writeln('counter_now after ' . $member_counter_now); + $output->writeln('extra_counter_time after = ' . $extra_counter_time); + } + } + } + + // $em->flush(); + // $output->writeln($countShiftLogs . ' logs de créneaux réalisés créés'); + } else { + $output->writeln('Parameter use_time_log_saving must be true'); + } + } +} diff --git a/src/AppBundle/EventListener/TimeLogEventListener.php b/src/AppBundle/EventListener/TimeLogEventListener.php index 04adb207c..e9cdfcd55 100644 --- a/src/AppBundle/EventListener/TimeLogEventListener.php +++ b/src/AppBundle/EventListener/TimeLogEventListener.php @@ -245,8 +245,8 @@ private function createShiftValidatedTimeLog(Shift $shift, \DateTime $date = nul $this->em->flush(); if ($this->use_time_log_saving) { - $counter_now = $member->getShiftTimeCount(); - $extra_counter_time = $counter_now - $this->due_duration_by_cycle; // + max_time_at_end_of_shift ?? + $member_counter_now = $member->getShiftTimeCount(); + $extra_counter_time = $member_counter_now - $this->due_duration_by_cycle; // + max_time_at_end_of_shift ?? // the extra time will go in the member's saving account if ($extra_counter_time > 0) { @@ -300,9 +300,9 @@ private function createCycleBeginningLog(Membership $member, \DateTime $date) $log = $this->container->get('time_log_service')->initCycleBeginningTimeLog($member); $this->em->persist($log); - $counter_today = $member->getShiftTimeCount($date); + $member_counter_date = $member->getShiftTimeCount($date); $allowed_cumul = $this->max_time_at_end_of_shift; - $extra_counter_time = $counter_today - ($this->due_duration_by_cycle + $allowed_cumul); // surbook + $extra_counter_time = $member_counter_date - ($this->due_duration_by_cycle + $allowed_cumul); // surbook if ($extra_counter_time > 0) { $log = $this->container->get('time_log_service')->initRegulateOptionalShiftsTimeLog($member, -1 * $extra_counter_time);