Class: Basket

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

Instance Method Summary collapse

Instance Method Details

#add_product(prod, quant = 1) ⇒ Object

when adding a product (with quantity) we ensure there is only one item for each product



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/basket.rb', line 94

def add_product prod , quant = 1
  return unless prod
  return unless quant != 0
  raise "Locked since #{self.locked}" if self.locked
  exists = items.where(:product_id => prod.id ).limit(1).first
  if exists
    exists.quantity += quant
  else
    exists = items.new :quantity => quant , :product => prod , :price => prod.price , 
                       :tax => prod.tax , :name => prod.full_name
  end
  if( exists.quantity == 0)
    exists.delete
    touch
  else
    exists.save!
  end
  reload
end

#cache_totalObject



20
21
22
23
# File 'app/models/basket.rb', line 20

def cache_total
  self.total_price = (items.to_a.sum{ |i| i.total }).round(2)
  self.total_tax =  (items.to_a.sum{ |i| i.tax_amount}).round(2)
end

#deduct!Object

locks the basket so receiving or deducting becomes an error. deduct the items from inventory, change affects immediately in the products



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/basket.rb', line 61

def deduct!
  raise "Locked since #{self.locked}" if self.locked
  sum = 0
  self.items.each do |item|
    prod = item.product
    prod.inventory = prod.inventory - item.quantity
    sum += item.quantity
    prod.save!
  end
  self.locked = Date.today
  self.save!
  sum
end

#empty?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/models/basket.rb', line 16

def empty?
  self.items.empty?
end

#inventory!Object

inventoying the basket means setting the item quantity as the stock we actually change the basket for it to be a relative change (so as to look like a receive)



77
78
79
80
# File 'app/models/basket.rb', line 77

def inventory!
  self.items.each { |item| item.quantity -= item.product.inventory  }
  self.receive!
end

#isa(typ) ⇒ Object



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

def isa typ
  self.kori_type.to_s.downcase == typ.to_s.downcase && self.kori_id != nil
end

#quantityObject



12
13
14
# File 'app/models/basket.rb', line 12

def quantity
  items.sum(:quantity)
end

#receive!Object

receiving the goods means that the item quantity is added to the stock (product.inventory) also we change the price to the products cost price locks the basket so receiving or deductiing becomes an error.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/basket.rb', line 43

def receive!
  raise "Locked since #{self.locked}" if self.locked
  sum = 0
  self.items.each do |item|
    prod = item.product
    prod.inventory = prod.inventory + item.quantity
    prod.save!
    sum += item.quantity
    item.price = item.product.cost
#      item.save!
  end
  self.locked = Date.today
  self.save!
  sum
end

#suppliersObject



86
87
88
89
90
91
# File 'app/models/basket.rb', line 86

def suppliers
  ss = items.collect{|i| i.product.supplier if i.product}
  ss.uniq!
  ss.delete(nil)
  ss
end

#taxesObject

return a hash of rate => amount



32
33
34
35
36
37
38
# File 'app/models/basket.rb', line 32

def taxes
  taxes = Hash.new(0.0)
  items.each do |item|
    taxes[item.tax] += item.tax_amount
  end
  taxes
end

#touchObject



25
26
27
28
29
# File 'app/models/basket.rb', line 25

def touch
  cache_total
  super
  save!
end