Skip to content

Instantly share code, notes, and snippets.

@lobo-tuerto
Created September 30, 2008 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lobo-tuerto/13913 to your computer and use it in GitHub Desktop.
Save lobo-tuerto/13913 to your computer and use it in GitHub Desktop.
class Sujeto
include DataMapper::Resource
# Configuración de las relaciones
# Puede tener un cliente asociado
has 1, :cliente, :child_key => [:sujeto_id]
has n, :direcciones, :child_key => [:sujeto_id]
# Puede tener muchos préstamos de socio
#has n, :prestamos, :child_key => [:sujeto_id]
#has n, :domicilios, :class_name => 'Address', :through => Resource, :child_key => [:subject_id]
#has n, :addresses, :through => Resource, :child_key => [:subject_id]
#
# Propiedades principales
property :id, Serial
property :rfc, String, :unique => true, :length => 13
property :ref_banamex, String#, :nullable => false
property :ref_bancomer, String#, :nullable => false
property :ref_hsbc, String#, :nullable => false
# Propiedades especiales
# Para tener Single Table Inheritance
property :tipo, Discriminator#, :nullable => false
# Propiedades comunes de marca de tiempo
tiene_marcas_de_tiempo
# Consejos para métodos (Advice, Aspect Oriented Programming)
before :save, :crea_referencias_bancarias
def crea_referencias_bancarias
# Las creamos sólo si se trata de un registro nuevo
return true unless self.new_record?
self.ref_banamex = referencia_banamex(self.attribute_get(:id))
self.ref_bancomer = referencia_bancomer(self.attribute_get(:id))
self.ref_hsbc = referencia_hsbc(self.attribute_get(:id))
end
def self.identifica_clase(rfc)
return nil if rfc.nil?
return Persona if rfc.match(/^[A-Z]{4}\d{6}([A-Z]|\d){3}$/)
return Empresa if rfc.match(/^[A-Z]{3}\d{6}([A-Z]|\d){3}$/)
nil
end
def referencia_banamex(id)
referencia = "#{id}".ljust(8, '0')
digitos = [37, 31, 29, 23, 19, 17, 13, 11]
cuenta = [2, 8, 8, 4, 7 ,5 ,0]
sucursal = [0, 7, 8, 0]
suma = 0
0.upto(7) do |i|
suma += (digitos[i] * referencia[i])
suma += (digitos[i] * cuenta[i]) if (i < 7)
suma += (digitos[i] * sucursal[i]) if (i < 4)
end
numero = (99 - suma % 97).to_s.rjust(2, '0')
referencia + numero
end
def referencia_bancomer(id)
referencia = "#{id}".rjust(6, '0')
suma = 0
0.upto(5) do |i|
t = referencia[i]
(t *= 2; t -= 9) if (i.odd? && referencia[i] > 4)
suma += t
end
digito = suma.to_s.reverse[0,1].to_i
numero = (digito > 0 ? 10 - digito : 0 ).to_s
referencia + numero
end
def referencia_hsbc(id)
referencia = "#{id}".rjust(9, '0')
secuencia = "12122121212121212121212121212121212121212121212121212"
iterador = secuencia.length - referencia.length
#iterador = 27
suma = iterador + 1
# suma = 28
0.upto(8) do |i|
t = secuencia[iterador] * referencia[i]
suma += (t >= 10 ? 1 + t - 10 : t)
iterador += 1
end
digito = suma.to_s.reverse[0,1].to_i
numero = (digito > 0 ? 10 - digito : 0 ).to_s
referencia + numero
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment