Class: OrderItem

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

Overview

Schema Information

Table name: order_items

id               :integer(4)      not null, primary key
price            :decimal(8, 2)
total            :decimal(8, 2)
order_id         :integer(4)      not null
variant_id       :integer(4)      not null
state            :string(255)     not null
tax_rate_id      :integer(4)
shipping_rate_id :integer(4)
shipment_id      :integer(4)
created_at       :datetime
updated_at       :datetime

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (OrderItem) order_items_in_cart(order_id)

called in checkout process. will give you the 'quantity', 'sum of all the prices' and 'sum of all the totals'

it is better to do the math in SQL than ruby

Parameters:

  • order.id (Integer)

Returns:

  • (OrderItem)

    Object has addional attributes of [sum_price, sum_total, shipping_category_id, and quantity]



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/order_item.rb', line 100

def self.order_items_in_cart(order_id)
  find(:all, :joins => {:variant => :product },
             :conditions => { :order_items => { :order_id => order_id}},
             :select => "order_items.id, order_items.order_id, order_items.shipping_rate_id, order_items.state, order_items.tax_rate_id, order_items.price, order_items.total, order_items.variant_id,
                         products.shipping_category_id,
                                        count(*) as quantity,
                                        products.shipping_category_id as shipping_category_id,
                                        SUM(order_items.price) as sum_price,
                                        SUM(order_items.total) as sum_total",
             :group => "order_items.id,
                        products.shipping_category_id,
                        order_items.order_id,
                        order_items.shipping_rate_id,
                        order_items.state,
                        order_items.tax_rate_id,
                        order_items.price,
                        order_items.total, order_items.variant_id")
end

Instance Method Details

- (Float) adjusted_price(coupon = nil)

this is the price after coupons and anything before calculating the price + tax

in the future coupons and discounts could change this value

Parameters:

  • (none)

Returns:

  • (Float)

    this is the price that the tax will be applied to.



156
157
158
159
160
161
# File 'app/models/order_item.rb', line 156

def adjusted_price(coupon = nil)
  ## coupon credit is calculated at the order level but because taxes we need to apply it now
  coupon_credit = coupon ? coupon.value([sale_price(order.transaction_time)], order) : 0.0

  self.price - coupon_credit
end

- (Float) amount_of_charge_is_vat

the VAT charge on an item

Parameters:

  • (none)

Returns:

  • (Float)

    tax charge on the item.



208
209
210
211
# File 'app/models/order_item.rb', line 208

def amount_of_charge_is_vat
  vat_percentage = tax_rate.try(:vat_percentage) ? tax_rate.vat_percentage : 0.0
  (adjusted_price * (vat_percentage / (100.0 + vat_percentage))).round_at(2)
end

- (Float) amount_of_charge_without_vat

the amount of an item if there were zero VAT

Parameters:

  • (none)

Returns:

  • (Float)

    tax charge on the item.



217
218
219
220
# File 'app/models/order_item.rb', line 217

def amount_of_charge_without_vat
  vat_percentage = tax_rate.try(:vat_percentage) ? tax_rate.vat_percentage : 0.0
  (100.0 * adjusted_price / (100.0 + vat_percentage)).round_at(2)
end

- (none) calculate_order

forces the order to be re-calculated. If the order item has changed then the order totals need to be adjusted

Parameters:

  • (none)

Returns:

  • (none)


123
124
125
126
127
128
129
# File 'app/models/order_item.rb', line 123

def calculate_order
  if self.ready_to_calculate? &&
      (shipping_rate_id != @beginning_shipping_rate_id || tax_rate_id != @beginning_tax_rate_id)
    set_beginning_values
    order.calculate_totals
  end
end

- (Float) calculate_total(coupon = nil)

this is the price after coupons, deals and sales

* this method does not save it just sets the value of total.
* Thus allowing you to save the whole order with one opperation

Parameters:

  • (none)

Returns:

  • (Float)

    this is the total of the item after coupons/deals/sales...



189
190
191
192
193
# File 'app/models/order_item.rb', line 189

def calculate_total(coupon = nil)
  # shipping charges are calculated in order.rb

  self.total = (adjusted_price(coupon)).round_at(2)
end

- (Float) item_total(coupon = nil)

this is the price after coupons and taxes

* this return total if has not been calculated, otherwise calculates the total.

Parameters:

  • (none)

Returns:

  • (Float)

    this is the total of the item after taxes and coupons...



177
178
179
180
181
# File 'app/models/order_item.rb', line 177

def item_total(coupon = nil)
  # shipping charges are calculated in order.rb

  self.total ||= calculate_total(coupon)
end

- (Object) product_type



52
53
54
# File 'app/models/order_item.rb', line 52

def product_type
  variant.product.product_type
end

- (Object) product_type_ids



56
57
58
# File 'app/models/order_item.rb', line 56

def product_type_ids
  product_type.self_and_ancestors.map(&:id)
end

- (Boolean) ready_to_calculate?

determines if the order item has all the attributes set and thus you can now determine the final total

Parameters:

  • (none)

Returns:

  • (Boolean)


146
147
148
# File 'app/models/order_item.rb', line 146

def ready_to_calculate?
  shipping_rate_id && tax_rate_id
end

- (Object) sale_at(at = Time.zone.now)



168
169
170
# File 'app/models/order_item.rb', line 168

def sale_at(at = Time.zone.now)
  Sale.for(variant.product_id, at)
end

- (Object) sale_price(at)



163
164
165
166
# File 'app/models/order_item.rb', line 163

def sale_price(at)
  sale = sale_at(at)
  sale ? ( (1.0 - sale.percent_off) * self.price ).round_at(2) : self.price
end

- (Object) set_beginning_values



34
35
36
37
38
# File 'app/models/order_item.rb', line 34

def set_beginning_values
  @beginning_tax_rate_id      = self.tax_rate_id      rescue @beginning_tax_rate_id = nil # this stores the initial value of the tax_rate
  @beginning_shipping_rate_id = self.shipping_rate_id rescue @beginning_shipping_rate_id = nil # this stores the initial value of the tax_rate
  @beginning_total            = self.total            rescue @beginning_total = nil # this stores the initial value of the total
end

- (none) set_order_calculated_at_to_nil

if something changes to the order item and you dont want to recalculate

(maybe because you are chnging several more things) then
 this method will mark the calculated at to be nil and thus tell the order that
 it needs to calculate the total again

Parameters:

  • (none)

Returns:

  • (none)


138
139
140
# File 'app/models/order_item.rb', line 138

def set_order_calculated_at_to_nil
  order.update_attribute(:calculated_at, nil)
end

- (Object) ship_category_id



85
86
87
# File 'app/models/order_item.rb', line 85

def ship_category_id
  variant.product.shipping_category_id
end

- (Boolean) shipped?

if the order item has been shipped it will return true

Parameters:

  • (none)

Returns:

  • (Boolean)


64
65
66
# File 'app/models/order_item.rb', line 64

def shipped?
  shipment_id?
end

- (ShippingMethod) shipping_method

shipping method for the order item

Parameters:

  • (none)

Returns:



72
73
74
# File 'app/models/order_item.rb', line 72

def shipping_method
  shipping_rate.shipping_method
end

- (Integer) shipping_method_id

shipping method id for the order item (use to reduce SQL calls)

refer to ShippingMethod ID constants to determine what shipping method is called

Parameters:

  • (none)

Returns:

  • (Integer)

    ShippingMethod id



81
82
83
# File 'app/models/order_item.rb', line 81

def shipping_method_id
  shipping_rate.shipping_method_id
end

- (Object) shipping_rate_options(total_charge)



89
90
91
92
93
# File 'app/models/order_item.rb', line 89

def shipping_rate_options(total_charge)
  ShippingRate.joins(:shipping_method).where(['shipping_rates.shipping_category_id = ?
                      AND shipping_methods.shipping_zone_id = ?
                      AND shipping_rates.minimum_charge <= ?', ship_category_id, order.ship_address.state.shipping_zone_id, total_charge]).all
end

- (Float) tax_charge

the tax charge on an item

Parameters:

  • (none)

Returns:

  • (Float)

    tax charge on the item.



199
200
201
202
# File 'app/models/order_item.rb', line 199

def tax_charge
  tax_percentage = tax_rate.try(:tax_percentage) ? tax_rate.tax_percentage : 0.0
  adjusted_price * tax_percentage / 100.0
end