diff --git a/app/jobs/finalize_user_invoices.rb b/app/jobs/finalize_user_invoices.rb index 20abb35095..b99c0328a5 100644 --- a/app/jobs/finalize_user_invoices.rb +++ b/app/jobs/finalize_user_invoices.rb @@ -1,7 +1,14 @@ -FinalizeUserInvoices = Struct.new("FinalizeUserInvoices", :year, :month) do +class FinalizeUserInvoices + attr_reader :start_date, :end_date + + def initialize(start_date = nil, end_date = nil) + @start_date = start_date || Time.now.beginning_of_month - 1.month + @end_date = end_date || Time.now.beginning_of_month + end + def before(job) - UpdateBillablePeriods.new.perform(start_date.year, start_date.month) - UpdateUserInvoices.new.perform(start_date.year, start_date.month) + UpdateBillablePeriods.new(start_date, end_date).perform + UpdateUserInvoices.new(start_date, end_date).perform end def perform @@ -28,26 +35,4 @@ FinalizeUserInvoices = Struct.new("FinalizeUserInvoices", :year, :month) do invoice.next end end - - private - - def start_date - # Start at the beginning of the specified month - # or at the beginning of last month if no month is specified - @start_date ||= if month && year - Time.new(year, month) - else - Time.now.beginning_of_month - 1.month - end - end - - def end_date - # Stop at the end of the specified month - # or at the beginning of this month if no month is specified - @end_date ||= if month && year - Time.new(year, month) + 1.month - else - Time.now.beginning_of_month - end - end end diff --git a/app/jobs/update_billable_periods.rb b/app/jobs/update_billable_periods.rb index 82d9973874..3a60d51435 100644 --- a/app/jobs/update_billable_periods.rb +++ b/app/jobs/update_billable_periods.rb @@ -1,4 +1,11 @@ -UpdateBillablePeriods = Struct.new("UpdateBillablePeriods", :year, :month) do +class UpdateBillablePeriods + attr_reader :start_date, :end_date + + def initialize(start_date = nil, end_date = nil) + @start_date = start_date || (Time.now - 1.day).beginning_of_month + @end_date = end_date || Time.now.beginning_of_day + end + def perform return unless end_date <= Time.now @@ -92,26 +99,4 @@ UpdateBillablePeriods = Struct.new("UpdateBillablePeriods", :year, :month) do obsolete_billable_periods.each(&:delete) end - - private - - def start_date - # Start at the beginning of the specified month - # or at the beginning of the month (prior to midnight last night) if none specified - @start_date ||= if month && year - Time.new(year, month) - else - (Time.now - 1.day).beginning_of_month - end - end - - def end_date - # Stop at the end of the specified month - # or at midnight last night if no month is specified - @end_date ||= if month && year - Time.new(year, month) + 1.month - else - Time.now.beginning_of_day - end - end end diff --git a/app/jobs/update_user_invoices.rb b/app/jobs/update_user_invoices.rb index 56893fccf1..99ce00898e 100644 --- a/app/jobs/update_user_invoices.rb +++ b/app/jobs/update_user_invoices.rb @@ -1,6 +1,13 @@ -UpdateUserInvoices = Struct.new("UpdateUserInvoices", :year, :month) do +class UpdateUserInvoices + attr_reader :start_date, :end_date + + def initialize(start_date = nil, end_date = nil) + @start_date = start_date || (Time.now - 1.day).beginning_of_month + @end_date = end_date || Time.now.beginning_of_day + end + def before(job) - UpdateBillablePeriods.new.perform(start_date.year, start_date.month) + UpdateBillablePeriods.new(start_date, end_date).perform end def perform @@ -76,26 +83,4 @@ UpdateUserInvoices = Struct.new("UpdateUserInvoices", :year, :month) do invoice.destroy end end - - private - - def start_date - # Start at the beginning of the specified month - # or at the beginning of the month (prior to midnight last night) if none specified - @start_date ||= if month && year - Time.new(year, month) - else - (Time.now - 1.day).beginning_of_month - end - end - - def end_date - # Stop at the end of the specified month - # or at midnight last night if no month is specified - @end_date ||= if month && year - Time.new(year, month) + 1.month - else - Time.now.beginning_of_day - end - end end diff --git a/spec/jobs/finalize_user_invoices_spec.rb b/spec/jobs/finalize_user_invoices_spec.rb index 9e0ff95722..dc0cc8d237 100644 --- a/spec/jobs/finalize_user_invoices_spec.rb +++ b/spec/jobs/finalize_user_invoices_spec.rb @@ -63,7 +63,7 @@ describe FinalizeUserInvoices do end context "when necessary global config setting have been set" do - context "and no arguments are passed to the job" do + context "and no date arguments are passed to the job" do travel_to(3.days) it "finalizes the uncompleted orders for accounts_distributor created in the previous calendar month (or on the 1st of this month)" do @@ -76,8 +76,8 @@ describe FinalizeUserInvoices do end end - context "and a specfic year and month are passed as arguments" do - let!(:finalizer) { FinalizeUserInvoices.new(Time.now.year, 6) } + context "and specfic start and end dates are passed as arguments" do + let!(:finalizer) { FinalizeUserInvoices.new(start_of_july - 1.month, start_of_july) } before do allow(finalizer).to receive(:finalizer) diff --git a/spec/jobs/update_billable_periods_spec.rb b/spec/jobs/update_billable_periods_spec.rb index 3d850587f3..7213fed5e8 100644 --- a/spec/jobs/update_billable_periods_spec.rb +++ b/spec/jobs/update_billable_periods_spec.rb @@ -17,7 +17,7 @@ describe UpdateBillablePeriods do allow(Enterprise).to receive(:select) { [enterprise] } end - context "when no arguments are passed to the job" do + context "when no date arguments are passed to the job" do before do expect(updater).to receive(:clean_up_untouched_billable_periods_for).once end @@ -43,8 +43,8 @@ describe UpdateBillablePeriods do end end - context "when a specfic year and month are passed as arguments" do - let!(:updater) { UpdateBillablePeriods.new(Time.now.year, 6) } + context "when specfic start and end dates are passed as arguments" do + let!(:updater) { UpdateBillablePeriods.new(start_of_july - 1.month, start_of_july) } before do allow(updater).to receive(:split_for_trial) diff --git a/spec/jobs/update_user_invoices_spec.rb b/spec/jobs/update_user_invoices_spec.rb index e8b49f8597..dfa78e0f32 100644 --- a/spec/jobs/update_user_invoices_spec.rb +++ b/spec/jobs/update_user_invoices_spec.rb @@ -58,8 +58,8 @@ describe UpdateUserInvoices do end end - context "when a specfic year and month are passed as arguments" do - let!(:updater) { UpdateUserInvoices.new(Time.now.year, 7) } + context "when specfic start and end dates are passed as arguments" do + let!(:updater) { UpdateUserInvoices.new(start_of_july, start_of_july + 1.month) } before do allow(updater).to receive(:update_invoice_for)