Module: Spree::SpreeChannable::OrderDecorator

Defined in:
app/models/spree/order_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
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
71
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/spree/order_decorator.rb', line 31

def self.prepended(base)
  base.state_machine.after_transition to: :cancelled, do: :cancel_channable_order

  class << base

    def channable_to_order_params(channable_order)
        order_params = {}

        order_params[:channable_order_id] = channable_order.id
        order_params[:channable_channel_order_id] = channable_order.channel_id
        order_params[:channable_channel_name] = channable_order.channel_name
        order_params[:completed_at] = DateTime.parse(channable_order.created)
        order_params[:email] = channable_order.data.shipping.email

        order_params[:bill_address_attributes] = build_address(channable_order.data.billing)
        order_params[:ship_address_attributes] = build_address(channable_order.data.shipping)

        order_params[:shipments_attributes] = build_shipments_attributes(channable_order)
        order_params[:payments_attributes] = build_payments_attributes(channable_order)
        order_params[:line_items_attributes] = build_line_items_attributes(channable_order)

        order_params
    end

    def build_address(address_params)
      {
          firstname: address_params.first_name,
          lastname: address_params.last_name,
          address1: address_params.street,
          address2: [address_params.house_number, address_params.house_number_ext].join(' '),
          city: address_params.city,
          zipcode: address_params.zip_code,
          phone: '0612345678',
          country: {
              'iso' => address_params.country_code
          }
      }
    end

    def build_shipments_attributes(channable_order)
      [
          {
              tracking: nil,
              stock_location: ::SpreeChannable.configuration.stock_location,
              shipping_method: channable_order.channel_name,
              inventory_units: channable_order.data.products.flat_map do |item|
                variant = Spree::Variant.active.find_by_sku!(item.ean)
                item.quantity.times.map do
                  {
                      variant_id: variant.id
                  }
                end
              end
          }
      ]
    end

    def build_payments_attributes(channable_order)
      [
          {
              amount: channable_order.data.price.total.to_f,
              state: 'completed',
              payment_method: ::SpreeChannable.configuration.payment_method
          }
      ]
    end

    def build_line_items_attributes(channable_order)
      channable_order.data.products.map do |item|
        variant = Spree::Variant.active.find_by_sku!(item.ean)
        {
            variant_id: variant.id,
            quantity: item.quantity,
            price: item.price.to_f
        }
      end.compact
    end


    def build_adjustments_attributes(channable_order)
      raise 'Not implemented'
      [
          {
              label: 'Channable Discount',
              amount: -(channable_order['discount'].abs.to_f)
          }
      ]
    end

  end
end

Instance Method Details

#cancel_channable_orderObject



25
26
27
28
29
# File 'app/models/spree/order_decorator.rb', line 25

def cancel_channable_order
  return unless is_channable_order?

  channable_client.cancellation_update(channable_order_id)
end

#channable_clientObject



5
6
7
# File 'app/models/spree/order_decorator.rb', line 5

def channable_client
  @channable_client ||= ::Channable::Client.new
end

#deliver_order_confirmation_emailObject



9
10
11
12
13
# File 'app/models/spree/order_decorator.rb', line 9

def deliver_order_confirmation_email
  unless is_channable_order?
    super
  end
end

#is_channable_order?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/models/spree/order_decorator.rb', line 21

def is_channable_order?
  channable_order_id.present?
end

#send_cancel_emailObject



15
16
17
18
19
# File 'app/models/spree/order_decorator.rb', line 15

def send_cancel_email
  unless is_channable_order?
    super
  end
end