2022-10-14 10:52:24 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for update total of cart
|
|
|
|
class Cart::UpdateTotalService
|
2023-01-09 17:36:11 +01:00
|
|
|
# @param order[Order]
|
2022-10-14 10:52:24 +02:00
|
|
|
def call(order)
|
|
|
|
total = 0
|
|
|
|
order.order_items.each do |item|
|
2023-01-09 17:36:11 +01:00
|
|
|
update_item_price(item)
|
2022-10-14 10:52:24 +02:00
|
|
|
total += (item.amount * item.quantity) unless item.is_offered
|
|
|
|
end
|
|
|
|
order.total = total
|
|
|
|
order.save
|
|
|
|
order
|
|
|
|
end
|
2023-01-09 17:36:11 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# @param item[OrderItem]
|
|
|
|
def update_item_price(item)
|
|
|
|
return unless item.orderable_type.match(/^CartItem::/)
|
|
|
|
|
|
|
|
item.update(amount: item.orderable.price[:amount] || 0)
|
|
|
|
end
|
2022-10-14 10:52:24 +02:00
|
|
|
end
|