Adding option to 'Inherit From Product' for enterprise_fee tax categories

This commit is contained in:
Rob Harrington
2016-02-04 15:35:49 +11:00
parent a66582a8fb
commit caa8818f02
11 changed files with 48 additions and 63 deletions

View File

@@ -1,6 +1,6 @@
angular.module('admin.enterpriseFees').controller 'enterpriseFeesCtrl', ($scope, $http, $window, enterprises, tax_categories, calculators) ->
$scope.enterprises = enterprises
$scope.tax_categories = tax_categories
$scope.tax_categories = [{id: -1, name: "Inherit From Product"}].concat tax_categories
$scope.calculators = calculators
$scope.enterpriseFeesUrl = ->

View File

@@ -0,0 +1,15 @@
angular.module("admin.enterpriseFees").directive 'watchTaxCategory', ->
# In order to have a nice user experience on this page, we're modelling tax_category
# inheritance using tax_category_id = -1.
# This directive acts as a parser for tax_category_id, storing the value the form as "" when
# tax_category is to be inherited and setting inherits_tax_category as appropriate.
(scope, element, attrs) ->
scope.$watch 'enterprise_fee.tax_category_id', (value) ->
if value == -1
scope.enterprise_fee.inherits_tax_category = true
element.val("")
else
scope.enterprise_fee.inherits_tax_category = false
element.val(value)
scope.enterprise_fee.tax_category_id = -1 if scope.enterprise_fee.inherits_tax_category

View File

@@ -16,18 +16,15 @@ module Admin
respond_to do |format|
format.html
format.json { @presented_collection = @collection.each_with_index.map { |ef, i| EnterpriseFeePresenter.new(self, ef, i) } }
format.json { render_as_json @collection, controller: self, include_calculators: @include_calculators }
# format.json { @presented_collection = @collection.each_with_index.map { |ef, i| EnterpriseFeePresenter.new(self, ef, i) } }
end
end
def for_order_cycle
respond_to do |format|
format.html
format.json do
render json: ActiveModel::ArraySerializer.new( @collection,
each_serializer: Api::Admin::EnterpriseFeeSerializer, controller: self
).to_json
end
format.json { render_as_json @collection, controller: self }
end
end

View File

@@ -9,7 +9,7 @@ class EnterpriseFee < ActiveRecord::Base
calculated_adjustments
attr_accessible :enterprise_id, :fee_type, :name, :tax_category_id, :calculator_type
attr_accessible :enterprise_id, :fee_type, :name, :tax_category_id, :calculator_type, :inherits_tax_category
FEE_TYPES = %w(packing transport admin sales fundraising)
PER_ORDER_CALCULATORS = ['Spree::Calculator::FlatRate', 'Spree::Calculator::FlexiRate']

View File

@@ -1,31 +0,0 @@
class EnterpriseFeePresenter
def initialize(controller, enterprise_fee, index)
@controller, @enterprise_fee, @index = controller, enterprise_fee, index
end
delegate :id, :enterprise_id, :fee_type, :name, :tax_category_id, :calculator_type, :to => :enterprise_fee
def enterprise_fee
@enterprise_fee
end
def enterprise_name
@enterprise_fee.enterprise.andand.name
end
def calculator_description
@enterprise_fee.calculator.andand.description
end
def calculator_settings
result = nil
@controller.send(:with_format, :html) do
result = @controller.render_to_string :partial => 'admin/enterprise_fees/calculator_settings', :locals => {:enterprise_fee => @enterprise_fee, :index => @index}
end
result.gsub('[0]', '[{{ $index }}]').gsub('_0_', '_{{ $index }}_')
end
end

View File

@@ -1,5 +1,5 @@
class Api::Admin::EnterpriseFeeSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id, :fee_type, :name, :tax_category_id, :calculator_type
attributes :id, :enterprise_id, :fee_type, :name, :tax_category_id, :inherits_tax_category, :calculator_type
attributes :enterprise_name, :calculator_description, :calculator_settings
def enterprise_name
@@ -11,6 +11,8 @@ class Api::Admin::EnterpriseFeeSerializer < ActiveModel::Serializer
end
def calculator_settings
return nil unless options[:include_calculators]
result = nil
options[:controller].send(:with_format, :html) do

View File

