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.62 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.61 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.60 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.59 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.58 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.57 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.56 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.55 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.54 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.53 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.51 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.50 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.49 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.48 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.47 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.46 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.45 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.44 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.43 app/models/comee/core/stock_transfer_request.rb
comee_core-0.3.42 app/models/comee/core/stock_transfer_request.rb