401 lines
14 KiB
C#
401 lines
14 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.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 int cantidad = 0;
|
|
private bool masBtnPresionado = false;
|
|
private bool menosBtnPresionado = false;
|
|
private List<Producto> productos;
|
|
private List<TableLayoutPanel> cartas = new List<TableLayoutPanel>();
|
|
// private List<Producto>
|
|
private decimal total = 0;
|
|
private Label ticketLabel;
|
|
|
|
|
|
|
|
public vender()
|
|
{
|
|
InitializeComponent();
|
|
getData();
|
|
llenarLayout();
|
|
}
|
|
|
|
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;
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
// Obtener información del Label id y precio
|
|
int idProducto = int.Parse(id.Text);
|
|
decimal precioProducto = producto.precion;
|
|
if (decimal.TryParse(precio.Text, NumberStyles.Currency, CultureInfo.CurrentCulture, out precioProducto))
|
|
{
|
|
// Generar ticket y agregarlo al flowLayoutPanel2
|
|
//GenerarTicket(idProducto, producto.nombre, precioProducto, int.Parse(contador.Text), total);
|
|
}
|
|
else
|
|
{
|
|
// Manejar el caso en el que la conversión no sea exitosa
|
|
//MessageBox.Show("Formato de precio no válido.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
// 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;
|
|
|
|
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();
|
|
menuPrincipal.Show();
|
|
this.Dispose(true);
|
|
}
|
|
|
|
private void flowLayoutPanel2_Paint(object sender, PaintEventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void label1_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|