Sha256: 8d3d00093c9e44d88efe87a96961b7355c9623a382246431706af55d2e08a8a5

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

class Unico::RegistrationCadastralCertificate < Unico::Model
  self.abstract_class = true
  self.table_name = :unico_registration_cadastral_certificates

  belongs_to :creditor, class_name: '::Creditor'

  delegate :id, :name, :address, :neighborhood, :email, :city, :state, :country,
           :zip_code, :phone, :fax, :cnpj, :state_registration, :responsible,
           :responsible_identity_document, :main_cnae_code, :main_cnae, :cnaes,
           :documents, to: :creditor, prefix: true, allow_nil: true

  validates :fiscal_year, :specification, :creditor, presence: true
  validates :registration_date, :validity_date, presence: true
  validates :commercial_registry_registration_date, timeliness: { type: :date, on: :create }, allow_blank: true
  validates :registration_date,
            timeliness: {
              on_or_before: :today,
              on_or_before_message: :should_be_on_or_before_today,
              type: :date,
              on: :create
            }, allow_blank: true
  validates :revocation_date,
            timeliness: {
              on_or_after: :registration_date,
              on_or_after_message: :should_be_on_or_after_registration_date,
              type: :date
            }, allow_blank: true
  validates :fiscal_year, mask: '9999', allow_blank: true
  validate :creditor_must_be_company

  orderize :fiscal_year, :id
  filterize

  def self.same_fiscal_year_and_creditor_and_less_than_or_equal_me(some_fiscal_year, some_creditor_id, some_id)
    where(fiscal_year: some_fiscal_year)
      .where(creditor_id: some_creditor_id)
      .where(arel_table[:id].lteq(some_id))
  end

  def to_s
    "#{count_crc}/#{fiscal_year}"
  end

  def count_crc
    RegistrationCadastralCertificate.same_fiscal_year_and_creditor_and_less_than_or_equal_me(fiscal_year, creditor_id, id).count
  end

  protected

  def creditor_must_be_company
    return unless creditor

    errors.add(:creditor, :invalid) unless creditor.company?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unico-training-7.8.0 app/models/unico/registration_cadastral_certificate.rb