BillablePeriods don't get updated when account_invoice has been finalized

This commit is contained in:
Rob Harrington
2015-10-09 12:08:53 +11:00
parent 1ad0f95536
commit b22d591775
2 changed files with 28 additions and 12 deletions

View File

@@ -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

View File

@@ -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{