Class: Query::OpportunityQuery

Inherits:
BaseQuery
  • Object
show all
Defined in:
app/services/query/opportunity_query.rb

Instance Method Summary collapse

Constructor Details

#initialize(relation = nil) ⇒ OpportunityQuery

Returns a new instance of OpportunityQuery.



3
4
5
# File 'app/services/query/opportunity_query.rb', line 3

def initialize(relation = nil)
  super(relation || Opportunity.all)
end

Instance Method Details

#average_aging_in_days_for_wonObject



27
28
29
# File 'app/services/query/opportunity_query.rb', line 27

def average_aging_in_days_for_won
  average_aging_in_days_for_won_between
end

#average_aging_in_days_for_won_between(start_range = nil, end_range = nil) ⇒ Object



32
33
34
35
36
37
# File 'app/services/query/opportunity_query.rb', line 32

def average_aging_in_days_for_won_between(start_range = nil, end_range = nil)
  opportunity_records_between(start_range, end_range).
    where("opportunities.won_lost_date IS NOT NULL and opportunities.created_at IS NOT NULL").
    average("opportunities.won_lost_date - opportunities.created_at").
    won
end

#average_opportunity_valueObject



7
8
9
# File 'app/services/query/opportunity_query.rb', line 7

def average_opportunity_value
	@relation.with_value.average(:value).round(2)
end

#median_opportunity_valueObject



12
13
14
# File 'app/services/query/opportunity_query.rb', line 12

def median_opportunity_value
	self.class.median_record(@relation.with_value).try(:value) || 0.00
end

#opportunities_conversion_ratioObject



45
46
47
# File 'app/services/query/opportunity_query.rb', line 45

def opportunities_conversion_ratio
  opportunities_conversion_ratio_between(nil,nil)
end

#opportunities_conversion_ratio_between(start_range = nil, end_range = nil, precision = 2) ⇒ Object



50
51
52
53
54
# File 'app/services/query/opportunity_query.rb', line 50

def opportunities_conversion_ratio_between(start_range = nil, end_range = nil, precision = 2)
  won = opportunities_won_count_between(start_range,end_range) 
  total = opportunities_count_between(start_range,end_range)
  ratio = (won / total).to_f.round(precision)
end

#opportunities_count_between(start_range = nil, end_range = nil) ⇒ Object



67
68
69
# File 'app/services/query/opportunity_query.rb', line 67

def opportunities_count_between(start_range = nil, end_range = nil)
  opportunity_records_between(start_range, end_range).count
end

#opportunities_lost_count_between(start_range = nil, end_range = nil) ⇒ Object



57
58
59
# File 'app/services/query/opportunity_query.rb', line 57

def opportunities_lost_count_between(start_range = nil, end_range = nil)
 opportunity_records_between(start_range, end_range).lost.count
end

#opportunities_won_countObject



22
23
24
# File 'app/services/query/opportunity_query.rb', line 22

def opportunities_won_count
	opportunities_won_count_between(nil,nil)
end

#opportunities_won_count_between(start_range = nil, end_range = nil) ⇒ Object



62
63
64
# File 'app/services/query/opportunity_query.rb', line 62

def opportunities_won_count_between(start_range = nil, end_range = nil)
  opportunity_records_between(start_range, end_range).won.count
end

#opportunities_won_valueObject



17
18
19
# File 'app/services/query/opportunity_query.rb', line 17

def opportunities_won_value
  @relation.with_value.sum(:value)
end

#opportunities_won_value_between(start_range = nil, end_range = nil) ⇒ Object



40
41
42
# File 'app/services/query/opportunity_query.rb', line 40

def opportunities_won_value_between(start_range = nil, end_range = nil)
  opportunity_records_between(start_range, end_range).with_value.won.sum(value)
end

#opportunity_records_between(start_range = nil, end_range = nil) ⇒ Object



72
73
74
75
76
77
# File 'app/services/query/opportunity_query.rb', line 72

def opportunity_records_between(start_range = nil, end_range = nil)
  rel = @relation
  rel = rel.where("opportunities.created_at >= ?", start_range) if start_range
  rel = rel.where("opportunities.created_at <= ?", end_range) if end_range
  rel
end