@@ -20,23 +20,20 @@
%th.actions
%tbody
= enterprise_fee_set_form.ng_fields_for :collection do |f|
%tr{'ng-repeat' => 'enterprise_fee in enterprise_fees | filter:query' }
%tr{ ng: { repeat: 'enterprise_fee in enterprise_fees | filter:query' } }
%td
= f.ng_hidden_field :id
%ofn-select{ :id => angular_id(:enterprise_id), data: 'enterprises', include_blank: true, ng: { model: 'enterprise_fee.enterprise_id' } }
%input{ type: "hidden", name: angular_name(:enterprise_id), ng: { value: "enterprise_fee.enterprise_id" } }
-# = f.ng_collection_select :enterprise_id, @enterprises, :id, :name, 'enterprise_fee.enterprise_id', include_blank: false
%td= f.ng_select :fee_type, enterprise_fee_type_options, 'enterprise_fee.fee_type'
%td= f.ng_text_field :name, { placeholder: 'e.g. packing fee' }
%td
= f.ng_hidden_field :inherits_tax_category
%ofn-select{ :id => angular_id(:tax_category_id), data: 'tax_categories', include_blank: true, ng: { model: 'enterprise_fee.tax_category_id' } }
%input{ type: "hidden", name: angular_name(:tax_category_id), ng: { value: "enterprise_fee.tax_category_id" } }
-# = f.ng_collection_select :tax_category_id, @tax_categories, :id, :name, 'enterprise_fee.tax_category_id', include_blank: " "
%input{ type: "hidden", name: angular_name(:tax_category_id), 'watch-tax-category' => true }
%input{ type: "hidden", name: angular_name(:inherits_tax_category), ng: { value: "enterprise_fee.inherits_tax_category" } }
%td
%ofn-select.calculator_type{ :id => angular_id(:calculator_type), ng: { model: 'enterprise_fee.calculator_type' }, value_attr: 'name', text_attr: 'description', data: 'calculators', 'spree-ensure-calculator-preferences-match-type' => true }
%input{ type: "hidden", name: angular_name(:calculator_type), ng: { value: "enterprise_fee.calculator_type" } }
-# = f.ng_collection_select :calculator_type, @calculators, :name, :description, 'enterprise_fee.calculator_type', {'class' => 'calculator_type', 'spree-ensure-calculator-preferences-match-type' => "1"}
%td{'ng-bind-html-unsafe-compiled' => 'enterprise_fee.calculator_settings'}
%td.actions{'spree-delete-resource' => "1"}

View File

@@ -1,11 +0,0 @@
r.list_of :enterprise_fees, @presented_collection do
r.element :id
r.element :enterprise_id
r.element :enterprise_name
r.element :fee_type
r.element :name
r.element :tax_category_id
r.element :calculator_type
r.element :calculator_description
r.element :calculator_settings if @include_calculators
end

View File

@@ -0,0 +1,5 @@
class AddInheritsTaxCategoryToEnterpriseFees < ActiveRecord::Migration
def change
add_column :enterprise_fees, :inherits_tax_category, :boolean, null: false, default: false
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20151128185900) do
ActiveRecord::Schema.define(:version => 20160204031816) do
create_table "account_invoices", :force => true do |t|
t.integer "user_id", :null => false
@@ -235,9 +235,10 @@ ActiveRecord::Schema.define(:version => 20151128185900) do
t.integer "enterprise_id"
t.string "fee_type"
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "tax_category_id"
t.boolean "inherits_tax_category", :default => false, :null => false
end
add_index "enterprise_fees", ["enterprise_id"], :name => "index_enterprise_fees_on_enterprise_id"

View File

@@ -57,7 +57,7 @@ feature %q{
scenario "editing an enterprise fee" do
# Given an enterprise fee
fee = create(:enterprise_fee)
create(:enterprise, name: 'Foo')
enterprise = create(:enterprise, name: 'Foo')
# When I go to the enterprise fees page
login_to_admin_section
@@ -68,7 +68,7 @@ feature %q{
select 'Foo', from: 'enterprise_fee_set_collection_attributes_0_enterprise_id'
select 'Admin', from: 'enterprise_fee_set_collection_attributes_0_fee_type'
fill_in 'enterprise_fee_set_collection_attributes_0_name', with: 'Greetings!'
select '', from: 'enterprise_fee_set_collection_attributes_0_tax_category_id'
select 'Inherit From Product', from: 'enterprise_fee_set_collection_attributes_0_tax_category_id'
select 'Flat Percent', from: 'enterprise_fee_set_collection_attributes_0_calculator_type'
click_button 'Update'
@@ -76,8 +76,18 @@ feature %q{
page.should have_select "enterprise_fee_set_collection_attributes_0_enterprise_id", selected: 'Foo'
page.should have_select "enterprise_fee_set_collection_attributes_0_fee_type", selected: 'Admin'
page.should have_selector "input[value='Greetings!']"
page.should have_select 'enterprise_fee_set_collection_attributes_0_tax_category_id', selected: ''
page.should have_select 'enterprise_fee_set_collection_attributes_0_tax_category_id', selected: 'Inherit From Product'
page.should have_selector "option[selected]", text: 'Flat Percent'
fee.reload
fee.enterprise.should == enterprise
fee.name.should == 'Greetings!'
fee.fee_type.should == 'admin'
fee.calculator_type.should == "Spree::Calculator::FlatPercentItemTotal"
# Sets tax_category and inherits_tax_category
fee.tax_category.should == nil
fee.inherits_tax_category.should == true
end
scenario "deleting an enterprise fee" do