Class: Jbr::Invoice

Inherits:
Resource show all
Defined in:
lib/jbr/invoice.rb

Overview

A bill a Jobber user issued for finished work.

Direct Known Subclasses

Mock::Invoice

Constant Summary collapse

FIND =

The query that reads one invoice, its total, its date and the job it bills.

<<~GRAPHQL
  query($id: EncodedId!) {
    invoice(id: $id) { id total invoiceStatus issuedDate
      jobs { nodes { id completedAt } } }
  }
GRAPHQL

Instance Attribute Summary collapse

Attributes inherited from Resource

#id

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Jbr::Resource

Instance Attribute Details

#job_idString? (readonly)

Returns the ID of the job billed, and the amount as Jobber writes it.

Returns:

  • (String, nil)

    the ID of the job billed, and the amount as Jobber writes it.



13
14
15
# File 'lib/jbr/invoice.rb', line 13

def job_id
  @job_id
end

#totalString? (readonly)

Returns the ID of the job billed, and the amount as Jobber writes it.

Returns:

  • (String, nil)

    the ID of the job billed, and the amount as Jobber writes it.



13
14
15
# File 'lib/jbr/invoice.rb', line 13

def total
  @total
end

Instance Method Details

#completed_atTime

Returns the job completed time.

Returns:

  • (Time)

    the job completed time



39
40
41
# File 'lib/jbr/invoice.rb', line 39

def completed_at
  Time.iso8601(@completed_at) if @completed_at
end

#find(id) ⇒ Invoice?

Returns itself, or nil when the invoice is missing or still a draft.

Parameters:

  • id (String)

    the Jobber ID of the invoice.

Returns:

  • (Invoice, nil)

    itself, or nil when the invoice is missing or still a draft.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jbr/invoice.rb', line 17

def find(id)
  output = @oauth.query FIND, variables: { id: id  }
  return unless invoice = output['invoice']
  return if invoice['invoiceStatus'].eql? 'draft'

  @id = invoice['id']
  @total = invoice['total']
  @issued_at = invoice['issuedDate']

  job = invoice.dig('jobs', 'nodes', 0) || {}
  @job_id = job['id']
  @completed_at = job['completedAt']

  self
end

#issued_atDate

Returns the invoice issued time.

Returns:

  • (Date)

    the invoice issued time



34
35
36
# File 'lib/jbr/invoice.rb', line 34

def issued_at
  Time.iso8601(@issued_at) if @issued_at
end