Class: MerchantSidekick::SalesOrder

Inherits:
Order
  • Object
show all
Defined in:
lib/merchant_sidekick/sales_order.rb

Instance Method Summary collapse

Methods inherited from Order

#enter_approved, #enter_canceled, #enter_created, #enter_pending, #enter_received, #enter_refunded, #enter_returned, #enter_returning, #enter_shipped, #enter_shipping, #evaluate, #exit_approved, #exit_canceled, #exit_created, #exit_pending, #exit_received, #exit_refunded, #exit_returned, #exit_returning, #exit_shipped, #exit_shipping, generate_unique_id, #gross_total, #guard_approve_payment_from_pending, #guard_cancel_from_created, #guard_cancel_from_pending, #guard_confirm_reception_from_shipped, #guard_confirm_return_from_returning, #guard_confirm_return_from_shipped, #guard_process_payment_from_created, #guard_process_shipping_from_approved, #guard_refund_from_returned, #guard_reject_from_received, #guard_ship_from_shipping, #items_count, #line_items_count, #net_total, #number, #push, #tax_total, #total

Instance Method Details

#build_addresses(options = {}) ⇒ Object

Builds billing, shipping and origin addresses

Raises:

  • (ArgumentError)


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
# File 'lib/merchant_sidekick/sales_order.rb', line 78

def build_addresses(options={})
  raise ArgumentError.new("No address declared for buyer (#{buyer.class.name} ##{buyer.id}), use acts_as_addressable") \
    unless buyer.respond_to?(:find_default_address)

  # buyer's billing address
  unless self.default_billing_address
    if buyer.respond_to?(:billing_address) && buyer.default_billing_address
      self.build_billing_address(buyer.default_billing_address.content_attributes)
    else
      if buyer_default_address = buyer.find_default_address
        self.build_billing_address(buyer_default_address.content_attributes)
      else
        raise ArgumentError.new(
          "No billing or default address found for buyer (#{buyer.class.name} ##{buyer.id}), use acts_as_addressable")
      end
    end
  end

  # buyer's shipping address is optional
  if buyer.respond_to?(:shipping_address)
    self.build_shipping_address(buyer.find_shipping_address_or_clone_from(
      self.billing_address
    ).content_attributes) unless self.default_shipping_address
  end

  # seller's address for origin address
  raise ArgumentError.new("No address declared for seller (#{seller.class.name} ##{seller.id}), use acts_as_addressable") \
    unless seller.respond_to?(:find_default_address)

  unless default_origin_address
    if seller.respond_to?(:billing_address) && seller.default_billing_address
      self.build_origin_address(seller.default_billing_address.content_attributes)
    else
      if seller_default_address = seller.find_default_address
        self.build_origin_address(seller_default_address.content_attributes)
      else
        raise ArgumentError.new(
          "No billing or default address found for seller (#{seller.class.name} ##{seller.id}), use acts_as_addressable")
      end
    end
  end
end

#build_invoiceObject

:nodoc:



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
# File 'lib/merchant_sidekick/sales_order.rb', line 45

def build_invoice #:nodoc:
  new_invoice = self.build_sales_invoice(
    :line_items => self.line_items,
    :net_amount => self.net_total,
    :tax_amount => self.tax_total,
    :gross_amount => self.gross_total,
    :buyer => self.buyer,
    :seller => self.seller,
    :origin_address => self.origin_address ? self.origin_address.dup : nil,
    :billing_address => self.billing_address ? self.billing_address.dup : nil,
    :shipping_address => self.shipping_address ? self.shipping_address.dup : nil
  )

  # set new invoice's line items to invoice we just created
  new_invoice.line_items.each do |li|
    if li.new_record?
      li.invoice = new_invoice
    else
      li.update_attribute(:invoice, new_invoice)
    end
  end

  # copy addresses
  new_invoice.build_origin_address(self.origin_address.content_attributes) if self.origin_address
  new_invoice.build_billing_address(self.billing_address.content_attributes) if self.billing_address
  new_invoice.build_shipping_address(self.shipping_address.content_attributes) if self.shipping_address

  self.invoice = new_invoice

  new_invoice
end

#cash(payment_object, options = {}) ⇒ Object

Cash the order and generate invoice



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/merchant_sidekick/sales_order.rb', line 7

def cash(payment_object, options={})
  defaults = { :order_id => number }
  options = defaults.merge(options).symbolize_keys

  # before_payment
  seller.send( :before_payment, self ) if seller && seller.respond_to?( :before_payment )

  self.build_addresses
  self.build_invoice unless self.invoice

  payment = self.invoice.cash(payment_object, options)
  if payment.success?
    process_payment!
    approve_payment!
  end

  # after_payment
  buyer.send( :after_payment, self ) if buyer && buyer.respond_to?( :after_payment )
  payment
end

#invoiceObject



37
38
39
# File 'lib/merchant_sidekick/sales_order.rb', line 37

def invoice
  self.sales_invoice
end

#invoice=(an_invoice) ⇒ Object



41
42
43
# File 'lib/merchant_sidekick/sales_order.rb', line 41

def invoice=(an_invoice)
  self.sales_invoice = an_invoice
end

#sales_order?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/merchant_sidekick/sales_order.rb', line 28

def sales_order?
  true
end

#to_invoice_class_nameObject

used in build_invoice to determine which type of invoice



33
34
35
# File 'lib/merchant_sidekick/sales_order.rb', line 33

def to_invoice_class_name
  "MerchantSidekick::SalesInvoice"
end