<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class EmployeePayroll extends Model
{
    protected $fillable = [
        'employee_id',
        'year',
        'month',
        'gross_salary',
        'deductions',
        'net_salary',
        'paid_at',
        'status',

        // Breakdown columns added by migration 000004
        'ot_hours',
        'ot_amount',
        'award',
        'absent_days',
        'absent_amount',
        'advance_deduction',
        'loan_deduction',
        'other_deduction',
    ];

    protected $casts = [
        'gross_salary'      => 'decimal:2',
        'deductions'        => 'decimal:2',
        'net_salary'        => 'decimal:2',
        'ot_hours'          => 'decimal:2',
        'ot_amount'         => 'decimal:2',
        'award'             => 'decimal:2',
        'absent_days'       => 'decimal:2',
        'absent_amount'     => 'decimal:2',
        'advance_deduction' => 'decimal:2',
        'loan_deduction'    => 'decimal:2',
        'other_deduction'   => 'decimal:2',
        'paid_at'           => 'datetime',
    ];

    public function employee()
    {
        return $this->belongsTo(Employees::class);
    }

    public function scopeForMonthYear($query, $month, $year)
    {
        return $query->where('month', $month)->where('year', $year);
    }

    public function isPaid(): bool
    {
        return $this->status === 'paid';
    }
}