Skip to content

Commit

Permalink
Script pour réparer les créneaux sans poste type (#1055)
Browse files Browse the repository at this point in the history
* FixShiftPositionCommand script

* Add extra rules

* Rename command
  • Loading branch information
raphodn authored Oct 27, 2023
1 parent 73b7f63 commit 376ae6b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
105 changes: 105 additions & 0 deletions src/AppBundle/Command/FixShiftMissingPositionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class FixShiftMissingPositionCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('app:shift:fix_missing_position')
->setDescription('Fix shifts without position (find and attach corresponding position)')
->setHelp('This command allows you to fix missing shift position data (most likely deleted by the migration Version20211223205749')
->addOption('dry_run', null, InputOption::VALUE_NONE, 'Dry run');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
$cycle_type = $this->getContainer()->getParameter('cycle_type');
$dry_run = $input->getOption('dry_run');

if ($dry_run) {
$output->writeln("<comment>Dry run: won't impact the database</comment>");
}

$shifts_without_position = $em->getRepository('AppBundle:Shift')->findBy(array('position' => null), array('start' => 'ASC'));
$output->writeln(count($shifts_without_position) . ' créneau' . ((count($shifts_without_position)>1) ? 'x':'') . ' sans poste type trouvé' . ((count($shifts_without_position)>1) ? 's':'') . '...');

if ($cycle_type == 'abcd') {
// TODO : add filter on weekCycle
// what if the cycle changed ?
$output->writeln('<error>Currently only works for coops without cycle_type.</error>');
return;
}

if ($shifts_without_position) {
$output->writeln('Premier créneau trouvé : ' . $shifts_without_position[0]->getDisplayDateFullWithTime());
$output->writeln('Dernier créneau trouvé : ' . end($shifts_without_position)->getDisplayDateFullWithTime());

$shifts_without_position_fixed = 0;
// faster to loop on periodPositions
$period_positions = $em->getRepository('AppBundle:PeriodPosition')
->createQueryBuilder('pp')
->leftJoin('pp.period', 'p')->addSelect('p')
->getQuery()
->getResult();
$output->writeln('Boucle sur chacun des ' . count($period_positions) . ' postes types');

foreach ($period_positions as $period_position) {
// find shifts_without_position corresponding to this period_position
$period_position_shifts_without_position = $em->getRepository('AppBundle:Shift')->createQueryBuilder('s')
// ->set('DATEFIRST', 1)
->where('s.position is null')
->andWhere("DATE_FORMAT(s.start, '%H:%i') = :period_start_time")
->andWhere("DATE_FORMAT(s.end, '%H:%i') = :period_end_time")
->andWhere("DATE_FORMAT(s.start, '%w') = :period_day_of_week")
// ->andWhere() // week cycle
->andWhere('s.job = :job')
->andWhere('s.formation = :formation')
->setParameter('period_start_time', $period_position->getPeriod()->getStart()->format('H:i'))
->setParameter('period_end_time', $period_position->getPeriod()->getEnd()->format('H:i'))
->setParameter('period_day_of_week', ($period_position->getPeriod()->getDayOfWeek() == 6) ? 0 : ($period_position->getPeriod()->getDayOfWeek() + 1))
// ->setParameter('period_position_week_cycle', $period_position->getWeekCycle())
->setParameter('job', $period_position->getPeriod()->getJob())
->setParameter('formation', $period_position->getFormation())
->orderBy('s.start')
->getQuery()
->getResult();

if (count($period_position_shifts_without_position)) {
$output->writeln('Poste ' . $period_position->getId());
$output->writeln('Premier créneau trouvé : ' . $period_position_shifts_without_position[0]->getDisplayDateFullWithTime() . ' |Dernier créneau trouvé : ' . end($period_position_shifts_without_position)->getDisplayDateFullWithTime());
$output->writeln('Nombre de créneau correspondants : ' . count($period_position_shifts_without_position));
// we only want 1 shift per week
// why ? period can have identical positions, which generate identical shifts...
$period_position_shifts_without_position_unique_days = array();
$period_position_shifts_without_position_filtered = array();
foreach($period_position_shifts_without_position as $s) {
if (!in_array($s->getStart()->format('Y-m-d'), $period_position_shifts_without_position_unique_days)) {
array_push($period_position_shifts_without_position_unique_days, $s->getStart()->format('Y-m-d'));
array_push($period_position_shifts_without_position_filtered, $s);
}
}

if (!$dry_run) {
$em->createQuery("UPDATE AppBundle:Shift s SET s.position = :position WHERE s.id in (:ids)")
->setParameter('position', $period_position)
->setParameter('ids', $period_position_shifts_without_position_filtered)
->execute();
}
$shifts_without_position_fixed += count($period_position_shifts_without_position_filtered);
}
}

if (!$dry_run) {
$output->writeln('<info>' . $shifts_without_position_fixed . ' créneau' . (($shifts_without_position_fixed>1) ? 'x':'') . ' réparé' . (($shifts_without_position_fixed>1) ? 's':'') . '</info>');
}
}
}
}
3 changes: 2 additions & 1 deletion src/AppBundle/Command/FixTimeLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$countShiftLogs = 0;
$em = $this->getContainer()->get('doctrine')->getManager();
$members = $em->getRepository('AppBundle:Membership')->findAll();

$countShiftLogs = 0;

foreach ($members as $member) {
if ($member->getFirstShiftDate()) {
$previous_cycle_start = $this->getContainer()->get('membership_service')->getStartOfCycle($member, -1);
Expand Down

0 comments on commit 376ae6b

Please sign in to comment.