<?php

namespace App\Models;

use CodeIgniter\Model;

class EmpPayTransIncomeDeductionModel extends Model
{
    protected $table            = 'emp_pay_trans_inded';
    protected $primaryKey       = 'emppaytransinded_id';
    protected $useAutoIncrement = true;
    protected $returnType       = \App\Entities\EmpPayTransIncomeDeduction::class;
    protected $useSoftDeletes   = true;
    protected $protectFields    = true;
    protected $allowedFields    = ['emppaytrans_id', 
                                   'inded_id',
                                   'payslip_display',
                                   'inded_name',
                                   'coa_code',
                                   'is_income',
                                   'is_taxable',
                                   'include_in_gross',
                                   'is_fixed_amt',
                                   'is_percent_amt',
                                   'worked_days_based',
                                   'amount',
                                   'base_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 getEmpPayTransInDedxInDedByEmpTransId($emptransid)
    {
        $builder = $this->db->table('emp_pay_trans_inded');
        $builder->select('*');
        $builder->join('pay_income_deduction', 'pay_income_deduction.inded_id = emp_pay_trans_inded.inded_id');
        $builder->where(['emp_pay_trans_inded.emppaytrans_id' => $emptransid,
                         'emp_pay_trans_inded.deleted_at' => null]);
        return $builder->get()->getResult();
    }

    public function getEmpPayTransInDedxInDedByEmpTransIdIsIncome($emptransid, $isincome)
    {
        $builder = $this->db->table('emp_pay_trans_inded');
        $builder->select('*');
        $builder->join('pay_income_deduction', 'pay_income_deduction.inded_id = emp_pay_trans_inded.inded_id');
        $builder->where(['emp_pay_trans_inded.emppaytrans_id' => $emptransid,
                         'emp_pay_trans_inded.is_income' => $isincome,
                         'emp_pay_trans_inded.deleted_at' => null]);
        return $builder->get()->getResult();
    }*/

    public function getTotalIncomeDeduction($emppaytransid, $isincome, $istaxable)
    {
        $builder = $this->db->table('emp_pay_trans_inded');
        $builder->selectSum('amount');
        $builder->where(['emppaytrans_id'=>$emppaytransid,
                         'is_income'=>$isincome,
                         'is_taxable'=>$istaxable,
                         'deleted_at'=>null]);
        $result = $builder->get()->getRow();

        if($result->amount == null) return 0;
        else return $result->amount;
    }

    public function getEmpPayTransInDedxEmPayTransByPayTransIdInDedId($paytransid, $indedid)
    {
        $builder = $this->db->table('emp_pay_trans_inded');
        $builder->select('*');
        $builder->join('emp_pay_trans', 'emp_pay_trans.emppaytrans_id = emp_pay_trans_inded.emppaytrans_id');
        $builder->where([
            'emp_pay_trans.paytrans_id' => $paytransid,
            'emp_pay_trans_inded.inded_id' => $indedid,
            'emp_pay_trans_inded.deleted_at' => null
        ]);
        return $builder->get()->getResult();
    }

    public function getEmpPayTransInDedCountxEmPayTransPayTransByInDedId($indedid)
    {
        $builder = $this->db->table('emp_pay_trans_inded');
        $builder->select(['pay_trans.paytrans_id',
                          'pay_trans.paytype_id', 
                          'pay_trans.payschedule_id', 
                          'pay_trans.payroll_from', 
                          'pay_trans.payroll_to', 
                          'pay_trans.no_of_days', 
                          'pay_trans.total_emp', 
                          'pay_trans.total_gross', 
                          'pay_trans.remarks',
                          'pay_trans.is_open']);
        $builder->selectCount('emp_pay_trans_inded.emppaytransinded_id', 'emp_count');
        $builder->join('emp_pay_trans', 'emp_pay_trans.emppaytrans_id = emp_pay_trans_inded.emppaytrans_id');
        $builder->join('pay_trans', 'pay_trans.paytrans_id = emp_pay_trans.paytrans_id');
        $builder->where([
            'emp_pay_trans_inded.inded_id' => $indedid,
            'emp_pay_trans_inded.deleted_at' => null
        ]);
        $builder->groupBy(['pay_trans.paytrans_id', 
                           'pay_trans.paytype_id', 
                           'pay_trans.payschedule_id', 
                           'pay_trans.payroll_from',
                           'pay_trans.payroll_to',
                           'pay_trans.no_of_days',
                           'pay_trans.total_emp',
                           'pay_trans.total_gross',
                           'pay_trans.remarks', 
                           'pay_trans.is_open']);
        return $builder->get()->getResult();
    }
}
