use of org.mifos.accounts.penalties.business.RatePenaltyBO in project head by mifos.
the class ApplyPenaltyToLoanAccountsHelper method addRatePenalty.
private void addRatePenalty(final AccountPenaltiesEntity penaltyEntity, final LoanBO loanAccount, final LoanScheduleEntity loanScheduleEntity) {
RatePenaltyBO penalty = (RatePenaltyBO) penaltyEntity.getPenalty();
Double radio = penaltyEntity.getAccountPenaltyAmount().getAmount().doubleValue() / 100.0d;
Money charge = null;
if (penalty.isOutstandingPrincipalAmount()) {
charge = loanAccount.getLoanSummary().getOriginalPrincipal().multiply(radio);
} else if (penalty.isOutstandingLoanAmount()) {
charge = loanAccount.getLoanSummary().getOutstandingBalance().multiply(radio);
} else if (penalty.isOverdueAmountDue()) {
charge = loanScheduleEntity.getTotalDue().multiply(radio);
} else if (penalty.isOverduePrincipal()) {
charge = loanScheduleEntity.getPrincipalDue().multiply(radio);
} else {
charge = Money.zero();
}
Money totalPenalty = loanAccount.getTotalPenalty(charge.getCurrency(), penalty.getPenaltyId());
charge = verifyLimits(totalPenalty, charge, penalty.getMinimumLimit() / loanAccount.calcFactorOfEntireLoan().doubleValue(), penalty.getMaximumLimit() / loanAccount.calcFactorOfEntireLoan().doubleValue());
if (charge.isGreaterThanZero()) {
loanAccount.applyPenalty(charge, loanScheduleEntity.getInstallmentId(), penaltyEntity, currentDate);
}
try {
StaticHibernateUtil.startTransaction();
StaticHibernateUtil.getSessionTL().update(loanAccount);
StaticHibernateUtil.commitTransaction();
} catch (Exception e) {
getLogger().error(e.getMessage());
StaticHibernateUtil.rollbackTransaction();
}
}
use of org.mifos.accounts.penalties.business.RatePenaltyBO in project head by mifos.
the class WebTierAccountServiceFacade method applyGroupCharge.
@Override
public void applyGroupCharge(Map<Integer, String> idsAndValues, Short chargeId, boolean isPenaltyType) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
TreeMap<Integer, String> idsAndValueAsTreeMap = new TreeMap<Integer, String>(idsAndValues);
try {
AccountBO parentAccount = ((LoanBO) legacyAccountDao.getAccount(new AccountBusinessService().getAccount(idsAndValueAsTreeMap.firstKey()).getAccountId())).getParentAccount();
BigDecimal parentAmount = ((LoanBO) parentAccount).getLoanAmount().getAmount();
BigDecimal membersAmount = BigDecimal.ZERO;
for (Map.Entry<Integer, String> entry : idsAndValues.entrySet()) {
LoanBO individual = loanDao.findById(entry.getKey());
Double chargeAmount = Double.valueOf(entry.getValue());
if (chargeAmount.equals(0.0)) {
continue;
}
membersAmount = membersAmount.add(individual.getLoanAmount().getAmount());
individual.updateDetails(userContext);
if (isPenaltyType && !chargeId.equals(Short.valueOf(AccountConstants.MISC_PENALTY))) {
PenaltyBO penalty = this.penaltyDao.findPenaltyById(chargeId.intValue());
individual.addAccountPenalty(new AccountPenaltiesEntity(individual, penalty, chargeAmount));
} else {
individual.applyCharge(chargeId, chargeAmount);
}
}
boolean isRateCharge = false;
if (!chargeId.equals(Short.valueOf(AccountConstants.MISC_FEES)) && !chargeId.equals(Short.valueOf(AccountConstants.MISC_PENALTY))) {
if (isPenaltyType) {
PenaltyBO penalty = this.penaltyDao.findPenaltyById(chargeId.intValue());
if (penalty instanceof RatePenaltyBO) {
isRateCharge = true;
}
} else {
FeeBO fee = feeDao.findById(chargeId);
if (fee.getFeeType().equals(RateAmountFlag.RATE)) {
isRateCharge = true;
}
}
}
Double chargeAmount = null;
if (!isRateCharge) {
chargeAmount = sumCharge(idsAndValues);
} else {
chargeAmount = Double.valueOf(idsAndValueAsTreeMap.firstEntry().getValue());
BigDecimal chargeAmountBig = new BigDecimal(chargeAmount);
membersAmount = membersAmount.multiply(chargeAmountBig);
int scale = Money.getInternalPrecision();
chargeAmountBig = membersAmount.divide(parentAmount, scale, RoundingMode.HALF_EVEN);
chargeAmount = chargeAmountBig.doubleValue();
}
parentAccount.updateDetails(userContext);
CustomerLevel customerLevel = null;
if (parentAccount.isCustomerAccount()) {
customerLevel = parentAccount.getCustomer().getLevel();
}
if (parentAccount.getPersonnel() != null) {
checkPermissionForApplyCharges(parentAccount.getType(), customerLevel, userContext, parentAccount.getOffice().getOfficeId(), parentAccount.getPersonnel().getPersonnelId());
} else {
checkPermissionForApplyCharges(parentAccount.getType(), customerLevel, userContext, parentAccount.getOffice().getOfficeId(), userContext.getId());
}
this.transactionHelper.startTransaction();
if (isPenaltyType && parentAccount instanceof LoanBO) {
PenaltyBO penalty = this.penaltyDao.findPenaltyById(chargeId.intValue());
((LoanBO) parentAccount).addAccountPenalty(new AccountPenaltiesEntity(parentAccount, penalty, chargeAmount));
} else {
parentAccount.applyCharge(chargeId, chargeAmount);
}
this.transactionHelper.commitTransaction();
} catch (ServiceException e) {
this.transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(e);
} catch (ApplicationException e) {
this.transactionHelper.rollbackTransaction();
throw new BusinessRuleException(e.getKey(), e);
}
}
use of org.mifos.accounts.penalties.business.RatePenaltyBO in project head by mifos.
the class WebTierPenaltyServiceFacade method createPenalty.
@Override
public void createPenalty(PenaltyFormDto dto) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = new UserContextFactory().create(user);
try {
PenaltyCategory penaltyCategory = dto.getCategoryType() != null ? PenaltyCategory.getPenaltyCategory(dto.getCategoryType()) : null;
PenaltyFrequency penaltyFrequency = dto.getPenaltyFrequency() != null ? PenaltyFrequency.getPenaltyFrequencyType(dto.getPenaltyFrequency()) : null;
PenaltyPeriod penaltyPeriod = dto.getPenaltyPeriod() != null ? PenaltyPeriod.getPenaltyPeriod(dto.getPenaltyPeriod()) : null;
PenaltyFormula penaltyFormula = dto.getPenaltyFormula() != null ? PenaltyFormula.getPenaltyFormula(dto.getPenaltyFormula()) : null;
PenaltyCategoryEntity penaltyCategoryEntity = this.penaltyDao.findPenaltyCategoryEntityByType(penaltyCategory);
PenaltyPeriodEntity penaltyPeriodEntity = this.penaltyDao.findPenaltyPeriodEntityByType(penaltyPeriod);
PenaltyFrequencyEntity penaltyFrequencyEntity = this.penaltyDao.findPenaltyFrequencyEntityByType(penaltyFrequency);
GLCodeEntity glCodeEntity = this.generalLedgerDao.findGlCodeById(dto.getGlCode());
PenaltyBO penaltyBO = null;
String penaltyName = dto.getPenaltyName();
Integer periodDuration = dto.getDuration();
Double min = dto.getMin();
Double max = dto.getMax();
if (dto.isRatePenalty()) {
Double rate = dto.getRate();
PenaltyFormulaEntity formula = this.penaltyDao.findPenaltyFormulaEntityByType(penaltyFormula);
penaltyBO = new RatePenaltyBO(userContext, penaltyName, penaltyCategoryEntity, penaltyPeriodEntity, periodDuration, min, max, penaltyFrequencyEntity, glCodeEntity, formula, rate);
} else {
Money amount = new Money(getCurrency(dto.getCurrencyId()), dto.getAmount());
penaltyBO = new AmountPenaltyBO(userContext, penaltyName, penaltyCategoryEntity, penaltyPeriodEntity, periodDuration, min, max, penaltyFrequencyEntity, glCodeEntity, amount);
}
try {
StaticHibernateUtil.startTransaction();
this.penaltyDao.save(penaltyBO);
StaticHibernateUtil.commitTransaction();
} catch (Exception e) {
StaticHibernateUtil.rollbackTransaction();
throw new MifosRuntimeException(e);
} finally {
StaticHibernateUtil.closeSession();
}
} catch (ApplicationException e) {
throw new BusinessRuleException(e.getKey(), e);
}
}
use of org.mifos.accounts.penalties.business.RatePenaltyBO in project head by mifos.
the class PenaltyDaoHibernateIntegrationTest method cleanDatabaseTables.
@Before
public void cleanDatabaseTables() throws Exception {
databaseCleaner.clean();
periodEntity = new PenaltyPeriodEntity(PenaltyPeriod.INSTALLMENTS);
statusEntity = new PenaltyStatusEntity(PenaltyStatus.ACTIVE);
categoryEntity = new PenaltyCategoryEntity(PenaltyCategory.LOAN);
frequencyEntity = new PenaltyFrequencyEntity(PenaltyFrequency.NONE);
formulaEntity = new PenaltyFormulaEntity(PenaltyFormula.OUTSTANDING_LOAN_AMOUNT);
glCodeEntity = new GLCodeEntity((short) 42, "31102");
amountPenalty = new AmountPenaltyBO(TestUtils.makeUser(), "Amount Penalty Test", new PenaltyCategoryEntity(PenaltyCategory.SAVING), periodEntity, 6, 1.0, 14.0, frequencyEntity, glCodeEntity, TestUtils.createMoney(158.5));
ratePenalty = new RatePenaltyBO(TestUtils.makeUser(), "Rate Penalty Test", categoryEntity, periodEntity, 6, 1.0, 10.0, frequencyEntity, glCodeEntity, formulaEntity, 10.5);
IntegrationTestObjectMother.createPenalty(amountPenalty);
IntegrationTestObjectMother.createPenalty(ratePenalty);
}
use of org.mifos.accounts.penalties.business.RatePenaltyBO in project head by mifos.
the class PenaltyDaoHibernateIntegrationTest method shouldFindPenaltyById.
@Test
public void shouldFindPenaltyById() throws Exception {
PenaltyBO found = penaltyDao.findPenaltyById(amountPenalty.getPenaltyId());
assertThat((AmountPenaltyBO) found, is(amountPenalty));
found = penaltyDao.findPenaltyById(ratePenalty.getPenaltyId());
assertThat((RatePenaltyBO) found, is(ratePenalty));
}
Aggregations