Sha256: e4b4b4b26d723f2421fdf730c3df627e72daad2a2da0a57475ba1bcf196e91ab

Contents?: true

Size: 699 Bytes

Versions: 17

Compression:

Stored size: 699 Bytes

Contents

module OrderValidations
  extend ActiveSupport::Concern

  prepended do
    validate :check_tax_id, if: :email_required?
    before_update :format_tax_id

    def check_tax_id
      cnpj_allowed = SolidusBrazilianAdaptations.config.allow_cnpj
      document = TaxIdBr.new(tax_id)
      if document.valid?
        if document.document_type == :cnpj && cnpj_allowed == false
          errors.add(:base, "Insira um CPF, não um CNPJ.")
        end
      else
        errors.add(:base, "#{cnpj_allowed ? "CPF/CPNJ" : "CPF"} inválido.")
      end
    end

    def format_tax_id
      document = TaxIdBr.new(tax_id)
      self.tax_id = document.formatted
    end
  end

  ::Spree::Order.prepend self
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
solidus_brazilian_adaptations-1.3.1 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.3.0 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.2.0 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.10 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.9 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.8 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.7 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.6 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.5 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.4 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.3 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.2 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.1 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.1.0 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.0.9 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.0.8 app/overrides/order_validations.rb
solidus_brazilian_adaptations-1.0.7 app/overrides/order_validations.rb