diff --git a/.gitignore b/.gitignore index e9466c0..c7a086e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /app/vendor +/resources/data \ No newline at end of file diff --git a/README.org b/README.org index 694c0ed..aec2536 100644 --- a/README.org +++ b/README.org @@ -18,6 +18,7 @@ - hourly wage - folder for reports + ** Components - report service - provides a csv of a report - local csv file read (provided via argument) diff --git a/app/composer.json b/app/composer.json index 9cdf1fa..0272ee5 100644 --- a/app/composer.json +++ b/app/composer.json @@ -21,7 +21,7 @@ "repositories": [ { "type": "vcs", - "url": "https://git.nixnet.xyz/l3/rprt" + "url": "https://git.nixnet.services/l3n/RprtCli.git" } ], "autoload": { diff --git a/app/dependencies.php b/app/dependencies.php index e120d07..10879dc 100644 --- a/app/dependencies.php +++ b/app/dependencies.php @@ -4,8 +4,13 @@ use function DI\create; use function DI\get; use RprtCli\Commands\RprtCommand; +use RprtCli\Utils\CsvReport\CsvReport; +use RprtCli\Utils\CsvReport\CsvReportInterface; return [ - RprtCommand::class => DI\create( + \GuzzleHttp\ClientInterface::class => DI\get(\GuzzleHttp\Client::class), + CsvReportInterface::class => DI\get(CsvReport::class), + RprtCommand::class => DI\create()->constructor( + DI\get(CsvReportInterface::class) ) ]; diff --git a/app/src/Commands/RprtCommand.php b/app/src/Commands/RprtCommand.php index d529855..6ae15ca 100644 --- a/app/src/Commands/RprtCommand.php +++ b/app/src/Commands/RprtCommand.php @@ -4,17 +4,22 @@ namespace RprtCli\Commands; +use RprtCli\Utils\CsvReport\CsvReportInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableSeparator; +use Symfony\Component\Console\Input\InputOption; class RprtCommand extends Command { + protected $csv; - //public function __construct() { - // } + public function __construct(CsvReportInterface $csv) { + $this->csv = $csv; + parent::__construct(); + } /** * Get configuration. @@ -22,14 +27,43 @@ class RprtCommand extends Command { protected function configure(): void { $this->setName('rprt'); $this->setDescription('Generate monthly report'); + // @TODO $this->addUsage(''); + $this->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'Specify the input csv file to generate report from.'); } protected function execute(InputInterface $input, OutputInterface $output): int { - + + if ($file = $input->getOption('file')) { + $data = $this->csv->getReportData($file); + $table = $this->generateTable($output, $data); + $table->render(); + return Command::SUCCESS; + } + $this->dummyOutput($input, $output); return Command::SUCCESS; } + protected function generateTable($output, $data) { + // @TODO - get configuration + $table = new Table($output); + $table->setHeaders(['Project', 'Hours']); + $rows = []; + $together = 0; + foreach ($data as $project => $hours) { + $row = [$project, $hours/60]; + $together += $hours / 60; + $rows[] = $row; + } + $rows[] = new TableSeparator(); + $rows[] = ['Zusamen', $together]; + $table->setRows($rows); + return $table; + } + + /** + * Dummy output for testing. + */ protected function dummyOutput(InputInterface $input, OutputInterface $output): void { $output->writeln('I will output a nice table.'); $table = New Table($output); diff --git a/app/src/Utils/Configuration/ConfigurationInterface.php b/app/src/Utils/Configuration/ConfigurationInterface.php new file mode 100644 index 0000000..adda288 --- /dev/null +++ b/app/src/Utils/Configuration/ConfigurationInterface.php @@ -0,0 +1,42 @@ +dummyConfig(); + foreach (array_keys($config['projects']) as $key) { + $output[$key] = 0; + } + if ($file = fopen($file_path, 'r')) { + while (($line = fgetcsv($file)) !== FALSE) { + $parsed = $this->parseCsvFile($line); + // $key = reset(array_keys($parsed)); + $key = array_key_first($parsed); + if (isset($output[$key])) { + $output[$key] += (int) reset($parsed); + } + } + } + return $output; + } + + protected function parseCsvFile(array $raw_data): array { + $config = $this->dummyConfig(); + // var_dump($raw_data); + foreach ($config['projects'] as $key => $project) { + if (preg_match('/'.$project['pattern'].'/', $raw_data[1])) { + return [$key => $raw_data[4]]; + } + } + return []; + } + + protected function dummyConfig(): array { + $config = [ + 'projects' => [ + 'LDP' => [ + 'name' => 'lupus.digital', + 'pattern' => 'LDP-[0-9]+', + 'price' => 26, + // optional specify columns + ], + 'WV' => [ + 'name' => 'Wirtschaftsverlag', + 'pattern' => 'WV-[0-9]+', + 'price' => 26, + // optional specify columns + ], + 'Other' => [ + 'name' => 'Other projects', + 'pattern' => '(?!.\bLDP\b)(?!.\bWV\b)', + 'price' => 26, + // optional specify columns + ], + ], + ]; + + return $config; + } +} diff --git a/app/src/Utils/CsvReport/CsvReportInterface.php b/app/src/Utils/CsvReport/CsvReportInterface.php new file mode 100644 index 0000000..69a1090 --- /dev/null +++ b/app/src/Utils/CsvReport/CsvReportInterface.php @@ -0,0 +1,22 @@ +