em = $em; parent::__construct(); } protected function configure() { $this->setName('report:rider-time') ->setDescription('Generate CSV file with report on rider time.') ->setHelp('Generate report on rider time from assignment to fulfilled.') ->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file.'); } protected function populateJOIndex() { $jo_events = $this->em->createQuery("select e from App\Entity\JOEvent e where e.date_create >= '20180905'") ->getResult(); // $jo_events = $this->em->getRepository(JOEvent::class)->findBy('date_create' => '20180905'); $this->jo_index = []; foreach ($jo_events as $jo_event) { $jo = $jo_event->getJobOrder(); $jo_id = $jo->getID(); if (!isset($this->jo_index[$jo_id])) $this->jo_index[$jo_id] = [ 'assign' => null, 'fulfill' => null, // 'data' => $jo, 'data' => [ 'id' => $jo->getID(), 'date_schedule' => $jo->getDateSchedule(), ], ]; // only interested in fulfilled and rider_assign switch ($jo_event->getTypeID()) { case JOEventType::RIDER_ASSIGN: $this->jo_index[$jo->getID()]['assign'] = $jo_event->getDateHappen(); break; case JOEventType::FULFILL: $this->jo_index[$jo->getID()]['fulfill'] = $jo_event->getDateHappen(); break; } } } protected function filterJOIndex() { // keep only the ones with both assign and fulfill status $this->filtered_jo_index = []; foreach ($this->jo_index as $index => $joi) { if($joi['assign'] != null && $joi['fulfill'] != null) { $this->filtered_jo_index[$index] = $joi; $interval = $joi['fulfill']->diff($joi['assign']); $this->filtered_jo_index[$index]['seconds'] = $joi['fulfill']->getTimestamp() - $joi['assign']->getTimestamp(); } } } protected function execute(InputInterface $input, OutputInterface $output) { $csv_file = $input->getArgument('file'); // attempt to open file try { $fh = fopen($csv_file, "w"); } catch (Exception $e) { throw new Exception('The file "' . $csv_file . '" could be opened.'); } $this->populateJOIndex(); $this->filterJOIndex(); foreach ($this->filtered_jo_index as $joi) { fputcsv($fh, [ $joi['data']['id'], $joi['data']['date_schedule']->format('Y-m-d'), $joi['seconds'], ]); } fclose($fh); } }