class Sellsy::Opportunity

Attributes

amount[RW]
comments[RW]
contacts[RW]
entity_id[RW]
entity_type[RW]
funnel_name[RW]
id[RW]
name[RW]
reference[RW]
source_name[RW]
step_name[RW]

Public Class Methods

all() click to toggle source
# File lib/sellsy/opportunity.rb, line 134
def self.all
  command = {
      'method' => 'Opportunities.getList',
      'params' => {}
  }

  response = MultiJson.load(Sellsy::Api.request command)

  opportunities = []
  if response['response']
    response['response']['result'].each do |key, value|
      opportunity = Opportunity.new
      opportunity.id = key
      opportunity.status = value['status']
      opportunity.name = value['name']
      opportunity.ident = value['ident']
      opportunity.signed = value['signed']
      opportunity.linkedid = value['linkedid']
      opportunities << opportunity
    end
  end

  opportunities
end
find(id) click to toggle source
# File lib/sellsy/opportunity.rb, line 88
def self.find(id)
  command = {
      'method' => 'Opportunities.getOne',
      'params' => {
          'id' => id
      }
  }

  response = MultiJson.load(Sellsy::Api.request command)

  opportunity = Opportunity.new

  if response['response']
    value = response['response']
    opportunity.id = value['id']
    opportunity.name = value['name']
  end

  return opportunity
end

Public Instance Methods

api_params() click to toggle source
# File lib/sellsy/opportunity.rb, line 62
def api_params
  funnel = get_funnel
  step = get_step(funnel['id'])
  source = get_source
  if funnel && step && source
    {
        'id' => @id,
        'opportunity' => {
            'linkedtype' => @entity_type,
            'linkedid' => @entity_id,
            'ident' => @reference,
            'sourceid' => source['id'],
            'name' => @name,
            'potential' => @amount,
            'funnelid' => funnel['id'],
            'dueDate' => (Date.today + 1.month).to_datetime.to_i,
            'stepid' => step['id'],
            'contacts' => @contacts.blank? ? nil : @contacts.join(','),
            'brief' => @comments
        }
    }
  else
    raise Exception.new("Could not find funnel, step, or source with names #{funnel_name} - #{step_name} - #{source_name}")
  end
end
create() click to toggle source
# File lib/sellsy/opportunity.rb, line 8
def create
  command = {
      'method' => 'Opportunities.create',
      'params' => api_params
  }

  response = MultiJson.load(Sellsy::Api.request command)
  @id = response['response']
  response['status'] == 'success'
end
get_funnel() click to toggle source
# File lib/sellsy/opportunity.rb, line 29
def get_funnel
  command = {'method' => 'Opportunities.getFunnels', 'params' => {}}
  response = MultiJson.load(Sellsy::Api.request command)

  funnel = nil
  unless response['response'].blank? || funnel_name.blank?
    funnel = response['response'].values.find {|f| f['name'].parameterize == funnel_name.parameterize}
  end
  funnel
end
get_source() click to toggle source
# File lib/sellsy/opportunity.rb, line 51
def get_source
  command = {'method' => 'Opportunities.getSources', 'params' => {}}
  response = MultiJson.load(Sellsy::Api.request command)

  source = nil
  unless response['response'].blank? || source_name.blank?
    source = response['response'].values.find {|s| s['label'].parameterize == source_name.parameterize}
  end
  source
end
get_step(funnel_id) click to toggle source
# File lib/sellsy/opportunity.rb, line 40
def get_step(funnel_id)
  command = {'method' => 'Opportunities.getStepsForFunnel', 'params' => {'funnelid' => funnel_id}}
  response = MultiJson.load(Sellsy::Api.request command)

  step = [nil]
  unless response['response'].blank? || step_name.blank?
    step = response['response'].find {|s| s['label'].parameterize == step_name.parameterize}
  end
  step
end
update() click to toggle source
# File lib/sellsy/opportunity.rb, line 19
def update
  command = {
      'method' => 'Opportunities.update',
      'params' => api_params
  }

  response = MultiJson.load(Sellsy::Api.request command)
  response['status'] == 'success'
end