Class: Sellsy::Opportunity

Inherits:
Object
  • Object
show all
Defined in:
lib/sellsy/opportunity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def amount
  @amount
end

#commentsObject

Returns the value of attribute comments.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def comments
  @comments
end

#contactsObject

Returns the value of attribute contacts.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def contacts
  @contacts
end

#entity_idObject

Returns the value of attribute entity_id.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def entity_id
  @entity_id
end

#entity_typeObject

Returns the value of attribute entity_type.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def entity_type
  @entity_type
end

#funnel_nameObject

Returns the value of attribute funnel_name.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def funnel_name
  @funnel_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def name
  @name
end

#referenceObject

Returns the value of attribute reference.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def reference
  @reference
end

#source_nameObject

Returns the value of attribute source_name.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def source_name
  @source_name
end

#step_nameObject

Returns the value of attribute step_name.



5
6
7
# File 'lib/sellsy/opportunity.rb', line 5

def step_name
  @step_name
end

Class Method Details

.allObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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

.search(params) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sellsy/opportunity.rb', line 109

def self.search(params)
  command = {
      'method' => 'Opportunities.getList',
      'params' => 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

Instance Method Details

#api_paramsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

#createObject



8
9
10
11
12
13
14
15
16
17
# 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_funnelObject



29
30
31
32
33
34
35
36
37
38
# 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_sourceObject



51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# 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

#updateObject



19
20
21
22
23
24
25
26
27
# 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