<?php

namespace App\Models;

use CodeIgniter\Model;

class EmpPayIncomeDeductionModel extends Model
{
    protected $table            = 'emp_pay_inded';
    protected $primaryKey       = 'emppayinded_id';
    protected $useAutoIncrement = true;
    protected $returnType       = \App\Entities\EmpPayIncomeDeduction::class;
    protected $useSoftDeletes   = true;
    protected $protectFields    = true;
    protected $allowedFields    = ['emppay_id',
                                   'payschedule_id',
                                   'inded_id',
                                   'is_fixed_amt',
                                   'is_percent_amt',
                                   'worked_days_based',
                                   'amount',
                                   'is_override',
                                  ];

    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 getEmpPayInDedByEmpPayIdSchedIdIsIncome($empPayId, $paySchedId, $isIncome)
    {
        $builder = $this->db->table('emp_pay_inded');
        $builder->select('*');
        $builder->join('pay_income_deduction', 'pay_income_deduction.inded_id = emp_pay_inded.inded_id');
        $builder->where([
            'emp_pay_inded.emppay_id' => $empPayId, 
            'pay_income_deduction.is_income' => $isIncome,
            'emp_pay_inded.payschedule_id' => $paySchedId,
            'emp_pay_inded.deleted_at' => null
        ]);
        return $builder->get()->getResult();
    }

    public function getEmpPayInDedByEmpPayIdSchedId($empPayId, $paySchedId)
    {
        $builder = $this->db->table('emp_pay_inded');
        $builder->select('*');
        $builder->join('pay_income_deduction', 'pay_income_deduction.inded_id = emp_pay_inded.inded_id');
        $builder->where([
            'emp_pay_inded.emppay_id' => $empPayId, 
            'emp_pay_inded.payschedule_id' => $paySchedId,
            'emp_pay_inded.deleted_at' => null
        ]);
        return $builder->get()->getResult();
    }
}
