@Transactional
@Override
@CacheEvict(value = "charges", key = "T(org.mifosplatform.infrastructure.core.service.ThreadLocalContextUtil).getTenant().getTenantIdentifier().concat('ch')")
public CommandProcessingResult deleteCharge(final Long chargeId) {
final Charge chargeForDelete = this.chargeRepository.findOne(chargeId);
if (chargeForDelete == null || chargeForDelete.isDeleted()) { throw new ChargeNotFoundException(chargeId); }
final Collection<LoanProduct> loanProducts = this.loanProductRepository.retrieveLoanProductsByChargeId(chargeId);
final Boolean isChargeExistWithLoans = isAnyLoansAssociateWithThisCharge(chargeId);
final Boolean isChargeExistWithSavings = isAnySavingsAssociateWithThisCharge(chargeId);
// TODO: Change error messages around:
if (!loanProducts.isEmpty() || isChargeExistWithLoans || isChargeExistWithSavings) { throw new ChargeCannotBeDeletedException(
"error.msg.charge.cannot.be.deleted.it.is.already.used.in.loan",
"This charge cannot be deleted, it is already used in loan"); }
chargeForDelete.delete();
this.chargeRepository.save(chargeForDelete);
return new CommandProcessingResultBuilder().withEntityId(chargeForDelete.getId()).build();
}