Implementing CLI entrypoint.

This commit is contained in:
Alexey Skobkin 2020-02-01 03:05:54 +03:00
parent edf24cd480
commit 29524f2aec
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7

View file

@ -2,12 +2,76 @@
namespace App\Command; namespace App\Command;
use Asika\SimpleConsole\Console; use App\Generator\InvoiceGenerator;
use App\Invoice\InvoiceData;
use App\Kernel;
use Asika\SimpleConsole\{CommandArgsException, Console};
use Mpdf\Mpdf;
use Symfony\Component\Yaml\Yaml;
class InvoiceGeneratorCommand extends Console class InvoiceGeneratorCommand extends Console
{ {
private const OUTPUT_NAME_TEMPLATE = 'invoice_%d_%s.pdf';
protected $help = <<<HELP
[Usage] generate <number> <path>
[Arguments]
number Invoice number
path Can be directory or file name. File name will be generated if directory is used.
[Options]
i | issue-date Invoice issue date (format: '20.10.2040')
a | month-date Accounted month date (format: '20.10.2040')
h | help Show help information
v Show more debug information.
HELP;
protected function doExecute(): int protected function doExecute(): int
{ {
$number = $this->getArgument(0);
$path = $this->getArgument(1);
if (!$number) {
throw new CommandArgsException('Invoice number is mandatory.');
}
if (!$path) {
throw new CommandArgsException('Path is mandatory.');
}
$config = Yaml::parseFile(Kernel::getProjectRoot().'/config/parameters.yaml');
$now = new \DateTime();
$issueDate = $this->getOption(['i', 'issue-date'], $now->format('d.m.y'));
$accountedMonthDate = $this->getOption(['a', 'month-date'], $issueDate);
$invoice = new InvoiceData(
(int) $this->getArgument(0),
new \DateTime($issueDate),
new \DateTime($config['contract']['date_from']),
new \DateTime($accountedMonthDate)
);
$html = InvoiceGenerator::generate($invoice, $config);
$mpdf = new Mpdf();
$mpdf->WriteHTML($html);
if (is_dir($path)) {
$filePath = realpath($path).'/'.sprintf(
self::OUTPUT_NAME_TEMPLATE,
$number,
$invoice->getIssueDate()->format('d_m_y')
);
} else {
$filePath = $path;
}
$mpdf->Output($filePath, 'F');
$this->out($filePath.' exported.');
return 0; return 0;
} }
} }