/
dlgreghistoria.cpp
170 lines (134 loc) · 4.99 KB
/
dlgreghistoria.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
#include "dlgreghistoria.h"
#include "ui_dlgreghistoria.h"
dlgRegHistoria::dlgRegHistoria(QString claveUsuario, QWidget *parent) :
QDialog(parent),
ui(new Ui::dlgRegHistoria)
{
ui->setupUi(this);
infoHistoria.clear();
clave = claveUsuario;
nombreListo = apellidoListo = sexoListo = false;
cedulaLista = fecha_nacLista = true;
ui->btnRegistrar->setEnabled(false);
QRegExp rx("^([a-zA-Z]+([ ]?[a-zA-Z]?[a-zA-Z]+)*)$");
QValidator *soloPalabras = new QRegExpValidator(rx, this);
ui->lineaNombre->setValidator(soloPalabras);
ui->lineaApellido->setValidator(soloPalabras);
QValidator *soloNumeros = new QIntValidator(1, 999999999, this);
ui->lineaCI->setValidator(soloNumeros);
QDate fechaMinima, fechaDefecto;
fechaMinima = fechaDefecto = QDate::currentDate();
fechaMinima.setDate(fechaMinima.year() - 120,fechaMinima.month(), fechaMinima.day() );
fechaDefecto.setDate(fechaDefecto.year() - 18, fechaMinima.month(), fechaMinima.day());
ui->editFechaNac->setDate(fechaDefecto);
ui->editFechaNac->setDateRange(fechaMinima, QDate::currentDate());
this->setFixedSize(this->size());
this->setWindowFlags(Qt::WindowCloseButtonHint);
}
bool dlgRegHistoria::camposListos()
{
return nombreListo && apellidoListo && cedulaLista && fecha_nacLista && sexoListo;
}
dlgRegHistoria::~dlgRegHistoria()
{
delete ui;
}
void dlgRegHistoria::on_btnCancelar_clicked()
{
close();
}
void dlgRegHistoria::on_lineaNombre_textChanged(const QString &arg1)
{
ui->lineaNombre->setText(arg1.toUpper());
if(ui->lineaNombre->text().length() >= 3){
nombreListo = true;
}else{
nombreListo = false;
}
if(camposListos())
ui->btnRegistrar->setEnabled(true);
else
ui->btnRegistrar->setEnabled(false);
}
void dlgRegHistoria::on_lineaApellido_textChanged(const QString &arg1)
{
ui->lineaApellido->setText(arg1.toUpper());
if(ui->lineaApellido->text().length() >= 3){
apellidoListo = true;
}else{
apellidoListo = false;
}
if(camposListos())
ui->btnRegistrar->setEnabled(true);
else
ui->btnRegistrar->setEnabled(false);
}
void dlgRegHistoria::on_cBoxSexo_currentIndexChanged(int index)
{
if(index != 0){
sexoListo = true;
}else{
sexoListo = false;
}
if(camposListos())
ui->btnRegistrar->setEnabled(true);
else
ui->btnRegistrar->setEnabled(false);
}
void dlgRegHistoria::on_btnRegistrar_clicked()
{
dlgConfirmarClave confirmar(clave);
connect(&confirmar, &dlgConfirmarClave::claveIntroducida, this, &dlgRegHistoria::on_claveIntroducida);
confirmar.exec();
}
void dlgRegHistoria::on_claveIntroducida(bool correcta)
{
if(correcta){
infoHistoria["nombre"] = ui->lineaNombre->text();
infoHistoria["apellido"] = ui->lineaApellido->text();
infoHistoria["fecha_nac"] = ui->editFechaNac->date().toString("yyyy-MM-dd");
infoHistoria["fecha_ingreso"] = QDate::currentDate().toString("yyyy-MM-dd");
if(ui->cBoxSexo->currentIndex() == 1)
infoHistoria["sexo"] = "F";
else
infoHistoria["sexo"] = "M";
if(!ui->lineaCI->text().isEmpty()){
infoHistoria["cedula"] = ui->cBoxCI->currentText() + ui->lineaCI->text();
}
QString consulta;
consulta = "INSERT INTO spectradb.historia(nombre, apellido, fecha_nac, fecha_ingreso, sexo";
if(infoHistoria.contains("cedula")){
consulta+= ", cedula";
}
consulta += ") VALUES(:nombre, :apellido, :fecha_nac, :fecha_ingreso, :sexo";
if(infoHistoria.contains("cedula")){
consulta+= ", :cedula";
}
consulta += ")";
QSqlQuery query;
query.prepare(consulta);
query.bindValue(":nombre", infoHistoria["nombre"]);
query.bindValue(":apellido", infoHistoria["apellido"]);
query.bindValue(":fecha_nac", infoHistoria["fecha_nac"]);
query.bindValue(":fecha_ingreso", infoHistoria["fecha_ingreso"]);
query.bindValue(":sexo", infoHistoria["sexo"]);
if(infoHistoria.contains("cedula")){
query.bindValue(":cedula", infoHistoria["cedula"]);
}
if(query.exec()){
infoHistoria["id_historia"] = query.lastInsertId().toString();
QMessageBox::information(this, "Historia registrada", "Se ha registrado la historia correctamente.");
close();
emit historia_registrada(infoHistoria);
}else{
//el codigo 23505 significa que se intento insertar un valor unico que ya existe
if(query.lastError().number() == 23505){
QMessageBox::critical(this, "Error al registrar", "La cédula de identidad " + infoHistoria["cedula"] + " ya está siendo utilizada.");
}
infoHistoria.clear();
ui->lineaCI->clear();
}
}else{
QMessageBox::critical(this, "Contraseña incorrecta", "La contraseña que introdujo es incorrecta.");
}
}