<?php

namespace App\Models;

use CodeIgniter\Model;

class Penalty extends Model
{
    protected $table = 'penalty_poin';
    protected $primaryKey = 'id';
    protected $allowedFields =  ['id', 'id_pegawai', 'kode_pegawai', 'poin', 'reason', 'tgl_absen', 'kode_absen'];

    public function getPenaltyData($id = null)
    {
        if ($id) {
            return $this->where('id', $id)->first(); // Mengambil satu lembur berdasarkan ID
        }

        return $this->orderBy('tgl_absen', 'DESC')->findAll(); // Mengambil semua lembur dan mengurutkan berdasarkan tanggal
    }
    public function updatePenalty($data, $id)
    {
        return $this->update($id, $data);
    }

    public function getTotalPenalty($start_date, $end_date, $id)
    {
        return $this->select('SUM(poin) as total_poin')
                    ->where('tgl_absen >=', $start_date)
                    ->where('tgl_absen <=', $end_date)
                    ->where('id_pegawai', $id)
                    ->first();
    }
}