Sha256: aabea7cf61440b7ca583720a749a64b7264d1642cc2e4d50905589bf76d33ab8

Contents?: true

Size: 1.27 KB

Versions: 36

Compression:

Stored size: 1.27 KB

Contents

module Comee
  module Core
    class PurchaseOrder < ApplicationRecord
      before_validation :generate_po_number, if: proc { |po| po.po_number.nil? }
      before_save :total_price, :set_terms

      belongs_to :supplier
      has_many :purchase_order_items

      validates :po_number, uniqueness: true
      validates :order_date, :delivery_address, :status, :delivery_date, presence: true
      enum status: {
        draft: 0,
        submitted: 1,
        approved: 2,
        confirmed: 3,
        accepted: 4,
        canceled: 5
      }

      def total_price
        items = PurchaseOrderItem.where(purchase_order_id: id)
        self.total_price = items.sum(&:total_price)
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[
          id
          po_number
          supplier_id
          unit_id
          order_date
          status
          delivery_address
          delivery_date
          order_reference
        ]
      end

      def generate_po_number
        self.po_number = Util.generate_number("PurchaseOrder", "po_number")
      end

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

      def set_terms
        return if terms

        self.terms = supplier.payment_terms
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
comee_core-0.3.15 app/models/comee/core/purchase_order.rb
comee_core-0.3.14 app/models/comee/core/purchase_order.rb
comee_core-0.3.13 app/models/comee/core/purchase_order.rb
comee_core-0.3.12 app/models/comee/core/purchase_order.rb
comee_core-0.3.11 app/models/comee/core/purchase_order.rb
comee_core-0.3.10 app/models/comee/core/purchase_order.rb
comee_core-0.3.9 app/models/comee/core/purchase_order.rb
comee_core-0.3.8 app/models/comee/core/purchase_order.rb
comee_core-0.3.7 app/models/comee/core/purchase_order.rb
comee_core-0.3.6 app/models/comee/core/purchase_order.rb
comee_core-0.3.5 app/models/comee/core/purchase_order.rb
comee_core-0.3.4 app/models/comee/core/purchase_order.rb
comee_core-0.3.3 app/models/comee/core/purchase_order.rb
comee_core-0.3.2 app/models/comee/core/purchase_order.rb
comee_core-0.3.1 app/models/comee/core/purchase_order.rb
comee_core-0.3.0 app/models/comee/core/purchase_order.rb
comee_core-0.2.99 app/models/comee/core/purchase_order.rb
comee_core-0.2.98 app/models/comee/core/purchase_order.rb
comee_core-0.2.97 app/models/comee/core/purchase_order.rb
comee_core-0.2.96 app/models/comee/core/purchase_order.rb