class Admin::Accounting::AllocationsController < Admin::Accounting::Controller
  def index
    data = transaction.allocations
      .includes(increase: [{ subcategory: :category }, :student, :accounting_transaction])

    render_success :ok, json: data.map { |a| allocation_props(a) }
  end

  private
    def transaction
      @transaction ||= current_school.accounting_transactions.find_by(id: params[:transaction_id])
    end

    def allocation_props(allocation)
      {
        id: allocation.id,
        increase_id: allocation.increase_id,
        decrease_id: allocation.decrease_id,
        amount: allocation.decorate.amount,
        category: allocation.increase.subcategory.decorate.title,
        student: allocation.increase.student.full_name(:reverse),
        memo: allocation.increase.accounting_transaction.memo,
        increase_date: allocation.increase.accounting_transaction.posted_on,
        increase_amount: allocation.increase.accounting_transaction.decorate.currency
      }
    end
end
