input.rb
1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Input < ActiveRecord::Base
attr_accessible :product, :product_id, :product_category, :product_category_id,
:amount_used, :unit_id, :price_per_unit, :relevant_to_price, :is_from_solidarity_economy
belongs_to :product
belongs_to :product_category
validates_presence_of :product
validates_presence_of :product_category
acts_as_list :scope => :product
belongs_to :unit
scope :relevant_to_price, :conditions => { :relevant_to_price => true }
include FloatHelper
def price_per_unit=(value)
if value.is_a?(String)
super(decimal_to_float(value))
else
super(value)
end
end
def amount_used=(value)
if value.is_a?(String)
super(decimal_to_float(value))
else
super(value)
end
end
def name
product_category.name
end
def formatted_value(value)
("%.2f" % self[value]).to_s.gsub('.', product.enterprise.environment.currency_separator) if self[value]
end
def formatted_amount
amount = self.amount_used
return '' if amount.blank? || amount.zero?
("%.2f" % amount).to_s.gsub('.00', '').gsub('.', product.enterprise.environment.currency_separator)
end
def has_price_details?
%w[price_per_unit amount_used].each do |field|
return true unless self.send(field).blank?
end
false
end
def has_all_price_details?
%w[price_per_unit unit amount_used].each do |field|
return false if self.send(field).blank?
end
true
end
def cost
return 0 if self.amount_used.blank? || self.price_per_unit.blank?
self.amount_used * self.price_per_unit
end
alias_method :price, :cost
end