<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class PayrollEntry extends Model
{
    protected $fillable = [
        'payroll_period_id', 'branch_id', 'user_id', 'payslip_number', 'employee_number_snapshot',
        'employee_name_snapshot', 'position_snapshot', 'basic_salary', 'allowances', 'bonus', 'commission',
        'overtime', 'deductions', 'advances', 'other_deductions', 'gross', 'total_deductions', 'net',
        'status', 'financial_account_id', 'financial_account_transaction_id', 'accounting_journal_number',
        'paid_by', 'paid_at',
    ];

    protected function casts(): array
    {
        return [
            'basic_salary' => 'decimal:2', 'allowances' => 'decimal:2', 'bonus' => 'decimal:2',
            'commission' => 'decimal:2', 'overtime' => 'decimal:2', 'deductions' => 'decimal:2',
            'advances' => 'decimal:2', 'other_deductions' => 'decimal:2', 'gross' => 'decimal:2',
            'total_deductions' => 'decimal:2', 'net' => 'decimal:2', 'paid_at' => 'datetime',
        ];
    }

    public function period(): BelongsTo { return $this->belongsTo(PayrollPeriod::class, 'payroll_period_id'); }
    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function employee(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); }
    public function financialAccount(): BelongsTo { return $this->belongsTo(FinancialAccount::class); }
    public function accountTransaction(): BelongsTo { return $this->belongsTo(FinancialAccountTransaction::class, 'financial_account_transaction_id'); }
    public function payer(): BelongsTo { return $this->belongsTo(User::class, 'paid_by'); }
}
