From 55fb2248b5760c216bb1da789c4d03e6e21778ed Mon Sep 17 00:00:00 2001 From: Jukka Ahonen Date: Tue, 5 Sep 2023 15:03:01 +0300 Subject: [PATCH] check subvention values when updating full_amount --- .../leaseSections/rent/RentAdjustmentEdit.js | 17 +++++++++++++++-- src/leases/helpers.js | 13 +++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/leases/components/leaseSections/rent/RentAdjustmentEdit.js b/src/leases/components/leaseSections/rent/RentAdjustmentEdit.js index 4dd3de63c..73292211c 100644 --- a/src/leases/components/leaseSections/rent/RentAdjustmentEdit.js +++ b/src/leases/components/leaseSections/rent/RentAdjustmentEdit.js @@ -41,6 +41,7 @@ import { calculateRentAdjustmentSubventionPercentCumulative, getDecisionById, hasSubventionDataChanged, + hasSubventionValues, } from '$src/leases/helpers'; import {getUiDataLeaseKey} from '$src/uiData/helpers'; import { @@ -265,9 +266,21 @@ class RentAdjustmentsEdit extends PureComponent { componentDidUpdate(prevProps: Props) { if (this.props && hasSubventionDataChanged(prevProps, this.props)) { - const {change, field} = this.props; + const { + change, + field, + fullAmount, + managementSubventions, + temporarySubventions + } = this.props; + + let newFullAmount = fullAmount + + if (hasSubventionValues(managementSubventions, temporarySubventions)) { + newFullAmount = this.calculateTotalSubventionPercent() + } - change(formName, `${field}.full_amount`, formatNumber(this.calculateTotalSubventionPercent())); // TODO + change(formName, `${field}.full_amount`, formatNumber(newFullAmount)); // TODO } } diff --git a/src/leases/helpers.js b/src/leases/helpers.js index c4104c369..34f78e5ee 100644 --- a/src/leases/helpers.js +++ b/src/leases/helpers.js @@ -1531,6 +1531,19 @@ export const calculateSubventionDiscountTotal = (initialYearRent: number, manage return Number(initialYearRent); }; +/** + * Check if subventions have values for calculation + * @param {number} initialYearRent + * @param {Object[]} managementSubventions + * @param {number} currentAmountPerArea + * @return {number} + */ +export const hasSubventionValues = (managementSubventions: ?Array, temporarySubventions: ?Array): boolean => { + let msWithValues = managementSubventions.filter((subvention) => !!subvention.subvention_amount) + let tsWithValues = temporarySubventions.filter((subvention) => !!subvention.subvention_percent) + return !!(msWithValues.length || tsWithValues.length) +}; + /** * Calculate basis of rent subvention percent * @param {number} initialYearRent