<?php

namespace App\Models;

use CodeIgniter\Model;

class EmployeePayTransactionModel extends Model
{
    protected $table            = 'emp_pay_trans';
    protected $primaryKey       = 'emppaytrans_id';
    protected $useAutoIncrement = true;
    protected $returnType       = \App\Entities\EmployeePayTransaction::class;
    protected $useSoftDeletes   = true;
    protected $protectFields    = true;
    protected $allowedFields    = ['paytrans_id', 
                                   'company_id', 
                                   'branch_code', 
                                   'dept_id', 
                                   'job_title_id', 
                                   'pay_group_id', 
                                   'emp_status_id', 
                                   'employee_id', 
                                   'company_issued_id', 
                                   'last_name', 
                                   'first_name', 
                                   'middle_name', 
                                   'suffix', 
                                   'email_address', 
                                   'is_ATM', 
                                   'savings_account', 
                                   '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', 
                                   'actual_work_days',
                                   'basic_pay',
                                   'gross_income', 
                                   'taxable_income',
                                   'nontaxable_income',
                                   'income_tax',
                                   'total_deduction', 
                                   'taxable_deduction',
                                   'nontaxable_deduction',
                                   'net_pay'];

    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 getEmpPayTransByPayTransIdGroupId($paytransid, $paygroupid)
    {
        $builder = $this->db->table('emp_pay_trans');
        $builder->select('*');
        $builder->join('pay_trans', 'pay_trans.paytrans_id = emp_pay_trans.paytrans_id');
        $builder->join('company_branch', 'company_branch.branch_code = emp_pay_trans.branch_code');
        $builder->join('company_dept', 'company_dept.dept_id = emp_pay_trans.dept_id');
        $builder->join('job_title', 'job_title.job_title_id = emp_pay_trans.job_title_id');
        $builder->join('pay_group', 'pay_group.pay_group_id = emp_pay_trans.pay_group_id');
        $builder->join('emp_status', 'emp_status.emp_status_id = emp_pay_trans.emp_status_id');
        $builder->join('employee', 'employee.employee_id = emp_pay_trans.employee_id');
        $builder->where(['emp_pay_trans.paytrans_id' => $paytransid,
                         'emp_pay_trans.pay_group_id' => $paygroupid,
                         'emp_pay_trans.deleted_at' => null]);
        $builder->orderBy('employee.last_name', 'ASC');
        return $builder->get()->getResult();
    }
    public function getEmpPayTransInDedByPayTransIdGroupId($paytransid, $paygroupid)
    {
        $builder = $this->db->table('emp_pay_trans');
        $builder->select('*');
        $builder->join('pay_trans', 'pay_trans.paytrans_id = emp_pay_trans.paytrans_id');
        $builder->join('company_branch', 'company_branch.branch_code = emp_pay_trans.branch_code');
        $builder->join('company_dept', 'company_dept.dept_id = emp_pay_trans.dept_id');
        $builder->join('job_title', 'job_title.job_title_id = emp_pay_trans.job_title_id');
        $builder->join('pay_group', 'pay_group.pay_group_id = emp_pay_trans.pay_group_id');
        $builder->join('emp_status', 'emp_status.emp_status_id = emp_pay_trans.emp_status_id');
        $builder->join('employee', 'employee.employee_id = emp_pay_trans.employee_id');
        $builder->join('emp_pay_trans_inded', 'emp_pay_trans_inded.emppaytrans_id = emp_pay_trans.emppaytrans_id');
        $builder->where(['emp_pay_trans.paytrans_id' => $paytransid,
                         'emp_pay_trans.pay_group_id' => $paygroupid,
                         'emp_pay_trans.deleted_at' => null,
                         'emp_pay_trans_inded.deleted_at' => null]);
        $builder->orderBy('employee.last_name', 'ASC');
        return $builder->get()->getResult();
    }

    public function changeActualDaysWorked($emppaytransid, $actualworkdays)
    {
        $builder = $this->db->table('emp_pay_trans');
        $builder->set('actual_work_days', $actualworkdays);
        $builder->where('emppaytrans_id', $emppaytransid);
        return $builder->update();
    }
}
