Standardize input options, some verbosity, separator constants.

master
Lio Novelli 2022-05-15 11:55:50 +02:00
parent 7dfe2df78b
commit f498315b53
7 changed files with 56 additions and 18 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/resources/data /resources/data
/scratch /scratch
.phpcs-cache .phpcs-cache
*~undo-tree~

View File

@ -103,7 +103,7 @@ class InvoiceCommand extends Command
); );
$this->addOption( $this->addOption(
'recipients', 'recipients',
'r', 't',
InputOption::VALUE_REQUIRED, InputOption::VALUE_REQUIRED,
'Comma separated list of recipients that should get the exported pdf.' 'Comma separated list of recipients that should get the exported pdf.'
); );
@ -129,7 +129,7 @@ class InvoiceCommand extends Command
); );
$this->addOption( $this->addOption(
'report', 'report',
't', 'r',
InputOption::VALUE_OPTIONAL, InputOption::VALUE_OPTIONAL,
'Show time tracked for report.', 'Show time tracked for report.',
FALSE FALSE
@ -164,6 +164,10 @@ class InvoiceCommand extends Command
} }
if ($youtrack = $input->getOption('youtrack')) { if ($youtrack = $input->getOption('youtrack')) {
$report_id = $this->youtrack->getReportId(); $report_id = $this->youtrack->getReportId();
$cache_clear_status = $this->youtrack->clearReportCache($report_id);
if ($output->isVerbose()) {
$output->writeln("Report cache cleared, status: {$cache_clear_status}");
}
$file = $this->youtrack->downloadReport($report_id); $file = $this->youtrack->downloadReport($report_id);
} }
if ($input->hasParameterOption('--expenses') || $input->hasParameterOption('-e')) { if ($input->hasParameterOption('--expenses') || $input->hasParameterOption('-e')) {
@ -174,7 +178,9 @@ class InvoiceCommand extends Command
} }
if ($youtrack || $file = $input->getOption('file')) { if ($youtrack || $file = $input->getOption('file')) {
// Youtrack can also provide a file name. // Youtrack can also provide a file name.
// var_dump($file); if ($output->isVerbose()) {
$output->writeln("Csv file downloaded to: <info>{$file}</info>");
}
$data = $this->csv->getInvoiceData($file); $data = $this->csv->getInvoiceData($file);
if (!empty($expenses)) { if (!empty($expenses)) {
$data = array_merge($data, $expenses); $data = array_merge($data, $expenses);

View File

@ -73,12 +73,25 @@ class ReportCommand extends Command {
elseif ($report = $this->config->get('tracking_service.youtrack.report.default')) { elseif ($report = $this->config->get('tracking_service.youtrack.report.default')) {
$this->trackingService->setReportId($report); $this->trackingService->setReportId($report);
} }
elseif ($timeRange = $input->getParameterOption('time-range')) {
// @TODO: Implement time range option:
// - Request workTime items from tracking service
// - Filter them, join by issue, project ...
$output->writeln('<error>This option is not supported yet.</error>');
return Command::FAILURE;
}
// Currently we only support csv download. // Currently we only support csv download.
$report_id = $this->trackingService->getReportId(); $report_id = $this->trackingService->getReportId();
$report_name = $reports[$report_id]; $report_name = $reports[$report_id];
$this->trackingService->clearReportCache($report_id); // Code duplication.
$cache_clear_status = $this->timeTrackingService->clearReportCache($report_id);
if ($output->isVerbose()) {
$output->writeln("Report cache cleared, status: {$cache_clear_status}");
}
$file = $this->trackingService->downloadReport($report_id); $file = $this->trackingService->downloadReport($report_id);
// var_dump($file); if ($output->isVerbose()) {
$output->writeln("Csv file downloaded to: <info>{$file}</info>");
}
$output->writeln("report: <info>{$report_name}</info>"); $output->writeln("report: <info>{$report_name}</info>");
$data = $this->csv->generateReportTable($file); $data = $this->csv->generateReportTable($file);
$table = $this->buildTable($output, $data); $table = $this->buildTable($output, $data);

View File

@ -128,8 +128,8 @@ class ReportCsv implements ReportCsvInterface
foreach ($data as $invoice_element) { foreach ($data as $invoice_element) {
if ($invoice_element instanceof ExpensesInterface) { if ($invoice_element instanceof ExpensesInterface) {
if (!isset($added_expenses)) { if (!isset($added_expenses)) {
// @TODO - separator 0: Make next line bold and centered. // Separator 0: Make next line bold and centered.
$rows[] = 0; $rows[] = ReportCsvInterface::SEPARATOR_HARD;
$rows[] = [ $rows[] = [
null, null,
null, null,
@ -137,7 +137,7 @@ class ReportCsv implements ReportCsvInterface
'EUR', 'EUR',
]; ];
// Don't make next line bold. See RprtCli\PdfExport\PdfExportService::parsedDataToHtml. // Don't make next line bold. See RprtCli\PdfExport\PdfExportService::parsedDataToHtml.
$rows[] = FALSE; $rows[] = ReportCsvInterface::SEPARATOR_SOFT;
$added_expenses = TRUE; $added_expenses = TRUE;
} }
$add_separator = TRUE; $add_separator = TRUE;
@ -151,7 +151,7 @@ class ReportCsv implements ReportCsvInterface
} }
} }
if ($add_separator) { if ($add_separator) {
$rows[] = null; $rows[] = ReportCsvInterface::SEPARATOR_MEDIUM;
} }
$rows[] = [null, null, 'Gessamt brutto', number_format($totalPrice, 2, ',', '.')]; $rows[] = [null, null, 'Gessamt brutto', number_format($totalPrice, 2, ',', '.')];
return $rows; return $rows;
@ -184,9 +184,9 @@ class ReportCsv implements ReportCsvInterface
$previous_project = explode('-', $previous)[0]; $previous_project = explode('-', $previous)[0];
if ($project !== $previous_project) { if ($project !== $previous_project) {
// When project changes, add a sum of time for that project. // When project changes, add a sum of time for that project.
$table[] = null; $table[] = ReportCsvInterface::SEPARATOR_MEDIUM;
$table[] = [null, $previous_project, null, $project_time/60]; $table[] = [null, $previous_project, null, $project_time/60];
$table[] = null; $table[] = ReportCsvInterface::SEPARATOR_MEDIUM;
$time_sum += (float) $project_time; $time_sum += (float) $project_time;
$project_time = 0; $project_time = 0;
$all_projects[] = $project; $all_projects[] = $project;
@ -196,12 +196,12 @@ class ReportCsv implements ReportCsvInterface
$table[] = array_values($line); $table[] = array_values($line);
} }
// Add sum for the last project. // Add sum for the last project.
$table[] = null; $table[] = ReportCsvInterface::SEPARATOR_MEDIUM;
$table[] = [null, $project, null, $project_time / 60]; $table[] = [null, $project, null, $project_time / 60];
$time_sum += (float) $project_time; $time_sum += (float) $project_time;
$all_projects[] = $project; $all_projects[] = $project;
// Add a sum of time for whole day. // Add a sum of time for whole day.
$table[] = null; $table[] = ReportCsvInterface::SEPARATOR_MEDIUM;
$table[] = [null, implode(', ', $all_projects), null, $time_sum/60]; $table[] = [null, implode(', ', $all_projects), null, $time_sum/60];
return $table; return $table;
} }

View File

@ -9,6 +9,22 @@ namespace RprtCli\Utils\CsvReport;
*/ */
interface ReportCsvInterface interface ReportCsvInterface
{ {
/**
* Normal separator.
*/
const SEPARATOR_SOFT = FALSE;
/**
* Medium separator. Next line bold.
*/
const SEPARATOR_MEDIUM = NULL;
/**
* Next line should be bold and centered.
*/
const SEPARATOR_HARD = 0;
/** /**
* Returns array of hours per configured projects. * Returns array of hours per configured projects.
* *

View File

@ -7,6 +7,7 @@ namespace RprtCli\Utils\PdfExport;
use Exception; use Exception;
use RprtCli\Utils\Configuration\ConfigurationInterface; use RprtCli\Utils\Configuration\ConfigurationInterface;
use Mpdf\Output\Destination; use Mpdf\Output\Destination;
use RprtCli\Utils\CsvReport\ReportCsvInterface;
class PdfExportService implements PdfExportInterface { class PdfExportService implements PdfExportInterface {
@ -59,10 +60,10 @@ class PdfExportService implements PdfExportInterface {
$table .= '</tr></thead><tbody>'; $table .= '</tr></thead><tbody>';
foreach ($data as $row_index => $row) { foreach ($data as $row_index => $row) {
if (!$row) { if (!$row) {
if ($row === NULL) { if ($row === ReportCsvInterface::SEPARATOR_MEDIUM) {
$classes = 'bold'; $classes = 'bold';
} }
elseif ($row === 0) { elseif ($row === ReportCsvInterface::SEPARATOR_HARD) {
$classes = 'bold center'; $classes = 'bold center';
} }
continue; continue;

View File

@ -104,7 +104,7 @@ class YoutrackService implements YoutrackInterface
]); ]);
// Write csv response test into temporary file. // Write csv response test into temporary file.
$csv_file = tempnam(sys_get_temp_dir(), 'yt_csv_'); $csv_file = tempnam(sys_get_temp_dir(), "rprt-cli/{$report_id}_");
file_put_contents($csv_file, $csv_response->getBody()); file_put_contents($csv_file, $csv_response->getBody());
return $csv_file; return $csv_file;
} }
@ -113,7 +113,7 @@ class YoutrackService implements YoutrackInterface
if (isset($this->ytToken)) { if (isset($this->ytToken)) {
return $this->ytToken; return $this->ytToken;
} }
$yt_token = $this->config->get('tracking_service.youtrack.auth_token', FALSE); // $yt_token = $this->config->get('tracking_service.youtrack.auth_token', FALSE);
if (!$yt_token) { if (!$yt_token) {
$yt_token = readline('Enter your youtrack authentication token: '); $yt_token = readline('Enter your youtrack authentication token: ');
} }
@ -184,8 +184,9 @@ class YoutrackService implements YoutrackInterface
'headers' => $this->getHeaders(), 'headers' => $this->getHeaders(),
'json' => $post, 'json' => $post,
]); ]);
$body = (string) $response->getBody()->getContents(); // $body = (string) $response->getBody()->getContents();
// var_dump($body); // var_dump($body);
return $response->getStatusCode();
} }
} }