fixed mifosx-304 deleting loan product charges not working

This commit is contained in:
John Woodlock
2013-04-26 17:29:40 +01:00
parent 44cef96643
commit a3a40f5a45
@@ -72,42 +72,74 @@ public class LoanProduct extends AbstractPersistable<Long> {
@Column(name = "accounting_type", nullable = false) @Column(name = "accounting_type", nullable = false)
private Integer accountingRule; private Integer accountingRule;
public static LoanProduct assembleFromJson(final Fund fund, final LoanTransactionProcessingStrategy loanTransactionProcessingStrategy, public static LoanProduct assembleFromJson(
final List<Charge> productCharges, final JsonCommand command, final AprCalculator aprCalculator) { final Fund fund,
final LoanTransactionProcessingStrategy loanTransactionProcessingStrategy,
final List<Charge> productCharges, final JsonCommand command,
final AprCalculator aprCalculator) {
final String name = command.stringValueOfParameterNamed("name"); final String name = command.stringValueOfParameterNamed("name");
final String description = command.stringValueOfParameterNamed("description"); final String description = command
final String currencyCode = command.stringValueOfParameterNamed("currencyCode"); .stringValueOfParameterNamed("description");
final Integer digitsAfterDecimal = command.integerValueOfParameterNamed("digitsAfterDecimal"); final String currencyCode = command
.stringValueOfParameterNamed("currencyCode");
final Integer digitsAfterDecimal = command
.integerValueOfParameterNamed("digitsAfterDecimal");
final MonetaryCurrency currency = new MonetaryCurrency(currencyCode, digitsAfterDecimal); final MonetaryCurrency currency = new MonetaryCurrency(currencyCode,
final BigDecimal principal = command.bigDecimalValueOfParameterNamed("principal"); digitsAfterDecimal);
final BigDecimal minPrincipal = command.bigDecimalValueOfParameterNamed("minPrincipal"); final BigDecimal principal = command
final BigDecimal maxPrincipal = command.bigDecimalValueOfParameterNamed("maxPrincipal"); .bigDecimalValueOfParameterNamed("principal");
final BigDecimal minPrincipal = command
.bigDecimalValueOfParameterNamed("minPrincipal");
final BigDecimal maxPrincipal = command
.bigDecimalValueOfParameterNamed("maxPrincipal");
final InterestMethod interestMethod = InterestMethod.fromInt(command.integerValueOfParameterNamed("interestType")); final InterestMethod interestMethod = InterestMethod.fromInt(command
final InterestCalculationPeriodMethod interestCalculationPeriodMethod = InterestCalculationPeriodMethod.fromInt(command .integerValueOfParameterNamed("interestType"));
final InterestCalculationPeriodMethod interestCalculationPeriodMethod = InterestCalculationPeriodMethod
.fromInt(command
.integerValueOfParameterNamed("interestCalculationPeriodType")); .integerValueOfParameterNamed("interestCalculationPeriodType"));
final AmortizationMethod amortizationMethod = AmortizationMethod.fromInt(command.integerValueOfParameterNamed("amortizationType")); final AmortizationMethod amortizationMethod = AmortizationMethod
final PeriodFrequencyType repaymentFrequencyType = PeriodFrequencyType.fromInt(command .fromInt(command
.integerValueOfParameterNamed("amortizationType"));
final PeriodFrequencyType repaymentFrequencyType = PeriodFrequencyType
.fromInt(command
.integerValueOfParameterNamed("repaymentFrequencyType")); .integerValueOfParameterNamed("repaymentFrequencyType"));
final PeriodFrequencyType interestFrequencyType = PeriodFrequencyType.fromInt(command final PeriodFrequencyType interestFrequencyType = PeriodFrequencyType
.fromInt(command
.integerValueOfParameterNamed("interestRateFrequencyType")); .integerValueOfParameterNamed("interestRateFrequencyType"));
final BigDecimal interestRatePerPeriod = command.bigDecimalValueOfParameterNamed("interestRatePerPeriod"); final BigDecimal interestRatePerPeriod = command
final BigDecimal minInterestRatePerPeriod = command.bigDecimalValueOfParameterNamed("minInterestRatePerPeriod"); .bigDecimalValueOfParameterNamed("interestRatePerPeriod");
final BigDecimal maxInterestRatePerPeriod = command.bigDecimalValueOfParameterNamed("maxInterestRatePerPeriod"); final BigDecimal minInterestRatePerPeriod = command
final BigDecimal annualInterestRate = aprCalculator.calculateFrom(interestFrequencyType, interestRatePerPeriod); .bigDecimalValueOfParameterNamed("minInterestRatePerPeriod");
final BigDecimal maxInterestRatePerPeriod = command
.bigDecimalValueOfParameterNamed("maxInterestRatePerPeriod");
final BigDecimal annualInterestRate = aprCalculator.calculateFrom(
interestFrequencyType, interestRatePerPeriod);
final Integer repaymentEvery = command.integerValueOfParameterNamed("repaymentEvery"); final Integer repaymentEvery = command
final Integer numberOfRepayments = command.integerValueOfParameterNamed("numberOfRepayments"); .integerValueOfParameterNamed("repaymentEvery");
final Integer minNumberOfRepayments = command.integerValueOfParameterNamed("minNumberOfRepayments"); final Integer numberOfRepayments = command
final Integer maxNumberOfRepayments = command.integerValueOfParameterNamed("maxNumberOfRepayments"); .integerValueOfParameterNamed("numberOfRepayments");
final BigDecimal inArrearsTolerance = command.bigDecimalValueOfParameterNamed("inArrearsTolerance"); final Integer minNumberOfRepayments = command
final AccountingRuleType accountingRuleType = AccountingRuleType.fromInt(command.integerValueOfParameterNamed("accountingRule")); .integerValueOfParameterNamed("minNumberOfRepayments");
final Integer maxNumberOfRepayments = command
.integerValueOfParameterNamed("maxNumberOfRepayments");
final BigDecimal inArrearsTolerance = command
.bigDecimalValueOfParameterNamed("inArrearsTolerance");
final AccountingRuleType accountingRuleType = AccountingRuleType
.fromInt(command.integerValueOfParameterNamed("accountingRule"));
return new LoanProduct(fund, loanTransactionProcessingStrategy, name, description, currency, principal, minPrincipal, maxPrincipal, return new LoanProduct(fund, loanTransactionProcessingStrategy, name,
interestRatePerPeriod, minInterestRatePerPeriod, maxInterestRatePerPeriod, interestFrequencyType, annualInterestRate, interestMethod, interestCalculationPeriodMethod, description, currency, principal, minPrincipal, maxPrincipal,
repaymentEvery, repaymentFrequencyType, numberOfRepayments, minNumberOfRepayments, maxNumberOfRepayments, amortizationMethod, inArrearsTolerance, productCharges, interestRatePerPeriod, minInterestRatePerPeriod,
maxInterestRatePerPeriod, interestFrequencyType,
annualInterestRate, interestMethod,
interestCalculationPeriodMethod, repaymentEvery,
repaymentFrequencyType, numberOfRepayments,
minNumberOfRepayments, maxNumberOfRepayments,
amortizationMethod, inArrearsTolerance, productCharges,
accountingRuleType); accountingRuleType);
} }
@@ -116,16 +148,29 @@ public class LoanProduct extends AbstractPersistable<Long> {
this.loanProductMinMaxConstraints = null; this.loanProductMinMaxConstraints = null;
} }
public LoanProduct(final Fund fund, final LoanTransactionProcessingStrategy transactionProcessingStrategy, final String name, public LoanProduct(
final String description, final MonetaryCurrency currency, final BigDecimal defaultPrincipal, final Fund fund,
final BigDecimal defaultMinPrincipal, final BigDecimal defaultMaxPrincipal, final LoanTransactionProcessingStrategy transactionProcessingStrategy,
final BigDecimal defaultNominalInterestRatePerPeriod, final BigDecimal defaultMinNominalInterestRatePerPeriod, final String name,
final BigDecimal defaultMaxNominalInterestRatePerPeriod, final PeriodFrequencyType interestPeriodFrequencyType, final String description,
final BigDecimal defaultAnnualNominalInterestRate, final InterestMethod interestMethod, final MonetaryCurrency currency,
final InterestCalculationPeriodMethod interestCalculationPeriodMethod, final Integer repayEvery, final BigDecimal defaultPrincipal,
final PeriodFrequencyType repaymentFrequencyType, final Integer defaultNumberOfInstallments, final BigDecimal defaultMinPrincipal,
final Integer defaultMinNumberOfInstallments, final Integer defaultMaxNumberOfInstallments, final BigDecimal defaultMaxPrincipal,
final AmortizationMethod amortizationMethod, final BigDecimal inArrearsTolerance, final List<Charge> charges, final BigDecimal defaultNominalInterestRatePerPeriod,
final BigDecimal defaultMinNominalInterestRatePerPeriod,
final BigDecimal defaultMaxNominalInterestRatePerPeriod,
final PeriodFrequencyType interestPeriodFrequencyType,
final BigDecimal defaultAnnualNominalInterestRate,
final InterestMethod interestMethod,
final InterestCalculationPeriodMethod interestCalculationPeriodMethod,
final Integer repayEvery,
final PeriodFrequencyType repaymentFrequencyType,
final Integer defaultNumberOfInstallments,
final Integer defaultMinNumberOfInstallments,
final Integer defaultMaxNumberOfInstallments,
final AmortizationMethod amortizationMethod,
final BigDecimal inArrearsTolerance, final List<Charge> charges,
final AccountingRuleType accountingRuleType) { final AccountingRuleType accountingRuleType) {
this.fund = fund; this.fund = fund;
this.transactionProcessingStrategy = transactionProcessingStrategy; this.transactionProcessingStrategy = transactionProcessingStrategy;
@@ -140,13 +185,18 @@ public class LoanProduct extends AbstractPersistable<Long> {
this.charges = charges; this.charges = charges;
} }
this.loanProductRelatedDetail = new LoanProductRelatedDetail(currency, defaultPrincipal, defaultNominalInterestRatePerPeriod, this.loanProductRelatedDetail = new LoanProductRelatedDetail(currency,
interestPeriodFrequencyType, defaultAnnualNominalInterestRate, interestMethod, interestCalculationPeriodMethod, repayEvery, defaultPrincipal, defaultNominalInterestRatePerPeriod,
repaymentFrequencyType, defaultNumberOfInstallments, amortizationMethod, inArrearsTolerance); interestPeriodFrequencyType, defaultAnnualNominalInterestRate,
interestMethod, interestCalculationPeriodMethod, repayEvery,
repaymentFrequencyType, defaultNumberOfInstallments,
amortizationMethod, inArrearsTolerance);
this.loanProductMinMaxConstraints = new LoanProductMinMaxConstraints(defaultMinPrincipal, defaultMaxPrincipal, this.loanProductMinMaxConstraints = new LoanProductMinMaxConstraints(
defaultMinNominalInterestRatePerPeriod, defaultMaxNominalInterestRatePerPeriod, defaultMinNumberOfInstallments, defaultMinPrincipal, defaultMaxPrincipal,
defaultMaxNumberOfInstallments); defaultMinNominalInterestRatePerPeriod,
defaultMaxNominalInterestRatePerPeriod,
defaultMinNumberOfInstallments, defaultMaxNumberOfInstallments);
if (accountingRuleType != null) { if (accountingRuleType != null) {
this.accountingRule = accountingRuleType.getValue(); this.accountingRule = accountingRuleType.getValue();
@@ -166,14 +216,23 @@ public class LoanProduct extends AbstractPersistable<Long> {
} }
public boolean update(final List<Charge> newProductCharges) { public boolean update(final List<Charge> newProductCharges) {
if (newProductCharges == null)
return false;
boolean updated = false; boolean updated = false;
if (this.charges != null) { if (this.charges != null) {
final Set<Charge> setOfCharges = new HashSet<Charge>(this.charges); final Set<Charge> currentSetOfCharges = new HashSet<Charge>(
this.charges);
final Set<Charge> newSetOfCharges = new HashSet<Charge>(
newProductCharges);
updated = setOfCharges.addAll(newProductCharges); if (!(currentSetOfCharges.equals(newSetOfCharges))) {
if (updated) { updated = true;
this.charges = newProductCharges; this.charges = newProductCharges;
} }
} else {
updated = true;
this.charges = newProductCharges;
} }
return updated; return updated;
} }
@@ -182,28 +241,36 @@ public class LoanProduct extends AbstractPersistable<Long> {
return this.accountingRule; return this.accountingRule;
} }
public Map<String, Object> update(final JsonCommand command, final AprCalculator aprCalculator) { public Map<String, Object> update(final JsonCommand command,
final AprCalculator aprCalculator) {
final Map<String, Object> actualChanges = this.loanProductRelatedDetail.update(command, aprCalculator); final Map<String, Object> actualChanges = this.loanProductRelatedDetail
actualChanges.putAll(this.loanProductMinMaxConstraints().update(command)); .update(command, aprCalculator);
actualChanges.putAll(this.loanProductMinMaxConstraints()
.update(command));
final String accountingTypeParamName = "accountingRule"; final String accountingTypeParamName = "accountingRule";
if (command.isChangeInIntegerParameterNamed(accountingTypeParamName, this.accountingRule)) { if (command.isChangeInIntegerParameterNamed(accountingTypeParamName,
final Integer newValue = command.integerValueOfParameterNamed(accountingTypeParamName); this.accountingRule)) {
final Integer newValue = command
.integerValueOfParameterNamed(accountingTypeParamName);
actualChanges.put(accountingTypeParamName, newValue); actualChanges.put(accountingTypeParamName, newValue);
this.accountingRule = newValue; this.accountingRule = newValue;
} }
final String nameParamName = "name"; final String nameParamName = "name";
if (command.isChangeInStringParameterNamed(nameParamName, this.name)) { if (command.isChangeInStringParameterNamed(nameParamName, this.name)) {
final String newValue = command.stringValueOfParameterNamed(nameParamName); final String newValue = command
.stringValueOfParameterNamed(nameParamName);
actualChanges.put(nameParamName, newValue); actualChanges.put(nameParamName, newValue);
this.name = newValue; this.name = newValue;
} }
final String descriptionParamName = "description"; final String descriptionParamName = "description";
if (command.isChangeInStringParameterNamed(descriptionParamName, this.description)) { if (command.isChangeInStringParameterNamed(descriptionParamName,
final String newValue = command.stringValueOfParameterNamed(descriptionParamName); this.description)) {
final String newValue = command
.stringValueOfParameterNamed(descriptionParamName);
actualChanges.put(descriptionParamName, newValue); actualChanges.put(descriptionParamName, newValue);
this.description = newValue; this.description = newValue;
} }
@@ -213,8 +280,10 @@ public class LoanProduct extends AbstractPersistable<Long> {
existingFundId = this.fund.getId(); existingFundId = this.fund.getId();
} }
final String fundIdParamName = "fundId"; final String fundIdParamName = "fundId";
if (command.isChangeInLongParameterNamed(fundIdParamName, existingFundId)) { if (command.isChangeInLongParameterNamed(fundIdParamName,
final Long newValue = command.longValueOfParameterNamed(fundIdParamName); existingFundId)) {
final Long newValue = command
.longValueOfParameterNamed(fundIdParamName);
actualChanges.put(fundIdParamName, newValue); actualChanges.put(fundIdParamName, newValue);
} }
@@ -223,29 +292,35 @@ public class LoanProduct extends AbstractPersistable<Long> {
existingStrategyId = this.transactionProcessingStrategy.getId(); existingStrategyId = this.transactionProcessingStrategy.getId();
} }
final String transactionProcessingStrategyParamName = "transactionProcessingStrategyId"; final String transactionProcessingStrategyParamName = "transactionProcessingStrategyId";
if (command.isChangeInLongParameterNamed(transactionProcessingStrategyParamName, existingStrategyId)) { if (command.isChangeInLongParameterNamed(
final Long newValue = command.longValueOfParameterNamed(transactionProcessingStrategyParamName); transactionProcessingStrategyParamName, existingStrategyId)) {
final Long newValue = command
.longValueOfParameterNamed(transactionProcessingStrategyParamName);
actualChanges.put(transactionProcessingStrategyParamName, newValue); actualChanges.put(transactionProcessingStrategyParamName, newValue);
} }
final String chargesParamName = "charges"; final String chargesParamName = "charges";
if (command.hasParameter(chargesParamName)) { if (command.hasParameter(chargesParamName)) {
JsonArray jsonArray = command.arrayOfParameterNamed(chargesParamName); JsonArray jsonArray = command
.arrayOfParameterNamed(chargesParamName);
if (jsonArray != null) { if (jsonArray != null) {
} }
actualChanges.put(chargesParamName, command.jsonFragment(chargesParamName)); actualChanges.put(chargesParamName,
command.jsonFragment(chargesParamName));
} }
return actualChanges; return actualChanges;
} }
public boolean isCashBasedAccountingEnabled() { public boolean isCashBasedAccountingEnabled() {
return AccountingRuleType.CASH_BASED.getValue().equals(this.accountingRule); return AccountingRuleType.CASH_BASED.getValue().equals(
this.accountingRule);
} }
public boolean isAccrualBasedAccountingEnabled() { public boolean isAccrualBasedAccountingEnabled() {
return AccountingRuleType.ACCRUAL_BASED.getValue().equals(this.accountingRule); return AccountingRuleType.ACCRUAL_BASED.getValue().equals(
this.accountingRule);
} }
public Money getPrincipalAmount() { public Money getPrincipalAmount() {
@@ -253,11 +328,13 @@ public class LoanProduct extends AbstractPersistable<Long> {
} }
public Money getMinPrincipalAmount() { public Money getMinPrincipalAmount() {
return Money.of(this.loanProductRelatedDetail.getCurrency(), this.loanProductMinMaxConstraints().getMinPrincipal()); return Money.of(this.loanProductRelatedDetail.getCurrency(), this
.loanProductMinMaxConstraints().getMinPrincipal());
} }
public Money getMaxPrincipalAmount() { public Money getMaxPrincipalAmount() {
return Money.of(this.loanProductRelatedDetail.getCurrency(), this.loanProductMinMaxConstraints().getMaxPrincipal()); return Money.of(this.loanProductRelatedDetail.getCurrency(), this
.loanProductMinMaxConstraints().getMaxPrincipal());
} }
public BigDecimal getNominalInterestRatePerPeriod() { public BigDecimal getNominalInterestRatePerPeriod() {
@@ -265,11 +342,13 @@ public class LoanProduct extends AbstractPersistable<Long> {
} }
public BigDecimal getMinNominalInterestRatePerPeriod() { public BigDecimal getMinNominalInterestRatePerPeriod() {
return this.loanProductMinMaxConstraints().getMinNominalInterestRatePerPeriod(); return this.loanProductMinMaxConstraints()
.getMinNominalInterestRatePerPeriod();
} }
public BigDecimal getMaxNominalInterestRatePerPeriod() { public BigDecimal getMaxNominalInterestRatePerPeriod() {
return this.loanProductMinMaxConstraints().getMaxNominalInterestRatePerPeriod(); return this.loanProductMinMaxConstraints()
.getMaxNominalInterestRatePerPeriod();
} }
public Integer getNumberOfRepayments() { public Integer getNumberOfRepayments() {
@@ -289,9 +368,11 @@ public class LoanProduct extends AbstractPersistable<Long> {
} }
public LoanProductMinMaxConstraints loanProductMinMaxConstraints() { public LoanProductMinMaxConstraints loanProductMinMaxConstraints() {
//If all min and max fields are null then loanProductMinMaxConstraints initialising to null // If all min and max fields are null then loanProductMinMaxConstraints
// initialising to null
// Reset LoanProductMinMaxConstraints with null values. // Reset LoanProductMinMaxConstraints with null values.
this.loanProductMinMaxConstraints = (this.loanProductMinMaxConstraints == null) ? new LoanProductMinMaxConstraints(null, null, null, null, null, null) this.loanProductMinMaxConstraints = (this.loanProductMinMaxConstraints == null) ? new LoanProductMinMaxConstraints(
null, null, null, null, null, null)
: this.loanProductMinMaxConstraints; : this.loanProductMinMaxConstraints;
return loanProductMinMaxConstraints; return loanProductMinMaxConstraints;
} }