Module: BrazilianUtils::CSTUtils

Defined in:
lib/brazilian-utils/cst-utils.rb

Overview

Utilities for the CST (Código de Situação Tributária) tables: ICMS (Tabela A "origem" + Tabela B "tributação"), IPI and PIS/COFINS (which share a table).

Constant Summary collapse

DATA_FILE =
File.join(File.dirname(__FILE__), 'data', 'cst.json')

Class Method Summary collapse

Class Method Details

.is_valid(value, options = {}) ⇒ Boolean Also known as: valid?

Checks whether a CST code is valid for a tax.

Parameters:

  • value (String, Integer) —

    A string or a non-negative integer. The 3-digit ICMS form (origin + Tabela B code) may have a single separator right after the origin digit (e.g. "1-10"). A single digit is padded to the 3-digit ICMS form; a 2-digit string is checked as a Tabela B / IPI / PIS-COFINS code.

  • options (Hash) (defaults to: {}) —

    :tax picks the table: "icms", "ipi", "pis" or "cofins" (PIS and COFINS share a table). Omitted or unknown, every table is accepted.

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/brazilian-utils/cst-utils.rb', line 50

def self.is_valid(value, options = {})
  return false if value.nil?

  tax = (options[:tax] || options['tax']).to_s.downcase
  tax = nil if tax.empty? || !%w[icms ipi pis cofins].include?(tax)

  raw = value.to_s.strip
  return false if raw.empty?

  three_digit, two_digit = normalize(raw)

  icms_ok = !three_digit.nil? && icms_origins.include?(three_digit[0]) && icms_csts.include?(three_digit[1, 2])

  case tax
  when 'icms'
    icms_ok
  when 'ipi'
    !two_digit.nil? && ipi_csts.include?(two_digit)
  when 'pis', 'cofins'
    !two_digit.nil? && pis_cofins_csts.include?(two_digit)
  else
    icms_ok ||
      (!two_digit.nil? && (icms_csts.include?(two_digit) || ipi_csts.include?(two_digit) || pis_cofins_csts.include?(two_digit)))
  end
end