From e0efe42b14e69b003312abfc4eb34c4aef79aa2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Sun, 17 Feb 2013 18:58:32 +0100 Subject: [PATCH] Simplify UpdateNextMonths function Add "create_if_not_exsits" to Database::GetAccountAmount --- ChangeLog | 4 +- src/controller/KissCount.cpp | 4 +- src/controller/KissCount.hpp | 2 +- src/model/Database.cpp | 5 +- src/model/Database.hpp | 2 +- src/view/AccountPanel.cpp | 124 +++++++++++------------------------ 6 files changed, 48 insertions(+), 93 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1af1317..f7d8f45 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -v0.4 (17/01/2013) +v0.4 (17/02/2013) ** User ** Add icons for low resolution Sort categories using translated names @@ -8,6 +8,8 @@ v0.4 (17/01/2013) ** Dev ** Primitive handle of low resolutions Rework GridAccount + Simplify UpdateNextMonths function + Add "create_if_not_exsits" to Database::GetAccountAmount ** Bugs ** Remove some bugs in GridAccount diff --git a/src/controller/KissCount.cpp b/src/controller/KissCount.cpp index a687f00..3697276 100644 --- a/src/controller/KissCount.cpp +++ b/src/controller/KissCount.cpp @@ -99,9 +99,9 @@ User* KissCount::GetUser() return _user; } -int KissCount::GetAccountAmount(int id, int month, int year, bool* had_value) +int KissCount::GetAccountAmount(int id, int month, int year, bool* had_value, bool create_if_not_exsits) { - return _db->GetAccountAmount(id, month, year, had_value); + return _db->GetAccountAmount(id, month, year, had_value, create_if_not_exsits); } int KissCount::CalcAccountAmount(int id, int month, int year, bool* had_values) diff --git a/src/controller/KissCount.hpp b/src/controller/KissCount.hpp index f7c8b88..74fe6b2 100644 --- a/src/controller/KissCount.hpp +++ b/src/controller/KissCount.hpp @@ -77,7 +77,7 @@ public: int MetaAmount(int id); int MetaPositiveAmount(int id); - int GetAccountAmount(int id, int month, int year, bool* had_values=0); + int GetAccountAmount(int id, int month, int year, bool* had_values=0, bool create_if_not_exsits=true); void SetAccountAmount(int accountId, int month, int year, int value); int CalcAccountAmount(int id, int month, int year, bool* had_values); diff --git a/src/model/Database.cpp b/src/model/Database.cpp index d411daa..d20a29f 100644 --- a/src/model/Database.cpp +++ b/src/model/Database.cpp @@ -379,7 +379,7 @@ void Database::LoadYear(User* user, int year) query.clear(); } -int Database::GetAccountAmount(int id, int month, int year, bool* had_value) +int Database::GetAccountAmount(int id, int month, int year, bool* had_value, bool create_if_not_exsits) { QSqlRecord set; QString req; @@ -400,7 +400,8 @@ int Database::GetAccountAmount(int id, int month, int year, bool* had_value) } else { - SetAccountAmount(id, month, year, 0); + if (create_if_not_exsits) + SetAccountAmount(id, month, year, 0); if (had_value) *had_value = false; } diff --git a/src/model/Database.hpp b/src/model/Database.hpp index 639b993..4ac3797 100644 --- a/src/model/Database.hpp +++ b/src/model/Database.hpp @@ -113,7 +113,7 @@ public: int MetaAmount(int id); int MetaPositiveAmount(int id); - int GetAccountAmount(int id, int month, int year, bool* had_value=0); + int GetAccountAmount(int id, int month, int year, bool* had_value=0, bool create_if_not_exsits=true); void SetAccountAmount(int accountId, int month, int year, int amount); int CalcAccountAmount(int id, int month, int year, bool* had_values); diff --git a/src/view/AccountPanel.cpp b/src/view/AccountPanel.cpp index 8dd08b9..11ee723 100644 --- a/src/view/AccountPanel.cpp +++ b/src/view/AccountPanel.cpp @@ -1054,112 +1054,65 @@ void AccountPanel::OnUnGroup() void AccountPanel::OnUpdateNextMonths() { - int* deltas, *cur_amounts, amount; - int i, a; + int *cur_amounts, amount, i; User* user = _kiss->GetUser(); - bool had_values, accounts_updated = false; - int last_month = 0, last_year = 0, account_updated = 0; - std::map > operations; + bool has_values, accounts_updated = false; + int cur_month = 0, cur_year = 0, delta_computed = 0; + int last_month, last_year; + bool *valid_account; - deltas = new int[user->_accounts.size()] ; cur_amounts = new int[user->_accounts.size()] ; + valid_account = new bool[user->_accounts.size()] ; - operations = _kiss->GetAllOperations(); + cur_month = _curMonth; + cur_year = _curYear; - if (_curMonth == 11) - { - last_month = 0; - last_year = _curYear+1; - } - else - { - last_month = _curMonth+1; - last_year = _curYear; - } - - // Compute deltas for (i=0; i<(int)user->_accounts.size(); i++) { - deltas[i] = _kiss->GetAccountAmount(user->_accounts[i].id, _curMonth, _curYear); - cur_amounts[i] = deltas[i] += _kiss->CalcAccountAmount(user->_accounts[i].id, _curMonth, _curYear, &had_values); - - for (a=0; a<(int)operations[last_year].size(); a++) - if (operations[last_year][a] == last_month) break; - - if (a == (int)operations[last_year].size()) - { - deltas[i] = 0; - continue; - } - - amount = _kiss->GetAccountAmount(user->_accounts[i].id, last_month, last_year); - - deltas[i] -= amount; - - if (deltas[i]) - account_updated++; + cur_amounts[i] = _kiss->GetAccountAmount(user->_accounts[i].id, cur_month, cur_year, &has_values, false); + valid_account[i] = has_values; } - if (!account_updated) - goto end; - - last_month = _curMonth; - last_year = _curYear; - - // Apply deltas while (1) { - account_updated = 0; + delta_computed = 0; - if (last_month == 11) + for (i=0; i<(int)user->_accounts.size(); i++) { - last_month = 0; - last_year = last_year+1; + if (!valid_account[i]) continue; + cur_amounts[i] += _kiss->CalcAccountAmount(user->_accounts[i].id, cur_month, cur_year, &has_values); + if (has_values) + delta_computed++; + } + + if (!delta_computed) break; + + if (cur_month == 11) + { + cur_month = 0; + cur_year++; } else - last_month++; + cur_month++; for (i=0; i<(int)user->_accounts.size(); i++) { - if (deltas[i] == 0.0) continue; - - _kiss->CalcAccountAmount(user->_accounts[i].id, last_month, last_year, &had_values); - if (had_values) - account_updated++; + if (!valid_account[i]) continue; + amount = _kiss->GetAccountAmount(user->_accounts[i].id, cur_month, cur_year, &has_values, false); + if (!has_values) + { + valid_account[i] = false; + continue; + } + if (amount != cur_amounts[i]) + { + accounts_updated = true; + _kiss->SetAccountAmount(user->_accounts[i].id, cur_month, cur_year, cur_amounts[i]); + last_month = cur_month; last_year = cur_year; + } } - - if (!account_updated) break; - - account_updated = 0; - - for (i=0; i<(int)user->_accounts.size(); i++) - { - if (deltas[i] == 0.0) continue; - - amount = _kiss->GetAccountAmount(user->_accounts[i].id, last_month, last_year); - if ((cur_amounts[i] - amount) != deltas[i]) continue; - - cur_amounts[i] = amount + deltas[i]; - _kiss->SetAccountAmount(user->_accounts[i].id, last_month, last_year, cur_amounts[i]); - cur_amounts[i] += _kiss->CalcAccountAmount(user->_accounts[i].id, last_month, last_year, &had_values); - - account_updated++; - } - - if (!account_updated) break; - - accounts_updated = true; } - if (last_month == 0) - { - last_month = 11; - last_year--; - } - else - last_month--; - -end: if (accounts_updated) { QString message = _("Accounts updated until ") + wxUI::months[last_month] + " " + QString::number(last_year); @@ -1169,7 +1122,6 @@ end: else QMessageBox::information(0, "KissCount", _("Any account updated !")); - delete[] deltas; delete[] cur_amounts; }