<?php

namespace App\Models;

use CodeIgniter\Model;

class EmployeePayrollInfoModel extends Model
{
    protected $table            = 'emp_pay_info';
    protected $primaryKey       = 'emppay_id';
    protected $useAutoIncrement = true;
    protected $returnType       = \App\Entities\EmployeePayrollInfo::class;
    protected $useSoftDeletes   = true;
    protected $protectFields    = true;
    protected $allowedFields    = ['employee_id', 
                                   'paytype_id', 
                                   'is_ATM', 
                                   'savings_account',
                                   'work_days',
                                   'basic_sal_computation',
                                   'basic_monthly_pay',
                                   'basic_semi_monthly_pay',
                                   'basic_daily_pay',
                                   'basic_hourly_pay',
                                   'has_cola',
                                   'has_philhealth',
                                   'has_hdmf', 
                                   'has_sss', 
                                   'has_gsis'];

    protected bool $allowEmptyInserts = false;

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = ['assignCreatedBy'];
    protected $afterInsert    = [];
    protected $beforeUpdate   = ['assignUpdatedBy'];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];


    public function assignCreatedBy(array $data)
    {
        $data['data']['created_by'] = auth()->user()->employee_id;
        return $data;
    }

    public function assignUpdatedBy(array $data)
    {
        $data['data']['updated_by'] = auth()->user()->employee_id;
        return $data;
    }

    public function getAllEmpPayInfoXEmpPayType()
    {
        $builder = $this->db->table('emp_pay_info');
        $builder->select('*');
        $builder->join('employee', 'employee.employee_id = emp_pay_info.employee_id');
        $builder->join('pay_type', 'pay_type.paytype_id = emp_pay_info.paytype_id');
        $builder->where('emp_pay_info.deleted_at', null);
        return $builder->get()->getResult();
    }

    public function getEmpPayInfoXEmpPayTypeByEmpID($empID)
    {
        $builder = $this->db->table('emp_pay_info');
        $builder->select('*');
        $builder->join('employee', 'employee.employee_id = emp_pay_info.employee_id');
        $builder->join('pay_type', 'pay_type.paytype_id = emp_pay_info.paytype_id');
        $builder->where(['emp_pay_info.employee_id' => $empID,
                         'emp_pay_info.deleted_at' => null]);
        return $builder->get()->getRow();
    }

    public function getEmpPayInfoXEmpPayTypeByPayGrpId($payGroupId, $payTypeId)
    {
        $builder = $this->db->table('emp_pay_info');
        $builder->select('*');
        $builder->join('employee', 'employee.employee_id = emp_pay_info.employee_id');
        $builder->join('pay_type', 'pay_type.paytype_id = emp_pay_info.paytype_id');
        $builder->where(['employee.pay_group_id' => $payGroupId, 
                         'emp_pay_info.paytype_id' => $payTypeId,
                         'emp_pay_info.deleted_at' => null]);
        return $builder->get()->getResult();
    }

    public function getEmpPayInfoXEmpPayTypeByEmpPayId($emppayid)
    {
        $builder = $this->db->table('emp_pay_info');
        $builder->select('*');
        $builder->join('employee', 'employee.employee_id = emp_pay_info.employee_id');
        $builder->join('pay_type', 'pay_type.paytype_id = emp_pay_info.paytype_id');
        $builder->where(['emp_pay_info.emppay_id' => $emppayid, 
                         'emp_pay_info.deleted_at' => null]);
        return $builder->get()->getRow();
    }
}
