Class: Wfirma::Invoices

Inherits:
Object
  • Object
show all
Defined in:
lib/wfirma/invoices.rb

Overview

Invoices resource. Maps a simple attrs hash to wFirma's invoices/add payload format and wraps the response in a Result.

Constant Summary collapse

KEY_MAP =
{
  payment_method: "paymentmethod",
  payment_date: "paymentdate"
}.freeze
PDF_OPTIONS =

Defaults for the download/send print options; see #pdf and #send_email.

{
  "page" => "invoice", "address" => 0, "leaflet" => 0, "duplicate" => 0
}.freeze
SEND_OPTIONS =
{
  "page" => "invoice", "leaflet" => 0, "duplicate" => 0
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Invoices

Returns a new instance of Invoices.



18
19
20
# File 'lib/wfirma/invoices.rb', line 18

def initialize(client)
  @client = client
end

Instance Method Details

#create(attrs, draft: false) ⇒ Object

attrs: {contractor: {...}, items: [{...}], payment_method: "transfer", ...} draft: true creates a "normal_draft" document (no book number, not sent to KSeF) - the safe way to test against the real API.



25
26
27
28
# File 'lib/wfirma/invoices.rb', line 25

def create(attrs, draft: false)
  payload = { "invoices" => { "invoice" => build_invoice(attrs, draft: draft) } }
  Result.new(@client.call("invoices", "add", payload))
end

#pdf(invoice_id, **options) ⇒ Object

PDF printout of an invoice, returned as raw bytes (binary String). Raises Wfirma::ApiError when wFirma answers with an error instead. Options (all optional, PDF_OPTIONS otherwise):

page:      "invoice" (original), "invoicecopy" (copy), "all" (both)
address:   1 prints the buyer's address on the back of the original
leaflet:   1 adds a transfer form (transfer payments in PLN only)
duplicate: 1 marks the printout as a duplicate


38
39
40
41
# File 'lib/wfirma/invoices.rb', line 38

def pdf(invoice_id, **options)
  payload = parameters_payload(PDF_OPTIONS, options)
  @client.download("invoices", "download", payload, {}, invoice_id)
end

#send_email(invoice_id, **options) ⇒ Object

Asks wFirma to email the invoice PDF to the customer. Options:

email:            recipient; omitted falls back to the contractor record
subject: / body:  omitted falls back to wFirma's email template
page / leaflet / duplicate: as in #pdf


48
49
50
51
# File 'lib/wfirma/invoices.rb', line 48

def send_email(invoice_id, **options)
  payload = parameters_payload(SEND_OPTIONS, options)
  Result.new(@client.call("invoices", "send", payload, {}, invoice_id))
end