<?php
namespace App\Models;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
class Employee extends BaseModel {
    use SoftDeletes;
    protected $fillable = [
        'company_id','parent_id','code','name','display_name','salutation','first_name','middle_name','last_name',
        'father_name','mother_name','gender','religion','nationality','spouse_name','date_of_birth','marital_status',
        'blood_group','designation','department','project','branch','email','phone','mobile','nid_no','passport_no',
        'emergency_contact','emergency_phone','investment_amount','present_address','permanent_address','reference_1',
        'reference_2','joining_date','release_date','date_of_confirmation','duty_type','date_of_resign','reason_for_resign',
        'terminated_employee','payslip_send_method','highest_education','last_organization','last_increment_date',
        'last_gross_salary','salary_payment_method','bank_name','branch_name','bank_account','account_type',
        'employment_notes','tax_id_tin','is_active',
    ];
    protected $casts = [
        'is_active' => 'boolean',
        'terminated_employee' => 'boolean',
        'joining_date' => 'date',
        'release_date' => 'date',
        'date_of_confirmation' => 'date',
        'date_of_resign' => 'date',
        'date_of_birth' => 'date',
        'last_increment_date' => 'date',
        'investment_amount' => 'decimal:2',
        'last_gross_salary' => 'decimal:2',
    ];
    public function company()  { return $this->belongsTo(Company::class); }
    public function parent()   { return $this->belongsTo(Employee::class,'parent_id'); }
    public function children() { return $this->hasMany(Employee::class,'parent_id'); }
    public function salaryLines() { return $this->hasMany(EmployeeSalaryLine::class); }
    public function documents() { return $this->hasMany(EmployeeDocument::class); }

    public function earningsLines()
    {
        return $this->hasMany(EmployeeSalaryLine::class)->where('line_type', 'earning')->orderBy('sort_order');
    }

    public function deductionLines()
    {
        return $this->hasMany(EmployeeSalaryLine::class)->where('line_type', 'deduction')->orderBy('sort_order');
    }

    public function irregularLines()
    {
        return $this->hasMany(EmployeeSalaryLine::class)->where('line_type', 'irregular')->orderBy('sort_order');
    }
}
