Class: BadBill::Invoice

Inherits:
BaseResource show all
Defined in:
lib/badbill/invoice.rb

Overview

The resource handles all invoices.

See www.billomat.com/en/api/invoices

Instance Attribute Summary

Attributes inherited from BaseResource

#data, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseResource

all, create, #delete, #error, find, #initialize, #save

Methods included from Resource

#call, #delete, #get, #post, #put

Methods included from ForwardMethods

#method_missing, #respond_to?

Constructor Details

This class inherits a constructor from BadBill::BaseResource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BadBill::ForwardMethods

Class Method Details

.group_by(*type) ⇒ Array<Invoices>

Get aggregated list of invoices.

BE CAREFUL: The returned objects are not really invoices. See www.billomat.com/en/api/invoices/ for the format.

Parameters:

  • type (String, Symbol, #to_s)

    One of [‘client’, ‘status’, ‘day’, ‘week’, ‘month’, ‘year’]. Specify multiple types for finer aggregation.

Returns:

  • (Array<Invoices>)

    All found resources.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/badbill/invoice.rb', line 21

def self.group_by *type
  if type.respond_to?(:join)
    type = type.join(',')
  end

  all = get resource_name, group_by: type

  return all if all.error

  data = all.__send__('invoice-groups').__send__('invoice-group')

  if data.kind_of? Array
    data
  else
    [data]
  end
end

Instance Method Details

#cancelBoolean

Cancel an invoice.

Returns:

  • (Boolean)

    Wether or not the call was successfull.



71
72
73
74
# File 'lib/badbill/invoice.rb', line 71

def cancel
  resp = put resource_name, "#{id}/cancel"
  !resp
end

#complete(template_id = nil) ⇒ Boolean

Closes a statement in the draft status (DRAFT). Here, the status of open (OPEN) or overdue (OVERDUE) is set and a PDF is generated and stored in the file system.

Parameters:

  • template_id (String, Integer) (defaults to: nil)

    ID of the Template used to create pdf, If not set, default template is used.

Returns:

  • (Boolean)

    Wether or not the call was successfull.



60
61
62
63
64
65
66
# File 'lib/badbill/invoice.rb', line 60

def complete template_id=nil
  data = { complete: {} }
  data[:complete] = { template_id: template_id } if template_id
  resp = put resource_name, "#{id}/complete", data

  !resp
end

#email(to, from = nil, subject = nil, body = nil, more = {}) ⇒ Boolean

Sends an invoice by email.

from, subject or body can be replaced by more.

Parameters:

  • to (String)

    Recipient of the email. cc and bcc can be set in the ‘more` Hash.

  • from (String) (defaults to: nil)

    Sender email address.

  • subject (String) (defaults to: nil)

    Subject for the email.

  • body (String) (defaults to: nil)

    Body for the email.

  • more (Hash) (defaults to: {})

    Hash-like object including more options. See online documentation for all allowed parameters.

Returns:

  • (Boolean)

    Wether or not the call was successfull.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/badbill/invoice.rb', line 89

def email to, from=nil, subject=nil, body=nil, more={}
  data = { recipients: {} }

  if more.empty? && from.kind_of?(Hash)
    more = from
    from = nil
  end

  if more.empty? && subject.kind_of?(Hash)
    more = subject
    subject = nil
  end

  if more.empty? && body.kind_of?(Hash)
    more = body
    body = nil
  end

  data[:from]    = from    if from
  data[:subject] = subject if subject
  data[:body]    = body    if body

  data.merge! more
  data[:recipients][:to] = to

  resp = post resource_name, "#{id}/email", email: data
  !resp
end

#pdfHashie::Mash

Get the PDF invoice.

Returns:



45
46
47
48
49
50
# File 'lib/badbill/invoice.rb', line 45

def pdf
  resp = get resource_name, "#{id}/pdf"
  ret = resp.pdf
  ret.id = ret.id.to_i
  ret
end

#upload_signature(file) ⇒ Boolean

Uploads a digital signature for a given invoice.

The status of the invoice may not be DRAFT.

Parameters:

  • file (String, #read)

    file name or a object responding to #read. This will be base64-encoded before send.

Returns:

  • (Boolean)

    false if status is DRAFT or another error occured. true if everything was successfull.



126
127
128
129
130
131
132
133
134
# File 'lib/badbill/invoice.rb', line 126

def upload_signature file
  return false if data.status == 'DRAFT'

  file = File.open(file, 'r') unless file.respond_to?(:read)

  base64 = Base64.encode64 file.read
  put resource_name, "#{id}/upload-signature", { signature: { base64file: base64 } }
  true
end