Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
16.67% |
6 / 36 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| PlanillasClients | |
16.67% |
6 / 36 |
|
33.33% |
1 / 3 |
26.83 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
16.13% |
5 / 31 |
|
0.00% |
0 / 1 |
4.36 | |||
| flashBatchResult | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Mail; |
| 6 | |
| 7 | use App\Controller\BaseController; |
| 8 | use App\Entity\Store; |
| 9 | use App\Repository\StoreRepository; |
| 10 | use App\Service\BulkMailService; |
| 11 | use App\Service\EmailHelper; |
| 12 | use App\Service\MailBatchResult; |
| 13 | use App\Service\PayrollHelper; |
| 14 | use App\Service\PdfHelper; |
| 15 | use Symfony\Component\Clock\ClockInterface; |
| 16 | use Symfony\Component\Mime\Email; |
| 17 | use Symfony\Component\HttpFoundation\Request; |
| 18 | use Symfony\Component\HttpFoundation\Response; |
| 19 | use Symfony\Component\Mime\Address; |
| 20 | use Symfony\Component\Routing\Attribute\Route; |
| 21 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 22 | |
| 23 | #[Route(path: '/mail/planillas-clients', name: 'mail_planillas_clients', methods: ['GET', 'POST'])] |
| 24 | #[IsGranted('ROLE_ADMIN')] |
| 25 | class PlanillasClients extends BaseController |
| 26 | { |
| 27 | public function __construct( |
| 28 | private readonly StoreRepository $storeRepository, |
| 29 | private readonly PdfHelper $PDFHelper, |
| 30 | private readonly EmailHelper $emailHelper, |
| 31 | private readonly PayrollHelper $payrollHelper, |
| 32 | private readonly BulkMailService $bulkMailService, |
| 33 | private readonly ClockInterface $clock, |
| 34 | ) {} |
| 35 | |
| 36 | public function __invoke(Request $request): Response |
| 37 | { |
| 38 | $recipients = $request->request->all('recipients'); |
| 39 | |
| 40 | if ($recipients === []) { |
| 41 | return $this->render('mail/planillas-clients.html.twig', [ |
| 42 | 'stores' => $this->storeRepository->getActive(), |
| 43 | ]); |
| 44 | } |
| 45 | |
| 46 | $now = $this->clock->now(); |
| 47 | $year = (int) $now->format('Y'); |
| 48 | $month = (int) $now->format('m'); |
| 49 | $fileName = sprintf('planilla-%d-%s.pdf', $year, $month); |
| 50 | |
| 51 | $result = $this->bulkMailService->sendToFilteredStores( |
| 52 | $this->storeRepository->getActive(), |
| 53 | $recipients, |
| 54 | function (Store $store) use ($year, $month, $fileName): Email { |
| 55 | $document = $this->PDFHelper->renderPayrollPdf($year, $month, $this->payrollHelper, (int) $store->getId()); |
| 56 | |
| 57 | return $this->emailHelper->createTemplatedEmail( |
| 58 | to: new Address($store->getUser()?->getEmail() ?? '', $store->getUser()?->getName() ?? ''), |
| 59 | subject: sprintf('Su planilla del local %s (%s - %d)', $store->getId(), $month, $year) |
| 60 | ) |
| 61 | ->htmlTemplate('email/client-planillas.twig') |
| 62 | ->context([ |
| 63 | 'user' => $store->getUser(), |
| 64 | 'store' => $store, |
| 65 | 'factDate' => sprintf('%d-%s-1', $year, $month), |
| 66 | 'fileName' => $fileName, |
| 67 | 'payroll' => $this->payrollHelper->getData($year, $month, (int) $store->getId()), |
| 68 | ]) |
| 69 | ->attach($document, $fileName); |
| 70 | } |
| 71 | ); |
| 72 | |
| 73 | $this->flashBatchResult($result); |
| 74 | |
| 75 | return $this->redirectToRoute('welcome'); |
| 76 | } |
| 77 | |
| 78 | private function flashBatchResult(MailBatchResult $result): void |
| 79 | { |
| 80 | if ($result->hasFailures()) { |
| 81 | $this->addFlash('warning', implode('<br>', $result->getFailures())); |
| 82 | } |
| 83 | |
| 84 | if ($result->hasSuccesses()) { |
| 85 | $this->addFlash('success', 'Mails have been sent to stores: '.implode(', ', $result->getSuccesses())); |
| 86 | } |
| 87 | } |
| 88 | } |