Files
compabetoOG/vender.cs
T
2024-01-17 23:03:46 -06:00

538 lines
20 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.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
namespace compabetov2
{
public partial class vender : Form
{
private bool masBtnPresionado = false;
private bool menosBtnPresionado = false;
private List<Producto> productos;
private List<TableLayoutPanel> cartas = new List<TableLayoutPanel>();
private decimal total = 0;
private LoginModel loginPrincipal;
public vender(LoginModel loginPrincipal)
{
InitializeComponent();
getData();
llenarLayout();
this.loginPrincipal = loginPrincipal;
}
private void llenarLayout()
{
foreach (Producto producto in productos)
{
TableLayoutPanel carta = new TableLayoutPanel();
carta.ColumnCount = 1;
carta.RowCount = 2;
carta.RowStyles.Add(new RowStyle(SizeType.Percent, 80F));
carta.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
carta.Width = 220;
carta.Height = 300;
PictureBox pic = new PictureBox();
pic.Dock = DockStyle.Fill;
pic.SizeMode = PictureBoxSizeMode.Zoom;
string rutaImg = Path.Combine(Application.StartupPath, "Resources", producto.img);
pic.Image = Image.FromFile(rutaImg);//hay error aquí
pic.BackgroundImageLayout = ImageLayout.Stretch;
pic.Tag = producto.idProducto;
Label id = new Label();
id.Text = producto.idProducto.ToString();
id.BackColor = Color.FromArgb(82, 82, 82);
id.ForeColor = Color.White;
id.Width = 20;
id.Font = new Font(id.Font, FontStyle.Bold);
id.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
pic.Controls.Add(id);
Label precio = new Label();
precio.Text = "$" + producto.precion.ToString() + " - " + producto.nombre;
precio.BackColor = Color.FromArgb(82, 82, 82);
precio.ForeColor = Color.White;
precio.Width = 40;
precio.Font = new Font(id.Font, FontStyle.Bold);
precio.Dock = DockStyle.Bottom;
pic.Controls.Add(precio);
TableLayoutPanel HUD = new TableLayoutPanel();
HUD.ColumnCount = 3;
HUD.RowCount = 1;
HUD.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
HUD.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
HUD.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
Label contador = new Label();
contador.Text = "0";
contador.Dock = DockStyle.Fill;
contador.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
contador.Font = new Font(contador.Font.FontFamily, 25);
Button btnMenos = new Button();
btnMenos.Text = "-";
btnMenos.Width = 25;
btnMenos.Height = 25;
btnMenos.Font = new Font(btnMenos.Font.FontFamily, 30);
btnMenos.ForeColor = Color.White;
btnMenos.Dock = DockStyle.Fill;
btnMenos.FlatStyle = FlatStyle.Flat;
btnMenos.FlatAppearance.BorderSize = 0;
btnMenos.BackColor = Color.FromArgb(33, 90, 255);
btnMenos.Click += new EventHandler(delegate (object sender, EventArgs e)
{
int numeroContador = int.Parse(contador.Text.ToString());
// Validar que el contador no sea 0 antes de restar
if (numeroContador > 0)
{
contador.Text = (numeroContador - 1).ToString();
total -= producto.precion;
label1.Text = $"Total: ${total}";
// Obtener información del Label id y precio
int idProducto = int.Parse(id.Text);
decimal precioProducto = producto.precion;
// Restar al ticket existente si ya se agregó
RestarDelTicket(idProducto, producto.nombre, precioProducto, 1);
}
else
{
// Mostrar mensaje de advertencia
MessageBox.Show("No puedes vender menos de 0 cosas", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
});
Button btnMas = new Button();
btnMas.Text = "+";
btnMas.Width = 25;
btnMas.Height = 25;
btnMas.Dock = DockStyle.Fill;
btnMas.Font = new Font(btnMas.Font.FontFamily, 30);
btnMas.ForeColor = Color.White;
btnMas.BackColor = Color.FromArgb(33, 90, 255);
btnMas.FlatStyle = FlatStyle.Flat;
btnMas.FlatAppearance.BorderSize = 0;
btnMas.Click += new EventHandler(delegate (object sender, EventArgs e)
{
int numeroContador = int.Parse(contador.Text.ToString());
contador.Text = (numeroContador + 1).ToString();
total += producto.precion;
label1.Text = $"Total: ${total}";
// Obtener información del Label id y precio
int idProducto = int.Parse(id.Text);
decimal precioProducto = producto.precion;
// Generar ticket y agregarlo al flowLayoutPanel2
GenerarTicket(idProducto, producto.nombre, producto.precion, int.Parse(contador.Text), total, rutaImg);
});
HUD.Controls.Add(btnMenos);
HUD.Controls.Add(contador);
HUD.Controls.Add(btnMas);
carta.Controls.Add(pic, 0, 0);
carta.Controls.Add(HUD, 0, 1);
cartas.Add(carta);
flowLayoutPanel3.Controls.Add(carta);
}
}
private void RestarDelTicket(int idProducto, string nombreProducto, decimal precio, int cantidad)
{
foreach (var item in flowLayoutPanel2.Controls)
{
TableLayoutPanel panelProducto = item as TableLayoutPanel;
if (panelProducto != null && panelProducto.Controls.Count > 1)
{
TableLayoutPanel panelInfo = panelProducto.Controls[1] as TableLayoutPanel;
if (panelInfo != null && panelInfo.Controls.Count > 2)
{
Label lbnombre = panelInfo.Controls[0] as Label;
if (lbnombre != null)
{
string textoLb = lbnombre.Text;
string[] cadenas = textoLb.Split(':');
string cadena = cadenas[1].Remove(0, 1);
if (cadena.Equals(nombreProducto))
{
Label Lbcantidad = panelInfo.Controls[2] as Label;
Label Lbtotal = panelInfo.Controls[3] as Label;
if (Lbcantidad != null && Lbtotal != null)
{
string textcantidad = Lbcantidad.Text;
string[] cantidadCadenas = textcantidad.Split(' ');
int NuevaCantiad = int.Parse(cantidadCadenas[1]) - cantidad;
int NuevoTotal = (int)(NuevaCantiad * precio);
// Actualizar el ticket existente
Lbcantidad.Text = $"Cantidad: {NuevaCantiad}";
Lbtotal.Text = $"Total: ${NuevoTotal}";
// Si la cantidad llega a 0, eliminar el ticket del flowLayoutPanel2
if (NuevaCantiad == 0)
{
flowLayoutPanel2.Controls.Remove(panelProducto);
}
break;
}
}
}
}
}
}
}
private void GenerarTicket(int idProducto, string nombreProducto, decimal precio, int cantidad, decimal total, string rutaImg)
{
bool existeProducto = false;
int contador_saltar_2_vueltas = 1;
foreach (var item in flowLayoutPanel2.Controls)
{
if (contador_saltar_2_vueltas <= 3)
{
contador_saltar_2_vueltas++;
continue;
}
TableLayoutPanel panelProducto = item as TableLayoutPanel;
TableLayoutPanel panelInfo = panelProducto.Controls[1] as TableLayoutPanel;
Label lbnombre = panelInfo.Controls[0] as Label;
string textoLb = lbnombre.Text;
string[] cadenas = textoLb.Split(':');
string cadena = cadenas[1].Remove(0, 1);
if (cadena.Equals(nombreProducto))
{
existeProducto = true;
Label Lbcantidad = panelInfo.Controls[2] as Label;
Label Lbtotal = panelInfo.Controls[3] as Label;
string textcantidad = Lbcantidad.Text;
string[] cantidadCadenas = textcantidad.Split(' ');
int NuevaCantiad = int.Parse(cantidadCadenas[1]) + 1;
int NuevoTotal = (int)(NuevaCantiad * precio);
Lbcantidad.Text = $"Cantidad: {NuevaCantiad}";
Lbtotal.Text = $"Total: ${NuevoTotal}";
}
}
if (!existeProducto)
{
TableLayoutPanel ticketProducto = new TableLayoutPanel();
ticketProducto.RowCount = 1;
ticketProducto.ColumnCount = 2;
ticketProducto.Height = 100;
ticketProducto.Width = 250;
ticketProducto.BackColor = Color.FromArgb(33, 90, 255);
PictureBox pic = new PictureBox();
pic.Dock = DockStyle.Fill;
pic.SizeMode = PictureBoxSizeMode.Zoom;
pic.Image = Image.FromFile(rutaImg);//hay error aquí
pic.BackgroundImageLayout = ImageLayout.Stretch;
Label id = new Label();
id.Text = idProducto.ToString();
id.BackColor = Color.FromArgb(82, 82, 82);
id.ForeColor = Color.White;
id.Width = 20;
id.Font = new Font(id.Font, FontStyle.Bold);
id.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
pic.Controls.Add(id);
TableLayoutPanel infoTicket = new TableLayoutPanel();
infoTicket.ColumnCount = 1;
infoTicket.RowCount = 4;
infoTicket.Dock = DockStyle.Fill;
Label lbNombre = new Label();
lbNombre.Dock = DockStyle.Fill;
lbNombre.Text = $"producto: {nombreProducto}";
lbNombre.ForeColor = Color.White;
Label lbPrecio = new Label();
lbPrecio.Dock = DockStyle.Fill;
lbPrecio.Text = $"Precio: ${precio}";
lbPrecio.ForeColor = Color.White;
Label lbCantidad = new Label();
lbCantidad.Dock = DockStyle.Fill;
lbCantidad.Text = $"Cantidad: {cantidad}";
lbCantidad.ForeColor = Color.White;
Label lbTotal = new Label();
lbTotal.Dock = DockStyle.Fill;
lbTotal.Text = $"Total: ${cantidad * precio}";
lbTotal.ForeColor = Color.White;
infoTicket.Controls.Add(lbNombre, 0, 0);
infoTicket.Controls.Add(lbPrecio, 0, 1);
infoTicket.Controls.Add(lbCantidad, 0, 2);
infoTicket.Controls.Add(lbTotal, 0, 3);
ticketProducto.Controls.Add(pic, 0, 0);
ticketProducto.Controls.Add(infoTicket, 1, 0);
// Agregar el ticket al flowLayoutPanel2
flowLayoutPanel2.Controls.Add(ticketProducto);
}
}
private void getData()
{
this.productos = service.conseguirProductos();
}
private void vender_Load(object sender, EventArgs e)
{
}
private void lblhora_Click(object sender, EventArgs e)
{
}
private void lblfecha_Click(object sender, EventArgs e)
{
}
private void horafecha_Tick(object sender, EventArgs e)
{
lblhora.Text = DateTime.Now.ToString("h:mm:ss");
lblfecha.Text = DateTime.Now.ToLongDateString();
}
private void cervezasgb_Enter(object sender, EventArgs e)
{
}
private void flowLayoutPanel3_Paint(object sender, PaintEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MenuPrincipal menuPrincipal = new MenuPrincipal(loginPrincipal);
menuPrincipal.Show();
this.Dispose(true);
}
private void flowLayoutPanel2_Paint(object sender, PaintEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void bntImprimir_Click(object sender, EventArgs e)
{
ImprimirTicket();
}
private void ImprimirTicket()
{
using (PrintDocument documentoImpresion = new PrintDocument())
{
documentoImpresion.PrintPage += (sender, e) => ImprimirContenido(e);
using (PrintDialog printDialog = new PrintDialog())
{
printDialog.Document = documentoImpresion;
if (printDialog.ShowDialog() == DialogResult.OK)
{
documentoImpresion.Print();
}
}
}
}
private void ImprimirContenido(PrintPageEventArgs e)
{
// Contenido que deseas imprimir
string contenido = " Mini Súper\n" +
" Compa BETO\n" +
" Saturnino Peña #44\n" +
" Lomas de la Laguna\n" +
" 311 169 2829\n" +
$" Fecha: {DateTime.Now.ToString("dd/MM/yyyy")}\n" +
$" Hora: {DateTime.Now.ToString("hh:mm:ss")}\n\n" +
"Producto | Precio | Cantidad\n" +
"------------------------------------\n";
contenido += crearContenidoImprimir();
contenido += $"------------------------------------\n" +
$" Total: ${total}\n" +
"------------------------------------\n" +
" Mini Súper\n" +
" Compa BETO\n" +
" ¡Gracias por su compra!";
// Configuración de la fuente y del área de impresión
Font fuente = new Font("Arial", 10); // Ajustar el tamaño de fuente según sea necesario
PointF posicion = new PointF(0, e.MarginBounds.Top);
// Imprimir el contenido
e.Graphics.DrawString(contenido, fuente, Brushes.Black, posicion);
}
private string crearContenidoImprimir()
{
int contador_saltar_2_vueltas = 1;
string resultado = "";
foreach (var item in flowLayoutPanel2.Controls)
{
if (contador_saltar_2_vueltas <= 3)
{
contador_saltar_2_vueltas++;
continue;
}
TableLayoutPanel panelProducto = item as TableLayoutPanel;
TableLayoutPanel panelInfo = panelProducto.Controls[1] as TableLayoutPanel;
Label lbnombre = panelInfo.Controls[0] as Label;
Label lbPrecio = panelInfo.Controls[1] as Label;
Label Lbcantidad = panelInfo.Controls[2] as Label;
Label Lbtotal = panelInfo.Controls[3] as Label;
//nombre producto
string textoLbnombre = lbnombre.Text;
string[] cadenas = textoLbnombre.Split(':');
string productoNombre = cadenas[1].Remove(0, 1);
//precio
string textoLbPrecio = lbPrecio.Text;
cadenas = textoLbPrecio.Split('$');
string precioProducto = cadenas[1];
//cantidad
string textoLbCantidad = Lbcantidad.Text;
cadenas = textoLbCantidad.Split(':');
string cantidadProducto = cadenas[1].Remove(0, 1);
//total
string textoLbTotal = Lbtotal.Text;
cadenas = textoLbTotal.Split('$');
string totalProducto = cadenas[1];
resultado += $"{productoNombre} | ${precioProducto} | {cantidadProducto}\n";
}
return resultado;
}
private void button7_Click(object sender, EventArgs e)
{
List<DetalleVentaModel> detalleVentas = listaProductosVenta();
if(detalleVentas.Count > 0)
{
VentaModel ventaModel = new VentaModel(total);
if(service.crearVentaDAO(ventaModel, detalleVentas))
{
MessageBox.Show("Venta hecha");
}
}
else
{
MessageBox.Show("No hay ningun producto agregado");
}
}
private List<DetalleVentaModel> listaProductosVenta()
{
List<DetalleVentaModel> detalleVentas = new List<DetalleVentaModel>();
int contador_saltar_2_vueltas = 1;
foreach (var item in flowLayoutPanel2.Controls)
{
if (contador_saltar_2_vueltas <= 3)
{
contador_saltar_2_vueltas++;
continue;
}
TableLayoutPanel panelProducto = item as TableLayoutPanel;
TableLayoutPanel panelInfo = panelProducto.Controls[1] as TableLayoutPanel;
PictureBox pic = panelProducto.Controls[0] as PictureBox;
Label idLabel = null;
foreach (Control control in pic.Controls)
{
if (control is Label)
{
idLabel = (Label)control;
break;
}
}
int idProducto = int.Parse(idLabel.Text);
Producto producto = new Producto(idProducto);
Label Lbcantidad = panelInfo.Controls[2] as Label;
string textcantidad = Lbcantidad.Text;
string[] cantidadCadenas = textcantidad.Split(' ');
int cantidad = int.Parse(cantidadCadenas[1]);
DetalleVentaModel detalleVenta = new DetalleVentaModel(cantidad, producto);
detalleVentas.Add(detalleVenta);
}
return detalleVentas;
}
}
}