This repository was archived by the owner on May 14, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbalancehandler.cpp
More file actions
137 lines (115 loc) · 4.15 KB
/
Copy pathbalancehandler.cpp
File metadata and controls
137 lines (115 loc) · 4.15 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
#include "balancehandler.h"
BalanceHandler::BalanceHandler(DataBase *db, QObject *parent)
: QObject(parent), price(600), db(db) {
this->ui.setupUi(&dialog);
dialog.installEventFilter(this);
}
int BalanceHandler::credit(unsigned long long clientId, double amount) {
Client client = db->getClientById(clientId);
if (client.isNull()) {
qDebug() << "Cliente no encontrado con ID:" << clientId;
return TR_CLIENT_NOT_FOUND;
}
return updateBalance(client, amount);
}
int BalanceHandler::credit(const QString &name, double amount) {
Client client = db->getClientByName(name.toStdString());
if (client.isNull()) {
qDebug() << "Cliente no encontrado con nombre:" << name;
return TR_CLIENT_NOT_FOUND;
}
return updateBalance(client, amount);
}
int BalanceHandler::debit(unsigned long long clientId, double amount) {
Client client = db->getClientById(clientId);
if (client.isNull()) {
qDebug() << "Cliente no encontrado con ID:" << clientId;
emit clientNotFound();
return TR_CLIENT_NOT_FOUND;
}
return updateBalance(client, -amount);
}
int BalanceHandler::debit(const QString &name, double amount) {
Client client = db->getClientByName(name.toStdString());
if (client.isNull()) {
qDebug() << "Cliente no encontrado con nombre:" << name;
return TR_CLIENT_NOT_FOUND;
}
return updateBalance(client, -amount);
}
int BalanceHandler::updateBalance(Client &client, double amount) {
double newBalance = client.getBalance() + amount;
if (newBalance < 0) {
emit balanceUpdateFailed("Fondos insuficientes para el cliente: " + QString::fromStdString(client.getName()));
return TR_NOT_ENOUGH_FUNDS;
}
client.setBalance(newBalance);
try {
db->updateClient(client);
emit balanceUpdated("Balance actualizado para el cliente: " + QString::fromStdString(client.getName()) + " Nuevo balance: " + QString::number(newBalance));
if(newBalance < price)
return TR_JUST_ENOUGH_FUNDS;
return OK;
} catch (const std::runtime_error &e) {
emit balanceUpdateFailed("Error al actualizar el balance: " + QString::fromStdString(e.what()));
return TR_ERROR;
}
}
double BalanceHandler::loadPrice() {
QFile file("culocacapis.txt");
if (file.exists()) {
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
in >> price;
file.close();
qDebug() << "Precio cargado desde archivo:" << price;
return price;
} else {
qDebug() << "No se pudo abrir el archivo para lectura.";
}
} else {
savePrice(price);
qDebug() << "Archivo no encontrado. Precio por defecto establecido a:" << price;
return price;
}
return 0;
}
void BalanceHandler::savePrice(double price) {
QFile file("culocacapis.txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << price;
file.close();
qDebug() << "Precio guardado en archivo:" << price;
} else {
qDebug() << "No se pudo abrir el archivo para escritura.";
}
}
void BalanceHandler::setPrice(double newPrice) {
savePrice(newPrice);
}
int BalanceHandler::openDialog(int isCharging,const QString &name) {
if(!isCharging){
this->ui.clientNameLineEdit->setText("");
this->ui.amountSpinBox->setValue(0.00);
// Configurar conexiones, por ejemplo, conectar el botón de aceptar o rechazar
if (dialog.exec() == QDialog::Accepted) {
QString clientName = ui.clientNameLineEdit->text();
double amount = QLocale::system().toDouble(ui.amountSpinBox->text());
return credit(clientName, amount);
}
}
else{
completeName(name, this->ui);
}
}
void BalanceHandler::completeName(const QString &name, Ui::BalanceHandlerDialog &ui){
ui.clientNameLineEdit->setText(name);
}
bool BalanceHandler::eventFilter(QObject *watched, QEvent *event) {
if (watched == &dialog && event->type() == QEvent::Close) {
emit windowClosed();
return false;
}
return QObject::eventFilter(watched, event);
}