Commit 0775f548 authored by Hugo Hörnquist's avatar Hugo Hörnquist

Add account select to money diff.

parent a6b8efbe
......@@ -242,14 +242,7 @@ MainWindow::MainWindow(QWidget *parent) :
acView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
acView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
// TODO this combo box isn't updated when new accounts are added.
ui->submitBuyAccountCombo->clear();
QSqlQuery accQuery("SELECT id, name FROM money WHERE amount IS NOT NULL");
while (accQuery.next()) {
QString name = accQuery.value(1).toString();
QVariant value = accQuery.value(0);
ui->submitBuyAccountCombo->addItem(name, value);
}
populateMoneyComboBox(ui->submitBuyAccountCombo);
// We usually want the "Vault" account. So we select it by default.
ui->submitBuyAccountCombo->setCurrentIndex(1);
......@@ -290,10 +283,12 @@ MainWindow::MainWindow(QWidget *parent) :
// -------------- Money Check -----------------------
setMoneyDiffLabels();
ui->diffDrawer->setValidator(anyIntVal);
populateMoneyComboBox(ui->moneyDiffCombo);
ui->moneyDiffCombo->setCurrentIndex(0);
setMoneyDiffLabels();
// ============== Drainage ==========================
// TODO some of these things possibly should requery under usage
......@@ -402,6 +397,18 @@ void MainWindow::setAccTransferCombo() {
}
}
void MainWindow::populateMoneyComboBox(QComboBox* box)
{
// TODO this combo box isn't updated when new accounts are added.
box->clear();
QSqlQuery accQuery("SELECT id, name FROM money WHERE amount IS NOT NULL");
while (accQuery.next()) {
QString name = accQuery.value(1).toString();
QVariant value = accQuery.value(0);
box->addItem(name, value);
}
}
void MainWindow::setMoneyAccountValues() {
QSqlTableModel* accModel = (QSqlTableModel*) ui->accTreeView->model();
......@@ -747,14 +754,17 @@ void MainWindow::on_stockRefreshButton_clicked()
void MainWindow::on_diffSubmit_clicked()
{
// TODO this doesn't update the money menu
QString str = ui->diffDrawer->text();
if (str == "") return;
int total = str.toInt() * 100;
QSqlQuery query;
query.prepare("INSERT INTO money_diffs (actual, expected, account) "
"SELECT :total, amount, id "
" FROM money WHERE name = 'Cash Drawer'");
"FROM money WHERE id = :id");
query.bindValue(":total", total);
query.bindValue(":id", ui->moneyDiffCombo->currentData());
if (!query.exec())
qDebug() << query.lastError();
......@@ -766,7 +776,7 @@ void MainWindow::on_diffSubmit_clicked()
void MainWindow::on_diffDrawer_textChanged(const QString &arg1)
{
int new_total = arg1.toInt() * 100;
setMoneyDiffLabels(new_total);
setMoneyDiffLabels(ui->moneyDiffCombo->currentData(), new_total);
}
void MainWindow::on_submitStockDiff_clicked()
......@@ -775,12 +785,27 @@ void MainWindow::on_submitStockDiff_clicked()
((QSqlRelationalTableModel*) ui->inventoryCheckView->model())->select();
}
int MainWindow::setMoneyDiffLabels()
void MainWindow::setMoneyDiffLabels()
{
QSqlQuery query("SELECT amount FROM money WHERE name = 'Cash Drawer'");
query.next();
int expected = query.value(0).toInt();
QString diff_text = QString("%1 kr").arg(expected / 100.0, 0, 'f', 2);
setMoneyDiffLabels(ui->moneyDiffCombo->currentData());
}
int MainWindow::setMoneyDiffLabels(QVariant accountId)
{
QSqlQuery query;
query.prepare("SELECT amount FROM money WHERE id = :id");
query.bindValue(":id", accountId);
query.exec();
QString diff_text;
int expected;
if (query.next()) {
expected = query.value(0).toInt();
diff_text = QString("%1 kr").arg(expected / 100.0, 0, 'f', 2);
} else {
qDebug() << query.lastError();
return -1;
}
// This might seem like a mistake at first sight, though it isn't.
// This just sets a default value for diffTotal. The new value is
......@@ -792,9 +817,9 @@ int MainWindow::setMoneyDiffLabels()
return expected;
}
void MainWindow::setMoneyDiffLabels(int new_total)
void MainWindow::setMoneyDiffLabels(QVariant accountId, int new_total)
{
double diff = new_total - setMoneyDiffLabels();
double diff = new_total - setMoneyDiffLabels(accountId);
ui->diffTotal->setText(QString("%1 kr").arg(diff / 100.0, 0, 'f', 2));
}
......@@ -1110,3 +1135,9 @@ void MainWindow::on_sqlBufferDeleteButton_clicked()
model->select();
}
void MainWindow::on_moneyDiffCombo_currentIndexChanged(int index)
{
setMoneyDiffLabels();
ui->diffDrawer->setText("");
}
......@@ -6,6 +6,7 @@
#include <QSqlRelationalTableModel>
#include <QTableView>
#include <QPrinter>
#include <QComboBox>
#include "disablingmodel.h"
......@@ -100,6 +101,8 @@ private slots:
void on_sqlBufferDeleteButton_clicked();
void on_moneyDiffCombo_currentIndexChanged(int index);
private:
Ui::MainWindow *ui;
QSqlTableModel* model;
......@@ -108,8 +111,9 @@ private:
char* port_name;
void updateMoneyString();
void setMoneyDiffLabels(int);
int setMoneyDiffLabels();
void setMoneyDiffLabels(QVariant, int);
int setMoneyDiffLabels(QVariant);
void setMoneyDiffLabels();
void transferMoney(QString from, QString to, int amount, QString message);
void transferMoney(QString from, QString to, int amount);
void updateDrainageView();
......@@ -120,6 +124,7 @@ private:
void clear_stockDiffTemp();
void setProductFilter();
void setMoneyAccountValues();
void populateMoneyComboBox(QComboBox* box);
void setSqlQuery(qlonglong id);
void setSqlQuery(QString name);
......
......@@ -357,10 +357,13 @@
<item>
<widget class="QLabel" name="label_11">
<property name="text">
<string>Kassadiff:</string>
<string>Pengadiff:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="moneyDiffCombo"/>
</item>
<item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment