<?php

namespace App\Console\Commands;

use App\Models\Company;
use App\Models\Employee;
use Illuminate\Console\Command;

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

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

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->daliy_salary();
    }

    public function daliy_salary(){

        $month_days = date('t'); 
        $companies = Company::all();
        foreach($companies as $company){
            $employees = $company->employees;
            $work_employees = $employees->where('work_status','working');

            foreach($work_employees as $employee){
                $employee_data = Employee::findOrFail($employee->id);
                $employee_salary_by_day = $employee_data->salary / $month_days;
                $employee_data->update([
                    'balance' => $employee_data->balance+$employee_salary_by_day,
                ]);
            }

        }
        
    }
}
