Funcionalidad de mardar mails hecha
This commit is contained in:
@@ -59,6 +59,14 @@ namespace Controladora
|
||||
// Utiliza la instancia singleton de InformeEmail para enviar el correo
|
||||
return InformeEmail.Instance.EnviarEmailFactura(titulo, detalles);
|
||||
}
|
||||
public string EnviarEmail(string titulo, List<Factura> facturas)
|
||||
{
|
||||
if (facturas == null || facturas.Count == 0)
|
||||
return "La lista de facturas está vacía o es nula.";
|
||||
|
||||
|
||||
return InformeEmail.Instance.EnviarEmailFacturas(titulo, facturas);
|
||||
}
|
||||
public bool Informa
|
||||
{
|
||||
get
|
||||
|
||||
@@ -20,12 +20,119 @@ namespace Informes
|
||||
get { return instance; }
|
||||
}
|
||||
|
||||
private InformeEmail()
|
||||
public string EnviarEmailFactura(string titulo, List<DetalleFactura> desc)
|
||||
{
|
||||
string body = GenerarTablaDetalle(desc);
|
||||
return SendMail(titulo, body);
|
||||
|
||||
}
|
||||
public string EnviarEmailFacturas(string titulo, List<Factura> facturas)
|
||||
{
|
||||
string body = GenerarTablaFacturas(facturas);
|
||||
return SendMail(titulo, body);
|
||||
}
|
||||
|
||||
public string EnviarEmailFactura(string titulo, List<DetalleFactura> desc)
|
||||
private string GenerarTablaFacturas(List<Factura> facturas)
|
||||
{
|
||||
string body = @"
|
||||
<style>
|
||||
table, td, th {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
";
|
||||
foreach (var item in facturas)
|
||||
{
|
||||
body += $"""
|
||||
<hr>
|
||||
<h3>Factura del {item.Fecha}, cliente: {item.NombreCliente}</h3>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Producto</td>
|
||||
<th>Cantidad</td>
|
||||
<th>Precio CU</td>
|
||||
<th>Subtotal</td>
|
||||
</tr>
|
||||
""";
|
||||
foreach (var i in item.Detalles)
|
||||
{
|
||||
body += $"""
|
||||
<tr>
|
||||
<td>{i.Producto.Nombre}</td>
|
||||
<td>{i.Cantidad}</td>
|
||||
<td>{i.Producto.Precio}</td>
|
||||
<td>{i.Producto.Precio * i.Cantidad}</td>
|
||||
</tr>
|
||||
""";
|
||||
}
|
||||
body += $"""
|
||||
<tr>
|
||||
<td>Total</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>{CalcularTotal(item.Detalles)}</td>
|
||||
</tr>
|
||||
</table>
|
||||
""";
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
private string GenerarTablaDetalle(List<DetalleFactura> desc)
|
||||
{
|
||||
// Esta seccion es el armado del mail html
|
||||
string body =
|
||||
@"
|
||||
<style>
|
||||
table, td, th {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Producto</td>
|
||||
<th>Cantidad</td>
|
||||
<th>Precio CU</td>
|
||||
<th>Subtotal</td>
|
||||
</tr>";
|
||||
|
||||
foreach (var i in desc)
|
||||
{
|
||||
body +=
|
||||
@$"
|
||||
<tr>
|
||||
<td>{i.Producto.Nombre}</td>
|
||||
<td>{i.Cantidad}</td>
|
||||
<td>{i.Producto.Precio}</td>
|
||||
<td>{i.Producto.Precio * i.Cantidad}</td>
|
||||
</tr>";
|
||||
}
|
||||
|
||||
body +=
|
||||
@$"
|
||||
<tr>
|
||||
<td>Total</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>{CalcularTotal(desc)}</td>
|
||||
</tr>
|
||||
</table>";
|
||||
|
||||
return body;
|
||||
}
|
||||
private double CalcularTotal(List<DetalleFactura> desc)
|
||||
{
|
||||
double total = 0;
|
||||
foreach (var i in desc)
|
||||
{
|
||||
total += i.Producto.Precio * i.Cantidad;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private string SendMail(string titulo, string body)
|
||||
{
|
||||
string? ret = null;
|
||||
string json;
|
||||
@@ -58,16 +165,13 @@ namespace Informes
|
||||
MailMessage mail = new MailMessage();
|
||||
mail.Subject = titulo;
|
||||
mail.IsBodyHtml = true;
|
||||
mail.Body = GenerarTabla(desc);
|
||||
mail.Body = body;
|
||||
mail.Sender = new MailAddress(config.EmailAddr);
|
||||
foreach (var i in config.EmailTarget) mail.To.Add(i);
|
||||
mail.From = new MailAddress(config.EmailAddr);
|
||||
|
||||
try
|
||||
{
|
||||
#if DEBUG
|
||||
Console.WriteLine($"From: {config.EmailAddr}, Title: {titulo}");
|
||||
#endif
|
||||
smtp.Send(mail);
|
||||
mail.Dispose();
|
||||
}
|
||||
@@ -76,65 +180,11 @@ namespace Informes
|
||||
ret = "No se pudo comunicar con el server SMTP";
|
||||
throw;
|
||||
}
|
||||
|
||||
//Decimos que se envio el email correctamente si el valor del retorno sigue siendo nulo en otro caso tendra la descripcion del error.
|
||||
return (ret == null) ?
|
||||
"Se envio el Email Correctamente":
|
||||
"Se envio el Email Correctamente" :
|
||||
ret;
|
||||
//
|
||||
}
|
||||
|
||||
private string GenerarTabla(List<DetalleFactura> desc)
|
||||
{
|
||||
// Esta seccion es el armado del mail html
|
||||
string body =
|
||||
@"
|
||||
<style>
|
||||
table, td, th {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Producto</td>
|
||||
<th>Cantidad</td>
|
||||
<th>Precio CU</td>
|
||||
<th>Subtotal</td>
|
||||
</tr>";
|
||||
|
||||
foreach (var i in desc)
|
||||
{
|
||||
body +=
|
||||
@$"
|
||||
<tr>
|
||||
<td>{i.Producto.Nombre}</td>
|
||||
<td>{i.Cantidad}</td>
|
||||
<td>{i.Producto.Precio}</td>
|
||||
<td>{i.Producto.Precio * i.Cantidad}</td>
|
||||
</tr>";
|
||||
}
|
||||
|
||||
body +=
|
||||
@$"
|
||||
<tr>
|
||||
<td>Total</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>{CalcularTotal(desc)}</td>
|
||||
</tr>
|
||||
</table>
|
||||
";
|
||||
return body;
|
||||
}
|
||||
private double CalcularTotal(List<DetalleFactura> desc)
|
||||
{
|
||||
double total = 0;
|
||||
foreach (var i in desc)
|
||||
{
|
||||
total += i.Producto.Precio * i.Cantidad;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,12 +136,13 @@
|
||||
//
|
||||
// btnEnviarEmail
|
||||
//
|
||||
btnEnviarEmail.Location = new Point(26, 364);
|
||||
btnEnviarEmail.Location = new Point(26, 143);
|
||||
btnEnviarEmail.Name = "btnEnviarEmail";
|
||||
btnEnviarEmail.Size = new Size(200, 26);
|
||||
btnEnviarEmail.TabIndex = 9;
|
||||
btnEnviarEmail.Text = "Enviar Informe Por Email";
|
||||
btnEnviarEmail.UseVisualStyleBackColor = true;
|
||||
btnEnviarEmail.Click += btnEnviarEmail_Click;
|
||||
//
|
||||
// FrmInformeFacturaPorFecha
|
||||
//
|
||||
|
||||
@@ -16,9 +16,11 @@ namespace Vista.Informes
|
||||
public partial class FrmInformeFacturaPorFecha : Form
|
||||
{
|
||||
private ReadOnlyCollection<Factura>? facturas;
|
||||
private DateTime fecini, fecfin;
|
||||
public FrmInformeFacturaPorFecha()
|
||||
{
|
||||
InitializeComponent();
|
||||
if (ControladoraInformes.Instance.RecuperarConfig().Informar == false) btnEnviarEmail.Enabled = false;
|
||||
}
|
||||
|
||||
private void btnBuscar_Click(object sender, EventArgs e)
|
||||
@@ -29,6 +31,10 @@ namespace Vista.Informes
|
||||
return;
|
||||
}
|
||||
|
||||
//es por si mandas un email mantener el state de las fechas
|
||||
fecini = dateInicio.Value;
|
||||
fecfin = dateFin.Value;
|
||||
|
||||
facturas = ControladoraInformes.Instance.MostrarFacturasEnRangoDeFechas(dateInicio.Value, dateFin.Value);
|
||||
|
||||
|
||||
@@ -79,5 +85,16 @@ namespace Vista.Informes
|
||||
column.Visible = column.Name == "Subtotal" || column.Name == "NombreProducto" || column.Name == "Cantidad";
|
||||
}
|
||||
}
|
||||
|
||||
private void btnEnviarEmail_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (facturas == null || facturas.Count <= 0)
|
||||
{
|
||||
MessageBox.Show("No hay facturas para mandar email");
|
||||
return;
|
||||
}
|
||||
string msg = ControladoraInformes.Instance.EnviarEmail($"Facturas del {fecini.ToString()} hasta {fecfin.ToString()}", facturas.ToList());
|
||||
MessageBox.Show(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,7 @@
|
||||
btnEnviarEmail.TabIndex = 19;
|
||||
btnEnviarEmail.Text = "Enviar Informe Por Email";
|
||||
btnEnviarEmail.UseVisualStyleBackColor = true;
|
||||
btnEnviarEmail.Click += btnEnviarEmail_Click;
|
||||
//
|
||||
// label4
|
||||
//
|
||||
|
||||
@@ -17,10 +17,13 @@ namespace Vista.Informes
|
||||
public partial class FrmInformeFacturasPorCliente : Form
|
||||
{
|
||||
private ReadOnlyCollection<Factura> facturas;
|
||||
private DateTime fecini, fecfin;
|
||||
public FrmInformeFacturasPorCliente()
|
||||
{
|
||||
InitializeComponent();
|
||||
IniciarTablaClientes();
|
||||
if (ControladoraInformes.Instance.RecuperarConfig().Informar == false) btnEnviarEmail.Enabled = false;
|
||||
|
||||
}
|
||||
|
||||
private void IniciarTablaClientes()
|
||||
@@ -37,6 +40,10 @@ namespace Vista.Informes
|
||||
if (Check())
|
||||
{
|
||||
Cliente cli = (Cliente)dgvCliente.SelectedRows[0].DataBoundItem;
|
||||
|
||||
//es por si mandas un email mantener el state de las fechas
|
||||
fecini = dateInicio.Value;
|
||||
fecfin = dateFin.Value;
|
||||
var lista = ControladoraInformes.Instance.MostrarFacturasDeClienteEnRangoDeFechas(cli, dateInicio.Value, dateFin.Value);
|
||||
|
||||
if (lista == null)
|
||||
@@ -104,5 +111,16 @@ namespace Vista.Informes
|
||||
column.Visible = column.Name == "Subtotal" || column.Name == "NombreProducto" || column.Name == "Cantidad";
|
||||
}
|
||||
}
|
||||
|
||||
private void btnEnviarEmail_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (facturas == null || facturas.Count <= 0)
|
||||
{
|
||||
MessageBox.Show("No hay facturas para mandar email");
|
||||
return;
|
||||
}
|
||||
string msg = ControladoraInformes.Instance.EnviarEmail($"Facturas cliente: {facturas[0].NombreCliente} del {fecini.ToString()} hasta {fecfin.ToString()}", facturas.ToList());
|
||||
MessageBox.Show(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user