Class: OpportunityObserver

Inherits:
ActiveRecord::Observer
  • Object
show all
Defined in:
app/models/observers/opportunity_observer.rb

Overview

Copyright © 2008-2013 Michael Dvorkin and contributors.

Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php


Constant Summary collapse

@@opportunities =
{}

Instance Method Summary collapse

Instance Method Details

#after_create(item) ⇒ Object



13
14
15
16
17
# File 'app/models/observers/opportunity_observer.rb', line 13

def after_create(item)
  if item.campaign && item.stage == "won"
    update_campaign_revenue(item.campaign, item.amount.to_f - item.discount.to_f)
  end
end

#after_update(item) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/observers/opportunity_observer.rb', line 23

def after_update(item)
  original = @@opportunities.delete(item.id)
  if original
    if original.stage != "won" && item.stage == "won" # :other to :won -- add to total campaign revenue.
      update_campaign_revenue(item.campaign, item.amount.to_f - item.discount.to_f)
      item.update_attribute(:probability, 100) # Set probability to 100% if won
      return log_activity(item, :won)
    elsif original.stage == "won" && item.stage != "won" # :won to :other -- substract from total campaign revenue.
      update_campaign_revenue(original.campaign, -(original.amount.to_f - original.discount.to_f))
    elsif original.stage != "lost" && item.stage == "lost"
      item.update_attribute(:probability, 0) # Set probability to 0% if lost
    end
  end
end

#before_update(item) ⇒ Object



19
20
21
# File 'app/models/observers/opportunity_observer.rb', line 19

def before_update(item)
  @@opportunities[item.id] = Opportunity.find(item.id).freeze
end