X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fjson%2FParsedLedgerAccount.java;h=04eff28f29e7cef40b9386bbc56bd5cd14e60012;hb=a87079ed41bdc3ad89fe8bd15dfba10e37b29b76;hp=dd19e27d2220b28dfa29967b5428d2618d997ed4;hpb=eccd11f1895e84dcc2a95db41934355311cebe2e;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.java index dd19e27d..04eff28f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2020 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -17,27 +17,94 @@ package net.ktnx.mobileledger.json; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import net.ktnx.mobileledger.async.RetrieveTransactionsTask; +import net.ktnx.mobileledger.model.LedgerAccount; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -@JsonIgnoreProperties(ignoreUnknown = true) -public class ParsedLedgerAccount { - private List aebalance; +public abstract class ParsedLedgerAccount { private String aname; - public ParsedLedgerAccount() { - } - public List getAebalance() { - return aebalance; - } - public void setAebalance(List aebalance) { - this.aebalance = aebalance; - } + private int anumpostings; + public abstract List getSimpleBalance(); public String getAname() { return aname; } public void setAname(String aname) { this.aname = aname; } + public int getAnumpostings() { + return anumpostings; + } + public void setAnumpostings(int anumpostings) { + this.anumpostings = anumpostings; + } + public LedgerAccount toLedgerAccount(RetrieveTransactionsTask task, + HashMap map) { + task.addNumberOfPostings(getAnumpostings()); + final String accName = getAname(); + LedgerAccount acc = map.get(accName); + if (acc != null) + throw new RuntimeException( + String.format("Account '%s' already present", acc.getName())); + String parentName = LedgerAccount.extractParentName(accName); + ArrayList createdParents = new ArrayList<>(); + LedgerAccount parent; + if (parentName == null) { + parent = null; + } + else { + parent = task.ensureAccountExists(parentName, map, createdParents); + parent.setHasSubAccounts(true); + } + acc = new LedgerAccount(task.getProfile(), accName, parent); + map.put(accName, acc); + String lastCurrency = null; + float lastCurrencyAmount = 0; + for (SimpleBalance b : getSimpleBalance()) { + task.throwIfCancelled(); + final String currency = b.getCommodity(); + final float amount = b.getAmount(); + if (currency.equals(lastCurrency)) { + lastCurrencyAmount += amount; + } + else { + if (lastCurrency != null) { + acc.addAmount(lastCurrencyAmount, lastCurrency); + } + lastCurrency = currency; + lastCurrencyAmount = amount; + } + } + if (lastCurrency != null) { + acc.addAmount(lastCurrencyAmount, lastCurrency); + } + for (LedgerAccount p : createdParents) + acc.propagateAmountsTo(p); + + return acc; + } + + static public class SimpleBalance { + private String commodity; + private float amount; + public SimpleBalance(String commodity, float amount) { + this.commodity = commodity; + this.amount = amount; + } + public String getCommodity() { + return commodity; + } + public void setCommodity(String commodity) { + this.commodity = commodity; + } + public float getAmount() { + return amount; + } + public void setAmount(float amount) { + this.amount = amount; + } + } }