Files
Final_Das/Vista/FrmFactura.cs

158 lines
5.0 KiB
C#

using Controladora;
using Entidades;
using Modelo;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Vista
{
public partial class FrmFactura : Form
{
private Factura factura;
private Cliente clienteSeleccionado;
public FrmFactura(Factura? factura = null)
{
InitializeComponent();
CargarClientes();
cmbCliente.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
// Para el primer control NumericUpDown
numid.Maximum = int.MaxValue; // Esto permitirá IDs muy grandes
// Para el segundo control NumericUpDown
numtotal.Maximum = decimal.MaxValue; // Esto permitirá totales muy grandes
cmbCliente.DisplayMember = "Cliente";
cmbCliente.SelectedIndex = -1;
if (factura != null)
{
this.factura = factura;
this.Text = "Modificar Factura";
CargarDatos();
}
else
{
this.Text = "Agregar Factura";
}
}
private void CargarClientes()
{
// Obtener la lista de clientes desde el repositorio
ReadOnlyCollection<Cliente> clientes = RepositorioClientes.Instance.Listar();
// Asignar la lista de clientes como origen de datos para el ComboBox
cmbCliente.DataSource = clientes;
// Establecer la propiedad para mostrar el nombre del cliente en el ComboBox
cmbCliente.DisplayMember = "NombreCompleto";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
clienteSeleccionado = (Cliente)cmbCliente.SelectedItem;
}
private void CargarDatos()
{
numid.Value = factura.Id;
numtotal.Value = (decimal)factura.Total;
datepick.Value = factura.Fecha;
// Asignar el cliente seleccionado en el ComboBox
if (factura.Cliente != null)
{
cmbCliente.SelectedItem = factura.Cliente;
}
}
private bool ValidarDatos()
{
string devolucion = "";
if (string.IsNullOrEmpty(numid.Text)) devolucion += "El ID no puede ser nulo o vacío\n";
if (numtotal.Value <= 0) devolucion += "El total debe ser mayor que cero\n";
if (clienteSeleccionado == null) devolucion += "Debe seleccionar un cliente\n";
if (devolucion == "")
{
return true;
}
else
{
MessageBox.Show(devolucion, "Errores de Validación", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (ValidarDatos())
{
try
{
if (factura == null)
{
// Crear una nueva factura con los datos proporcionados
factura = new Factura
{
Id = (int)numid.Value,
Total = (double)numtotal.Value,
Fecha = datepick.Value,
Cliente = (Cliente)cmbCliente.SelectedItem,
};
// Agregar la factura a la colección
ControladoraFacturas.Instance.Añadir(factura);
}
else
{
// Actualizar los datos de la factura existente
factura.Id = (int)numid.Value;
factura.Total = (double)numtotal.Value;
factura.Fecha = datepick.Value;
factura.Cliente = (Cliente)cmbCliente.SelectedItem;
// Modificar la factura en la colección
ControladoraFacturas.Instance.Modificar(factura);
}
MessageBox.Show("Operación realizada con éxito", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information);
Close();
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
// Captura cualquier otra excepción que pueda ocurrir
MessageBox.Show("Ocurrió un error inesperado: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}