2021-04-04 22:41:15 +02:00
|
|
|
<?php
|
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-04 22:41:15 +02:00
|
|
|
// src/Commands/RprtCommand.php;
|
|
|
|
|
|
|
|
namespace RprtCli\Commands;
|
|
|
|
|
2021-04-05 17:20:59 +02:00
|
|
|
use RprtCli\Utils\Configuration\ConfigurationInterface;
|
2021-04-05 16:23:06 +02:00
|
|
|
use RprtCli\Utils\CsvReport\CsvReportInterface;
|
2021-10-02 03:52:07 +02:00
|
|
|
use RprtCli\Utils\Mailer\MailerInterface;
|
2021-09-21 01:13:15 +02:00
|
|
|
use RprtCli\Utils\PdfExport\PdfExportInterface;
|
2021-09-20 01:08:42 +02:00
|
|
|
use RprtCli\Utils\TimeTrackingServices\YoutrackInterface;
|
2021-04-04 22:41:15 +02:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Helper\Table;
|
|
|
|
use Symfony\Component\Console\Helper\TableSeparator;
|
2021-09-20 01:08:42 +02:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2021-04-05 16:23:06 +02:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2021-09-20 01:08:42 +02:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
// use Symfony\Contracts\Translation\TranslatorInterface;
|
2021-04-04 22:41:15 +02:00
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
use function var_dump;
|
2021-04-04 22:41:15 +02:00
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
/**
|
|
|
|
* Main file - rprt command.
|
|
|
|
*/
|
|
|
|
class RprtCommand extends Command
|
|
|
|
{
|
|
|
|
protected $csv;
|
2021-04-05 17:20:59 +02:00
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
protected $configuration;
|
2021-04-04 22:41:15 +02:00
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
protected $youtrack;
|
2021-04-04 22:41:15 +02:00
|
|
|
|
2021-09-21 01:13:15 +02:00
|
|
|
protected $pdfExport;
|
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
public function __construct(
|
|
|
|
CsvReportInterface $csv,
|
|
|
|
ConfigurationInterface $configuration,
|
|
|
|
YoutrackInterface $youtrack,
|
2021-09-21 01:13:15 +02:00
|
|
|
PdfExportInterface $pdf_export,
|
2021-10-02 03:52:07 +02:00
|
|
|
MailerInterface $mailer,
|
2021-09-20 01:08:42 +02:00
|
|
|
?string $name = null
|
|
|
|
) {
|
|
|
|
$this->csv = $csv;
|
|
|
|
$this->configuration = $configuration;
|
|
|
|
$this->youtrack = $youtrack;
|
2021-09-21 01:13:15 +02:00
|
|
|
$this->pdfExport = $pdf_export;
|
2021-10-02 03:52:07 +02:00
|
|
|
$this->mailer = $mailer;
|
2021-09-20 01:08:42 +02:00
|
|
|
parent::__construct($name);
|
|
|
|
}
|
2021-04-05 16:23:06 +02:00
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
/**
|
|
|
|
* Get configuration.
|
|
|
|
*/
|
|
|
|
protected function configure() : void
|
|
|
|
{
|
|
|
|
$this->setName('rprt');
|
|
|
|
$this->setDescription('Generate monthly report');
|
|
|
|
// @TODO $this->addUsage('');
|
2021-09-22 23:46:07 +02:00
|
|
|
// @TODO add sub options (config overrides)
|
2021-09-20 01:08:42 +02:00
|
|
|
$this->addOption(
|
|
|
|
'file',
|
|
|
|
'f',
|
|
|
|
InputOption::VALUE_REQUIRED,
|
|
|
|
'Specify the input csv file to generate report from.'
|
|
|
|
);
|
|
|
|
$this->addOption(
|
|
|
|
'youtrack',
|
|
|
|
'y',
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Use youtrack api to get a report. If this option is used --file does not have any effect.'
|
|
|
|
);
|
|
|
|
$this->addOption(
|
|
|
|
'pdf',
|
|
|
|
'p',
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Create invoice pdf from template.'
|
|
|
|
);
|
|
|
|
$this->addOption(
|
|
|
|
'test',
|
|
|
|
't',
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Test login into youtrack service.'
|
|
|
|
);
|
2021-09-22 23:46:07 +02:00
|
|
|
$this->addOption(
|
|
|
|
'output',
|
|
|
|
'o',
|
|
|
|
InputOption::VALUE_REQUIRED,
|
|
|
|
'Provide output file path. This option overrides configuration.'
|
|
|
|
);
|
|
|
|
$this->addOption(
|
|
|
|
'send',
|
|
|
|
's',
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Send pdf export via email to recipient.'
|
|
|
|
);
|
2021-04-05 16:23:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 01:08:42 +02:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : int
|
|
|
|
{
|
|
|
|
if ($input->getOption('test')) {
|
|
|
|
$test = $this->youtrack->testYoutrackapi();
|
|
|
|
$output->writeln($test);
|
|
|
|
}
|
2021-09-21 01:13:15 +02:00
|
|
|
if ($youtrack = $input->getOption('youtrack')) {
|
|
|
|
$report_id = $this->youtrack->getReportId();
|
|
|
|
$file = $this->youtrack->downloadReport($report_id);
|
|
|
|
}
|
|
|
|
if ($youtrack || $file = $input->getOption('file')) {
|
|
|
|
// Youtrack can also provide a file name.
|
|
|
|
var_dump($file);
|
2021-09-20 01:08:42 +02:00
|
|
|
$data = $this->csv->getReportData($file);
|
|
|
|
$table = $this->generateTable($output, $data);
|
|
|
|
$table->render();
|
2021-09-21 01:13:15 +02:00
|
|
|
|
|
|
|
if ($pdf = $input->getOption('pdf')) {
|
2021-10-02 03:52:07 +02:00
|
|
|
$nice_data = $this->csv->arangeDataForDefaultPdfExport($data);
|
2021-09-21 01:13:15 +02:00
|
|
|
// @TODO method gatherTokens();
|
2021-09-22 23:46:07 +02:00
|
|
|
if ($output = $input->getOption('output')) {
|
|
|
|
$this->pdfExport->setOutput($output);
|
|
|
|
}
|
2021-10-02 03:52:07 +02:00
|
|
|
$output_path = $this->pdfExport->fromDefaultDataToPdf($nice_data);
|
2021-09-21 01:13:15 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 23:46:07 +02:00
|
|
|
// return Command::SUCCESS;
|
|
|
|
}
|
|
|
|
if ($send = $input->getOption('send') && $output_path) {
|
|
|
|
// Send email to configured address.
|
2021-10-02 03:52:07 +02:00
|
|
|
$this->mailer->sendDefaultMail($output_path);
|
2021-09-20 01:08:42 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 23:46:07 +02:00
|
|
|
// $this->dummyOutput($input, $output);
|
2021-09-20 01:08:42 +02:00
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
2021-04-04 22:41:15 +02:00
|
|
|
|
2021-04-08 19:23:19 +02:00
|
|
|
/**
|
|
|
|
* Create table from data that is already inline with configuration.
|
|
|
|
*/
|
2021-09-20 01:08:42 +02:00
|
|
|
protected function generateTable(OutputInterface $output, array $data) : Table
|
|
|
|
{
|
|
|
|
$table = new Table($output);
|
|
|
|
$table->setHeaders([
|
|
|
|
// $this->translator->trans('Project', [], 'messages', 'sl_SI'),
|
|
|
|
// $this->translator->trans('Hours', [], 'messages', 'sl_SI'),
|
|
|
|
// $this->translator->trans('Rate'),
|
|
|
|
// $this->translator->trans('Price'),
|
|
|
|
'Project', 'Hours', 'Rate', 'Price',
|
|
|
|
]);
|
|
|
|
[$rows, $totalHours, $totalPrice] = [[], 0, 0];
|
|
|
|
$projectsConfig = $this->configuration->get('projects');
|
|
|
|
foreach ($projectsConfig as $name => $config) {
|
|
|
|
if (! isset($data[$name])) {
|
|
|
|
// @TODO Proper error handling.
|
|
|
|
var_dump('Project ' . $name . ' is not set!');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$hours = $data[$name];
|
|
|
|
if ($config['time_format'] === 'm') {
|
|
|
|
$hours /= 60;
|
|
|
|
}
|
|
|
|
$price = $hours * (int) $config['price'];
|
|
|
|
$row = [
|
|
|
|
$config['name'],
|
|
|
|
$hours,
|
|
|
|
$config['price'],
|
|
|
|
$hours * $config['price'],
|
|
|
|
];
|
|
|
|
$rows[] = $row;
|
|
|
|
$totalHours += $hours;
|
|
|
|
$totalPrice += $price;
|
|
|
|
}
|
|
|
|
$rows[] = new TableSeparator();
|
|
|
|
// @TODO Check rate in final result.
|
|
|
|
// $rows[] = [$this->translator->trans('Sum'), $totalHours, $config['price'], $totalPrice];
|
|
|
|
$rows[] = ['Sum', $totalHours, $config['price'], $totalPrice];
|
|
|
|
|
|
|
|
$table->setRows($rows);
|
|
|
|
return $table;
|
2021-04-05 16:23:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dummy output for testing.
|
|
|
|
*/
|
2021-09-20 01:08:42 +02:00
|
|
|
protected function dummyOutput(InputInterface $input, OutputInterface $output) : void
|
|
|
|
{
|
|
|
|
// $txt = $this->translator->trans('From [start-date] to [end-date].', [], 'rprt', 'sl_SI');
|
|
|
|
// $output->writeln($txt);
|
|
|
|
$table = new Table($output);
|
|
|
|
$table->setHeaders(['Project', 'Hours', 'Price']);
|
|
|
|
$table->setRows([
|
|
|
|
['LDP', 100, 2600],
|
|
|
|
['WV', 50, 1300],
|
|
|
|
new TableSeparator(),
|
|
|
|
['Zusamen', 150, 3900],
|
|
|
|
]);
|
|
|
|
// $table->setStyle('borderless');
|
|
|
|
$table->render();
|
|
|
|
}
|
2021-04-04 22:41:15 +02:00
|
|
|
}
|