Skip to content

Commit

Permalink
Removed unused payment account/remarks from credit accounts; added bi…
Browse files Browse the repository at this point in the history
…lling month offset
  • Loading branch information
ystxn committed Dec 11, 2024
1 parent f92ce5d commit f755a53
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 47 deletions.
6 changes: 2 additions & 4 deletions src/main/java/tech/sledger/endpoints/AccountEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public Account addAccount(Authentication auth, @RequestBody NewAccount newAccoun
.name(newAccount.getName())
.type(newAccount.getType())
.billingCycle(newAccount.getBillingCycle())
.paymentAccountId(newAccount.getPaymentAccount())
.paymentRemarks(newAccount.getPaymentRemarks())
.billingMonthOffset(newAccount.getBillingMonthOffset())
.owner(user)
.multiCurrency(newAccount.isMultiCurrency())
.build();
Expand Down Expand Up @@ -105,8 +104,7 @@ public Account updateAccount(
creditAccount.setName(editAccount.getName());
creditAccount.setMultiCurrency(editAccount.isMultiCurrency());
creditAccount.setBillingCycle(editAccount.getBillingCycle());
creditAccount.setPaymentAccountId(editAccount.getPaymentAccount());
creditAccount.setPaymentRemarks(editAccount.getPaymentRemarks());
creditAccount.setBillingMonthOffset(editAccount.getBillingMonthOffset());
} else if (account instanceof CashAccount cashAccount) {
cashAccount.setIssuer(issuer);
cashAccount.setName(editAccount.getName());
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/tech/sledger/model/account/CreditAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
@Document("account")
public class CreditAccount extends CashAccount {
private int billingCycle;
private long paymentAccountId;
private String paymentRemarks;
private int billingMonthOffset;
}
3 changes: 1 addition & 2 deletions src/main/java/tech/sledger/model/account/NewAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ public class NewAccount {
private AccountType type;
private long issuerId;
private int billingCycle;
private int billingMonthOffset;
private boolean multiCurrency;
private long paymentAccount;
private String paymentRemarks;
private BigDecimal ordinaryRatio;
private BigDecimal specialRatio;
private BigDecimal medisaveRatio;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/tech/sledger/model/dto/AccountDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public class AccountDTO {
private long transactions;
private boolean multiCurrency;
private Integer billingCycle;
private Long paymentAccountId;
private String paymentRemarks;
private Integer billingMonthOffset;
private BigDecimal ordinaryRatio;
private BigDecimal specialRatio;
private BigDecimal medisaveRatio;
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/tech/sledger/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ public Account add(Account account) {
long id = (previous == null) ? 1 : previous.getId() + 1;
account.setId(id);
account.setSortOrder((previous == null) ? 0 : previous.getSortOrder() + 1);

if (account instanceof CreditAccount creditAccount && creditAccount.getPaymentAccountId() > 0) {
Account paymentAccount = get(creditAccount.getPaymentAccountId());
if (paymentAccount == null || !account.getOwner().equals(paymentAccount.getOwner())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid payment account id");
}
}
return accountRepo.save(account);
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/tech/sledger/service/importer/Importer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ default Template matchTemplate(String remarks, List<Template> templates) {
);
}

default Instant getBillingMonth(LocalDate date, CreditAccount account) {
default Instant getBillingMonth(LocalDate date, CreditAccount account, String category) {
int cycle = account.getBillingCycle();
LocalDate localMonth = date.withDayOfMonth(1);
long offset = account.getBillingMonthOffset();
if (date.getDayOfMonth() < cycle) {
localMonth = localMonth.minusMonths(1).withDayOfMonth(1);
offset -= 1;
}
return localMonth.atStartOfDay(ZoneOffset.UTC).toInstant();
if (category.equalsIgnoreCase("Credit Card Bill")) {
offset -= 1;
}
return localMonth
.plusMonths(offset)
.withDayOfMonth(1)
.atStartOfDay(ZoneOffset.UTC)
.toInstant();
}

private String toProperCase(String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ private List<Transaction> processCredit(List<String[]> data, Account account, Li
for (String[] row : data) {
LocalDate localDate = LocalDate.parse(row[0], dateFormat);
Instant date = localDate.atStartOfDay(ZoneOffset.UTC).toInstant();
Instant billingMonth = getBillingMonth(localDate, (CreditAccount) account);
Template template = matchTemplate(cleanCreditRemarks(row[1]), templates);
Instant billingMonth = getBillingMonth(localDate, (CreditAccount) account, template.getCategory());
BigDecimal debit = parseDecimal(row[2]);
BigDecimal credit = parseDecimal(row[3]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ private List<Transaction> processCredit(
List<Object> dataRow = data.get(i);
LocalDate localDate = LocalDate.parse(dataRow.getFirst().toString(), dateFormat);
Instant date = localDate.atStartOfDay(ZoneOffset.UTC).toInstant();
Instant billingMonth = getBillingMonth(localDate, (CreditAccount) account);
String remarks = dataRow.get(2).toString().trim();
BigDecimal amount = parseDecimal(dataRow.get(6).toString());
Template template = matchTemplate(remarks, templates);
Instant billingMonth = getBillingMonth(localDate, (CreditAccount) account, template.getCategory());

if (amount.compareTo(BigDecimal.ZERO) == 0) {
continue;
Expand Down
23 changes: 1 addition & 22 deletions src/test/java/tech/sledger/AccountTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public void setup() {
"name", "creditAccount",
"type", "Credit",
"issuerId", accountIssuer.getId(),
"billingCycle", 15L,
"paymentAccount", myAccount.getId()
"billingCycle", 15L
)),
new HashMap<>(Map.of(
"name", "creditAccountNoPayment",
Expand Down Expand Up @@ -139,26 +138,6 @@ public void badInput() throws Exception {
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail").value("No such issuer"));

Map<String, ?> badPaymentAccount = Map.of(
"name", "creditAccount",
"type", "Credit",
"issuerId", accountIssuer.getId(),
"paymentAccount", 1234567
);
mvc.perform(request(POST, "/api/account", badPaymentAccount))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail").value("Invalid payment account id"));

Map<String, ?> badPaymentAccount2 = Map.of(
"name", "creditAccount",
"type", "Credit",
"issuerId", accountIssuer.getId(),
"paymentAccount", someoneElsesAccount.getId()
);
mvc.perform(request(POST, "/api/account", badPaymentAccount2))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail").value("Invalid payment account id"));

Map<String, Object> noIssuerAccount = Map.of("name", "badAccount");
mvc.perform(request(POST, "/api/account", noIssuerAccount))
.andExpect(status().isBadRequest());
Expand Down
1 change: 0 additions & 1 deletion src/test/java/tech/sledger/DashTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public void setup() throws Exception {
.type(AccountType.Credit)
.billingCycle(1)
.multiCurrency(false)
.paymentRemarks("Credit Card Bill")
.build()).getId();

List<Map<String, ?>> transactions = List.of(
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/tech/sledger/TransactionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ public void setup() {
.owner(user)
.type(AccountType.Credit)
.billingCycle(1)
.paymentAccountId(cashAccountId)
.billingMonthOffset(0)
.multiCurrency(false)
.paymentRemarks("Credit Card Bill")
.build()).getId();

cpfAccountId = accountService.add(CPFAccount.builder()
Expand Down

0 comments on commit f755a53

Please sign in to comment.