Class: Query::OpportunityQuery

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

Overview

Service object: opportunity query.

Instance Method Summary collapse

Constructor Details

#initialize(relation = nil) ⇒ OpportunityQuery

Returns a new instance of OpportunityQuery.



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

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

Instance Method Details

#average_aging_in_days_for_wonObject



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

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



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

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



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

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

#median_opportunity_valueObject



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

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

#opportunities_conversion_ratioObject



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

def opportunities_conversion_ratio
  opportunities_conversion_ratio_between(nil, nil)
end

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



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

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)
  (won / total).to_f.round(precision)
end

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



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

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



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

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

#opportunities_won_countObject



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

def opportunities_won_count
  opportunities_won_count_between(nil, nil)
end

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



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

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

#opportunities_won_valueObject



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

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

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



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

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



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

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