<?php

namespace App\Models;

use CodeIgniter\Model;

class PayrollModel extends Model
{
    protected $table = 'payrolls';

    protected $primaryKey = 'id_payroll';

    protected $returnType = 'array';

    protected $useTimestamps = true;

    protected $createdField = 'created_at';

    protected $updatedField = 'updated_at';

    protected $allowedFields = [

        'id_user',

        'periode',

        'gaji_pokok',

        'bonus',

        'potongan',

        'total_gaji',

        'status'

    ];

    public function getPayrollWithUser()
    {
        return $this->select('
                payrolls.*,
                users.nama_lengkap
            ')
            ->join('users', 'users.id_user = payrolls.id_user')
            ->orderBy('periode', 'DESC')
            ->findAll();
    }

    public function totalPayroll()
    {
        $row = $this->selectSum('total_gaji')->first();

        return $row['total_gaji'] ?? 0;
    }

    public function payrollPeriode($periode)
    {
        return $this->where('periode', $periode)->findAll();
    }
}