Class: Caboose::Order
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Order
- Defined in:
- app/models/caboose/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
95 96 97 98 99 100 101 |
# File 'app/models/caboose/order.rb', line 95 def as_json(={}) self.attributes.merge({ :line_items => self.line_items, :shipping_address => self.shipping_address, :billing_address => self.billing_address }) end |
#authorized? ⇒ Boolean
131 132 133 |
# File 'app/models/caboose/order.rb', line 131 def self.financial_status == 'authorized' end |
#calculate ⇒ Object
155 156 157 158 159 160 161 |
# File 'app/models/caboose/order.rb', line 155 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
178 179 180 181 |
# File 'app/models/caboose/order.rb', line 178 def calculate_handling return 0 if !Caboose::store_handling_percentage self.shipping * Caboose::store_handling_percentage end |
#calculate_shipping ⇒ Object
173 174 175 176 |
# File 'app/models/caboose/order.rb', line 173 def calculate_shipping return 0 if !self.shipping_address || !self.shipping_method_code ShippingCalculator.rate(self)[:total_price] end |
#calculate_subtotal ⇒ Object
163 164 165 166 |
# File 'app/models/caboose/order.rb', line 163 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
168 169 170 171 |
# File 'app/models/caboose/order.rb', line 168 def calculate_tax return 0 if !self.shipping_address self.subtotal * TaxCalculator.tax_rate(self.shipping_address) end |
#calculate_total ⇒ Object
183 184 185 |
# File 'app/models/caboose/order.rb', line 183 def calculate_total [self.subtotal, self.tax, self.shipping, self.handling].compact.inject { |sum, price| sum + price } end |
#capture ⇒ Object
135 136 137 |
# File 'app/models/caboose/order.rb', line 135 def capture PaymentProcessor.capture(self) end |
#decrement_quantities ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'app/models/caboose/order.rb', line 103 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
113 114 115 116 117 118 119 120 121 |
# File 'app/models/caboose/order.rb', line 113 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
147 148 149 |
# File 'app/models/caboose/order.rb', line 147 def line_item_added(line_item) self.calculate end |
#line_item_removed(line_item) ⇒ Object
151 152 153 |
# File 'app/models/caboose/order.rb', line 151 def line_item_removed(line_item) self.calculate end |
#refuned ⇒ Object
139 140 141 |
# File 'app/models/caboose/order.rb', line 139 def refuned PaymentProcessor.refund(self) end |
#resend_confirmation ⇒ Object
123 124 125 |
# File 'app/models/caboose/order.rb', line 123 def resend_confirmation OrdersMailer.customer_new_order(self).deliver end |
#shipping_and_handling ⇒ Object
187 188 189 |
# File 'app/models/caboose/order.rb', line 187 def shipping_and_handling (self.shipping ? self.shipping : 0.0) + (self.handling ? self.handling : 0.0) end |
#test? ⇒ Boolean
127 128 129 |
# File 'app/models/caboose/order.rb', line 127 def test? self.status == 'testing' end |
#void ⇒ Object
143 144 145 |
# File 'app/models/caboose/order.rb', line 143 def void PaymentProcessor.void(self) end |