-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetalleFactura.cs
340 lines (286 loc) · 12.8 KB
/
DetalleFactura.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using BE;
using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.IO;
using System.Diagnostics;
using iText.IO.Image;
using Aspose.Pdf.Annotations;
using iText.Kernel.Pdf.Canvas;
namespace VisionTFI
{
public partial class DetalleFactura : Form
{
private BLLcliente _clienteBll = new BLLcliente();
private BLLarticulo _articulo = new BLLarticulo();
private BLLfactura _factura = new BLLfactura();
private BLLdetallefactura _detallefactura=new BLLdetallefactura();
public DetalleFactura()
{
InitializeComponent();
}
private void DetalleFactura_Load(object sender, EventArgs e)
{
RellenarDgvCliente();
RellenarDgvProducto();
}
public void RellenarDgvCliente()
{
dg_cliente.DataSource = _clienteBll.Listar();
dg_cliente.RowHeadersVisible = false;
dg_cliente.AllowUserToAddRows = false;
dg_cliente.AllowUserToDeleteRows = false;
dg_cliente.EditMode = DataGridViewEditMode.EditProgrammatically;
dg_cliente.MultiSelect = false;
dg_cliente.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dg_cliente.Columns[0].HeaderText = "Nombre";
dg_cliente.Columns[1].HeaderText = "Apellido";
dg_cliente.Columns[2].HeaderText = "Localidad";
dg_cliente.Columns[5].HeaderText = "Id cliente";
dg_cliente.Columns[3].Visible = false;
dg_cliente.Columns[4].Visible = false;
dg_cliente.Columns[6].Visible = false;
}
public void RellenarDgvProducto()
{
dg_articulo.DataSource = _articulo.Listar();
dg_articulo.RowHeadersVisible = false;
dg_articulo.AllowUserToAddRows = false;
dg_articulo.AllowUserToDeleteRows = false;
dg_articulo.EditMode = DataGridViewEditMode.EditProgrammatically;
dg_articulo.MultiSelect = false;
dg_articulo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dg_articulo.Columns[0].HeaderText = "Nombre";
dg_articulo.Columns[1].HeaderText = "Color";
dg_articulo.Columns[2].HeaderText = "Origen";
//dg_articulo.Columns[3].HeaderText = "Cantidad";
dg_articulo.Columns[4].HeaderText = "Id articulo";
dg_articulo.Columns[5].HeaderText = "Precio";
dg_articulo.Columns[3].Visible = false;
}
private void btn_cargarArticulo_Click(object sender, EventArgs e)
{
try
{
decimal a, b, subtotal;
decimal acumulador;
string aux = "0";
a = decimal.Parse(txt_cantidad.Text);
b = decimal.Parse(Convert.ToString(dg_articulo[5,dg_articulo.CurrentRow.Index].Value));
subtotal = a * b;
dg_detalleFac.Rows.Add(new string[]
{
Convert.ToString(dg_articulo[4,dg_articulo.CurrentRow.Index].Value), // articulo
Convert.ToString(dg_articulo[0,dg_articulo.CurrentRow.Index].Value),// nombre
Convert.ToString(txt_cantidad.Text), // aca paso la cantidad
Convert.ToString(dg_articulo[5,dg_articulo.CurrentRow.Index].Value), // precio
Convert.ToString(a*b),//subtotal
});
aux = lbl_total.Text;
acumulador= Convert.ToDecimal(aux)+subtotal;
lbl_total.Text= Convert.ToString(acumulador);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//private void btn_generarFactura_Click(object sender, EventArgs e)
//{
// try
// {
// BEfactura factura = new BEfactura
// {
// IdCliente = Convert.ToInt32(dg_cliente.CurrentRow.Cells[5].Value),
// Fecha = DateTime.Now,
// };
// List<BEdetallefactura> detalle = new List<BEdetallefactura>();
// BEdetallefactura detalleFactu = new BEdetallefactura();
// foreach (DataGridViewRow row in dg_detalleFac.Rows)
// {
// detalleFactu.IdArticulo = Convert.ToInt32(row.Cells[0].Value);
// detalleFactu.Cantidad = Convert.ToInt32(row.Cells[0].Value);
// detalle.Add(detalleFactu);
// }
// foreach (BEdetallefactura det in detalle)
// {
// factura.Detalles = detalle;
// }
// if (_factura.Add(factura))
// {
// MessageBox.Show("Factura exitosa");
// //GenerarFactura();
// GenerarPDFYMostrar();
// dg_detalleFac.DataSource = "";
// txt_cantidad.Clear();
// lbl_total.Text = "0";
// this.Close();
// }
// else
// {
// MessageBox.Show("Error al generar la factura");
// }
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
//}
private void btn_generarFactura_Click(object sender, EventArgs e)
{
try
{
BEfactura factura = new BEfactura
{
IdCliente = Convert.ToInt32(dg_cliente.CurrentRow.Cells[5].Value),
Fecha = DateTime.Now,
};
// Insertar factura y obtener el ID generado
int facturaId = _factura.AddPrueba(factura);
factura.Id = facturaId;
List<BEdetallefactura> detalle = new List<BEdetallefactura>();
foreach (DataGridViewRow row in dg_detalleFac.Rows)
{
BEdetallefactura detalleFactu = new BEdetallefactura
{
IdFactura = facturaId,
IdArticulo = Convert.ToInt32(row.Cells[0].Value),
Cantidad = Convert.ToInt32(row.Cells[2].Value) // Cambié esto para que tenga sentido
};
detalle.Add(detalleFactu);
}
bool allDetailsAdded = true;
foreach (BEdetallefactura det in detalle)
{
if (!_detallefactura.Add(det))
{
allDetailsAdded = false;
break;
}
}
if (allDetailsAdded)
{
MessageBox.Show("Factura exitosa");
// GenerarFactura();
GenerarPDFYMostrar();
dg_detalleFac.DataSource = null;
txt_cantidad.Clear();
lbl_total.Text = "0";
this.Close();
}
else
{
MessageBox.Show("Error al agregar detalles de la factura");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GenerarFactura(string rutaGuardado)
{
try
{
//Creo el documento pdf
PdfWriter pdfwriter = new PdfWriter(rutaGuardado);
PdfDocument pdf= new PdfDocument(pdfwriter);
Document documento= new Document(pdf);
//agrego el logo
string logoPath = @"C:\Users\Usuario\Desktop\Jair\Imagenes VisionManagement\V.png"; // Ruta al logo de la empresa
if (!File.Exists(logoPath))
{
throw new FileNotFoundException("El archivo no se encontro!");
}
ImageData imageData = ImageDataFactory.Create(logoPath);
iText.Layout.Element.Image logo = new iText.Layout.Element.Image(imageData).ScaleToFit(100, 100).SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.LEFT);
documento.Add(logo);
//obtengo los datos de la factura
string nombreEmpresa = "VisionManagement SRL";
DateTime fechaFactu = DateTime.Now;
string nombreCliente = dg_cliente.CurrentRow.Cells[0].Value.ToString();
string apellidoCliente = dg_cliente.CurrentRow.Cells[1].Value.ToString();
//pongo los encabezados
Table encabezado1 = new Table(1).SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.TOP).SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER);
Cell tipoFactura = new Cell().Add(new Paragraph("Factura \nB")).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).SetBold().SetFontSize(15);
encabezado1.AddCell(tipoFactura);
documento.Add(encabezado1);
documento.Add(new Paragraph("\n"));
// encabezado info de la empresa y el cliente
Table encabezado = new Table(1).UseAllAvailableWidth();
Cell infoEmpresa = new Cell().
Add(new Paragraph(nombreEmpresa.ToUpper()).SetBold().SetFontSize(20))
.Add(new Paragraph("Fecha :" + fechaFactu.ToString("dd/MM/yyyy")).SetFontSize(10))
.Add(new Paragraph("Cliente :" + nombreCliente.ToUpper() + " " + apellidoCliente.ToUpper()).SetFontSize(10))
.SetTextAlignment(iText.Layout.Properties.TextAlignment.LEFT);
encabezado.AddCell(infoEmpresa);
documento.Add(encabezado);
//agrego el detalle de la factura
documento.Add(new Paragraph("\nDetalle"));
Table table = new Table(4).UseAllAvailableWidth();
table.AddCell("Producto");
table.AddCell("Cantidad");
table.AddCell("Precio");
table.AddCell("Subtotal");
decimal total = 0;
foreach(DataGridViewRow row in dg_detalleFac.Rows)
{
string nombreProducto= row.Cells[1].Value.ToString();//nombre del producto
int cantidad= Convert.ToInt32(row.Cells[2].Value);//cantidad
decimal precio= Convert.ToDecimal(row.Cells[3].Value);//precio
decimal subTotal = Convert.ToDecimal(row.Cells[4].Value);// subtotal
table.AddCell(nombreProducto);
table.AddCell(cantidad.ToString());
table.AddCell(precio.ToString());
table.AddCell(subTotal.ToString());
total += subTotal;
}
documento.Add(table);
documento.Add(new Paragraph("Total :"+ total.ToString()).SetBold().SetFontSize(20));
documento.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error al generar el pdf :"+ ex.Message);
}
}
private void GenerarPDFYMostrar()
{
try
{
// Generar el PDF
string directorioFacturas = @"C:\Users\Usuario\Desktop\FacturasVM";
string nombreCliente = dg_cliente.CurrentRow.Cells[0].Value.ToString();
DateTime fecha = DateTime.Now;
string nombreArchivo = $"Factura_{nombreCliente}_{fecha:yyyyMMdd_HHmmss}.pdf";
// string rutaArchivoPDF = Path.Combine(directorioFacturas, "Factura_" +nombreCliente+".pdf");
string rutaArchivoPDF = Path.Combine(directorioFacturas, nombreArchivo);
GenerarFactura(rutaArchivoPDF);
// Verificar si el archivo existe
if (File.Exists(rutaArchivoPDF))
{
// Abrir el archivo PDF con la aplicación predeterminada del sistema
Process.Start(rutaArchivoPDF);
}
else
{
MessageBox.Show("El archivo PDF no se ha generado correctamente.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error al generar o mostrar el PDF: " + ex.Message);
}
}
}
}