Class: CabooseStore::Order
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CabooseStore::Order
- Defined in:
- app/models/caboose_store/order.rb
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Methods.
- #authorized? ⇒ Boolean
- #calculate ⇒ Object
- #calculate_handling ⇒ Object
- #calculate_shipping ⇒ Object
- #calculate_subtotal ⇒ Object
- #calculate_tax ⇒ Object
- #calculate_total ⇒ Object
- #capture ⇒ Object
- #decrement_quantities ⇒ Object
- #increment_quantities ⇒ Object
- #line_item_added(line_item) ⇒ Object
- #line_item_removed(line_item) ⇒ Object
- #refuned ⇒ Object
- #resend_confirmation ⇒ Object
- #shipping_and_handling ⇒ Object
- #test? ⇒ Boolean
- #void ⇒ Object
Instance Method Details
#as_json(options = {}) ⇒ Object
Methods
94 95 96 97 98 99 100 |
# File 'app/models/caboose_store/order.rb', line 94 def as_json(={}) self.attributes.merge({ :line_items => self.line_items, :shipping_address => self.shipping_address, :billing_address => self.billing_address }) end |
#authorized? ⇒ Boolean
130 131 132 |
# File 'app/models/caboose_store/order.rb', line 130 def self.financial_status == 'authorized' end |
#calculate ⇒ Object
154 155 156 157 158 159 160 |
# File 'app/models/caboose_store/order.rb', line 154 def calculate self.update_column(:subtotal, (self.calculate_subtotal * 100).ceil / 100.00) self.update_column(:tax, (self.calculate_tax * 100).ceil / 100.00) self.update_column(:shipping, (self.calculate_shipping * 100).ceil / 100.00) self.update_column(:handling, (self.calculate_handling * 100).ceil / 100.00) self.update_column(:total, (self.calculate_total * 100).ceil / 100.00) end |
#calculate_handling ⇒ Object
177 178 179 180 |
# File 'app/models/caboose_store/order.rb', line 177 def calculate_handling return 0 if !CabooseStore::handling_percentage self.shipping * CabooseStore::handling_percentage end |
#calculate_shipping ⇒ Object
172 173 174 175 |
# File 'app/models/caboose_store/order.rb', line 172 def calculate_shipping return 0 if !self.shipping_address || !self.shipping_method_code ShippingCalculator.rate(self)[:total_price] end |
#calculate_subtotal ⇒ Object
162 163 164 165 |
# File 'app/models/caboose_store/order.rb', line 162 def calculate_subtotal return 0 if self.line_items.empty? self.line_items.collect { |line_item| line_item.price }.inject { |sum, price| sum + price } end |
#calculate_tax ⇒ Object
167 168 169 170 |
# File 'app/models/caboose_store/order.rb', line 167 def calculate_tax return 0 if !self.shipping_address self.subtotal * TaxCalculator.tax_rate(self.shipping_address) end |
#calculate_total ⇒ Object
182 183 184 |
# File 'app/models/caboose_store/order.rb', line 182 def calculate_total [self.subtotal, self.tax, self.shipping, self.handling].compact.inject { |sum, price| sum + price } end |
#capture ⇒ Object
134 135 136 |
# File 'app/models/caboose_store/order.rb', line 134 def capture PaymentProcessor.capture(self) end |
#decrement_quantities ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'app/models/caboose_store/order.rb', line 102 def decrement_quantities return false if self.decremented self.line_items.each do |line_item| line_item.variant.update_attribute(:quantity, line_item.variant.quantity_in_stock - line_item.quantity) end self.update_attribute(:decremented, true) end |
#increment_quantities ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'app/models/caboose_store/order.rb', line 112 def increment_quantities return false if !self.decremented self.line_items.each do |line_item| line_item.variant.update_attribute(:quantity, line_item.variant.quantity_in_stock - line_item.quantity) end self.update_attribute(:decremented, false) end |
#line_item_added(line_item) ⇒ Object
146 147 148 |
# File 'app/models/caboose_store/order.rb', line 146 def line_item_added(line_item) self.calculate end |
#line_item_removed(line_item) ⇒ Object
150 151 152 |
# File 'app/models/caboose_store/order.rb', line 150 def line_item_removed(line_item) self.calculate end |
#refuned ⇒ Object
138 139 140 |
# File 'app/models/caboose_store/order.rb', line 138 def refuned PaymentProcessor.refund(self) end |
#resend_confirmation ⇒ Object
122 123 124 |
# File 'app/models/caboose_store/order.rb', line 122 def resend_confirmation OrdersMailer.customer_new_order(self).deliver end |
#shipping_and_handling ⇒ Object
186 187 188 |
# File 'app/models/caboose_store/order.rb', line 186 def shipping_and_handling (self.shipping ? self.shipping : 0.0) + (self.handling ? self.handling : 0.0) end |
#test? ⇒ Boolean
126 127 128 |
# File 'app/models/caboose_store/order.rb', line 126 def test? self.status == 'testing' end |
#void ⇒ Object
142 143 144 |
# File 'app/models/caboose_store/order.rb', line 142 def void PaymentProcessor.void(self) end |