Class: Nfe::Resources::TransportationInvoices

Inherits:
AbstractResource show all
Defined in:
lib/nfe/resources/transportation_invoices.rb,
sig/nfe/resources/transportation_invoices.rbs

Overview

Inbound CT-e (Conhecimento de Transporte Eletrônico) resource for the :cte host family (+https://api.nfse.io+). Manages automatic CT-e fetch via SEFAZ Distribuição DFe and reads received CT-e documents/events by 44-digit access key.

This is an inbound/query resource (settings + access-key lookups), not an emission resource: there is no discriminated 202 +Pending+/+Issued+ contract here. The host already carries no version segment, so paths embed /v2 literally and #api_version is "".

Examples:

Enable auto-fetch and read a received CT-e

client.transportation_invoices.enable(company_id: "co-1")
cte = client.transportation_invoices.retrieve(
  company_id: "co-1",
  access_key: "3524...7890"
)

Instance Attribute Summary

Attributes inherited from AbstractResource

#client

Instance Method Summary collapse

Methods inherited from AbstractResource

#build_list_page, #build_multipart_body, #cursor_list_page, #delete, #dig_key, #download, #extract_invoice_id, #full_path, #get, #handle_async_response, #hydrate, #hydrate_list, #initialize, #multipart_part, #page_list_page, #parse_json, #post, #put, #unwrap, #upload_multipart

Constructor Details

This class inherits a constructor from Nfe::Resources::AbstractResource

Instance Method Details

#api_familySymbol

Returns:

  • (Symbol)


29
30
31
# File 'lib/nfe/resources/transportation_invoices.rb', line 29

def api_family
  :cte
end

#api_versionString

The :cte host carries no version segment; /v2 is embedded per path.

Returns:

  • (String)


34
35
36
# File 'lib/nfe/resources/transportation_invoices.rb', line 34

def api_version
  ""
end

#compact(hash) ⇒ Hash[untyped, untyped]

Drop nil values so optional fields are omitted from the body.

Parameters:

  • hash (Hash[untyped, untyped])

Returns:

  • (Hash[untyped, untyped])


139
140
141
# File 'lib/nfe/resources/transportation_invoices.rb', line 139

def compact(hash)
  hash.compact
end

#disable(company_id:) ⇒ Nfe::InboundSettings

Disable automatic CT-e search for a company.

Parameters:

  • company_id (String)
  • company_id: (String)

Returns:



58
59
60
61
62
# File 'lib/nfe/resources/transportation_invoices.rb', line 58

def disable(company_id:)
  id = Nfe::IdValidator.company_id(company_id)
  response = delete("/v2/companies/#{id}/inbound/transportationinvoices")
  hydrate(Nfe::InboundSettings, parse_json(response.body))
end

#download_event_xml(company_id:, access_key:, event_key:) ⇒ String

Download the XML of a CT-e event.

Parameters:

  • company_id (String)
  • access_key (String)
  • event_key (String)
  • company_id: (String)
  • access_key: (String)
  • event_key: (String)

Returns:

  • (String)

    raw XML bytes (+ASCII-8BIT+).



117
118
119
120
121
122
# File 'lib/nfe/resources/transportation_invoices.rb', line 117

def download_event_xml(company_id:, access_key:, event_key:)
  id = Nfe::IdValidator.company_id(company_id)
  key = Nfe::IdValidator.access_key(access_key)
  ev = Nfe::IdValidator.event_key(event_key)
  download("/v2/companies/#{id}/inbound/#{key}/events/#{ev}/xml", headers: xml_accept)
end

#download_xml(company_id:, access_key:) ⇒ String

Download the CT-e XML by access key.

Parameters:

  • company_id (String)
  • access_key (String)
  • company_id: (String)
  • access_key: (String)

Returns:

  • (String)

    raw XML bytes (+ASCII-8BIT+).



91
92
93
94
95
# File 'lib/nfe/resources/transportation_invoices.rb', line 91

def download_xml(company_id:, access_key:)
  id = Nfe::IdValidator.company_id(company_id)
  key = Nfe::IdValidator.access_key(access_key)
  download("/v2/companies/#{id}/inbound/#{key}/xml", headers: xml_accept)
end

#enable(company_id:, start_from_nsu: nil, start_from_date: nil) ⇒ Nfe::InboundSettings

Enable automatic CT-e search for a company via Distribuição DFe.

Parameters:

  • company_id (String)
  • start_from_nsu (Integer, String, nil) (defaults to: nil)

    optional starting NSU.

  • start_from_date (String, nil) (defaults to: nil)

    optional starting date (ISO 8601).

  • company_id: (String)
  • start_from_nsu: (Object) (defaults to: nil)
  • start_from_date: (String, nil) (defaults to: nil)

Returns:



46
47
48
49
50
51
52
# File 'lib/nfe/resources/transportation_invoices.rb', line 46

def enable(company_id:, start_from_nsu: nil, start_from_date: nil)
  id = Nfe::IdValidator.company_id(company_id)
  body = compact("startFromNsu" => start_from_nsu, "startFromDate" => start_from_date)
  response = post("/v2/companies/#{id}/inbound/transportationinvoices",
                  body: json_body(body), headers: json_headers)
  hydrate(Nfe::InboundSettings, parse_json(response.body))
end

#get_event(company_id:, access_key:, event_key:) ⇒ Nfe::InboundInvoiceMetadata

Retrieve the metadata of an event related to a received CT-e.

Parameters:

  • company_id (String)
  • access_key (String)
  • event_key (String)
  • company_id: (String)
  • access_key: (String)
  • event_key: (String)

Returns:



103
104
105
106
107
108
109
# File 'lib/nfe/resources/transportation_invoices.rb', line 103

def get_event(company_id:, access_key:, event_key:)
  id = Nfe::IdValidator.company_id(company_id)
  key = Nfe::IdValidator.access_key(access_key)
  ev = Nfe::IdValidator.event_key(event_key)
  response = get("/v2/companies/#{id}/inbound/#{key}/events/#{ev}")
  hydrate(Nfe::InboundInvoiceMetadata, parse_json(response.body))
end

#get_settings(company_id:) ⇒ Nfe::InboundSettings

Get the current automatic CT-e search settings.

Parameters:

  • company_id (String)
  • company_id: (String)

Returns:



68
69
70
71
72
# File 'lib/nfe/resources/transportation_invoices.rb', line 68

def get_settings(company_id:)
  id = Nfe::IdValidator.company_id(company_id)
  response = get("/v2/companies/#{id}/inbound/transportationinvoices")
  hydrate(Nfe::InboundSettings, parse_json(response.body))
end

#json_body(data) ⇒ String

Parameters:

  • data (Object)

Returns:

  • (String)


130
131
132
# File 'lib/nfe/resources/transportation_invoices.rb', line 130

def json_body(data)
  JSON.generate(data)
end

#json_headersHash[String, String]

Returns:

  • (Hash[String, String])


126
127
128
# File 'lib/nfe/resources/transportation_invoices.rb', line 126

def json_headers
  { "Content-Type" => "application/json" }
end

#retrieve(company_id:, access_key:) ⇒ Nfe::InboundInvoiceMetadata

Retrieve CT-e metadata by its 44-digit access key (normalized).

Parameters:

  • company_id (String)
  • access_key (String)

    44-digit key; separators are stripped.

  • company_id: (String)
  • access_key: (String)

Returns:



79
80
81
82
83
84
# File 'lib/nfe/resources/transportation_invoices.rb', line 79

def retrieve(company_id:, access_key:)
  id = Nfe::IdValidator.company_id(company_id)
  key = Nfe::IdValidator.access_key(access_key)
  response = get("/v2/companies/#{id}/inbound/#{key}")
  hydrate(Nfe::InboundInvoiceMetadata, parse_json(response.body))
end

#xml_acceptHash[String, String]

Returns:

  • (Hash[String, String])


134
135
136
# File 'lib/nfe/resources/transportation_invoices.rb', line 134

def xml_accept
  { "Accept" => "application/xml" }
end