create_enterprise.rb
6.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
class CreateEnterprise < Task
  N_('Identifier')
  N_('Name')
  N_('Address')
  N_('Contact phone')
  N_('Contact person')
  N_('Acronym')
  N_('Foundation year')
  N_('Legal form')
  N_('Economic activity')
  N_('Management information')
  DATA_FIELDS = Enterprise.fields + %w[name identifier region_id]
  serialize :data, Hash
  attr_protected :data
  def data
    self[:data] ||= Hash.new
  end
  DATA_FIELDS.each do |field|
    # getter
    define_method(field) do
      self.data[field.to_sym]
    end
    # setter
    define_method("#{field}=") do |value|
      self.data[field.to_sym] = value
    end
  end
  # checks for virtual attributes 
  validates_presence_of :name, :identifier
  #checks if the validation method is region to validates
  validates_presence_of :region_id, :if => lambda { |obj| obj.environment.organization_approval_method == :region }
  validates_format_of :foundation_year, :with => /^\d*$/
  # checks for actual attributes
  validates_presence_of :requestor_id, :target_id
  # checks for admins required attributes
  DATA_FIELDS.each do |attribute|
    validates_presence_of attribute, :if => lambda { |obj| obj.environment.required_enterprise_fields.include?(attribute) }
  end
  # check for explanation when rejecting
  validates_presence_of :reject_explanation, :if => (lambda { |record| record.status == Task::Status::CANCELLED } )
  xss_terminate :only => [ :acronym, :address, :contact_person, :contact_phone, :economic_activity, :legal_form, :management_information, :name ], :on => 'validation'
  def validate
    if self.region && self.target
      unless self.region.validators.include?(self.target) || self.target_type == "Environment"
        self.errors.add(:target, '%{fn} is not a validator for the chosen region')
      end
    end
    if self.status != Task::Status::CANCELLED && self.identifier && Profile.exists?(:identifier => self.identifier)
      self.errors.add(:identifier, '%{fn} is already being as identifier by another enterprise, organization or person.')
    end
  end
  def valid_before_selecting_target?
    if valid?
      true
    else
      self.errors.size == 1 && !self.errors[:target_id].nil?
    end
  end
  # gets the associated region for the enterprise creation
  def region(reload = false)
    if self.region_id
      if reload || @region.nil?
        @region = Region.find(self.region_id)
      end
    end
    @region
  end
  # sets the associated region for the enterprise creation
  def region=(value)
    raise ArgumentError.new("Region expected, but got #{value.class}") unless value.kind_of?(Region)
    @region = value
    self.region_id = value.id
  end
  def environment
    requestor.environment
  end
  def available_regions
    environment.regions.with_validators
  end
  def active_fields
    environment ? environment.active_enterprise_fields : []
  end
  def required_fields
    environment ? environment.required_enterprise_fields : []
  end
  def signup_fields
    environment ? environment.signup_enterprise_fields : []
  end
  def community?
    false
  end
  def enterprise?
    true
  end
  # Rejects the enterprise registration request.
  def reject
    cancel
  end
  def rejected?
    self.status == Task::Status::CANCELLED
  end
  # Approves the enterprise registration request.
  def approve
    finish
  end
  # tells if this request was appoved 
  def approved?
    self.status == Task::Status::FINISHED
  end
  # actually creates the enterprise after the request is approved.
  def perform
    enterprise = Enterprise.new
    DATA_FIELDS.reject{|field| field == "reject_explanation"}.each do |field|
      enterprise.send("#{field}=", self.send(field))
    end
    enterprise.environment = environment
    enterprise.user = self.requestor.user
    enterprise.save!
    enterprise.add_admin(enterprise.user.person)
  end
  def title
    _("Enterprise registration")
  end
  def icon
    {:type => :defined_image, :src => '/images/icons-app/enterprise-minor.png', :name => name}
  end
  def subject
    name
  end
  def information
    {:message => _('%{requestor} wants to create enterprise %{subject}.')}
  end
  def reject_details
    true
  end
  def task_created_message
    _('Your request for registering enterprise "%{enterprise}" at %{environment} was just received. It will be reviewed by the validator organization of your choice, according to its methods and criteria.
      You will be notified as soon as the validator organization has a position about your request.') % { :enterprise => self.name, :environment => self.environment }
  end
  def task_finished_message
    __('Your request for registering the enterprise "%{enterprise}" was approved. You can access %{environment} now and provide start providing all relevant information your new enterprise.') % { :enterprise => self.name, :environment => self.environment }
  end
  def task_cancelled_message
    __("Your request for registering the enterprise %{enterprise} at %{environment} was NOT approved by the validator organization. The following explanation was given: \n\n%{explanation}") % { :enterprise => self.name, :environment => self.environment, :explanation => self.reject_explanation }
  end
  def target_notification_message
    msg = ""
    msg << __("Enterprise \"%{enterprise}\" just requested to enter %{environment}. You have to approve or reject it through the \"Pending Validations\" section in your control panel.\n") % { :enterprise => self.name, :environment => self.environment }
    msg << "\n"
    msg << __("The data provided by the enterprise was the following:\n") << "\n"
    msg << (_("Name: %s") % self.name) << "\n"
    msg << (_("Acronym: %s") % self.acronym) << "\n"
    msg << (_("Address: %s") % self.address) << "\n"
    msg << (_("Legal form: %s") % self.legal_form) << "\n"
    msg << (_("Foundation Year: %d") % self.foundation_year) << "\n" unless self.foundation_year.blank?
    msg << (_("Economic activity: %s") % self.economic_activity) << "\n"
    msg << __("Information about enterprise's management:\n") << self.management_information.to_s << "\n"
    msg << (_("Contact phone: %s") % self.contact_phone) << "\n"
    msg << (_("Contact person: %s") % self.contact_person) << "\n"
    msg << __('CreateEnterprise|Identifier')
    msg
  end
  def target_notification_description
    _('%{requestor} wants to create enterprise %{subject}.') % {:requestor => requestor.name, :subject => subject}
  end
  def permission
    :validate_enterprise
  end
end