1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/services/cart/set_quantity_service.rb

23 lines
657 B
Ruby
Raw Normal View History

2022-08-19 19:59:13 +02:00
# frozen_string_literal: true
# Provides methods for update quantity of order item
class Cart::SetQuantityService
def call(order, orderable, quantity = nil)
return order if quantity.to_i.zero?
2022-08-20 18:47:15 +02:00
raise Cart::OutStockError if quantity.to_i > orderable.stock['external']
2022-08-19 19:59:13 +02:00
item = order.order_items.find_by(orderable: orderable)
raise ActiveRecord::RecordNotFound if item.nil?
2022-08-20 18:47:15 +02:00
different_quantity = quantity.to_i - item.quantity
order.total += (item.amount * different_quantity) unless item.is_offered
2022-08-19 19:59:13 +02:00
ActiveRecord::Base.transaction do
2022-08-20 18:47:15 +02:00
item.update(quantity: quantity.to_i)
2022-08-19 19:59:13 +02:00
order.save
end
order.reload
end
end