Files
openfoodnetwork/app/models/spree/item_adjustments.rb
Matt-Yorkley 7425ad5c4a Include ItemAdjustments handling in Adjustment class
Currently only applies to shipments, but will later include line items, etc
2021-04-04 19:19:06 +01:00

43 lines
1.0 KiB
Ruby

# frozen_string_literal: true
module Spree
# Manage (recalculate) item (LineItem or Shipment) adjustments
class ItemAdjustments
attr_reader :item
delegate :adjustments, :order, to: :item
def initialize(item)
@item = item
end
def update
update_adjustments if updatable_totals?
item
end
def update_adjustments
adjustment_total = adjustments.additional.map(&:update!).compact.sum
included_tax_total = tax_adjustments.inclusive.reload.map(&:update!).compact.sum
additional_tax_total = tax_adjustments.additional.reload.map(&:update!).compact.sum
item.update_columns(
included_tax_total: included_tax_total,
additional_tax_total: additional_tax_total,
adjustment_total: adjustment_total,
updated_at: Time.zone.now
)
end
private
def updatable_totals?
item.persisted? && item.is_a?(Spree::Shipment)
end
def tax_adjustments
(item.respond_to?(:all_adjustments) ? item.all_adjustments : item.adjustments).tax
end
end
end