Search in sources :

Example 1 with RatePenaltyBO

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();
    }
}
Also used : RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) Money(org.mifos.framework.util.helpers.Money) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException)

Example 2 with RatePenaltyBO

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);
    }
}
Also used : AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) CustomerLevel(org.mifos.customers.api.CustomerLevel) PenaltyBO(org.mifos.accounts.penalties.business.PenaltyBO) RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) UserContext(org.mifos.security.util.UserContext) LoanBO(org.mifos.accounts.loan.business.LoanBO) MifosUser(org.mifos.security.MifosUser) TreeMap(java.util.TreeMap) BigDecimal(java.math.BigDecimal) AccountBO(org.mifos.accounts.business.AccountBO) BusinessRuleException(org.mifos.service.BusinessRuleException) AccountBusinessService(org.mifos.accounts.business.service.AccountBusinessService) ApplicationException(org.mifos.framework.exceptions.ApplicationException) ServiceException(org.mifos.framework.exceptions.ServiceException) FeeBO(org.mifos.accounts.fees.business.FeeBO) RateFeeBO(org.mifos.accounts.fees.business.RateFeeBO) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 3 with RatePenaltyBO

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);
    }
}
Also used : PenaltyCategory(org.mifos.accounts.penalties.util.helpers.PenaltyCategory) RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) PenaltyBO(org.mifos.accounts.penalties.business.PenaltyBO) AmountPenaltyBO(org.mifos.accounts.penalties.business.AmountPenaltyBO) RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) UserContext(org.mifos.security.util.UserContext) PenaltyPeriod(org.mifos.accounts.penalties.util.helpers.PenaltyPeriod) GLCodeEntity(org.mifos.accounts.financial.business.GLCodeEntity) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) PenaltyPeriodEntity(org.mifos.accounts.penalties.business.PenaltyPeriodEntity) BusinessRuleException(org.mifos.service.BusinessRuleException) FinancialException(org.mifos.accounts.financial.exceptions.FinancialException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) Money(org.mifos.framework.util.helpers.Money) PenaltyCategoryEntity(org.mifos.accounts.penalties.business.PenaltyCategoryEntity) BusinessRuleException(org.mifos.service.BusinessRuleException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) PenaltyFrequency(org.mifos.accounts.penalties.util.helpers.PenaltyFrequency) PenaltyFrequencyEntity(org.mifos.accounts.penalties.business.PenaltyFrequencyEntity) AmountPenaltyBO(org.mifos.accounts.penalties.business.AmountPenaltyBO) PenaltyFormula(org.mifos.accounts.penalties.util.helpers.PenaltyFormula) PenaltyFormulaEntity(org.mifos.accounts.penalties.business.PenaltyFormulaEntity) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 4 with RatePenaltyBO

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);
}
Also used : RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) PenaltyCategoryEntity(org.mifos.accounts.penalties.business.PenaltyCategoryEntity) PenaltyFrequencyEntity(org.mifos.accounts.penalties.business.PenaltyFrequencyEntity) PenaltyStatusEntity(org.mifos.accounts.penalties.business.PenaltyStatusEntity) AmountPenaltyBO(org.mifos.accounts.penalties.business.AmountPenaltyBO) GLCodeEntity(org.mifos.accounts.financial.business.GLCodeEntity) PenaltyPeriodEntity(org.mifos.accounts.penalties.business.PenaltyPeriodEntity) PenaltyFormulaEntity(org.mifos.accounts.penalties.business.PenaltyFormulaEntity) Before(org.junit.Before)

Example 5 with RatePenaltyBO

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));
}
Also used : PenaltyBO(org.mifos.accounts.penalties.business.PenaltyBO) AmountPenaltyBO(org.mifos.accounts.penalties.business.AmountPenaltyBO) RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) Test(org.junit.Test)

Aggregations

RatePenaltyBO (org.mifos.accounts.penalties.business.RatePenaltyBO)7 AmountPenaltyBO (org.mifos.accounts.penalties.business.AmountPenaltyBO)5 PenaltyBO (org.mifos.accounts.penalties.business.PenaltyBO)5 MifosRuntimeException (org.mifos.core.MifosRuntimeException)3 ApplicationException (org.mifos.framework.exceptions.ApplicationException)3 Money (org.mifos.framework.util.helpers.Money)3 MifosUser (org.mifos.security.MifosUser)3 UserContext (org.mifos.security.util.UserContext)3 BusinessRuleException (org.mifos.service.BusinessRuleException)3 GLCodeEntity (org.mifos.accounts.financial.business.GLCodeEntity)2 FinancialException (org.mifos.accounts.financial.exceptions.FinancialException)2 PenaltyCategoryEntity (org.mifos.accounts.penalties.business.PenaltyCategoryEntity)2 PenaltyFormulaEntity (org.mifos.accounts.penalties.business.PenaltyFormulaEntity)2 PenaltyFrequencyEntity (org.mifos.accounts.penalties.business.PenaltyFrequencyEntity)2 PenaltyPeriodEntity (org.mifos.accounts.penalties.business.PenaltyPeriodEntity)2 PenaltyFormula (org.mifos.accounts.penalties.util.helpers.PenaltyFormula)2 PenaltyFrequency (org.mifos.accounts.penalties.util.helpers.PenaltyFrequency)2 PenaltyPeriod (org.mifos.accounts.penalties.util.helpers.PenaltyPeriod)2 UserContextFactory (org.mifos.accounts.servicefacade.UserContextFactory)2 BigDecimal (java.math.BigDecimal)1