-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDBHistory.cpp
82 lines (63 loc) · 2.41 KB
/
TDBHistory.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
#include "TDBHistory.h"
TDBHistory::TDBHistory (QWidget* parent) : QTreeWidget(parent)
{
setColumnCount(3);
QStringList header_strings;
header_strings << "Montant" << "Compte" << "Commentaire" << "Admin" << "Date";
setHeaderLabels(header_strings);
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
setSortingEnabled(true);
setFocusPolicy(Qt::NoFocus);
}
void TDBHistory::fill (TDBAccount* account)
{
// affichage par défaut, effort minimal puisque la fenêtre doit être vide actuellement
sortByColumn(-1);
TDBDatabase::open();
QSqlQuery query;
query.prepare("SELECT price, transactions.id, id2, comment, admin, date, accounts.trigramme as tr2 FROM transactions LEFT JOIN accounts ON accounts.id = transactions.id2 WHERE transactions.id = :id1"
" UNION SELECT -price as price, transactions.id, id2, comment, admin, date, accounts.trigramme as tr2 FROM transactions LEFT JOIN accounts ON accounts.id = transactions.id WHERE transactions.id2= :id2 ORDER BY date DESC");
query.bindValue(":id1", account->get_id());
query.bindValue(":id2", account->get_id());
query.exec();
if (query.first())
insert_item(query);
while (query.next())
insert_item(query);
TDBDatabase::close();
}
void TDBHistory::update (TDBAccount* account)
{
clear();
fill(account);
}
void TDBHistory::insert_item(QSqlQuery& query)
{
QStringList item;
QSqlRecord record;
record = query.record();
item.clear();
int admin_id = record.value("admin").toInt();
QString admin;
// si un admin est intervenu dans cette ligne d'historique, on récupère son trigramme pour l'afficher.
if (admin_id)
{
TDBDatabase::open();
QSqlQuery admin_query;
admin_query.prepare("SELECT trigramme FROM accounts WHERE id = :id");
admin_query.bindValue(":id", admin_id);
admin_query.exec();
admin_query.first();
admin = admin_query.record().value("trigramme").toString();
TDBDatabase::close();
}
else
admin = "";
item<<QString::number(record.value("price").toDouble()/100, 'f', 2)
<<record.value("tr2").toString()
<<record.value("comment").toString()
<<admin
<<QDateTime::fromTime_t(record.value("date").toInt()).toString("dd/MM/yyyy hh:mm:ss");
addTopLevelItem(new QTreeWidgetItem(this, item));
}