Class: Query::OpportunityQuery
- Inherits:
-
BaseQuery
- Object
- BaseQuery
- Query::OpportunityQuery
- Defined in:
- app/services/query/opportunity_query.rb
Overview
Service object: opportunity query.
Instance Method Summary collapse
- #average_aging_in_days_for_won ⇒ Object
- #average_aging_in_days_for_won_between(start_range = nil, end_range = nil) ⇒ Object
- #average_opportunity_value ⇒ Object
-
#initialize(relation = nil) ⇒ OpportunityQuery
constructor
A new instance of OpportunityQuery.
- #median_opportunity_value ⇒ Object
- #opportunities_conversion_ratio ⇒ Object
- #opportunities_conversion_ratio_between(start_range = nil, end_range = nil, precision = 2) ⇒ Object
- #opportunities_count_between(start_range = nil, end_range = nil) ⇒ Object
- #opportunities_lost_count_between(start_range = nil, end_range = nil) ⇒ Object
- #opportunities_won_count ⇒ Object
- #opportunities_won_count_between(start_range = nil, end_range = nil) ⇒ Object
- #opportunities_won_value ⇒ Object
- #opportunities_won_value_between(start_range = nil, end_range = nil) ⇒ Object
- #opportunity_records_between(start_range = nil, end_range = nil) ⇒ Object
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_won ⇒ Object
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_value ⇒ Object
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_value ⇒ Object
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_ratio ⇒ Object
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_count ⇒ Object
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_value ⇒ Object
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 |