Sha256: a13c6fb769f5ae30562fbd57c394b716f2c724053e198d671cb7c573b87c9865

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

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

      validates :purchase_price, :selling_price, :pp_valid_from, :pp_valid_to, :sp_valid_from, :sp_valid_to, presence: true
      validates :margin, presence: true, numericality: {greater_than_or_equal_to: 0, less_than_or_equal_to: 100}
      validates :product_id, uniqueness: {scope: [:supplier_id, :previous_price_id]}
      validate :validate_primary_price, :validate_purchase_price_validity_dates, :validate_selling_price_validity_dates

      def validate_primary_price
        return unless product && primary

        count = MasterPrice.where(product:, primary: true, active: true).count
        errors.add(:base, "There is already a primary supplier for item '#{product.code}'") if count.positive?
      end

      def validate_purchase_price_validity_dates
        return unless pp_valid_from && pp_valid_to

        period = Period.new(pp_valid_from, pp_valid_to)
        errors.add(:base, "Purchase price validity date range is not correct.") unless period.valid?
      end

      def validate_selling_price_validity_dates
        return unless sp_valid_from && sp_valid_to

        period = Period.new(sp_valid_from, sp_valid_to)
        errors.add(:base, "Selling price validity date range is not correct.") unless period.valid?
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
comee_core-0.1.22 app/models/comee/core/master_price.rb
comee_core-0.1.21 app/models/comee/core/master_price.rb
comee_core-0.1.20 app/models/comee/core/master_price.rb
comee_core-0.1.19 app/models/comee/core/master_price.rb
comee_core-0.1.18 app/models/comee/core/master_price.rb
comee_core-0.1.17 app/models/comee/core/master_price.rb