Class: HrLite::Admin::LoansController

Inherits:
SuperadminController show all
Defined in:
app/controllers/hr_lite/admin/loans_controller.rb

Overview

Loans and salary advances. Money tier: the instalment lands as a deduction on a payslip, so whoever sets it is setting somebody's pay.

Instance Method Summary collapse

Instance Method Details

#cancelObject



40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/hr_lite/admin/loans_controller.rb', line 40

def cancel
  loan = Loan.find(params[:id])
  if loan.loan_repayments.exists?
    return redirect_to admin_loan_path(loan),
                       alert: "Instalments have already been deducted. Close it instead — " \
                              "cancelling would deny money that has left somebody's salary."
  end

  loan.update!(status: "cancelled")
  redirect_to admin_loans_path, notice: "Cancelled before any deduction."
end

#closeObject

Closing stops the deduction. It does NOT delete the repayments — those are on published payslips and are what the outstanding balance is derived from.



34
35
36
37
38
# File 'app/controllers/hr_lite/admin/loans_controller.rb', line 34

def close
  loan = Loan.find(params[:id])
  loan.update!(status: "closed")
  redirect_to admin_loans_path, notice: "Closed — no further deductions."
end

#createObject



16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/hr_lite/admin/loans_controller.rb', line 16

def create
  @loan = Loan.new(loan_params.merge(approved_by_id: hr_current_user.id))
  if @loan.save
    redirect_to admin_loans_path,
                notice: "Loan recorded for #{HrLite.display_name(@loan.user)}" \
                        "deductions start #{@loan.starts_on.strftime('%b %Y')}."
  else
    render :new, status: :unprocessable_entity
  end
end

#indexObject



6
7
8
9
10
# File 'app/controllers/hr_lite/admin/loans_controller.rb', line 6

def index
  @status = params[:status].presence_in(Loan::STATUSES) || "active"
  @loans = paginate(Loan.includes(:user).where(status: @status).order(starts_on: :desc))
  @outstanding = Loan.active.includes(:loan_repayments).sum(BigDecimal(0), &:outstanding)
end

#newObject



12
13
14
# File 'app/controllers/hr_lite/admin/loans_controller.rb', line 12

def new
  @loan = Loan.new(starts_on: Date.current.next_month.beginning_of_month)
end

#showObject



27
28
29
# File 'app/controllers/hr_lite/admin/loans_controller.rb', line 27

def show
  @loan = Loan.includes(:loan_repayments).find(params[:id])
end