Class: Workarea::Admin::OrderViewModel

Inherits:
ApplicationViewModel
  • Object
show all
Includes:
CommentableViewModel
Defined in:
app/view_models/workarea/admin/order_view_model.rb

Instance Method Summary collapse

Methods included from CommentableViewModel

#comment_count, #comments, #has_comments?, #new_comments_for?, #subscribed_users

Instance Method Details

#billing_addressObject

Payment



79
80
81
# File 'app/view_models/workarea/admin/order_view_model.rb', line 79

def billing_address
  payment.address
end

#can_cancel?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/view_models/workarea/admin/order_view_model.rb', line 25

def can_cancel?
  model.placed? && !canceled?
end

#checkout_byObject



29
30
31
32
# File 'app/view_models/workarea/admin/order_view_model.rb', line 29

def checkout_by
  return nil unless model.checkout_by_id.present?
  @checkout_by ||= User.where(id: model.checkout_by_id).first
end

#fulfillmentObject

Fulfillment



102
103
104
105
106
107
# File 'app/view_models/workarea/admin/order_view_model.rb', line 102

def fulfillment
  @fulfillment ||= FulfillmentViewModel.wrap(
    Fulfillment.find_or_initialize_by(id: model.id),
    order: self
  )
end

#fulfillment_statusObject



109
110
111
# File 'app/view_models/workarea/admin/order_view_model.rb', line 109

def fulfillment_status
  options['source'].try(:dig, 'facets', 'fulfillment_status').try(:to_sym)
end

#full_nameObject



13
14
15
16
17
18
19
# File 'app/view_models/workarea/admin/order_view_model.rb', line 13

def full_name
  if billing_address.present?
    "#{billing_address.first_name} #{billing_address.last_name}"
  elsif shipping_address.present?
    "#{shipping_address.first_name} #{shipping_address.last_name}"
  end
end

#itemsObject



21
22
23
# File 'app/view_models/workarea/admin/order_view_model.rb', line 21

def items
  @items ||= model.items.by_newest.map { |item| OrderItemViewModel.new(item) }
end

#packagesObject



113
114
115
# File 'app/view_models/workarea/admin/order_view_model.rb', line 113

def packages
  fulfillment.packages
end

#paymentObject



83
84
85
86
87
# File 'app/view_models/workarea/admin/order_view_model.rb', line 83

def payment
  @payment ||= PaymentViewModel.wrap(
    Payment.find_or_initialize_by(id: model.id)
  )
end

#payment_statusObject



93
94
95
# File 'app/view_models/workarea/admin/order_view_model.rb', line 93

def payment_status
  options['source'].try(:dig, 'facets', 'payment_status').try(:to_sym)
end

#segmentsObject



43
44
45
# File 'app/view_models/workarea/admin/order_view_model.rb', line 43

def segments
  @segments ||= Segment.in(id: model.segment_ids).to_a
end

#shippingObject



60
61
62
63
64
# File 'app/view_models/workarea/admin/order_view_model.rb', line 60

def shipping
  @shipping ||=
    shippings.detect { |s| s.id.to_s == options[:shipping_id].to_s } ||
    shippings.first
end

#shipping_addressObject

Shipping



52
53
54
# File 'app/view_models/workarea/admin/order_view_model.rb', line 52

def shipping_address
  shipping.try(:address)
end

#shipping_serviceObject



56
57
58
# File 'app/view_models/workarea/admin/order_view_model.rb', line 56

def shipping_service
  shipping.try(:shipping_service)
end

#shipping_subtotalObject



70
71
72
# File 'app/view_models/workarea/admin/order_view_model.rb', line 70

def shipping_subtotal
  shippings.sum { |s| s.price_adjustments.first.try(:amount) || 0.to_m }
end

#shippingsObject



66
67
68
# File 'app/view_models/workarea/admin/order_view_model.rb', line 66

def shippings
  @shippings ||= Shipping.by_order(model.id).to_a
end

#timelineObject



34
35
36
# File 'app/view_models/workarea/admin/order_view_model.rb', line 34

def timeline
  @timeline ||= OrderTimelineViewModel.wrap(self, options)
end

#total_adjustmentsObject



89
90
91
# File 'app/view_models/workarea/admin/order_view_model.rb', line 89

def total_adjustments
  @total_adjustments ||= price_adjustments.reduce_by_description('order')
end

#updated_atObject



38
39
40
41
# File 'app/view_models/workarea/admin/order_view_model.rb', line 38

def updated_at
  options['source'].try(:[], 'updated_at').try(:to_datetime) ||
    model.updated_at
end

#userObject



8
9
10
11
# File 'app/view_models/workarea/admin/order_view_model.rb', line 8

def user
  return nil unless model.user_id.present?
  @user ||= User.where(id: model.user_id).first
end