-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListadoFactura.cs
200 lines (172 loc) · 7.45 KB
/
ListadoFactura.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SmartPhone7
{
public partial class ListadoFactura : Form
{
public ListadoFactura()
{
InitializeComponent();
}
// Cadena de conexión a la base de datos
Conexion conexion1 = new Conexion();
// Objeto de conexión a la base de datos
public int id = 0;
private void MostrarFactura()
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "SELECT c.NombreCliente AS NombreCliente, f.NumeroOrden, f.FechaFactura, t.NombreTecnico AS NombreTecnico, f.Falla, f.ServicioOfrecido \r\nFROM Factura AS f\r\nINNER JOIN Clientes c ON f.IdCliente = c.Id \r\nINNER JOIN Tecnicos t ON f.IdTecnico = t.Id;";
DataTable dt = new DataTable();
try
{
conexion.Open();
SqlCommand comando = new SqlCommand(consulta, conexion);
SqlDataAdapter adaptador = new SqlDataAdapter(comando);
adaptador.Fill(dt);
adaptador.Dispose();
conexion.Close();
}
catch (Exception ex)
{
throw new Exception("Error al mostrar los usuarios: " + ex.Message);
}
DataGridViewFactura.DataSource = dt;
lblTotal.Text = DataGridViewFactura.RowCount.ToString();
}
}
private void BuscarFactura(string busqueda)
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "SELECT c.NombreCliente AS NombreCliente, f.NumeroOrden, f.FechaFactura, t.NombreTecnico AS NombreTecnico, f.Falla, f.ServicioOfrecido \r\nFROM Factura AS f\r\nINNER JOIN Clientes c ON f.IdCliente = c.Id \r\nINNER JOIN Tecnicos t ON f.IdTecnico = t.Id\r\nWhere c.NombreCliente LIKE @busqueda OR f.NumeroOrden LIKE @busqueda OR t.NombreTecnico LIKE @busqueda OR f.Falla LIKE @busqueda OR f.ServicioOfrecido LIKE @busqueda ";
DataTable dtUsuarios = new DataTable();
try
{
conexion.Open();
SqlCommand comando = new SqlCommand(consulta, conexion);
comando.Parameters.AddWithValue("@busqueda", "%" + busqueda + "%");
SqlDataAdapter adaptador = new SqlDataAdapter(comando);
adaptador.Fill(dtUsuarios);
adaptador.Dispose();
conexion.Close();
}
catch (Exception ex)
{
throw new Exception("Error al buscar: " + ex.Message);
}
DataGridViewFactura.DataSource = dtUsuarios;
lblTotal.Text = DataGridViewFactura.RowCount.ToString();
}
}
private void EliminarFactura(int id)
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "DELETE FROM Factura WHERE Id = @id";
try
{
conexion.Open();
SqlCommand comando = new SqlCommand(consulta, conexion);
comando.Parameters.AddWithValue("@id", id);
int filasAfectadas = comando.ExecuteNonQuery();
if (filasAfectadas > 0)
{
MessageBox.Show("Registro eliminado correctamente.");
}
else
{
MessageBox.Show("No se encontró ningún registro con el Id especificado.");
}
conexion.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error al eliminar el registro: " + ex.Message);
}
}
}
private void btnNuevo_Click(object sender, EventArgs e)
{
id = 0;
Factura formularioModal = new Factura();
Form formularioDeFondo = UtilidadesFormularios.CrearFormularioDeFondo(Principal.ActiveForm);
UtilidadesFormularios.CentrarFormularioModal(formularioModal, Principal.ActiveForm);
formularioModal.ShowDialog(formularioDeFondo);
formularioModal.Dispose();
formularioDeFondo.Dispose();
}
private void ListadoFactura_Load(object sender, EventArgs e)
{
MostrarFactura();
}
private void txtBuscar_TextChanged(object sender, EventArgs e)
{
BuscarFactura(txtBuscar.Text);
if (DataGridViewFactura.Rows.Count < 1)
{
panelSearch.Visible = true;
}
else
{
panelSearch.Visible = false;
}
}
private void btnPDF_Click(object sender, EventArgs e)
{
UtilidadesFormularios.ExportToPDF(DataGridViewFactura, "Listado de Facturas");
}
private void btnEditar_Click(object sender, EventArgs e)
{
if (id != 0)
{
//Factura.IdFactura = id;
//Factura.IdCliente = id;
//Factura.IdIdTecnico = id;
Clientes formularioModal = new Clientes();
Form formularioDeFondo = UtilidadesFormularios.CrearFormularioDeFondo(Principal.ActiveForm);
UtilidadesFormularios.CentrarFormularioModal(formularioModal, Principal.ActiveForm);
formularioModal.ShowDialog(formularioDeFondo);
formularioModal.Dispose();
formularioDeFondo.Dispose();
MostrarFactura();
}
else
{
DialogResult result = MessageBox.Show("Primero tienes que seleccionar la fila que deaseas eliminar para poder realizar la accion", "Error" + id.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
private void DataGridViewFactura_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = DataGridViewFactura.Rows[e.RowIndex];
id = Convert.ToInt16(row.Cells[0].Value);
}
}
private void btnEliminar_Click_1(object sender, EventArgs e)
{
if (DataGridViewFactura.SelectedRows.Count > 0)
{
DialogResult result = MessageBox.Show("Estas Seguro que quieres eliminar esta Factura, esto significara que no esta informacion no volvera a estar disponible", "Eliminar Factura" + id.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
if (result == DialogResult.OK)
{
EliminarFactura(id);
MostrarFactura();
}
}
else
{
DialogResult result = MessageBox.Show("Primero tienes que seleccionar la fila que deaseas eliminar para poder realizar la accion", "Error" + id.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
}
}