Skip to content

Commit

Permalink
Fix out of pocket and start on line 2 of Issue #65.
Browse files Browse the repository at this point in the history
  • Loading branch information
lcreid committed Mar 8, 2017
1 parent d5d7aa4 commit 6b983ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
48 changes: 31 additions & 17 deletions app/assets/javascripts/invoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ $(document).on('turbolinks:load', function() {
a = $('input').filter(function() {
return this.id.match(/invoice_allocations_attributes_[0-9]+_amount/);
});
// console.log(a);
console.log('allocation_fields returning ' + a.size() + ' fields');
return a;
}

function update_out_of_pocket(e) {
console.log('Trigger fired at 59.');
console.log('e: ' + e);
console.log('e.target: ' + e.target);
console.log('e.target.id: ' + e.target.id);
console.log('e.target.value: ' + e.target.value);
function update_invoice_allocation_amount(e) {
// console.log('Trigger fired at 59.');
// console.log('e: ' + e);
// console.log('e.target: ' + e.target);
// console.log('e.target.id: ' + e.target.id);
// console.log('e.target.value: ' + e.target.value);
// TODO: Refactor allocated_spending to a function

allocated_spending = allocation_fields().toArray().reduce(function(a, b) {
// console.log('update_out_of_pocket b: ' + b.value);
return $.isNumeric(b.value)? a + Number(b.value.replace(/[,$]/g, "")): a;
Expand All @@ -75,28 +77,40 @@ $(document).on('turbolinks:load', function() {
if (available_for_this_invoice < allocation_on_changed_field) {
e.target.value = available_for_this_invoice.toFixed(2);
console.log('Just set the target to: ' + e.target.value);
out_of_pocket = 0;
} else {
out_of_pocket = Math.max(0, invoice_amount - allocated_spending);
}
}

function update_out_of_pocket() {
// TODO: Refactor allocated_spending to a function
allocated_spending = allocation_fields().toArray().reduce(function(a, b) {
// console.log('update_out_of_pocket b: ' + b.value);
return $.isNumeric(b.value)? a + Number(b.value.replace(/[,$]/g, "")): a;
}, 0);

invoice_amount = Number($('#invoice_invoice_amount').val().replace(/[,$]/g, ""));

out_of_pocket = Math.max(0, invoice_amount - allocated_spending);
console.log('Setting out_of_pocket: ' + out_of_pocket.toFixed(2));
out_of_pocket_field.val(out_of_pocket.toFixed(2));
}

function update_out_of_pocket_for_invoice_amount_change() {
console.log('into update_out_of_pocket_for_invoice_amount_change');
// console.log('into update_out_of_pocket_for_invoice_amount_change');
// console.log('Calling triggers on ' + allocation_fields().size() + ' fields');
allocation_fields().change();
console.log('out of update_out_of_pocket_for_invoice_amount_change');
update_out_of_pocket();
// console.log('out of update_out_of_pocket_for_invoice_amount_change');
}

function set_up_triggers() {
// trigger_fields = $.merge($.merge(allocation_fields(),
// out_of_pocket_field));
// console.log('Setting up triggers on ' + trigger_fields);
console.log('Setting up triggers on ' + allocation_fields().size() + ' fields');
allocation_fields().change(function(e) {
console.log('Trigger fired at 91ish.');
console.log(e.target);
update_out_of_pocket(e);
// console.log('Trigger fired at 91ish.');
// console.log(e.target);
update_invoice_allocation_amount(e);
update_out_of_pocket();
});

invoice_amount_field.change(update_out_of_pocket_for_invoice_amount_change);
Expand Down Expand Up @@ -131,8 +145,8 @@ $(document).on('turbolinks:load', function() {
// $(msg).appendTo('.cf0925-list-replace');
$('.cf0925-list-replace').append(msg);
// console.log('Finished updating select');
update_out_of_pocket_for_invoice_amount_change();
set_up_triggers();
update_out_of_pocket_for_invoice_amount_change();
}).fail(function(xhr, textStatus, errorThrown) {
if (xhr.status !== 0) {
console.log("Error: Status: " + textStatus + " error: " + errorThrown);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/invoices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ inv_valtest_complete:
invoice_from: service_provider_name

invoice_with_rtp_matched:
invoice_amount:
invoice_amount: 200
invoice_date: '2017-03-01'
notes:
service_end: '2017-02-28'
Expand Down
8 changes: 6 additions & 2 deletions test/integration/invoices_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class InvoicesTest < PoltergeistTest
# TODO: Make sure I've retrieved the right ones.
end

test 'invoice with no valid RTPs' do
test 'invoice with no matching RTPs' do
fill_in_login(users(:years))
child = funded_people(:two_fiscal_years)

Expand Down Expand Up @@ -108,6 +108,7 @@ class InvoicesTest < PoltergeistTest
assert_field 'Service Start', with: '2015-07-01'
assert_field 'Service End', with: '2015-07-31'
# assert_field '#invoice_out_of_pocket' # , visible: :all, with: 400
puts "Out of Pocket: #{find_field('Out of Pocket', disabled: true).value}"
assert_field 'Out of Pocket', disabled: true, with: '400.00'

click_link_or_button 'Save'
Expand Down Expand Up @@ -135,13 +136,16 @@ class InvoicesTest < PoltergeistTest

visit edit_invoice_path(invoice)
assert_selector 'tr.test-cf0925-invoice-row', count: 1
find('tr.test-cf0925-invoice-row').fill_in('Amount', with: 200)
within find('tr.test-cf0925-invoice-row') do
fill_in('Amount', with: 200)
end

click_link_or_button 'Save'

visit edit_invoice_path(invoice)
assert_selector 'tr.test-cf0925-invoice-row', count: 1
within find('tr.test-cf0925-invoice-row') do
# puts "Invoice allocation amount 149ish: #{find_field('Amount').value}"
assert_field('Amount', with: '200.00')
end
end
Expand Down

0 comments on commit 6b983ca

Please sign in to comment.