Sha256: d36672e5209918607fbcade14668a61dff40bacdb50ed4e73e0e9e3b0d4a4016

Contents?: true

Size: 1.2 KB

Versions: 28

Compression:

Stored size: 1.2 KB

Contents

module Workarea
  module Pricing
    class ItemTaxApplier
      include TaxApplication

      def initialize(address, adjustments_to_tax)
        @address = address
        @adjustments_to_tax = adjustments_to_tax
        @adjustments_by_parent = adjustments_to_tax.grouped_by_parent
      end

      def apply
        @adjustments_to_tax.each { |adjustment| assign_item_tax(adjustment) }
      end

      private

      def assign_item_tax(adjustment)
        order_item = adjustment._parent
        taxable_amount = @adjustments_by_parent[order_item].taxable_share_for(adjustment)
        return if taxable_amount.zero?

        rate = Tax.find_rate(adjustment.data['tax_code'], taxable_amount, @address)
        tax_amounts = calculate_tax_amounts(taxable_amount, rate)
        return if tax_amounts.values.sum.zero?

        order_item.adjust_pricing(
          price: 'tax',
          calculator: self.class.name,
          description: 'Item Tax',
          amount: tax_amounts.values.sum,
          data: tax_amounts.merge(
            'adjustment' => adjustment.id,
            'order_item_id' => adjustment._parent.id,
            'tax_code' => adjustment.data['tax_code']
          )
        )
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.26 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.25 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.23 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.22 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.21 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.20 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.19 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.18 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.17 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.16 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.15 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.14 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.13 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.12 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.11 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.10 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.9 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.8 app/models/workarea/pricing/item_tax_applier.rb
workarea-core-3.5.7 app/models/workarea/pricing/item_tax_applier.rb