app/models/campaign.rb in e9_crm-0.1.24 vs app/models/campaign.rb in e9_crm-0.1.25
- old
+ new
@@ -36,11 +36,11 @@
scope :active, lambda {|val=true| where(:active => val) }
scope :inactive, lambda { active(false) }
scope :of_group, lambda {|val| joins(:campaign_group).where(:campaign_group_id => val.to_param) }
scope :typed, lambda { where(arel_table[:type].not_eq('NoCampaign')) }
scope :ordered, lambda { order(arel_table[:name].asc) }
-
+
scope :reports, lambda {|*args|
options = args.extract_options!
selects = <<-SQL.gsub(/\s+/, ' ')
campaigns.*,
@@ -48,23 +48,27 @@
SUM(IF(deals.status != 'lead',1,0)) deal_count,
COUNT(deals.id) lead_count,
SUM(IF(deals.status='won',1,0)) won_deal_count,
+ SUM(IF(deals.status IN('won','lost'),1,0)) closed_deal_count,
SUM(IF(deals.status='won',deals.value,0)) total_value,
AVG(IF(deals.status='won',deals.value,NULL)) average_value,
SUM(costs.total) total_cost,
SUM(costs.total) /
SUM(IF(deals.status='won',1,0)) average_cost,
rv.count repeat_visits,
nv.count new_visits,
- FLOOR(AVG(
- DATEDIFF(
- deals.closed_at,
- deals.created_at))) average_elapsed
+ SUM(DATEDIFF(
+ deals.closed_at,
+ deals.created_at)) total_elapsed,
+
+ AVG(DATEDIFF(
+ deals.closed_at,
+ deals.created_at)) average_elapsed
SQL
select(selects)
.joins(
'LEFT OUTER JOIN deals ' +