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.



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

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

#accept!Object

This method will be trigger when the parent order is accepted.



102
103
# File 'app/models/shoppe/order_item.rb', line 102

def accept!
end

#confirm!Object

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



95
96
97
98
99
# File 'app/models/shoppe/order_item.rb', line 95

def confirm!
  if self.product.stock_control?
    self.product.stock_level_adjustments.create(:parent => self, :adjustment => 0 - self.quantity, :description => "Order ##{self.order.number} deduction")
  end
end

#decrease!(amount = 1) ⇒ Object

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



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

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)


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

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.



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

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

#reject!Object

This method will be trigger when the parent order is rejected.



106
107
108
# File 'app/models/shoppe/order_item.rb', line 106

def reject!
  self.stock_level_adjustments.destroy_all
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.



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

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

#sub_totalObject

Return the sub total for the product



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

def sub_total
  quantity * unit_price
end

#totalObject

Return the total price including tax for the order line



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

def total
  tax_amount + sub_total
end

#total_costObject

Return the total cost for the product



79
80
81
# File 'app/models/shoppe/order_item.rb', line 79

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.



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

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