Class: MultiRoomModel::RoomsLineItemChanger
- Inherits:
-
BaseService
- Object
- BaseService
- MultiRoomModel::RoomsLineItemChanger
- Defined in:
- app/services/multi_room_model/rooms_line_item_changer.rb
Overview
Service object: rooms line item changer.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #add_line_items_to_all_rooms(sku, qty) ⇒ Object
-
#initialize(quote_or_order, options = {}) ⇒ RoomsLineItemChanger
constructor
A new instance of RoomsLineItemChanger.
- #remove_line_items_from_all_rooms(sku) ⇒ Object
- #replace_line_items_in_all_rooms(orig_sku, new_sku) ⇒ Object
Constructor Details
#initialize(quote_or_order, options = {}) ⇒ RoomsLineItemChanger
Returns a new instance of RoomsLineItemChanger.
6 7 8 9 10 11 12 |
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 6 def initialize(quote_or_order, = {}) @quote_or_order = quote_or_order @customer = @quote_or_order.customer @options = @logger = [:logger] || Rails.logger @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
4 5 6 |
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 4 def errors @errors end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
4 5 6 |
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 4 def logger @logger end |
Instance Method Details
#add_line_items_to_all_rooms(sku, qty) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 14 def add_line_items_to_all_rooms(sku, qty) @errors = [] cat_item = @quote_or_order.customer.catalog.catalog_items.active.by_skus(sku).first @quote_or_order.do_not_detect_shipping = true @quote_or_order.room_configurations.each do |rc| RoomConfiguration.transaction do rc.add_line_item({ quantity: qty, price: cat_item.amount, discounted_price: cat_item.discounted_price(@quote_or_order.customer, rc), catalog_item_id: cat_item.id }) @errors += rc.errors. unless rc.save rc.synchronize_lines(nil, true, true) rescue StandardError => e @errors << "an unexpected error occured while replacing line items of SKU #{orig_sku} with SKU #{new_sku} in all rooms: #{e}, changes have been rolled back." raise ActiveRecord::Rollback end end @quote_or_order.recalculate_shipping = true @quote_or_order.do_not_detect_shipping = false @errors += rc.errors. unless @quote_or_order.save @errors.empty? end |
#remove_line_items_from_all_rooms(sku) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 37 def remove_line_items_from_all_rooms(sku) @errors = [] @quote_or_order.do_not_detect_shipping = true @quote_or_order.room_configurations.each do |rc| RoomConfiguration.transaction do old_li = rc.line_items.find { |li| li.sku == sku } rc.remove_line_item(old_li, old_li.quantity) @errors += rc.errors. unless rc.save rc.synchronize_lines(nil, true, true) rescue StandardError => e @errors << "an unexpected error occured while replacing line items of SKU #{orig_sku} with SKU #{new_sku} in all rooms: #{e}, changes have been rolled back." raise ActiveRecord::Rollback end end @quote_or_order.recalculate_shipping = true @quote_or_order.do_not_detect_shipping = false @errors += rc.errors. unless @quote_or_order.save @errors.empty? end |
#replace_line_items_in_all_rooms(orig_sku, new_sku) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 57 def replace_line_items_in_all_rooms(orig_sku, new_sku) @errors = [] cat_item = @quote_or_order.customer.catalog.catalog_items.active.by_skus(new_sku).first @quote_or_order.do_not_detect_shipping = true @quote_or_order.room_configurations.each do |rc| RoomConfiguration.transaction do old_li = rc.line_items.find { |li| li.sku == orig_sku } rc.add_line_item({ quantity: old_li.quantity, price: cat_item.amount, discounted_price: cat_item.discounted_price(@quote_or_order.customer, rc), catalog_item_id: cat_item.id }) rc.remove_line_item(old_li, old_li.quantity) @errors += rc.errors. unless rc.save rc.synchronize_lines(nil, true, true) rescue StandardError => e @errors << "an unexpected error occured while replacing line items of SKU #{orig_sku} with SKU #{new_sku} in all rooms: #{e}, changes have been rolled back." raise ActiveRecord::Rollback end end @quote_or_order.recalculate_shipping = true @quote_or_order.do_not_detect_shipping = false @errors += rc.errors. unless @quote_or_order.save @errors.empty? end |