Browse Source

DIV-663: Dynamically calculating claimant and spouse's share of child support amounts correctly

pull/160/head
Benard Ebinu 8 years ago
parent
commit
1bf77e0bee
2 changed files with 62 additions and 41 deletions
  1. +59
    -38
      edivorce/apps/core/static/js/main.js
  2. +3
    -3
      edivorce/apps/core/templates/question/06_children_income_expenses.html

+ 59
- 38
edivorce/apps/core/static/js/main.js View File

@ -72,28 +72,6 @@ $(function () {
$('[data-calc_delta="true"]').on('change', deltaFieldOnChange);
$('[data-calc_delta="true"]').each(deltaFieldOnChange);
var fraction = function(part1, part2) {
part1 = parseFloat(part1);
part2 = parseFloat(part2);
if (part1 + part2 === 0) {
return 0;
} else {
return part1 / (part1 + part2);
}
};
// Calculates the proportionate percentage of claimantOne to the sum of claimantOne
// and claimantTwo. The result is a value between [0,100] inclusive.
var calcPercentage = function(targetElement, claimantOne, claimantTwo) {
targetElement.val(Math.round(fraction(claimantOne, claimantTwo) * 100));
};
// Calculates the proportionate amount of claimantOne to the sum of claimantOne
// and claimantTwo. The result is a value between [0,claimantOne] inclusive.
var calcPercentageAmount = function (targetElement, claimantOne, claimantTwo) {
var amount = parseFloat(claimantOne) * fraction(claimantOne, claimantTwo);
targetElement.val(amount.toFixed(2));
};
// We want a way to dynamically calculate what proportion a given number is
// relative to the sum of that number with another number. Our specific use
@ -101,31 +79,74 @@ $(function () {
// payment relative to their spouse. We calculate these proportions based off
// of each claimant's income.
// data-calc_percentage=[true|false] - whether to compute amount between [0,100] inclusive.
// data-calc_proportions=[true|false] - whether to compute amount between [0, value at data-claimant_one_selector]
// data-claimant_one_selector=[any jQuery input selector] - the input field that will contain claimant one
// income information. When the value in this input field changes the
// proportionate amounts will automatically get recalculated.
// data-claimant_two_selector=[any jQuery input selector] - the input field that will contain claimant two
// income information.
$('[data-calc_percentage="true"], [data-calc_proportions="true"]').each(function() {
$('[data-calc_percentage="true"]').each(function() {
var fraction = function(part1, part2) {
part1 = parseFloat(part1);
part2 = parseFloat(part2);
if (part1 + part2 === 0) {
return 0;
} else {
return part1 / (part1 + part2);
}
};
// Calculates the proportionate percentage of claimantOne to the sum of claimantOne
// and claimantTwo. The result is a value between [0,100] inclusive.
var calcPercentage = function(targetElement, claimantOne, claimantTwo) {
targetElement.val(Math.round(fraction(claimantOne, claimantTwo) * 100));
};
var self = $(this);
var claimantOneElement = $($(this).attr('data-claimant_one_selector'));
var claimantTwoElement = $($(this).attr('data-claimant_two_selector'));
var calcFunction = calcPercentage;
if ($(this).attr('data-calc_proportions') !== undefined) {
calcFunction = calcPercentageAmount;
}
// Calculate and populate the field on initialization of page.
calcFunction(self, claimantOneElement.val(), claimantTwoElement.val());
calcPercentage(self, claimantOneElement.val(), claimantTwoElement.val());
// Calculate and populate the fields whenever there is a change in the input
// selectors.
claimantOneElement.on('change', function() {
calcFunction(self, claimantOneElement.val(), claimantTwoElement.val());
calcPercentage(self, claimantOneElement.val(), claimantTwoElement.val());
self.trigger('change');
});
claimantTwoElement.on('change', function() {
calcFunction(self, claimantOneElement.val(), claimantTwoElement.val());
calcPercentage(self, claimantOneElement.val(), claimantTwoElement.val());
self.trigger('change');
});
});
// Scale the value in the data-quantity element by the scale factor in the data-scale_factor
// element, and store the result in the element with the data-scale attribute set.
// Our specific use case is calculating the proportion of a given claimant's child support
// payment relative to their spouse. We calculate these proportions based off of each claimant's income.
// data-scale=[true|false] - whether to store scaled product in current element
// data-quantity=[any jQuery input selector] - the input field that will contain the factor that will be scaled.
// data-scale_factor=[any jQuery input selector] - the input field that will contain the scale that will be applied.
$('[data-scale="true"]').each(function(){
var self = $(this);
var quantityElement = $($(this).attr('data-quantity'));
var scaleFactorElement = $($(this).attr('data-scale_factor'));
// Calculates the proportionate amount of claimantOne to the sum of claimantOne
// and claimantTwo. The result is a value between [0,claimantOne] inclusive.
var scale = function (targetElement, claimantAmount, proportionFactor) {
var amount = parseFloat(proportionFactor) / 100 * parseFloat(claimantAmount);
targetElement.val(amount.toFixed(2));
};
scale(self, quantityElement.val(), scaleFactorElement.val());
quantityElement.on('change', function() {
scale(self, quantityElement.val(), scaleFactorElement.val());
});
scaleFactorElement.on('change', function() {
scale(self, quantityElement.val(), scaleFactorElement.val());
});
});
@ -804,21 +825,21 @@ var sumFieldOnChange = function() {
sumFields('.' + sumClass, '#' + sumTargetId);
};
var sumFields = function(addend_selector, sum_selector) {
var sumFields = function(addendSelector, sumSelector) {
var total = 0.0;
$(addend_selector).each(function () {
$(addendSelector).each(function () {
if ($(this).val() !== undefined && $(this).val().length !== 0) {
total += parseFloat($(this).val());
}
});
total = total.toFixed(2);
if (addend_selector !== undefined) {
if ($(sum_selector).is("p")) {
$(sum_selector).text(total);
if (addendSelector !== undefined) {
if ($(sumSelector).is("p")) {
$(sumSelector).text(total);
}
else {
$(sum_selector).val(total);
$(sum_selector).trigger("change");
$(sumSelector).val(total);
$(sumSelector).trigger("change");
}
}
};


+ 3
- 3
edivorce/apps/core/templates/question/06_children_income_expenses.html View File

@ -751,7 +751,7 @@
<td class="fact-sheet-answer" readonly>
<div class="dollar-prefix">
{% money_input_field value_src="totaInl_section_seven_expenses" id="total_extraordinary_expense_annually" class="fact-sheet-input money " scale_factor="12" readonly="" %}
{% money_input_field value_src="total_section_seven_expenses" id="total_extraordinary_expense_annually" class="fact-sheet-input money " scale_factor="12" readonly="" %}
</div>
</td>
</tr>
@ -792,7 +792,7 @@
</td>
<td class="fact-sheet-answer" readonly>
<div class="dollar-prefix">
{% money_input_field name="your_proportionate_share_amount" id="your_proportionate_share_amount" class="fact-sheet-input money" data_calc_proportions="true" data_claimant_one_selector="input[name='annual_gross_income']" data_claimant_two_selector="input[name='spouse_annual_gross_income']" readonly="" %}
{% money_input_field name="your_proportionate_share_amount" id="your_proportionate_share_amount" class="fact-sheet-input money" data_scale="true" data_quantity="#total_extraordinary_expense_monthly" data_scale_factor="input[name='your_proportionate_share_percent']" readonly="" %}
</div>
</td>
</tr>
@ -803,7 +803,7 @@
</td>
<td class="fact-sheet-answer" readonly>
<div class="dollar-prefix">
{% money_input_field name="spouse_proportionate_share_amount" id="spouse_proportionate_share_amount" class="fact-sheet-input money" data_calc_proportions="true" data_claimant_one_selector="input[name='spouse_annual_gross_income']" data_claimant_two_selector="input[name='annual_gross_income']" readonly="" %}
{% money_input_field name="spouse_proportionate_share_amount" id="spouse_proportionate_share_amount" class="fact-sheet-input money" data_scale="true" data_quantity="#total_extraordinary_expense_monthly" data_scale_factor="input[name='spouse_proportionate_share_percent']" readonly="" %}
</div>
</td>
</tr>


Loading…
Cancel
Save