Sha256: 90b3e72895eb4233638709cf21a8645c056d6ddb1bae4e550b4c3781794f8bd6

Contents?: true

Size: 1.55 KB

Versions: 10

Compression:

Stored size: 1.55 KB

Contents

module Comee
  module Core
    class PurchaseOrderItem < ApplicationRecord
      before_save :calculate_total_price, :calculate_confirmation_values

      has_one :purchase_order_item_diff

      enum status: {active: 0, canceled: 1}

      belongs_to :purchase_order
      belongs_to :product
      belongs_to :unit

      validates :quantity, presence: true, numericality: {greater_than: 0}
      validates :price, presence: true, numericality: {greater_than_or_equal_to: 0}
      validates :status, :delivery_date, presence: true
      validates :serial_no, presence: true, numericality: {greater_than: 0, only_integer: true}

      delegate(:po_number, to: :purchase_order, prefix: false)

      def calculate_total_price
        self.total_price = price * quantity
      end

      def calculate_confirmation_values
        self.confirmed_price = price unless confirmed_price

        self.confirmed_quantity = quantity unless confirmed_quantity

        self.confirmed_delivery_date = delivery_date unless confirmed_delivery_date

        self.confirmed_total_price = confirmed_price * confirmed_quantity
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[
          id
          purchase_order_id
          unit_id
          quantity
          confirmed_quantity
          price
          confirmed_price
          delivery_date
          confirmed_delivery_date
          status
        ]
      end

      def self.ransackable_associations(_auth_object = nil)
        %w[
          purchase_order
          unit
        ]
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
comee_core-0.2.93 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.92 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.91 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.90 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.89 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.88 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.87 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.86 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.85 app/models/comee/core/purchase_order_item.rb
comee_core-0.2.84 app/models/comee/core/purchase_order_item.rb