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 37d808e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 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 @@ -32,5 +32,5 @@ void bulkUpdateLedgerDetailBalance(
int amount
);

Optional<LedgerDetail> findMostRecentLedgerDetail(ZonedDateTime paymentDate);
Optional<LedgerDetail> findMostRecentLedgerDetail(Ledger ledger, ZonedDateTime paymentDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ public void bulkUpdateLedgerDetailBalance(Ledger ledger, ZonedDateTime paymentDa
}

@Override
public Optional<LedgerDetail> findMostRecentLedgerDetail(ZonedDateTime paymentDate) {
public Optional<LedgerDetail> findMostRecentLedgerDetail(Ledger ledger, ZonedDateTime paymentDate) {
return Optional.ofNullable(jpaQueryFactory
.selectFrom(ledgerDetail)
.where(ledgerDetail.paymentDate.lt(paymentDate))
.where(
ledgerDetail.ledger.eq(ledger),
ledgerDetail.paymentDate.lt(paymentDate)
)
.orderBy(ledgerDetail.paymentDate.desc())
.limit(1)
.fetchOne());
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(ledger, 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 37d808e

Please sign in to comment.