Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 96
DailyAttendanceReportController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
756
0.00% covered (danger)
0.00%
0 / 96
 index
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 11
 create
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 store
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 show
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 edit
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 update
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 destroy
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getDailyAttendanceReport
0.00% covered (danger)
0.00%
0 / 1
240
0.00% covered (danger)
0.00%
0 / 71
 convertToHoursMins
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 8
<?php
namespace App\Http\Controllers\Reports;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Admin\Team;
use App\Models\Auth\User;
use DB;
use Carbon\Carbon;
use GetShiftDetailHelper;
use App\Helpers\AttendanceHelper;
use Gate;
class DailyAttendanceReportController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = '/Reports/Daily Attendance Report';
        
        if(auth()->user()->can('View Org Daily Attendance Report')){
           $teams =Team::all();
        }else if(auth()->user()->can('View My Team Daily Attendance Report')){
            $teams =  GetShiftDetailHelper::getMyteams();
        }else{
            $teams=[];
        }
        // For Attendance Marking
        $val = AttendanceHelper::getAttendanceMarkValue();
        $valTeam = AttendanceHelper::getAttendanceTeam($request);
        $valUser = AttendanceHelper::getAttendanceUser($request);
        return view('Reports.DailyAttendance.DailyAttendanceReport',compact('teams','title',
                    'val','valTeam','valUser'));
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public function getDailyAttendanceReport(Request $request)
    {
        $selectedDate = $request->fromDate;
       // $date = Carbon::parse($selectedDate ,Config('app.timezone'));
       // $startDate = Carbon::parse($selectedDate ,Config('app.timezone'))->toDateString();
        $date = Carbon::parse(Carbon::createFromFormat( config('settings.php_date_format') , $selectedDate ),Config('app.timezone'));
        //$startDate = Carbon::parse(Carbon::createFromFormat( config('settings.php_date_format') , $selectedDate ),Config('app.timezone'))->toDateString();
        $selectedDateWithTimezone = Carbon::createFromFormat( config('settings.php_date_format') , $selectedDate )->toDateString();
       // $dateDayAdded = $date->addDay()->toDateString();
        $selectedEndDateWithTimezone = Carbon::createFromFormat( config('settings.php_date_format') , $selectedDate )->addDay()->toDateString();
        
             
        $startDate = Carbon::parse($selectedDateWithTimezone.'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone'));
        $dateDayAdded = Carbon::parse($selectedEndDateWithTimezone.'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone'));
              
        $teamId = $request->teamId;
        $columnHeaders = collect(['Employee Name','Team Name','Check In','check In Location','Check Out','Check Out Location','Hours']);
        if($teamId == 0)
        {   
            $teamQuery = '';
            if(auth()->user()->can('View My Team Daily Attendance Report')  &&
                !auth()->user()->can('View Org Daily Attendance Report') ){
                    $getTeamIds =  GetShiftDetailHelper::getMyteamIds();
                    $teamQuery = 'and  t.id in (' . implode(',', $getTeamIds) . ')';
            }
            if(auth()->user()->can('View My Daily Attendance Report') &&
            !auth()->user()->can('View Org Daily Attendance Report') &&
            !auth()->user()->can('View My Team Daily Attendance Report')){
                // $attendanceReport =  DB::select('select  concat(us.name," ",us.lastName) as name,t.team,a.check_in as check_in,
                //                                         a.check_out as check_out
                //                                         from users us 
                //                                         join user_details u on us.id = u.user_id
                //                                         join team_user_detail tsd on  tsd.user_detail_id = u.id
                //                                         join teams t on t.id = tsd.team_id
                //                                         left join attendance_details a on a.user_id = u.user_id
                //                                         AND  (a.check_in between :startDate1  and :endDate1)
                //                                         where us.deleted_at IS  NULL AND us.id ='.auth()->id(),
                //                                         ['startDate1'=>$startDate,'endDate1'=> $dateDayAdded]);
                $attendanceReport =  DB::select('select  concat(us.name," ",us.lastName) as name,t.team,a.check_in as check_in,
                                                            a.check_in_location_address as check_in_location_address,a.check_out as check_out,
                                                            a.check_out_location_address as check_out_location_address
                                                            from users us 
                                                            join user_details u on us.id = u.user_id
                                                            join teams t on t.id = u.primary_team_id
                                                            left join attendance_details a on a.user_id = u.user_id
                                                            AND  (a.check_in between :startDate1  and :endDate1)
                                                            where us.deleted_at IS  NULL AND us.id ='.auth()->id(),
                                                            ['startDate1'=>$startDate,'endDate1'=> $dateDayAdded]);
                }else{
                // $attendanceReport =  DB::select('select  concat(us.name," ",us.lastName) as name,t.team,a.check_in as check_in,
                //                                         a.check_out as check_out
                //                                         from users us 
                //                                         join user_details u on us.id = u.user_id
                //                                         join team_user_detail tsd on  tsd.user_detail_id = u.id
                //                                         join teams t on t.id = tsd.team_id
                //                                         left join attendance_details a on a.user_id = u.user_id
                //                                         AND  (a.check_in between :startDate1  and :endDate1)
                //                                         where us.deleted_at IS  NULL '.$teamQuery,
                //                                         ['startDate1'=>$startDate,'endDate1'=> $dateDayAdded]);
                
                $attendanceReport =  DB::select('select  concat(us.name," ",us.lastName) as name,t.team,a.check_in as check_in,
                                                            a.check_in_location_address as check_in_location_address,a.check_out as check_out,
                                                            a.check_out_location_address as check_out_location_address
                                                            from users us 
                                                            join user_details u on us.id = u.user_id
                                                            join teams t on t.id = u.primary_team_id
                                                            left join attendance_details a on a.user_id = u.user_id
                                                            AND  (a.check_in between :startDate1  and :endDate1)
                                                            where us.deleted_at IS  NULL '.$teamQuery,
                                                            ['startDate1'=>$startDate,'endDate1'=> $dateDayAdded]);                                       
            }
            
        }
        else
        {
            // $attendanceReport =  DB::select('select  concat(us.name," ",us.lastName) as name,t.team,a.check_in as check_in,
            //                                             a.check_out  as check_out
            //                                             from users us
            //                                             join user_details u on us.id = u.user_id
            //                                             join team_user_detail tsd on  tsd.user_detail_id = u.id
            //                                             join teams t on t.id = tsd.team_id
            //                                             left join attendance_details a on a.user_id = u.user_id
            //                                             AND  ((a.check_in between :startDate1 and :endDate1))
            //                                             where  us.deleted_at IS  NULL and  tsd.team_id in ('.$teamId.') order by us.id,a.id',
            //                                             ['startDate1'=>$startDate,'endDate1'=>$dateDayAdded]);
            if (auth()->user()->can('View Team Attendance Report')){
            $attendanceReport =  DB::select('select  concat(us.name," ",us.lastName) as name,t.team,a.check_in as check_in,
                                                            a.check_in_location_address as check_in_location_address,a.check_out as check_out,
                                                            a.check_out_location_address as check_out_location_address
                                                            from users us
                                                            join user_details u on us.id = u.user_id
                                                            join teams t on t.id = u.primary_team_id
                                                            left join attendance_details a on a.user_id = u.user_id
                                                            AND  ((a.check_in between :startDate1 and :endDate1))
                                                            where  us.deleted_at IS  NULL and  u.primary_team_id in ('.$teamId.') order by us.id,a.id',
                                                            ['startDate1'=>$startDate,'endDate1'=>$dateDayAdded]);
            }else{
            // $attendanceReport =  DB::select('select  concat(us.name," ",us.lastName) as name,t.team,a.check_in as check_in,
            //                                                 a.check_in_location_address as check_in_location_address,a.check_out as check_out,
            //                                                 a.check_out_location_address as check_out_location_address
            //                                                 from users us 
            //                                                 join user_details u on us.id = u.user_id
            //                                                 join teams t on t.id = u.primary_team_id
            //                                                 left join attendance_details a on a.user_id = u.user_id
            //                                                 AND  (a.check_in between :startDate1  and :endDate1)
            //                                                 where us.deleted_at IS  NULL AND us.id ='.auth()->id(),
            //                                                 ['startDate1'=>$startDate,'endDate1'=> $dateDayAdded]);
            $attendanceReport = DB::select('SELECT
                                    CONCAT(us.name, " ", us.lastName) AS name,
                                    t.team,
                                    a.check_in AS check_in,
                                    a.check_in_location_address AS check_in_location_address,
                                    a.check_out AS check_out,
                                    a.check_out_location_address AS check_out_location_address
                                FROM
                                    users us
                                JOIN
                                    user_details u ON us.id = u.user_id
                                JOIN
                                    teams t ON t.id = u.primary_team_id
                                LEFT JOIN
                                    attendance_details a ON a.user_id = u.user_id
                                    AND (a.check_in BETWEEN :startDate1 AND :endDate1)
                                WHERE
                                    us.deleted_at IS NULL
                                    AND us.id = :userId
                                ORDER BY
                                    a.updated_at DESC
                                LIMIT 1',
                                [
                                    'startDate1' => $startDate,
                                    'endDate1' => $dateDayAdded,
                                    'userId' => auth()->id(),
                                ]);
            }
        }
     $attendanceReportColl = collect($attendanceReport);
       $attendanceReportTempColl = collect([]);
            foreach($attendanceReportColl as $data)
            {
                 $attendance = collect([]);
                 $attendance->name = $data->name;
                 $attendance->team = $data->team;
                if(isset($data->check_in))
                {
                    $checkInDate = Carbon::parse($data->check_in);
                    $checkInDate->setTimezone(config('settings.time_zone'));
                    //$attendance->check_in =  $checkInDate->isoFormat('H:mm');
                     $attendance->check_in =  date(Config('settings.time_format'), strtotime($checkInDate));
                }
                else{
                    $attendance->check_in = '';
                }
                if(isset($data->check_in_location_address)){
                    $attendance->check_in_location_address =$data->check_in_location_address ;
                }else{
                    $attendance->check_in_location_address = '';
                }
                if(isset($data->check_out))
                {
                    $checkOutDate = Carbon::parse($data->check_out);
                    $checkOutDate->setTimezone(config('settings.time_zone'));
                    //$attendance->check_out =  $checkOutDate->isoFormat('HH:mm');
                    $attendance->check_out = date(Config('settings.time_format'), strtotime($checkOutDate));
                }
                else{
                    $attendance->check_out = '';
                }
                if(isset($data->check_out_location_address)){
                    $attendance->check_out_location_address =$data->check_out_location_address ;
                }else{
                    $attendance->check_out_location_address = '';
                }
                if(isset($checkOutDate) && isset($checkInDate))
                    {
                        $minutes = $checkInDate->diffInMinutes($checkOutDate);
                        $attendance->hours = $this->convertToHoursMins($minutes);
                    }
                    else
                    {
                        $attendance->hours = '';
                    }
                $attendanceArr = array(
                    "name" => $attendance->name,
                    "team" => $attendance->team,
                    "check_in" => $attendance->check_in,
                    "check_in_location_address" => $attendance->check_in_location_address,
                    "check_out" => $attendance->check_out,
                    "check_out_location_address" => $attendance->check_out_location_address,
                    "hours" => $attendance->hours
                  );
                $attendanceReportTempColl->push($attendanceArr);
                $checkOutDate = null;
                $checkInDate = null;
            }
        $attendanceReportColl = collect([]);
        $attendanceReportColl = $attendanceReportTempColl;
        $teams =Team::all();
       return view('Reports.DailyAttendance.DailyAttendanceTable', compact('teams','attendanceReportColl','columnHeaders','selectedDate'));
    }
  public function convertToHoursMins($minutes) {
        if ($minutes < 1) {
            $time = '0:01';
        }
        $hours = floor($minutes / 60);
        $minutes = ($minutes % 60);
        if($minutes < 10)
        {
            $minutes = '0'.$minutes;
        }
        $time = $hours.':'.$minutes;
        return $time;
    }
}