diff --git a/app/models/spree/item_adjustments.rb b/app/models/spree/item_adjustments.rb new file mode 100644 index 0000000000..c6cb1238c4 --- /dev/null +++ b/app/models/spree/item_adjustments.rb @@ -0,0 +1,28 @@ +# 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 item.persisted? + item + end + + def update_adjustments + adjustment_total = adjustments.map(&:update!).compact.sum + + item.update_columns( + adjustment_total: adjustment_total, + updated_at: Time.zone.now + ) + end + end +end