Simplify UpdateNextMonths function
Add "create_if_not_exsits" to Database::GetAccountAmount
This commit is contained in:
@@ -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<int, std::vector<int> > 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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user