Sha256: 218aa27bf993b154e07e756948cc56c2a90a0b58903c31cdccf289d96bac5ec7

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module Comee
  module Core
    class MasterPrice < Price
      belongs_to :supplier
      belongs_to :previous_price, class_name: "Comee::Core::MasterPrice", optional: true

      validates :purchase_price, :selling_price, presence: true
      validates :margin, presence: true, numericality: {greater_than_or_equal_to: 0, less_than_or_equal_to: 100}
      validates :product_id, uniqueness: {scope: %i[supplier_id previous_price_id]}
      validate :validate_primary_price

      scope :current_primary, -> { where(primary: true).current }
      scope :unapplied, -> { where(propagated_to_client: false).current_primary }

      def validate_primary_price
        return unless product && primary

        prices = MasterPrice.where(product: product, primary: true)
        unless prices.count.positive? && primary && MasterPrice.statuses[status] == MasterPrice.statuses[:current] && prices.first.id != id
          return
        end

        errors.add(:base, "There is already a primary price entry for item '#{product.code}'")
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[
          valid_from
          valid_to
          purchase_price
          selling_price
          status
          product_id
          supplier_id
          previous_price_id
        ]
      end

      def self.ransackable_associations(_auth_object = nil)
        %w[product supplier]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comee_core-0.1.35 app/models/comee/core/master_price.rb