Class: FreeAgent::CreditNotesResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/free_agent/resources/credit_notes.rb

Instance Attribute Summary

Attributes inherited from Resource

#client

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from FreeAgent::Resource

Instance Method Details

#create(contact:, dated_on:, payment_terms_in_days: 0, **params) ⇒ Object



30
31
32
33
34
35
# File 'lib/free_agent/resources/credit_notes.rb', line 30

def create(contact:, dated_on:, payment_terms_in_days: 0, **params)
  attributes = {contact: contact, dated_on: dated_on, payment_terms_in_days: payment_terms_in_days}

  response = post_request("credit_notes", body: {credit_note: attributes.merge(params)})
  CreditNote.new(response.body["credit_note"]) if response.success?
end

#delete(id:) ⇒ Object



47
48
49
50
# File 'lib/free_agent/resources/credit_notes.rb', line 47

def delete(id:)
  response = delete_request("credit_notes/#{id}")
  response.success?
end

#email(id:, to:, **params) ⇒ Object



52
53
54
55
56
# File 'lib/free_agent/resources/credit_notes.rb', line 52

def email(id:, to:, **params)
  attributes = {to: to}
  response = post_request("credit_notes/#{id}/send_email", body: {credit_note: {email: attributes.merge(params)}})
  response.success?
end

#list(**params) ⇒ Object



4
5
6
7
# File 'lib/free_agent/resources/credit_notes.rb', line 4

def list(**params)
  response = get_request("credit_notes", params: params)
  Collection.from_response(response, type: CreditNote, key: "credit_notes")
end

#list_for_contact(contact:, **params) ⇒ Object



9
10
11
12
# File 'lib/free_agent/resources/credit_notes.rb', line 9

def list_for_contact(contact:, **params)
  response = get_request("credit_notes?contact=#{contact}", params: params)
  Collection.from_response(response, type: CreditNote, key: "credit_notes")
end

#list_for_project(project:, **params) ⇒ Object



14
15
16
17
# File 'lib/free_agent/resources/credit_notes.rb', line 14

def list_for_project(project:, **params)
  response = get_request("credit_notes?project=#{project}", params: params)
  Collection.from_response(response, type: CreditNote, key: "credit_notes")
end

#mark_as_cancelled(id:) ⇒ Object



68
69
70
71
# File 'lib/free_agent/resources/credit_notes.rb', line 68

def mark_as_cancelled(id:)
  response = put_request("credit_notes/#{id}/transitions/mark_as_cancelled", body: {})
  response.success?
end

#mark_as_draft(id:) ⇒ Object



63
64
65
66
# File 'lib/free_agent/resources/credit_notes.rb', line 63

def mark_as_draft(id:)
  response = put_request("credit_notes/#{id}/transitions/mark_as_draft", body: {})
  response.success?
end

#mark_as_sent(id:) ⇒ Object



58
59
60
61
# File 'lib/free_agent/resources/credit_notes.rb', line 58

def mark_as_sent(id:)
  response = put_request("credit_notes/#{id}/transitions/mark_as_sent", body: {})
  response.success?
end

#retrieve(id:) ⇒ Object



19
20
21
22
# File 'lib/free_agent/resources/credit_notes.rb', line 19

def retrieve(id:)
  response = get_request("credit_notes/#{id}")
  CreditNote.new(response.body["credit_note"])
end

#retrieve_pdf(id:) ⇒ Object

Returns a Base64-encoded PDF



25
26
27
28
# File 'lib/free_agent/resources/credit_notes.rb', line 25

def retrieve_pdf(id:)
  response = get_request("credit_notes/#{id}/pdf")
  response.body["pdf"]["content"] if response.success?
end

#update(id:, **params) ⇒ Object

def duplicate(id:)

response = post_request("invoices/#{id}/duplicate", body: {})
Invoice.new(response.body["invoice"]) if response.success?

end



42
43
44
45
# File 'lib/free_agent/resources/credit_notes.rb', line 42

def update(id:, **params)
  response = put_request("credit_notes/#{id}", body: {credit_note: params})
  CreditNote.new(response.body["credit_note"]) if response.success?
end