Skip to content

Commit 9607314

Browse files
committed
Allow shares/commodities to have decimal values
1 parent 26bf2b1 commit 9607314

5 files changed

Lines changed: 17 additions & 10 deletions

File tree

src/main/java/com/hz/mymoney/data/models/internal/Account.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.math.BigDecimal;
99
import java.math.RoundingMode;
10+
import java.text.DecimalFormat;
1011
import java.time.LocalDate;
1112
import java.util.List;
1213
import java.util.PriorityQueue;
@@ -93,9 +94,9 @@ public boolean hasMovements() {
9394
private BigDecimal addMovementAmount(Movement movement) {
9495
if (isShareAccount) {
9596
if (movement.split()) {
96-
return movement.amount().setScale(0, RoundingMode.HALF_UP);
97+
return movement.amount().setScale(2, RoundingMode.HALF_UP);
9798
}
98-
return cachedAmount.add(movement.amount()).setScale(0, RoundingMode.HALF_UP);
99+
return cachedAmount.add(movement.amount()).setScale(2, RoundingMode.HALF_UP);
99100
}
100101
return cachedAmount.add(movement.amount());
101102
}
@@ -108,7 +109,7 @@ private BigDecimal addMovementAmount(Movement movement) {
108109
for (Movement movement : movements) {
109110
sum = movement.split() ? movement.amount() : sum.add(movement.amount());
110111
}
111-
return sum.setScale(0, RoundingMode.HALF_UP);
112+
return sum.setScale(2, RoundingMode.HALF_UP);
112113
}
113114
return sumBigDecimals(movements.stream()
114115
.map(Movement::amount));
@@ -202,7 +203,8 @@ public String getSimpleName() {
202203
// Option not share?
203204
return getTotalAmount() + " options (" + getCode() + ") for " + getCode().substring(0,3);
204205
}
205-
return getTotalAmount() + " shares in " + simpleName;
206+
;
207+
return new DecimalFormat("#.##").format(getTotalAmount()) + " shares in " + simpleName;
206208
} else if (isManagedFund()) {
207209
return simpleName + " Fund";
208210
}

src/main/java/com/hz/mymoney/ui/models/InvestmentSummary.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import java.math.BigDecimal;
44
import java.math.RoundingMode;
5+
import java.text.DecimalFormat;
56
import java.time.LocalDate;
67

78
public record InvestmentSummary(
89
String code,
9-
int count,
10+
BigDecimal count,
1011
BigDecimal lastPrice,
1112
BigDecimal balance,
1213
BigDecimal costBase,
@@ -18,6 +19,10 @@ public record InvestmentSummary(
1819
String note
1920
) {
2021

22+
public String quantity() {
23+
return new DecimalFormat("#,###.##").format(count);
24+
}
25+
2126
public boolean hasNote() {
2227
return note != null;
2328
}
@@ -27,7 +32,7 @@ public boolean isProfit() {
2732
}
2833

2934
public boolean isClosed() {
30-
return count == 0;
35+
return count.equals(BigDecimal.ZERO);
3136
}
3237

3338
public BigDecimal yearlyReturn() {

src/main/java/com/hz/mymoney/ui/models/templates/InvestmentsTemplateData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class InvestmentsTemplateData {
1616
public final String mode;
1717

1818
public List<InvestmentTotal> getInvestmentTotals() {
19-
return investmentSummaries.stream().map(investmentSummary -> new InvestmentTotal(investmentSummary.code(), investmentSummary.count())).toList();
19+
return investmentSummaries.stream().map(investmentSummary -> new InvestmentTotal(investmentSummary.code(), investmentSummary.count().intValue())).toList();
2020
}
2121

2222
public List<MarketValue> getMarketValues() {

src/main/java/com/hz/mymoney/ui/services/UiModelBuilderService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,13 @@ private InvestmentSummary mapAccountToShareSummary(com.hz.mymoney.data.models.in
320320

321321
PriorityQueue<Movement> movementsForCode = account.getMovementsForCode(account.getCode());
322322
if (movementsForCode.isEmpty()) {
323-
return new InvestmentSummary(account.getCode(), account.getTotalAmount().intValue(), shareValue, currentValue, account.getCostBase(), account.getSales(), earnings, netProfitLoss, LocalDate.of(2000,1,1), LocalDate.now(), getNextInvestmentIncomeNote(account.getCode()));
323+
return new InvestmentSummary(account.getCode(), account.getTotalAmount(), shareValue, currentValue, account.getCostBase(), account.getSales(), earnings, netProfitLoss, LocalDate.of(2000,1,1), LocalDate.now(), getNextInvestmentIncomeNote(account.getCode()));
324324
}
325325

326326
LocalDate earliestMovementDate = movementsForCode.stream().findFirst().orElseThrow().date();
327327
LocalDate lastMovementDate = movementsForCode.stream().toList().getLast().date();
328328

329-
return new InvestmentSummary(account.getCode(), account.getTotalAmount().intValue(), shareValue, currentValue, account.getCostBase(), account.getSales(), earnings, netProfitLoss, earliestMovementDate, lastMovementDate, getNextInvestmentIncomeNote(account.getCode()));
329+
return new InvestmentSummary(account.getCode(), account.getTotalAmount(), shareValue, currentValue, account.getCostBase(), account.getSales(), earnings, netProfitLoss, earliestMovementDate, lastMovementDate, getNextInvestmentIncomeNote(account.getCode()));
330330
}
331331

332332
private NetAssetLiabilityPosition createNetAssetLiabilityPosition(ChartOfAccounts chartOfAccounts, boolean includeNote) {

src/main/resources/templates/fragments/Investment.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
th:each="investment : ${investmentsData.investmentSummaries}">
2424
<td th:text="${investment.code}" th:attr="data-tooltip = ${investment.hasNote()} ? ${investment.note} : _"></td>
2525
<td th:text="${#temporals.format(investment.firstPurchase, 'dd MMM yyyy')}"></td>
26-
<td class="right aligned" th:if="${#strings.contains(header,'Current')}" th:text="${investment.count}"></td>
26+
<td class="right aligned" th:if="${#strings.contains(header,'Current')}" th:text="${investment.quantity()}"></td>
2727
<td class="right aligned" th:if="${#strings.contains(header,'Current')}"
2828
th:text="${#numbers.formatCurrency(investment.lastPrice)}"></td>
2929
<td class="right aligned big-screen-table" th:text="${#numbers.formatCurrency(investment.costBase)}"></td>

0 commit comments

Comments
 (0)