From 2ce46f61d23f3a499020ad4484481056dbaab5e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@hornquist.se>
Date: Sat, 7 Apr 2018 01:45:01 +0200
Subject: [PATCH] started work on better diff system

---
 admin/mainwindow.cpp | 56 +++++++++++++++++++++++++++++++++-----------
 admin/mainwindow.h   |  2 ++
 admin/mainwindow.ui  | 14 +++++++++++
 create-db.sql        | 53 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 111 insertions(+), 14 deletions(-)

diff --git a/admin/mainwindow.cpp b/admin/mainwindow.cpp
index f5dc120..cd55cdb 100644
--- a/admin/mainwindow.cpp
+++ b/admin/mainwindow.cpp
@@ -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;
+}
diff --git a/admin/mainwindow.h b/admin/mainwindow.h
index a42ffca..cdbf0aa 100644
--- a/admin/mainwindow.h
+++ b/admin/mainwindow.h
@@ -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;
diff --git a/admin/mainwindow.ui b/admin/mainwindow.ui
index 2ae1d84..bd15459 100644
--- a/admin/mainwindow.ui
+++ b/admin/mainwindow.ui
@@ -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">
diff --git a/create-db.sql b/create-db.sql
index 50f6813..fdf716b 100644
--- a/create-db.sql
+++ b/create-db.sql
@@ -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;
-- 
GitLab