Class: MultiRoomModel::RoomsLineItemChanger

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/multi_room_model/rooms_line_item_changer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quote_or_order, options = {}) ⇒ RoomsLineItemChanger

Returns a new instance of RoomsLineItemChanger.



5
6
7
8
9
10
11
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 5

def initialize(quote_or_order, options={})
  @quote_or_order = quote_or_order
  @customer = @quote_or_order.customer
  @options = options
  @logger = options[:logger] || Rails.logger
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 3

def errors
  @errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 3

def logger
  @logger
end

Instance Method Details

#add_line_items_to_all_rooms(sku, qty) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 13

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
      begin
        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})
        if rc.save
        else
          @errors += rc.errors.full_messages
        end
        rc.synchronize_lines(target=nil,save_targets=true,do_not_recalc_shipping=true)
      rescue StandardError => exc
        @errors << "an unexpected error occured while replacing line items of SKU #{orig_sku} with SKU #{new_sku} in all rooms: #{exc.to_s}, changes have been rolled back."
        raise ActiveRecord::Rollback
      end
    end
  end
  @quote_or_order.recalculate_shipping = true
  @quote_or_order.do_not_detect_shipping = false
  if @quote_or_order.save
  else
    @errors += rc.errors.full_messages
  end
  @errors.empty?
end

#remove_line_items_from_all_rooms(sku) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 44

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
      begin
        old_li = rc.line_items.detect{|li| li.sku == sku}
        rc.remove_line_item(old_li, old_li.quantity)
        if rc.save
        else
          @errors += rc.errors.full_messages
        end
        rc.synchronize_lines(target=nil,save_targets=true,do_not_recalc_shipping=true)
      rescue StandardError => exc
        @errors << "an unexpected error occured while replacing line items of SKU #{orig_sku} with SKU #{new_sku} in all rooms: #{exc.to_s}, changes have been rolled back."
        raise ActiveRecord::Rollback
      end
    end
  end
  @quote_or_order.recalculate_shipping = true
  @quote_or_order.do_not_detect_shipping = false
  if @quote_or_order.save
  else
    @errors += rc.errors.full_messages
  end
  @errors.empty?
end

#replace_line_items_in_all_rooms(orig_sku, new_sku) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/multi_room_model/rooms_line_item_changer.rb', line 72

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
      begin
        old_li = rc.line_items.detect{|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)
        if rc.save
        else
          @errors += rc.errors.full_messages
        end
        rc.synchronize_lines(target=nil,save_targets=true,do_not_recalc_shipping=true)
      rescue StandardError => exc
        @errors << "an unexpected error occured while replacing line items of SKU #{orig_sku} with SKU #{new_sku} in all rooms: #{exc.to_s}, changes have been rolled back."
        raise ActiveRecord::Rollback
      end
    end
  end
  @quote_or_order.recalculate_shipping = true
  @quote_or_order.do_not_detect_shipping = false
  if @quote_or_order.save
  else
    @errors += rc.errors.full_messages
  end
  @errors.empty?
end