resq/src/Command/ReportRiderTime.php

148 lines
4.7 KiB
PHP

<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use DateTime;
use App\Entity\JOEvent;
use App\Ramcar\JOEventType;
class ReportRiderTime extends Command
{
protected $em;
protected $jo_index;
protected $filtered_jo_index;
public function __construct(EntityManagerInterface $em)
{
$this->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.')
->addArgument('date_from', InputArgument::REQUIRED, 'Date from.')
->addArgument('date_to', InputArgument::REQUIRED, 'Date to.');
}
protected function populateJOIndex($date_from, $date_to)
{
$jo_events = $this->em->createQuery("select e from App\Entity\JOEvent e where e.date_create >= :dfrom and e.date_create <= :dto")
->setParameters([
'dfrom' => $date_from,
'dto' => $date_to,
])
->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] = [
'create' => $jo->getDateCreate(),
'hub_assign' => null,
'rider_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::CREATE:
$this->jo_index[$jo_id]['create'] = $jo_event->getDateHappen();
break;
case JOEventType::HUB_ASSIGN:
$this->jo_index[$jo_id]['hub_assign'] = $jo_event->getDateHappen();
break;
case JOEventType::RIDER_ASSIGN:
$this->jo_index[$jo_id]['rider_assign'] = $jo_event->getDateHappen();
break;
case JOEventType::FULFILL:
$this->jo_index[$jo_id]['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)
{
// no hub assign because they used re-assign right away
// TODO: fix so we take this into account
if ($joi['hub_assign'] == null)
continue;
if($joi['rider_assign'] != null && $joi['fulfill'] != null)
{
$this->filtered_jo_index[$index] = $joi;
$interval = $joi['fulfill']->diff($joi['rider_assign']);
$this->filtered_jo_index[$index]['dispatch_secs'] = $joi['hub_assign']->getTimestamp() - $joi['create']->getTimestamp();
$this->filtered_jo_index[$index]['seconds'] = $joi['fulfill']->getTimestamp() - $joi['rider_assign']->getTimestamp();
}
}
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$csv_file = $input->getArgument('file');
$date_from = $input->getArgument('date_from');
$date_to = $input->getArgument('date_to');
// 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($date_from, $date_to);
$this->filterJOIndex();
foreach ($this->filtered_jo_index as $joi)
{
fputcsv($fh, [
$joi['data']['id'],
$joi['data']['date_schedule']->format('Y-m-d'),
$joi['dispatch_secs'],
$joi['seconds'],
]);
}
fclose($fh);
return 0;
}
}