Class: Shoppe::OrderItem

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_product(product, quantity = 1) ⇒ Object

This allows you to add a product to the scoped order. For example Order.first.order_items.add_product(…). This will either increase the quantity of the value in the order or create a new item if one does not exist already.



32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/shoppe/order_item.rb', line 32

def self.add_product(product, quantity = 1)
  transaction do
    if existing = self.where(:product_id => product.id).first
      existing.increase!(quantity)
      existing
    else
      item = self.create(:product => product, :quantity => 0)
      item.increase!(quantity)
    end
  end
end

Instance Method Details

#confirm!Object

This method will be triggered when the parent order is confirmed. This should automatically update the stock levels on the source product.



93
94
95
# File 'app/models/shoppe/order_item.rb', line 93

def confirm!
  self.product.update_stock_level(quantity)
end

#decrease!(amount = 1) ⇒ Object

Decreases the quantity of items in the order by the number provided.



68
69
70
71
72
73
74
# File 'app/models/shoppe/order_item.rb', line 68

def decrease!(amount = 1)
  transaction do
    self.quantity -= amount
    self.quantity == 0 ? self.destroy : self.save!
    self.order.remove_delivery_service_if_invalid
  end
end

#in_stock?Boolean

Do we have the stock needed to fulfil this order?

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'app/models/shoppe/order_item.rb', line 98

def in_stock?
  if self.product.stock_control?
    self.product.stock >= self.quantity
  else
    true
  end
end

#increase!(amount = 1) ⇒ Object

Increase the quantity of items in the order by the number provided. Will raise an error if we don’t have the stock to do this.



56
57
58
59
60
61
62
63
64
65
# File 'app/models/shoppe/order_item.rb', line 56

def increase!(amount = 1)
  transaction do
    self.quantity += amount
    if self.product.stock_control? && self.product.stock < self.quantity
      raise Shoppe::Errors::NotEnoughStock, :product => self.product, :requested_stock => self.quantity
    end
    self.save!
    self.order.remove_delivery_service_if_invalid
  end
end

#removeObject

This allows you to remove a product from an order. It will also ensure that the order’s custom delivery service is updated.



46
47
48
49
50
51
# File 'app/models/shoppe/order_item.rb', line 46

def remove
  transaction do
    self.destroy!
    self.order.remove_delivery_service_if_invalid
  end
end

#sub_totalObject

Return the sub total for the product



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

def sub_total
  quantity * unit_price
end

#totalObject

Return the total price including tax for the order line



87
88
89
# File 'app/models/shoppe/order_item.rb', line 87

def total
  tax_amount + sub_total
end

#total_costObject

Return the total cost for the product



77
78
79
# File 'app/models/shoppe/order_item.rb', line 77

def total_cost
  quantity * unit_cost_price
end

#validate_stock_levelsObject

Validate the stock level against the product and update as appropriate. This method will be executed before an order is completed. If we have run out of this product, we will update the quantity to an appropriate level (or remove the order item) and return the object.



109
110
111
112
113
114
115
116
117
# File 'app/models/shoppe/order_item.rb', line 109

def validate_stock_levels
  if in_stock?
    false
  else
    self.quantity = self.product.stock
    self.quantity == 0 ? self.destroy : self.save!
    self
  end
end