Commit b0ba6627 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Add ability to do acquisitions with any money account.

To install, source the patch.sql file into the current database.
parent eb502a67
...@@ -246,6 +246,18 @@ MainWindow::MainWindow(QWidget *parent) : ...@@ -246,6 +246,18 @@ MainWindow::MainWindow(QWidget *parent) :
acView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); acView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
acView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); 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);
}
// We usually want the "Vault" account. So we select it by default.
ui->submitBuyAccountCombo->setCurrentIndex(1);
// ============== Stock Check ======================= // ============== Stock Check =======================
clear_stockDiffTemp(); clear_stockDiffTemp();
...@@ -387,8 +399,10 @@ void MainWindow::setAccTransferCombo() { ...@@ -387,8 +399,10 @@ void MainWindow::setAccTransferCombo() {
QSqlQuery accQuery("SELECT id, name FROM money"); QSqlQuery accQuery("SELECT id, name FROM money");
while (accQuery.next()) { while (accQuery.next()) {
ui->accTransferFrom->addItem(accQuery.value(1).toString(), accQuery.value(0)); QString name = accQuery.value(1).toString();
ui->accTransferTo->addItem(accQuery.value(1).toString(), accQuery.value(0)); QVariant value = accQuery.value(0);
ui->accTransferFrom->addItem(name, value);
ui->accTransferTo->addItem(name, value);
} }
} }
...@@ -561,7 +575,22 @@ void MainWindow::on_buySearch_textChanged(const QString &arg1) ...@@ -561,7 +575,22 @@ void MainWindow::on_buySearch_textChanged(const QString &arg1)
*/ */
void MainWindow::on_submitBuyButton_clicked() void MainWindow::on_submitBuyButton_clicked()
{ {
QVariant activeAccount = ui->submitBuyAccountCombo->currentData();
// TODO is this safe?
// Or could some other process modify active_money_account while
// the acquisitions_temp triggers fire?
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("UPDATE my_db_settings SET value = :account WHERE "
"name = 'active_money_account'");
query.bindValue(":account", activeAccount);
query.exec();
QSqlQuery("DELETE FROM acquisitions_temp"); QSqlQuery("DELETE FROM acquisitions_temp");
if (! QSqlDatabase::database().commit()) {
qDebug() << QSqlDatabase::database().lastError();
//QMessageBox::warning( this, "SQL Querry error", "Error while executing query\n\n " + model->lastError().text());
}
tempAcquisitionsModel->select(); tempAcquisitionsModel->select();
updateMoneyString(); updateMoneyString();
......
...@@ -251,11 +251,31 @@ ...@@ -251,11 +251,31 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="submitBuyButton"> <layout class="QHBoxLayout" name="horizontalLayout_8">
<property name="text"> <item>
<string>Submit</string> <widget class="QComboBox" name="submitBuyAccountCombo">
</property> <property name="sizePolicy">
</widget> <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="submitBuyButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Submit</string>
</property>
</widget>
</item>
</layout>
</item> </item>
</layout> </layout>
</widget> </widget>
......
...@@ -31,6 +31,7 @@ CREATE TABLE log ( ...@@ -31,6 +31,7 @@ CREATE TABLE log (
product_id INTEGER NOT NULL, product_id INTEGER NOT NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
price INTEGER NOT NULL, price INTEGER NOT NULL,
account INTEGER NOT NULL REFERENCES money(id),
FOREIGN KEY (product_id) REFERENCES products(id) FOREIGN KEY (product_id) REFERENCES products(id)
); );
...@@ -77,6 +78,7 @@ CREATE TABLE acquisitions ( ...@@ -77,6 +78,7 @@ CREATE TABLE acquisitions (
product_id INTEGER NOT NULL, product_id INTEGER NOT NULL,
item_price INTEGER DEFAULT 0, -- price of each unit item_price INTEGER DEFAULT 0, -- price of each unit
amount INTEGER DEFAULT 0, amount INTEGER DEFAULT 0,
account INTEGER NOT NULL DEFAULT 0 REFERENCES money(id),
FOREIGN KEY (product_id) REFERENCES products(id) FOREIGN KEY (product_id) REFERENCES products(id)
); );
...@@ -121,7 +123,7 @@ CREATE TABLE drainage ( ...@@ -121,7 +123,7 @@ CREATE TABLE drainage (
CREATE TABLE my_db_settings ( CREATE TABLE my_db_settings (
id INTEGER PRIMARY KEY NOT NULL, id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL, name TEXT UNIQUE NOT NULL,
value value
); );
...@@ -258,14 +260,15 @@ BEFORE DELETE ON acquisitions_temp ...@@ -258,14 +260,15 @@ BEFORE DELETE ON acquisitions_temp
FOR EACH ROW FOR EACH ROW
WHEN (SELECT value = 1 FROM my_db_settings WHERE name = 'acquisitions_temp_transfer') WHEN (SELECT value = 1 FROM my_db_settings WHERE name = 'acquisitions_temp_transfer')
BEGIN BEGIN
INSERT INTO acquisitions (product_id, item_price, amount) INSERT INTO acquisitions (product_id, item_price, amount, account)
SELECT OLD.product_id, OLD.price, OLD.amount; SELECT OLD.product_id, OLD.price, OLD.amount,
(SELECT value FROM my_db_settings WHERE name = 'active_money_account');
UPDATE stock SET active = active + OLD.amount UPDATE stock SET active = active + OLD.amount
WHERE product_id = OLD.product_id; WHERE product_id = OLD.product_id;
UPDATE money SET amount = amount - (OLD.price * OLD.amount) UPDATE money SET amount = amount - (OLD.price * OLD.amount)
WHERE name = "Cash Drawer"; WHERE id = (SELECT value FROM my_db_settings WHERE name = 'active_money_account');
END; END;
/* This is already done in the before trigger /* This is already done in the before trigger
...@@ -302,8 +305,10 @@ AFTER DELETE ON big_buy ...@@ -302,8 +305,10 @@ AFTER DELETE ON big_buy
FOR EACH ROW FOR EACH ROW
WHEN (SELECT value = 1 FROM my_db_settings WHERE name = 'big_buy_transfer') WHEN (SELECT value = 1 FROM my_db_settings WHERE name = 'big_buy_transfer')
BEGIN BEGIN
INSERT INTO log (product_id, price) -- A regular "buy" is currently always done to the default
SELECT id, price FROM current_products WHERE id = OLD.product_id; -- account.
INSERT INTO log (product_id, price, account)
SELECT id, price, 1 FROM current_products WHERE id = OLD.product_id;
END; END;
/* /*
...@@ -382,7 +387,12 @@ VALUES ("Cash Drawer"), ("Vault"); ...@@ -382,7 +387,12 @@ VALUES ("Cash Drawer"), ("Vault");
INSERT INTO my_db_settings (name, value) INSERT INTO my_db_settings (name, value)
VALUES ("big_buy_transfer", 1), VALUES ("big_buy_transfer", 1),
("acquisitions_temp_transfer", 1); ("acquisitions_temp_transfer", 1),
-- Not actually a setting, but a DB technicality
("active_money_account", 1),
-- Account which money is drained from by default.
-- Should be possible for the user to set (from the GUI).
("default_money_account", 1);
-- This is mostly so that diff_help have something to insert -- This is mostly so that diff_help have something to insert
INSERT INTO products (name) INSERT INTO products (name)
......
PRAGMA FOREIGN_KEYS = 0;
BEGIN TRANSACTION;
-- Add new columns to tables.
ALTER TABLE log ADD COLUMN
account INTEGER NOT NULL DEFAULT 1 REFERENCES money(id);
ALTER TABLE acquisitions ADD COLUMN
account INTEGER NOT NULL DEFAULT 1 REFERENCES money(id);
-- Set values in new fields
UPDATE log SET account = 1;
UPDATE acquisitions SET account = 1;
-- Add new db_settings
DROP TABLE my_db_settings;
CREATE TABLE my_db_settings (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT UNIQUE NOT NULL,
value
);
INSERT INTO my_db_settings (name, value)
VALUES ("big_buy_transfer", 1),
("acquisitions_temp_transfer", 1),
-- Not actually a setting, but a DB technicality
("active_money_account", 1),
-- Account which money is drained from by default.
-- Should be possible for the user to set (from the GUI).
("default_money_account", 1);
-- Recreate triggers
DROP TRIGGER IF EXISTS before_acquisitions_transfer;
CREATE TRIGGER before_acquisitions_transfer
BEFORE DELETE ON acquisitions_temp
FOR EACH ROW
WHEN (SELECT value = 1 FROM my_db_settings WHERE name = 'acquisitions_temp_transfer')
BEGIN
INSERT INTO acquisitions (product_id, item_price, amount, account)
SELECT OLD.product_id, OLD.price, OLD.amount,
(SELECT value FROM my_db_settings WHERE name = 'active_money_account');
UPDATE stock SET active = active + OLD.amount
WHERE product_id = OLD.product_id;
UPDATE money SET amount = amount - (OLD.price * OLD.amount)
WHERE id = (SELECT value FROM my_db_settings WHERE name = 'active_money_account');
END;
DROP TRIGGER IF EXISTS after_big_buy_delete;
CREATE TRIGGER after_big_buy_delete
AFTER DELETE ON big_buy
FOR EACH ROW
WHEN (SELECT value = 1 FROM my_db_settings WHERE name = 'big_buy_transfer')
BEGIN
INSERT INTO log (product_id, price, account)
SELECT id, price, (SELECT value FROM my_db_settings WHERE name = 'default_money_account')
FROM current_products WHERE id = OLD.product_id;
END;
COMMIT;
PRAGMA FOREIGN_KEYS = 1;
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