-
Notifications
You must be signed in to change notification settings - Fork 0
/
aceptarpedido.cpp
315 lines (281 loc) · 13.7 KB
/
aceptarpedido.cpp
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
#include "aceptarpedido.h"
#include "ui_aceptarpedido.h"
#include <QMessageBox>
#include <QInputDialog>
AceptarPedido::AceptarPedido(QString pedido, QString proveedor, QString nDoc, QString fechaPedido, QWidget *parent) :
QDialog(parent),
ui(new Ui::AceptarPedido)
{
ui->setupUi(this);
correcto = false;
idPedido = pedido;
ui->labelProveedor->setText(proveedor);
descuento=1;
idProveedor = base.idProveedor(proveedor, conf->getConexionLocal());
descuentoReal=0;
fecha = fechaPedido;
ui->lineEditNDoc->setText(nDoc);
ui->dateEditDocumento->setDate(QDate::fromString(fecha,"yyyy-MM-dd"));
llenarTabla(idPedido,descuento);
modeloPedido = new QSqlQueryModel(this);
modeloPedido->setQuery(QString("SELECT * FROM lineaspedido_tmp WHERE idPedido = %1").arg(idPedido),QSqlDatabase::database("DB"));
}
AceptarPedido::~AceptarPedido()
{
delete ui;
}
void AceptarPedido::llenarTabla(QString idPedido, double desc)
{
ui->leIva10->setText(QString::number(base.sumarIvasPedido(conf->getConexionLocal(), idPedido,"10")*desc));
ui->leIva21->setText(QString::number(base.sumarIvasPedido(conf->getConexionLocal(), idPedido,"21")*desc));
ui->leIva4->setText(QString::number(base.sumarIvasPedido(conf->getConexionLocal(), idPedido,"4")*desc));
ui->leIva5->setText(QString::number(base.sumarIvasPedido(conf->getConexionLocal(), idPedido,"5")*desc));
ui->leIva0->setText(QString::number(base.sumarIvasPedido(conf->getConexionLocal(), idPedido,"0")*desc));
ui->leRe0->setText(QString::number(base.sumarRePedido(conf->getConexionLocal(), idPedido,"0")*desc));
ui->leRe4->setText(QString::number(base.sumarRePedido(conf->getConexionLocal(), idPedido,"4")*desc));
ui->leRe5->setText(QString::number(base.sumarRePedido(conf->getConexionLocal(), idPedido,"5")*desc));
ui->leRe10->setText(QString::number(base.sumarRePedido(conf->getConexionLocal(), idPedido,"10")*desc));
ui->leRe21->setText(QString::number(base.sumarRePedido(conf->getConexionLocal(), idPedido,"21")*desc));
ui->leBase0->setText(QString::number(base.sumarBasesPedido(conf->getConexionLocal(), idPedido,"0")*desc));
ui->leBase4->setText(QString::number(base.sumarBasesPedido(conf->getConexionLocal(), idPedido,"4")*desc));
ui->leBase5->setText(QString::number(base.sumarBasesPedido(conf->getConexionLocal(), idPedido,"5")*desc));
ui->leBase10->setText(QString::number(base.sumarBasesPedido(conf->getConexionLocal(), idPedido,"10")*desc));
ui->leBase21->setText(QString::number(base.sumarBasesPedido(conf->getConexionLocal(), idPedido,"21")*desc));
ui->leTotal0->setText(QString::number(ui->leBase0->text().toDouble()));
ui->leTotal4->setText(QString::number(ui->leBase4->text().toDouble()+ui->leIva4->text().toDouble()+ui->leRe4->text().toDouble()));
ui->leTotal5->setText(QString::number(ui->leBase5->text().toDouble()+ui->leIva5->text().toDouble()+ui->leRe5->text().toDouble()));
ui->leTotal10->setText(QString::number(ui->leBase10->text().toDouble()+ui->leIva10->text().toDouble()+ui->leRe10->text().toDouble()));
ui->leTotal21->setText(QString::number(ui->leBase21->text().toDouble()+ui->leIva21->text().toDouble()+ui->leRe21->text().toDouble()));
ui->leTotalBase->setText(QString::number(ui->leBase0->text().toDouble()+ui->leBase4->text().toDouble()+ui->leBase5->text().toDouble()+ui->leBase10->text().toDouble()+ui->leBase21->text().toDouble()));
ui->leTotalIva->setText(QString::number(ui->leIva4->text().toDouble()+ui->leIva5->text().toDouble()+ui->leIva10->text().toDouble()+ui->leIva21->text().toDouble()));
ui->leTotalRe->setText(QString::number(ui->leRe4->text().toDouble()+ui->leRe5->text().toDouble()+ui->leRe10->text().toDouble()+ui->leRe21->text().toDouble()));
ui->leTotal->setText(QString::number(ui->leTotalBase->text().toDouble()+ui->leTotalIva->text().toDouble()+ui->leTotalRe->text().toDouble()));
}
bool AceptarPedido::procesarPedido(QSqlQueryModel *modelo)
{
QStringList datos;
QString idLinea , ean,descripcion, lote, fechaCaducidad, descuentoLinea , tipoIva , baseProducto , baseLinea , ivaLinea , reLinea;
int uds , unidades, bonificacion, tipoActualizacion = 0; //0-preguntar 1-pedido 2 almacen
double precioCosto, pvp ;
if (ui->comboBox->currentText() == "Seleccionar") {
QMessageBox msg;
msg.setText("Seleccione primero");
msg.setInformativeText("Antes de poder aceptar el pedido debe indicar si es un albarán o una factura");
msg.exec();
return false;
}
if (ui->lineEditNDoc->text().isEmpty()) {
QMessageBox msg;
msg.setText("Falta el número de documento");
msg.setInformativeText("Antes de poder aceptar el pedido debe indicar el número del documento al que pertenece");
msg.exec();
return false;
}
for (int i = 0; i < modelo->rowCount(); ++i) {
datos.clear();
idLinea = modelo->record(i).value("id").toString();
ean = modelo->record(i).value("cod").toString();
unidades = modelo->record(i).value("cantidad").toInt();
bonificacion = modelo->record(i).value("bonificacion").toInt();
uds = unidades+bonificacion;
descuentoLinea = modelo->record(i).value("descuento1").toString();
descripcion = modelo->record(i).value("descripcion").toString();
lote = modelo->record(i).value("lote").toString();
fechaCaducidad = modelo->record(i).value("fc").toString();
precioCosto = modelo->record(i).value("costo").toDouble();
pvp = modelo->record(i).value("pvp").toDouble();
baseProducto = modelo->record(i).value("base").toString();
tipoIva = modelo->record(i).value("tipoIva").toString();
baseLinea = modelo->record(i).value("totalbase").toString();
ivaLinea = modelo->record(i).value("iva").toString();
reLinea = modelo->record(i).value("re").toString();
//qDebug() << ean << " " << descripcion << " " << uds << " " << lote << " " << fechaCaducidad << " " << precioCosto << " " <<pvp;
// Buscar si hay lotes con unidades pendientes
QString idLote = base.idLote(conf->getConexionLocal(), ean,"","2000-01-01");
qDebug() << "IDLOTE "+ idLote;
if (idLote != "0") {
qDebug() << "IDLOTE "+ idLote;
int pendientes = base.unidadesLote(conf->getConexionLocal(), idLote);
qDebug() << "PENDIENTES: "+QString::number(pendientes);
if(abs(pendientes) > uds ){
base.aumentarLote(conf->getConexionLocal(), idLote,uds);
}else if (abs(pendientes) == uds) {
base.ejecutarSentencia("DELETE FROM lotes WHERE id = '"+idLote+"'", conf->getConexionLocal());
}else {
base.ejecutarSentencia("DELETE FROM lotes WHERE id = '"+idLote+"'", conf->getConexionLocal());
uds = uds+pendientes;
//Modificar o crear lotes
idLote = base.idLote(conf->getConexionLocal(), ean,lote,fechaCaducidad);
if (idLote == "0") {
base.crearLote(conf->getConexionLocal(), ean,lote,fechaCaducidad,QString::number(uds));
qDebug() << "Creando lote";
} else {
base.aumentarLote(conf->getConexionLocal(), idLote,uds);
}
}
}else {
//Modificar o crear lotes
idLote = base.idLote(conf->getConexionLocal(), ean,lote,fechaCaducidad);
if (idLote == "0") {
base.crearLote(conf->getConexionLocal(), ean,lote,fechaCaducidad,QString::number(uds));
qDebug() << "Creando lote";
} else {
base.aumentarLote(conf->getConexionLocal(), idLote,uds);
}
}
//Comprobar cambio de nombre o PVP y actualizar artículos
consulta = base.consulta_producto(conf->getConexionLocal(),ean);
consulta.first();
QString descripcionAnterior = consulta.value("descripcion").toString();
QString precioAnterior = consulta.value("pvp").toString();
switch (tipoActualizacion) {
case 2:
descripcion=descripcionAnterior;
pvp = precioAnterior.toDouble();
break;
case 0:
if (!(descripcionAnterior == descripcion && precioAnterior == QString::number(pvp))) {
cdProducto = new CambioDatosProducto(descripcion,descripcionAnterior,QString::number(pvp),precioAnterior,this);
cdProducto->exec();
descripcion = cdProducto->nombre;
pvp = cdProducto->pvp.toDouble();
tipoActualizacion = cdProducto->tipoActualizacion;
if (precioAnterior != QString::number(pvp)) {
base.insertarEtiqueta(conf->getConexionLocal(), ean);
}
}
case 1:
if (precioAnterior != QString::number(pvp)) {
base.insertarEtiqueta(conf->getConexionLocal(), ean);
}
default:
break;
}
datos.append(ean);
datos.append(descripcion);
datos.append(QString::number(pvp));
datos.append(tipoIva);
datos.append(QString::number(precioCosto));
datos.append(fecha);
if(base.actualizarArticulosDesdeCompras(datos)){
qDebug() << "actualizando " << datos.at(1);
}else{
QMessageBox msg;
msg.setText("ERROR");
msg.setInformativeText("No se ha podido actualizar la información de la tabla artículos");
msg.show();
return false;
}
//Grabar linea pedido
QStringList datosLineaPedido;
datosLineaPedido.clear();
datosLineaPedido.append(nDoc);
datosLineaPedido.append(idProveedor);
datosLineaPedido.append(ean);
datosLineaPedido.append(descripcion);
datosLineaPedido.append(QString::number(unidades));
datosLineaPedido.append(QString::number(bonificacion));
datosLineaPedido.append(lote);
datosLineaPedido.append(fechaCaducidad);
datosLineaPedido.append(QString::number(precioCosto));
datosLineaPedido.append(descuentoLinea);
datosLineaPedido.append(baseProducto);
datosLineaPedido.append(tipoIva);
datosLineaPedido.append(baseLinea);
datosLineaPedido.append(ivaLinea);
datosLineaPedido.append(reLinea);
datosLineaPedido.append(QString::number(pvp));
if (!base.pasarLineaPedidoAHistorico(conf->getConexionLocal(), datosLineaPedido)) {
return false;
}else{
qDebug() << base.borrarPedido(conf->getConexionLocal(), idPedido);
}
}
//Grabar pedido
QStringList datosPedido;
datosPedido.clear();
datosPedido.append(idProveedor);
datosPedido.append(nDoc);
datosPedido.append(fecha);
datosPedido.append(QString::number(modeloPedido->rowCount()));
// double unidades = base.sumarColumna("lineaspedido_tmp","cantidad","idPedido",idPedido);
// unidades += base.sumarColumna("lineaspedido_tmp","bonificacion","idPedido",idPedido);
datosPedido.append(QString::number(unidades));
datosPedido.append(QString::number(descuentoReal));
datosPedido.append(ui->leBase21->text());
datosPedido.append(ui->leIva21->text());
datosPedido.append(ui->leRe21->text());
datosPedido.append(ui->leBase10->text());
datosPedido.append(ui->leIva10->text());
datosPedido.append(ui->leRe10->text());
datosPedido.append(ui->leBase4->text());
datosPedido.append(ui->leIva4->text());
datosPedido.append(ui->leRe4->text());
datosPedido.append(ui->leBase0->text());
datosPedido.append(ui->leIva0->text());
datosPedido.append(ui->leRe0->text());
datosPedido.append(ui->leBase5->text());
datosPedido.append(ui->leIva5->text());
datosPedido.append(ui->leRe5->text());
datosPedido.append(ui->leTotalBase->text());
datosPedido.append(ui->leTotalIva->text());
datosPedido.append(ui->leTotalRe->text());
datosPedido.append(ui->leTotal->text());
if(ui->comboBox->currentText() == "Factura"){
datosPedido.append(ui->lineEditNDoc->text());
}else{
datosPedido.append("0");
}
if (!base.contabilizarPedido(conf->getConexionLocal(), datosPedido)) {
return false;
}
qDebug() << "Pedido grabado";
QStringList datosFactura;
datosFactura.clear();
datosFactura.append(ui->lineEditNDoc->text());
datosFactura.append(fecha);
datosFactura.append(base.idProveedor(ui->labelProveedor->text(),conf->getConexionLocal()));
datosFactura.append(ui->leTotalBase->text());
datosFactura.append(ui->leTotalIva->text());
datosFactura.append(ui->leTotalRe->text());
datosFactura.append(ui->leTotal->text());
if (ui->comboBox->currentText() == "Factura") {
datosFactura.append(ui->dateEditVencimiento->text());
datosFactura.append("0");
if (!base.grabarFactura(datosFactura)) {
return false;
}
}else {
datosFactura.append("0");
if (base.grabarAlbaran(datosFactura)) {
return false;
}
}
qDebug() << "Factura grabada";
return true;
}
void AceptarPedido::on_doubleSpinBox_valueChanged(double arg1)
{
descuento = ((100-arg1)/100);
descuentoReal = arg1;
llenarTabla(idPedido,descuento);
}
void AceptarPedido::on_pushButton_clicked()
{
correcto = procesarPedido(modeloPedido);
qDebug() << "Pedido aceptado";
close();
}
void AceptarPedido::on_lineEditNDoc_textChanged(const QString &arg1)
{
nDoc=arg1;
}
void AceptarPedido::on_comboBox_currentTextChanged(const QString &arg1)
{
if (arg1 == "Factura") {
ui->dateEditVencimiento->setEnabled(true);
ui->dateEditVencimiento->setDate(QDate::currentDate());
}else{
ui->dateEditVencimiento->setEnabled(false);
}
}