diff --git a/app/controllers/admin/accounts_and_billing_settings_controller.rb b/app/controllers/admin/accounts_and_billing_settings_controller.rb index ec95e45307..86754ce571 100644 --- a/app/controllers/admin/accounts_and_billing_settings_controller.rb +++ b/app/controllers/admin/accounts_and_billing_settings_controller.rb @@ -14,9 +14,14 @@ class Admin::AccountsAndBillingSettingsController < Spree::Admin::BaseController end def update - @settings = OpenFoodNetwork::AccountsAndBillingSettings.new(params[:settings]) + @settings = OpenFoodNetwork::AccountsAndBillingSettings.new(params[:settings], params[:button]) if @settings.valid? Spree::Config.set(params[:settings]) + + if params[:button] == "update_and_run_job" + Delayed::Job.enqueue UpdateBillablePeriods.new({create_invoices: true}) + end + flash[:success] = t(:successfully_updated, :resource => t(:billing_and_account_settings)) redirect_to main_app.edit_admin_accounts_and_billing_settings_path else diff --git a/app/views/admin/accounts_and_billing_settings/edit.html.haml b/app/views/admin/accounts_and_billing_settings/edit.html.haml index eea938532f..71c21623e0 100644 --- a/app/views/admin/accounts_and_billing_settings/edit.html.haml +++ b/app/views/admin/accounts_and_billing_settings/edit.html.haml @@ -17,18 +17,28 @@ = f.hidden_field :default_accounts_shipping_method_id, value: '' %div{ 'method-settings-for' => 'enterprise_id' } - .row - .six.columns.alpha - .field - = f.hidden_field :collect_billing_information, value: '0' - = f.check_box :collect_billing_information, value: '1' - = f.label :collect_billing_information, t(:collect_billing_information) + -# .row + -# .six.columns.alpha + -# %fieldset.no-border-bottom + -# %legend Billing Information + -# .field + -# = f.hidden_field :collect_billing_information, value: '0' + -# = f.check_box :collect_billing_information, value: '1' + -# = f.label :collect_billing_information, t(:collect_billing_information) + -# .field + -# = button t(:collect_now), 'icon-forward' - .six.columns.omega - .field - = f.hidden_field :create_invoices_for_enterprise_users, value: '0' - = f.check_box :create_invoices_for_enterprise_users, value: '1' - = f.label :create_invoices_for_enterprise_users, t(:create_invoices_for_enterprise_users) + -# .six.columns.omega + -# %fieldset.no-border-bottom + -# %legend User Invoices + -# .field + -# = f.hidden_field :create_invoices_for_enterprise_users, value: '0' + -# = f.check_box :create_invoices_for_enterprise_users, value: '1' + -# = f.label :create_invoices_for_enterprise_users, t(:create_invoices_for_enterprise_users) + -# .field + -# = button t(:create_now), 'icon-forward' .form-buttons{"data-hook" => "buttons"} - = button t(:update), 'icon-refresh' + = button t(:update), 'icon-refresh', value: "update" + or + = button t(:update_and_create_invoices_now), 'icon-undo', value: 'update_and_run_job' diff --git a/lib/open_food_network/accounts_and_billing_settings.rb b/lib/open_food_network/accounts_and_billing_settings.rb index bead690e3f..2bd5883886 100644 --- a/lib/open_food_network/accounts_and_billing_settings.rb +++ b/lib/open_food_network/accounts_and_billing_settings.rb @@ -4,14 +4,17 @@ module OpenFoodNetwork class AccountsAndBillingSettings include ActiveModel::Validations - attr_accessor :accounts_distributor_id, :collect_billing_information, :create_invoices_for_enterprise_users - attr_accessor :default_accounts_payment_method_id, :default_accounts_shipping_method_id - validate :ensure_accounts_distributor_set, unless: lambda { create_invoices_for_enterprise_users == '0' } - validate :ensure_billing_info_collected, unless: lambda { create_invoices_for_enterprise_users == '0' } - validate :ensure_default_methods_set, unless: lambda { create_invoices_for_enterprise_users == '0' } - def initialize(attr) + attr_accessor :accounts_distributor_id, :default_accounts_payment_method_id, :default_accounts_shipping_method_id + # attr_accessor :collect_billing_information, :create_invoices_for_enterprise_users + + validate :ensure_accounts_distributor_set, if: lambda { @button == 'update_and_run_job' } + validate :ensure_default_methods_set, if: lambda { @button == 'update_and_run_job' } + # validate :ensure_billing_info_collected, unless: lambda { create_invoices_for_enterprise_users == '0' } + + def initialize(attr, button=nil) attr.each { |k,v| instance_variable_set("@#{k}", v) } + @button = button end def ensure_accounts_distributor_set @@ -20,12 +23,6 @@ module OpenFoodNetwork end end - def ensure_billing_info_collected - unless collect_billing_information == '1' - errors.add(:billing_information, "must be collected if you wish to create invoices for enterprise users.") - end - end - def ensure_default_methods_set unless Enterprise.find_by_id(accounts_distributor_id) && Enterprise.find_by_id(accounts_distributor_id).payment_methods.find_by_id(default_accounts_payment_method_id) @@ -37,5 +34,11 @@ module OpenFoodNetwork errors.add(:default_shipping_method, "must be set if you wish to create invoices for enterprise users.") end end + + # def ensure_billing_info_collected + # unless collect_billing_information == '1' + # errors.add(:billing_information, "must be collected if you wish to create invoices for enterprise users.") + # end + # end end end diff --git a/spec/controllers/admin/accounts_and_billing_settings_controller_spec.rb b/spec/controllers/admin/accounts_and_billing_settings_controller_spec.rb index dae301a4b7..c0af5fc7ea 100644 --- a/spec/controllers/admin/accounts_and_billing_settings_controller_spec.rb +++ b/spec/controllers/admin/accounts_and_billing_settings_controller_spec.rb @@ -60,24 +60,25 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do before {allow(controller).to receive(:spree_current_user) { admin } } let(:params) { { settings: { } } } - context "when create_invoices_for_enterprise_users is false" do - before { params[:settings][:create_invoices_for_enterprise_users] = '0' } + context "when we are not creating user invoices" do + before { params[:button] = 'update' } - context "and other settings are not set" do + context "and settings have no values" do before do params[:settings][:accounts_distributor_id] = '' params[:settings][:default_accounts_payment_method_id] = '0' params[:settings][:default_accounts_shipping_method_id] = '0' - params[:settings][:collect_billing_information] = '0' + # params[:settings][:collect_billing_information] = '0' spree_get :update, params end it "allows them to be empty/false" do + expect(assigns(:settings).errors.count).to be 0 expect(Spree::Config.accounts_distributor_id).to eq 0 expect(Spree::Config.default_accounts_payment_method_id).to eq 0 expect(Spree::Config.default_accounts_shipping_method_id).to eq 0 - expect(Spree::Config.collect_billing_information).to be false - expect(Spree::Config.create_invoices_for_enterprise_users).to be false + # expect(Spree::Config.collect_billing_information).to be false + # expect(Spree::Config.create_invoices_for_enterprise_users).to be false end end @@ -86,7 +87,7 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do params[:settings][:accounts_distributor_id] = new_distributor.id params[:settings][:default_accounts_payment_method_id] = pm2.id params[:settings][:default_accounts_shipping_method_id] = sm2.id - params[:settings][:collect_billing_information] = '1' + # params[:settings][:collect_billing_information] = '1' spree_get :update, params end @@ -94,32 +95,32 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do expect(Spree::Config.accounts_distributor_id).to eq new_distributor.id expect(Spree::Config.default_accounts_payment_method_id).to eq pm2.id expect(Spree::Config.default_accounts_shipping_method_id).to eq sm2.id - expect(Spree::Config.collect_billing_information).to be true - expect(Spree::Config.create_invoices_for_enterprise_users).to be false + # expect(Spree::Config.collect_billing_information).to be true + # expect(Spree::Config.create_invoices_for_enterprise_users).to be false end end end - context "when create_invoices_for_enterprise_users is true" do - before { params[:settings][:create_invoices_for_enterprise_users] = '1' } + context "when we are creating user invoices" do + before { params[:button] = 'update_and_run_job' } - context "and other settings are not set" do + context "and settings have no values" do before do params[:settings][:accounts_distributor_id] = '' params[:settings][:default_accounts_payment_method_id] = '0' params[:settings][:default_accounts_shipping_method_id] = '0' - params[:settings][:collect_billing_information] = '0' + # params[:settings][:collect_billing_information] = '0' spree_get :update, params end it "does not allow them to be empty/false" do expect(response).to render_template :edit - expect(assigns(:settings).errors.count).to be 4 + expect(assigns(:settings).errors.count).to be 3 expect(Spree::Config.accounts_distributor_id).to eq accounts_distributor.id expect(Spree::Config.default_accounts_payment_method_id).to eq pm1.id expect(Spree::Config.default_accounts_shipping_method_id).to eq sm1.id - expect(Spree::Config.collect_billing_information).to be true - expect(Spree::Config.create_invoices_for_enterprise_users).to be false + # expect(Spree::Config.collect_billing_information).to be true + # expect(Spree::Config.create_invoices_for_enterprise_users).to be false end end @@ -128,16 +129,20 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do params[:settings][:accounts_distributor_id] = new_distributor.id params[:settings][:default_accounts_payment_method_id] = pm2.id params[:settings][:default_accounts_shipping_method_id] = sm2.id - params[:settings][:collect_billing_information] = '1' - spree_get :update, params + # params[:settings][:collect_billing_information] = '1' end it "sets global config to the specified values" do + spree_get :update, params expect(Spree::Config.accounts_distributor_id).to eq new_distributor.id expect(Spree::Config.default_accounts_payment_method_id).to eq pm2.id expect(Spree::Config.default_accounts_shipping_method_id).to eq sm2.id - expect(Spree::Config.collect_billing_information).to be true - expect(Spree::Config.create_invoices_for_enterprise_users).to be true + # expect(Spree::Config.collect_billing_information).to be true + # expect(Spree::Config.create_invoices_for_enterprise_users).to be false + end + + it "runs the job" do + expect{spree_get :update, params}.to enqueue_job UpdateBillablePeriods end end end