CasperSecurity
<?php
namespace App\Http\Livewire\Documentmanagement;
use Livewire\Component;
class ReportLivewire extends Component
{
public function dailyAttendanceBulk(){
//dd($this->startOfMonth);
if( $this->startOfMonth==null){
$this->dispatchBrowserEvent('closemodal');
$this->errorstartmsg();
}
elseif($this->endOfMonth==null){
$this->dispatchBrowserEvent('closemodal');
$this->errorendmsg();
}
else{
$this->bulkreport=[];
$holidayDates =[];
$leaveDates =[];
//dd($this->filter_type);
if($this->filter_type!=""){
if($this->filter_type=="by_dept"){
if($this->search_info){
$team = Team::where('display_name', 'LIKE', '%' . $this->search_info . '%')->first();
// dd($team);
if($team!=null){
$users=EmployeeRegistration::where('team_id',$team->id)->where('status',1)->get();
}
else{
$users=[];
}
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_name"){
if($this->search_info){
$users=EmployeeRegistration::where('fullname', 'LIKE', '%' . $this->search_info . '%')->get();
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_user_id"){
if($this->search_info){
$users=EmployeeRegistration::where('user_id', 'LIKE', '%' . $this->search_info . '%')->get();
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_gender"){
if($this->search_info){
$users=EmployeeRegistration::where('gender',$this->search_info )->get();
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_bloodgrp"){
if($this->search_info){
$users=EmployeeRegistration::where('blood_group', 'LIKE', '%' . $this->search_info . '%')->get();
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_location"){
if($this->search_info){
$users=EmployeeRegistration::where('location', 'LIKE', '%' . $this->search_info . '%')->get();
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_dob"){
$users = EmployeeRegistration::whereMonth('date_of_birth', '>=', Carbon::parse($this->startOfMonth)->month)
->whereMonth('date_of_birth', '<=', Carbon::parse($this->endOfMonth)->month)
->get();
}
elseif($this->filter_type=="by_salary"){
if($this->search_info){
$salary = SalaryGroup::where('salary_group_name', 'LIKE', '%' . $this->search_info . '%')->first();
if ($salary) {
$assgn = AssignDepartments::where('salary_group', $salary->salary_group_name)->get();
$users = collect(); // Initialize an empty collection
foreach ($assgn as $assignment) {
$user = EmployeeRegistration::find($assignment->employee_id);
if ($user) {
$users->push($user);
}
}
// dd($users);
}
else{
$users=[];
}
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
}
else{
$users=EmployeeRegistration::where('status',1)
->get();
}
$holidays = Holiday::where(function ($query) {
$query->where('start_dt', '<=', $this->endOfMonth)
->where('end_dt', '>=', $this->startOfMonth);
})
->orWhere(function ($query) {
$query->where('start_dt', '<=', $this->startOfMonth)
->where('end_dt', '>=', $this->startOfMonth);
})
->orWhere(function ($query) {
$query->where('start_dt', '>=', $this->startOfMonth)
->where('start_dt', '<=', $this->endOfMonth)
->where('end_dt', '>', $this->endOfMonth);
})
->where('status', 1)
->get();
foreach ($holidays as $holiday) {
$startDt = max($this->startOfMonth, $holiday->start_dt);
$endDt = min($this->endOfMonth, $holiday->end_dt);
$datesInRange = CarbonPeriod::create($startDt, $endDt)->toArray();
$formattedDates = array_map(function ($date) {
return $date->format('Y-m-d');
}, $datesInRange);
$holidayDates = array_merge($holidayDates, $formattedDates);
}
//dd($holidayDates);
$uniqueHolidayDates = array_unique($holidayDates);
//dd($uniqueHolidayDates);
$leaves = ManageLeave::where(function ($query) {
$query->where('from_date', '<=', $this->endOfMonth)
->where('to_date', '>=', $this->startOfMonth);
})
->orWhere(function ($query) {
$query->where('from_date', '<=', $this->startOfMonth)
->where('to_date', '>=', $this->startOfMonth);
})
->orWhere(function ($query) {
$query->where('from_date', '>=', $this->startOfMonth)
->where('from_date', '<=', $this->endOfMonth)
->where('to_date', '>', $this->endOfMonth);
})
->where('status', 1)
->get();
foreach ($leaves as $leave) {
$startDt = max($this->startOfMonth, $leave->from_date);
$endDt = min($this->endOfMonth, $leave->to_date);
$datesInRange = CarbonPeriod::create($startDt, $endDt)->toArray();
$formattedDates = array_map(function ($date) {
return $date->format('Y-m-d');
}, $datesInRange);
$leaveDates = array_merge($leaveDates, $formattedDates);
}
//dd($holidayDates);
$uniqueleaveDates = array_unique($leaveDates);
//dd($uniqueleaveDates);
$start = Carbon::parse($this->startOfMonth);
$end = Carbon::parse($this->endOfMonth);
$dateArray = [];
while ($start <= $end) {
$dateArray[] = $start->format('Y-m-d');
$start->addDay(); // Move to the next day
}
foreach($users as $user){
//dd($user->fullname);
//$this->rep_of="";
$this->rep_of=$user->fullname;
$dailyUserData=[];
$this->totalhours=0;
foreach($dateArray as $date){
//dd($user->id);
// $this->totalhours=0;
$dateCarbon = Carbon::parse($date);
$emp_attendances=EmployeeAttendance::where('employee_id', $user->id)
->whereDate('date',$date)
->where('attendance_time','!=',null)
//->where('attendance_out_time','!=',null)
->orderBy('date', 'asc')
->latest()->first();
if($emp_attendances !=null){
//dd($emp_attendances->working_hours);
$this->totalhours+=$emp_attendances->working_hours;
}
//dd($emp_attendances);
if ($emp_attendances != null) {
$entryhm=substr($emp_attendances->attendance_time, 0, 5);
$exithm=substr($emp_attendances->attendance_out_time, 0, 5);
$status = in_array($date, $uniqueHolidayDates) ? "Holiday(comp_off)" : ($dateCarbon->isWeekend() ? "Weekday(comp_off)" : "Present");
$dailyUserData[] = [
"date"=>$date,
"entry" => $entryhm,
"exit" => $exithm,
"status"=>$status,
"hours" => $emp_attendances->working_hours
];
}
else {
$status = in_array($date, $uniqueHolidayDates)
? "Holiday"
: ($dateCarbon->isWeekend()
? "Weekday"
: (in_array($date, $uniqueleaveDates)
? "Leave"
: "Absent"));
$dailyUserData[] = [
"date"=>$date,
"entry" => "NA",
"exit" => "NA",
"status"=>$status,
"hours" => "NA"
];
}
}
//dd($this->totalhours);
$this->bulkreport[$user->fullname] = $dailyUserData;
// $this->bulkreport[$user->user_id] = $dailyUserData;
}
$this->dispatchBrowserEvent('bulk_modal');
}
}
public function render()
{
return view('livewire.documentmanagement.report-livewire');
}
}