Prevent invoices from being sent unless the distributor of an order has set their ABN

This commit is contained in:
Rob Harrington
2015-10-21 16:23:48 +11:00
parent 9b2653aa2d
commit ef08977dbe
6 changed files with 41 additions and 7 deletions

View File

@@ -17,6 +17,8 @@ Spree::Admin::OrdersController.class_eval do
# instead of the update_distribution_charge method.
after_filter :update_distribution_charge, :only => :update
before_filter :require_distributor_abn, only: :invoice
respond_override :index => { :html =>
{ :success => lambda {
# Filter orders to only show those distributed by current user (or all for admin user)
@@ -63,4 +65,13 @@ Spree::Admin::OrdersController.class_eval do
@orders = permissions.editable_orders.order(:id).ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
render json: @orders, each_serializer: Api::Admin::OrderSerializer
end
private
def require_distributor_abn
unless @order.distributor.abn.present?
flash[:error] = t(:must_have_valid_business_number, enterprise_name: @order.distributor.name)
respond_with(@order) { |format| format.html { redirect_to edit_admin_order_path(@order) } }
end
end
end

View File

@@ -334,6 +334,10 @@ class Enterprise < ActiveRecord::Base
shop_trial_start_date.andand + Enterprise::SHOP_TRIAL_LENGTH.days
end
def can_invoice?
abn.present?
end
protected
def devise_mailer

View File

@@ -1,3 +1,6 @@
/ insert_after "code[erb-loud]:contains('button_link_to t(:resend)')"
- if @order.complete?
%li= button_link_to t(:invoice), invoice_admin_order_url(@order), :method => :put, :icon => 'icon-email', :data => { :confirm => t(:are_you_sure) }
- if @order.distributor.can_invoice?
%li= button_link_to t(:invoice), invoice_admin_order_url(@order), :icon => 'icon-email', :data => { :confirm => t(:confirm_send_invoice) }
- else
%li= button_link_to t(:invoice), "#", :icon => 'icon-email', :data => { :confirm => t(:must_have_valid_business_number, enterprise_name: @order.distributor.name) }

View File

@@ -20,6 +20,8 @@ en:
producers_join: Australian producers are now welcome to join the Open Food Network.
charges_sales_tax: Charges GST?
print: "Print"
confirm_send_invoice: "An invoice for this order will be sent to the customer. Are you sure you want to continue?"
must_have_valid_business_number: "%{enterprise_name} must have a valid ABN before invoices can be sent."
logo: "Logo (640x130)"
logo_mobile: "Mobile logo (75x26)"

View File

@@ -204,7 +204,7 @@ Spree::Core::Engine.routes.prepend do
end
resources :orders do
put :invoice, on: :member
get :invoice, on: :member
get :print, on: :member
get :managed, on: :collection
end

View File

@@ -192,11 +192,25 @@ describe Spree::Admin::OrdersController do
context "which is a manager of the distributor for an order" do
before { controller.stub spree_current_user: distributor.owner }
it "should allow me to send order invoices" do
expect do
spree_get :invoice, params
end.to change{Spree::OrderMailer.deliveries.count}.by(1)
expect(response).to redirect_to spree.edit_admin_order_path(order)
context "when the distributor's ABN has not been set" do
before { distributor.update_attribute(:abn, "") }
it "should allow me to send order invoices" do
expect do
spree_get :invoice, params
end.to_not change{Spree::OrderMailer.deliveries.count}
expect(response).to redirect_to spree.edit_admin_order_path(order)
expect(flash[:error]).to eq "#{distributor.name} must have a valid ABN before invoices can be sent."
end
end
context "when the distributor's ABN has been set" do
before { distributor.update_attribute(:abn, "123") }
it "should allow me to send order invoices" do
expect do
spree_get :invoice, params
end.to change{Spree::OrderMailer.deliveries.count}.by(1)
expect(response).to redirect_to spree.edit_admin_order_path(order)
end
end
end
end