Files
compabetoOG/admin/addvariante.cs
T
2024-06-30 10:37:30 -06:00

101 lines
3.1 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.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace compabetov2.admin
{
public partial class addvariante : Form
{
private string imagePath = "";
private Producto producto;
Variantes ventanaPadre;
public addvariante(Producto producto, Variantes ventanaPadre)
{
InitializeComponent();
this.ventanaPadre = ventanaPadre;
this.producto = producto;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Close();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (isInt(txtCantidad.Text) && isDecimal(txtPrecio.Text) && isDecimal(txtPrecioCompra.Text)) {
Variante NuevaVariante = new Variante(
-1,
txtNombre.Text,
Decimal.Parse(txtPrecio.Text),
int.Parse(txtCantidad.Text),
Path.GetFileName(imagePath),
producto.idProducto,
Decimal.Parse(txtPrecioCompra.Text)
);
if (service.insertarVariante(NuevaVariante))
{
string destinationFolder = Path.Combine(Application.StartupPath, "Resources");
string destinationFile = Path.Combine(destinationFolder, Path.GetFileName(imagePath));
File.Copy(imagePath, destinationFile, true);
Close();
ventanaPadre.llenarLayout();
}
}
}
private static bool isInt(string cadena)
{
try
{
int.Parse(cadena);
return true;
}
catch
{
MessageBox.Show("Por favor introduzca un numero valido en volumen", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private static bool isDecimal(string cadena)
{
try
{
Decimal.Parse(cadena);
return true;
}
catch
{
MessageBox.Show("Por favor introduzca un precio valido", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Archivos de imagen (*.jpg, *.jpeg, *.png, *.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
openFileDialog1.Title = "Seleccionar imagen";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
imagePath = openFileDialog1.FileName;
lbImg.Text = $"Imagen: {Path.GetFileName(imagePath)}";
}
}
}
}