class Billing::Template < ApplicationRecord
  belongs_to :school
  belongs_to :school_year

  has_many :template_transactions, dependent: :destroy
  has_many :transaction_details, through: :template_transactions, source: :detail
  has_many :plan_transactions, through: :template_transactions

  before_destroy :check_students_attached, prepend: true

  scope :by_school_year, ->(id) { where(school_year_id: id) if id }

  private
    def check_students_attached
      return unless plan_transactions.exists?

      errors.add(:base, 'Cannot delete if students are attached')
      throw(:abort)
    end
end
