cirandas.net

ref: master

app/models/create_enterprise.rb


  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
222
223
224
225
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 address
    zip_code city state country district lat lng ] - [ 'location' ]
  DATA_FIELDS.each do |field|
    settings_items field.to_sym
  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_numericality_of :foundation_year, only_integer: true, if: -> o { o.foundation_year.present? }

  # checks for actual attributes
  validates_presence_of :requestor_id, :target_id

  validates :requestor, kind_of: {kind: Person}

  # 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'

  validate :validator_correct_region
  validate :not_used_identifier

  def validator_correct_region
    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').fix_i18n)
      end
    end
  end

  def not_used_identifier
    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.').fix_i18n)
    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)
    unless value.kind_of?(Region)
      begin
        value = Region.find(value)
      rescue
        raise ArgumentError.new("Could not find any region with the id #{value}")
      end
    end

    @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}.').html_safe}
  end

  def reject_details
    true
  end

  def task_created_message
    # CIRANDAS: disable it
    return super

    _('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
    # CIRANDAS: disable it
    return nil

    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