From b22d5917751e2fa8abd4794b7b5442140a59f25f Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 9 Oct 2015 12:08:53 +1100 Subject: [PATCH] BillablePeriods don't get updated when account_invoice has been finalized --- app/jobs/update_billable_periods.rb | 19 +++++++++++-------- spec/jobs/update_billable_periods_spec.rb | 21 +++++++++++++++++---- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/jobs/update_billable_periods.rb b/app/jobs/update_billable_periods.rb index 3ffcaa07d0..35daf48dbc 100644 --- a/app/jobs/update_billable_periods.rb +++ b/app/jobs/update_billable_periods.rb @@ -74,14 +74,17 @@ class UpdateBillablePeriods account_invoice = AccountInvoice.find_or_create_by_user_id_and_year_and_month(owner_id, begins_at.year, begins_at.month) billable_period = BillablePeriod.where(account_invoice_id: account_invoice.id, begins_at: begins_at, enterprise_id: enterprise.id).first - billable_period ||= BillablePeriod.new(account_invoice_id: account_invoice.id, begins_at: begins_at, enterprise_id: enterprise.id) - billable_period.update_attributes({ - ends_at: ends_at, - sells: sells, - trial: trial, - owner_id: owner_id, - turnover: orders.sum(&:total) - }) + + unless account_invoice.order.andand.complete? + billable_period ||= BillablePeriod.new(account_invoice_id: account_invoice.id, begins_at: begins_at, enterprise_id: enterprise.id) + billable_period.update_attributes({ + ends_at: ends_at, + sells: sells, + trial: trial, + owner_id: owner_id, + turnover: orders.sum(&:total) + }) + end billable_period.touch end diff --git a/spec/jobs/update_billable_periods_spec.rb b/spec/jobs/update_billable_periods_spec.rb index 62c1d63171..ef6d2b12ad 100644 --- a/spec/jobs/update_billable_periods_spec.rb +++ b/spec/jobs/update_billable_periods_spec.rb @@ -14,10 +14,6 @@ describe UpdateBillablePeriods do describe "perform", versioning: true do let!(:enterprise) { create(:supplier_enterprise, created_at: start_of_july - 1.month, sells: 'any') } - before do - allow(Enterprise).to receive(:where) { double(:enterprises, select: [enterprise]) } - end - context "when no date arguments are passed to the job" do before do expect(updater).to receive(:clean_up_untouched_billable_periods_for).once @@ -419,6 +415,23 @@ describe UpdateBillablePeriods do ]} end + context "when the account invoice is already_complete" do + before do + allow(BillablePeriod).to receive(:where) { [existing] } + allow(existing.account_invoice).to receive(:order) { double(:order, complete?: true ) } + allow(AccountInvoice).to receive(:find_or_create_by_user_id_and_year_and_month) { existing.account_invoice } + end + + it "does not update the billing period, but changes updated_at by touching the billable period " do + expect(existing).to_not receive(:update_attributes) + expect(existing).to receive(:touch) + expect(Bugsnag).to_not receive(:notify) + expect{ + updater.update_billable_period(enterprise, start_of_july, start_of_july + 20.days, false) + }.to_not change{ BillablePeriod.count } + end + end + context "when arguments match both 'begins_at' and 'enterprise_id' of an existing billable period" do it "updates the existing billable period" do expect{