|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AppBundle\Command; |
| 4 | + |
| 5 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Input\InputOption; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | + |
| 10 | +class FixShiftMissingPositionCommand extends ContainerAwareCommand |
| 11 | +{ |
| 12 | + protected function configure() |
| 13 | + { |
| 14 | + $this |
| 15 | + ->setName('app:shift:fix_missing_position') |
| 16 | + ->setDescription('Fix shifts without position (find and attach corresponding position)') |
| 17 | + ->setHelp('This command allows you to fix missing shift position data (most likely deleted by the migration Version20211223205749') |
| 18 | + ->addOption('dry_run', null, InputOption::VALUE_NONE, 'Dry run'); |
| 19 | + } |
| 20 | + |
| 21 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 22 | + { |
| 23 | + $em = $this->getContainer()->get('doctrine')->getManager(); |
| 24 | + $cycle_type = $this->getContainer()->getParameter('cycle_type'); |
| 25 | + $dry_run = $input->getOption('dry_run'); |
| 26 | + |
| 27 | + if ($dry_run) { |
| 28 | + $output->writeln("<comment>Dry run: won't impact the database</comment>"); |
| 29 | + } |
| 30 | + |
| 31 | + $shifts_without_position = $em->getRepository('AppBundle:Shift')->findBy(array('position' => null), array('start' => 'ASC')); |
| 32 | + $output->writeln(count($shifts_without_position) . ' créneau' . ((count($shifts_without_position)>1) ? 'x':'') . ' sans poste type trouvé' . ((count($shifts_without_position)>1) ? 's':'') . '...'); |
| 33 | + |
| 34 | + if ($cycle_type == 'abcd') { |
| 35 | + // TODO : add filter on weekCycle |
| 36 | + // what if the cycle changed ? |
| 37 | + $output->writeln('<error>Currently only works for coops without cycle_type.</error>'); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if ($shifts_without_position) { |
| 42 | + $output->writeln('Premier créneau trouvé : ' . $shifts_without_position[0]->getDisplayDateFullWithTime()); |
| 43 | + $output->writeln('Dernier créneau trouvé : ' . end($shifts_without_position)->getDisplayDateFullWithTime()); |
| 44 | + |
| 45 | + $shifts_without_position_fixed = 0; |
| 46 | + // faster to loop on periodPositions |
| 47 | + $period_positions = $em->getRepository('AppBundle:PeriodPosition') |
| 48 | + ->createQueryBuilder('pp') |
| 49 | + ->leftJoin('pp.period', 'p')->addSelect('p') |
| 50 | + ->getQuery() |
| 51 | + ->getResult(); |
| 52 | + $output->writeln('Boucle sur chacun des ' . count($period_positions) . ' postes types'); |
| 53 | + |
| 54 | + foreach ($period_positions as $period_position) { |
| 55 | + // find shifts_without_position corresponding to this period_position |
| 56 | + $period_position_shifts_without_position = $em->getRepository('AppBundle:Shift')->createQueryBuilder('s') |
| 57 | + // ->set('DATEFIRST', 1) |
| 58 | + ->where('s.position is null') |
| 59 | + ->andWhere("DATE_FORMAT(s.start, '%H:%i') = :period_start_time") |
| 60 | + ->andWhere("DATE_FORMAT(s.end, '%H:%i') = :period_end_time") |
| 61 | + ->andWhere("DATE_FORMAT(s.start, '%w') = :period_day_of_week") |
| 62 | + // ->andWhere() // week cycle |
| 63 | + ->andWhere('s.job = :job') |
| 64 | + ->andWhere('s.formation = :formation') |
| 65 | + ->setParameter('period_start_time', $period_position->getPeriod()->getStart()->format('H:i')) |
| 66 | + ->setParameter('period_end_time', $period_position->getPeriod()->getEnd()->format('H:i')) |
| 67 | + ->setParameter('period_day_of_week', ($period_position->getPeriod()->getDayOfWeek() == 6) ? 0 : ($period_position->getPeriod()->getDayOfWeek() + 1)) |
| 68 | + // ->setParameter('period_position_week_cycle', $period_position->getWeekCycle()) |
| 69 | + ->setParameter('job', $period_position->getPeriod()->getJob()) |
| 70 | + ->setParameter('formation', $period_position->getFormation()) |
| 71 | + ->orderBy('s.start') |
| 72 | + ->getQuery() |
| 73 | + ->getResult(); |
| 74 | + |
| 75 | + if (count($period_position_shifts_without_position)) { |
| 76 | + $output->writeln('Poste ' . $period_position->getId()); |
| 77 | + $output->writeln('Premier créneau trouvé : ' . $period_position_shifts_without_position[0]->getDisplayDateFullWithTime() . ' |Dernier créneau trouvé : ' . end($period_position_shifts_without_position)->getDisplayDateFullWithTime()); |
| 78 | + $output->writeln('Nombre de créneau correspondants : ' . count($period_position_shifts_without_position)); |
| 79 | + // we only want 1 shift per week |
| 80 | + // why ? period can have identical positions, which generate identical shifts... |
| 81 | + $period_position_shifts_without_position_unique_days = array(); |
| 82 | + $period_position_shifts_without_position_filtered = array(); |
| 83 | + foreach($period_position_shifts_without_position as $s) { |
| 84 | + if (!in_array($s->getStart()->format('Y-m-d'), $period_position_shifts_without_position_unique_days)) { |
| 85 | + array_push($period_position_shifts_without_position_unique_days, $s->getStart()->format('Y-m-d')); |
| 86 | + array_push($period_position_shifts_without_position_filtered, $s); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (!$dry_run) { |
| 91 | + $em->createQuery("UPDATE AppBundle:Shift s SET s.position = :position WHERE s.id in (:ids)") |
| 92 | + ->setParameter('position', $period_position) |
| 93 | + ->setParameter('ids', $period_position_shifts_without_position_filtered) |
| 94 | + ->execute(); |
| 95 | + } |
| 96 | + $shifts_without_position_fixed += count($period_position_shifts_without_position_filtered); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (!$dry_run) { |
| 101 | + $output->writeln('<info>' . $shifts_without_position_fixed . ' créneau' . (($shifts_without_position_fixed>1) ? 'x':'') . ' réparé' . (($shifts_without_position_fixed>1) ? 's':'') . '</info>'); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments