diff --git a/app/services/cart/add_item_service.rb b/app/services/cart/add_item_service.rb index 393359f67..f4e1c16d2 100644 --- a/app/services/cart/add_item_service.rb +++ b/app/services/cart/add_item_service.rb @@ -15,13 +15,13 @@ class Cart::AddItemService if item.nil? item = order.order_items.new(quantity: quantity, orderable: orderable, amount: orderable.amount || 0) else - item.quantity += quantity.to_i + item.quantity += quantity end raise Cart::OutStockError if item.quantity > orderable.stock['external'] - order.total += (item.amount * item.quantity.to_i) unless item.is_offered ActiveRecord::Base.transaction do item.save + Cart::UpdateTotalService.new.call(order) order.save end order.reload diff --git a/app/services/cart/find_or_create_service.rb b/app/services/cart/find_or_create_service.rb index 1e3a8d5ab..e882ca725 100644 --- a/app/services/cart/find_or_create_service.rb +++ b/app/services/cart/find_or_create_service.rb @@ -18,6 +18,7 @@ class Cart::FindOrCreateService clean_old_cart if @user @order.update(statistic_profile_id: @user.statistic_profile.id) if @order.statistic_profile_id.nil? && @user&.member? @order.update(operator_profile_id: @user.invoicing_profile.id) if @order.operator_profile_id.nil? && @user&.privileged? + Cart::UpdateTotalService.new.call(@order) return @order end diff --git a/app/services/cart/refresh_item_service.rb b/app/services/cart/refresh_item_service.rb index 7828f0be7..143df6e12 100644 --- a/app/services/cart/refresh_item_service.rb +++ b/app/services/cart/refresh_item_service.rb @@ -9,11 +9,10 @@ class Cart::RefreshItemService raise ActiveRecord::RecordNotFound if item.nil? - order.total -= (item.amount * item.quantity.to_i) unless item.is_offered item.amount = orderable.amount || 0 - order.total += (item.amount * item.quantity.to_i) unless item.is_offered ActiveRecord::Base.transaction do item.save + Cart::UpdateTotalService.new.call(order) order.save end order.reload diff --git a/app/services/cart/remove_item_service.rb b/app/services/cart/remove_item_service.rb index 09890a973..01d8d01fe 100644 --- a/app/services/cart/remove_item_service.rb +++ b/app/services/cart/remove_item_service.rb @@ -7,9 +7,9 @@ class Cart::RemoveItemService raise ActiveRecord::RecordNotFound if item.nil? - order.total -= (item.amount * item.quantity.to_i) unless item.is_offered ActiveRecord::Base.transaction do item.destroy! + Cart::UpdateTotalService.new.call(order) order.save end order.reload diff --git a/app/services/cart/set_offer_service.rb b/app/services/cart/set_offer_service.rb index a5d2ac38f..d9bd11b1a 100644 --- a/app/services/cart/set_offer_service.rb +++ b/app/services/cart/set_offer_service.rb @@ -7,14 +7,10 @@ class Cart::SetOfferService raise ActiveRecord::RecordNotFound if item.nil? - if !item.is_offered && is_offered - order.total -= (item.amount * item.quantity) - elsif item.is_offered && !is_offered - order.total += (item.amount * item.quantity) - end item.is_offered = is_offered ActiveRecord::Base.transaction do item.save + Cart::UpdateTotalService.new.call(order) order.save end order.reload diff --git a/app/services/cart/set_quantity_service.rb b/app/services/cart/set_quantity_service.rb index 45051b7a1..545ff99b3 100644 --- a/app/services/cart/set_quantity_service.rb +++ b/app/services/cart/set_quantity_service.rb @@ -13,10 +13,9 @@ class Cart::SetQuantityService raise ActiveRecord::RecordNotFound if item.nil? - different_quantity = quantity.to_i - item.quantity - order.total += (item.amount * different_quantity) unless item.is_offered ActiveRecord::Base.transaction do item.update(quantity: quantity.to_i) + Cart::UpdateTotalService.new.call(order) order.save end order.reload diff --git a/app/services/cart/update_total_service.rb b/app/services/cart/update_total_service.rb new file mode 100644 index 000000000..24dec77e2 --- /dev/null +++ b/app/services/cart/update_total_service.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Provides methods for update total of cart +class Cart::UpdateTotalService + def call(order) + total = 0 + order.order_items.each do |item| + total += (item.amount * item.quantity) unless item.is_offered + end + order.total = total + order.save + order + end +end