Class: Quickeebooks::Online::Service::Invoice

Inherits:
ServiceBase
  • Object
show all
Defined in:
lib/quickeebooks/online/service/invoice.rb

Constant Summary

Constants inherited from ServiceBase

ServiceBase::QB_BASE_URI, ServiceBase::XML_NS

Instance Attribute Summary

Attributes inherited from ServiceBase

#base_uri, #oauth, #realm_id

Instance Method Summary collapse

Methods inherited from ServiceBase

#access_token=, #base_url=, #initialize, #login_name, #url_for_base, #url_for_resource

Constructor Details

This class inherits a constructor from Quickeebooks::Online::Service::ServiceBase

Instance Method Details

#create(invoice) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/quickeebooks/online/service/invoice.rb', line 12

def create(invoice)
  raise InvalidModelException unless invoice.valid?
  xml = invoice.to_xml_ns
  response = do_http_post(url_for_resource(Quickeebooks::Online::Model::Invoice.resource_for_singular), valid_xml_document(xml))
  if response.code.to_i == 200
    Quickeebooks::Online::Model::Invoice.from_xml(response.body)
  else
    nil
  end
end

#delete(invoice) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/quickeebooks/online/service/invoice.rb', line 68

def delete(invoice)
  raise InvalidModelException.new("Missing required parameters for delete") unless invoice.valid_for_deletion?
  xml = valid_xml_document(invoice.to_xml_ns(:fields => ['Id', 'SyncToken']))
  url = "#{url_for_resource(Quickeebooks::Online::Model::Invoice::REST_RESOURCE)}/#{invoice.id}"
  response = do_http_post(url, xml, {:methodx => "delete"})
  response.code.to_i == 200
end

#fetch_by_id(invoice_id) ⇒ Object

Fetch an invoice by its ID Returns: Invoice object



25
26
27
28
# File 'lib/quickeebooks/online/service/invoice.rb', line 25

def fetch_by_id(invoice_id)
  response = do_http_get("#{url_for_resource(Quickeebooks::Online::Model::Invoice::REST_RESOURCE)}/#{invoice_id}")
  Quickeebooks::Online::Model::Invoice.from_xml(response.body)
end

#invoice_as_pdf(invoice_id, destination_file_name) ⇒ Object

Returns the absolute path to the PDF on disk Its left to the caller to unlink the file at some later date Returns: String : absolute path to file on disk or nil if couldnt fetch PDF



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/quickeebooks/online/service/invoice.rb', line 56

def invoice_as_pdf(invoice_id, destination_file_name)
  response = do_http_get("#{url_for_resource("invoice-document")}/#{invoice_id}", {}, {'Content-Type' => 'application/pdf'})
  if response && response.code.to_i == 200
    File.open(destination_file_name, "wb") do |file|
      file.write(response.body)
    end
    destination_file_name
  else
    nil
  end
end

#list(filters = [], page = 1, per_page = 20, sort = nil, options = {}) ⇒ Object

Fetch a Collection of Invoice objects Arguments: filters: Array of Filter objects to apply page: Fixnum Starting page per_page: Fixnum How many results to fetch per page sort: Sort object options: Hash extra arguments



49
50
51
# File 'lib/quickeebooks/online/service/invoice.rb', line 49

def list(filters = [], page = 1, per_page = 20, sort = nil, options = {})
  fetch_collection(Quickeebooks::Online::Model::Invoice, filters, page, per_page, sort, options)
end

#update(invoice) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quickeebooks/online/service/invoice.rb', line 30

def update(invoice)
  raise InvalidModelException.new("Missing required parameters for update") unless invoice.valid_for_update?
  url = "#{url_for_resource(Quickeebooks::Online::Model::Invoice.resource_for_singular)}/#{invoice.id.value}"
  xml = invoice.to_xml_ns
  response = do_http_post(url, valid_xml_document(xml))
  if response.code.to_i == 200
    Quickeebooks::Online::Model::Invoice.from_xml(response.body)
  else
    nil
  end
end