- LedgerAccount acc = new LedgerAccount(parsedAccount.getAname());
- profile.storeAccount(acc);
- for (ParsedBalance b : parsedAccount.getAebalance()) {
- profile.storeAccountValue(acc.getName(), b.getAcommodity(),
- b.getAquantity().asFloat());
+ while (true) {
+ throwIfCancelled();
+ ParsedLedgerAccount parsedAccount = parser.nextAccount();
+ if (parsedAccount == null) break;
+
+ LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
+ if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
+ else acc.removeAmounts();
+
+ profile.storeAccount(db, acc);
+ String lastCurrency = null;
+ float lastCurrencyAmount = 0;
+ for (ParsedBalance b : parsedAccount.getAibalance()) {
+ final String currency = b.getAcommodity();
+ final float amount = b.getAquantity().asFloat();
+ if (currency.equals(lastCurrency)) lastCurrencyAmount += amount;
+ else {
+ if (lastCurrency != null) {
+ profile.storeAccountValue(db, acc.getName(), lastCurrency,
+ lastCurrencyAmount);
+ acc.addAmount(lastCurrencyAmount, lastCurrency);
+ }
+ lastCurrency = currency;
+ lastCurrencyAmount = amount;