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.




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

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



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

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

.per_pageObject

Default values provided through class methods.




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

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.




137
138
139
140
141
# File 'app/models/entities/opportunity.rb', line 137

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

#discard!(attachment) ⇒ Object

Discard given attachment from the opportunity.




145
146
147
148
149
150
151
# File 'app/models/entities/opportunity.rb', line 145

def discard!(attachment)
  if attachment.is_a?(Task)
    attachment.update_attribute(:asset, nil)
  else # Contacts
    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).




109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/entities/opportunity.rb', line 109

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




123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/entities/opportunity.rb', line 123

def (params)
  if params[:account] && (params[:account][:id] == "" || params[:account][:name] == "")
    self. = nil # Opportunity is not associated with the account anymore.
  elsif params[:account]
    self. = Account.create_or_select_for(self, params[:account])
  end
  # 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]
  save
end

#weighted_amountObject




103
104
105
# File 'app/models/entities/opportunity.rb', line 103

def weighted_amount
  (amount.to_f - discount.to_f) * probability.to_i / 100.0
end