Skip to content

Commit

Permalink
[Feat/#80] 전월 대비 손익현황 비교 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnsugyeong committed Feb 5, 2024
1 parent fef5576 commit 9605250
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public static class IncomeStatementDto {
@Schema(description = "비용 백분율", example = "20")
private BigDecimal expensePercentage;

@Schema(description = "전월 대비 수익 변화율", example = "88")
private BigDecimal revenueChangePercentage;

@Schema(description = "전월 대비 비용 변화율", example = "-12")
private BigDecimal expenseChangePercentage;

}

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ public IncomeStatementDto getIncomeStatement(LocalDateTime startDate, LocalDateT
BigDecimal revenue = getSumOfRevenueAccounts(memberId, startDate, endDate);
BigDecimal expense = getSumOfExpenseAccounts(memberId, startDate, endDate);

LocalDateTime lastMonthStart = startDate.minusMonths(1).withDayOfMonth(1);
LocalDateTime lastMonthEnd = lastMonthStart.plusMonths(1).minusDays(1);

BigDecimal lastMonthRevenue = getSumOfRevenueAccounts(memberId, lastMonthStart, lastMonthEnd);
BigDecimal lastMonthExpense = getSumOfExpenseAccounts(memberId, lastMonthStart, lastMonthEnd);

BigDecimal revenueChangePercentage = calculateChangePercentage(revenue, lastMonthRevenue);
BigDecimal expenseChangePercentage = calculateChangePercentage(expense, lastMonthExpense);

BigDecimal expensePercentage = BigDecimal.ZERO;
BigDecimal revenuePercentage = BigDecimal.ZERO;

Expand All @@ -136,9 +145,20 @@ public IncomeStatementDto getIncomeStatement(LocalDateTime startDate, LocalDateT
.expense(expense)
.expensePercentage(expensePercentage)
.revenuePercentage(revenuePercentage)
.expenseChangePercentage(expenseChangePercentage)
.revenueChangePercentage(revenueChangePercentage)
.build();
}

private BigDecimal calculateChangePercentage(BigDecimal current, BigDecimal previous) {
if (previous.compareTo(BigDecimal.ZERO) == 0) {
return BigDecimal.ZERO; // 이전 값이 0이면 변화율을 계산할 수 없음
}
return current.subtract(previous)
.divide(previous, 4, RoundingMode.HALF_UP)
.multiply(new BigDecimal(100));
}

@Override
public IncomeStatementSummaryDto getIncomeStatementSummary(LocalDateTime startDate, LocalDateTime endDate) {
Long memberId = 1L;
Expand Down

0 comments on commit 9605250

Please sign in to comment.