Class: Order
- Defined in:
- app/models/order.rb
Instance Method Summary collapse
- #add_variant(variant, quantity = 1) ⇒ Object
- #allow_cancel? ⇒ Boolean
- #allow_pay? ⇒ Boolean
- #allow_resume? ⇒ Boolean
- #contains?(variant) ⇒ Boolean
- #generate_order_number ⇒ Object
- #grant_access?(token = nil) ⇒ Boolean
-
#item_total ⇒ Object
total of line items (no tax or shipping inc.).
- #mark_shipped ⇒ Object
- #payment_total ⇒ Object
- #restore_state ⇒ Object
-
#shipment ⇒ Object
convenience method since many stores will not allow user to create multiple shipments.
-
#shipping_countries ⇒ Object
collection of available shipping countries.
- #shipping_methods ⇒ Object
- #to_param ⇒ Object
- #total ⇒ Object
- #update_totals ⇒ Object
Instance Method Details
#add_variant(variant, quantity = 1) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/order.rb', line 90 def add_variant(variant, quantity=1) current_item = contains?(variant) if current_item current_item.increment_quantity unless quantity > 1 current_item.quantity = (current_item.quantity + quantity) if quantity > 1 current_item.save else current_item = LineItem.new(:quantity => quantity, :variant => variant, :price => variant.price) self.line_items << current_item end # populate line_items attributes for additional_fields entries # that have populate => [:line_item] Variant.additional_fields.select{|f| !f[:populate].nil? && f[:populate].include?(:line_item) }.each do |field| value = "" if field[:only].nil? || field[:only].include?(:variant) value = variant.send(field[:name].gsub(" ", "_").downcase) elsif field[:only].include?(:product) value = variant.product.send(field[:name].gsub(" ", "_").downcase) end current_item.update_attribute(field[:name].gsub(" ", "_").downcase, value) end end |
#allow_cancel? ⇒ Boolean
76 77 78 |
# File 'app/models/order.rb', line 76 def allow_cancel? self.state != 'canceled' end |
#allow_pay? ⇒ Boolean
86 87 88 |
# File 'app/models/order.rb', line 86 def allow_pay? checkout_complete end |
#allow_resume? ⇒ Boolean
80 81 82 83 84 |
# File 'app/models/order.rb', line 80 def allow_resume? # we shouldn't allow resume for legacy orders b/c we lack the information necessary to restore to a previous state return false if state_events.empty? || state_events.last.previous_state.nil? true end |
#contains?(variant) ⇒ Boolean
146 147 148 |
# File 'app/models/order.rb', line 146 def contains?(variant) line_items.select { |line_item| line_item.variant == variant }.first end |
#generate_order_number ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'app/models/order.rb', line 115 def generate_order_number record = true while record random = "R#{Array.new(9){rand(9)}.join}" record = Order.find(:first, :conditions => ["number = ?", random]) end self.number = random end |
#grant_access?(token = nil) ⇒ Boolean
150 151 152 153 154 |
# File 'app/models/order.rb', line 150 def grant_access?(token=nil) return true if token && token == self.token return false unless current_user_session = UserSession.find return current_user_session.user == self.user end |
#item_total ⇒ Object
total of line items (no tax or shipping inc.)
129 130 131 132 133 134 135 |
# File 'app/models/order.rb', line 129 def item_total tot = 0 self.line_items.each do |li| tot += li.total end self.item_total = tot end |
#mark_shipped ⇒ Object
155 156 157 158 159 |
# File 'app/models/order.rb', line 155 def mark_shipped inventory_units.each do |inventory_unit| inventory_unit.ship! end end |
#payment_total ⇒ Object
124 125 126 |
# File 'app/models/order.rb', line 124 def payment_total payments.inject(0) {|sum, payment| sum + payment.amount} end |
#restore_state ⇒ Object
70 71 72 73 74 |
# File 'app/models/order.rb', line 70 def restore_state # pop the resume event so we can see what the event before that was state_events.pop if state_events.last.name == "resume" update_attribute("state", state_events.last.previous_state) end |
#shipment ⇒ Object
convenience method since many stores will not allow user to create multiple shipments
142 143 144 |
# File 'app/models/order.rb', line 142 def shipment shipments.last end |
#shipping_countries ⇒ Object
collection of available shipping countries
162 163 164 |
# File 'app/models/order.rb', line 162 def shipping_countries ShippingMethod.all.collect { |method| method.zone.country_list }.flatten.uniq.sort_by {|item| item.send 'name'} end |
#shipping_methods ⇒ Object
166 167 168 169 |
# File 'app/models/order.rb', line 166 def shipping_methods return [] unless ship_address ShippingMethod.all.select { |method| method.zone.include?(ship_address) && method.available?(self) } end |
#to_param ⇒ Object
35 36 37 38 39 |
# File 'app/models/order.rb', line 35 def to_param self.number if self.number generate_order_number unless self.number self.number.parameterize.to_s.upcase end |
#total ⇒ Object
137 138 139 |
# File 'app/models/order.rb', line 137 def total self.total = self.item_total + self.ship_amount + self.tax_amount end |
#update_totals ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'app/models/order.rb', line 171 def update_totals # finalize order totals unless shipment.nil? calculator = shipment.shipping_method.shipping_calculator.constantize.new self.ship_amount = calculator.calculate_shipping(shipment) else self.ship_amount = 0 end self.tax_amount = calculate_tax end |