Fix balance and order paying

This commit is contained in:
Benjamin Cousaert 2014-12-10 10:00:00 +01:00
parent 11097ccfb9
commit f6a37a80cf
3 changed files with 13 additions and 3 deletions

View file

@ -33,7 +33,7 @@ class Order < ActiveRecord::Base
def price def price
price = 0 price = 0
products.each do |p| products.each do |p|
price += p.price * p.count(self) price += p.read_attribute(:price) * p.count(self)
end end
price price
end end

View file

@ -39,6 +39,16 @@ class User < ActiveRecord::Base
end end
def pay(amount) def pay(amount)
self.increment!(:balance, - amount) write_attribute(:balance, read_attribute(:balance) - amount)
self.save
end
def balance
(read_attribute(:balance) || 0) / 100.0
end
def balance=(value)
if value.is_a? String then value.sub!(',', '.') end
write_attribute(:balance, (value.to_f * 100).to_int)
end end
end end

View file

@ -6,7 +6,7 @@
<p><strong>Name:</strong> <%= user.name %></p> <p><strong>Name:</strong> <%= user.name %></p>
<p><strong>Last name:</strong> <%= user.last_name %></p> <p><strong>Last name:</strong> <%= user.last_name %></p>
<p><strong>Nickname:</strong> <%= user.nickname %></p> <p><strong>Nickname:</strong> <%= user.nickname %></p>
<p><strong>Balance:</strong> <%= user.balance %></p> <p><strong>Balance:</strong> <%= euro(user.balance) %></p>
</div> </div>
<div class="footer"> <div class="footer">
<p> <p>