152 lines
5.0 KiB
C#
152 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 Cliente clienteSeleccionado;
|
|
Factura factura;
|
|
public FrmFactura(Factura? factura = null)
|
|
{
|
|
InitializeComponent();
|
|
CargarClientes();
|
|
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
|
|
// Para el primer control NumericUpDown
|
|
numericUpDown1.Maximum = int.MaxValue; // Esto permitirá IDs muy grandes
|
|
|
|
// Para el segundo control NumericUpDown
|
|
numericUpDown2.Maximum = decimal.MaxValue; // Esto permitirá totales muy grandes
|
|
|
|
|
|
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
|
|
comboBox1.DataSource = clientes;
|
|
|
|
// Establecer la propiedad para mostrar el nombre del cliente en el ComboBox
|
|
comboBox1.DisplayMember = "NombreCompleto";
|
|
}
|
|
|
|
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
clienteSeleccionado = (Cliente)comboBox1.SelectedItem;
|
|
}
|
|
|
|
|
|
|
|
private void CargarDatos()
|
|
{
|
|
numericUpDown1.Value = factura.Id;
|
|
numericUpDown2.Value = (decimal)factura.Total;
|
|
dateTimePicker1.Value = factura.Fecha;
|
|
|
|
// Asignar el cliente seleccionado en el ComboBox
|
|
if (factura.Cliente != null)
|
|
{
|
|
comboBox1.SelectedItem = factura.Cliente;
|
|
}
|
|
}
|
|
|
|
|
|
private bool ValidarDatos()
|
|
{
|
|
string devolucion = "";
|
|
|
|
if (string.IsNullOrEmpty(numericUpDown1.Text)) devolucion += "El ID no puede ser nulo o vacío\n";
|
|
if (numericUpDown2.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)numericUpDown1.Value,
|
|
Total = (double)numericUpDown2.Value,
|
|
Fecha = dateTimePicker1.Value,
|
|
Cliente = (Cliente)comboBox1.SelectedItem,
|
|
};
|
|
// Agregar la factura a la colección
|
|
ControladoraFacturas.Instance.Añadir(factura);
|
|
}
|
|
else
|
|
{
|
|
// Actualizar los datos de la factura existente
|
|
factura.Id = (int)numericUpDown1.Value;
|
|
factura.Total = (double)numericUpDown2.Value;
|
|
factura.Fecha = dateTimePicker1.Value;
|
|
factura.Cliente = (Cliente)comboBox1.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();
|
|
}
|
|
|
|
|
|
}
|
|
}
|