Class: Cornerstore::Order

Inherits:
Model::Base show all
Defined in:
lib/cornerstore/api/order.rb

Instance Attribute Summary collapse

Attributes inherited from Model::Base

#_id, #_slugs, #created_at, #parent, #updated_at

Instance Method Summary collapse

Methods inherited from Model::Base

#==, #attributes, #attributes=, #inspect, #method_missing, method_missing, #to_param, #url

Constructor Details

#initialize(attributes, parent = nil) ⇒ Order

Returns a new instance of Order.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cornerstore/api/order.rb', line 27

def initialize(attributes, parent=nil)
  self.payment_costs      = Cornerstore::Price.new(attributes.delete('payment_costs')) if attributes['payment_costs']
  self.shipping_costs     = Cornerstore::Price.new(attributes.delete('shipping_costs')) if attributes['shipping_costs']
  self.subtotal           = Cornerstore::Price.new(attributes.delete('subtotal'))
  self.sales_tax          = Cornerstore::Price.new(attributes.delete('sales_tax')) if attributes['sales_tax']
  self.total              = Cornerstore::Price.new(attributes.delete('total'))

  self.customer           = Cornerstore::Customer.new(attributes.delete('customer'))
  self.shipping_address   = Cornerstore::Address.new(attributes.delete('shipping_address'))
  self.billing_address    = Cornerstore::Address.new(attributes.delete('billing_address'))
  self.payment_means      = Cornerstore::PaymentMeans.new(attributes.delete('payment_means'))
  self.requested_carrier  = Cornerstore::Carrier.new(attributes.delete('requested_carrier'))

  self.line_items         = Cornerstore::LineItem::Resource.new(self, attributes.delete('line_items') || [], 'line_items')
  self.shipments          = Cornerstore::Shipment::Resource.new(self, attributes.delete('shipments') || [], 'shipments')
  self.cancellations      = Cornerstore::Cancellation::Resource.new(self, attributes.delete('cancellations') || [], 'cancellations')

  self.placed_at          = DateTime.parse(attributes.delete('placed_at')) unless attributes['placed_at'].blank?
  self.invoiced_at        = DateTime.parse(attributes.delete('invoiced_at')) unless attributes['invoiced_at'].blank?

  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cornerstore::Model::Base

Instance Attribute Details

#billing_addressObject

Returns the value of attribute billing_address.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def billing_address
  @billing_address
end

#cancellationsObject

Returns the value of attribute cancellations.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def cancellations
  @cancellations
end

#checkout_idObject

Returns the value of attribute checkout_id.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def checkout_id
  @checkout_id
end

#checkout_started_atObject

Returns the value of attribute checkout_started_at.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def checkout_started_at
  @checkout_started_at
end

#customerObject

Returns the value of attribute customer.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def customer
  @customer
end

#customer_commentObject

Returns the value of attribute customer_comment.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def customer_comment
  @customer_comment
end

#invoice_numberObject

Returns the value of attribute invoice_number.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def invoice_number
  @invoice_number
end

#invoiced_atObject

Returns the value of attribute invoiced_at.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def invoiced_at
  @invoiced_at
end

#line_itemsObject

Returns the value of attribute line_items.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def line_items
  @line_items
end

#numberObject

Returns the value of attribute number.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def number
  @number
end

#payment_costsObject

Returns the value of attribute payment_costs.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def payment_costs
  @payment_costs
end

#payment_meansObject

Returns the value of attribute payment_means.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def payment_means
  @payment_means
end

#placed_atObject

Returns the value of attribute placed_at.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def placed_at
  @placed_at
end

#referenceObject

Returns the value of attribute reference.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def reference
  @reference
end

#requested_carrierObject

Returns the value of attribute requested_carrier.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def requested_carrier
  @requested_carrier
end

#sales_taxObject

Returns the value of attribute sales_tax.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def sales_tax
  @sales_tax
end

#shipmentsObject

Returns the value of attribute shipments.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def shipments
  @shipments
end

#shipping_addressObject

Returns the value of attribute shipping_address.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def shipping_address
  @shipping_address
end

#shipping_costsObject

Returns the value of attribute shipping_costs.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def shipping_costs
  @shipping_costs
end

#subtotalObject

Returns the value of attribute subtotal.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def subtotal
  @subtotal
end

#totalObject

Returns the value of attribute total.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def total
  @total
end

#weightObject

Returns the value of attribute weight.



2
3
4
# File 'lib/cornerstore/api/order.rb', line 2

def weight
  @weight
end

Instance Method Details

#canceled?Boolean

Returns true if at least one line item of this order is canceled

Returns:

  • (Boolean)


60
61
62
# File 'lib/cornerstore/api/order.rb', line 60

def canceled?
  !self.cancellations.empty?
end

#check_signatureObject



90
91
92
93
# File 'lib/cornerstore/api/order.rb', line 90

def check_signature
  # TODO: Implement signature check
  return true
end

#completely_canceled?Boolean

Returns true if all line items are canceled

Returns:

  • (Boolean)


74
75
76
# File 'lib/cornerstore/api/order.rb', line 74

def completely_canceled?
  self.cancellations.collect(&:canceled_items).flatten.size == self.line_items.size
end

#completely_shipped?Boolean

Returns true if all line items are shipped

Returns:

  • (Boolean)


69
70
71
# File 'lib/cornerstore/api/order.rb', line 69

def completely_shipped?
  self.shipments.collect(&:shipped_items).flatten.size == self.line_items.size
end

#handling_costsObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cornerstore/api/order.rb', line 78

def handling_costs
  if self.shipping_costs and self.payment_costs
    self.shipping_costs + self.payment_costs
  elsif self.shipping_costs
    self.shipping_costs
  elsif self.payment_costs
    self.payment_costs
  else
    nil      
  end
end

#invoiced?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/cornerstore/api/order.rb', line 64

def invoiced?
  !self.invoice_number.blank?
end

#requires_shipment?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cornerstore/api/order.rb', line 50

def requires_shipment?
  self.line_items.any? { |li| li.requires_shipment? }
end

#shipped?Boolean

Returns true if at least one line item of this order is shipped

Returns:

  • (Boolean)


55
56
57
# File 'lib/cornerstore/api/order.rb', line 55

def shipped?
  !self.shipments.empty?
end