public function testRepeatedDrivingAssertionCRUD()
 {
     $driverCategory = $this->createDriverCategory('Zivildienst');
     $address = Address::registerAddress('Burstrasse 22c', '6333', 'Baar', 'Schweiz');
     $this->init->addressRepo->store($address);
     $driver = Driver::registerDriver('m', 'Max', 'Mühlemann', '041 222 32 32', $address, 'F3234141', $driverCategory, true, '', '*****@*****.**', new \DateTime(), new \DateTime(), 5, 'alles nur ein Test');
     $this->init->driverRepo->store($driver);
     $shiftTypes[] = $this->createShiftType('Shift 1');
     $shiftTypes[] = $this->createShiftType('Shift 2');
     //weekly
     $repeatedDrivingAssertionWeekly = new RepeatedWeeklyDrivingAssertion();
     $repeatedDrivingAssertionWeekly->setWeekday(1);
     $repeatedDrivingAssertionWeekly->setShiftTypes($shiftTypes);
     $this->init->repeatedDrivingAssertionRepo->store($repeatedDrivingAssertionWeekly);
     $repeatedDrivingAssertionPlan = RepeatedDrivingAssertionPlan::registerRepeatedAssertionPlan('test', new \DateTime(), 'weekly', true);
     $repeatedDrivingAssertionPlan->assignDriver($driver);
     $repeatedDrivingAssertionPlan->assignRepeatedDrivingAssertion($repeatedDrivingAssertionWeekly);
     $this->init->repeatedDrivingAssertionPlanRepo->store($repeatedDrivingAssertionPlan);
     $this->init->em->flush();
     $find = $this->init->repeatedDrivingAssertionPlanRepo->find($repeatedDrivingAssertionPlan->getId());
     $this->assertEquals($find, $repeatedDrivingAssertionPlan);
     //monthly
     $repeatedDrivingAssertionMonthly = new RepeatedMonthlyDrivingAssertion();
     $repeatedDrivingAssertionMonthly->setRelativeWeekAsText('first');
     $repeatedDrivingAssertionMonthly->setWeekdayAsText('monday');
     $this->init->repeatedDrivingAssertionRepo->store($repeatedDrivingAssertionMonthly);
     $repeatedDrivingAssertionPlan = RepeatedDrivingAssertionPlan::registerRepeatedAssertionPlan('test', new \DateTime(), 'monthly', true);
     $repeatedDrivingAssertionPlan->assignDriver($driver);
     $repeatedDrivingAssertionPlan->assignRepeatedDrivingAssertion($repeatedDrivingAssertionMonthly);
     $this->init->repeatedDrivingAssertionPlanRepo->store($repeatedDrivingAssertionPlan);
     $this->init->em->flush();
     $find = $this->init->repeatedDrivingAssertionPlanRepo->find($repeatedDrivingAssertionPlan->getId());
     $this->assertEquals($find, $repeatedDrivingAssertionPlan);
 }
 /**
  * @param RepeatedDrivingAssertionRegisterDTO $dto
  * @return ArrayCollection
  */
 public function repeatedRegisterDTOtoWeeklyDrivingAssertions(RepeatedDrivingAssertionRegisterDTO $dto)
 {
     $weeklyDrivingAssertions = new ArrayCollection();
     /** @var ShiftSelectionDTO $shiftSelectionDTO */
     foreach ($dto->getWeeklyShiftSelections() as $shiftSelectionDTO) {
         $weekday = $this->explodeWeeklySelectionId($shiftSelectionDTO->getSelectionId());
         $weeklyDrivingAssertion = new RepeatedWeeklyDrivingAssertion();
         $weeklyDrivingAssertion->setWeekday(WeekdayService::$weekdayToNumericConverter[$weekday]);
         $weeklyDrivingAssertion->setShiftTypes($shiftSelectionDTO->getShiftSelection());
         $weeklyDrivingAssertions->add($weeklyDrivingAssertion);
     }
     return $weeklyDrivingAssertions;
 }
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('building variables...');
     $month = $input->getArgument('month');
     if (!$month) {
         $month = 1;
     }
     $em = $this->getContainer()->get('entity_manager');
     $workingMonthRepo = $this->getContainer()->get('workingmonth_repository');
     $workingDayRepo = $this->getContainer()->get('workingday_repository');
     $shiftRepo = $this->getContainer()->get('shift_repository');
     $shiftTypeRepo = $this->getContainer()->get('shifttype_repository');
     $drivingPoolRepo = $this->getContainer()->get('drivingpool_repository');
     $passengerRepo = $this->getContainer()->get('passenger_repository');
     $driverRepo = $this->getContainer()->get('driver_repository');
     $vehicleRepo = $this->getContainer()->get('vehicle_repository');
     $addressRepo = $this->getContainer()->get('address_repository');
     $poiRepo = $this->getContainer()->get('poi_repository');
     $routeRepo = $this->getContainer()->get('route_repository');
     $drivingMissionRepo = $this->getContainer()->get('drivingmission_repository');
     $drivingOrderRepo = $this->getContainer()->get('drivingorder_repository');
     $repeatedDrivingAssertionRepo = $this->getContainer()->get('repeateddrivingassertion_repository');
     $repeatedDrivingAssertionPlanRepo = $this->getContainer()->get('repeateddrivingassertionplan_repository');
     $repeatedDrivingOrderRepo = $this->getContainer()->get('repeateddrivingorder_repository.doctrine');
     $repeatedDrivingOrderPlanRepo = $this->getContainer()->get('repeateddrivingorderplan_repository.doctrine');
     $time = $this->getContainer()->get('tixi_api.datetimeservice');
     $routingMachine = $this->getContainer()->get('tixi_app.routingmachine');
     $routeManagement = $this->getContainer()->get('tixi_app.routemanagement');
     $dispoManagement = $this->getContainer()->get('tixi_app.dispomanagement');
     $this->zonePlanRepository = $this->getContainer()->get('zoneplan_repository');
     $this->zoneRepository = $this->getContainer()->get('zone_repository');
     $monthDate = new \DateTime('today');
     $monthDate->modify('+' . $month . ' month');
     // depreciated: $monthDate->modify('+ 10 year');
     $monthDate->modify('first day of this month');
     $shiftTypes = $shiftTypeRepo->findAllActive();
     $output->writeln('target date is ' . $monthDate->format('d.m.Y'));
     $output->writeln('building driver assertion plans...');
     $drivers = $driverRepo->findAllActive();
     foreach ($drivers as $driver) {
         //no other assertion for zivis
         if ($driver->getDriverCategory()->getId() == 2) {
             continue;
         }
         $reDrivingAssertionPlan = RepeatedDrivingAssertionPlan::registerRepeatedAssertionPlan('test', new \DateTime('today'), 'weekly', rand(0, 1));
         $reDrivingAssertionPlan->assignDriver($driver);
         $driver->assignRepeatedDrivingAssertionPlan($reDrivingAssertionPlan);
         $repeatedDrivingAssertionPlanRepo->store($reDrivingAssertionPlan);
         for ($i = 1; $i <= 7; $i++) {
             if (rand(0, 3) < 3) {
                 $reDrivingWeeklyAssertion = new RepeatedWeeklyDrivingAssertion();
                 $reDrivingWeeklyAssertion->addShiftType($shiftTypes[rand(0, count($shiftTypes) - 1)]);
                 $reDrivingWeeklyAssertion->addShiftType($shiftTypes[rand(0, count($shiftTypes) - 1)]);
                 $reDrivingWeeklyAssertion->addShiftType($shiftTypes[rand(0, count($shiftTypes) - 1)]);
                 $reDrivingWeeklyAssertion->setWeekday($i);
                 $reDrivingWeeklyAssertion->setAssertionPlan($reDrivingAssertionPlan);
                 $reDrivingAssertionPlan->assignRepeatedDrivingAssertion($reDrivingWeeklyAssertion);
                 $repeatedDrivingAssertionRepo->store($reDrivingWeeklyAssertion);
             }
         }
     }
     $em->flush();
     $output->writeln('building driving pools...');
     $drivingPools = 0;
     $workingMonth = $workingMonthRepo->findWorkingMonthByDate($monthDate);
     if ($workingMonth !== null) {
         $output->writeln("WorkingMonth " . $monthDate->format('m') . " already exists");
     } else {
         $workingMonth = $dispoManagement->openWorkingMonth($monthDate->format('Y'), $monthDate->format('m'));
         $workingDays = $workingMonth->getWorkingDays();
         //create workingDays shifts, assign them drivingpools, get amount of needed drivers
         /** @var $workingDay WorkingDay */
         foreach ($workingDays as $workingDay) {
             /** @var $shiftType ShiftType */
             foreach ($workingDay->getShifts() as $shift) {
                 $shift->setAmountOfDrivers(rand(12, 18));
                 for ($i = 1; $i <= $shift->getAmountOfDrivers(); $i++) {
                     $drivingPool = DrivingPool::registerDrivingPool($shift);
                     $shift->assignDrivingPool($drivingPool);
                     $drivingPoolRepo->store($drivingPool);
                     $drivingPools++;
                 }
             }
         }
     }
     $em->flush();
     $output->writeln('building unclassified zone...');
     $this->unclassifiedZone = $this->getUnclassifiedZone();
     if (is_null($this->unclassifiedZone)) {
         $output->writeln('error: cannot create unclassified zone!');
     }
     $output->writeln('building driving orders...');
     /**@var $pois POI[] */
     $pois = $poiRepo->findAll();
     $countPois = count($pois);
     $orders = array();
     $routes = array();
     //create Driving Orders
     $countOrders = 0;
     foreach ($shiftTypes as $shiftType) {
         $approxOrdersPerShift = rand(40, 60);
         for ($i = 0; $i < $approxOrdersPerShift; $i++) {
             /**@var $passenger Passenger */
             $passenger = $passengerRepo->find(rand(100, 500));
             $passenger->setIsInWheelChair(rand(0, 1));
             /**  WARNING: saving times in UTC on database, but minutesOfDay are from midnight, causing
              * wrong data if a time is UTC 23:30 but CET 00:30 (= 30 minutesOfDay)
              */
             $stStart = $time->convertToLocalDateTime($shiftType->getStart());
             $stEnd = $time->convertToLocalDateTime($shiftType->getEnd());
             $stDuration = $stStart->diff($stEnd);
             $minutes = $stDuration->h * 60 + $stDuration->i;
             $pickupTime = clone $stStart;
             $pickupTime->add(new \DateInterval('PT' . rand(1, $minutes) . 'M'));
             $order = DrivingOrder::registerDrivingOrder($passenger, $monthDate, $pickupTime, rand(0, 1), null, 0, 0, 1);
             $start = $passenger->getAddress();
             $target = $pois[rand(0, $countPois - 1)]->getAddress();
             $route = Route::registerRoute($start, $target);
             $hashKey = hash('crc32', $start->getHashFromBigIntCoordinates() . $target->getHashFromBigIntCoordinates());
             $routes[$hashKey] = $route;
             $route = $routeRepo->storeRouteIfNotExist($route);
             $order->assignRoute($route);
             $order->assignPassenger($passenger);
             $passenger->assignDrivingOrder($order);
             $drivingOrderRepo->store($order);
             array_push($orders, $order);
         }
     }
     $output->writeln('building driving missions and routes...');
     $routingMachine->fillRoutingInformationForMultipleRoutes($routes);
     /**@var $order DrivingOrder */
     foreach ($orders as $order) {
         $passenger = $order->getPassenger();
         $route = $order->getRoute();
         $boardingTime = DispositionVariables::BOARDING_TIME + DispositionVariables::DEBOARDING_TIME;
         $extraMinutesPassenger = $passenger->getExtraMinutes();
         $additionalTimesOnRide = $boardingTime + $extraMinutesPassenger;
         $serviceMinuteOfDay = $time->getMinutesOfDay($order->getPickUpTime());
         $serviceDuration = $route->getDurationInMinutes() + $additionalTimesOnRide;
         $serviceDistance = $route->getDistanceInMeters();
         //DrivingMission <-> DrivingOrder
         $drivingMission = DrivingMission::registerDrivingMission(rand(0, 1), $serviceMinuteOfDay, $serviceDuration, $serviceDistance);
         $drivingMission->assignDrivingOrder($order);
         $order->assignDrivingMission($drivingMission);
         $drivingMissionRepo->store($drivingMission);
         // DrivingOrder <-> Zone
         $cities = array($route->getStartAddress()->getCity(), $route->getTargetAddress()->getCity());
         $zone = $this->getZoneWithHighestPriorityForCities($cities);
         $order->assignZone($zone);
         $countOrders++;
     }
     $em->flush();
     $output->writeln("\n--------------------------------------------\n" . "Testdata created for month: " . $monthDate->format('m.Y') . " with:\n" . $drivingPools . " DrivingPools \n" . "And orders for one day: " . $monthDate->format('d.m.Y') . " with:\n" . $countOrders . " DrivingOrders and Routes \n");
 }