|
| 1 | +package com.carvalhotechsolutions.mundoanimal.utils; |
| 2 | + |
| 3 | +import com.carvalhotechsolutions.mundoanimal.model.Agendamento; |
| 4 | +import com.itextpdf.text.*; |
| 5 | +import com.itextpdf.text.Font; |
| 6 | +import com.itextpdf.text.Image; |
| 7 | +import com.itextpdf.text.pdf.*; |
| 8 | +import org.jfree.chart.ChartFactory; |
| 9 | +import org.jfree.chart.ChartUtilities; |
| 10 | +import org.jfree.chart.JFreeChart; |
| 11 | +import org.jfree.data.general.DefaultPieDataset; |
| 12 | +import java.io.*; |
| 13 | +import java.time.LocalDate; |
| 14 | +import java.time.format.DateTimeFormatter; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +public class RelatorioManager { |
| 20 | + |
| 21 | + public void gerarRelatorioPDF(List<Agendamento> agendamentos, LocalDate dataInicio, LocalDate dataFim, File file) throws IOException, DocumentException { |
| 22 | + Document document = new Document(PageSize.A4, 40, 40, 50, 50); |
| 23 | + PdfWriter.getInstance(document, new FileOutputStream(file)); |
| 24 | + |
| 25 | + document.open(); |
| 26 | + |
| 27 | + // Título |
| 28 | + Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); |
| 29 | + Paragraph title = new Paragraph("Relatório de Agendamentos - Pet Shop Mundo Animal", titleFont); |
| 30 | + title.setAlignment(Element.ALIGN_CENTER); |
| 31 | + document.add(title); |
| 32 | + |
| 33 | + // Período do relatório |
| 34 | + Font periodFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.ITALIC); |
| 35 | + Paragraph period = new Paragraph("Período: " + formatarData(dataInicio) + " - " + formatarData(dataFim), periodFont); |
| 36 | + period.setAlignment(Element.ALIGN_CENTER); |
| 37 | + document.add(period); |
| 38 | + document.add(new Paragraph("\n")); |
| 39 | + |
| 40 | + // Tabela de agendamentos |
| 41 | + PdfPTable table = criarTabelaAgendamentos(agendamentos); |
| 42 | + document.add(table); |
| 43 | + |
| 44 | + // Total arrecadado |
| 45 | + double total = calcularTotal(agendamentos); |
| 46 | + Font totalFont = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD); |
| 47 | + Paragraph totalParagraph = new Paragraph("Total Arrecadado No Período: R$ " + String.format("%.2f", total), totalFont); |
| 48 | + totalParagraph.setAlignment(Element.ALIGN_RIGHT); |
| 49 | + totalParagraph.setSpacingBefore(10f); |
| 50 | + document.add(totalParagraph); |
| 51 | + |
| 52 | + // Gerar gráfico de pizza |
| 53 | + JFreeChart chart = criarGraficoPizza(agendamentos); |
| 54 | + Image chartImage = converterGraficoParaImagem(chart); |
| 55 | + document.add(chartImage); |
| 56 | + |
| 57 | + document.close(); |
| 58 | + } |
| 59 | + |
| 60 | + private PdfPTable criarTabelaAgendamentos(List<Agendamento> agendamentos) { |
| 61 | + PdfPTable table = new PdfPTable(6); |
| 62 | + table.setWidthPercentage(100); |
| 63 | + table.setSpacingBefore(10f); |
| 64 | + table.setSpacingAfter(10f); |
| 65 | + |
| 66 | + String[] headers = {"Serviço", "Data", "Pet", "Tutor", "Profissional", "Valor"}; |
| 67 | + for (String header : headers) { |
| 68 | + PdfPCell cell = new PdfPCell(new Phrase(header, new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.WHITE))); |
| 69 | + cell.setHorizontalAlignment(Element.ALIGN_CENTER); |
| 70 | + cell.setBackgroundColor(BaseColor.DARK_GRAY); |
| 71 | + table.addCell(cell); |
| 72 | + } |
| 73 | + |
| 74 | + for (Agendamento a : agendamentos) { |
| 75 | + table.addCell(a.getServico().getNomeServico()); |
| 76 | + table.addCell(formatarData(a.getDataAgendamento())); |
| 77 | + table.addCell(a.getAnimal().getNome()); |
| 78 | + table.addCell(a.getCliente().getNome()); |
| 79 | + table.addCell(a.getResponsavelAtendimento()); |
| 80 | + table.addCell(String.format("R$ %.2f", a.getServico().getValorServico())); |
| 81 | + } |
| 82 | + |
| 83 | + return table; |
| 84 | + } |
| 85 | + |
| 86 | + private double calcularTotal(List<Agendamento> agendamentos) { |
| 87 | + return agendamentos.stream().mapToDouble(a -> a.getServico().getValorServico().doubleValue()).sum(); |
| 88 | + } |
| 89 | + |
| 90 | + private JFreeChart criarGraficoPizza(List<Agendamento> agendamentos) { |
| 91 | + DefaultPieDataset dataset = new DefaultPieDataset(); |
| 92 | + Map<String, Integer> servicoContagem = new HashMap<>(); |
| 93 | + |
| 94 | + // Contar quantos agendamentos tem por tipo de serviço |
| 95 | + for (Agendamento a : agendamentos) { |
| 96 | + servicoContagem.put(a.getServico().getNomeServico(), servicoContagem.getOrDefault(a.getServico().getNomeServico(), 0) + 1); |
| 97 | + } |
| 98 | + |
| 99 | + // Adicionar ao dataset do gráfico |
| 100 | + for (Map.Entry<String, Integer> entry : servicoContagem.entrySet()) { |
| 101 | + dataset.setValue(entry.getKey(), entry.getValue()); |
| 102 | + } |
| 103 | + |
| 104 | + return ChartFactory.createPieChart( |
| 105 | + "Distribuição de Agendamentos por Serviço", |
| 106 | + dataset, |
| 107 | + true, // Legenda |
| 108 | + true, // Tooltips |
| 109 | + false // URLs |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + private Image converterGraficoParaImagem(JFreeChart chart) throws IOException, BadElementException { |
| 114 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 115 | + ChartUtilities.writeChartAsPNG(baos, chart, 500, 300); |
| 116 | + return Image.getInstance(baos.toByteArray()); |
| 117 | + } |
| 118 | + |
| 119 | + private String formatarData(LocalDate data) { |
| 120 | + return data.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | + |
0 commit comments