Skip to content
Snippets Groups Projects
Commit c64781fb authored by Verin Antoine's avatar Verin Antoine
Browse files

Ajout génération PDF #29

parent ff62641c
Branches
1 merge request!28Resolve "Générer pdf"
......@@ -169,6 +169,7 @@
<DependentUpon>FormMain.cs</DependentUpon>
</Compile>
<Compile Include="MailManager.cs" />
<Compile Include="PdfManager.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Controls\LoginForm.resx">
......
using System;
using System.Data;
using System.Windows.Forms;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Colorspace;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using HorizontalAlignment = iText.Layout.Properties.HorizontalAlignment;
namespace Mango
{
public class PdfManager
{
private static ConnectionManager _connection = ConnectionManager.GetInstance();
public static void GenererPdf(int eventId, string output)
{
var invites = _connection.Filter("Invites", "codeEvent = " + eventId);
foreach (DataRow row in invites.Rows)
{
DataRow participant = GetParticipant((int) row["codePart"]);
GenererPdf(eventId, (int) participant["codeParticipant"], output + "_" + participant["nomPart"] + ".pdf");
}
}
private static void GenererPdf(int eventId, int userId, string output)
{
DataRow even = _connection.Filter("Evenements", "codeEvent = " + eventId).Rows[0];
DataRow user = GetParticipant(userId);
DataTable bilan = _connection.Filter("BilanPart", "codeEvent = " + eventId);
// Génération du pdf
PdfDocument pdf = new PdfDocument(new PdfWriter(output));
Document document = new Document(pdf);
document.Add(new Paragraph()
.Add(new Text("Récapitulatif de l'évenement " + even["titreEvent"])
.SetFontColor(ColorConstants.ORANGE))
.Add(new Text("\n Date de début: " + DateTime.Parse(even["dateDebut"].ToString()).ToString("dd/MM/yyyy")))
.Add(new Text("\n Date de début: " + DateTime.Parse(even["datefin"].ToString()).ToString("dd/MM/yyyy")))
.Add(new Text("\n Description: " + even["description"]))
.SetHorizontalAlignment(HorizontalAlignment.CENTER));
document.Add(new Paragraph()
.Add(new Text("\n\nRécapitulatif pour " + user["nomComp"])
.SetFontColor(ColorConstants.ORANGE))
.Add("\n Nombre de part: " + user["nbParts"]));
document.Add(CreateDepenses(eventId, userId));
document.Add(CreateDepensesBeneficiaires(eventId, userId));
document.Add(CreateDoitPayerA(eventId, userId));
document.Add(CreateDoitRecevoirDe(eventId, userId));
document.Close();
}
private static Paragraph CreateDepenses(int eventId, int userId)
{
var paragraph = new Paragraph();
paragraph.Add(new Text("\n\nDépenses effectuées:\n"));
Table table = CreateTable("Description");
try
{
DataTable depenses = _connection.Filter("Depenses",
"codePart = " + userId + " AND codeEvent = " + eventId);
foreach (DataRow row in depenses.Rows)
{
table.AddCell(new Cell().Add(new Paragraph(row["description"] as string)));
table.AddCell(new Cell().Add(new Paragraph(row["montant"] + " €")));
}
}catch(ArgumentException) { }
paragraph.Add(table);
return paragraph;
}
private static Paragraph CreateDepensesBeneficiaires(int eventId, int userId)
{
Paragraph paragraph = new Paragraph();
paragraph.Add(new Text("\nDépenses bénéficiaires:\n"));
Table table = CreateTable("Description");
try
{
DataTable benefices = GetDepensesBeneficiaires(eventId, userId);
foreach (DataRow row in benefices.Rows)
{
table.AddCell(new Cell().Add(new Paragraph(row["description"] as string)));
table.AddCell(new Cell().Add(new Paragraph(row["montant"] + " €")));
}
}catch(ArgumentException) { }
paragraph.Add(table);
return paragraph;
}
private static Paragraph CreateDoitPayerA(int eventId, int userId)
{
Paragraph paragraph = new Paragraph();
paragraph.Add(new Text("\nDoit payer à:\n"));
Table table = CreateTable("Nom");
try
{
DataTable data = _connection.Filter("BilanPart",
"codeEvent = " + eventId + " AND codeDonneur = " + userId);
foreach (DataRow row in data.Rows)
{
table.AddCell(new Cell().Add(new Paragraph((string) GetParticipant((int) row["codeReceveur"])["nomComp"])));
table.AddCell(new Cell().Add(new Paragraph(row["montant"] + " €")));
}
}catch(ArgumentException) { }
paragraph.Add(table);
return paragraph;
}
private static Paragraph CreateDoitRecevoirDe(int eventId, int userId)
{
Paragraph paragraph = new Paragraph();
paragraph.Add(new Text("\nDoit recevoir de:\n"));
Table table = CreateTable("Nom");
try
{
DataTable data = _connection.Filter("BilanPart",
"codeEvent = " + eventId + " AND codeReceveur = " + userId);
foreach (DataRow row in data.Rows)
{
table.AddCell(new Cell().Add(new Paragraph((string) GetParticipant((int) row["codeDonneur"])["nomComp"])));
table.AddCell(new Cell().Add(new Paragraph(row["montant"] + " €")));
}
}catch(ArgumentException) { }
paragraph.Add(table);
return paragraph;
}
private static Table CreateTable(string colonneName)
{
Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
table.AddCell(new Cell().Add(new Paragraph(colonneName))
.SetBackgroundColor(ColorConstants.ORANGE));
table.AddCell(new Cell().Add(new Paragraph("Montant"))
.SetBackgroundColor(ColorConstants.ORANGE));
return table;
}
private static DataTable GetDepensesBeneficiaires(int eventId, int userId)
{
DataTable codeDettes = _connection.Filter("Beneficiaires", "codePart = " + userId);
bool first = true;
string codes = "";
foreach (DataRow row in codeDettes.Rows)
{
if (!first) codes += ", ";
else first = false;
codes += row["numDepense"];
}
return _connection.Filter("Depenses", "numDepense IN (" + codes + ") AND codeEvent = " + eventId);
}
private static DataRow GetParticipant(int userId)
{
return _connection.Filter("Participants", "codeParticipant = " + userId).Rows[0];
}
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment