<?php

namespace App\Models;

use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;

class PayrollItem extends Model
{
    use BelongsToTenant;

    protected $fillable = ['tenant_id', 'payroll_run_id', 'employee_id', 'salary_structure_id', 'basic_salary', 'housing_allowance', 'transport_allowance', 'other_earnings', 'overtime_amount', 'bonus_amount', 'gross_salary', 'absence_deduction', 'unpaid_leave_deduction', 'loan_deduction', 'other_deductions', 'total_deductions', 'net_salary', 'working_days', 'present_days', 'unpaid_leave_days', 'absence_days', 'overtime_minutes', 'calculation_snapshot', 'status'];

    protected $casts = ['basic_salary' => 'decimal:2', 'housing_allowance' => 'decimal:2', 'transport_allowance' => 'decimal:2', 'other_earnings' => 'decimal:2', 'overtime_amount' => 'decimal:2', 'bonus_amount' => 'decimal:2', 'gross_salary' => 'decimal:2', 'absence_deduction' => 'decimal:2', 'unpaid_leave_deduction' => 'decimal:2', 'loan_deduction' => 'decimal:2', 'other_deductions' => 'decimal:2', 'total_deductions' => 'decimal:2', 'net_salary' => 'decimal:2', 'unpaid_leave_days' => 'decimal:2', 'calculation_snapshot' => 'array'];

    public function run()
    {
        return $this->belongsTo(PayrollRun::class, 'payroll_run_id');
    }

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

    public function salaryStructure()
    {
        return $this->belongsTo(EmployeeSalaryStructure::class, 'salary_structure_id');
    }

    public function components()
    {
        return $this->hasMany(PayrollItemComponent::class);
    }

    public function adjustments()
    {
        return $this->hasMany(PayrollAdjustment::class);
    }
}
