Sha256: 270850987d34e49dd0b8882d379dd98c1599e17b9d7a6ec961a8b8ab9e62ac98

Contents?: true

Size: 1.52 KB

Versions: 16

Compression:

Stored size: 1.52 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).round(2)
      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

16 entries across 16 versions & 1 rubygems

Version Path
comee_core-0.3.33 app/models/comee/core/purchase_order.rb
comee_core-0.3.32 app/models/comee/core/purchase_order.rb
comee_core-0.3.31 app/models/comee/core/purchase_order.rb
comee_core-0.3.30 app/models/comee/core/purchase_order.rb
comee_core-0.3.29 app/models/comee/core/purchase_order.rb
comee_core-0.3.28 app/models/comee/core/purchase_order.rb
comee_core-0.3.27 app/models/comee/core/purchase_order.rb
comee_core-0.3.26 app/models/comee/core/purchase_order.rb
comee_core-0.3.25 app/models/comee/core/purchase_order.rb
comee_core-0.3.24 app/models/comee/core/purchase_order.rb
comee_core-0.3.23 app/models/comee/core/purchase_order.rb
comee_core-0.3.22 app/models/comee/core/purchase_order.rb
comee_core-0.3.21 app/models/comee/core/purchase_order.rb
comee_core-0.3.20 app/models/comee/core/purchase_order.rb
comee_core-0.3.19 app/models/comee/core/purchase_order.rb
comee_core-0.3.18 app/models/comee/core/purchase_order.rb