135 lines
5.0 KiB
PHP
135 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Service\Cron;
|
|
|
|
use App\Email\Mailer;
|
|
use App\Entity\Disposition;
|
|
use App\Entity\Upload;
|
|
use App\Repository\DispositionRepository;
|
|
use Doctrine\ORM\Query\Expr\Join;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
class UploadReminderService
|
|
{
|
|
public function __construct(
|
|
private readonly DispositionRepository $dispositionRepository,
|
|
private readonly Mailer $mailer,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
public function sendContractUploadReminders(): string
|
|
{
|
|
// Contracts have to be uploaded before assignment's begin date. Reminder is
|
|
// sent 7 and 14 days after disposition has been created.
|
|
$contractDueDateFirst = (new \DateTimeImmutable())->modify('-7 days');
|
|
$contractDueDateSecond = (new \DateTimeImmutable())->modify('-14 days');
|
|
|
|
// Find dispositions of assignments with due contract upload
|
|
$qb = $this->dispositionRepository->createQueryBuilder('disposition');
|
|
|
|
$dispositions = $qb
|
|
->select('disposition', 'assignment', 'destination')
|
|
->innerJoin('disposition.assignment', 'assignment')
|
|
->innerJoin('assignment.destination', 'destination')
|
|
->leftJoin('disposition.documents', 'document', Join::WITH, 'document.type = :documentType')
|
|
->where($qb->expr()->andX(
|
|
$qb->expr()->isNull('document'),
|
|
$qb->expr()->orX(
|
|
$qb->expr()->eq('document.createdAt', ':contractDueDateFirst'),
|
|
$qb->expr()->eq('document.createdAt', ':contractDueDateSecond'),
|
|
)
|
|
))
|
|
->setParameters([
|
|
'documentType' => Upload::TYPE_CONTRACT,
|
|
'contractDueDateFirst' => $contractDueDateFirst,
|
|
'contractDueDateSecond' => $contractDueDateSecond,
|
|
])
|
|
->getQuery()
|
|
->getResult()
|
|
;
|
|
|
|
if (0 === $count = count($dispositions)) {
|
|
$message = 'No due contract upload reminders to be sent to teamers';
|
|
$this->logger->info($message);
|
|
|
|
return $message;
|
|
}
|
|
|
|
foreach ($dispositions as $disposition) {
|
|
$teamer = $disposition->getTeamer();
|
|
|
|
$this->mailer->createAndSendEmail([
|
|
'disposition' => $disposition,
|
|
], [
|
|
'to' => $teamer->getCommunication()->getEmail(),
|
|
'subject' => 'Reminder: Fehlende Dokumente',
|
|
'template' => 'email/reminder_contract_upload.html.twig',
|
|
]);
|
|
}
|
|
|
|
$message = 'Sent '.$count.' due contract upload reminders to teamers';
|
|
$this->logger->info($message);
|
|
|
|
return $message;
|
|
}
|
|
|
|
public function sendInvoiceUploadReminders(): string
|
|
{
|
|
// Invoices have to be uploaded until 14 days after end of assignment. Reminder
|
|
// is sent three days before end of this period thus on the 11th day after end of assignment.
|
|
$invoiceDueDate = (new \DateTimeImmutable())->modify('-11 days');
|
|
|
|
// Find dispositions of assignments with due invoice upload
|
|
$qb = $this->dispositionRepository->createQueryBuilder('disposition');
|
|
|
|
$dispositions = $qb
|
|
->select('disposition', 'assignment', 'destination')
|
|
->innerJoin('disposition.assignment', 'assignment')
|
|
->innerJoin('assignment.destination', 'destination')
|
|
->leftJoin('disposition.documents', 'document', Join::WITH, 'document.type = :documentType')
|
|
->where($qb->expr()->andX(
|
|
$qb->expr()->isNull('document'),
|
|
$qb->expr()->orX(
|
|
// Due date can be determined by assignment's date which potentially overrides destination date
|
|
$qb->expr()->andX(
|
|
$qb->expr()->isNotNull('assignment.dateTo'),
|
|
$qb->expr()->eq('assignment.dateTo', ':dateTo')
|
|
),
|
|
$qb->expr()->eq('destination.dateTo', ':dateTo'),
|
|
)
|
|
))
|
|
->setParameters([
|
|
'documentType' => Upload::TYPE_INVOICE,
|
|
'dateTo' => $invoiceDueDate,
|
|
])
|
|
->getQuery()
|
|
->getResult()
|
|
;
|
|
|
|
if (0 === $count = count($dispositions)) {
|
|
$message = 'No due invoice upload reminders to be sent to teamers';
|
|
$this->logger->info($message);
|
|
|
|
return $message;
|
|
}
|
|
|
|
foreach ($dispositions as $disposition) {
|
|
$teamer = $disposition->getTeamer();
|
|
|
|
$this->mailer->createAndSendEmail([
|
|
'disposition' => $disposition,
|
|
], [
|
|
'to' => $teamer->getCommunication()->getEmail(),
|
|
'subject' => 'Reminder: Fehlende Dokumente',
|
|
'template' => 'email/reminder_invoice_upload.html.twig',
|
|
]);
|
|
}
|
|
|
|
$message = 'Sent '.$count.' due invoice upload reminders to teamers';
|
|
$this->logger->info($message);
|
|
|
|
return $message;
|
|
}
|
|
} |