Commit 2ce46f61 authored by Hugo Hörnquist's avatar Hugo Hörnquist

started work on better diff system

parent 1539df10
......@@ -9,6 +9,7 @@
#include <QModelIndexList>
#include <QSqlError>
#include <QSqlIndex>
#include <QSqlRelationalDelegate>
#include <QHeaderView>
......@@ -236,31 +237,43 @@ MainWindow::MainWindow(QWidget *parent) :
// ==================================================
// TODO
clear_stockDiffTemp();
QList<int> inventoryCheckDisabled;
inventoryCheckDisabled << 0 << 1 << 2 << 4;
inventoryCheckDisabled << 0 << 1 << 2 << 3 << 4 << 6;
DisablingModel* inventoryModel = new DisablingModel(inventoryCheckDisabled);
inventoryModel->setTable("stock_diff_temp");
// inventoryModel->setTable("stock_diff_temp");
inventoryModel->setTable("diff_view");
inventoryModel->setHeaderData(0, Qt::Horizontal, "id");
inventoryModel->setHeaderData(1, Qt::Horizontal, "Namn");
inventoryModel->setHeaderData(2, Qt::Horizontal, "Förväntat");
inventoryModel->setHeaderData(3, Qt::Horizontal, "Faktiskt");
inventoryModel->setHeaderData(4, Qt::Horizontal, "Diff");
inventoryModel->setHeaderData(1, Qt::Horizontal, "product id");
inventoryModel->setHeaderData(2, Qt::Horizontal, "sale status");
inventoryModel->setHeaderData(3, Qt::Horizontal, "Namn");
inventoryModel->setHeaderData(4, Qt::Horizontal, "Förväntat");
inventoryModel->setHeaderData(5, Qt::Horizontal, "Faktiskt");
inventoryModel->setHeaderData(6, Qt::Horizontal, "Diff");
qDebug() << "Primary key" << inventoryModel->primaryKey();
inventoryModel->setEditStrategy(QSqlTableModel::OnFieldChange);
QObject::connect(inventoryModel, &QAbstractItemModel::dataChanged,
this, &MainWindow::on_inventoryModel_dataChanged);
inventoryModel->setRelation(1, QSqlRelation("products", "id", "name"));
// QObject::connect(inventoryModel, &QAbstractItemModel::dataChanged,
// this, &MainWindow::on_inventoryModel_dataChanged);
// inventoryModel->setRelation(1, QSqlRelation("products", "id", "name"));
inventoryModel->select();
ui->inventoryCheckView->setModel(inventoryModel);
ui->inventoryCheckView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->inventoryCheckView->setEditTriggers(QAbstractItemView::AllEditTriggers);
QIntOrNullValidator* intOrNullVal = new QIntOrNullValidator(INT_MIN, INT_MAX);
MaybeValidatorDelegate* maybeValDel = new MaybeValidatorDelegate(intOrNullVal);
ui->inventoryCheckView->setItemDelegateForColumn(3, maybeValDel);
ui->inventoryCheckView->setItemDelegateForColumn(5, maybeValDel);
ui->inventoryCheckView->hideColumn(0);
ui->inventoryCheckView->hideColumn(1);
ui->inventoryCheckView->hideColumn(2);
// TODO this line makes stuff dis-a-pear
inventoryModel->setData( inventoryModel->index(0, 5) , QVariant(5));
// --------------------------------------------------
......@@ -320,15 +333,18 @@ MainWindow::~MainWindow()
void MainWindow::on_inventoryModel_dataChanged(const QModelIndex &topLeft, const QModelIndex& /*bottomRight*/, const QVector<int>& /*roles*/)
{
if (topLeft.column() != 3) return;
/*
if (topLeft.column() != 4) return;
DisablingModel* model = (DisablingModel*) ui->inventoryCheckView->model();
int row = topLeft.row();
QModelIndex index = model->index(row, 2);
QModelIndex index = model->index(row, 3);
int expected = model->data(index).toInt();
int actual = model->data(topLeft).toInt();
int diff = actual - expected;
model->setData(model->index(row, 4), diff);
int diff = actual - expected
model->setData(model->index(row, 5), diff);
*/
qDebug() << "Data Chaned" << topLeft;
}
/*
......@@ -864,3 +880,15 @@ void MainWindow::on_productListSearch_textChanged(const QString &arg1)
{
setProductFilter();
}
void MainWindow::on_quickInventoryCheckBox_toggled(bool checked)
{
QSqlRelationalTableModel* model = (QSqlRelationalTableModel*) ui->inventoryCheckView->model();
// This repopulates
model->setFilter(
checked
? "sale_status in (0, 1)"
: "sale_status = 0 or expected != 0");
qDebug() << "Checkbox toggled" << checked;
}
......@@ -75,6 +75,8 @@ private slots:
void on_productListSearch_textChanged(const QString &arg1);
void on_quickInventoryCheckBox_toggled(bool checked);
private:
Ui::MainWindow *ui;
QSqlTableModel* model;
......
......@@ -280,6 +280,20 @@
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<widget class="QCheckBox" name="quickInventoryCheckBox">
<property name="text">
<string>Snabbläge</string>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_6">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="stockDiffReset">
<property name="sizePolicy">
......
......@@ -368,3 +368,56 @@ VALUES ("stock_diff_temp_transfer", 1),
INSERT INTO stock_diff_temp (product_id, expected_amount, diff)
SELECT id, 0, 0 FROM products LIMIT 1;
DELETE FROM stock_diff_temp;
------------------ New Stuff -------------------------------
CREATE TABLE diff_help (
id INTEGER PRIMARY KEY NOT NULL,
product_id INTEGER NOT NULL,
amount INTEGER,
FOREIGN KEY (product_id) REFERENCES products(id)
);
CREATE TRIGGER after_diff_help_delete_last
AFTER DELETE ON diff_help
FOR EACH ROW
WHEN (SELECT count (1) = 0 FROM diff_help)
BEGIN
INSERT INTO diff_help (product_id)
SELECT id FROM products p;
END;
-- create initial data
INSERT INTO diff_help (product_id)
SELECT id FROM products LIMIT 1;
DELETE FROM diff_help;
-- create view
CREATE VIEW diff_view AS
SELECT d.id AS id,
p.id AS product_id,
p.sale_status AS sale_status,
-- visible border
p.name AS name,
s.active AS expected,
d.amount AS actual,
s.active - d.amount as diff
FROM products p
INNER JOIN stock s ON p.id = s.product_id
INNER JOIN diff_help d ON p.id = d.product_id;
-- WHERE p.sale_status = 0 OR s.active != 0;
-- where p.sale_status in (0, 1);
CREATE TRIGGER diff_view_transfer
INSTEAD OF DELETE ON diff_view
BEGIN
UPDATE stock SET active = OLD.amount WHERE product_id = OLD.product_id;
INSERT INTO stock_diff (product_id, expected, actual)
SELECT OLD.product_id, OLD.expected, OLD.actual;
END;
CREATE TRIGGER update_view_trigger
INSTEAD OF UPDATE ON diff_view
BEGIN
UPDATE diff_help SET amount = NEW.actual WHERE id = NEW.id;
END;
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