339 lines
12 KiB
C#
339 lines
12 KiB
C#
using compabetov2.database;
|
|
using compabetov2.database.modelos;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace compabetov2
|
|
{
|
|
public partial class Estadiscticas : Form
|
|
{
|
|
Dictionary<string, List<Dictionary<string, dynamic>>> resumen;
|
|
public Estadiscticas()
|
|
{
|
|
InitializeComponent();
|
|
darFormatoDTP();
|
|
}
|
|
|
|
private void darFormatoDTP()
|
|
{
|
|
dtpBuscar.Format = DateTimePickerFormat.Custom;
|
|
dtpBuscar.CustomFormat = "dd/MM/yyyy";
|
|
}
|
|
|
|
private void btnBuscar_Click(object sender, EventArgs e)
|
|
{
|
|
string fechaBuscaStr = dtpBuscar.Text;
|
|
|
|
DateTime.TryParseExact(fechaBuscaStr, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime fechaBuscar);
|
|
resumen = service.estadisticas(fechaBuscar);
|
|
List<DetalleVentaModel> productosMes = estadisticasMes();
|
|
List<DetalleVentaModel> productosDia = estadisticasDia();
|
|
|
|
Dictionary<string,dynamic> estadisticas = new Dictionary<string, dynamic>
|
|
{
|
|
{ "mes", new Dictionary<string, dynamic>{
|
|
{ "productos", productosMes },
|
|
{ "productoMasVendido", prodctoMasVendido(productosMes) },
|
|
{ "mejorVendedor", mejorVenderdorMes() },
|
|
}
|
|
},
|
|
{ "dia", new Dictionary<string, dynamic>{
|
|
{ "productos", productosDia },
|
|
{ "productoMasVendido", prodctoMasVendido(productosDia) },
|
|
{ "mejorVendedor", mejorVenderdorDia() },
|
|
}
|
|
}
|
|
};
|
|
mostrarDatos(estadisticas);
|
|
}
|
|
|
|
private List<DetalleVentaModel> estadisticasDia()
|
|
{
|
|
//solo copie y pegue le metodo estadisticas dia y cambie el key en resumen de mes a dia
|
|
List<Dictionary<string, dynamic>> mes = resumen["dia"];
|
|
|
|
List<DetalleVentaModel> detallesMes = new List<DetalleVentaModel>();
|
|
foreach (Dictionary<string, dynamic> venta_y_detalles in mes)
|
|
{
|
|
foreach (DetalleVentaModel detallesVenta in venta_y_detalles["detalles"])
|
|
{
|
|
int totalElementos = detallesMes.Count;
|
|
int contador = 0;
|
|
for (int i = 0; i <= totalElementos; i++)
|
|
{
|
|
contador++;
|
|
if (i == totalElementos)
|
|
{
|
|
DetalleVentaModel detalleAgregar = new DetalleVentaModel(detallesVenta.cantidad, detallesVenta.producto, detallesVenta.idVenta);
|
|
detallesMes.Add(detalleAgregar);
|
|
break;
|
|
}
|
|
|
|
DetalleVentaModel producto = detallesMes[i];
|
|
|
|
if (producto.producto.nombre.Equals(detallesVenta.producto.nombre))
|
|
{
|
|
producto.cantidad += detallesVenta.cantidad;
|
|
break;
|
|
}
|
|
|
|
//si no existe el producto lo agrega
|
|
|
|
}
|
|
|
|
if (contador == 0)
|
|
{
|
|
detallesMes.Add(detallesVenta);
|
|
}
|
|
|
|
}
|
|
}
|
|
return detallesMes;
|
|
}
|
|
|
|
private void mostrarDatos(Dictionary<string, dynamic> estadisticas)
|
|
{
|
|
dgvDia.Rows.Clear();
|
|
dgvMes.Rows.Clear();
|
|
List<DetalleVentaModel> detallesMes = estadisticas["mes"]["productos"];
|
|
decimal totalVendidoMes = 0;
|
|
foreach (DetalleVentaModel producto in detallesMes)
|
|
{
|
|
totalVendidoMes += producto.cantidad * producto.producto.precion;
|
|
// Crear una nueva fila
|
|
DataGridViewRow nuevaFila = new DataGridViewRow();
|
|
|
|
// Crear celdas para la nueva fila
|
|
DataGridViewCell productoN = new DataGridViewTextBoxCell();
|
|
DataGridViewCell precio = new DataGridViewTextBoxCell();
|
|
DataGridViewCell cantidad = new DataGridViewTextBoxCell();
|
|
DataGridViewCell total = new DataGridViewTextBoxCell();
|
|
|
|
// Establecer los valores de las celdas (puedes ajustar según tu necesidad)
|
|
productoN.Value = $"{producto.producto.nombre}";
|
|
precio.Value = $"{producto.producto.precion}";
|
|
cantidad.Value = $"{producto.cantidad}";
|
|
total.Value = $"{producto.cantidad * producto.producto.precion}";
|
|
|
|
// Agregar las celdas a la fila
|
|
nuevaFila.Cells.Add(productoN);
|
|
nuevaFila.Cells.Add(precio);
|
|
nuevaFila.Cells.Add(cantidad);
|
|
nuevaFila.Cells.Add(total);
|
|
|
|
// Agregar la nueva fila al DataGridView
|
|
dgvMes.Rows.Add(nuevaFila);
|
|
}
|
|
|
|
lbTotalMes.Text = $"Total de mes: ${totalVendidoMes}";
|
|
lbProductoMes.Text = $"Producto mas vendido: {estadisticas["mes"]["productoMasVendido"]}";
|
|
lbVendedorMes.Text = $"Mejor vendedor: {estadisticas["mes"]["mejorVendedor"]}";
|
|
|
|
//dia
|
|
List<DetalleVentaModel> detallesDia = estadisticas["dia"]["productos"];
|
|
decimal totalVendidoDia = 0;
|
|
foreach (DetalleVentaModel producto in detallesDia)
|
|
{
|
|
totalVendidoDia += producto.cantidad * producto.producto.precion;
|
|
// Crear una nueva fila
|
|
DataGridViewRow nuevaFila = new DataGridViewRow();
|
|
|
|
// Crear celdas para la nueva fila
|
|
DataGridViewCell productoN = new DataGridViewTextBoxCell();
|
|
DataGridViewCell precio = new DataGridViewTextBoxCell();
|
|
DataGridViewCell cantidad = new DataGridViewTextBoxCell();
|
|
DataGridViewCell total = new DataGridViewTextBoxCell();
|
|
|
|
// Establecer los valores de las celdas (puedes ajustar según tu necesidad)
|
|
productoN.Value = $"{producto.producto.nombre}";
|
|
precio.Value = $"{producto.producto.precion}";
|
|
cantidad.Value = $"{producto.cantidad}";
|
|
total.Value = $"{producto.cantidad * producto.producto.precion}";
|
|
|
|
// Agregar las celdas a la fila
|
|
nuevaFila.Cells.Add(productoN);
|
|
nuevaFila.Cells.Add(precio);
|
|
nuevaFila.Cells.Add(cantidad);
|
|
nuevaFila.Cells.Add(total);
|
|
|
|
// Agregar la nueva fila al DataGridView
|
|
dgvDia.Rows.Add(nuevaFila);
|
|
}
|
|
|
|
lbTotalDia.Text = $"Total de dia: ${totalVendidoDia}";
|
|
lbProductDia.Text = $"Producto mas vendido: {estadisticas["dia"]["productoMasVendido"]}";
|
|
lbVendedorDia.Text = $"Mejor vendedor: {estadisticas["dia"]["mejorVendedor"]}";
|
|
}
|
|
|
|
private List<DetalleVentaModel> estadisticasMes()
|
|
{
|
|
List<Dictionary<string, dynamic>> mes = resumen["mes"];
|
|
|
|
List <DetalleVentaModel> detallesMes = new List<DetalleVentaModel>();
|
|
foreach (Dictionary<string, dynamic> venta_y_detalles in mes)
|
|
{
|
|
foreach (DetalleVentaModel detallesVenta in venta_y_detalles["detalles"])
|
|
{
|
|
int totalElementos = detallesMes.Count;
|
|
int contador = 0;
|
|
for(int i = 0; i <= totalElementos; i++) {
|
|
contador++;
|
|
if (i == totalElementos)
|
|
{
|
|
DetalleVentaModel detalleAgregar = new DetalleVentaModel(detallesVenta.cantidad, detallesVenta.producto, detallesVenta.idVenta);
|
|
detallesMes.Add(detalleAgregar);
|
|
break;
|
|
}
|
|
|
|
DetalleVentaModel producto = detallesMes[i];
|
|
|
|
if (producto.producto.nombre.Equals(detallesVenta.producto.nombre))
|
|
{
|
|
producto.cantidad += detallesVenta.cantidad;
|
|
break;
|
|
}
|
|
|
|
//si no existe el producto lo agrega
|
|
|
|
}
|
|
|
|
if(contador == 0)
|
|
{
|
|
detallesMes.Add(detallesVenta);
|
|
}
|
|
|
|
}
|
|
}
|
|
return detallesMes;
|
|
}
|
|
|
|
private string mejorVenderdorMes()
|
|
{
|
|
List<Dictionary<string, dynamic>> mes = resumen["mes"];
|
|
string mejorVendedor = "";
|
|
int contadorNumeroVentas = 0;
|
|
|
|
Dictionary<string,int> empleados = new Dictionary<string,int>();
|
|
foreach(Dictionary<string, dynamic> venta_y_detalle in mes)
|
|
{
|
|
VentaModel venta = venta_y_detalle["venta"];
|
|
|
|
if (empleados.ContainsKey(venta.responzable))
|
|
{
|
|
empleados[venta.responzable] += 1;
|
|
}
|
|
else
|
|
{
|
|
empleados.Add(venta.responzable, 1);
|
|
}
|
|
}
|
|
|
|
foreach(string empleado in empleados.Keys)
|
|
{
|
|
if (empleados[empleado] > contadorNumeroVentas)
|
|
{
|
|
contadorNumeroVentas = empleados[empleado];
|
|
mejorVendedor = empleado;
|
|
}
|
|
}
|
|
|
|
return mejorVendedor;
|
|
}
|
|
|
|
|
|
private string prodctoMasVendido(List<DetalleVentaModel> productos)
|
|
{
|
|
string productoMasVendido = "";
|
|
int cantidadProducto = 0;
|
|
|
|
|
|
foreach(DetalleVentaModel producto in productos)
|
|
{
|
|
if(producto.cantidad > cantidadProducto)
|
|
{
|
|
productoMasVendido = producto.producto.nombre;
|
|
cantidadProducto = producto.cantidad;
|
|
|
|
}
|
|
}
|
|
return productoMasVendido;
|
|
}
|
|
|
|
private string mejorVenderdorDia()
|
|
{
|
|
List<Dictionary<string, dynamic>> mes = resumen["dia"];
|
|
string mejorVendedor = "";
|
|
int contadorNumeroVentas = 0;
|
|
|
|
Dictionary<string, int> empleados = new Dictionary<string, int>();
|
|
foreach (Dictionary<string, dynamic> venta_y_detalle in mes)
|
|
{
|
|
VentaModel venta = venta_y_detalle["venta"];
|
|
|
|
if (empleados.ContainsKey(venta.responzable))
|
|
{
|
|
empleados[venta.responzable] += 1;
|
|
}
|
|
else
|
|
{
|
|
empleados.Add(venta.responzable, 1);
|
|
}
|
|
}
|
|
|
|
foreach (string empleado in empleados.Keys)
|
|
{
|
|
if (empleados[empleado] > contadorNumeroVentas)
|
|
{
|
|
contadorNumeroVentas = empleados[empleado];
|
|
mejorVendedor = empleado;
|
|
}
|
|
}
|
|
|
|
return mejorVendedor;
|
|
}
|
|
|
|
private void label5_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void Estadiscticas_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void lbVendedorMes_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void lbProductoMes_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void lbVendedorDia_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void lbProductDia_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void pictureBox2_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
}
|
|
}
|