diff --git a/app/jobs/finalize_user_invoices.rb b/app/jobs/finalize_user_invoices.rb index 6281aec5bd..20abb35095 100644 --- a/app/jobs/finalize_user_invoices.rb +++ b/app/jobs/finalize_user_invoices.rb @@ -1,15 +1,17 @@ -FinalizeUserInvoices = Struct.new("FinalizeUserInvoices") do +FinalizeUserInvoices = Struct.new("FinalizeUserInvoices", :year, :month) do + def before(job) + UpdateBillablePeriods.new.perform(start_date.year, start_date.month) + UpdateUserInvoices.new.perform(start_date.year, start_date.month) + end def perform + return unless end_date <= Time.now return unless accounts_distributor = Enterprise.find_by_id(Spree::Config.accounts_distributor_id) return unless accounts_distributor.payment_methods.find_by_id(Spree::Config.default_accounts_payment_method_id) return unless accounts_distributor.shipping_methods.find_by_id(Spree::Config.default_accounts_shipping_method_id) - start_date = (Time.now.beginning_of_month - 1.month) + 1.day - end_date = Time.now.beginning_of_month + 1.day - invoices = Spree::Order.where('distributor_id = (?) AND created_at >= (?) AND created_at <= (?) AND completed_at IS NULL', - accounts_distributor, start_date, end_date) + accounts_distributor, start_date + 1.day, end_date + 1.day) invoices.each do |invoice| finalize(invoice) @@ -26,4 +28,26 @@ FinalizeUserInvoices = Struct.new("FinalizeUserInvoices") 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/spec/jobs/finalize_user_invoices_spec.rb b/spec/jobs/finalize_user_invoices_spec.rb index 2435d47808..9e0ff95722 100644 --- a/spec/jobs/finalize_user_invoices_spec.rb +++ b/spec/jobs/finalize_user_invoices_spec.rb @@ -16,6 +16,7 @@ describe FinalizeUserInvoices do let!(:invoice2) { create(:order, distributor: accounts_distributor, created_at: start_of_july - 10.days, completed_at: start_of_july - 10.days) } let!(:invoice3) { create(:order, distributor: accounts_distributor, created_at: start_of_july + 3.hours, completed_at: nil) } let!(:invoice4) { create(:order, distributor: accounts_distributor, created_at: start_of_july + 10.days, completed_at: nil) } + let!(:invoice5) { create(:order, distributor: accounts_distributor, created_at: start_of_july - 30.days + 3.hours, completed_at: nil) } before do allow(Enterprise).to receive(:find_by_id) { accounts_distributor } @@ -62,14 +63,47 @@ describe FinalizeUserInvoices do end context "when necessary global config setting have been set" do - travel_to(3.days) + context "and no 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 - finalizer.perform - expect(finalizer).to have_received(:finalize).with(invoice1) - expect(finalizer).to have_received(:finalize).with(invoice3) - expect(finalizer).to_not have_received(:finalize).with(invoice2) - expect(finalizer).to_not have_received(:finalize).with(invoice4) + it "finalizes the uncompleted orders for accounts_distributor created in the previous calendar month (or on the 1st of this month)" do + finalizer.perform + expect(finalizer).to have_received(:finalize).with(invoice1) + expect(finalizer).to have_received(:finalize).with(invoice3) + expect(finalizer).to_not have_received(:finalize).with(invoice2) + expect(finalizer).to_not have_received(:finalize).with(invoice4) + expect(finalizer).to_not have_received(:finalize).with(invoice5) + end + end + + context "and a specfic year and month are passed as arguments" do + let!(:finalizer) { FinalizeUserInvoices.new(Time.now.year, 6) } + + before do + allow(finalizer).to receive(:finalizer) + end + + context "that ends in the past" do + travel_to(3.hours) + + it "finalizes the uncompleted orders for accounts_distributor created in the specified calendar month (or on the first of the following month)" do + finalizer.perform + expect(finalizer).to have_received(:finalize).with(invoice1) + expect(finalizer).to have_received(:finalize).with(invoice3) + expect(finalizer).to_not have_received(:finalize).with(invoice2) + expect(finalizer).to_not have_received(:finalize).with(invoice4) + expect(finalizer).to_not have_received(:finalize).with(invoice5) + end + end + + context "that ends in the future" do + travel_to -1.day + + it "does not finalize any orders" do + finalizer.perform + expect(finalizer).to_not have_received(:finalize) + end + end end end end