Class: Piggybak::Order

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/piggybak/order.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#disable_order_notesObject

Returns the value of attribute disable_order_notes.



15
16
17
# File 'app/models/piggybak/order.rb', line 15

def disable_order_notes
  @disable_order_notes
end

#recorded_changerObject

Returns the value of attribute recorded_changer.



15
16
17
# File 'app/models/piggybak/order.rb', line 15

def recorded_changer
  @recorded_changer
end

#recorded_changesObject

Returns the value of attribute recorded_changes.



15
16
17
# File 'app/models/piggybak/order.rb', line 15

def recorded_changes
  @recorded_changes
end

#was_new_recordObject

Returns the value of attribute was_new_record.



15
16
17
# File 'app/models/piggybak/order.rb', line 15

def was_new_record
  @was_new_record
end

Instance Method Details

#add_line_items(cart) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'app/models/piggybak/order.rb', line 168

def add_line_items(cart)
  cart.update_quantities

  cart.sellables.each do |item|
    self.line_items << Piggybak::LineItem.new({ :sellable_id => item[:sellable].id,
      :unit_price => item[:sellable].price,
      :price => item[:sellable].price*item[:quantity],
      :description => item[:sellable].description,
      :quantity => item[:quantity] })
  end
end

#admin_labelObject



217
218
219
# File 'app/models/piggybak/order.rb', line 217

def admin_label
  "Order ##{self.id}"    
end

#avs_addressObject



207
208
209
210
211
212
213
214
215
# File 'app/models/piggybak/order.rb', line 207

def avs_address
  {
  :address1 => self.billing_address.address1,
  :city     => self.billing_address.city,
  :state    => self.billing_address.state_display,
  :zip      => self.billing_address.zip,
  :country  => "US" 
  }
end

#create_payment_shipmentObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/piggybak/order.rb', line 146

def create_payment_shipment
  shipment_line_item = self.line_items.detect { |li| li.line_item_type == "shipment" }

  if shipment_line_item.nil?
    new_shipment_line_item = Piggybak::LineItem.new({ :line_item_type => "shipment" })
    new_shipment_line_item.build_shipment
    self.line_items << new_shipment_line_item
  elsif shipment_line_item.shipment.nil?
    shipment_line_item.build_shipment
  else
    previous_method = shipment_line_item.shipment.shipping_method_id
    shipment_line_item.build_shipment
    shipment_line_item.shipment.shipping_method_id = previous_method
  end

  if !self.line_items.detect { |li| li.line_item_type == "payment" }
    payment_line_item = Piggybak::LineItem.new({ :line_item_type => "payment" })
    payment_line_item.build_payment 
    self.line_items << payment_line_item
  end
end

#deliver_order_confirmationObject



35
36
37
38
# File 'app/models/piggybak/order.rb', line 35

def deliver_order_confirmation
  Piggybak::Notifier.order_notification(self).deliver
  self.update_column(:confirmation_sent,true)
end

#initialize_defaultsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/piggybak/order.rb', line 40

def initialize_defaults
  self.recorded_changes ||= []

  self.billing_address ||= Piggybak::Address.new
  self.shipping_address ||= Piggybak::Address.new
  self.shipping_address.is_shipping = true

  self.ip_address ||= 'admin'
  self.user_agent ||= 'admin'

  self.created_at ||= Time.now
  self.status ||= "new"
  self.total ||= 0
  self.total_due ||= 0
  self.disable_order_notes = false
end

#initialize_user(user) ⇒ Object



68
69
70
71
72
73
# File 'app/models/piggybak/order.rb', line 68

def initialize_user(user)
  if user
    self.user = user
    self.email = user.email 
  end
end

#number_paymentsObject



57
58
59
60
61
62
63
64
65
66
# File 'app/models/piggybak/order.rb', line 57

def number_payments

  new_payments = self.line_items.payments.select { |li| li.new_record? }
  if new_payments.size > 1
    self.errors.add(:base, "Only one payment may be created at a time.")
    new_payments.each do |li|
      li.errors.add(:line_item_type, "Only one payment may be created at a time.")
    end
  end
end

#postprocess_orderObject



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
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/piggybak/order.rb', line 75

def postprocess_order

  # Mark line items for destruction if quantity == 0
  self.line_items.each do |line_item|
    if line_item.quantity == 0
      line_item.mark_for_destruction
    end
  end

  # Postprocess everything but payments first
  self.line_items.each do |line_item|
    next if line_item.line_item_type == "payment"
    method = "postprocess_#{line_item.line_item_type}"
    if line_item.respond_to?(method)
      if !line_item.send(method)
        return false
      end
    end
  end
  
  # Recalculate and create line item for tax
  # If a tax line item already exists, reset price
  # If a tax line item doesn't, create
  # If tax is 0, destroy tax line item
  tax = TaxMethod.calculate_tax(self)
  tax_line_item = self.line_items.taxes
  if tax > 0
    if tax_line_item.any?
      tax_line_item.first.price = tax
    else
      self.line_items << LineItem.new({ :line_item_type => "tax", :description => "Tax Charge", :price => tax })
    end
  elsif tax_line_item.any?
    tax_line_item.first.mark_for_destruction
  end
 
  # Recalculating total and total due, in case post process changed totals
  self.total_due = 0
  self.total = 0
  self.line_items.each do |line_item|
    if !line_item._destroy
      self.total_due += line_item.price
      if line_item.line_item_type != "payment" 
        self.total += line_item.price
      end 
    end
  end

  # Postprocess payment last
  self.line_items.payments.each do |line_item|
    method = "postprocess_payment"
    if line_item.respond_to?("postprocess_payment")
      if !line_item.postprocess_payment
        return false
      end
    end
  end

  true
end

#record_order_noteObject



136
137
138
139
140
141
142
143
144
# File 'app/models/piggybak/order.rb', line 136

def record_order_note
  if self.changed? && !self.was_new_record
    self.recorded_changes << self.formatted_changes
  end

  if self.recorded_changes.any? && !self.disable_order_notes
    OrderNote.create(:order_id => self.id, :note => self.recorded_changes.join("<br />"), :user_id => self.recorded_changer.to_i)
  end
end

#set_new_recordObject



198
199
200
201
# File 'app/models/piggybak/order.rb', line 198

def set_new_record
  self.was_new_record = self.new_record?
  true
end

#status_enumObject



203
204
205
# File 'app/models/piggybak/order.rb', line 203

def status_enum
  ["new", "processing", "shipped"]
end

#update_statusObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/piggybak/order.rb', line 180

def update_status
  return if self.status == "cancelled"  # do nothing

  if self.total_due != 0.00
    self.status = "unbalanced" 
  else
    if self.to_be_cancelled
      self.status = "cancelled"
    elsif line_items.shipments.any? && line_items.shipments.all? { |li| li.shipment.status == "shipped" }
      self.status = "shipped"
    elsif line_items.shipments.any? && line_items.shipments.all? { |li| li.shipment.status == "processing" }
      self.status = "processing"
    else
      self.status = "new"
    end
  end
end