From 6b983ca7b896c40e875ae12ca2cd75af5245dda9 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Wed, 8 Mar 2017 14:03:52 -0800 Subject: [PATCH] Fix out of pocket and start on line 2 of Issue #65. --- app/assets/javascripts/invoices.js | 48 +++++++++++++++++++----------- test/fixtures/invoices.yml | 2 +- test/integration/invoices_test.rb | 8 +++-- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/app/assets/javascripts/invoices.js b/app/assets/javascripts/invoices.js index ced080f..1691887 100644 --- a/app/assets/javascripts/invoices.js +++ b/app/assets/javascripts/invoices.js @@ -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; @@ -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); @@ -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); diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index 43ceced..057ee8f 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -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' diff --git a/test/integration/invoices_test.rb b/test/integration/invoices_test.rb index 7c63743..6c0bc3c 100644 --- a/test/integration/invoices_test.rb +++ b/test/integration/invoices_test.rb @@ -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) @@ -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' @@ -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