changeset 130:10c6bd84eb32

Add "total balance" information in the UI, which displays the total sum balance of all people.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Aug 2017 16:53:05 +0300
parents f6685c2eb75d
children 931bb422a356
files src/main.cpp src/main.h src/mainwindow.ui
diffstat 3 files changed, 69 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.cpp	Wed Aug 23 14:11:27 2017 +0300
+++ b/src/main.cpp	Wed Aug 23 16:53:05 2017 +0300
@@ -84,7 +84,8 @@
     {
         QTextStream out(&fh);
         out <<
-            slDateTimeToLocal(QDateTime::currentDateTimeUtc()).toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
+            slDateTimeToLocal(QDateTime::currentDateTimeUtc()).
+            toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
             << " : " << msg << "\n";
         fh.close();
     }
@@ -914,7 +915,7 @@
 //
 void SyntilistaMainWindow::updatePersonList()
 {
-    static QString queryBase =
+    static const QString queryBase =
         "SELECT id,last_name,first_name,"
         "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance,"
         "updated FROM people";
@@ -976,6 +977,44 @@
     model_People->setHeaderData(2, Qt::Horizontal, tr("Etunimi"));
     model_People->setHeaderData(3, Qt::Horizontal, tr("Tase"));
     model_People->setHeaderData(4, Qt::Horizontal, tr("Muutettu"));
+
+    updateTotalBalance();
+}
+
+
+//
+// Update total balance value in the UI
+//
+void SyntilistaMainWindow::updateTotalBalance()
+{
+    // Update total balance value
+    QSqlQuery query;
+    query.prepare(QStringLiteral("SELECT TOTAL(value) FROM transactions AS balance"));
+    query.exec();
+    if (!slCheckAndReportSQLError("updatePersonList() get total balance query", query.lastError()))
+    {
+        slErrorMsg(
+            tr("SQL-tietokantavirhe"),
+            tr("Tietokantaa kyseltäessä tapahtui virhe."));
+    }
+    else
+    {
+        double balance;
+        QString tmp ;
+        if (query.next())
+        {
+            balance = query.value(0).toDouble();;
+            tmp = slMoneyValueToStr(balance);
+        }
+        else
+        {
+            balance = -1;
+            tmp = "?";
+        }
+
+        ui->label_TotalBalanceValue->setText(tmp);
+        ui->label_TotalBalanceValue->setStyleSheet(balance < 0 ? "color: red;" : "color: green;");
+    }
 }
 
 
@@ -1023,6 +1062,8 @@
 
     QSqlDatabase::database().commit();
 
+    updateTotalBalance();
+
     return 0;
 }
 
--- a/src/main.h	Wed Aug 23 14:11:27 2017 +0300
+++ b/src/main.h	Wed Aug 23 16:53:05 2017 +0300
@@ -131,6 +131,7 @@
     int  addTransaction(qint64 id, double value, PersonInfo &info);
     int  addTransactionGUI(qint64 id, bool debt, double value);
     void updatePersonList();
+    void updateTotalBalance();
 
     bool printDocumentPage(SLPageInfo &pinfo, const bool getPageInfo, const int page, QPainter *pt, QPrinter *printer);
 
--- a/src/mainwindow.ui	Wed Aug 23 14:11:27 2017 +0300
+++ b/src/mainwindow.ui	Wed Aug 23 16:53:05 2017 +0300
@@ -50,6 +50,31 @@
         </widget>
        </item>
        <item>
+        <layout class="QHBoxLayout" name="horizontalLayout_7">
+         <item>
+          <widget class="QLabel" name="label_TotalBalanceText">
+           <property name="text">
+            <string>Kokonaistase:</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QLabel" name="label_TotalBalanceValue">
+           <property name="text">
+            <string>12345</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QLabel" name="label_EUR2">
+           <property name="text">
+            <string>EUR</string>
+           </property>
+          </widget>
+         </item>
+        </layout>
+       </item>
+       <item>
         <layout class="QHBoxLayout" name="horizontalLayout">
          <item>
           <widget class="QPushButton" name="button_DeletePerson">