class BillingInformation < ActiveRecord::Base
  ##--- Associaciones
  belongs_to :customer
  belongs_to :supplier
  belongs_to :spmx_county, :foreign_key => "county_id"
  belongs_to :spmx_state, :foreign_key => "state_id"

  ##--- Llevar registro de Actividad del usuario
  # audited

  enum status: [:erased, :active, :inactive]

  ##--- Validaciones previas de guardar
  validates_presence_of :name, message: "Debe capturar el nombre de la razón social."
  validates :rfc, :presence => {message: "Debe capturar el RFC del cliente."}, length:{ maximum: 13, too_long: "El RFC no debe ser mayor a %{count} caracteres." }
  validates_presence_of :state_id, message: "Debe especificar el estado."
  validates_presence_of :county_id, message: "Debe especificar el municipio."
  # validates_presence_of :city, message: "Debe especificar la ciudad o localidad."
  # validates_presence_of :suburb, message: "Debe especificar la colonia."
  # validates_presence_of :address, message: "Debe capturar la dirección de la Empresa."
  validates :zipcode, :presence => { :message => "Debe especificar el código postal"},  :numericality => { :only_integer => true, :message => "El código postal debe ser numérico.", allow_nil: true }
  validates :num_ext,  length: { maximum: 10, too_long: "El no. ext no debe ser mayor a %{count} caracteres." }, :unless => Proc.new {|c| c.num_ext.blank?}
  validates :num_int, length: { maximum: 10, too_long: "El no. int no debe ser mayor a %{count} caracteres." }, :unless => Proc.new {|c| c.num_int.blank?}

  ##--- Funciones personalizadas
  def address_complete
    "#{self.address} No. #{num_ext}" + (self.num_int != '' ? "Int. #{num_int}," : ",") +" C.P. #{zipcode} \n #{suburb}, #{city}, #{spmx_county.name}, #{spmx_state.name} "
  end
end
