Class: Spina::Shop::OrderItem
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Spina::Shop::OrderItem
- Defined in:
- app/models/spina/shop/order_item.rb
Instance Attribute Summary collapse
-
#validate_stock ⇒ Object
Returns the value of attribute validate_stock.
Instance Method Summary collapse
- #allocated_stock(product_id) ⇒ Object
- #description ⇒ Object
-
#discount_amount ⇒ Object
Discount as amount.
- #in_stock? ⇒ Boolean
- #is_product_bundle? ⇒ Boolean
- #tax_modifier ⇒ Object
- #tax_rate ⇒ Object
- #total ⇒ Object
- #total_weight ⇒ Object
- #total_without_discount ⇒ Object
- #unallocated_stock(product_id) ⇒ Object
- #unit_cost_price ⇒ Object
- #unit_price ⇒ Object
- #weight ⇒ Object
Instance Attribute Details
#validate_stock ⇒ Object
Returns the value of attribute validate_stock.
3 4 5 |
# File 'app/models/spina/shop/order_item.rb', line 3 def validate_stock @validate_stock end |
Instance Method Details
#allocated_stock(product_id) ⇒ Object
69 70 71 |
# File 'app/models/spina/shop/order_item.rb', line 69 def allocated_stock(product_id) -stock_level_adjustments.where(product_id: product_id).sum(:adjustment) end |
#description ⇒ Object
37 38 39 |
# File 'app/models/spina/shop/order_item.rb', line 37 def description orderable.name end |
#discount_amount ⇒ Object
Discount as amount
29 30 31 |
# File 'app/models/spina/shop/order_item.rb', line 29 def discount_amount read_attribute(:discount_amount) || order.discount.try(:discount_for_order_item, self) || BigDecimal(0) end |
#in_stock? ⇒ Boolean
57 58 59 60 61 62 63 |
# File 'app/models/spina/shop/order_item.rb', line 57 def in_stock? if is_product_bundle? orderable.bundled_products.all?{|p| product_in_stock?(p.product_id)} else product_in_stock?(orderable_id) end end |
#is_product_bundle? ⇒ Boolean
65 66 67 |
# File 'app/models/spina/shop/order_item.rb', line 65 def is_product_bundle? orderable_type == "Spina::Shop::ProductBundle" end |
#tax_modifier ⇒ Object
49 50 51 |
# File 'app/models/spina/shop/order_item.rb', line 49 def tax_modifier (tax_rate + BigDecimal(100)) / BigDecimal(100) end |
#tax_rate ⇒ Object
24 25 26 |
# File 'app/models/spina/shop/order_item.rb', line 24 def tax_rate read_attribute(:tax_rate) || orderable.tax_group.tax_rate_for_order(order) || BigDecimal(0) end |
#total ⇒ Object
45 46 47 |
# File 'app/models/spina/shop/order_item.rb', line 45 def total total_without_discount - discount_amount end |
#total_weight ⇒ Object
53 54 55 |
# File 'app/models/spina/shop/order_item.rb', line 53 def total_weight quantity * weight end |
#total_without_discount ⇒ Object
41 42 43 |
# File 'app/models/spina/shop/order_item.rb', line 41 def total_without_discount unit_price * quantity end |
#unallocated_stock(product_id) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'app/models/spina/shop/order_item.rb', line 73 def unallocated_stock(product_id) multiplier = if is_product_bundle? orderable.bundled_products.find_by(product_id: product_id).try(:quantity).to_i else orderable_id == product_id ? 1 : 0 end quantity * multiplier - allocated_stock(product_id) end |
#unit_cost_price ⇒ Object
20 21 22 |
# File 'app/models/spina/shop/order_item.rb', line 20 def unit_cost_price read_attribute(:unit_cost_price) || orderable.cost_price || BigDecimal(0) end |
#unit_price ⇒ Object
16 17 18 |
# File 'app/models/spina/shop/order_item.rb', line 16 def unit_price read_attribute(:unit_price) || orderable.price_for_order(order) || BigDecimal(0) end |
#weight ⇒ Object
33 34 35 |
# File 'app/models/spina/shop/order_item.rb', line 33 def weight read_attribute(:weight) || orderable.weight || BigDecimal(0) end |