Sha256: 7929576fbde4d46848cde0d81f769807cc4322defbe43ca8e1d591aa98b7273a

Contents?: true

Size: 1.69 KB

Versions: 65

Compression:

Stored size: 1.69 KB

Contents

module Comee
  module Core
    class StockTransferRequest < ApplicationRecord
      before_create :set_reference_number

      belongs_to :from, class_name: "Comee::Core::Store", optional: true
      belongs_to :to, class_name: "Comee::Core::FulfillmentCenter", optional: true
      belongs_to :initiated_by, class_name: "Comee::Core::User", optional: true
      belongs_to :sales_order
      has_many :stock_transfer_request_items

      validates :reference_number, uniqueness: true
      validates :reference_number, :reference_date, :due_date, :status, presence: true
      validate :validate_due_date_is_after_reference_date, if: -> { due_date.present? && reference_date.present? }
      enum status: {
        draft: 0,
        issued: 1,
        accepted: 2,
        transferred: 3
      }
      def validate_due_date_is_after_reference_date
        return if due_date > reference_date

        errors.add(:due_date, "must be after reference date")
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[created_at due_date from_id id id_value initiated_by_id reference_date reference_number remark
           sales_order_id status to_id updated_at]
      end

      def self.ransackable_associations(_auth_object = nil)
        %w[from initiated_by sales_order to]
      end

      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 = "STR-#{number}"

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

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
comee_core-0.3.21 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.20 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.19 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.18 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.17 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.16 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.15 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.14 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.13 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.12 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.11 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.10 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.9 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.8 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.7 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.6 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.5 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.4 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.3 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.2 app/models/comee/core/stock_transfer_request.rb