use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.
the class SavingsServiceFacadeWebTier method closeSavingsAccount.
@Override
public void closeSavingsAccount(Long savingsId, String notes, SavingsWithdrawalDto closeAccountDto) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
SavingsBO savingsAccount = this.savingsDao.findById(savingsId);
savingsAccount.updateDetails(userContext);
PersonnelBO createdBy = this.personnelDao.findPersonnelById(userContext.getId());
LocalDate closureDate = closeAccountDto.getDateOfWithdrawal();
// Assumption that all previous interest postings occurred correctly
InterestScheduledEvent postingSchedule = savingsInterestScheduledEventFactory.createScheduledEventFrom(savingsAccount.getInterestPostingMeeting());
LocalDate nextPostingDate = new LocalDate(savingsAccount.getNextIntPostDate());
LocalDate startOfPostingPeriod = postingSchedule.findFirstDateOfPeriodForMatchingDate(nextPostingDate);
CalendarPeriod postingPeriodAtClosure;
if (startOfPostingPeriod.isAfter(closureDate)) {
postingPeriodAtClosure = new CalendarPeriod(closureDate, closureDate);
} else {
postingPeriodAtClosure = new CalendarPeriod(startOfPostingPeriod, closureDate);
}
List<EndOfDayDetail> allEndOfDayDetailsForAccount = savingsDao.retrieveAllEndOfDayDetailsFor(savingsAccount.getCurrency(), Long.valueOf(savingsId));
InterestPostingPeriodResult postingPeriodAtClosureResult = determinePostingPeriodResult(postingPeriodAtClosure, savingsAccount, allEndOfDayDetailsForAccount);
savingsAccount.postInterest(postingSchedule, postingPeriodAtClosureResult, createdBy);
AccountNotesEntity notesEntity = new AccountNotesEntity(new DateTimeService().getCurrentJavaSqlDate(), notes, createdBy, savingsAccount);
try {
CustomerBO customer = savingsAccount.getCustomer();
if (closeAccountDto.getCustomerId() != null) {
List<CustomerBO> clientList = new CustomerPersistence().getActiveAndOnHoldChildren(savingsAccount.getCustomer().getSearchId(), savingsAccount.getCustomer().getOfficeId(), CustomerLevel.CLIENT);
for (CustomerBO client : clientList) {
if (closeAccountDto.getCustomerId().intValue() == client.getCustomerId().intValue()) {
customer = client;
break;
}
}
}
Money amount = new Money(savingsAccount.getCurrency(), closeAccountDto.getAmount().toString());
PaymentTypeEntity paymentType = new PaymentTypeEntity(closeAccountDto.getModeOfPayment().shortValue());
Date receiptDate = null;
if (closeAccountDto.getDateOfReceipt() != null) {
receiptDate = closeAccountDto.getDateOfReceipt().toDateMidnight().toDate();
}
AccountPaymentEntity closeAccount = new AccountPaymentEntity(savingsAccount, amount, closeAccountDto.getReceiptId(), receiptDate, paymentType, closeAccountDto.getDateOfWithdrawal().toDateMidnight().toDate());
this.transactionHelper.startTransaction();
this.transactionHelper.beginAuditLoggingFor(savingsAccount);
savingsAccount.closeAccount(closeAccount, notesEntity, customer, createdBy);
this.savingsDao.save(savingsAccount);
this.transactionHelper.commitTransaction();
} catch (BusinessRuleException e) {
this.transactionHelper.rollbackTransaction();
throw new BusinessRuleException(e.getMessageKey(), e);
} catch (PersistenceException e) {
this.transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(e);
} finally {
this.transactionHelper.closeSession();
}
}
use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.
the class SavingsServiceFacadeWebTier method fundTransfer.
@Override
public void fundTransfer(FundTransferDto fundTransferDto) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
SavingsBO targetAcc = this.savingsDao.findBySystemId(fundTransferDto.getTargetGlobalAccountNum());
SavingsBO sourceAcc = this.savingsDao.findBySystemId(fundTransferDto.getSourceGlobalAccountNum());
SavingsDepositDto depositDto;
SavingsWithdrawalDto withdrawalDto;
// prepare data
try {
depositDto = new SavingsDepositDto(targetAcc.getAccountId().longValue(), targetAcc.getCustomer().getCustomerId().longValue(), fundTransferDto.getTrxnDate(), fundTransferDto.getAmount().doubleValue(), this.legacyAcceptedPaymentTypeDao.getSavingsTransferId().intValue(), fundTransferDto.getReceiptId(), fundTransferDto.getReceiptDate(), userContext.getPreferredLocale());
withdrawalDto = new SavingsWithdrawalDto(sourceAcc.getAccountId().longValue(), sourceAcc.getCustomer().getCustomerId().longValue(), fundTransferDto.getTrxnDate(), fundTransferDto.getAmount().doubleValue(), this.legacyAcceptedPaymentTypeDao.getSavingsTransferId().intValue(), fundTransferDto.getReceiptId(), fundTransferDto.getReceiptDate(), userContext.getPreferredLocale());
} catch (PersistenceException ex) {
throw new MifosRuntimeException(ex);
}
// transaction
try {
this.transactionHelper.startTransaction();
PaymentDto deposit = deposit(depositDto, true);
PaymentDto withdrawal = withdraw(withdrawalDto, true);
// connect the two payments
AccountPaymentEntity sourcePayment = sourceAcc.findPaymentById(withdrawal.getPaymentId());
AccountPaymentEntity targetPayment = targetAcc.findPaymentById(deposit.getPaymentId());
sourcePayment.setOtherTransferPayment(targetPayment);
targetPayment.setOtherTransferPayment(sourcePayment);
this.savingsDao.save(sourceAcc);
this.savingsDao.save(targetAcc);
this.transactionHelper.commitTransaction();
} catch (BusinessRuleException ex) {
this.transactionHelper.rollbackTransaction();
throw ex;
} catch (Exception ex) {
this.transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(ex);
} finally {
this.transactionHelper.closeSession();
}
}
use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.
the class LoanBOTest method repayInstallmentsShouldPopulateCalculatedInterestsForDIPBLoans.
@Test
public void repayInstallmentsShouldPopulateCalculatedInterestsForDIPBLoans() throws PersistenceException {
final LegacyLoanDao legacyLoanDao = mock(LegacyLoanDao.class);
final CustomerBO customerBO = mock(CustomerBO.class);
final LoanSummaryEntity loanSummaryEntity = mock(LoanSummaryEntity.class);
LoanBO loanBO = new LoanBO() {
@Override
public boolean isDecliningBalanceInterestRecalculation() {
return true;
}
@Override
public LegacyLoanDao getlegacyLoanDao() {
return legacyLoanDao;
}
@Override
public CustomerBO getCustomer() {
return customerBO;
}
@Override
public LoanSummaryEntity getLoanSummary() {
return loanSummaryEntity;
}
};
AccountActionTypes accountActionTypes = AccountActionTypes.LOAN_REPAYMENT;
AccountActionEntity accountActionEntity = mock(AccountActionEntity.class);
AccountPaymentEntity accountPaymentEntity = new AccountPaymentEntityBuilder().with(loanBO).build();
PersonnelBO user = new PersonnelBO();
Money extraInterestDue = new Money(rupee, "0.98");
Money interest = new Money(rupee, "10");
Money interestDue = new Money(rupee, "2.07");
when(legacyLoanDao.getPersistentObject(AccountActionEntity.class, accountActionTypes.getValue())).thenReturn(accountActionEntity);
when(loanScheduleEntity.getPrincipalDue()).thenReturn(new Money(rupee, "1000"));
when(loanScheduleEntity.getTotalFeeDueWithMiscFeeDue()).thenReturn(new Money(rupee, "10"));
when(loanScheduleEntity.getPenaltyDue()).thenReturn(new Money(rupee, "10"));
when(loanScheduleEntity.getPenalty()).thenReturn(new Money(rupee, "100"));
when(loanScheduleEntity.getExtraInterestDue()).thenReturn(extraInterestDue);
when(loanScheduleEntity.getExtraInterestPaid()).thenReturn(extraInterestDue);
when(loanScheduleEntity.getInterest()).thenReturn(interest);
when(loanScheduleEntity.getInterestDue()).thenReturn(interestDue);
loanBO.repayInstallment(loanScheduleEntity, accountPaymentEntity, accountActionTypes, user, "", interestDue);
Set<AccountTrxnEntity> accountTrxns = accountPaymentEntity.getAccountTrxns();
AccountTrxnEntity accountTrxnEntity = accountTrxns.toArray(new AccountTrxnEntity[accountTrxns.size()])[0];
LoanTrxnDetailEntity loanTrxnDetailEntity = (LoanTrxnDetailEntity) accountTrxnEntity;
assertThat(loanTrxnDetailEntity.getInterestAmount().getAmount().doubleValue(), is(3.05));
CalculatedInterestOnPayment calculatedInterestOnPayment = loanTrxnDetailEntity.getCalculatedInterestOnPayment();
assertNotNull(calculatedInterestOnPayment);
assertThat(calculatedInterestOnPayment.getExtraInterestPaid(), is(extraInterestDue));
assertThat(calculatedInterestOnPayment.getInterestDueTillPaid(), is(interestDue));
assertThat(calculatedInterestOnPayment.getOriginalInterest(), is(interest));
Mockito.verify(loanScheduleEntity).makeEarlyRepaymentEntries(LoanConstants.PAY_FEES_PENALTY_INTEREST, interestDue, accountPaymentEntity.getPaymentDate());
}
use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.
the class FinancialBusinessServiceIntegrationTest method testLoanWriteOffAccountingEntries.
@Test
public void testLoanWriteOffAccountingEntries() throws Exception {
loan = getLoanAccount();
loan.setUserContext(TestUtils.makeUser());
AccountPaymentEntity accountPaymentEntity = new AccountPaymentEntity(loan, TestUtils.createMoney(630), null, null, new PaymentTypeEntity(Short.valueOf("1")), new Date(System.currentTimeMillis()));
FinancialBusinessService financialBusinessService = new FinancialBusinessService();
AccountActionDateEntity accountActionDateEntity = loan.getAccountActionDate(Short.valueOf("1"));
PersonnelBO personnel = legacyPersonnelDao.getPersonnel(loan.getUserContext().getId());
LoanTrxnDetailEntity loanTrxnDetailEntity = new LoanTrxnDetailEntity(accountPaymentEntity, AccountActionTypes.WRITEOFF, accountActionDateEntity.getInstallmentId(), accountActionDateEntity.getActionDate(), personnel, new Date(System.currentTimeMillis()), ((LoanScheduleEntity) accountActionDateEntity).getPrincipal(), "Loan Written Off", null, ((LoanScheduleEntity) accountActionDateEntity).getPrincipal(), new Money(getCurrency()), new Money(getCurrency()), new Money(getCurrency()), new Money(getCurrency()), null, null);
accountPaymentEntity.addAccountTrxn(loanTrxnDetailEntity);
AccountTestUtils.addAccountPayment(accountPaymentEntity, loan);
financialBusinessService.buildAccountingEntries(loanTrxnDetailEntity);
TestObjectFactory.updateObject(loan);
Set<FinancialTransactionBO> finTrxnSet = loanTrxnDetailEntity.getFinancialTransactions();
Assert.assertEquals(finTrxnSet.size(), 2);
for (FinancialTransactionBO finTrxn : finTrxnSet) {
if (finTrxn.getFinancialAction().getId().equals(Short.valueOf("22")) && finTrxn.isCreditEntry()) {
Assert.assertEquals(finTrxn.getPostedAmount(), TestUtils.createMoney("100"));
Assert.assertEquals(finTrxn.getBalanceAmount(), TestUtils.createMoney("100"));
Assert.assertEquals(finTrxn.getGlcode().getGlcodeId(), Short.valueOf("22"));
} else if (finTrxn.getFinancialAction().getId().equals(Short.valueOf("22")) && finTrxn.isDebitEntry()) {
Assert.assertEquals(finTrxn.getPostedAmount(), TestUtils.createMoney("100"));
Assert.assertEquals(finTrxn.getBalanceAmount(), TestUtils.createMoney("100"));
Assert.assertEquals(finTrxn.getGlcode().getGlcodeId(), Short.valueOf("24"));
} else {
//--fail("There should not be any other entry");
}
}
}
use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.
the class SavingsCloseTest method whenClosingAccountShouldWithdrawRemainingBalanceOnAccount.
@Test
public void whenClosingAccountShouldWithdrawRemainingBalanceOnAccount() {
Money remainingBalance = TestUtils.createMoney("100");
savingsAccount = new SavingsAccountBuilder().active().withSavingsProduct(savingsProduct).withCustomer(client).withBalanceOf(remainingBalance).build();
AccountPaymentEntity payment = new AccountPaymentEntityBuilder().with(savingsAccount).with(remainingBalance).build();
AccountNotesEntity notes = new AccountNotesEntityBuilder().build();
CustomerBO customer = new ClientBuilder().buildForUnitTests();
PersonnelBO loggedInUser = new PersonnelBuilder().build();
// pre verification
assertTrue(savingsAccount.getAccountPayments().isEmpty());
// exercise test
savingsAccount.closeAccount(payment, notes, customer, loggedInUser);
// verification
assertFalse(savingsAccount.getAccountPayments().isEmpty());
AccountPaymentEntity withdrawalPayment = savingsAccount.getAccountPayments().get(0);
assertThat(withdrawalPayment.getAmount(), is(TestUtils.createMoney("100")));
}
Aggregations