feat: cli command to stamp uploaded invoice
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\Upload;
|
||||
use App\Service\Pdf\InvoiceApprover;
|
||||
use App\Service\Upload\UploadHandler;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'app:upload:stamp', description: 'Stamps upload of provided disposition as accepted invoice')]
|
||||
class StampUploadCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly UploadHandler $uploadHandler,
|
||||
private readonly InvoiceApprover $invoiceApprover,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addArgument('uuid', InputArgument::REQUIRED, 'Disposition uuid');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$disposition = $this
|
||||
->entityManager
|
||||
->getRepository(Disposition::class)
|
||||
->findOneBy(['uuid' => $input->getArgument('uuid')])
|
||||
;
|
||||
|
||||
$invoicePdf = $disposition->getDocumentByType(Upload::TYPE_INVOICE);
|
||||
$this->uploadHandler->ensurePdf($invoicePdf);
|
||||
$this->invoiceApprover->render($disposition);
|
||||
$invoicePdf->setStatus(Upload::STATUS_PAID);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\Upload;
|
||||
use App\Entity\User;
|
||||
use App\Service\Pdf\InvoiceApprover;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Spatie\Blink\Blink;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'app:uploads:stamp', description: 'Stamps all uploads of accepted invoices')]
|
||||
class StampUploadsCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly InvoiceApprover $invoiceApprover,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
$dispositions = $qb
|
||||
->select('disposition', 'upload')
|
||||
->from(Disposition::class, 'disposition')
|
||||
->innerJoin('disposition.documents', 'upload')
|
||||
->where($qb->expr()->andX(
|
||||
$qb->expr()->eq('upload.type', ':type'),
|
||||
$qb->expr()->eq('upload.status', ':status')
|
||||
))
|
||||
->setParameter('type', Upload::TYPE_INVOICE)
|
||||
->setParameter('status', Upload::STATUS_PAID)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
$progressBar = new ProgressBar($output, count($dispositions));
|
||||
$progressBar->start();
|
||||
$blink = new Blink();
|
||||
$userRepository = $this->entityManager->getRepository(User::class);
|
||||
|
||||
foreach ($dispositions as $disposition) {
|
||||
/** @var Disposition $disposition */
|
||||
$upload = $disposition->getDocumentByType(Upload::TYPE_INVOICE);
|
||||
if (null === $upload->getApprovedBy()) {
|
||||
$email = $upload->getUpdatedBy();
|
||||
if ($blink->has($email)) {
|
||||
$approvingUser = $blink->get($email);
|
||||
} else {
|
||||
$approvingUser = $userRepository->findOneBy(['email' => $email]);
|
||||
$blink->put($email, $approvingUser);
|
||||
}
|
||||
if (null !== $approvingUser) {
|
||||
$upload->setApprovedBy($approvingUser->getInitials());
|
||||
}
|
||||
}
|
||||
$this->invoiceApprover->render($disposition);
|
||||
$progressBar->advance();
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
$progressBar->finish();
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user