-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompra.java
More file actions
397 lines (378 loc) · 16.9 KB
/
Copy pathCompra.java
File metadata and controls
397 lines (378 loc) · 16.9 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ModelClass;
import Graphics.RenderCelda;
import Connection.Consult;
import Models.*;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
/**
*
* @author AlexJPZ
*/
public class Compra extends Consult {
private String sql,codeBarra,codigoCompra;
private Object[] obect;
private DefaultTableModel modelo1, modelo2, modelo3;
private double deuda = 0, enCaja, pago, deudas;
private boolean deudaProveedor, verificar;
public void guardarTempoCompra(String des, int cant, String precio) {
double precio1 = Double.valueOf(precio);
numTempoCompras = tempoCompras().stream()
.filter(t -> t.getDescripcion().equals(des) && t.getPrecioCompra().equals("$" + formato.decimal(precio1)))
.collect(Collectors.toList());
if (0 < numTempoCompras.size()) {
int cant1;
double importe2, importe3;
importe2 = precio1 * cant;
importe3 = formato.reconstruir(numTempoCompras.get(0).getImporte().replace("$", ""));
importe2 = importe2 + importe3;
cant1 = cant + numTempoCompras.get(0).getCantidad();
sql = "UPDATE tempo_compras SET Descripcion = ?,Cantidad = ?,PrecioCompra = ?,"
+ "Importe = ? WHERE IdCompra =" + numTempoCompras.get(0).getIdCompra();
obect = new Object[]{des, cant1, "$" + formato.decimal(precio1), "$" + formato.decimal(importe2)};
update(sql, obect);
} else {
double importe;
importe = precio1 * cant;
sql = "INSERT INTO tempo_compras(Descripcion,Cantidad,PrecioCompra,Importe)"
+ "VALUES(?,?,?,?)";
obect = new Object[]{des, cant, "$" + formato.decimal(precio1), "$" + formato.decimal(importe)};
insert(sql, obect);
}
}
public void searchCompras(JTable table, String campo, int num_registro, int reg_por_pagina) {
String[] registros = new String[5];
String[] titulos = {"Id", "Descripcion", "Cantidad", "Precio Compra", "Importe"};
modelo1 = new DefaultTableModel(null, titulos);
if (campo.equals("")) {
numTempoCompras = tempoCompras().stream()
.skip(num_registro).limit(reg_por_pagina)
.collect(Collectors.toList());
} else {
numTempoCompras = tempoCompras().stream()
.filter(C -> C.getDescripcion().startsWith(campo))
.skip(num_registro).limit(reg_por_pagina)
.collect(Collectors.toList());
}
numTempoCompras.forEach(item -> {
registros[0] = String.valueOf(item.getIdCompra());
registros[1] = item.getDescripcion();
registros[2] = String.valueOf(item.getCantidad());
registros[3] = item.getPrecioCompra();
registros[4] = item.getImporte();
modelo1.addRow(registros);
});
table.setModel(modelo1);
table.setRowHeight(30);
table.getColumnModel().getColumn(0).setMaxWidth(0);
table.getColumnModel().getColumn(0).setMinWidth(0);
table.getColumnModel().getColumn(0).setPreferredWidth(0);
table.setDefaultRenderer(Object.class, new RenderCelda(4,0));
}
public List<Tempo_compras> getTempoCompras() {
return tempoCompras();
}
public DefaultTableModel getModelo() {
return modelo1;
}
public void importesTempo(JLabel label1, JLabel label2, JLabel label3) {
deuda = 0;
numTempoCompras = tempoCompras().stream()
.collect(Collectors.toList());
if (0 < numTempoCompras.size()) {
numTempoCompras.forEach(item -> {
double importes = formato.reconstruir(item.getImporte().replace("$", ""));
deuda += importes;
});
label1.setText("$" + formato.decimal(deuda));
label2.setText("$" + formato.decimal(deuda));
label3.setText("$" + formato.decimal(deuda));
} else {
label1.setText("$0.00");
label2.setText("$0.00");
label3.setText("$0.00");
}
}
public void updateTempoCompra(int id, String des, int cant, String precio) {
double precio1, importe;
precio1 = formato.reconstruir(precio.replace("$", ""));
precio1 = Double.valueOf(precio);
importe = precio1 * cant;
sql = "UPDATE tempo_compras SET Descripcion = ?,Cantidad = ?,PrecioCompra = ?,"
+ "Importe = ? WHERE IdCompra =" + id;
obect = new Object[]{des, cant, "$" + formato.decimal(precio1), "$" + formato.decimal(importe)};
update(sql, obect);
}
public void searchProveedores(JTable table, String campo) {
String[] registros = new String[5];
String[] titulos = {"Id", "Proveedor", "Email", "Telefono", "Saldo"};
modelo2 = new DefaultTableModel(null, titulos);
if (campo.equals("")) {
reportes_proveedores = new ArrayList<>();
} else {
reportes_proveedores = reportesProveedores().stream()
.filter(P -> P.getProveedor().startsWith(campo) || P.getEmail().startsWith(campo)
|| P.getTelefono().startsWith(campo))
.collect(Collectors.toList());
}
reportes_proveedores.forEach(item -> {
registros[0] = String.valueOf(item.getIdProveedor());
registros[1] = item.getProveedor();
registros[2] = item.getEmail();
registros[3] = item.getTelefono();
registros[4] = item.getSaldoActual();
modelo2.addRow(registros);
});
table.setModel(modelo2);
table.setRowHeight(30);
table.getColumnModel().getColumn(0).setMaxWidth(0);
table.getColumnModel().getColumn(0).setMinWidth(0);
table.getColumnModel().getColumn(0).setPreferredWidth(0);
}
public DefaultTableModel getModelo2() {
return modelo2;
}
public void deleteCompras(int idCompra) {
if (0 < idCompra) {
sql = "DELETE FROM tempo_compras WHERE IdCompra LIKE ?";
delete(sql, idCompra);
} else {
sql = "DELETE FROM tempo_compras";
delete(sql, 0);
}
}
public void getIngresos(JLabel label) {
enCaja = 0;
cajasIngresos = cajasIngresos().stream()
.filter(c -> c.getFecha().equals(new Calendario().getFecha()))
.collect(Collectors.toList());
if (0 < cajasIngresos.size()) {
cajasIngresos.forEach(item -> {
String data = item.getIngreso().replace("$", "");
double importes = formato.reconstruir(data.replace(" ", ""));
enCaja += importes;
});
label.setText("$" + formato.decimal(enCaja));
} else {
label.setText("$0.00");
}
}
public boolean verificarPago(JTextField TextField, JLabel Label, JCheckBox CheckBox,
JLabel Label1, JLabel Label2, JLabel Label3, int idProveedor) {
verificar = false;
deudaProveedor = false;
if (!TextField.getText().equalsIgnoreCase("")) {
if (idProveedor != 0) {
pago = Double.valueOf(TextField.getText());
if (enCaja == 0) {
if (CheckBox.isSelected()) {
if (pago > deuda) {
Label.setText("Sea sobrepasado del pago");
Label.setForeground(Color.RED);
} else {
Label.setText("Se solicito un crédito al proveedor");
Label.setForeground(Color.RED);
deudaProveedor = true;
verificar = true;
pagos(pago, idProveedor, Label1, Label2, Label3);
}
} else {
if (pago > deuda) {
Label.setText("Sea sobrepasado del pago");
Label.setForeground(Color.RED);
} else {
Label.setText("No hay saldo en caja");
Label.setForeground(Color.RED);
pagos(pago, idProveedor, Label1, Label2, Label3);
}
}
} else {
if (pago > enCaja) {
if (CheckBox.isSelected()) {
if (pago > deuda) {
Label.setText("Sea sobrepasado del pago");
Label.setForeground(Color.RED);
} else {
Label.setText("Se genera una deuda del sistema al proveedor");
Label.setForeground(Color.RED);
deudaProveedor = true;
verificar = true;
pagos(pago, idProveedor, Label1, Label2, Label3);
}
} else {
if (pago > deuda) {
Label.setText("Sea sobrepasado del pago");
Label.setForeground(Color.RED);
} else {
Label.setText("No hay ingresos suficientes en caja");
Label.setForeground(Color.RED);
pagos(pago, idProveedor, Label1, Label2, Label3);
}
}
} else {
if (pago == deuda) {
if (CheckBox.isSelected()) {
if (pago > deuda) {
Label.setText("Sea sobrepasado del pago");
Label.setForeground(Color.RED);
} else {
Label.setText("Se genera una deuda del sistema al proveedor");
Label.setForeground(Color.RED);
deudaProveedor = true;
verificar = true;
pagos(pago, idProveedor, Label1, Label2, Label3);
}
} else {
Label.setText("Monto a pagar");
Label.setForeground(new Color(0, 153, 51));
verificar = true;
}
} else {
if (pago > deuda) {
Label.setText("Sea sobrepasado del pago");
Label.setForeground(Color.RED);
} else {
if (CheckBox.isSelected()) {
Label.setText("Se genera deuda del sistema al proveedor");
Label.setForeground(Color.RED);
deudaProveedor = true;
verificar = true;
pagos(pago, idProveedor, Label1, Label2, Label3);
} else {
Label.setText("Pago insuficiente");
Label.setForeground(Color.RED);
pagos(pago, idProveedor, Label1, Label2, Label3);
}
}
}
}
}
} else {
Label.setText("Seleccione un proveedor");
Label.setForeground(Color.RED);
TextField.setText("");
}
}
return verificar;
}
private void pagos(double pago, int idProveedor, JLabel Label1, JLabel Label2, JLabel Label3) {
double saldo;
if (enCaja == 0) {
deudas = deuda;
} else if (deuda > enCaja) {
deudas = deuda - enCaja;
} else if (deuda == pago) {
deudas = deuda - pago;
} else {
deudas = deuda - pago;
}
String data = "$" + formato.decimal(deudas);
Label1.setText(data);
Label2.setText(data);
saldo = formato.reconstruir(getReporte(idProveedor).get(0)
.getSaldoActual().replace("$", ""));
saldo += deudas;
Label3.setText("$" + formato.decimal(saldo));
}
public List<Reportes_proveedores> getReporte(int idProveedor) {
reportes_proveedores = reportesProveedor(idProveedor).stream()
.filter(p -> p.getIdProveedor() == idProveedor)
.collect(Collectors.toList());
return reportes_proveedores;
}
public void saveCompras(String proveedor, int idProveedor, String usuario, int idUsuario,
String role) {
int count1, count2, ini, idRegistro;
sql = "INSERT INTO compras(Producto,Cantidad,Precio,Importe,IdProveedor,Proveedor,IdUsuario"
+ ",Usuario,Role,Dia,Mes,Year,Fecha,Codigo)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
numTempoCompras = tempoCompras().stream()
.collect(Collectors.toList());
if (0 < numTempoCompras.size()) {
numTempoCompras.forEach(item -> {
codigoCompra = codeCompra(item.getDescripcion(),item.getPrecioCompra());
obect = new Object[]{
item.getDescripcion(),
item.getCantidad(),
item.getPrecioCompra(),
item.getImporte(),
idProveedor,
proveedor,
idUsuario,
usuario,
role,
new Calendario().getDia(),
new Calendario().getMes(),
new Calendario().getYear(),
new Calendario().getFecha(),
codigoCompra
};
insert(sql, obect);
});
count1 = numTempoCompras.size();
if (deudaProveedor) {
reportes_proveedores = getReporte(idProveedor);
idRegistro = reportes_proveedores.get(0).getIdRegistro();
sql = "UPDATE reportes_proveedores SET IdProveedor = ?,SaldoActual = ?,FechaActual = ?,"
+ "UltimoPago = ?,FechaPago = ? WHERE IdRegistro =" + idRegistro;
double saldo = formato.reconstruir(reportes_proveedores.get(0)
.getSaldoActual().replace("$", ""));
String ultimoPago = reportes_proveedores.get(0).getUltimoPago();
String fechaPago = reportes_proveedores.get(0).getFechaPago();
saldo += deudas;
String dataSaldo = "$" + formato.decimal(saldo);
Object[] reporte = new Object[]{
idProveedor,
dataSaldo,
new Calendario().getFecha(),
ultimoPago,
fechaPago
};
update(sql, reporte);
}
count2 = compras().size();
if (count1 == count2) {
ini = 0;
} else {
ini = count2 - count1;
}
for (int i = ini; i < count2; i++) {
sql = "INSERT INTO tempo_productos(IdCompra)VALUES(?)";
obect = new Object[]{compras().get(i).getIdCompra()};
insert(sql, obect);
}
}
}
public String codeCompra(String producto, String precio){
int codigo, count;
compras = compras().stream()
.filter(p -> p.getProducto().equals(producto)&& p.getPrecio().equals(precio)
&& p.getYear().equals(new Calendario().getYear()))
.collect(Collectors.toList());
if (0 < compras.size()) {
codeBarra = compras.get(0).getCodigo();
}else{
do{
codigo = ThreadLocalRandom.current().nextInt(100000, 1000000000 + 1);
codeBarra = String.valueOf(codigo);
compras = compras().stream()
.filter(p -> p.getCodigo().equals(codeBarra)
&& p.getYear().equals(new Calendario().getYear()))
.collect(Collectors.toList());
count = compras.size();
}while (count > 0);
}
return codeBarra;
}
}