Simplify UpdateNextMonths function

Add "create_if_not_exsits" to Database::GetAccountAmount
This commit is contained in:
Grégory Soutadé 2013-02-17 18:58:32 +01:00
parent fcc7d09071
commit e0efe42b14
6 changed files with 48 additions and 93 deletions

View File

@ -1,4 +1,4 @@
v0.4 (17/01/2013) v0.4 (17/02/2013)
** User ** ** User **
Add icons for low resolution Add icons for low resolution
Sort categories using translated names Sort categories using translated names
@ -8,6 +8,8 @@ v0.4 (17/01/2013)
** Dev ** ** Dev **
Primitive handle of low resolutions Primitive handle of low resolutions
Rework GridAccount Rework GridAccount
Simplify UpdateNextMonths function
Add "create_if_not_exsits" to Database::GetAccountAmount
** Bugs ** ** Bugs **
Remove some bugs in GridAccount Remove some bugs in GridAccount

View File

@ -99,9 +99,9 @@ User* KissCount::GetUser()
return _user; 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) int KissCount::CalcAccountAmount(int id, int month, int year, bool* had_values)

View File

@ -77,7 +77,7 @@ public:
int MetaAmount(int id); int MetaAmount(int id);
int MetaPositiveAmount(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); void SetAccountAmount(int accountId, int month, int year, int value);
int CalcAccountAmount(int id, int month, int year, bool* had_values); int CalcAccountAmount(int id, int month, int year, bool* had_values);

View File

@ -379,7 +379,7 @@ void Database::LoadYear(User* user, int year)
query.clear(); 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; QSqlRecord set;
QString req; QString req;
@ -400,6 +400,7 @@ int Database::GetAccountAmount(int id, int month, int year, bool* had_value)
} }
else else
{ {
if (create_if_not_exsits)
SetAccountAmount(id, month, year, 0); SetAccountAmount(id, month, year, 0);
if (had_value) if (had_value)
*had_value = false; *had_value = false;

View File

@ -113,7 +113,7 @@ public:
int MetaAmount(int id); int MetaAmount(int id);
int MetaPositiveAmount(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); void SetAccountAmount(int accountId, int month, int year, int amount);
int CalcAccountAmount(int id, int month, int year, bool* had_values); int CalcAccountAmount(int id, int month, int year, bool* had_values);

View File

@ -1054,112 +1054,65 @@ void AccountPanel::OnUnGroup()
void AccountPanel::OnUpdateNextMonths() void AccountPanel::OnUpdateNextMonths()
{ {
int* deltas, *cur_amounts, amount; int *cur_amounts, amount, i;
int i, a;
User* user = _kiss->GetUser(); User* user = _kiss->GetUser();
bool had_values, accounts_updated = false; bool has_values, accounts_updated = false;
int last_month = 0, last_year = 0, account_updated = 0; int cur_month = 0, cur_year = 0, delta_computed = 0;
std::map<int, std::vector<int> > operations; int last_month, last_year;
bool *valid_account;
deltas = new int[user->_accounts.size()] ;
cur_amounts = 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++) for (i=0; i<(int)user->_accounts.size(); i++)
{ {
deltas[i] = _kiss->GetAccountAmount(user->_accounts[i].id, _curMonth, _curYear); cur_amounts[i] = _kiss->GetAccountAmount(user->_accounts[i].id, cur_month, cur_year, &has_values, false);
cur_amounts[i] = deltas[i] += _kiss->CalcAccountAmount(user->_accounts[i].id, _curMonth, _curYear, &had_values); valid_account[i] = has_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++;
}
if (!account_updated)
goto end;
last_month = _curMonth;
last_year = _curYear;
// Apply deltas
while (1) while (1)
{ {
account_updated = 0; delta_computed = 0;
if (last_month == 11) for (i=0; i<(int)user->_accounts.size(); i++)
{ {
last_month = 0; if (!valid_account[i]) continue;
last_year = last_year+1; 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 else
last_month++; cur_month++;
for (i=0; i<(int)user->_accounts.size(); i++) for (i=0; i<(int)user->_accounts.size(); i++)
{ {
if (deltas[i] == 0.0) continue; if (!valid_account[i]) continue;
amount = _kiss->GetAccountAmount(user->_accounts[i].id, cur_month, cur_year, &has_values, false);
_kiss->CalcAccountAmount(user->_accounts[i].id, last_month, last_year, &had_values); if (!has_values)
if (had_values)
account_updated++;
}
if (!account_updated) break;
account_updated = 0;
for (i=0; i<(int)user->_accounts.size(); i++)
{ {
if (deltas[i] == 0.0) continue; valid_account[i] = false;
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 (amount != cur_amounts[i])
if (!account_updated) break; {
accounts_updated = true; 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 (last_month == 0)
{
last_month = 11;
last_year--;
}
else
last_month--;
end:
if (accounts_updated) if (accounts_updated)
{ {
QString message = _("Accounts updated until ") + wxUI::months[last_month] + " " + QString::number(last_year); QString message = _("Accounts updated until ") + wxUI::months[last_month] + " " + QString::number(last_year);
@ -1169,7 +1122,6 @@ end:
else else
QMessageBox::information(0, "KissCount", _("Any account updated !")); QMessageBox::information(0, "KissCount", _("Any account updated !"));
delete[] deltas;
delete[] cur_amounts; delete[] cur_amounts;
} }