Class: Opportunity
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Opportunity
- 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
-
.create_for(model, account, params) ⇒ Object
Class methods.
- .default_stage ⇒ Object
-
.per_page ⇒ Object
Default values provided through class methods.
Instance Method Summary collapse
-
#attach!(attachment) ⇒ Object
Attach given attachment to the opportunity if it hasn’t been attached already.
-
#discard!(attachment) ⇒ Object
Discard given attachment from the opportunity.
-
#save_with_account_and_permissions(params) ⇒ Object
Backend handler for [Create New Opportunity] form (see opportunity/create).
-
#update_with_account_and_permissions(params) ⇒ Object
Backend handler for [Update Opportunity] form (see opportunity/update).
-
#weighted_amount ⇒ Object
—————————————————————————-.
Class Method Details
.create_for(model, account, params) ⇒ Object
Class methods.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'app/models/entities/opportunity.rb', line 154 def self.create_for(model, account, params) opportunity = Opportunity.new(params) # Save the opportunity if its name was specified and account has no errors. if opportunity.name? && account.errors.empty? # Note: opportunity.account = account doesn't seem to work here. opportunity.account_opportunity = AccountOpportunity.new(account: account, opportunity: opportunity) unless account.id.blank? if opportunity.access != "Lead" || model.nil? opportunity.save else opportunity.(model) end end opportunity end |
.default_stage ⇒ Object
97 98 99 |
# File 'app/models/entities/opportunity.rb', line 97 def self.default_stage Setting[:opportunity_default_stage].try(:to_s) || 'prospecting' end |
.per_page ⇒ Object
Default values provided through class methods.
93 94 95 |
# File 'app/models/entities/opportunity.rb', line 93 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.
136 137 138 139 140 |
# File 'app/models/entities/opportunity.rb', line 136 def attach!() unless send("#{.class.name.downcase}_ids").include?(.id) send(.class.name.tableize) << end end |
#discard!(attachment) ⇒ Object
Discard given attachment from the opportunity.
144 145 146 147 148 149 150 |
# File 'app/models/entities/opportunity.rb', line 144 def discard!() if .is_a?(Task) .update_attribute(:asset, nil) else # Contacts send(.class.name.tableize).delete() end end |
#save_with_account_and_permissions(params) ⇒ Object
Backend handler for [Create New Opportunity] form (see opportunity/create).
108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/entities/opportunity.rb', line 108 def (params) # Quick sanitization, makes sure Account will not search for blank id. params[:account].delete(:id) if params[:account][:id].blank? account = Account.create_or_select_for(self, params[:account]) self.account_opportunity = AccountOpportunity.new(account: account, opportunity: self) unless account.id.blank? self.account = account 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).
122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/entities/opportunity.rb', line 122 def (params) if params[:account] && (params[:account][:id] == "" || params[:account][:name] == "") self.account = nil # Opportunity is not associated with the account anymore. elsif params[:account] self.account = 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_amount ⇒ Object
102 103 104 |
# File 'app/models/entities/opportunity.rb', line 102 def weighted_amount ((amount || 0) - (discount || 0)) * (probability || 0) / 100.0 end |