mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-11 23:17:48 +00:00
Introduce TaxCategories to ShippingMethods
This commit is contained in:
@@ -79,6 +79,7 @@ module Spree
|
||||
|
||||
def load_data
|
||||
@available_zones = Zone.order(:name)
|
||||
@tax_categories = Spree::TaxCategory.order(:name)
|
||||
@calculators = ShippingMethod.calculators.sort_by(&:name)
|
||||
end
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ module Calculator
|
||||
case computable
|
||||
when Spree::Order
|
||||
compute_order(computable)
|
||||
when Spree::Shipment
|
||||
compute_shipment(computable)
|
||||
when Spree::LineItem
|
||||
compute_line_item(computable)
|
||||
end
|
||||
@@ -70,17 +72,19 @@ module Calculator
|
||||
.sum { |applicator| applicator.enterprise_fee.compute_amount(order) }
|
||||
end
|
||||
|
||||
def compute_line_item(line_item)
|
||||
if line_item.tax_category == rate.tax_category
|
||||
def compute_shipment_or_line_item(item)
|
||||
if item.tax_category == rate.tax_category
|
||||
if rate.included_in_price
|
||||
deduced_total_by_rate(line_item.total, rate)
|
||||
deduced_total_by_rate(item.amount, rate)
|
||||
else
|
||||
round_to_two_places(line_item.total * rate.amount)
|
||||
round_to_two_places(item.amount * rate.amount)
|
||||
end
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
alias_method :compute_shipment, :compute_shipment_or_line_item
|
||||
alias_method :compute_line_item, :compute_shipment_or_line_item
|
||||
|
||||
def round_to_two_places(amount)
|
||||
BigDecimal(amount.to_s).round(2, BigDecimal::ROUND_HALF_UP)
|
||||
|
||||
@@ -104,6 +104,10 @@ module Spree
|
||||
save!
|
||||
end
|
||||
|
||||
def tax_category
|
||||
selected_shipping_rate.try(:shipping_method).try(:tax_category)
|
||||
end
|
||||
|
||||
def refresh_rates
|
||||
return shipping_rates if shipped?
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@ module Spree
|
||||
class_name: 'Spree::Zone',
|
||||
foreign_key: 'shipping_method_id'
|
||||
|
||||
belongs_to :tax_category, class_name: 'Spree::TaxCategory'
|
||||
|
||||
validates :name, presence: true
|
||||
validate :distributor_validation
|
||||
validate :at_least_one_shipping_category
|
||||
|
||||
@@ -43,9 +43,14 @@
|
||||
= f.hidden_field :tag_list, "ng-value" => "shippingMethod.tag_list"
|
||||
%tags-with-translation#something{ object: "shippingMethod", 'find-tags' => 'findTags(query)' }
|
||||
|
||||
|
||||
|
||||
= render partial: 'spree/admin/shared/calculator_fields', locals: { f: f }
|
||||
|
||||
%fieldset.tax_categories.no-border-bottom
|
||||
= f.field_container :tax_categories do
|
||||
= f.select :tax_category_id, @tax_categories.map { |tc| [tc.name, tc.id] }, {}, :class => "select2 fullwidth"
|
||||
= error_message_on :shipping_method, :tax_category_id
|
||||
|
||||
%fieldset.categories.no-border-bottom
|
||||
%legend{align: "center"}= t('.categories')
|
||||
= f.field_container :categories do
|
||||
@@ -56,7 +61,7 @@
|
||||
%br/
|
||||
= error_message_on :shipping_method, :shipping_category_id
|
||||
|
||||
|
||||
|
||||
%fieldset.no-border-bottom
|
||||
%legend{align: "center"}= t('.zones')
|
||||
= f.field_container :zones do
|
||||
|
||||
Reference in New Issue
Block a user