class Admin::Accounting::Students::Billing::TemplatesController < Admin::Accounting::Controller
  include Admin::Accounting::Students::BillingScoped

  def create
    template_details.each do |detail_id|
      bill_plan.plan_transactions.find_or_initialize_by(detail_id: detail_id)
    end

    if bill_plan.save
      render_success :ok, message: 'Template attached.'
    else
      render_error :unprocessable_entity, errors: bill_plan
    end
  end

  private
    def template
      @template ||= current_school.billing_templates.find_by(id: params[:id])
    end

    def template_details
      template.template_transactions.pluck(:detail_id)
    end

    def school_year
      template.school_year
    end
end
