def find_best_simple_for program_id site_id answers look up the simple

 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
def find_best_simple_form(program_id, site_id, answers)
# # look up the simple form
# find active, available forms with this program
potential_form_ids = LeadGen::SimpleForm.connection.select_values("
select f.id
from forms_with_available f, simple_forms_programs sfp
where f.id = sfp.simple_form_id
and sfp.program_id = #{program_id.to_i.to_s}
and f.id not in (select simple_form_id from disallowed_simple_forms_sites where site_id = #{site_id})
and available = true
").collect {|id| id.to_i}
raise NoFormForProgramError.new({ :program_id => program_id }, @arrival.id,
"The program '#{program_id}' has no available forms.") if potential_form_ids.blank?
questions = AffiliatePrograms.get_questions_for_program(program_id, site_id)
raise NoQuestionsForProgramError.new({ :program_id => program_id }, @arrival.id,
"The program '#{program_id}' has no questions.") if questions.blank?
questions_missing_answers = questions.select {|q| !answers.has_key?(q.id) or CaUtil.something_empty?(answers[q.id])}
unless questions_missing_answers.blank?
raise MissingAnswersError.new(questions_missing_answers.collect {|a| {:id => a.id, :display_name => a.display_name} }, @arrival.id,
"Lead not valid. Missing answers to the following questions:
#{questions_missing_answers.map(&:id).join(", \n")}")
end
# validate answers before qualifying them
invalid_answers = []
questions.each do |q|
answer = answers[q.id]
if q.respond_to?(:validate_field_type)
valid, message = q.validate_field_type(answer)
invalid_answers << {
:name => "question_#{q.id.to_s}",
:display_name => q.display_name,
:value => (answer.is_a?(Hash) ? answer.map{|k,v| "#{k.to_s}:#{v.to_s}"}.join(', ') : answer.to_s),
:message => message
} unless valid
end
end
raise InvalidOptionForFieldError.new(invalid_answers, @arrival.id,
"Invalid answers submitted for some questions: #{
invalid_answers.map{|a|
"#{a[:display_name]}(#{a[:name]}): #{a[:value]}"
}.join(",\n")
}") unless invalid_answers.blank? #"
# figure out which of the remaining forms the user qualifies for
qualified = potential_form_ids
questions.each do |q|
qualified = q.qualified_form_ids(answers[q.id], qualified)
if qualified.blank?
break
end
end
# if there are none, return error
raise NotQualifiedError.new({ :program_id => program_id }, @arrival.id,
"Lead is not qualified for any form.") if qualified.blank?
# if there is one, proceed with the highest commission one
form_id = ::ActiveRecord::Base.connection.select_values(
"select id
from forms
where id in (#{qualified.join(',')})
order by commission_cents desc
limit 1"
).to_s
# if form disappeared, return error
raise FormNotFoundError.new({ :program_id => program_id,
:form_id => form_id }, @arrival.id,
"User was qualified for the form '#{form_id}' of the program '#{program_id}' but the form was not found.") if form_id.blank?
return LeadGen::SimpleForm.find(form_id)
end