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
|
// Utiliza la instancia singleton de InformeEmail para enviar el correo
|
||||||
return InformeEmail.Instance.EnviarEmailFactura(titulo, detalles);
|
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
|
public bool Informa
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|||||||
@@ -20,12 +20,119 @@ namespace Informes
|
|||||||
get { return instance; }
|
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? ret = null;
|
||||||
string json;
|
string json;
|
||||||
@@ -58,16 +165,13 @@ namespace Informes
|
|||||||
MailMessage mail = new MailMessage();
|
MailMessage mail = new MailMessage();
|
||||||
mail.Subject = titulo;
|
mail.Subject = titulo;
|
||||||
mail.IsBodyHtml = true;
|
mail.IsBodyHtml = true;
|
||||||
mail.Body = GenerarTabla(desc);
|
mail.Body = body;
|
||||||
mail.Sender = new MailAddress(config.EmailAddr);
|
mail.Sender = new MailAddress(config.EmailAddr);
|
||||||
foreach (var i in config.EmailTarget) mail.To.Add(i);
|
foreach (var i in config.EmailTarget) mail.To.Add(i);
|
||||||
mail.From = new MailAddress(config.EmailAddr);
|
mail.From = new MailAddress(config.EmailAddr);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if DEBUG
|
|
||||||
Console.WriteLine($"From: {config.EmailAddr}, Title: {titulo}");
|
|
||||||
#endif
|
|
||||||
smtp.Send(mail);
|
smtp.Send(mail);
|
||||||
mail.Dispose();
|
mail.Dispose();
|
||||||
}
|
}
|
||||||
@@ -76,65 +180,11 @@ namespace Informes
|
|||||||
ret = "No se pudo comunicar con el server SMTP";
|
ret = "No se pudo comunicar con el server SMTP";
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Decimos que se envio el email correctamente si el valor del retorno sigue siendo nulo en otro caso tendra la descripcion del error.
|
//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) ?
|
return (ret == null) ?
|
||||||
"Se envio el Email Correctamente":
|
"Se envio el Email Correctamente" :
|
||||||
ret;
|
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
|
||||||
//
|
//
|
||||||
btnEnviarEmail.Location = new Point(26, 364);
|
btnEnviarEmail.Location = new Point(26, 143);
|
||||||
btnEnviarEmail.Name = "btnEnviarEmail";
|
btnEnviarEmail.Name = "btnEnviarEmail";
|
||||||
btnEnviarEmail.Size = new Size(200, 26);
|
btnEnviarEmail.Size = new Size(200, 26);
|
||||||
btnEnviarEmail.TabIndex = 9;
|
btnEnviarEmail.TabIndex = 9;
|
||||||
btnEnviarEmail.Text = "Enviar Informe Por Email";
|
btnEnviarEmail.Text = "Enviar Informe Por Email";
|
||||||
btnEnviarEmail.UseVisualStyleBackColor = true;
|
btnEnviarEmail.UseVisualStyleBackColor = true;
|
||||||
|
btnEnviarEmail.Click += btnEnviarEmail_Click;
|
||||||
//
|
//
|
||||||
// FrmInformeFacturaPorFecha
|
// FrmInformeFacturaPorFecha
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -16,9 +16,11 @@ namespace Vista.Informes
|
|||||||
public partial class FrmInformeFacturaPorFecha : Form
|
public partial class FrmInformeFacturaPorFecha : Form
|
||||||
{
|
{
|
||||||
private ReadOnlyCollection<Factura>? facturas;
|
private ReadOnlyCollection<Factura>? facturas;
|
||||||
|
private DateTime fecini, fecfin;
|
||||||
public FrmInformeFacturaPorFecha()
|
public FrmInformeFacturaPorFecha()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
if (ControladoraInformes.Instance.RecuperarConfig().Informar == false) btnEnviarEmail.Enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void btnBuscar_Click(object sender, EventArgs e)
|
private void btnBuscar_Click(object sender, EventArgs e)
|
||||||
@@ -28,6 +30,10 @@ namespace Vista.Informes
|
|||||||
MessageBox.Show("La fecha de inicio no puede ser mayor que la fecha final.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("La fecha de inicio no puede ser mayor que la fecha final.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
return;
|
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);
|
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";
|
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.TabIndex = 19;
|
||||||
btnEnviarEmail.Text = "Enviar Informe Por Email";
|
btnEnviarEmail.Text = "Enviar Informe Por Email";
|
||||||
btnEnviarEmail.UseVisualStyleBackColor = true;
|
btnEnviarEmail.UseVisualStyleBackColor = true;
|
||||||
|
btnEnviarEmail.Click += btnEnviarEmail_Click;
|
||||||
//
|
//
|
||||||
// label4
|
// label4
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -17,10 +17,13 @@ namespace Vista.Informes
|
|||||||
public partial class FrmInformeFacturasPorCliente : Form
|
public partial class FrmInformeFacturasPorCliente : Form
|
||||||
{
|
{
|
||||||
private ReadOnlyCollection<Factura> facturas;
|
private ReadOnlyCollection<Factura> facturas;
|
||||||
|
private DateTime fecini, fecfin;
|
||||||
public FrmInformeFacturasPorCliente()
|
public FrmInformeFacturasPorCliente()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
IniciarTablaClientes();
|
IniciarTablaClientes();
|
||||||
|
if (ControladoraInformes.Instance.RecuperarConfig().Informar == false) btnEnviarEmail.Enabled = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void IniciarTablaClientes()
|
private void IniciarTablaClientes()
|
||||||
@@ -37,6 +40,10 @@ namespace Vista.Informes
|
|||||||
if (Check())
|
if (Check())
|
||||||
{
|
{
|
||||||
Cliente cli = (Cliente)dgvCliente.SelectedRows[0].DataBoundItem;
|
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);
|
var lista = ControladoraInformes.Instance.MostrarFacturasDeClienteEnRangoDeFechas(cli, dateInicio.Value, dateFin.Value);
|
||||||
|
|
||||||
if (lista == null)
|
if (lista == null)
|
||||||
@@ -97,12 +104,23 @@ namespace Vista.Informes
|
|||||||
}
|
}
|
||||||
private void RefrescarTablaDetalles(List<DetalleFactura> list)
|
private void RefrescarTablaDetalles(List<DetalleFactura> list)
|
||||||
{
|
{
|
||||||
dgvDetalle.DataSource = null;
|
dgvDetalle.DataSource = null;
|
||||||
dgvDetalle.DataSource = list;
|
dgvDetalle.DataSource = list;
|
||||||
foreach (DataGridViewColumn column in dgvDetalle.Columns)
|
foreach (DataGridViewColumn column in dgvDetalle.Columns)
|
||||||
{
|
{
|
||||||
column.Visible = column.Name == "Subtotal" || column.Name == "NombreProducto" || column.Name == "Cantidad";
|
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