Merge branch '162-phase-2-changes' into 'master'

Add report:rider-time command #162

Closes #162

See merge request jankstudio/resq!189
This commit is contained in:
Kendrick Chan 2018-09-14 06:50:21 +00:00
commit fa55f0c1ed

View file

@ -0,0 +1,121 @@
<?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\Common\Persistence\ObjectManager;
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(ObjectManager $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.');
}
protected function populateJOIndex()
{
$jo_events = $this->em->getRepository(JOEvent::class)->findAll();
$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);
}
}