Sha256: 25feebe55d28f98765807942947f4c248e858c9f076d9e7e6a7d1bb2039854ae

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module Comee
  module Core
    class PurchaseOrder < ApplicationRecord
      before_validation :generate_po_number, if: proc { |po| po.po_number.nil? }
      before_save :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 calculate_total_price
        items = PurchaseOrderItem.where(purchase_order_id: id)
        self.total_price = items.sum(&:total_price)
      end

      def calculate_vat
        return unless supplier.tax_code == "Inland"

        items = PurchaseOrderItem.where(purchase_order_id: id)
        total_price = items.sum(&:total_price)
        self.vat = (total_price * 0.19).round(2)
      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

2 entries across 2 versions & 1 rubygems

Version Path
comee_core-0.3.17 app/models/comee/core/purchase_order.rb
comee_core-0.3.16 app/models/comee/core/purchase_order.rb