<?php

namespace App\Console\Commands;

use App\Models\BankInfo;
use App\Models\Payroll;
use App\Models\PayrollEmployee;
use App\Models\PayrollTransaction;
use Illuminate\Console\Command;

class add_payroll_employee_id_to_payroll_transactions_using_bank_account extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:addeid_payment';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $records = PayrollTransaction::whereNull('payroll_employee_id')->get();
        foreach ($records as $key => $record) {
            // find employ with same account number. 
            $bankInfo = BankInfo::where('account_number', $record->destination_account)->first();
            if ($bankInfo) {
                // 
                $payrollData = PayrollEmployee::where('employee_id', $bankInfo->user_id)->where('payroll_id', $record->payroll_id)->first();
                if ($payrollData) {
                    $record->payroll_employee_id = $payrollData->id;
                    $record->save();
                }
            }
        }
    }
}
