contract.rb
1.98 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
72
73
74
75
76
77
78
79
80
81
82
83
84
class BscPlugin::Contract < Noosfero::Plugin::ActiveRecord
validates_presence_of :bsc, :client_name
has_many :sales, :class_name => 'BscPlugin::Sale'
has_many :products, :through => :sales
has_and_belongs_to_many :enterprises, :join_table => 'bsc_plugin_contracts_enterprises'
belongs_to :bsc, :class_name => 'BscPlugin::Bsc'
named_scope :status, lambda { |status_list| status_list.blank? ? {} : {:conditions => ['status in (?)', status_list]} }
named_scope :sorted_by, lambda { |sorter, direction| {:order => "#{sorter} #{direction}"} }
before_create do |contract|
contract.created_at ||= Time.now.utc
contract.updated_at ||= Time.now.utc
end
before_update do |contract|
contract.updated_at ||= Time.now.utc
end
module Status
OPENED = 0
NEGOTIATING = 1
EXECUTING = 2
CLOSED = 3
def self.types
[OPENED, NEGOTIATING, EXECUTING, CLOSED]
end
def self.names
[_('Opened'), _('Negotiating'), _('Executing'), _('Closed')]
end
end
module ClientType
STATE = 0
FEDERAL = 1
def self.types
[STATE, FEDERAL]
end
def self.names
[c_('State'), _('Federal')]
end
end
module BusinessType
PROJECTA = 0
PROJECTB = 1
def self.types
[PROJECTA, PROJECTB]
end
def self.names
[_('ProjectA'), _('ProjectB')]
end
end
def enterprises_to_token_input
enterprises.map { |enterprise| {:id => enterprise.id, :name => enterprise.name} }
end
def save_sales(sales)
failed_sales = {}
sales.each do |sale|
sale.merge!({:contract_id => id})
begin
BscPlugin::Sale.create!(sale)
rescue Exception => exception
name = Product.find(sale[:product_id]).name
failed_sales[exception.clean_message] ? failed_sales[exception.clean_message] << name : failed_sales[exception.clean_message] = [name]
end
end
failed_sales
end
def total_price
sales.inject(0) {|result, sale| sale.price*sale.quantity + result}
end
end