CasperSecurity
<?php
namespace App\Console\Commands;
use App\Models\EmployeeLeaveBalance;
use App\Models\LeaveRule;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
class EmployeeLeaveCreditAutomate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'leavecredit:automate';
/**
* The console command description.
*
* @var string
*/
protected $description = 'It will automate the process of Leave Credit';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$leave_ruleinfos = LeaveRule::where('status', 1)->get();
foreach ($leave_ruleinfos as $key => $info) {
if ($info) {
$emp_per = User::where('status', 1)->where('id', '!=', '1')->get();
if (sizeof($emp_per) > 0) {
/* $att_dep=$info['attendance_dependent'];
if($att_dep!=null){
$att_days=$info['attendance_days'];
if($att_days>=20){
$leave_credit=$att_days/20;
}
}
else{
} */
foreach ($emp_per as $emp) {
//dd($emp);
$emp_id = $emp['user_id'];
if ($emp_id != null) {
$leavetype = $info['leave_type_id'];
$leavecreditdays = $info['credit_no_days'];
if ($info['credit_period'] == '12') {
$startyeardate = Carbon::now()->startOfYear()->format('Y-m-d');
$endyeardate = Carbon::now()->endOfYear()->format('Y-m-d');
$leavebalance = EmployeeLeaveBalance::where('emp_id', $emp_id)
->where('leave_type', $leavetype)
->whereBetween('leave_credit_date', [$startyeardate, $endyeardate])
->where('status', 1)
->latest()->first();
if ($leavebalance) {
} else {
$previousyearstart = Carbon::now()->subYears('1')->startOfYear()->format('Y-m-d');
$previousyearend = Carbon::now()->subYears('1')->endOfYear()->format('Y-m-d');
$leavebalanceprev = EmployeeLeaveBalance::where('emp_id', $emp_id)
->where('leave_type', $leavetype)
->whereBetween('leave_credit_date', [$previousyearstart, $previousyearend])
->where('status', 1)
->latest()->first();
if ($info['carry_forwarded'] == '1' && $leavebalanceprev != "") {
$iscarryfowward = false;
$monthstart = Carbon::now()->startOfMonth()->format('Y-m-d');
$monthend = Carbon::now()->endOfMonth()->format('Y-m-d');
if ($info['carry_forward_date'] >= $monthstart && $info['carry_forward_date'] <= $monthend) {
if ($info['max_days'] != "") {
if ($info['after_carry_forward_lapsed'] == '1') {
$leavebalance = $leavebalanceprev['leave_balance'] - $info['max_days'];
if ($leavebalance < 0) {
} else {
$iscarryfowward = true;
}
}
if ($iscarryfowward) {
$leavebalanceprev->status = 0;
if ($leavebalanceprev->save()) {
$empleavebal = new EmployeeLeaveBalance();
$empleavebal->emp_id = $emp_id;
$empleavebal->leave_type = $leavetype;
$empleavebal->leave_credit_date = Carbon::now()->format('Y-m-d');
$empleavebal->leave_credit = $leavecreditdays;
$empleavebal->leave_balance = $leavecreditdays + $info['max_days'];
$empleavebal->save();
}
} else {
$leavebalanceprev->status = 0;
if ($leavebalanceprev->save()) {
$empleavebal = new EmployeeLeaveBalance();
$empleavebal->emp_id = $emp_id;
$empleavebal->leave_type = $leavetype;
$empleavebal->leave_credit_date = Carbon::now()->format('Y-m-d');
$empleavebal->leave_credit = $leavecreditdays;
$empleavebal->leave_balance = $leavecreditdays + $leavebalanceprev['leave_balance'];
$empleavebal->save();
}
}
}
}
} else {
$empleavebal = new EmployeeLeaveBalance();
$empleavebal->emp_id = $emp_id;
$empleavebal->leave_type = $leavetype;
$empleavebal->leave_credit_date = Carbon::now()->format('Y-m-d');
$empleavebal->leave_credit = $leavecreditdays;
$empleavebal->leave_balance = $leavecreditdays;
$empleavebal->leave_balance_hour = 0;
$empleavebal->save();
}
}
}
}
}
}
}
}
return "Leave Credited";
}
}