comparison src/main.cpp @ 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 dc9fe580da42
comparison
equal deleted inserted replaced
129:f6685c2eb75d 130:10c6bd84eb32
82 QFile fh(filename); 82 QFile fh(filename);
83 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) 83 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
84 { 84 {
85 QTextStream out(&fh); 85 QTextStream out(&fh);
86 out << 86 out <<
87 slDateTimeToLocal(QDateTime::currentDateTimeUtc()).toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")) 87 slDateTimeToLocal(QDateTime::currentDateTimeUtc()).
88 toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
88 << " : " << msg << "\n"; 89 << " : " << msg << "\n";
89 fh.close(); 90 fh.close();
90 } 91 }
91 } 92 }
92 93
912 // Update visible person list/query based on the current 913 // Update visible person list/query based on the current
913 // filtering and sorting settings. 914 // filtering and sorting settings.
914 // 915 //
915 void SyntilistaMainWindow::updatePersonList() 916 void SyntilistaMainWindow::updatePersonList()
916 { 917 {
917 static QString queryBase = 918 static const QString queryBase =
918 "SELECT id,last_name,first_name," 919 "SELECT id,last_name,first_name,"
919 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance," 920 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance,"
920 "updated FROM people"; 921 "updated FROM people";
921 922
922 QString queryOrderDir, queryOrderBy; 923 QString queryOrderDir, queryOrderBy;
974 model_People->setHeaderData(0, Qt::Horizontal, tr("ID")); 975 model_People->setHeaderData(0, Qt::Horizontal, tr("ID"));
975 model_People->setHeaderData(1, Qt::Horizontal, tr("Sukunimi")); 976 model_People->setHeaderData(1, Qt::Horizontal, tr("Sukunimi"));
976 model_People->setHeaderData(2, Qt::Horizontal, tr("Etunimi")); 977 model_People->setHeaderData(2, Qt::Horizontal, tr("Etunimi"));
977 model_People->setHeaderData(3, Qt::Horizontal, tr("Tase")); 978 model_People->setHeaderData(3, Qt::Horizontal, tr("Tase"));
978 model_People->setHeaderData(4, Qt::Horizontal, tr("Muutettu")); 979 model_People->setHeaderData(4, Qt::Horizontal, tr("Muutettu"));
980
981 updateTotalBalance();
982 }
983
984
985 //
986 // Update total balance value in the UI
987 //
988 void SyntilistaMainWindow::updateTotalBalance()
989 {
990 // Update total balance value
991 QSqlQuery query;
992 query.prepare(QStringLiteral("SELECT TOTAL(value) FROM transactions AS balance"));
993 query.exec();
994 if (!slCheckAndReportSQLError("updatePersonList() get total balance query", query.lastError()))
995 {
996 slErrorMsg(
997 tr("SQL-tietokantavirhe"),
998 tr("Tietokantaa kyseltäessä tapahtui virhe."));
999 }
1000 else
1001 {
1002 double balance;
1003 QString tmp ;
1004 if (query.next())
1005 {
1006 balance = query.value(0).toDouble();;
1007 tmp = slMoneyValueToStr(balance);
1008 }
1009 else
1010 {
1011 balance = -1;
1012 tmp = "?";
1013 }
1014
1015 ui->label_TotalBalanceValue->setText(tmp);
1016 ui->label_TotalBalanceValue->setStyleSheet(balance < 0 ? "color: red;" : "color: green;");
1017 }
979 } 1018 }
980 1019
981 1020
982 // 1021 //
983 // Update the list of people when filter parameter changes 1022 // Update the list of people when filter parameter changes
1020 QSqlDatabase::database().rollback(); 1059 QSqlDatabase::database().rollback();
1021 return -3; 1060 return -3;
1022 } 1061 }
1023 1062
1024 QSqlDatabase::database().commit(); 1063 QSqlDatabase::database().commit();
1064
1065 updateTotalBalance();
1025 1066
1026 return 0; 1067 return 0;
1027 } 1068 }
1028 1069
1029 1070