estadisticas del mes
This commit is contained in:
+162
-1
@@ -1,8 +1,11 @@
|
||||
using System;
|
||||
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;
|
||||
@@ -12,9 +15,167 @@ 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();
|
||||
Dictionary<string,dynamic> estadisticas = new Dictionary<string, dynamic>
|
||||
{
|
||||
{ "mes", new Dictionary<string, dynamic>{
|
||||
{ "productos", productosMes },
|
||||
{ "productoMasVendido", prodctoMasVendidoMes(productosMes) },
|
||||
{ "mejorVendedor", mejorVenderdorMes() },
|
||||
} }
|
||||
};
|
||||
mostrarDatos(estadisticas);
|
||||
}
|
||||
|
||||
private void mostrarDatos(Dictionary<string, dynamic> estadisticas)
|
||||
{
|
||||
|
||||
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"]}";
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
detallesMes.Add(detallesVenta);
|
||||
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 prodctoMasVendidoMes(List<DetalleVentaModel> productosMes)
|
||||
{
|
||||
string productoMasVendido = "";
|
||||
int cantidadProducto = 0;
|
||||
Dictionary<string, int> productosNombres = new Dictionary<string,int>();
|
||||
|
||||
foreach(DetalleVentaModel producto in productosMes)
|
||||
{
|
||||
if(producto.cantidad > cantidadProducto)
|
||||
{
|
||||
productoMasVendido = producto.producto.nombre;
|
||||
cantidadProducto = producto.cantidad;
|
||||
|
||||
}
|
||||
}
|
||||
return productoMasVendido;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user