Class: Opportunity

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/entities/opportunity.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


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) ⇒ Object

Class methods.




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

def self.create_for(model, , params)
  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
    else
      opportunity.save_with_model_permissions(model)
    end
  end
  opportunity
end

.default_stageObject



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

def self.default_stage; Setting[:opportunity_default_stage].try(:to_s) || 'prospecting'; end

.per_pageObject

Default values provided through class methods.




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

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.




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

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.




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

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).




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

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])
  self. = AccountOpportunity.new(:account => , :opportunity => self) unless .id.blank?
  self. = 
  self.campaign = Campaign.find(params[:campaign]) unless params[:campaign].blank?
  result = self.save
  self.contacts << Contact.find(params[:contact]) unless params[:contact].blank?
  result
end

#update_with_account_and_permissions(params) ⇒ Object

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




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

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])
    if self. !=  and .id.present?
      self. = AccountOpportunity.new(:account => , :opportunity => self)
    end
  end
  self.reload
  # Must set access before user_ids, because user_ids= method depends on access value.
  self.access = params[:opportunity][:access] if params[:opportunity][:access]
  self.attributes = params[:opportunity]
  self.save
end

#weighted_amountObject




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

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