Class: Basket
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Basket
- Defined in:
- app/models/basket.rb
Instance Method Summary collapse
-
#add_product(prod, quant = 1) ⇒ Object
when adding a product (with quantity) we ensure there is only one item for each product.
- #cache_total ⇒ Object
-
#cancel_order! ⇒ Object
return inventory and cancel basket very similar to receive, just we don’t change prices (and don’t lock).
-
#deduct! ⇒ Object
deduct the items from inventory, change affects immediately in the products locks the basket so receiving or deducting becomes an error.
- #empty? ⇒ Boolean
-
#inventory! ⇒ Object
inventorying 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).
- #isa(typ) ⇒ Object
- #locked? ⇒ Boolean
- #quantity ⇒ Object
-
#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 deducting becomes an error.
- #suppliers ⇒ Object
-
#taxes ⇒ Object
return a hash of rate => amount.
- #touch ⇒ Object
-
#zero_prices! ⇒ Object
set all items prices to zero.
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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/basket.rb', line 108 def add_product prod , quant = 1 return unless prod return unless quant != 0 raise "Locked since #{self.locked}" if 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_total ⇒ Object
25 26 27 28 |
# File 'app/models/basket.rb', line 25 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 |
#cancel_order! ⇒ Object
return inventory and cancel basket very similar to receive, just we don’t change prices (and don’t lock)
74 75 76 77 78 |
# File 'app/models/basket.rb', line 74 def cancel_order! self.locked = nil do_receive(false) #don't change prices self.save! end |
#deduct! ⇒ Object
deduct the items from inventory, change affects immediately in the products locks the basket so receiving or deducting becomes an error.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/basket.rb', line 58 def deduct! raise "Locked since #{self.locked}" if 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
17 18 19 |
# File 'app/models/basket.rb', line 17 def empty? self.items.empty? end |
#inventory! ⇒ Object
inventorying 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)
82 83 84 85 |
# File 'app/models/basket.rb', line 82 def inventory! self.items.each { |item| item.quantity -= item.product.inventory } self.receive! end |
#isa(typ) ⇒ Object
96 97 98 |
# File 'app/models/basket.rb', line 96 def isa typ self.kori_type.to_s.downcase == typ.to_s.downcase && self.kori_id != nil end |
#locked? ⇒ Boolean
21 22 23 |
# File 'app/models/basket.rb', line 21 def locked? not self.locked.blank? end |
#quantity ⇒ Object
13 14 15 |
# File 'app/models/basket.rb', line 13 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 deducting becomes an error.
48 49 50 51 52 53 54 |
# File 'app/models/basket.rb', line 48 def receive! raise "Locked since #{self.locked}" if locked? sum = do_receive(true) # change the prices self.locked = Date.today self.save! sum end |
#suppliers ⇒ Object
100 101 102 103 104 105 |
# File 'app/models/basket.rb', line 100 def suppliers ss = items.collect{|i| i.product.supplier if i.product} ss.uniq! ss.delete(nil) ss end |
#taxes ⇒ Object
return a hash of rate => amount
37 38 39 40 41 42 43 |
# File 'app/models/basket.rb', line 37 def taxes taxes = Hash.new(0.0) items.each do |item| taxes[item.tax] += item.tax_amount end taxes end |
#touch ⇒ Object
30 31 32 33 34 |
# File 'app/models/basket.rb', line 30 def touch cache_total super save! end |
#zero_prices! ⇒ Object
set all items prices to zero
88 89 90 91 92 93 94 |
# File 'app/models/basket.rb', line 88 def zero_prices! raise "Locked since #{self.locked}" if locked? self.items.each do |item| item.price = 0.0 end self.save! end |