diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 080b1e5251..fd61e1e709 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -49,6 +49,12 @@ Spree.user_class.class_eval do owned_enterprises(:reload).size < enterprise_limit end + def current_invoice + start_of_current_billing_period = (Time.now - 1.day).beginning_of_month + existing = orders.where('distributor_id = (?) AND created_at >= (?) AND completed_at IS NULL', + Spree::Config[:accounts_distributor_id], start_of_current_billing_period).first + existing || orders.new(distributor_id: Spree::Config[:accounts_distributor_id]) + end private diff --git a/spec/models/spree/user_spec.rb b/spec/models/spree/user_spec.rb index 848febb9c5..b096055d85 100644 --- a/spec/models/spree/user_spec.rb +++ b/spec/models/spree/user_spec.rb @@ -56,9 +56,9 @@ describe Spree.user_class do end describe "known_users" do - let!(:u1) { create_enterprise_user } - let!(:u2) { create_enterprise_user } - let!(:u3) { create_enterprise_user } + let!(:u1) { create(:user) } + let!(:u2) { create(:user) } + let!(:u3) { create(:user) } let!(:e1) { create(:enterprise, owner: u1, users: [u1, u2]) } describe "as an enterprise user" do @@ -73,9 +73,52 @@ describe Spree.user_class do describe "as admin" do let(:admin) { quick_login_as_admin } + it "returns all users" do expect(admin.known_users).to include u1, u2, u3 end end end + + describe "current_invoice" do + let!(:user) { create(:user) } + let!(:accounts_distributor) { create(:distributor_enterprise) } + let!(:start_of_month) { Time.now.beginning_of_month } + + before do + Spree::Config.accounts_distributor_id = accounts_distributor.id + end + + context "where no relevant invoice exists for the current month" do + # Created during previous month + let!(:order1) { create(:order, user: user, created_at: start_of_month - 20.days, completed_at: nil, distributor: accounts_distributor) } + # Already Completed + let!(:order2) { create(:order, user: user, created_at: start_of_month + 3.hours, completed_at: start_of_month + 3.days, distributor: accounts_distributor) } + # Incorrect distributor + let!(:order3) { create(:order, user: user, created_at: start_of_month + 3.hours, completed_at: nil, distributor: create(:distributor_enterprise)) } + # Incorrect user + let!(:order4) { create(:order, user: create(:user), created_at: start_of_month + 3.hours, completed_at: nil, distributor: accounts_distributor) } + + around { |example| Timecop.travel(start_of_month + 20.days) { example.run } } + + it "creates a new invoice" do + current_invoice = user.current_invoice + expect(current_invoice).to be_a_new Spree::Order + expect(current_invoice.completed_at).to be nil + expect(current_invoice.distributor).to eq accounts_distributor + expect(current_invoice.user).to eq user + end + end + + context "where an invoice exists for the current month" do + let!(:order) { create(:order, user: user, created_at: start_of_month + 3.hours, completed_at: nil, distributor: accounts_distributor) } + + around { |example| Timecop.travel(start_of_month + 20.days) { example.run } } + + it "returns the existing invoice" do + current_invoice = user.current_invoice + expect(current_invoice).to eq order + end + end + end end