Sha256: d0f1131b28fc6d65f90c667686365932c3ee2267b764ae1de35f552d7ff92cbc

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

module Comee
  module Core
    class PurchaseOrderItem < ApplicationRecord
      default_scope { order(serial_no: :asc) }

      before_save :calculate_total_price, :calculate_confirmation_values
      after_save :update_purchase_order
      after_destroy :update_purchase_order

      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).round(2)
      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).round(2)
      end

      def update_purchase_order
        purchase_order.calculate_total_price
        purchase_order.calculate_vat
        purchase_order.save!
      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

13 entries across 13 versions & 1 rubygems

Version Path
comee_core-0.3.49 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.48 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.47 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.46 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.45 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.44 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.43 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.42 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.41 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.40 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.39 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.38 app/models/comee/core/purchase_order_item.rb
comee_core-0.3.37 app/models/comee/core/purchase_order_item.rb