Skip to content

Commit

Permalink
fix: 장부 생성 시 잔고가 정상 반영되도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarltj committed Feb 22, 2024
1 parent 46ee746 commit aba6993
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void update(
this.paymentDate = paymentDate;
}

public void updateBalance(int balance) {
this.balance = balance;
}

public static LedgerDetail of(
final Ledger ledger,
final User user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import com.moneymong.global.exception.enums.ErrorCode;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;

import com.moneymong.utils.AmountCalculatorByFundType;
import com.moneymong.utils.ModificationAmountCalculator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -70,6 +72,21 @@ public LedgerDetail createLedgerDetail(
AmountCalculatorByFundType.calculate(fundType, amount)
);

/**
* 가장 오래된 장부 내역인 경우 잔고를 amount 값으로 설정한다.
* 이전 내역이 있는 경우, 가장 가까운 시일에 생성된 장부 내역을 기준으로 잔고를 저장한다.
*/
Optional<LedgerDetail> mostRecentLedgerDetail = ledgerDetailRepository.findMostRecentLedgerDetail(paymentDate);

if (mostRecentLedgerDetail.isPresent()) {
LedgerDetail recentDetail = mostRecentLedgerDetail.get();

Integer newAmount = ModificationAmountCalculator.calculate(fundType, recentDetail.getBalance(), amount);
ledgerDetail.updateBalance(newAmount);
}else {
ledgerDetail.updateBalance(amount);
}

return ledgerDetailRepository.save(ledgerDetail);
}

Expand Down

0 comments on commit aba6993

Please sign in to comment.