Class: Nfe::Resources::TaxCalculation

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

Overview

Tax calculation engine for the :cte family (host api.nfse.io). Runs the tax rules engine for a tenant and returns a per-item tax breakdown.

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)


22
# File 'lib/nfe/resources/tax_calculation.rb', line 22

def api_family = :cte

#api_versionString

The path already carries its own segments, so no version prefix is added.

Returns:

  • (String)


25
# File 'lib/nfe/resources/tax_calculation.rb', line 25

def api_version = ""

#calculate(tenant_id, request) ⇒ Nfe::Generated::CalculoImpostosV1::CalculateResponse?

request accepts a plain Hash; build it from Nfe::Generated::CalculoImpostosV1::CalculateRequest#to_h for type-safety.

Parameters:

  • tenant_id (String)
  • request (Hash[untyped, untyped])

Returns:



41
42
43
44
45
46
47
# File 'lib/nfe/resources/tax_calculation.rb', line 41

def calculate(tenant_id, request)
  validate_calculate!(tenant_id, request)
  path = "/tax-rules/#{CGI.escape(tenant_id.to_s.strip)}/engine/calculate"
  response = post(path, body: JSON.generate(request),
                        headers: { "Content-Type" => "application/json" })
  hydrate(Nfe::Generated::CalculoImpostosV1::CalculateResponse, parse_json(response.body))
end

#items?(request) ⇒ Boolean

Parameters:

  • request (Hash[untyped, untyped])

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/nfe/resources/tax_calculation.rb', line 65

def items?(request)
  items = request[:items] || request["items"]
  items.is_a?(Array) && !items.empty?
end

#operation_type?(request) ⇒ Boolean

Parameters:

  • request (Hash[untyped, untyped])

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/nfe/resources/tax_calculation.rb', line 60

def operation_type?(request)
  !request[:operation_type].nil? || !request["operation_type"].nil? ||
    !request[:operationType].nil? || !request["operationType"].nil?
end

#validate_calculate!(tenant_id, request) ⇒ void

This method returns an undefined value.

Fail fast (no HTTP) when the tenant or request shape is invalid.

Parameters:

  • tenant_id (String)
  • request (Object)

Raises:



52
53
54
55
56
57
58
# File 'lib/nfe/resources/tax_calculation.rb', line 52

def validate_calculate!(tenant_id, request)
  Nfe::IdValidator.presence!(tenant_id, "tenant_id")
  return if request.is_a?(Hash) && operation_type?(request) && items?(request)

  raise Nfe::InvalidRequestError,
        "request deve conter operation_type (ou operationType) e items (Array não vazio)"
end