summaryrefslogtreecommitdiff
path: root/geld.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'geld.cpp')
-rw-r--r--geld.cpp144
1 files changed, 119 insertions, 25 deletions
diff --git a/geld.cpp b/geld.cpp
index aaab6dd..1482a5a 100644
--- a/geld.cpp
+++ b/geld.cpp
@@ -5,6 +5,7 @@
#include <QtGlobal>
#include <QSqlQueryModel>
#include <QMessageBox>
+#include "contraaccount.h"
#include "transactionmodel.h"
#include "transaction.h"
@@ -28,20 +29,35 @@ Geld::Geld(QWidget *parent)
TransactionModel *transmod = new TransactionModel(ui->tableTransactions);
ui->tableTransactions->setModel(transmod);
ui->tableTransactions->setColumnHidden(0, true);
- refreshTransactions();
+
+ QSqlQueryModel *contramod = new QSqlQueryModel(ui->tableContra);
+ ui->tableContra->setModel(contramod);
+ ui->tableTransactions->setColumnHidden(0, true);
+ refreshLists();
// the Qt Designer interface lacks functionality for connecting actions to widgets
- ui->tableTransactions->addAction(ui->action_trans_Add);
- ui->tableTransactions->addAction(ui->action_trans_Edit);
- ui->tableTransactions->addAction(ui->action_trans_Delete);
+ ui->tableTransactions->addAction(ui->actionTransAdd);
+ ui->tableTransactions->addAction(ui->actionTransEdit);
+ ui->tableTransactions->addAction(ui->actionTransDelete);
+
+ ui->tableContra->addAction(ui->actionContraAdd);
+ ui->tableContra->addAction(ui->actionContraEdit);
+ ui->tableContra->addAction(ui->actionContraDelete);
// the Qt Designer interface also lacks some functionality when it comes to connecting signals to functions
- connect(ui->btnAddTransaction, SIGNAL(clicked()), this, SLOT(addTransaction()));
- connect(ui->action_trans_Add, SIGNAL(triggered()), this, SLOT(addTransaction()));
- connect(ui->btnEditTransaction, SIGNAL(clicked()), this, SLOT(editTransaction()));
- connect(ui->action_trans_Edit, SIGNAL(triggered()), this, SLOT(editTransaction()));
- connect(ui->tableTransactions, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(editTransaction()));
- connect(ui->action_trans_Delete, SIGNAL(triggered()), this, SLOT(deleteTransaction()));
+ connect(ui->btnAddTransaction, SIGNAL(clicked()), this, SLOT(addTransaction()));
+ connect(ui->actionTransAdd, SIGNAL(triggered()), this, SLOT(addTransaction()));
+ connect(ui->btnEditTransaction, SIGNAL(clicked()), this, SLOT(editTransaction()));
+ connect(ui->actionTransEdit, SIGNAL(triggered()), this, SLOT(editTransaction()));
+ connect(ui->tableTransactions, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(editTransaction()));
+ connect(ui->actionTransDelete, SIGNAL(triggered()), this, SLOT(deleteTransaction()));
+
+ connect(ui->btnAddContra, SIGNAL(clicked()), this, SLOT(addContra()));
+ connect(ui->actionContraAdd, SIGNAL(triggered()), this, SLOT(addContra()));
+ connect(ui->btnEditContra, SIGNAL(clicked()), this, SLOT(editContra()));
+ connect(ui->actionContraEdit, SIGNAL(triggered()), this, SLOT(editContra()));
+ connect(ui->tableContra, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(editContra()));
+ connect(ui->actionContraDelete, SIGNAL(triggered()), this, SLOT(deleteContra()));
ui->statusBar->showMessage(QString("Opened database file %1").arg(f), 0);
}
@@ -92,18 +108,7 @@ void Geld::closeDB()
QSqlDatabase::removeDatabase(conn);
}
-int Geld::selectedTransaction() {
- // this. is. soooooooo. ugly.
- TransactionModel *mod = (TransactionModel *) ui->tableTransactions->model();
- QModelIndexList indexes = ui->tableTransactions->selectionModel()->selection().indexes();
- if(indexes.count() != mod->columnCount()) {
- ui->statusBar->showMessage("Nothing selected.", 3000);
- return 0;
- }
- return mod->data(mod->index(indexes.at(0).row(), 0), Qt::DisplayRole).toInt();
-}
-
-void Geld::refreshTransactions() {
+void Geld::refreshLists() {
ui->tableTransactions->horizontalHeader()->setStretchLastSection(false);
((TransactionModel *) ui->tableTransactions->model())->refresh();
ui->tableTransactions->resizeColumnsToContents();
@@ -114,6 +119,36 @@ void Geld::refreshTransactions() {
q.next();
ui->lblTransactionBalance->setText(QString("Balance: <span style=\"color: %1\">%2</span>")
.arg(q.value(0).toInt() < 0 ? "red" : "green").arg(q.value(0).toDouble()/100, 4, 'f', 2));
+
+ ui->tableContra->horizontalHeader()->setStretchLastSection(false);
+ QSqlQueryModel *m = (QSqlQueryModel *)ui->tableContra->model();
+ m->setQuery(
+ "SELECT c.id, c.account, c.name, COUNT(t.id), c.address "
+ "FROM contra_accounts c "
+ "LEFT JOIN transactions t ON t.contra = c.id "
+ "GROUP BY c.id "
+ "ORDER BY name"
+ );
+ m->setHeaderData(0, Qt::Horizontal, "ID");
+ m->setHeaderData(1, Qt::Horizontal, "Account");
+ m->setHeaderData(2, Qt::Horizontal, "Name");
+ m->setHeaderData(3, Qt::Horizontal, "Transactions");
+ m->setHeaderData(4, Qt::Horizontal, "Address");
+ ui->tableContra->setColumnHidden(0, true);
+ ui->tableContra->resizeColumnsToContents();
+ ui->tableContra->horizontalHeader()->setStretchLastSection(true);
+}
+
+int Geld::selectedTransaction()
+{
+ // this. is. soooooooo. ugly.
+ TransactionModel *mod = (TransactionModel *) ui->tableTransactions->model();
+ QModelIndexList indexes = ui->tableTransactions->selectionModel()->selection().indexes();
+ if(indexes.count() != mod->columnCount()) {
+ ui->statusBar->showMessage("Nothing selected.", 3000);
+ return 0;
+ }
+ return mod->data(mod->index(indexes.at(0).row(), 0), Qt::DisplayRole).toInt();
}
void Geld::editTransaction()
@@ -125,7 +160,7 @@ void Geld::editTransaction()
t.load(id);
if(t.exec() == QDialog::Accepted) {
t.save(id);
- refreshTransactions();
+ refreshLists();
}
}
@@ -134,7 +169,7 @@ void Geld::addTransaction()
Transaction t(this);
if(t.exec() == QDialog::Accepted) {
t.save();
- refreshTransactions();
+ refreshLists();
}
}
@@ -148,7 +183,66 @@ void Geld::deleteTransaction()
q.prepare("DELETE FROM transactions WHERE id = ?");
q.addBindValue(id);
q.exec();
- refreshTransactions();
+ refreshLists();
+ }
+}
+
+int Geld::selectedContra()
+{
+ // this. is. soooooooo. ugly.
+ QAbstractItemModel *mod = ui->tableContra->model();
+ QModelIndexList indexes = ui->tableContra->selectionModel()->selection().indexes();
+ if(indexes.count() != mod->columnCount()) {
+ ui->statusBar->showMessage("Nothing selected.", 3000);
+ return 0;
+ }
+ return mod->data(mod->index(indexes.at(0).row(), 0), Qt::DisplayRole).toInt();
+}
+
+void Geld::addContra()
+{
+ ContraAccount c(this);
+ if(c.exec() == QDialog::Accepted) {
+ c.save();
+ refreshLists();
+ }
+}
+
+void Geld::editContra()
+{
+ int id = selectedContra();
+ if(!id)
+ return;
+ ContraAccount c(this);
+ c.load(id);
+ if(c.exec() == QDialog::Accepted) {
+ c.save(id);
+ refreshLists();
+ }
+}
+
+void Geld::deleteContra()
+{
+ int id = selectedContra();
+ if(!id)
+ return;
+ QSqlQuery q;
+ q.prepare("SELECT 1 FROM transactions WHERE contra = ? LIMIT 1");
+ q.addBindValue(id);
+ q.exec();
+ if(q.next()) {
+ QMessageBox::critical(this, "Can't delete contra account",
+ "This contra account appears to be refered to from a transaction. "
+ "Please make sure there are no transactions with this contra account in the database and try again."
+ );
+ return;
+ }
+ if(QMessageBox::question(this, "You sure?", "Are you sure you want to remove the selected contra account?", QMessageBox::No|QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
+ QSqlQuery q;
+ q.prepare("DELETE FROM contra_accounts WHERE id = ?");
+ q.addBindValue(id);
+ q.exec();
+ refreshLists();
}
}