From c53b5f3edb7b0b7780206a67cb43ff168d5009cf Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Mon, 6 Jul 2015 15:46:14 +0800 Subject: [PATCH] Adding finalize step to accounts and billing settigns page + controller --- .../accounts_and_billing_settings.css.scss | 10 +++ ...ccounts_and_billing_settings_controller.rb | 18 ++-- .../edit.html.haml | 89 ++++++++++++++----- config/routes.rb | 2 +- ...ts_and_billing_settings_controller_spec.rb | 61 +++++++++---- 5 files changed, 135 insertions(+), 45 deletions(-) create mode 100644 app/assets/stylesheets/admin/accounts_and_billing_settings.css.scss diff --git a/app/assets/stylesheets/admin/accounts_and_billing_settings.css.scss b/app/assets/stylesheets/admin/accounts_and_billing_settings.css.scss new file mode 100644 index 0000000000..775e50cd90 --- /dev/null +++ b/app/assets/stylesheets/admin/accounts_and_billing_settings.css.scss @@ -0,0 +1,10 @@ +.step { + a.explain:hover { + cursor: pointer; + } + + p.explanation { + margin-top: 20px; + display: none; + } +} diff --git a/app/controllers/admin/accounts_and_billing_settings_controller.rb b/app/controllers/admin/accounts_and_billing_settings_controller.rb index edb9a21882..259b20027c 100644 --- a/app/controllers/admin/accounts_and_billing_settings_controller.rb +++ b/app/controllers/admin/accounts_and_billing_settings_controller.rb @@ -14,10 +14,10 @@ class Admin::AccountsAndBillingSettingsController < Spree::Admin::BaseController end def start_job - if @billing_period_job || @user_invoice_job + if @update_billable_periods_job || @update_user_invoices_job || @finalize_user_invoices_job flash[:error] = "A task is already running, please wait until it has finished" else - new_job = "update_#{params[:job_name]}".camelize.constantize.new + new_job = "#{params[:job][:name]}".camelize.constantize.new Delayed::Job.enqueue new_job flash[:success] = "Task Queued" end @@ -42,10 +42,13 @@ class Admin::AccountsAndBillingSettingsController < Spree::Admin::BaseController render :edit unless @settings.valid? end + def known_jobs + ['update_billable_periods', 'update_user_invoices', 'finalize_user_invoices'] + end + def require_known_job - known_jobs = ['user_invoices', 'billable_periods'] - unless known_jobs.include?(params[:job_name]) - flash[:error] = "Unknown Task: #{params[:job_name].to_s}" + unless known_jobs.include?(params[:job][:name]) + flash[:error] = "Unknown Task: #{params[:job][:name].to_s}" redirect_to_edit end end @@ -65,7 +68,8 @@ class Admin::AccountsAndBillingSettingsController < Spree::Admin::BaseController end def load_jobs - @billing_period_job = Delayed::Job.where("handler LIKE (?)", "%Struct::UpdateBillablePeriods%").last - @user_invoice_job = Delayed::Job.where("handler LIKE (?)", "%Struct::UpdateUserInvoices%").last + @update_billable_periods_job = Delayed::Job.where("handler LIKE (?)", "%Struct::UpdateBillablePeriods%").last + @update_user_invoices_job = Delayed::Job.where("handler LIKE (?)", "%Struct::UpdateUserInvoices%").last + @finalize_user_invoices_job = Delayed::Job.where("handler LIKE (?)", "%Struct::FinalizeUserInvoices%").last end end 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 3182bb09a2..3b02a76a67 100644 --- a/app/views/admin/accounts_and_billing_settings/edit.html.haml +++ b/app/views/admin/accounts_and_billing_settings/edit.html.haml @@ -5,6 +5,8 @@ = render 'spree/shared/error_messages', target: @settings +-# - month_options = (0...12).map { |i| Time.now.beginning_of_month - i.months }.map{ |t| [t.strftime("%b %Y"), t.strftime("%b %Y %z")]} + .row{ ng: { app: 'admin.accounts_and_billing_settings' } } .twelve.columns.alpha.omega = form_for @settings, as: :settings, url: main_app.admin_accounts_and_billing_settings_path, :method => :put do |f| @@ -22,22 +24,21 @@ = button t(:update), 'icon-refresh', value: "update" .row - .six.columns.alpha + .four.columns.alpha.step.text-center %fieldset.no-border-bottom - %legend Billing Information + %legend Step 1 - %p - Use this button to calculate the billable total for the month to date for each enterprise in the system. Do this before generating invoices on the right. + = form_for :job, url: main_app.start_job_admin_accounts_and_billing_settings_path, method: :post do |f| + = f.hidden_field :name, value: "update_billable_periods" + -# = f.select :start, options_for_select(month_options), {}, class: "fullwidth select2" + .form-buttons{"data-hook" => "buttons"} + = button "Calculate Bill Totals", "icon-bar-chart button fullwidth", :submit, disabled: @update_billable_periods_job.present? %br - .form-buttons{"data-hook" => "buttons"} - = link_to_with_icon "icon-bar-chart", "Calculate Totals Now", main_app.start_job_admin_accounts_and_billing_settings_path(job_name: "billable_periods"), { class: 'button', disabled: @billing_period_job.present?} - - - - if @billing_period_job + - if @update_billable_periods_job %p.text-center - - if @billing_period_job.run_at < Time.now + - if @update_billable_periods_job.run_at < Time.now %strong In Progress %br Started at: @@ -45,23 +46,29 @@ %strong Queued %br Scheduled for: - = @billing_period_job.run_at + = @update_billable_periods_job.run_at + - else + %a.explain What is this? - .six.columns.alpha + %p.explanation + Use this button to immediately calculate the billable total for the month to date for each enterprise in the system. Do this before generating invoices on the right. This task is run automatically every night. + + + .four.columns.step.text-center %fieldset.no-border-bottom - %legend User Invoices + %legend Step 2 - %p - Use this button to update invoices for the month to date for each enterprise user in the system. Do this after calculating totals on the left. + = form_for :job, url: main_app.start_job_admin_accounts_and_billing_settings_path, method: :post do |f| + = f.hidden_field :name, value: "update_user_invoices" + -# = f.select :start, options_for_select(month_options), {}, class: "fullwidth select2" + .form-buttons{"data-hook" => "buttons"} + = button "Update User Invoices", "icon-undo button fullwidth", :submit, disabled: @update_user_invoices_job.present? %br - .form-buttons{"data-hook" => "buttons"} - = link_to_with_icon "icon-undo", "Update Invoices Now", main_app.start_job_admin_accounts_and_billing_settings_path(job_name: "user_invoices"), class: 'button', disabled: @user_invoice_job.present? - - - if @user_invoice_job + - if @update_user_invoices_job %p.text-center - - if @user_invoice_job.run_at < Time.now + - if @update_user_invoices_job.run_at < Time.now %strong In Progress %br Started at: @@ -69,4 +76,44 @@ %strong Queued %br Scheduled for: - = @user_invoice_job.run_at + = @update_user_invoices_job.run_at + - else + %a.explain What is this? + + %p.explanation + Use this button to immediately update invoices for the month to date for each enterprise user in the system. Do this after calculating totals on the left. This task is run automatically every night. + + + .four.columns.omega.step.text-center + %fieldset.no-border-bottom + %legend Step 3 + + = form_for :job, url: main_app.start_job_admin_accounts_and_billing_settings_path, method: :post do |f| + = f.hidden_field :name, value: "finalize_user_invoices" + -# = f.select :start, options_for_select(month_options), {}, class: "fullwidth select2" + .form-buttons{"data-hook" => "buttons"} + = button "Finalise User Invoices", "icon-ok-sign button fullwidth", :submit, disabled: @finalize_user_invoices_job.present? + + %br + + - if @finalize_user_invoices_job + %p.text-center + - if @finalize_user_invoices_job.run_at < Time.now + %strong In Progress + %br + Started at: + - else + %strong Queued + %br + Scheduled for: + = @finalize_user_invoices_job.run_at + - else + %a.explain What is this? + + %p.explanation + Use this button to finalize all invoices in the system for the pervious calendar month. Do this after steps one and two. This task is automatically run once a month. + + +:coffee + $('a.explain').click -> + $(this).siblings('.explanation').slideToggle(200) diff --git a/config/routes.rb b/config/routes.rb index f2ae6ea78f..2b94b0a4cf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -107,7 +107,7 @@ Openfoodnetwork::Application.routes.draw do resource :accounts_and_billing_settings, only: [:edit, :update] do collection do get :show_methods - get :start_job + post :start_job 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 c9f093e151..1e6e096525 100644 --- a/spec/controllers/admin/accounts_and_billing_settings_controller_spec.rb +++ b/spec/controllers/admin/accounts_and_billing_settings_controller_spec.rb @@ -15,8 +15,8 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do accounts_distributor_id: accounts_distributor.id, default_accounts_payment_method_id: pm1.id, default_accounts_shipping_method_id: sm1.id, - collect_billing_information: true, - create_invoices_for_enterprise_users: false + # collect_billing_information: true, + # create_invoices_for_enterprise_users: false }) end @@ -104,7 +104,7 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do context "as an enterprise user" do before do allow(controller).to receive(:spree_current_user) { user } - spree_get :show_methods, enterprise_id: accounts_distributor.id + spree_post :start_job, enterprise_id: accounts_distributor.id end it "does not allow access" do @@ -122,7 +122,7 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do Spree::Config.set({ accounts_distributor_id: "" }) Spree::Config.set({ default_accounts_payment_method_id: "" }) Spree::Config.set({ default_accounts_shipping_method_id: "" }) - spree_get :start_job, job_name: "" + spree_post :start_job, job: { name: "" } end it "returns immediately and renders :edit" do @@ -138,9 +138,9 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do Spree::Config.set({ default_accounts_shipping_method_id: sm1.id }) end - context "and job_name is not billable_periods or user_invoices" do + context "and job_name is not on the known_jobs list" do before do - spree_get :start_job, job_name: "" + spree_post :start_job, job: { name: "" } end it "returns immediately with an error" do @@ -149,8 +149,8 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do end end - context "and job_name is billable_periods" do - let!(:params) { { job_name: "billable_periods" } } + context "and job_name is update_billable_periods" do + let!(:params) { { job: { name: "update_billable_periods" } } } context "and no jobs are currently running" do before do @@ -158,7 +158,7 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do end it "runs the job" do - expect{spree_get :start_job, params}.to enqueue_job UpdateBillablePeriods + expect{spree_post :start_job, params}.to enqueue_job UpdateBillablePeriods expect(flash[:success]).to eq "Task Queued" expect(response).to redirect_to edit_admin_accounts_and_billing_settings_path end @@ -167,19 +167,19 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do context "and there are jobs currently running" do before do allow(controller).to receive(:load_jobs) - controller.instance_variable_set("@billing_period_job", double(:billing_period_job)) + controller.instance_variable_set("@update_billing_periods_job", double(:update_billing_periods_job)) end it "does not run the job" do - expect{spree_get :start_job, params}.to_not enqueue_job UpdateBillablePeriods + expect{spree_post :start_job, params}.to_not enqueue_job UpdateBillablePeriods expect(flash[:error]).to eq "A task is already running, please wait until it has finished" expect(response).to redirect_to edit_admin_accounts_and_billing_settings_path end end end - context "and job_name is user_invoices" do - let!(:params) { { job_name: "user_invoices" } } + context "and job_name is update_user_invoices" do + let!(:params) { { job: { name: "update_user_invoices" } } } context "and no jobs are currently running" do before do @@ -187,7 +187,7 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do end it "runs the job" do - expect{spree_get :start_job, params}.to enqueue_job UpdateUserInvoices + expect{spree_post :start_job, params}.to enqueue_job UpdateUserInvoices expect(flash[:success]).to eq "Task Queued" expect(response).to redirect_to edit_admin_accounts_and_billing_settings_path end @@ -196,11 +196,40 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do context "and there are jobs currently running" do before do allow(controller).to receive(:load_jobs) - controller.instance_variable_set("@user_invoice_job", double(:user_invoice_job)) + controller.instance_variable_set("@update_user_invoices_job", double(:update_user_invoices_job)) end it "does not run the job" do - expect{spree_get :start_job, params}.to_not enqueue_job UpdateUserInvoices + expect{spree_post :start_job, params}.to_not enqueue_job UpdateUserInvoices + expect(flash[:error]).to eq "A task is already running, please wait until it has finished" + expect(response).to redirect_to edit_admin_accounts_and_billing_settings_path + end + end + end + + context "and job_name is finalize_user_invoices" do + let!(:params) { { job: { name: "finalize_user_invoices" } } } + + context "and no jobs are currently running" do + before do + allow(controller).to receive(:load_jobs) + end + + it "runs the job" do + expect{spree_post :start_job, params}.to enqueue_job FinalizeUserInvoices + expect(flash[:success]).to eq "Task Queued" + expect(response).to redirect_to edit_admin_accounts_and_billing_settings_path + end + end + + context "and there are jobs currently running" do + before do + allow(controller).to receive(:load_jobs) + controller.instance_variable_set("@finalize_user_invoices_job", double(:finalize_user_invoices_job)) + end + + it "does not run the job" do + expect{spree_post :start_job, params}.to_not enqueue_job FinalizeUserInvoices expect(flash[:error]).to eq "A task is already running, please wait until it has finished" expect(response).to redirect_to edit_admin_accounts_and_billing_settings_path end