Class: Opportunity

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/entities/opportunity.rb

Overview

Schema Information

Table name: opportunities

id              :integer         not null, primary key
user_id         :integer
campaign_id     :integer
assigned_to     :integer
name            :string(64)      default(""), not null
access          :string(8)       default("Public")
source          :string(32)
stage           :string(32)
probability     :integer
amount          :decimal(12, 2)
discount        :decimal(12, 2)
closes_on       :date
deleted_at      :datetime
created_at      :datetime
updated_at      :datetime
background_info :string(255)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_for(model, account, params, users) ⇒ Object

Class methods.




151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/entities/opportunity.rb', line 151

def self.create_for(model, , params, users)
  opportunity = Opportunity.new(params)

  # Save the opportunity if its name was specified and account has no errors.
  if opportunity.name? && .errors.empty?
    # Note: opportunity.account = account doesn't seem to work here.
    opportunity. = AccountOpportunity.new(:account => , :opportunity => opportunity) unless .id.blank?
    if opportunity.access != "Lead" || model.nil?
      opportunity.save_with_permissions(users)
    else
      opportunity.save_with_model_permissions(model)
    end
  end
  opportunity
end

.outlineObject



96
# File 'app/models/entities/opportunity.rb', line 96

def self.outline  ; "long" ; end

.per_pageObject

Default values provided through class methods.




95
# File 'app/models/entities/opportunity.rb', line 95

def self.per_page ; 20     ; end

Instance Method Details

#attach!(attachment) ⇒ Object

Attach given attachment to the opportunity if it hasn’t been attached already.




133
134
135
136
137
# File 'app/models/entities/opportunity.rb', line 133

def attach!(attachment)
  unless self.send("#{attachment.class.name.downcase}_ids").include?(attachment.id)
    self.send(attachment.class.name.tableize) << attachment
  end
end

#discard!(attachment) ⇒ Object

Discard given attachment from the opportunity.




141
142
143
144
145
146
147
# File 'app/models/entities/opportunity.rb', line 141

def discard!(attachment)
  if attachment.is_a?(Task)
    attachment.update_attribute(:asset, nil)
  else # Contacts
    self.send(attachment.class.name.tableize).delete(attachment)
  end
end

#save_with_account_and_permissions(params) ⇒ Object

Backend handler for [Create New Opportunity] form (see opportunity/create).




105
106
107
108
109
110
111
112
113
114
# File 'app/models/entities/opportunity.rb', line 105

def (params)
  # Quick sanitization, makes sure Account will not search for blank id.
  params[:account].delete(:id) if params[:account][:id].blank?
   = Account.create_or_select_for(self, params[:account], params[:users])
  self. = AccountOpportunity.new(:account => , :opportunity => self) unless .id.blank?
  self. = 
  self.contacts << Contact.find(params[:contact]) unless params[:contact].blank?
  self.campaign = Campaign.find(params[:campaign]) unless params[:campaign].blank?
  self.save_with_permissions(params[:users])
end

#update_with_account_and_permissions(params) ⇒ Object

Backend handler for [Update Opportunity] form (see opportunity/update).




118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/entities/opportunity.rb', line 118

def (params)
  if params[:account] && (params[:account][:id] == "" || params[:account][:name] == "")
    self. = nil # Opportunity is not associated with the account anymore.
  elsif params[:account]
     = Account.create_or_select_for(self, params[:account], params[:users])
    if self. !=  and .id.present?
      self. = AccountOpportunity.new(:account => , :opportunity => self)
    end
  end
  self.reload
  self.update_with_permissions(params[:opportunity], params[:users])
end

#weighted_amountObject




99
100
101
# File 'app/models/entities/opportunity.rb', line 99

def weighted_amount
  ((amount || 0) - (discount || 0)) * (probability || 0) / 100.0
end