Sha256: 2bd9af63465525a1317c9dad2cbcdc248e345f114d458a4d4f09671dbf99fbf0

Contents?: true

Size: 685 Bytes

Versions: 5

Compression:

Stored size: 685 Bytes

Contents

module Comee
  module Core
    class ReorderReport < ApplicationRecord
      before_create :set_reference_number
      belongs_to :store
      has_many :reorder_report_items

      validates :report_date, presence: true
      validates :reference_number, uniqueness: true

      private

      def set_reference_number
        self.reference_number = generate_reference_number
      end

      def generate_reference_number
        loop do
          number = SecureRandom.random_number(10000000)
          reference_number = "RR-#{number}"

          break reference_number unless ReorderReport.where(reference_number: reference_number).exists?
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
comee_core-0.3.1 app/models/comee/core/reorder_report.rb
comee_core-0.3.0 app/models/comee/core/reorder_report.rb
comee_core-0.2.99 app/models/comee/core/reorder_report.rb
comee_core-0.2.98 app/models/comee/core/reorder_report.rb
comee_core-0.2.97 app/models/comee/core/reorder_report.rb