sat_rfc
Ruby gem to generate Mexican RFC tax identifiers for natural persons (personas físicas) and legal entities (personas morales), following the SAT algorithm documented by the IFAI (Instituto Federal de Acceso a la Información). Natural person support is inspired by rfc-facil and rfc_facil.
Installation
Add this line to your application's Gemfile:
gem "sat_rfc"
Or install from the repository:
gem "sat_rfc", git: "https://github.com/elosnaya/rfc.git"
Local development install:
bundle exec rake install
Usage
require "sat_rfc"
rfc = Rfc::Generator.new.for_natural_person(
name: "Juan Carlos",
first_last_name: "Perez",
second_last_name: "Garcia",
date_of_birth: "15/03/1990"
)
puts rfc # => "PEGJ900315PE9"
To inspect individual RFC parts, use the calculators directly:
name = "Juan Carlos"
first_last_name = "Perez"
second_last_name = "Garcia"
ten_digits = Rfc::NaturalTenDigitsCodeCalculator.new(
name: name,
first_last_name: first_last_name,
second_last_name: second_last_name,
day: 15, month: 3, year: 1990
).calculate
# => "PEGJ900315"
homoclave = Rfc::HomoclaveCalculator.new(
name: name,
first_last_name: first_last_name,
second_last_name: second_last_name
).calculate
# => "PE"
verification_digit = Rfc::VerificationDigitCalculator.new("#{ten_digits}#{homoclave}").calculate
# => "9"
You can also pass the birth date as separate values:
Rfc::Generator.new.for_natural_person(
name: "Juan Carlos",
first_last_name: "Perez",
second_last_name: "Garcia",
day: 15,
month: 3,
year: 1990
)
date_of_birth accepts DD/MM/YYYY format or any string parseable by Ruby's Date.
Legal entity (persona moral)
rfc = Rfc::Generator.new.for_legal_entity(
legal_name: "Mu Networks S.A.P.I. de C.V.",
constitution_date: "10/07/2014"
)
puts rfc # => "MNP1407108U7"
To inspect individual RFC parts:
legal_name = "Mu Networks S.A.P.I. de C.V."
ten_digits = Rfc::LegalEntityTenDigitsCodeCalculator.new(
legal_name: legal_name,
day: 10, month: 7, year: 2014
).calculate
# => "MNP140710"
homoclave = Rfc::HomoclaveCalculator.new(legal_name: legal_name).calculate
# => "8U"
verification_digit = Rfc::VerificationDigitCalculator.new("#{ten_digits}#{homoclave}").calculate
# => "7"
constitution_date accepts DD/MM/YYYY format or any string parseable by Ruby's Date.
Errors
begin
Rfc::Generator.new.for_natural_person(name: "Ana", first_last_name: "Lopez", second_last_name: "Garcia")
rescue Rfc::Generator::InvalidDateError => e
puts e. # => "Date information missing"
end
Development
bin/setup # install dependencies
bundle exec rspec # run tests
bin/console # interactive console
Example in the console:
Rfc::Generator.new.for_natural_person(
name: "Juan Carlos",
first_last_name: "Perez",
second_last_name: "Garcia",
date_of_birth: "15/03/1990"
)
Algorithm reference
This gem implements the RFC generation algorithm with homoclave for natural persons and legal entities, based on official documentation obtained through the IFAI.
- INFOMEX folio:
0610100135506 - Document: Algoritmo para generar el RFC con homoclave para personas físicas y morales (PDF)
Acknowledgments
Natural person (persona física)
Natural person support is inspired by rfc-facil by josketres and rfc_facil by acrogenesis, following the SAT algorithm documented by the IFAI.
Contributing
Bug reports and pull requests are welcome on GitHub at elosnaya/rfc.
License
The gem is available as open source under the terms of the MIT License.