-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFinDatabase.cpp
More file actions
443 lines (391 loc) · 11.6 KB
/
FinDatabase.cpp
File metadata and controls
443 lines (391 loc) · 11.6 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <QtSql>
#include <QDebug>
#include "common.h"
#include "FinDatabase.h"
#include <QTreeWidget>
FinDatabase::FinDatabase()
{}
FinDatabase::~FinDatabase()
{
closeDatabase();
}
FinDatabase *FinDatabase::Instance()
{
static FinDatabase *fd = NULL;
if (!fd)
fd = new FinDatabase;
return fd;
}
void FinDatabase::openDatabase(const QString& path)
{
if (db.isOpen())
db.close();
if (!db.databaseName().isEmpty())
QSqlDatabase::removeDatabase(db.databaseName());
db = QSqlDatabase::addDatabase("QSQLITE", path);
db.setDatabaseName(path);
qDebug() << db.databaseName();
if (!db.open())
throw FinException("failed to open db");
}
void FinDatabase::closeDatabase()
{
if (db.isOpen())
{
qDebug("closing db");
db.close();
}
db = QSqlDatabase();
}
void FinDatabase::createDatabase(const QString& path)
{
openDatabase(path);
QSqlQuery q(db);
q.exec("DROP TABLE operations");
q.exec("CREATE TABLE operations "
"(id INT PRIMARY KEY, "
"value NUMBER, "
"notes VARCHAR, "
"discount NUMBER NOT NULL DEFAULT 0, "
"amount NUMBER NOT NULL DEFAULT 1, "
"oprtype INT NOT NULL DEFAULT 0, "
"rdate DATE NOT NULL DEFAULT CURRENT_DATE)");
q.exec("DROP TABLE groups");
q.exec("CREATE TABLE groups (id_group INT NOT NULL, id INT NOT NULL)");
q.exec("DROP TABLE oprtypes");
q.exec("CREATE TABLE oprtypes (id INT NOT NULL, name VARCHAR NOT NULL, path VARCHAR NOT NULL)");
}
int FinDatabase::getNextId(QString tableName)
{
QString sql = "SELECT MAX(id) FROM " + tableName;
int val = 0;
QSqlQuery q(db);
if (!q.exec(sql))
{
qDebug() << q.lastError().text();
return 0;
}
while (q.next())
val = q.value(0).toInt();
return val + 1;
}
int FinDatabase::addRecord(const Operation& opr)
{
int id = getNextId("operations");
QSqlQuery query(db);
query.prepare("INSERT INTO operations (id, value, notes, amount, discount, oprtype, rdate) "
"VALUES (:id, :value, :notes, :amount, :discount, :oprtype, :rdate)");
query.bindValue(":id", id);
query.bindValue(":value", opr.value);
query.bindValue(":notes", opr.notes);
query.bindValue(":amount", opr.amount);
query.bindValue(":discount", opr.discount);
query.bindValue(":oprtype", opr.oprtype);
query.bindValue(":rdate", opr.rdate);
if (!query.exec())
{
qDebug() << query.lastError();
return 0;
}
return id;
}
void FinDatabase::editRecord(const Operation& opr)
{
QSqlQuery query(db);
query.prepare("UPDATE operations SET "
"value = :value, notes = :notes, amount = :amount, "
"discount = :discount, oprtype = :oprtype, rdate = :rdate "
"WHERE id = :id");
query.bindValue(":id", opr.id);
query.bindValue(":value", opr.value);
query.bindValue(":notes", opr.notes);
query.bindValue(":amount", opr.amount);
query.bindValue(":discount", opr.discount);
query.bindValue(":oprtype", opr.oprtype);
query.bindValue(":rdate", opr.rdate);
if (!query.exec())
{
qDebug() << query.lastError();
throw FinException("updating failed");
}
}
void FinDatabase::delRecord(int id)
{
QSqlQuery query(db);
query.prepare("DELETE FROM operations WHERE id = :id1 OR id IN (select id from groups where id_group = :id2)");
query.bindValue(":id1", id);
query.bindValue(":id2", id);
if (!query.exec())
throw FinException("deleting failed");
}
void FinDatabase::delRecordFromGroup(int id_group, int id)
{
QSqlQuery query(db);
query.prepare("DELETE FROM groups WHERE id_group = :id_group and id = :id");
query.bindValue(":id_group", id_group);
query.bindValue(":id", id);
if (!query.exec())
throw FinException("deleting failed");
}
void FinDatabase::loadRange(const QDate& from, const QDate& to, QList<QTreeWidgetItem*>& items)
{
QSqlQuery q(db);
q.prepare("select id, rdate, value, amount, discount, notes from operations where rdate >= :dtFrom and rdate <= :dtTo and id not in (select id from groups) order by rdate");
q.bindValue(":dtFrom", from);
q.bindValue(":dtTo", to);
if (!q.exec())
{
qDebug() << q.lastError().text();
return;
}
while (q.next())
{
QStringList list;
list << q.value(1).toString() << q.value(2).toString() << q.value(3).toString() << q.value(4).toString() << q.value(5).toString();
QTreeWidgetItem *item = new QTreeWidgetItem(list);
item->setData(0, Qt::UserRole, q.value(0));
items.append(item);
}
}
void FinDatabase::loadRangeOperations(const QDate& from, const QDate& to, bool oprType, Records& recs)
{
QString sql(
"select value, notes from operations "
"where rdate >= :dtFrom and rdate <= :dtTo "
"and id not in (select id from groups) ");
if (oprType)
sql += "and value > 0 ";
else
sql += "and value < 0 ";
sql += "order by rdate";
QSqlQuery q(db);
q.prepare(sql);
q.bindValue(":dtFrom", from);
q.bindValue(":dtTo", to);
if (!q.exec())
{
qDebug() << q.lastError().text();
return;
}
while (q.next())
{
Record rec;
rec.num = q.value(0).toDouble();
rec.name = q.value(1).toString();
recs.push_back(rec);
}
}
void FinDatabase::loadGroupContent(int id_group, QList<QTreeWidgetItem*>& items)
{
QSqlQuery q(db);
q.prepare("select o.id, o.rdate, o.value, o.amount, o.discount, o.notes from operations o, groups g where o.id = g.id and g.id_group = :id_group");
q.bindValue(":id_group", id_group);
if (!q.exec())
{
qDebug() << q.lastError().text();
return;
}
while (q.next())
{
QStringList list;
list << q.value(1).toString() << q.value(2).toString() << q.value(3).toString() << q.value(4).toString() << q.value(5).toString();
QTreeWidgetItem *item = new QTreeWidgetItem(list);
item->setData(0, Qt::UserRole, q.value(0));
items.append(item);
}
}
bool FinDatabase::isGroupRecord(int id)
{
QSqlQuery q(db);
q.prepare("select count(id) from groups where id_group = :id");
q.bindValue(":id", id);
if (!q.exec())
{
qDebug("checking groups failed");
return false;
}
int res = 0;
while (q.next())
res = q.value(0).toInt();
return res;
}
void FinDatabase::addRecordToGroup(int id_group, int id_rec)
{
qDebug("adding rec %d to group %d", id_rec, id_group);
QSqlQuery query(db);
query.prepare("INSERT INTO groups VALUES (:id_group, :id_rec)");
query.bindValue(":id_group", id_group);
query.bindValue(":id_rec", id_rec);
if (!query.exec())
{
qDebug() << query.lastError().text();
throw FinException("inserting in group failed");
}
}
void FinDatabase::beginTransaction()
{
QSqlQuery query(db);
query.prepare("begin transaction");
if (!query.exec())
throw FinException("failed to begin transaction");
}
void FinDatabase::commitTransaction()
{
QSqlQuery query(db);
query.prepare("commit transaction");
if (!query.exec())
throw FinException("failed to commit transaction");
}
void FinDatabase::rollbackTransaction()
{
QSqlQuery query(db);
query.prepare("rollback transaction");
if (!query.exec())
throw FinException("failed to rollback transaction");
}
void FinDatabase::loadCompletion(QStringList &completionList)
{
QSqlQuery q(db);
q.prepare("select distinct notes from operations order by notes");
if (!q.exec())
{
qDebug("loading completion failed");
return;
}
while (q.next())
completionList.append(q.value(0).toString());
}
namespace
{
void prepareDate(int month, int year, QString& date)
{
date = QVariant(year).toString() + "-";
if (month >= 10)
date += QVariant(month).toString();
else
date += "0" + QVariant(month).toString();
}
void prepareDates(int month, int year, QString& startDate, QString& finishDate)
{
prepareDate(month, year, startDate);
if (month == 12)
finishDate = QVariant(year + 1).toString() + "-01";
else
{
month += 1;
prepareDate(month, year, finishDate);
}
}
}
double FinDatabase::calcMonthCharge(int month, int year)
{
QString startDate, finishDate;
prepareDates(month, year, startDate, finishDate);
QString sql = "select sum(value) from operations where value < 0 ";
sql += "and rdate > '" + startDate + "' ";
sql += "and rdate < '" + finishDate + "' ";
sql += "and id not in (select id from groups)";
double res = 0;
QSqlQuery q(db);
q.prepare(sql);
if (!q.exec())
{
qDebug("calcMonthCharge failed");
return 0;
}
while (q.next())
res = q.value(0).toDouble();
return res;
}
double FinDatabase::calcMonthIncome(int month, int year)
{
QString startDate, finishDate;
prepareDates(month, year, startDate, finishDate);
QString sql = "select sum(value) from operations where value > 0 ";
sql += "and rdate > '" + startDate + "' ";
sql += "and rdate < '" + finishDate + "' ";
sql += "and id not in (select id from groups)";
double res = 0;
QSqlQuery q(db);
q.prepare(sql);
if (!q.exec())
{
qDebug("calcMonthCharge failed");
return 0;
}
while (q.next())
res = q.value(0).toDouble();
return res;
}
void FinDatabase::loadMaxCharges(int month, int year, int num, Records& recs)
{
QString startDate, finishDate;
prepareDates(month, year, startDate, finishDate);
QString sql = "select value, notes from operations where value < 0 ";
sql += "and rdate > '" + startDate + "' ";
sql += "and rdate < '" + finishDate + "' ";
sql += "and id not in (select id from groups) ";
sql += "order by value";
QSqlQuery q(db);
q.prepare(sql);
if (!q.exec())
{
qDebug("loadMaxCharges failed");
return;
}
int i = 1;
while (q.next())
{
Record rec;
rec.num = q.value(0).toDouble();
rec.name = q.value(1).toString();
recs.push_back(rec);
if (i >= num)
break;
i++;
}
}
void FinDatabase::loadMaxIncomes(int month, int year, int num, Records& recs)
{
QString startDate, finishDate;
prepareDates(month, year, startDate, finishDate);
QString sql = "select value, notes from operations where value > 0 ";
sql += "and rdate > '" + startDate + "' ";
sql += "and rdate < '" + finishDate + "' ";
sql += "and id not in (select id from groups) ";
sql += "order by value desc";
QSqlQuery q(db);
q.prepare(sql);
if (!q.exec())
{
qDebug("loadMaxIncomes failed");
return;
}
int i = 1;
while (q.next())
{
Record rec;
rec.num = q.value(0).toDouble();
rec.name = q.value(1).toString();
recs.push_back(rec);
if (i >= num)
break;
i++;
}
}
void FinDatabase::loadPeriod(int startMonth, int startYear, int finishMonth, int finishYear, PeriodInfos& recs)
{
QDate startDate(startYear, startMonth, 1);
QDate finishDate(finishYear, finishMonth, 1);
QDate currDate(startDate);
while (currDate < finishDate)
{
PeriodInfo pi;
prepareDate(currDate.month(), currDate.year(), pi.date);
pi.income = calcMonthIncome(currDate.month(), currDate.year());
pi.charge = calcMonthCharge(currDate.month(), currDate.year());
if (pi.income || pi.charge)
recs.push_back(pi);
currDate = currDate.addMonths(1);
}
}