<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;

// Models
use App\Models\EmployeePayTransactionModel;
use App\Models\PayrollTransactionModel;
use App\Models\EmployeeModel;
use App\Models\EmpPayTransIncomeDeductionModel;

// Entities
use App\Entities\EmployeePayTransaction;
use App\Entities\PayrollTransaction;
use App\Entities\Employee;
use App\Entities\EmpPayTransIncomeDeduction;

class RegularEmployeeController extends BaseController
{
    public function index()
    {
        return redirect()->to(base_url('/login'));
    }

    public function payslipView()
    {
        $data['paytransid'] = $this->request->getGet('paytransid');
        $data['payTransactions'] = (new PayrollTransactionModel())->orderBy('paytype_id','DESC')->limit(15)->findAll();
        $data['loggedEmployee'] = (new EmployeeModel())->where('employee_id', auth()->user()->sys_emp_id)->first();

        if($data['paytransid'] != null)
        {
            $data['empPayTrans'] = (new EmployeePayTransactionModel())->where(['paytrans_id' => $data['paytransid'], 'employee_id'=>auth()->user()->sys_emp_id])->first();

            if($data['empPayTrans'] != null)
            {
                $empPayTransInDedModel = new EmpPayTransIncomeDeductionModel();
                $data['empPayTransIncomes'] = $empPayTransInDedModel->where(["emppaytrans_id"=>$data['empPayTrans']->emppaytrans_id, "is_income"=>true])->findAll();
                $data['empPayTransDeductions'] = $empPayTransInDedModel->where(["emppaytrans_id"=>$data['empPayTrans']->emppaytrans_id, "is_income"=>false])->findAll();
            }
        }

        return view('regemp/payslipview', $data);
    }
    public function payslipViewPrint($paytransid)
    {
        $data['loggedEmployee'] = (new EmployeeModel())->where('employee_id', auth()->user()->sys_emp_id)->first();
        $data['payTransaction'] = (new PayrollTransactionModel())->find($paytransid);
        $data['empPayTrans'] = (new EmployeePayTransactionModel())->where(['paytrans_id' => $paytransid, 'employee_id'=>auth()->user()->sys_emp_id])->first();

        if($data['empPayTrans'] != null)
        {
            $empPayTransInDedModel = new EmpPayTransIncomeDeductionModel();
            $data['empPayTransIncomes'] = $empPayTransInDedModel->where(["emppaytrans_id"=>$data['empPayTrans']->emppaytrans_id, "is_income"=>true])->findAll();
            $data['empPayTransDeductions'] = $empPayTransInDedModel->where(["emppaytrans_id"=>$data['empPayTrans']->emppaytrans_id, "is_income"=>false])->findAll();
        }

        return view('regemp/payslipviewprint', $data);
    }
}
